Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | /* vim:set ts=2 sw=2 sts=2 et: */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | function test() { |
michael@0 | 7 | // sanity check |
michael@0 | 8 | ok(PlacesUtils, "checking PlacesUtils, running in chrome context?"); |
michael@0 | 9 | ok(PlacesUIUtils, "checking PlacesUIUtils, running in chrome context?"); |
michael@0 | 10 | ok(PlacesControllerDragHelper, "checking PlacesControllerDragHelper, running in chrome context?"); |
michael@0 | 11 | |
michael@0 | 12 | const IDX = PlacesUtils.bookmarks.DEFAULT_INDEX; |
michael@0 | 13 | |
michael@0 | 14 | // setup |
michael@0 | 15 | var rootId = PlacesUtils.bookmarks.createFolder(PlacesUtils.toolbarFolderId, "", IDX); |
michael@0 | 16 | var rootNode = PlacesUtils.getFolderContents(rootId, false, true).root; |
michael@0 | 17 | is(rootNode.childCount, 0, "confirm test root is empty"); |
michael@0 | 18 | |
michael@0 | 19 | var tests = []; |
michael@0 | 20 | |
michael@0 | 21 | // add a regular folder, should be moveable |
michael@0 | 22 | tests.push({ |
michael@0 | 23 | populate: function() { |
michael@0 | 24 | this.id = |
michael@0 | 25 | PlacesUtils.bookmarks.createFolder(rootId, "", IDX); |
michael@0 | 26 | }, |
michael@0 | 27 | validate: function() { |
michael@0 | 28 | is(rootNode.childCount, 1, |
michael@0 | 29 | "populate added data to the test root"); |
michael@0 | 30 | is(PlacesControllerDragHelper.canMoveContainer(this.id), |
michael@0 | 31 | true, "can move regular folder id"); |
michael@0 | 32 | is(PlacesControllerDragHelper.canMoveNode(rootNode.getChild(0)), |
michael@0 | 33 | true, "can move regular folder node"); |
michael@0 | 34 | } |
michael@0 | 35 | }); |
michael@0 | 36 | |
michael@0 | 37 | // add a regular folder shortcut, should be moveable |
michael@0 | 38 | tests.push({ |
michael@0 | 39 | populate: function() { |
michael@0 | 40 | this.folderId = |
michael@0 | 41 | PlacesUtils.bookmarks.createFolder(rootId, "foo", IDX); |
michael@0 | 42 | this.shortcutId = |
michael@0 | 43 | PlacesUtils.bookmarks.insertBookmark(rootId, makeURI("place:folder="+this.folderId), IDX, "bar"); |
michael@0 | 44 | }, |
michael@0 | 45 | validate: function() { |
michael@0 | 46 | is(rootNode.childCount, 2, |
michael@0 | 47 | "populated data to the test root"); |
michael@0 | 48 | |
michael@0 | 49 | var folderNode = rootNode.getChild(0); |
michael@0 | 50 | is(folderNode.type, 6, "node is folder"); |
michael@0 | 51 | is(this.folderId, folderNode.itemId, "folder id and folder node item id match"); |
michael@0 | 52 | |
michael@0 | 53 | var shortcutNode = rootNode.getChild(1); |
michael@0 | 54 | is(shortcutNode.type, 9, "node is folder shortcut"); |
michael@0 | 55 | is(this.shortcutId, shortcutNode.itemId, "shortcut id and shortcut node item id match"); |
michael@0 | 56 | |
michael@0 | 57 | var concreteId = PlacesUtils.getConcreteItemId(shortcutNode); |
michael@0 | 58 | is(concreteId, folderNode.itemId, "shortcut node id and concrete id match"); |
michael@0 | 59 | |
michael@0 | 60 | is(PlacesControllerDragHelper.canMoveContainer(this.shortcutId), |
michael@0 | 61 | true, "can move folder shortcut id"); |
michael@0 | 62 | |
michael@0 | 63 | is(PlacesControllerDragHelper.canMoveNode(shortcutNode), |
michael@0 | 64 | true, "can move folder shortcut node"); |
michael@0 | 65 | } |
michael@0 | 66 | }); |
michael@0 | 67 | |
michael@0 | 68 | // add a regular query, should be moveable |
michael@0 | 69 | tests.push({ |
michael@0 | 70 | populate: function() { |
michael@0 | 71 | this.bookmarkId = |
michael@0 | 72 | PlacesUtils.bookmarks.insertBookmark(rootId, makeURI("http://foo.com"), IDX, "foo"); |
michael@0 | 73 | this.queryId = |
michael@0 | 74 | PlacesUtils.bookmarks.insertBookmark(rootId, makeURI("place:terms=foo"), IDX, "bar"); |
michael@0 | 75 | }, |
michael@0 | 76 | validate: function() { |
michael@0 | 77 | is(rootNode.childCount, 2, |
michael@0 | 78 | "populated data to the test root"); |
michael@0 | 79 | |
michael@0 | 80 | var bmNode = rootNode.getChild(0); |
michael@0 | 81 | is(bmNode.itemId, this.bookmarkId, "bookmark id and bookmark node item id match"); |
michael@0 | 82 | |
michael@0 | 83 | var queryNode = rootNode.getChild(1); |
michael@0 | 84 | is(queryNode.itemId, this.queryId, "query id and query node item id match"); |
michael@0 | 85 | |
michael@0 | 86 | is(PlacesControllerDragHelper.canMoveContainer(this.queryId), |
michael@0 | 87 | true, "can move query id"); |
michael@0 | 88 | |
michael@0 | 89 | is(PlacesControllerDragHelper.canMoveNode(queryNode), |
michael@0 | 90 | true, "can move query node"); |
michael@0 | 91 | } |
michael@0 | 92 | }); |
michael@0 | 93 | |
michael@0 | 94 | // test that special folders cannot be moved |
michael@0 | 95 | // test that special folders shortcuts can be moved |
michael@0 | 96 | tests.push({ |
michael@0 | 97 | folders: [PlacesUtils.bookmarksMenuFolderId, |
michael@0 | 98 | PlacesUtils.tagsFolderId, PlacesUtils.unfiledBookmarksFolderId, |
michael@0 | 99 | PlacesUtils.toolbarFolderId], |
michael@0 | 100 | shortcuts: {}, |
michael@0 | 101 | populate: function() { |
michael@0 | 102 | for (var i = 0; i < this.folders.length; i++) { |
michael@0 | 103 | var id = this.folders[i]; |
michael@0 | 104 | this.shortcuts[id] = |
michael@0 | 105 | PlacesUtils.bookmarks.insertBookmark(rootId, makeURI("place:folder=" + id), IDX, ""); |
michael@0 | 106 | } |
michael@0 | 107 | }, |
michael@0 | 108 | validate: function() { |
michael@0 | 109 | // test toolbar shortcut node |
michael@0 | 110 | is(rootNode.childCount, this.folders.length, |
michael@0 | 111 | "populated data to the test root"); |
michael@0 | 112 | |
michael@0 | 113 | function getRootChildNode(aId) { |
michael@0 | 114 | var node = PlacesUtils.getFolderContents(PlacesUtils.placesRootId, false, true).root; |
michael@0 | 115 | for (var i = 0; i < node.childCount; i++) { |
michael@0 | 116 | var child = node.getChild(i); |
michael@0 | 117 | if (child.itemId == aId) { |
michael@0 | 118 | node.containerOpen = false; |
michael@0 | 119 | return child; |
michael@0 | 120 | } |
michael@0 | 121 | } |
michael@0 | 122 | node.containerOpen = false; |
michael@0 | 123 | ok(false, "Unable to find child node"); |
michael@0 | 124 | return null; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | for (var i = 0; i < this.folders.length; i++) { |
michael@0 | 128 | var id = this.folders[i]; |
michael@0 | 129 | |
michael@0 | 130 | is(PlacesControllerDragHelper.canMoveContainer(id), |
michael@0 | 131 | false, "shouldn't be able to move special folder id"); |
michael@0 | 132 | |
michael@0 | 133 | var node = getRootChildNode(id); |
michael@0 | 134 | isnot(node, null, "Node found"); |
michael@0 | 135 | is(PlacesControllerDragHelper.canMoveNode(node), |
michael@0 | 136 | false, "shouldn't be able to move special folder node"); |
michael@0 | 137 | |
michael@0 | 138 | var shortcutId = this.shortcuts[id]; |
michael@0 | 139 | var shortcutNode = rootNode.getChild(i); |
michael@0 | 140 | |
michael@0 | 141 | is(shortcutNode.itemId, shortcutId, "shortcut id and shortcut node item id match"); |
michael@0 | 142 | |
michael@0 | 143 | dump("can move shortcut id?\n"); |
michael@0 | 144 | is(PlacesControllerDragHelper.canMoveContainer(shortcutId), |
michael@0 | 145 | true, "should be able to move special folder shortcut id"); |
michael@0 | 146 | |
michael@0 | 147 | dump("can move shortcut node?\n"); |
michael@0 | 148 | is(PlacesControllerDragHelper.canMoveNode(shortcutNode), |
michael@0 | 149 | true, "should be able to move special folder shortcut node"); |
michael@0 | 150 | } |
michael@0 | 151 | } |
michael@0 | 152 | }); |
michael@0 | 153 | |
michael@0 | 154 | // test that a tag container cannot be moved |
michael@0 | 155 | tests.push({ |
michael@0 | 156 | populate: function() { |
michael@0 | 157 | // tag a uri |
michael@0 | 158 | this.uri = makeURI("http://foo.com"); |
michael@0 | 159 | PlacesUtils.tagging.tagURI(this.uri, ["bar"]); |
michael@0 | 160 | }, |
michael@0 | 161 | validate: function() { |
michael@0 | 162 | // get tag root |
michael@0 | 163 | var query = PlacesUtils.history.getNewQuery(); |
michael@0 | 164 | var options = PlacesUtils.history.getNewQueryOptions(); |
michael@0 | 165 | options.resultType = Ci.nsINavHistoryQueryOptions.RESULTS_AS_TAG_QUERY; |
michael@0 | 166 | var tagsNode = PlacesUtils.history.executeQuery(query, options).root; |
michael@0 | 167 | |
michael@0 | 168 | tagsNode.containerOpen = true; |
michael@0 | 169 | is(tagsNode.childCount, 1, "has new tag"); |
michael@0 | 170 | |
michael@0 | 171 | var tagNode = tagsNode.getChild(0); |
michael@0 | 172 | |
michael@0 | 173 | is(PlacesControllerDragHelper.canMoveNode(tagNode), |
michael@0 | 174 | false, "should not be able to move tag container node"); |
michael@0 | 175 | |
michael@0 | 176 | tagsNode.containerOpen = false; |
michael@0 | 177 | } |
michael@0 | 178 | }); |
michael@0 | 179 | |
michael@0 | 180 | // test that any child of a read-only node cannot be moved |
michael@0 | 181 | tests.push({ |
michael@0 | 182 | populate: function() { |
michael@0 | 183 | this.id = |
michael@0 | 184 | PlacesUtils.bookmarks.createFolder(rootId, "foo", IDX); |
michael@0 | 185 | PlacesUtils.bookmarks.createFolder(this.id, "bar", IDX); |
michael@0 | 186 | PlacesUtils.bookmarks.setFolderReadonly(this.id, true); |
michael@0 | 187 | }, |
michael@0 | 188 | validate: function() { |
michael@0 | 189 | is(rootNode.childCount, 1, |
michael@0 | 190 | "populate added data to the test root"); |
michael@0 | 191 | var readOnlyFolder = rootNode.getChild(0); |
michael@0 | 192 | |
michael@0 | 193 | // test that we can move the read-only folder |
michael@0 | 194 | is(PlacesControllerDragHelper.canMoveContainer(this.id), |
michael@0 | 195 | true, "can move read-only folder id"); |
michael@0 | 196 | is(PlacesControllerDragHelper.canMoveNode(readOnlyFolder), |
michael@0 | 197 | true, "can move read-only folder node"); |
michael@0 | 198 | |
michael@0 | 199 | // test that we cannot move the child of a read-only folder |
michael@0 | 200 | readOnlyFolder.QueryInterface(Ci.nsINavHistoryContainerResultNode); |
michael@0 | 201 | readOnlyFolder.containerOpen = true; |
michael@0 | 202 | var childFolder = readOnlyFolder.getChild(0); |
michael@0 | 203 | |
michael@0 | 204 | is(PlacesControllerDragHelper.canMoveContainer(childFolder.itemId), |
michael@0 | 205 | false, "cannot move a child of a read-only folder"); |
michael@0 | 206 | is(PlacesControllerDragHelper.canMoveNode(childFolder), |
michael@0 | 207 | false, "cannot move a child node of a read-only folder node"); |
michael@0 | 208 | readOnlyFolder.containerOpen = false; |
michael@0 | 209 | } |
michael@0 | 210 | }); |
michael@0 | 211 | |
michael@0 | 212 | tests.forEach(function(aTest) { |
michael@0 | 213 | PlacesUtils.bookmarks.removeFolderChildren(rootId); |
michael@0 | 214 | aTest.populate(); |
michael@0 | 215 | aTest.validate(); |
michael@0 | 216 | }); |
michael@0 | 217 | |
michael@0 | 218 | rootNode.containerOpen = false; |
michael@0 | 219 | PlacesUtils.bookmarks.removeItem(rootId); |
michael@0 | 220 | } |