toolkit/components/places/tests/unit/test_removeVisitsByTimeframe.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 const bmsvc = PlacesUtils.bookmarks;
michael@0 8 const histsvc = PlacesUtils.history;
michael@0 9
michael@0 10 const NOW = Date.now() * 1000;
michael@0 11 const TEST_URL = "http://example.com/";
michael@0 12 const TEST_URI = uri(TEST_URL);
michael@0 13 const PLACE_URL = "place:queryType=0&sort=8&maxResults=10";
michael@0 14 const PLACE_URI = uri(PLACE_URL);
michael@0 15
michael@0 16 var tests = [
michael@0 17 {
michael@0 18 desc: "Remove some visits outside valid timeframe from an unbookmarked URI",
michael@0 19 run: function () {
michael@0 20 print("Add 10 visits for the URI from way in the past.");
michael@0 21 let visits = [];
michael@0 22 for (let i = 0; i < 10; i++) {
michael@0 23 visits.push({ uri: TEST_URI, visitDate: NOW - 1000 - i });
michael@0 24 }
michael@0 25 promiseAddVisits(visits).then(this.continue_run.bind(this));
michael@0 26 },
michael@0 27 continue_run: function () {
michael@0 28 print("Remove visits using timerange outside the URI's visits.");
michael@0 29 histsvc.QueryInterface(Ci.nsIBrowserHistory).
michael@0 30 removeVisitsByTimeframe(NOW - 10, NOW);
michael@0 31
michael@0 32 print("URI should still exist in moz_places.");
michael@0 33 do_check_true(page_in_database(TEST_URL));
michael@0 34
michael@0 35 print("Run a history query and check that all visits still exist.");
michael@0 36 var query = histsvc.getNewQuery();
michael@0 37 var opts = histsvc.getNewQueryOptions();
michael@0 38 opts.resultType = opts.RESULTS_AS_VISIT;
michael@0 39 opts.sortingMode = opts.SORT_BY_DATE_DESCENDING;
michael@0 40 var resultRoot = histsvc.executeQuery(query, opts).root;
michael@0 41 resultRoot.containerOpen = true;
michael@0 42 do_check_eq(resultRoot.childCount, 10);
michael@0 43 for (let i = 0; i < resultRoot.childCount; i++) {
michael@0 44 var visitTime = resultRoot.getChild(i).time;
michael@0 45 do_check_eq(visitTime, NOW - 1000 - i);
michael@0 46 }
michael@0 47 resultRoot.containerOpen = false;
michael@0 48
michael@0 49 print("asyncHistory.isURIVisited should return true.");
michael@0 50 PlacesUtils.asyncHistory.isURIVisited(TEST_URI, function(aURI, aIsVisited) {
michael@0 51 do_check_true(aIsVisited);
michael@0 52
michael@0 53 promiseAsyncUpdates().then(function () {
michael@0 54 print("Frecency should be positive.")
michael@0 55 do_check_true(frecencyForUrl(TEST_URI) > 0);
michael@0 56 run_next_test();
michael@0 57 });
michael@0 58 });
michael@0 59 }
michael@0 60 },
michael@0 61
michael@0 62 {
michael@0 63 desc: "Remove some visits outside valid timeframe from a bookmarked URI",
michael@0 64 run: function () {
michael@0 65 print("Add 10 visits for the URI from way in the past.");
michael@0 66 let visits = [];
michael@0 67 for (let i = 0; i < 10; i++) {
michael@0 68 visits.push({ uri: TEST_URI, visitDate: NOW - 1000 - i });
michael@0 69 }
michael@0 70 promiseAddVisits(visits).then(function () {
michael@0 71 print("Bookmark the URI.");
michael@0 72 bmsvc.insertBookmark(bmsvc.unfiledBookmarksFolder,
michael@0 73 TEST_URI,
michael@0 74 bmsvc.DEFAULT_INDEX,
michael@0 75 "bookmark title");
michael@0 76
michael@0 77 promiseAsyncUpdates().then(this.continue_run.bind(this));
michael@0 78 }.bind(this));
michael@0 79 },
michael@0 80 continue_run: function () {
michael@0 81 print("Remove visits using timerange outside the URI's visits.");
michael@0 82 histsvc.QueryInterface(Ci.nsIBrowserHistory).
michael@0 83 removeVisitsByTimeframe(NOW - 10, NOW);
michael@0 84
michael@0 85 print("URI should still exist in moz_places.");
michael@0 86 do_check_true(page_in_database(TEST_URL));
michael@0 87
michael@0 88 print("Run a history query and check that all visits still exist.");
michael@0 89 var query = histsvc.getNewQuery();
michael@0 90 var opts = histsvc.getNewQueryOptions();
michael@0 91 opts.resultType = opts.RESULTS_AS_VISIT;
michael@0 92 opts.sortingMode = opts.SORT_BY_DATE_DESCENDING;
michael@0 93 var resultRoot = histsvc.executeQuery(query, opts).root;
michael@0 94 resultRoot.containerOpen = true;
michael@0 95 do_check_eq(resultRoot.childCount, 10);
michael@0 96 for (let i = 0; i < resultRoot.childCount; i++) {
michael@0 97 var visitTime = resultRoot.getChild(i).time;
michael@0 98 do_check_eq(visitTime, NOW - 1000 - i);
michael@0 99 }
michael@0 100 resultRoot.containerOpen = false;
michael@0 101
michael@0 102 print("asyncHistory.isURIVisited should return true.");
michael@0 103 PlacesUtils.asyncHistory.isURIVisited(TEST_URI, function(aURI, aIsVisited) {
michael@0 104 do_check_true(aIsVisited);
michael@0 105
michael@0 106 promiseAsyncUpdates().then(function () {
michael@0 107 print("Frecency should be positive.")
michael@0 108 do_check_true(frecencyForUrl(TEST_URI) > 0);
michael@0 109 run_next_test();
michael@0 110 });
michael@0 111 });
michael@0 112 }
michael@0 113 },
michael@0 114
michael@0 115 {
michael@0 116 desc: "Remove some visits from an unbookmarked URI",
michael@0 117 run: function () {
michael@0 118 print("Add 10 visits for the URI from now to 9 usecs in the past.");
michael@0 119 let visits = [];
michael@0 120 for (let i = 0; i < 10; i++) {
michael@0 121 visits.push({ uri: TEST_URI, visitDate: NOW - i });
michael@0 122 }
michael@0 123 promiseAddVisits(visits).then(this.continue_run.bind(this));
michael@0 124 },
michael@0 125 continue_run: function () {
michael@0 126 print("Remove the 5 most recent visits.");
michael@0 127 histsvc.QueryInterface(Ci.nsIBrowserHistory).
michael@0 128 removeVisitsByTimeframe(NOW - 4, NOW);
michael@0 129
michael@0 130 print("URI should still exist in moz_places.");
michael@0 131 do_check_true(page_in_database(TEST_URL));
michael@0 132
michael@0 133 print("Run a history query and check that only the older 5 visits " +
michael@0 134 "still exist.");
michael@0 135 var query = histsvc.getNewQuery();
michael@0 136 var opts = histsvc.getNewQueryOptions();
michael@0 137 opts.resultType = opts.RESULTS_AS_VISIT;
michael@0 138 opts.sortingMode = opts.SORT_BY_DATE_DESCENDING;
michael@0 139 var resultRoot = histsvc.executeQuery(query, opts).root;
michael@0 140 resultRoot.containerOpen = true;
michael@0 141 do_check_eq(resultRoot.childCount, 5);
michael@0 142 for (let i = 0; i < resultRoot.childCount; i++) {
michael@0 143 var visitTime = resultRoot.getChild(i).time;
michael@0 144 do_check_eq(visitTime, NOW - i - 5);
michael@0 145 }
michael@0 146 resultRoot.containerOpen = false;
michael@0 147
michael@0 148 print("asyncHistory.isURIVisited should return true.");
michael@0 149 PlacesUtils.asyncHistory.isURIVisited(TEST_URI, function(aURI, aIsVisited) {
michael@0 150 do_check_true(aIsVisited);
michael@0 151
michael@0 152 promiseAsyncUpdates().then(function () {
michael@0 153 print("Frecency should be positive.")
michael@0 154 do_check_true(frecencyForUrl(TEST_URI) > 0);
michael@0 155 run_next_test();
michael@0 156 });
michael@0 157 });
michael@0 158 }
michael@0 159 },
michael@0 160
michael@0 161 {
michael@0 162 desc: "Remove some visits from a bookmarked URI",
michael@0 163 run: function () {
michael@0 164 print("Add 10 visits for the URI from now to 9 usecs in the past.");
michael@0 165 let visits = [];
michael@0 166 for (let i = 0; i < 10; i++) {
michael@0 167 visits.push({ uri: TEST_URI, visitDate: NOW - i });
michael@0 168 }
michael@0 169 promiseAddVisits(visits).then(function () {
michael@0 170 print("Bookmark the URI.");
michael@0 171 bmsvc.insertBookmark(bmsvc.unfiledBookmarksFolder,
michael@0 172 TEST_URI,
michael@0 173 bmsvc.DEFAULT_INDEX,
michael@0 174 "bookmark title");
michael@0 175 promiseAsyncUpdates().then(this.continue_run.bind(this));
michael@0 176 }.bind(this));
michael@0 177 },
michael@0 178 continue_run: function () {
michael@0 179 print("Remove the 5 most recent visits.");
michael@0 180 histsvc.QueryInterface(Ci.nsIBrowserHistory).
michael@0 181 removeVisitsByTimeframe(NOW - 4, NOW);
michael@0 182
michael@0 183 print("URI should still exist in moz_places.");
michael@0 184 do_check_true(page_in_database(TEST_URL));
michael@0 185
michael@0 186 print("Run a history query and check that only the older 5 visits " +
michael@0 187 "still exist.");
michael@0 188 var query = histsvc.getNewQuery();
michael@0 189 var opts = histsvc.getNewQueryOptions();
michael@0 190 opts.resultType = opts.RESULTS_AS_VISIT;
michael@0 191 opts.sortingMode = opts.SORT_BY_DATE_DESCENDING;
michael@0 192 var resultRoot = histsvc.executeQuery(query, opts).root;
michael@0 193 resultRoot.containerOpen = true;
michael@0 194 do_check_eq(resultRoot.childCount, 5);
michael@0 195 for (let i = 0; i < resultRoot.childCount; i++) {
michael@0 196 var visitTime = resultRoot.getChild(i).time;
michael@0 197 do_check_eq(visitTime, NOW - i - 5);
michael@0 198 }
michael@0 199 resultRoot.containerOpen = false;
michael@0 200
michael@0 201 print("asyncHistory.isURIVisited should return true.");
michael@0 202 PlacesUtils.asyncHistory.isURIVisited(TEST_URI, function(aURI, aIsVisited) {
michael@0 203 do_check_true(aIsVisited);
michael@0 204
michael@0 205 promiseAsyncUpdates().then(function () {
michael@0 206 print("Frecency should be positive.")
michael@0 207 do_check_true(frecencyForUrl(TEST_URI) > 0);
michael@0 208 run_next_test();
michael@0 209 });
michael@0 210 });
michael@0 211 }
michael@0 212 },
michael@0 213
michael@0 214 {
michael@0 215 desc: "Remove all visits from an unbookmarked URI",
michael@0 216 run: function () {
michael@0 217 print("Add some visits for the URI.");
michael@0 218 let visits = [];
michael@0 219 for (let i = 0; i < 10; i++) {
michael@0 220 visits.push({ uri: TEST_URI, visitDate: NOW - i });
michael@0 221 }
michael@0 222 promiseAddVisits(visits).then(this.continue_run.bind(this));
michael@0 223 },
michael@0 224 continue_run: function () {
michael@0 225 print("Remove all visits.");
michael@0 226 histsvc.QueryInterface(Ci.nsIBrowserHistory).
michael@0 227 removeVisitsByTimeframe(NOW - 10, NOW);
michael@0 228
michael@0 229 print("URI should no longer exist in moz_places.");
michael@0 230 do_check_false(page_in_database(TEST_URL));
michael@0 231
michael@0 232 print("Run a history query and check that no visits exist.");
michael@0 233 var query = histsvc.getNewQuery();
michael@0 234 var opts = histsvc.getNewQueryOptions();
michael@0 235 opts.resultType = opts.RESULTS_AS_VISIT;
michael@0 236 opts.sortingMode = opts.SORT_BY_DATE_DESCENDING;
michael@0 237 var resultRoot = histsvc.executeQuery(query, opts).root;
michael@0 238 resultRoot.containerOpen = true;
michael@0 239 do_check_eq(resultRoot.childCount, 0);
michael@0 240 resultRoot.containerOpen = false;
michael@0 241
michael@0 242 print("asyncHistory.isURIVisited should return false.");
michael@0 243 PlacesUtils.asyncHistory.isURIVisited(TEST_URI, function(aURI, aIsVisited) {
michael@0 244 do_check_false(aIsVisited);
michael@0 245 run_next_test();
michael@0 246 });
michael@0 247 }
michael@0 248 },
michael@0 249
michael@0 250 {
michael@0 251 desc: "Remove all visits from an unbookmarked place: URI",
michael@0 252 run: function () {
michael@0 253 print("Add some visits for the URI.");
michael@0 254 let visits = [];
michael@0 255 for (let i = 0; i < 10; i++) {
michael@0 256 visits.push({ uri: PLACE_URI, visitDate: NOW - i });
michael@0 257 }
michael@0 258 promiseAddVisits(visits).then(this.continue_run.bind(this));
michael@0 259 },
michael@0 260 continue_run: function () {
michael@0 261 print("Remove all visits.");
michael@0 262 histsvc.QueryInterface(Ci.nsIBrowserHistory).
michael@0 263 removeVisitsByTimeframe(NOW - 10, NOW);
michael@0 264
michael@0 265 print("URI should still exist in moz_places.");
michael@0 266 do_check_true(page_in_database(PLACE_URL));
michael@0 267
michael@0 268 print("Run a history query and check that no visits exist.");
michael@0 269 var query = histsvc.getNewQuery();
michael@0 270 var opts = histsvc.getNewQueryOptions();
michael@0 271 opts.resultType = opts.RESULTS_AS_VISIT;
michael@0 272 opts.sortingMode = opts.SORT_BY_DATE_DESCENDING;
michael@0 273 var resultRoot = histsvc.executeQuery(query, opts).root;
michael@0 274 resultRoot.containerOpen = true;
michael@0 275 do_check_eq(resultRoot.childCount, 0);
michael@0 276 resultRoot.containerOpen = false;
michael@0 277
michael@0 278 print("asyncHistory.isURIVisited should return false.");
michael@0 279 PlacesUtils.asyncHistory.isURIVisited(PLACE_URI, function(aURI, aIsVisited) {
michael@0 280 do_check_false(aIsVisited);
michael@0 281
michael@0 282 promiseAsyncUpdates().then(function () {
michael@0 283 print("Frecency should be zero.")
michael@0 284 do_check_eq(frecencyForUrl(PLACE_URL), 0);
michael@0 285 run_next_test();
michael@0 286 });
michael@0 287 });
michael@0 288 }
michael@0 289 },
michael@0 290
michael@0 291 {
michael@0 292 desc: "Remove all visits from a bookmarked URI",
michael@0 293 run: function () {
michael@0 294 print("Add some visits for the URI.");
michael@0 295 let visits = [];
michael@0 296 for (let i = 0; i < 10; i++) {
michael@0 297 visits.push({ uri: TEST_URI, visitDate: NOW - i });
michael@0 298 }
michael@0 299 promiseAddVisits(visits).then(function () {
michael@0 300 print("Bookmark the URI.");
michael@0 301 bmsvc.insertBookmark(bmsvc.unfiledBookmarksFolder,
michael@0 302 TEST_URI,
michael@0 303 bmsvc.DEFAULT_INDEX,
michael@0 304 "bookmark title");
michael@0 305 promiseAsyncUpdates().then(this.continue_run.bind(this));
michael@0 306 }.bind(this));
michael@0 307 },
michael@0 308 continue_run: function () {
michael@0 309 print("Remove all visits.");
michael@0 310 histsvc.QueryInterface(Ci.nsIBrowserHistory).
michael@0 311 removeVisitsByTimeframe(NOW - 10, NOW);
michael@0 312
michael@0 313 print("URI should still exist in moz_places.");
michael@0 314 do_check_true(page_in_database(TEST_URL));
michael@0 315
michael@0 316 print("Run a history query and check that no visits exist.");
michael@0 317 var query = histsvc.getNewQuery();
michael@0 318 var opts = histsvc.getNewQueryOptions();
michael@0 319 opts.resultType = opts.RESULTS_AS_VISIT;
michael@0 320 opts.sortingMode = opts.SORT_BY_DATE_DESCENDING;
michael@0 321 var resultRoot = histsvc.executeQuery(query, opts).root;
michael@0 322 resultRoot.containerOpen = true;
michael@0 323 do_check_eq(resultRoot.childCount, 0);
michael@0 324 resultRoot.containerOpen = false;
michael@0 325
michael@0 326 print("asyncHistory.isURIVisited should return false.");
michael@0 327 PlacesUtils.asyncHistory.isURIVisited(TEST_URI, function(aURI, aIsVisited) {
michael@0 328 do_check_false(aIsVisited);
michael@0 329
michael@0 330 print("nsINavBookmarksService.isBookmarked should return true.");
michael@0 331 do_check_true(bmsvc.isBookmarked(TEST_URI));
michael@0 332
michael@0 333 promiseAsyncUpdates().then(function () {
michael@0 334 print("Frecency should be negative.")
michael@0 335 do_check_true(frecencyForUrl(TEST_URI) < 0);
michael@0 336 run_next_test();
michael@0 337 });
michael@0 338 });
michael@0 339 }
michael@0 340 },
michael@0 341
michael@0 342 {
michael@0 343 desc: "Remove some visits from a zero frecency URI retains zero frecency",
michael@0 344 run: function () {
michael@0 345 do_log_info("Add some visits for the URI.");
michael@0 346 promiseAddVisits([{ uri: TEST_URI, transition: TRANSITION_FRAMED_LINK,
michael@0 347 visitDate: (NOW - 86400000000) },
michael@0 348 { uri: TEST_URI, transition: TRANSITION_FRAMED_LINK,
michael@0 349 visitDate: NOW }]).then(
michael@0 350 this.continue_run.bind(this));
michael@0 351 },
michael@0 352 continue_run: function () {
michael@0 353 do_log_info("Remove newer visit.");
michael@0 354 histsvc.QueryInterface(Ci.nsIBrowserHistory).
michael@0 355 removeVisitsByTimeframe(NOW - 10, NOW);
michael@0 356
michael@0 357 promiseAsyncUpdates().then(function() {
michael@0 358 do_log_info("URI should still exist in moz_places.");
michael@0 359 do_check_true(page_in_database(TEST_URL));
michael@0 360 do_log_info("Frecency should be zero.")
michael@0 361 do_check_eq(frecencyForUrl(TEST_URI), 0);
michael@0 362 run_next_test();
michael@0 363 });
michael@0 364 }
michael@0 365 }
michael@0 366 ];
michael@0 367
michael@0 368 ///////////////////////////////////////////////////////////////////////////////
michael@0 369
michael@0 370 function run_test()
michael@0 371 {
michael@0 372 do_test_pending();
michael@0 373 run_next_test();
michael@0 374 }
michael@0 375
michael@0 376 function run_next_test() {
michael@0 377 if (tests.length) {
michael@0 378 let test = tests.shift();
michael@0 379 print("\n ***Test: " + test.desc);
michael@0 380 promiseClearHistory().then(function() {
michael@0 381 remove_all_bookmarks();
michael@0 382 DBConn().executeSimpleSQL("DELETE FROM moz_places");
michael@0 383 test.run.call(test);
michael@0 384 });
michael@0 385 }
michael@0 386 else {
michael@0 387 do_test_finished();
michael@0 388 }
michael@0 389 }

mercurial