browser_425884.js
Go to the documentation of this file.
1 /* vim:set ts=2 sw=2 sts=2 et: */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is Places test code.
16  *
17  * The Initial Developer of the Original Code is Mozilla Corp.
18  * Portions created by the Initial Developer are Copyright (C) 2008
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dietrich Ayala <dietrich@mozilla.com>
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37 
38 function test() {
39  // sanity check
40  ok(PlacesUtils, "checking PlacesUtils, running in chrome context?");
41  ok(PlacesUIUtils, "checking PlacesUIUtils, running in chrome context?");
42 
43  /*
44  Deep copy of bookmark data, using the front-end codepath:
45 
46  - create test folder A
47  - add a subfolder to folder A, and add items to it
48  - validate folder A (sanity check)
49  - copy folder A, creating new folder B, using the front-end path
50  - validate folder B
51  - undo copy transaction
52  - validate folder B (empty)
53  - redo copy transaction
54  - validate folder B's contents
55 
56  */
57 
58  var toolbarId = PlacesUtils.toolbarFolderId;
59  var toolbarNode = PlacesUtils.getFolderContents(toolbarId).root;
60 
61  var oldCount = toolbarNode.childCount;
62  var testRootId = PlacesUtils.bookmarks.createFolder(toolbarId, "test root", -1);
63  is(toolbarNode.childCount, oldCount+1, "confirm test root node is a container, and is empty");
64  var testRootNode = toolbarNode.getChild(toolbarNode.childCount-1);
65  testRootNode.QueryInterface(Ci.nsINavHistoryContainerResultNode);
66  testRootNode.containerOpen = true;
67  is(testRootNode.childCount, 0, "confirm test root node is a container, and is empty");
68 
69  // create folder A, fill it, validate its contents
70  var folderAId = PlacesUtils.bookmarks.createFolder(testRootId, "A", -1);
71  populate(folderAId);
72  var folderANode = PlacesUtils.getFolderContents(folderAId).root;
73  validate(folderANode);
74  is(testRootNode.childCount, 1, "create test folder");
75 
76  // copy it, using the front-end helper functions
77  var serializedNode = PlacesUtils.wrapNode(folderANode, PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER);
78  var rawNode = PlacesUtils.unwrapNodes(serializedNode, PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER).shift();
79  // confirm serialization
80  ok(rawNode.type, "confirm json node");
81  var transaction = PlacesUIUtils.makeTransaction(rawNode,
82  PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER,
83  testRootId,
84  -1,
85  true);
86  ok(transaction, "create transaction");
87  PlacesUIUtils.ptm.doTransaction(transaction);
88  // confirm copy
89  is(testRootNode.childCount, 2, "create test folder via copy");
90 
91  // validate the copy
92  var folderBNode = testRootNode.getChild(1);
93  validate(folderBNode);
94 
95  // undo the transaction, confirm the removal
96  PlacesUIUtils.ptm.undoTransaction();
97  is(testRootNode.childCount, 1, "confirm undo removed the copied folder");
98 
99  // redo the transaction
100  PlacesUIUtils.ptm.redoTransaction();
101  is(testRootNode.childCount, 2, "confirm redo re-copied the folder");
102  folderBNode = testRootNode.getChild(1);
103  validate(folderBNode);
104 
105  // Close containers, cleaning up their observers.
106  testRootNode.containerOpen = false;
107  toolbarNode.containerOpen = false;
108 
109  // clean up
110  PlacesUIUtils.ptm.undoTransaction();
111  PlacesUtils.bookmarks.removeItem(folderAId);
112 }
113 
114 function populate(aFolderId) {
115  var folderId = PlacesUtils.bookmarks.createFolder(aFolderId, "test folder", -1);
116  PlacesUtils.bookmarks.insertBookmark(folderId, PlacesUtils._uri("http://foo"), -1, "test bookmark");
117  PlacesUtils.bookmarks.insertSeparator(folderId, -1);
118 }
119 
120 function validate(aNode) {
121  asContainer(aNode);
122  aNode.containerOpen = true;
123  is(aNode.childCount, 1, "confirm child count match");
124  var folderNode = aNode.getChild(0);
125  is(folderNode.title, "test folder", "confirm folder title");
126  asContainer(folderNode);
127  folderNode.containerOpen = true;
128  is(folderNode.childCount, 2, "confirm child count match");
129  var bookmarkNode = folderNode.getChild(0);
130  var separatorNode = folderNode.getChild(1);
131  folderNode.containerOpen = false;
132  aNode.containerOpen = false;
133 }
var PlacesUIUtils
Definition: utils.js:85
function populate(aFolderId)
function test()
const Ci
function asContainer(aNode)
Definition: utils.js:82
function validate(aNode)