browser_457473_no_copy_guid.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  - create, a test folder, add bookmark, separator, livemark to it
45  - fetch guids for all
46  - copy the folder
47  - test that guids are all different
48  - undo copy
49  - redo copy
50  - test that guids for the copy stay the same
51  */
52 
53  var toolbarId = PlacesUtils.toolbarFolderId;
54  var toolbarNode = PlacesUtils.getFolderContents(toolbarId).root;
55 
56  var oldCount = toolbarNode.childCount;
57  var testRootId = PlacesUtils.bookmarks.createFolder(toolbarId, "test root", -1);
58  is(toolbarNode.childCount, oldCount+1, "confirm test root node is a container, and is empty");
59  var testRootNode = toolbarNode.getChild(toolbarNode.childCount-1);
60  asContainer(testRootNode);
61  testRootNode.containerOpen = true;
62  is(testRootNode.childCount, 0, "confirm test root node is a container, and is empty");
63 
64  // create folder A, fill it w/ each item type
65  var folderAId = PlacesUtils.bookmarks.createFolder(testRootId, "A", -1);
66  PlacesUtils.bookmarks.insertBookmark(folderAId, PlacesUtils._uri("http://foo"),
67  -1, "test bookmark");
68  PlacesUtils.bookmarks.insertSeparator(folderAId, -1);
69  PlacesUtils.livemarks.createLivemarkFolderOnly(folderAId, "test livemark",
70  PlacesUtils._uri("http://test"),
71  PlacesUtils._uri("http://test"), -1);
72 
73  var folderANode = testRootNode.getChild(0);
74  var folderAGUIDs = getGUIDs(folderANode);
75 
76  // test the test function
77  ok(checkGUIDs(folderANode, folderAGUIDs, true), "confirm guid test works");
78 
79  // serialize the folder
80  var serializedNode = PlacesUtils.wrapNode(folderANode, PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER);
81  var rawNode = PlacesUtils.unwrapNodes(serializedNode, PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER).shift();
82  ok(rawNode.type, "confirm json node was made");
83 
84  // Create a copy transaction from the serialization.
85  // this exercises the guid-filtering
86  var transaction = PlacesUIUtils.makeTransaction(rawNode,
87  PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER,
88  testRootId, -1, true);
89  ok(transaction, "create transaction");
90 
91  // execute it, copying to the test root folder
92  PlacesUIUtils.ptm.doTransaction(transaction);
93  is(testRootNode.childCount, 2, "create test folder via copy");
94 
95  // check GUIDs are different
96  var folderBNode = testRootNode.getChild(1);
97  ok(checkGUIDs(folderBNode, folderAGUIDs, false), "confirm folder A GUIDs don't match folder B GUIDs");
98  var folderBGUIDs = getGUIDs(folderBNode);
99  ok(checkGUIDs(folderBNode, folderBGUIDs, true), "confirm test of new GUIDs");
100 
101  // undo the transaction, confirm the removal
102  PlacesUIUtils.ptm.undoTransaction();
103  is(testRootNode.childCount, 1, "confirm undo removed the copied folder");
104 
105  // redo the transaction
106  // confirming GUIDs persist through undo/redo
107  PlacesUIUtils.ptm.redoTransaction();
108  is(testRootNode.childCount, 2, "confirm redo re-copied the folder");
109  folderBNode = testRootNode.getChild(1);
110  ok(checkGUIDs(folderBNode, folderAGUIDs, false), "folder B GUIDs after undo/redo don't match folder A GUIDs"); // sanity check
111  ok(checkGUIDs(folderBNode, folderBGUIDs, true), "folder B GUIDs after under/redo should match pre-undo/redo folder B GUIDs");
112 
113  // Close containers, cleaning up their observers.
114  testRootNode.containerOpen = false;
115  toolbarNode.containerOpen = false;
116 
117  // clean up
118  PlacesUIUtils.ptm.undoTransaction();
119  PlacesUtils.bookmarks.removeItem(testRootId);
120 }
121 
122 function getGUIDs(aNode) {
123  asContainer(aNode);
124  aNode.containerOpen = true;
125  var GUIDs = {
126  folder: PlacesUtils.bookmarks.getItemGUID(aNode.itemId),
127  bookmark: PlacesUtils.bookmarks.getItemGUID(aNode.getChild(0).itemId),
128  separator: PlacesUtils.bookmarks.getItemGUID(aNode.getChild(1).itemId),
129  livemark: PlacesUtils.bookmarks.getItemGUID(aNode.getChild(2).itemId)
130  };
131  aNode.containerOpen = false;
132  return GUIDs;
133 }
134 
135 function checkGUIDs(aFolderNode, aGUIDs, aShouldMatch) {
136 
137  function check(aNode, aGUID, aEquals) {
138  var nodeGUID = PlacesUtils.bookmarks.getItemGUID(aNode.itemId);
139  return aEquals ? (nodeGUID == aGUID) : (nodeGUID != aGUID);
140  }
141 
142  asContainer(aFolderNode);
143  aFolderNode.containerOpen = true;
144 
145  var allMatch = check(aFolderNode, aGUIDs.folder, aShouldMatch) &&
146  check(aFolderNode.getChild(0), aGUIDs.bookmark, aShouldMatch) &&
147  check(aFolderNode.getChild(1), aGUIDs.separator, aShouldMatch) &&
148  check(aFolderNode.getChild(2), aGUIDs.livemark, aShouldMatch);
149 
150  aFolderNode.containerOpen = false;
151  return allMatch;
152 }
var PlacesUIUtils
Definition: utils.js:85
function getGUIDs(aNode)
function checkGUIDs(aFolderNode, aGUIDs, aShouldMatch)
function check(ch, cx)
function test()
function asContainer(aNode)
Definition: utils.js:82