toolkit/components/places/tests/bookmarks/test_async_observers.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 /* This test checks that bookmarks service is correctly forwarding async
michael@0 5 * events like visit or favicon additions. */
michael@0 6
michael@0 7 const NOW = Date.now() * 1000;
michael@0 8
michael@0 9 let observer = {
michael@0 10 bookmarks: [],
michael@0 11 observedBookmarks: 0,
michael@0 12 observedVisitId: 0,
michael@0 13 deferred: null,
michael@0 14
michael@0 15 /**
michael@0 16 * Returns a promise that is resolved when the observer determines that the
michael@0 17 * test can continue. This is required rather than calling run_next_test
michael@0 18 * directly in the observer because there are cases where we must wait for
michael@0 19 * other asynchronous events to be completed in addition to this.
michael@0 20 */
michael@0 21 setupCompletionPromise: function ()
michael@0 22 {
michael@0 23 this.observedBookmarks = 0;
michael@0 24 this.deferred = Promise.defer();
michael@0 25 return this.deferred.promise;
michael@0 26 },
michael@0 27
michael@0 28 onBeginUpdateBatch: function () {},
michael@0 29 onEndUpdateBatch: function () {},
michael@0 30 onItemAdded: function () {},
michael@0 31 onItemRemoved: function () {},
michael@0 32 onItemMoved: function () {},
michael@0 33 onItemChanged: function(aItemId, aProperty, aIsAnnotation, aNewValue,
michael@0 34 aLastModified, aItemType)
michael@0 35 {
michael@0 36 do_log_info("Check that we got the correct change information.");
michael@0 37 do_check_neq(this.bookmarks.indexOf(aItemId), -1);
michael@0 38 if (aProperty == "favicon") {
michael@0 39 do_check_false(aIsAnnotation);
michael@0 40 do_check_eq(aNewValue, SMALLPNG_DATA_URI.spec);
michael@0 41 do_check_eq(aLastModified, 0);
michael@0 42 do_check_eq(aItemType, PlacesUtils.bookmarks.TYPE_BOOKMARK);
michael@0 43 }
michael@0 44 else if (aProperty == "cleartime") {
michael@0 45 do_check_false(aIsAnnotation);
michael@0 46 do_check_eq(aNewValue, "");
michael@0 47 do_check_eq(aLastModified, 0);
michael@0 48 do_check_eq(aItemType, PlacesUtils.bookmarks.TYPE_BOOKMARK);
michael@0 49 }
michael@0 50 else {
michael@0 51 do_throw("Unexpected property change " + aProperty);
michael@0 52 }
michael@0 53
michael@0 54 if (++this.observedBookmarks == this.bookmarks.length) {
michael@0 55 this.deferred.resolve();
michael@0 56 }
michael@0 57 },
michael@0 58 onItemVisited: function(aItemId, aVisitId, aTime)
michael@0 59 {
michael@0 60 do_log_info("Check that we got the correct visit information.");
michael@0 61 do_check_neq(this.bookmarks.indexOf(aItemId), -1);
michael@0 62 this.observedVisitId = aVisitId;
michael@0 63 do_check_eq(aTime, NOW);
michael@0 64 if (++this.observedBookmarks == this.bookmarks.length) {
michael@0 65 this.deferred.resolve();
michael@0 66 }
michael@0 67 },
michael@0 68
michael@0 69 QueryInterface: XPCOMUtils.generateQI([
michael@0 70 Ci.nsINavBookmarkObserver,
michael@0 71 ])
michael@0 72 };
michael@0 73 PlacesUtils.bookmarks.addObserver(observer, false);
michael@0 74
michael@0 75 add_task(function test_add_visit()
michael@0 76 {
michael@0 77 let observerPromise = observer.setupCompletionPromise();
michael@0 78
michael@0 79 // Add a visit to the bookmark and wait for the observer.
michael@0 80 let visitId;
michael@0 81 let deferUpdatePlaces = Promise.defer();
michael@0 82 PlacesUtils.asyncHistory.updatePlaces({
michael@0 83 uri: NetUtil.newURI("http://book.ma.rk/"),
michael@0 84 visits: [{ transitionType: TRANSITION_TYPED, visitDate: NOW }]
michael@0 85 }, {
michael@0 86 handleError: function TAV_handleError() {
michael@0 87 deferUpdatePlaces.reject(new Error("Unexpected error in adding visit."));
michael@0 88 },
michael@0 89 handleResult: function (aPlaceInfo) {
michael@0 90 visitId = aPlaceInfo.visits[0].visitId;
michael@0 91 },
michael@0 92 handleCompletion: function TAV_handleCompletion() {
michael@0 93 deferUpdatePlaces.resolve();
michael@0 94 }
michael@0 95 });
michael@0 96
michael@0 97 // Wait for both the observer and the asynchronous update, in any order.
michael@0 98 yield deferUpdatePlaces.promise;
michael@0 99 yield observerPromise;
michael@0 100
michael@0 101 // Check that both asynchronous results are consistent.
michael@0 102 do_check_eq(observer.observedVisitId, visitId);
michael@0 103 });
michael@0 104
michael@0 105 add_task(function test_add_icon()
michael@0 106 {
michael@0 107 let observerPromise = observer.setupCompletionPromise();
michael@0 108 PlacesUtils.favicons.setAndFetchFaviconForPage(NetUtil.newURI("http://book.ma.rk/"),
michael@0 109 SMALLPNG_DATA_URI, true,
michael@0 110 PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE);
michael@0 111 yield observerPromise;
michael@0 112 });
michael@0 113
michael@0 114 add_task(function test_remove_page()
michael@0 115 {
michael@0 116 let observerPromise = observer.setupCompletionPromise();
michael@0 117 PlacesUtils.history.removePage(NetUtil.newURI("http://book.ma.rk/"));
michael@0 118 yield observerPromise;
michael@0 119 });
michael@0 120
michael@0 121 add_task(function cleanup()
michael@0 122 {
michael@0 123 PlacesUtils.bookmarks.removeObserver(observer, false);
michael@0 124 });
michael@0 125
michael@0 126 add_task(function shutdown()
michael@0 127 {
michael@0 128 // Check that async observers don't try to create async statements after
michael@0 129 // shutdown. That would cause assertions, since the async thread is gone
michael@0 130 // already. Note that in such a case the notifications are not fired, so we
michael@0 131 // cannot test for them.
michael@0 132 // Put an history notification that triggers AsyncGetBookmarksForURI between
michael@0 133 // asyncClose() and the actual connection closing. Enqueuing a main-thread
michael@0 134 // event just after places-will-close-connection should ensure it runs before
michael@0 135 // places-connection-closed.
michael@0 136 // Notice this code is not using helpers cause it depends on a very specific
michael@0 137 // order, a change in the helpers code could make this test useless.
michael@0 138 let deferred = Promise.defer();
michael@0 139
michael@0 140 Services.obs.addObserver(function onNotification() {
michael@0 141 Services.obs.removeObserver(onNotification, "places-will-close-connection");
michael@0 142 do_check_true(true, "Observed fake places shutdown");
michael@0 143
michael@0 144 Services.tm.mainThread.dispatch(() => {
michael@0 145 // WARNING: this is very bad, never use out of testing code.
michael@0 146 PlacesUtils.bookmarks.QueryInterface(Ci.nsINavHistoryObserver)
michael@0 147 .onPageChanged(NetUtil.newURI("http://book.ma.rk/"),
michael@0 148 Ci.nsINavHistoryObserver.ATTRIBUTE_FAVICON,
michael@0 149 "test", "test");
michael@0 150 deferred.resolve(promiseTopicObserved("places-connection-closed"));
michael@0 151 }, Ci.nsIThread.DISPATCH_NORMAL);
michael@0 152 }, "places-will-close-connection", false);
michael@0 153 shutdownPlaces();
michael@0 154
michael@0 155 yield deferred.promise;
michael@0 156 });
michael@0 157
michael@0 158 function run_test()
michael@0 159 {
michael@0 160 // Add multiple bookmarks to the same uri.
michael@0 161 observer.bookmarks.push(
michael@0 162 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
michael@0 163 NetUtil.newURI("http://book.ma.rk/"),
michael@0 164 PlacesUtils.bookmarks.DEFAULT_INDEX,
michael@0 165 "Bookmark")
michael@0 166 );
michael@0 167 observer.bookmarks.push(
michael@0 168 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.toolbarFolderId,
michael@0 169 NetUtil.newURI("http://book.ma.rk/"),
michael@0 170 PlacesUtils.bookmarks.DEFAULT_INDEX,
michael@0 171 "Bookmark")
michael@0 172 );
michael@0 173
michael@0 174 run_next_test();
michael@0 175 }

mercurial