B2G/gecko/browser/components/places/tests/browser/browser_425884.js
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
This Source Code Form is subject to the terms of the Mozilla Public
License, v.
This Source Code Form is subject to the terms of the Mozilla Public License v .
function test() {
  ok(PlacesUtils, "checking PlacesUtils, running in chrome context?");
  ok(PlacesUIUtils, "checking PlacesUIUtils, running in chrome context?");
  var toolbarId = PlacesUtils.toolbarFolderId;
  var toolbarNode = PlacesUtils.getFolderContents(toolbarId).root;
  var oldCount = toolbarNode.childCount;
  var testRootId = PlacesUtils.bookmarks.createFolder(toolbarId, "test root", -1);
  is(toolbarNode.childCount, oldCount+1, "confirm test root node is a container, and is empty");
  var testRootNode = toolbarNode.getChild(toolbarNode.childCount-1);
  testRootNode.QueryInterface(Ci.nsINavHistoryContainerResultNode);
  testRootNode.containerOpen = true;
  is(testRootNode.childCount, 0, "confirm test root node is a container, and is empty");
  var folderAId = PlacesUtils.bookmarks.createFolder(testRootId, "A", -1);
  populate(folderAId);
  var folderANode = PlacesUtils.getFolderContents(folderAId).root;
  validate(folderANode);
  is(testRootNode.childCount, 1, "create test folder");
  var serializedNode = PlacesUtils.wrapNode(folderANode, PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER);
  var rawNode = PlacesUtils.unwrapNodes(serializedNode, PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER).shift();
  ok(rawNode.type, "confirm json node");
  folderANode.containerOpen = false;
  var transaction = PlacesUIUtils.makeTransaction(rawNode,
                                                  PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER,
                                                  testRootId,
                                                  -1,
                                                  true);
  ok(transaction, "create transaction");
  PlacesUtils.transactionManager.doTransaction(transaction);
  is(testRootNode.childCount, 2, "create test folder via copy");
  var folderBNode = testRootNode.getChild(1);
  validate(folderBNode);
  PlacesUtils.transactionManager.undoTransaction();
  is(testRootNode.childCount, 1, "confirm undo removed the copied folder");
  PlacesUtils.transactionManager.redoTransaction();
  is(testRootNode.childCount, 2, "confirm redo re-copied the folder");
  folderBNode = testRootNode.getChild(1);
  validate(folderBNode);
  testRootNode.containerOpen = false;
  toolbarNode.containerOpen = false;
  PlacesUtils.transactionManager.undoTransaction();
  PlacesUtils.bookmarks.removeItem(folderAId);
}
function test ( ) { ok ( PlacesUtils , "checking PlacesUtils, running in chrome context?" ) ; ok ( PlacesUIUtils , "checking PlacesUIUtils, running in chrome context?" ) ; var toolbarId = PlacesUtils . toolbarFolderId ; var toolbarNode = PlacesUtils . getFolderContents ( toolbarId ) . root ; var oldCount = toolbarNode . childCount ; var testRootId = PlacesUtils . bookmarks . createFolder ( toolbarId , "test root" , - 1 ) ; is ( toolbarNode . childCount , oldCount + 1 , "confirm test root node is a container, and is empty" ) ; var testRootNode = toolbarNode . getChild ( toolbarNode . childCount - 1 ) ; testRootNode . QueryInterface ( Ci . nsINavHistoryContainerResultNode ) ; testRootNode . containerOpen = true ; is ( testRootNode . childCount , 0 , "confirm test root node is a container, and is empty" ) ; var folderAId = PlacesUtils . bookmarks . createFolder ( testRootId , "A" , - 1 ) ; populate ( folderAId ) ; var folderANode = PlacesUtils . getFolderContents ( folderAId ) . root ; validate ( folderANode ) ; is ( testRootNode . childCount , 1 , "create test folder" ) ; var serializedNode = PlacesUtils . wrapNode ( folderANode , PlacesUtils . TYPE_X_MOZ_PLACE_CONTAINER ) ; var rawNode = PlacesUtils . unwrapNodes ( serializedNode , PlacesUtils . TYPE_X_MOZ_PLACE_CONTAINER ) . shift ( ) ; ok ( rawNode . type , "confirm json node" ) ; folderANode . containerOpen = false ; var transaction = PlacesUIUtils . makeTransaction ( rawNode , PlacesUtils . TYPE_X_MOZ_PLACE_CONTAINER , testRootId , - 1 , true ) ; ok ( transaction , "create transaction" ) ; PlacesUtils . transactionManager . doTransaction ( transaction ) ; is ( testRootNode . childCount , 2 , "create test folder via copy" ) ; var folderBNode = testRootNode . getChild ( 1 ) ; validate ( folderBNode ) ; PlacesUtils . transactionManager . undoTransaction ( ) ; is ( testRootNode . childCount , 1 , "confirm undo removed the copied folder" ) ; PlacesUtils . transactionManager . redoTransaction ( ) ; is ( testRootNode . childCount , 2 , "confirm redo re-copied the folder" ) ; folderBNode = testRootNode . getChild ( 1 ) ; validate ( folderBNode ) ; testRootNode . containerOpen = false ; toolbarNode . containerOpen = false ; PlacesUtils . transactionManager . undoTransaction ( ) ; PlacesUtils . bookmarks . removeItem ( folderAId ) ; }
This Source Code Form is subject to the terms of the Mozilla Public
 License, v. 2.0.
This Source Code Form is subject to the terms of the Mozilla Public License v . 2 . 0 .
// sanity check /*
   Deep copy of bookmark data, using the front-end codepath:

   - create test folder A
   - add a subfolder to folder A, and add items to it
   - validate folder A (sanity check)
   - copy folder A, creating new folder B, using the front-end path
   - validate folder B
   - undo copy transaction
   - validate folder B (empty)
   - redo copy transaction
   - validate folder B's contents

  */ // create folder A, fill it, validate its contents // copy it, using the front-end helper functions // confirm serialization // confirm copy // validate the copy // undo the transaction, confirm the removal // redo the transaction // Close containers, cleaning up their observers. // clean up