Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | // Get history service |
michael@0 | 8 | var histsvc = PlacesUtils.history; |
michael@0 | 9 | var bhist = PlacesUtils.bhistory; |
michael@0 | 10 | var bmsvc = PlacesUtils.bookmarks; |
michael@0 | 11 | |
michael@0 | 12 | var resultObserver = { |
michael@0 | 13 | insertedNode: null, |
michael@0 | 14 | nodeInserted: function(parent, node, newIndex) { |
michael@0 | 15 | this.insertedNode = node; |
michael@0 | 16 | }, |
michael@0 | 17 | removedNode: null, |
michael@0 | 18 | nodeRemoved: function(parent, node, oldIndex) { |
michael@0 | 19 | this.removedNode = node; |
michael@0 | 20 | }, |
michael@0 | 21 | |
michael@0 | 22 | nodeAnnotationChanged: function() {}, |
michael@0 | 23 | |
michael@0 | 24 | newTitle: "", |
michael@0 | 25 | nodeChangedByTitle: null, |
michael@0 | 26 | nodeTitleChanged: function(node, newTitle) { |
michael@0 | 27 | this.nodeChangedByTitle = node; |
michael@0 | 28 | this.newTitle = newTitle; |
michael@0 | 29 | }, |
michael@0 | 30 | |
michael@0 | 31 | newAccessCount: 0, |
michael@0 | 32 | newTime: 0, |
michael@0 | 33 | nodeChangedByHistoryDetails: null, |
michael@0 | 34 | nodeHistoryDetailsChanged: function(node, |
michael@0 | 35 | updatedVisitDate, |
michael@0 | 36 | updatedVisitCount) { |
michael@0 | 37 | this.nodeChangedByHistoryDetails = node |
michael@0 | 38 | this.newTime = updatedVisitDate; |
michael@0 | 39 | this.newAccessCount = updatedVisitCount; |
michael@0 | 40 | }, |
michael@0 | 41 | |
michael@0 | 42 | movedNode: null, |
michael@0 | 43 | nodeMoved: function(node, oldParent, oldIndex, newParent, newIndex) { |
michael@0 | 44 | this.movedNode = node; |
michael@0 | 45 | }, |
michael@0 | 46 | openedContainer: null, |
michael@0 | 47 | closedContainer: null, |
michael@0 | 48 | containerStateChanged: function (aNode, aOldState, aNewState) { |
michael@0 | 49 | if (aNewState == Ci.nsINavHistoryContainerResultNode.STATE_OPENED) { |
michael@0 | 50 | this.openedContainer = aNode; |
michael@0 | 51 | } |
michael@0 | 52 | else if (aNewState == Ci.nsINavHistoryContainerResultNode.STATE_CLOSED) { |
michael@0 | 53 | this.closedContainer = aNode; |
michael@0 | 54 | } |
michael@0 | 55 | }, |
michael@0 | 56 | invalidatedContainer: null, |
michael@0 | 57 | invalidateContainer: function(node) { |
michael@0 | 58 | this.invalidatedContainer = node; |
michael@0 | 59 | }, |
michael@0 | 60 | sortingMode: null, |
michael@0 | 61 | sortingChanged: function(sortingMode) { |
michael@0 | 62 | this.sortingMode = sortingMode; |
michael@0 | 63 | }, |
michael@0 | 64 | inBatchMode: false, |
michael@0 | 65 | batching: function(aToggleMode) { |
michael@0 | 66 | do_check_neq(this.inBatchMode, aToggleMode); |
michael@0 | 67 | this.inBatchMode = aToggleMode; |
michael@0 | 68 | }, |
michael@0 | 69 | result: null, |
michael@0 | 70 | reset: function() { |
michael@0 | 71 | this.insertedNode = null; |
michael@0 | 72 | this.removedNode = null; |
michael@0 | 73 | this.nodeChangedByTitle = null; |
michael@0 | 74 | this.nodeChangedByHistoryDetails = null; |
michael@0 | 75 | this.replacedNode = null; |
michael@0 | 76 | this.movedNode = null; |
michael@0 | 77 | this.openedContainer = null; |
michael@0 | 78 | this.closedContainer = null; |
michael@0 | 79 | this.invalidatedContainer = null; |
michael@0 | 80 | this.sortingMode = null; |
michael@0 | 81 | } |
michael@0 | 82 | }; |
michael@0 | 83 | |
michael@0 | 84 | var testURI = uri("http://mozilla.com"); |
michael@0 | 85 | |
michael@0 | 86 | function run_test() { |
michael@0 | 87 | run_next_test(); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | add_test(function check_history_query() { |
michael@0 | 91 | var options = histsvc.getNewQueryOptions(); |
michael@0 | 92 | options.sortingMode = options.SORT_BY_DATE_DESCENDING; |
michael@0 | 93 | options.resultType = options.RESULTS_AS_VISIT; |
michael@0 | 94 | var query = histsvc.getNewQuery(); |
michael@0 | 95 | var result = histsvc.executeQuery(query, options); |
michael@0 | 96 | result.addObserver(resultObserver, false); |
michael@0 | 97 | var root = result.root; |
michael@0 | 98 | root.containerOpen = true; |
michael@0 | 99 | |
michael@0 | 100 | do_check_neq(resultObserver.openedContainer, null); |
michael@0 | 101 | |
michael@0 | 102 | // nsINavHistoryResultObserver.nodeInserted |
michael@0 | 103 | // add a visit |
michael@0 | 104 | promiseAddVisits(testURI).then(function() { |
michael@0 | 105 | do_check_eq(testURI.spec, resultObserver.insertedNode.uri); |
michael@0 | 106 | |
michael@0 | 107 | // nsINavHistoryResultObserver.nodeHistoryDetailsChanged |
michael@0 | 108 | // adding a visit causes nodeHistoryDetailsChanged for the folder |
michael@0 | 109 | do_check_eq(root.uri, resultObserver.nodeChangedByHistoryDetails.uri); |
michael@0 | 110 | |
michael@0 | 111 | // nsINavHistoryResultObserver.itemTitleChanged for a leaf node |
michael@0 | 112 | promiseAddVisits({ uri: testURI, title: "baz" }).then(function () { |
michael@0 | 113 | do_check_eq(resultObserver.nodeChangedByTitle.title, "baz"); |
michael@0 | 114 | |
michael@0 | 115 | // nsINavHistoryResultObserver.nodeRemoved |
michael@0 | 116 | var removedURI = uri("http://google.com"); |
michael@0 | 117 | promiseAddVisits(removedURI).then(function() { |
michael@0 | 118 | bhist.removePage(removedURI); |
michael@0 | 119 | do_check_eq(removedURI.spec, resultObserver.removedNode.uri); |
michael@0 | 120 | |
michael@0 | 121 | // nsINavHistoryResultObserver.invalidateContainer |
michael@0 | 122 | bhist.removePagesFromHost("mozilla.com", false); |
michael@0 | 123 | do_check_eq(root.uri, resultObserver.invalidatedContainer.uri); |
michael@0 | 124 | |
michael@0 | 125 | // nsINavHistoryResultObserver.sortingChanged |
michael@0 | 126 | resultObserver.invalidatedContainer = null; |
michael@0 | 127 | result.sortingMode = options.SORT_BY_TITLE_ASCENDING; |
michael@0 | 128 | do_check_eq(resultObserver.sortingMode, options.SORT_BY_TITLE_ASCENDING); |
michael@0 | 129 | do_check_eq(resultObserver.invalidatedContainer, result.root); |
michael@0 | 130 | |
michael@0 | 131 | // nsINavHistoryResultObserver.invalidateContainer |
michael@0 | 132 | bhist.removeAllPages(); |
michael@0 | 133 | do_check_eq(root.uri, resultObserver.invalidatedContainer.uri); |
michael@0 | 134 | |
michael@0 | 135 | // nsINavHistoryResultObserver.batching |
michael@0 | 136 | do_check_false(resultObserver.inBatchMode); |
michael@0 | 137 | histsvc.runInBatchMode({ |
michael@0 | 138 | runBatched: function (aUserData) { |
michael@0 | 139 | do_check_true(resultObserver.inBatchMode); |
michael@0 | 140 | } |
michael@0 | 141 | }, null); |
michael@0 | 142 | do_check_false(resultObserver.inBatchMode); |
michael@0 | 143 | bmsvc.runInBatchMode({ |
michael@0 | 144 | runBatched: function (aUserData) { |
michael@0 | 145 | do_check_true(resultObserver.inBatchMode); |
michael@0 | 146 | } |
michael@0 | 147 | }, null); |
michael@0 | 148 | do_check_false(resultObserver.inBatchMode); |
michael@0 | 149 | |
michael@0 | 150 | root.containerOpen = false; |
michael@0 | 151 | do_check_eq(resultObserver.closedContainer, resultObserver.openedContainer); |
michael@0 | 152 | result.removeObserver(resultObserver); |
michael@0 | 153 | resultObserver.reset(); |
michael@0 | 154 | promiseAsyncUpdates().then(run_next_test); |
michael@0 | 155 | }); |
michael@0 | 156 | }); |
michael@0 | 157 | }); |
michael@0 | 158 | }); |
michael@0 | 159 | |
michael@0 | 160 | add_test(function check_bookmarks_query() { |
michael@0 | 161 | var options = histsvc.getNewQueryOptions(); |
michael@0 | 162 | var query = histsvc.getNewQuery(); |
michael@0 | 163 | query.setFolders([bmsvc.bookmarksMenuFolder], 1); |
michael@0 | 164 | var result = histsvc.executeQuery(query, options); |
michael@0 | 165 | result.addObserver(resultObserver, false); |
michael@0 | 166 | var root = result.root; |
michael@0 | 167 | root.containerOpen = true; |
michael@0 | 168 | |
michael@0 | 169 | do_check_neq(resultObserver.openedContainer, null); |
michael@0 | 170 | |
michael@0 | 171 | // nsINavHistoryResultObserver.nodeInserted |
michael@0 | 172 | // add a bookmark |
michael@0 | 173 | var testBookmark = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, testURI, bmsvc.DEFAULT_INDEX, "foo"); |
michael@0 | 174 | do_check_eq("foo", resultObserver.insertedNode.title); |
michael@0 | 175 | do_check_eq(testURI.spec, resultObserver.insertedNode.uri); |
michael@0 | 176 | |
michael@0 | 177 | // nsINavHistoryResultObserver.nodeHistoryDetailsChanged |
michael@0 | 178 | // adding a visit causes nodeHistoryDetailsChanged for the folder |
michael@0 | 179 | do_check_eq(root.uri, resultObserver.nodeChangedByHistoryDetails.uri); |
michael@0 | 180 | |
michael@0 | 181 | // nsINavHistoryResultObserver.nodeTitleChanged for a leaf node |
michael@0 | 182 | bmsvc.setItemTitle(testBookmark, "baz"); |
michael@0 | 183 | do_check_eq(resultObserver.nodeChangedByTitle.title, "baz"); |
michael@0 | 184 | do_check_eq(resultObserver.newTitle, "baz"); |
michael@0 | 185 | |
michael@0 | 186 | var testBookmark2 = bmsvc.insertBookmark(bmsvc.bookmarksMenuFolder, uri("http://google.com"), bmsvc.DEFAULT_INDEX, "foo"); |
michael@0 | 187 | bmsvc.moveItem(testBookmark2, bmsvc.bookmarksMenuFolder, 0); |
michael@0 | 188 | do_check_eq(resultObserver.movedNode.itemId, testBookmark2); |
michael@0 | 189 | |
michael@0 | 190 | // nsINavHistoryResultObserver.nodeRemoved |
michael@0 | 191 | bmsvc.removeItem(testBookmark2); |
michael@0 | 192 | do_check_eq(testBookmark2, resultObserver.removedNode.itemId); |
michael@0 | 193 | |
michael@0 | 194 | // XXX nsINavHistoryResultObserver.invalidateContainer |
michael@0 | 195 | |
michael@0 | 196 | // nsINavHistoryResultObserver.sortingChanged |
michael@0 | 197 | resultObserver.invalidatedContainer = null; |
michael@0 | 198 | result.sortingMode = options.SORT_BY_TITLE_ASCENDING; |
michael@0 | 199 | do_check_eq(resultObserver.sortingMode, options.SORT_BY_TITLE_ASCENDING); |
michael@0 | 200 | do_check_eq(resultObserver.invalidatedContainer, result.root); |
michael@0 | 201 | |
michael@0 | 202 | // nsINavHistoryResultObserver.batching |
michael@0 | 203 | do_check_false(resultObserver.inBatchMode); |
michael@0 | 204 | histsvc.runInBatchMode({ |
michael@0 | 205 | runBatched: function (aUserData) { |
michael@0 | 206 | do_check_true(resultObserver.inBatchMode); |
michael@0 | 207 | } |
michael@0 | 208 | }, null); |
michael@0 | 209 | do_check_false(resultObserver.inBatchMode); |
michael@0 | 210 | bmsvc.runInBatchMode({ |
michael@0 | 211 | runBatched: function (aUserData) { |
michael@0 | 212 | do_check_true(resultObserver.inBatchMode); |
michael@0 | 213 | } |
michael@0 | 214 | }, null); |
michael@0 | 215 | do_check_false(resultObserver.inBatchMode); |
michael@0 | 216 | |
michael@0 | 217 | root.containerOpen = false; |
michael@0 | 218 | do_check_eq(resultObserver.closedContainer, resultObserver.openedContainer); |
michael@0 | 219 | result.removeObserver(resultObserver); |
michael@0 | 220 | resultObserver.reset(); |
michael@0 | 221 | promiseAsyncUpdates().then(run_next_test); |
michael@0 | 222 | }); |
michael@0 | 223 | |
michael@0 | 224 | add_test(function check_mixed_query() { |
michael@0 | 225 | var options = histsvc.getNewQueryOptions(); |
michael@0 | 226 | var query = histsvc.getNewQuery(); |
michael@0 | 227 | query.onlyBookmarked = true; |
michael@0 | 228 | var result = histsvc.executeQuery(query, options); |
michael@0 | 229 | result.addObserver(resultObserver, false); |
michael@0 | 230 | var root = result.root; |
michael@0 | 231 | root.containerOpen = true; |
michael@0 | 232 | |
michael@0 | 233 | do_check_neq(resultObserver.openedContainer, null); |
michael@0 | 234 | |
michael@0 | 235 | // nsINavHistoryResultObserver.batching |
michael@0 | 236 | do_check_false(resultObserver.inBatchMode); |
michael@0 | 237 | histsvc.runInBatchMode({ |
michael@0 | 238 | runBatched: function (aUserData) { |
michael@0 | 239 | do_check_true(resultObserver.inBatchMode); |
michael@0 | 240 | } |
michael@0 | 241 | }, null); |
michael@0 | 242 | do_check_false(resultObserver.inBatchMode); |
michael@0 | 243 | bmsvc.runInBatchMode({ |
michael@0 | 244 | runBatched: function (aUserData) { |
michael@0 | 245 | do_check_true(resultObserver.inBatchMode); |
michael@0 | 246 | } |
michael@0 | 247 | }, null); |
michael@0 | 248 | do_check_false(resultObserver.inBatchMode); |
michael@0 | 249 | |
michael@0 | 250 | root.containerOpen = false; |
michael@0 | 251 | do_check_eq(resultObserver.closedContainer, resultObserver.openedContainer); |
michael@0 | 252 | result.removeObserver(resultObserver); |
michael@0 | 253 | resultObserver.reset(); |
michael@0 | 254 | promiseAsyncUpdates().then(run_next_test); |
michael@0 | 255 | }); |