1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/bookmarks/test_async_observers.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,175 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +/* This test checks that bookmarks service is correctly forwarding async 1.8 + * events like visit or favicon additions. */ 1.9 + 1.10 +const NOW = Date.now() * 1000; 1.11 + 1.12 +let observer = { 1.13 + bookmarks: [], 1.14 + observedBookmarks: 0, 1.15 + observedVisitId: 0, 1.16 + deferred: null, 1.17 + 1.18 + /** 1.19 + * Returns a promise that is resolved when the observer determines that the 1.20 + * test can continue. This is required rather than calling run_next_test 1.21 + * directly in the observer because there are cases where we must wait for 1.22 + * other asynchronous events to be completed in addition to this. 1.23 + */ 1.24 + setupCompletionPromise: function () 1.25 + { 1.26 + this.observedBookmarks = 0; 1.27 + this.deferred = Promise.defer(); 1.28 + return this.deferred.promise; 1.29 + }, 1.30 + 1.31 + onBeginUpdateBatch: function () {}, 1.32 + onEndUpdateBatch: function () {}, 1.33 + onItemAdded: function () {}, 1.34 + onItemRemoved: function () {}, 1.35 + onItemMoved: function () {}, 1.36 + onItemChanged: function(aItemId, aProperty, aIsAnnotation, aNewValue, 1.37 + aLastModified, aItemType) 1.38 + { 1.39 + do_log_info("Check that we got the correct change information."); 1.40 + do_check_neq(this.bookmarks.indexOf(aItemId), -1); 1.41 + if (aProperty == "favicon") { 1.42 + do_check_false(aIsAnnotation); 1.43 + do_check_eq(aNewValue, SMALLPNG_DATA_URI.spec); 1.44 + do_check_eq(aLastModified, 0); 1.45 + do_check_eq(aItemType, PlacesUtils.bookmarks.TYPE_BOOKMARK); 1.46 + } 1.47 + else if (aProperty == "cleartime") { 1.48 + do_check_false(aIsAnnotation); 1.49 + do_check_eq(aNewValue, ""); 1.50 + do_check_eq(aLastModified, 0); 1.51 + do_check_eq(aItemType, PlacesUtils.bookmarks.TYPE_BOOKMARK); 1.52 + } 1.53 + else { 1.54 + do_throw("Unexpected property change " + aProperty); 1.55 + } 1.56 + 1.57 + if (++this.observedBookmarks == this.bookmarks.length) { 1.58 + this.deferred.resolve(); 1.59 + } 1.60 + }, 1.61 + onItemVisited: function(aItemId, aVisitId, aTime) 1.62 + { 1.63 + do_log_info("Check that we got the correct visit information."); 1.64 + do_check_neq(this.bookmarks.indexOf(aItemId), -1); 1.65 + this.observedVisitId = aVisitId; 1.66 + do_check_eq(aTime, NOW); 1.67 + if (++this.observedBookmarks == this.bookmarks.length) { 1.68 + this.deferred.resolve(); 1.69 + } 1.70 + }, 1.71 + 1.72 + QueryInterface: XPCOMUtils.generateQI([ 1.73 + Ci.nsINavBookmarkObserver, 1.74 + ]) 1.75 +}; 1.76 +PlacesUtils.bookmarks.addObserver(observer, false); 1.77 + 1.78 +add_task(function test_add_visit() 1.79 +{ 1.80 + let observerPromise = observer.setupCompletionPromise(); 1.81 + 1.82 + // Add a visit to the bookmark and wait for the observer. 1.83 + let visitId; 1.84 + let deferUpdatePlaces = Promise.defer(); 1.85 + PlacesUtils.asyncHistory.updatePlaces({ 1.86 + uri: NetUtil.newURI("http://book.ma.rk/"), 1.87 + visits: [{ transitionType: TRANSITION_TYPED, visitDate: NOW }] 1.88 + }, { 1.89 + handleError: function TAV_handleError() { 1.90 + deferUpdatePlaces.reject(new Error("Unexpected error in adding visit.")); 1.91 + }, 1.92 + handleResult: function (aPlaceInfo) { 1.93 + visitId = aPlaceInfo.visits[0].visitId; 1.94 + }, 1.95 + handleCompletion: function TAV_handleCompletion() { 1.96 + deferUpdatePlaces.resolve(); 1.97 + } 1.98 + }); 1.99 + 1.100 + // Wait for both the observer and the asynchronous update, in any order. 1.101 + yield deferUpdatePlaces.promise; 1.102 + yield observerPromise; 1.103 + 1.104 + // Check that both asynchronous results are consistent. 1.105 + do_check_eq(observer.observedVisitId, visitId); 1.106 +}); 1.107 + 1.108 +add_task(function test_add_icon() 1.109 +{ 1.110 + let observerPromise = observer.setupCompletionPromise(); 1.111 + PlacesUtils.favicons.setAndFetchFaviconForPage(NetUtil.newURI("http://book.ma.rk/"), 1.112 + SMALLPNG_DATA_URI, true, 1.113 + PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE); 1.114 + yield observerPromise; 1.115 +}); 1.116 + 1.117 +add_task(function test_remove_page() 1.118 +{ 1.119 + let observerPromise = observer.setupCompletionPromise(); 1.120 + PlacesUtils.history.removePage(NetUtil.newURI("http://book.ma.rk/")); 1.121 + yield observerPromise; 1.122 +}); 1.123 + 1.124 +add_task(function cleanup() 1.125 +{ 1.126 + PlacesUtils.bookmarks.removeObserver(observer, false); 1.127 +}); 1.128 + 1.129 +add_task(function shutdown() 1.130 +{ 1.131 + // Check that async observers don't try to create async statements after 1.132 + // shutdown. That would cause assertions, since the async thread is gone 1.133 + // already. Note that in such a case the notifications are not fired, so we 1.134 + // cannot test for them. 1.135 + // Put an history notification that triggers AsyncGetBookmarksForURI between 1.136 + // asyncClose() and the actual connection closing. Enqueuing a main-thread 1.137 + // event just after places-will-close-connection should ensure it runs before 1.138 + // places-connection-closed. 1.139 + // Notice this code is not using helpers cause it depends on a very specific 1.140 + // order, a change in the helpers code could make this test useless. 1.141 + let deferred = Promise.defer(); 1.142 + 1.143 + Services.obs.addObserver(function onNotification() { 1.144 + Services.obs.removeObserver(onNotification, "places-will-close-connection"); 1.145 + do_check_true(true, "Observed fake places shutdown"); 1.146 + 1.147 + Services.tm.mainThread.dispatch(() => { 1.148 + // WARNING: this is very bad, never use out of testing code. 1.149 + PlacesUtils.bookmarks.QueryInterface(Ci.nsINavHistoryObserver) 1.150 + .onPageChanged(NetUtil.newURI("http://book.ma.rk/"), 1.151 + Ci.nsINavHistoryObserver.ATTRIBUTE_FAVICON, 1.152 + "test", "test"); 1.153 + deferred.resolve(promiseTopicObserved("places-connection-closed")); 1.154 + }, Ci.nsIThread.DISPATCH_NORMAL); 1.155 + }, "places-will-close-connection", false); 1.156 + shutdownPlaces(); 1.157 + 1.158 + yield deferred.promise; 1.159 +}); 1.160 + 1.161 +function run_test() 1.162 +{ 1.163 + // Add multiple bookmarks to the same uri. 1.164 + observer.bookmarks.push( 1.165 + PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, 1.166 + NetUtil.newURI("http://book.ma.rk/"), 1.167 + PlacesUtils.bookmarks.DEFAULT_INDEX, 1.168 + "Bookmark") 1.169 + ); 1.170 + observer.bookmarks.push( 1.171 + PlacesUtils.bookmarks.insertBookmark(PlacesUtils.toolbarFolderId, 1.172 + NetUtil.newURI("http://book.ma.rk/"), 1.173 + PlacesUtils.bookmarks.DEFAULT_INDEX, 1.174 + "Bookmark") 1.175 + ); 1.176 + 1.177 + run_next_test(); 1.178 +}