browser_423515.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  ok(PlacesControllerDragHelper, "checking PlacesControllerDragHelper, running in chrome context?");
43 
44  const IDX = PlacesUtils.bookmarks.DEFAULT_INDEX;
45 
46  // setup
47  var rootId = PlacesUtils.bookmarks.createFolder(PlacesUtils.toolbarFolderId, "", IDX);
48  var rootNode = PlacesUtils.getFolderContents(rootId, false, true).root;
49  is(rootNode.childCount, 0, "confirm test root is empty");
50 
51  var tests = [];
52 
53  // add a regular folder, should be moveable
54  tests.push({
55  populate: function() {
56  this.id =
57  PlacesUtils.bookmarks.createFolder(rootId, "", IDX);
58  },
59  validate: function() {
60  is(rootNode.childCount, 1,
61  "populate added data to the test root");
62  is(PlacesControllerDragHelper.canMoveContainer(this.id),
63  true, "can move regular folder id");
64  is(PlacesControllerDragHelper.canMoveNode(rootNode.getChild(0)),
65  true, "can move regular folder node");
66  }
67  });
68 
69  // add a regular folder shortcut, should be moveable
70  tests.push({
71  populate: function() {
72  this.folderId =
73  PlacesUtils.bookmarks.createFolder(rootId, "foo", IDX);
74  this.shortcutId =
75  PlacesUtils.bookmarks.insertBookmark(rootId, makeURI("place:folder="+this.folderId), IDX, "bar");
76  },
77  validate: function() {
78  is(rootNode.childCount, 2,
79  "populated data to the test root");
80 
81  var folderNode = rootNode.getChild(0);
82  is(folderNode.type, 6, "node is folder");
83  is(this.folderId, folderNode.itemId, "folder id and folder node item id match");
84 
85  var shortcutNode = rootNode.getChild(1);
86  is(shortcutNode.type, 9, "node is folder shortcut");
87  is(this.shortcutId, shortcutNode.itemId, "shortcut id and shortcut node item id match");
88 
89  var concreteId = PlacesUtils.getConcreteItemId(shortcutNode);
90  is(concreteId, folderNode.itemId, "shortcut node id and concrete id match");
91 
92  is(PlacesControllerDragHelper.canMoveContainer(this.shortcutId),
93  true, "can move folder shortcut id");
94 
95  is(PlacesControllerDragHelper.canMoveNode(shortcutNode),
96  true, "can move folder shortcut node");
97  }
98  });
99 
100  // add a regular query, should be moveable
101  tests.push({
102  populate: function() {
103  this.bookmarkId =
104  PlacesUtils.bookmarks.insertBookmark(rootId, makeURI("http://foo.com"), IDX, "foo");
105  this.queryId =
106  PlacesUtils.bookmarks.insertBookmark(rootId, makeURI("place:terms=foo"), IDX, "bar");
107  },
108  validate: function() {
109  is(rootNode.childCount, 2,
110  "populated data to the test root");
111 
112  var bmNode = rootNode.getChild(0);
113  is(bmNode.itemId, this.bookmarkId, "bookmark id and bookmark node item id match");
114 
115  var queryNode = rootNode.getChild(1);
116  is(queryNode.itemId, this.queryId, "query id and query node item id match");
117 
118  is(PlacesControllerDragHelper.canMoveContainer(this.queryId),
119  true, "can move query id");
120 
121  is(PlacesControllerDragHelper.canMoveNode(queryNode),
122  true, "can move query node");
123  }
124  });
125 
126  // test that special folders cannot be moved
127  // test that special folders shortcuts can be moved
128  tests.push({
129  folders: [PlacesUtils.bookmarksMenuFolderId,
130  PlacesUtils.tagsFolderId, PlacesUtils.unfiledBookmarksFolderId,
131  PlacesUtils.toolbarFolderId],
132  shortcuts: {},
133  populate: function() {
134  for (var i = 0; i < this.folders.length; i++) {
135  var id = this.folders[i];
136  this.shortcuts[id] =
137  PlacesUtils.bookmarks.insertBookmark(rootId, makeURI("place:folder=" + id), IDX, "");
138  }
139  },
140  validate: function() {
141  // test toolbar shortcut node
142  is(rootNode.childCount, this.folders.length,
143  "populated data to the test root");
144 
145  function getRootChildNode(aId) {
146  var node = PlacesUtils.getFolderContents(PlacesUtils.placesRootId, false, true).root;
147  for (var i = 0; i < node.childCount; i++) {
148  var child = node.getChild(i);
149  if (child.itemId == aId) {
150  node.containerOpen = false;
151  return child;
152  }
153  }
154  node.containerOpen = false;
155  throw("Unable to find child node");
156  return null;
157  }
158 
159  for (var i = 0; i < this.folders.length; i++) {
160  var id = this.folders[i];
161 
162  is(PlacesControllerDragHelper.canMoveContainer(id),
163  false, "shouldn't be able to move special folder id");
164 
165  var node = getRootChildNode(id);
166  isnot(node, null, "Node found");
167  is(PlacesControllerDragHelper.canMoveNode(node),
168  false, "shouldn't be able to move special folder node");
169 
170  var shortcutId = this.shortcuts[id];
171  var shortcutNode = rootNode.getChild(i);
172 
173  is(shortcutNode.itemId, shortcutId, "shortcut id and shortcut node item id match");
174 
175  LOG("can move shortcut id?");
176  is(PlacesControllerDragHelper.canMoveContainer(shortcutId),
177  true, "should be able to move special folder shortcut id");
178 
179  LOG("can move shortcut node?");
180  is(PlacesControllerDragHelper.canMoveNode(shortcutNode),
181  true, "should be able to move special folder shortcut node");
182  }
183  }
184  });
185 
186  // test that a tag container cannot be moved
187  tests.push({
188  populate: function() {
189  // tag a uri
190  this.uri = makeURI("http://foo.com");
191  PlacesUtils.tagging.tagURI(this.uri, ["bar"]);
192  },
193  validate: function() {
194  // get tag root
195  var query = PlacesUtils.history.getNewQuery();
196  var options = PlacesUtils.history.getNewQueryOptions();
197  options.resultType = Ci.nsINavHistoryQueryOptions.RESULTS_AS_TAG_QUERY;
198  var tagsNode = PlacesUtils.history.executeQuery(query, options).root;
199 
200  tagsNode.containerOpen = true;
201  is(tagsNode.childCount, 1, "has new tag");
202 
203  var tagNode = tagsNode.getChild(0);
204 
205  is(PlacesControllerDragHelper.canMoveNode(tagNode),
206  false, "should not be able to move tag container node");
207 
208  tagsNode.containerOpen = false;
209  }
210  });
211 
212  // test that any child of a read-only node cannot be moved
213  tests.push({
214  populate: function() {
215  this.id =
216  PlacesUtils.bookmarks.createFolder(rootId, "foo", IDX);
217  PlacesUtils.bookmarks.createFolder(this.id, "bar", IDX);
218  PlacesUtils.bookmarks.setFolderReadonly(this.id, true);
219  },
220  validate: function() {
221  is(rootNode.childCount, 1,
222  "populate added data to the test root");
223  var readOnlyFolder = rootNode.getChild(0);
224 
225  // test that we can move the read-only folder
226  is(PlacesControllerDragHelper.canMoveContainer(this.id),
227  true, "can move read-only folder id");
228  is(PlacesControllerDragHelper.canMoveNode(readOnlyFolder),
229  true, "can move read-only folder node");
230 
231  // test that we cannot move the child of a read-only folder
232  readOnlyFolder.QueryInterface(Ci.nsINavHistoryContainerResultNode);
233  readOnlyFolder.containerOpen = true;
234  var childFolder = readOnlyFolder.getChild(0);
235 
236  is(PlacesControllerDragHelper.canMoveContainer(childFolder.itemId),
237  false, "cannot move a child of a read-only folder");
238  is(PlacesControllerDragHelper.canMoveNode(childFolder),
239  false, "cannot move a child node of a read-only folder node");
240  }
241  });
242 
243  tests.forEach(function(aTest) {
244  PlacesUtils.bookmarks.removeFolderChildren(rootId);
245  aTest.populate();
246  aTest.validate();
247  });
248 
249  PlacesUtils.bookmarks.removeItem(rootId);
250 }
var PlacesUIUtils
Definition: utils.js:85
#define LOG(args)
menuItem id
Definition: FeedWriter.js:971
function populate(aFolderId)
function test()
function makeURI(aURLSpec, aCharset)
Definition: FeedWriter.js:71
return null
Definition: FeedWriter.js:1143
let node
var uri
Definition: FeedWriter.js:1135
var PlacesControllerDragHelper
Definition: controller.js:1333
foldersync options
Definition: options.js:13
const Ci
_getSelectedPageStyle s i
function validate(aNode)