|
12 | 12 | //////////////////////////////////////////////////////////////////////////////// |
13 | 13 | //// Globals |
14 | 14 |
|
| 15 | +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
15 | 16 | Cu.import("resource://gre/modules/Services.jsm"); |
16 | 17 | Cu.import("resource://gre/modules/PlacesUtils.jsm"); |
17 | 18 | Cu.import("resource://gre/modules/ForgetAboutSite.jsm"); |
18 | 19 |
|
| 20 | +XPCOMUtils.defineLazyModuleGetter(this, "Promise", |
| 21 | + "resource://gre/modules/commonjs/promise/core.js"); |
| 22 | + |
19 | 23 | const COOKIE_EXPIRY = Math.round(Date.now() / 1000) + 60; |
20 | 24 | const COOKIE_NAME = "testcookie"; |
21 | 25 | const COOKIE_PATH = "/"; |
@@ -48,32 +52,87 @@ function uri(aURIString) |
48 | 52 | } |
49 | 53 |
|
50 | 54 | /** |
51 | | - * Adds a visit to history. |
| 55 | + * Asynchronously adds visits to a page. |
52 | 56 | * |
53 | | - * @param aURI |
54 | | - * The URI to add. |
| 57 | + * @param aPlaceInfo |
| 58 | + * Can be an nsIURI, in such a case a single LINK visit will be added. |
| 59 | + * Otherwise can be an object describing the visit to add, or an array |
| 60 | + * of these objects: |
| 61 | + * { uri: nsIURI of the page, |
| 62 | + * transition: one of the TRANSITION_* from nsINavHistoryService, |
| 63 | + * [optional] title: title of the page, |
| 64 | + * [optional] visitDate: visit date in microseconds from the epoch |
| 65 | + * [optional] referrer: nsIURI of the referrer for this visit |
| 66 | + * } |
| 67 | + * |
| 68 | + * @return {Promise} |
| 69 | + * @resolves When all visits have been added successfully. |
| 70 | + * @rejects JavaScript exception. |
55 | 71 | */ |
56 | | -function add_visit(aURI) |
| 72 | +function promiseAddVisits(aPlaceInfo) |
57 | 73 | { |
58 | | - check_visited(aURI, false); |
59 | | - PlacesUtils.history.addVisit(aURI, Date.now() * 1000, null, |
60 | | - Ci.nsINavHistoryService.TRANSITION_LINK, false, |
61 | | - 0); |
62 | | - check_visited(aURI, true); |
| 74 | + let deferred = Promise.defer(); |
| 75 | + let places = []; |
| 76 | + if (aPlaceInfo instanceof Ci.nsIURI) { |
| 77 | + places.push({ uri: aPlaceInfo }); |
| 78 | + } |
| 79 | + else if (Array.isArray(aPlaceInfo)) { |
| 80 | + places = places.concat(aPlaceInfo); |
| 81 | + } else { |
| 82 | + places.push(aPlaceInfo) |
| 83 | + } |
| 84 | + |
| 85 | + // Create mozIVisitInfo for each entry. |
| 86 | + let now = Date.now(); |
| 87 | + for (let i = 0; i < places.length; i++) { |
| 88 | + if (!places[i].title) { |
| 89 | + places[i].title = "test visit for " + places[i].uri.spec; |
| 90 | + } |
| 91 | + places[i].visits = [{ |
| 92 | + transitionType: places[i].transition === undefined ? Ci.nsINavHistoryService.TRANSITION_LINK |
| 93 | + : places[i].transition, |
| 94 | + visitDate: places[i].visitDate || (now++) * 1000, |
| 95 | + referrerURI: places[i].referrer |
| 96 | + }]; |
| 97 | + } |
| 98 | + |
| 99 | + PlacesUtils.asyncHistory.updatePlaces( |
| 100 | + places, |
| 101 | + { |
| 102 | + handleError: function handleError(aResultCode, aPlaceInfo) { |
| 103 | + let ex = new Components.Exception("Unexpected error in adding visits.", |
| 104 | + aResultCode); |
| 105 | + deferred.reject(ex); |
| 106 | + }, |
| 107 | + handleResult: function () {}, |
| 108 | + handleCompletion: function handleCompletion() { |
| 109 | + deferred.resolve(); |
| 110 | + } |
| 111 | + } |
| 112 | + ); |
| 113 | + |
| 114 | + return deferred.promise; |
63 | 115 | } |
64 | 116 |
|
| 117 | + |
65 | 118 | /** |
66 | | - * Checks to ensure a URI string is visited or not. |
| 119 | + * Asynchronously check a url is visited. |
67 | 120 | * |
68 | 121 | * @param aURI |
69 | | - * The URI to check. |
70 | | - * @param aIsVisited |
71 | | - * True if the URI should be visited, false otherwise. |
| 122 | + * The URI. |
| 123 | + * |
| 124 | + * @return {Promise} |
| 125 | + * @resolves When the check has been added successfully. |
| 126 | + * @rejects JavaScript exception. |
72 | 127 | */ |
73 | | -function check_visited(aURI, aIsVisited) |
| 128 | +function promiseIsURIVisited(aURI) |
74 | 129 | { |
75 | | - let checker = aIsVisited ? do_check_true : do_check_false; |
76 | | - checker(PlacesUtils.ghistory2.isVisited(aURI)); |
| 130 | + let deferred = Promise.defer(); |
| 131 | + PlacesUtils.asyncHistory.isURIVisited(aURI, function(aURI, aIsVisited) { |
| 132 | + deferred.resolve(aIsVisited); |
| 133 | + }); |
| 134 | + |
| 135 | + return deferred.promise; |
77 | 136 | } |
78 | 137 |
|
79 | 138 | /** |
@@ -323,25 +382,31 @@ function check_preference_exists(aURI, aExists) |
323 | 382 | function test_history_cleared_with_direct_match() |
324 | 383 | { |
325 | 384 | const TEST_URI = uri("http://mozilla.org/foo"); |
326 | | - add_visit(TEST_URI); |
| 385 | + do_check_false(yield promiseIsURIVisited(TEST_URI)); |
| 386 | + yield promiseAddVisits(TEST_URI); |
| 387 | + do_check_true(yield promiseIsURIVisited(TEST_URI)); |
327 | 388 | ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
328 | | - check_visited(TEST_URI, false); |
| 389 | + do_check_false(yield promiseIsURIVisited(TEST_URI)); |
329 | 390 | } |
330 | 391 |
|
331 | 392 | function test_history_cleared_with_subdomain() |
332 | 393 | { |
333 | 394 | const TEST_URI = uri("http://www.mozilla.org/foo"); |
334 | | - add_visit(TEST_URI); |
| 395 | + do_check_false(yield promiseIsURIVisited(TEST_URI)); |
| 396 | + yield promiseAddVisits(TEST_URI); |
| 397 | + do_check_true(yield promiseIsURIVisited(TEST_URI)); |
335 | 398 | ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
336 | | - check_visited(TEST_URI, false); |
| 399 | + do_check_false(yield promiseIsURIVisited(TEST_URI)); |
337 | 400 | } |
338 | 401 |
|
339 | 402 | function test_history_not_cleared_with_uri_contains_domain() |
340 | 403 | { |
341 | 404 | const TEST_URI = uri("http://ilovemozilla.org/foo"); |
342 | | - add_visit(TEST_URI); |
| 405 | + do_check_false(yield promiseIsURIVisited(TEST_URI)); |
| 406 | + yield promiseAddVisits(TEST_URI); |
| 407 | + do_check_true(yield promiseIsURIVisited(TEST_URI)); |
343 | 408 | ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
344 | | - check_visited(TEST_URI, true); |
| 409 | + do_check_true(yield promiseIsURIVisited(TEST_URI)); |
345 | 410 |
|
346 | 411 | // Clear history since we left something there from this test. |
347 | 412 | PlacesUtils.bhistory.removeAllPages(); |
@@ -544,6 +609,8 @@ function test_cache_cleared() |
544 | 609 | observe: function(aSubject, aTopic, aData) |
545 | 610 | { |
546 | 611 | os.removeObserver(observer, "cacheservice:empty-cache"); |
| 612 | + // Shutdown the download manager. |
| 613 | + Services.obs.notifyObservers(null, "quit-application", null); |
547 | 614 | do_test_finished(); |
548 | 615 | } |
549 | 616 | }; |
@@ -635,8 +702,7 @@ function run_test() |
635 | 702 | Services.prefs.setBoolPref("places.history.enabled", true); |
636 | 703 |
|
637 | 704 | for (let i = 0; i < tests.length; i++) |
638 | | - tests[i](); |
| 705 | + add_task(tests[i]); |
639 | 706 |
|
640 | | - // Shutdown the download manager. |
641 | | - Services.obs.notifyObservers(null, "quit-application", null); |
| 707 | + run_next_test(); |
642 | 708 | } |
0 commit comments