toolkit/components/places/tests/unit/test_download_history.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 /**
michael@0 5 * This file tests the nsIDownloadHistory Places implementation.
michael@0 6 */
michael@0 7
michael@0 8 XPCOMUtils.defineLazyServiceGetter(this, "gDownloadHistory",
michael@0 9 "@mozilla.org/browser/download-history;1",
michael@0 10 "nsIDownloadHistory");
michael@0 11
michael@0 12 const DOWNLOAD_URI = NetUtil.newURI("http://www.example.com/");
michael@0 13 const REFERRER_URI = NetUtil.newURI("http://www.example.org/");
michael@0 14 const PRIVATE_URI = NetUtil.newURI("http://www.example.net/");
michael@0 15
michael@0 16 /**
michael@0 17 * Waits for the first visit notification to be received.
michael@0 18 *
michael@0 19 * @param aCallback
michael@0 20 * Callback function to be called with the same arguments of onVisit.
michael@0 21 */
michael@0 22 function waitForOnVisit(aCallback) {
michael@0 23 let historyObserver = {
michael@0 24 __proto__: NavHistoryObserver.prototype,
michael@0 25 onVisit: function HO_onVisit() {
michael@0 26 PlacesUtils.history.removeObserver(this);
michael@0 27 aCallback.apply(null, arguments);
michael@0 28 }
michael@0 29 };
michael@0 30 PlacesUtils.history.addObserver(historyObserver, false);
michael@0 31 }
michael@0 32
michael@0 33 /**
michael@0 34 * Waits for the first onDeleteURI notification to be received.
michael@0 35 *
michael@0 36 * @param aCallback
michael@0 37 * Callback function to be called with the same arguments of onDeleteURI.
michael@0 38 */
michael@0 39 function waitForOnDeleteURI(aCallback) {
michael@0 40 let historyObserver = {
michael@0 41 __proto__: NavHistoryObserver.prototype,
michael@0 42 onDeleteURI: function HO_onDeleteURI() {
michael@0 43 PlacesUtils.history.removeObserver(this);
michael@0 44 aCallback.apply(null, arguments);
michael@0 45 }
michael@0 46 };
michael@0 47 PlacesUtils.history.addObserver(historyObserver, false);
michael@0 48 }
michael@0 49
michael@0 50 /**
michael@0 51 * Waits for the first onDeleteVisits notification to be received.
michael@0 52 *
michael@0 53 * @param aCallback
michael@0 54 * Callback function to be called with the same arguments of onDeleteVisits.
michael@0 55 */
michael@0 56 function waitForOnDeleteVisits(aCallback) {
michael@0 57 let historyObserver = {
michael@0 58 __proto__: NavHistoryObserver.prototype,
michael@0 59 onDeleteVisits: function HO_onDeleteVisits() {
michael@0 60 PlacesUtils.history.removeObserver(this);
michael@0 61 aCallback.apply(null, arguments);
michael@0 62 }
michael@0 63 };
michael@0 64 PlacesUtils.history.addObserver(historyObserver, false);
michael@0 65 }
michael@0 66
michael@0 67 function run_test()
michael@0 68 {
michael@0 69 run_next_test();
michael@0 70 }
michael@0 71
michael@0 72 add_test(function test_dh_is_from_places()
michael@0 73 {
michael@0 74 // Test that this nsIDownloadHistory is the one places implements.
michael@0 75 do_check_true(gDownloadHistory instanceof Ci.mozIAsyncHistory);
michael@0 76
michael@0 77 run_next_test();
michael@0 78 });
michael@0 79
michael@0 80 add_test(function test_dh_addRemoveDownload()
michael@0 81 {
michael@0 82 waitForOnVisit(function DHAD_onVisit(aURI) {
michael@0 83 do_check_true(aURI.equals(DOWNLOAD_URI));
michael@0 84
michael@0 85 // Verify that the URI is already available in results at this time.
michael@0 86 do_check_true(!!page_in_database(DOWNLOAD_URI));
michael@0 87
michael@0 88 waitForOnDeleteURI(function DHRAD_onDeleteURI(aURI) {
michael@0 89 do_check_true(aURI.equals(DOWNLOAD_URI));
michael@0 90
michael@0 91 // Verify that the URI is already available in results at this time.
michael@0 92 do_check_false(!!page_in_database(DOWNLOAD_URI));
michael@0 93
michael@0 94 run_next_test();
michael@0 95 });
michael@0 96 gDownloadHistory.removeAllDownloads();
michael@0 97 });
michael@0 98
michael@0 99 gDownloadHistory.addDownload(DOWNLOAD_URI, null, Date.now() * 1000);
michael@0 100 });
michael@0 101
michael@0 102 add_test(function test_dh_addMultiRemoveDownload()
michael@0 103 {
michael@0 104 promiseAddVisits({ uri: DOWNLOAD_URI,
michael@0 105 transition: TRANSITION_TYPED }).then(function () {
michael@0 106 waitForOnVisit(function DHAD_onVisit(aURI) {
michael@0 107 do_check_true(aURI.equals(DOWNLOAD_URI));
michael@0 108 do_check_true(!!page_in_database(DOWNLOAD_URI));
michael@0 109
michael@0 110 waitForOnDeleteVisits(function DHRAD_onDeleteVisits(aURI) {
michael@0 111 do_check_true(aURI.equals(DOWNLOAD_URI));
michael@0 112 do_check_true(!!page_in_database(DOWNLOAD_URI));
michael@0 113
michael@0 114 promiseClearHistory().then(run_next_test);
michael@0 115 });
michael@0 116 gDownloadHistory.removeAllDownloads();
michael@0 117 });
michael@0 118
michael@0 119 gDownloadHistory.addDownload(DOWNLOAD_URI, null, Date.now() * 1000);
michael@0 120 });
michael@0 121 });
michael@0 122
michael@0 123 add_test(function test_dh_addBookmarkRemoveDownload()
michael@0 124 {
michael@0 125 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
michael@0 126 DOWNLOAD_URI,
michael@0 127 PlacesUtils.bookmarks.DEFAULT_INDEX,
michael@0 128 "A bookmark");
michael@0 129 waitForOnVisit(function DHAD_onVisit(aURI) {
michael@0 130 do_check_true(aURI.equals(DOWNLOAD_URI));
michael@0 131 do_check_true(!!page_in_database(DOWNLOAD_URI));
michael@0 132
michael@0 133 waitForOnDeleteVisits(function DHRAD_onDeleteVisits(aURI) {
michael@0 134 do_check_true(aURI.equals(DOWNLOAD_URI));
michael@0 135 do_check_true(!!page_in_database(DOWNLOAD_URI));
michael@0 136
michael@0 137 promiseClearHistory().then(run_next_test);
michael@0 138 });
michael@0 139 gDownloadHistory.removeAllDownloads();
michael@0 140 });
michael@0 141
michael@0 142 gDownloadHistory.addDownload(DOWNLOAD_URI, null, Date.now() * 1000);
michael@0 143 });
michael@0 144
michael@0 145 add_test(function test_dh_addDownload_referrer()
michael@0 146 {
michael@0 147 waitForOnVisit(function DHAD_prepareReferrer(aURI, aVisitID) {
michael@0 148 do_check_true(aURI.equals(REFERRER_URI));
michael@0 149 let referrerVisitId = aVisitID;
michael@0 150
michael@0 151 waitForOnVisit(function DHAD_onVisit(aURI, aVisitID, aTime, aSessionID,
michael@0 152 aReferringID) {
michael@0 153 do_check_true(aURI.equals(DOWNLOAD_URI));
michael@0 154 do_check_eq(aReferringID, referrerVisitId);
michael@0 155
michael@0 156 // Verify that the URI is already available in results at this time.
michael@0 157 do_check_true(!!page_in_database(DOWNLOAD_URI));
michael@0 158
michael@0 159 promiseClearHistory().then(run_next_test);
michael@0 160 });
michael@0 161
michael@0 162 gDownloadHistory.addDownload(DOWNLOAD_URI, REFERRER_URI, Date.now() * 1000);
michael@0 163 });
michael@0 164
michael@0 165 // Note that we don't pass the optional callback argument here because we must
michael@0 166 // ensure that we receive the onVisit notification before we call addDownload.
michael@0 167 PlacesUtils.asyncHistory.updatePlaces({
michael@0 168 uri: REFERRER_URI,
michael@0 169 visits: [{
michael@0 170 transitionType: Ci.nsINavHistoryService.TRANSITION_TYPED,
michael@0 171 visitDate: Date.now() * 1000
michael@0 172 }]
michael@0 173 });
michael@0 174 });
michael@0 175
michael@0 176 add_test(function test_dh_addDownload_disabledHistory()
michael@0 177 {
michael@0 178 waitForOnVisit(function DHAD_onVisit(aURI) {
michael@0 179 // We should only receive the notification for the non-private URI. This
michael@0 180 // test is based on the assumption that visit notifications are received in
michael@0 181 // the same order of the addDownload calls, which is currently true because
michael@0 182 // database access is serialized on the same worker thread.
michael@0 183 do_check_true(aURI.equals(DOWNLOAD_URI));
michael@0 184
michael@0 185 do_check_true(!!page_in_database(DOWNLOAD_URI));
michael@0 186 do_check_false(!!page_in_database(PRIVATE_URI));
michael@0 187
michael@0 188 promiseClearHistory().then(run_next_test);
michael@0 189 });
michael@0 190
michael@0 191 Services.prefs.setBoolPref("places.history.enabled", false);
michael@0 192 gDownloadHistory.addDownload(PRIVATE_URI, REFERRER_URI, Date.now() * 1000);
michael@0 193
michael@0 194 // The addDownload functions calls CanAddURI synchronously, thus we can set
michael@0 195 // the preference back to true immediately (not all apps enable places by
michael@0 196 // default).
michael@0 197 Services.prefs.setBoolPref("places.history.enabled", true);
michael@0 198 gDownloadHistory.addDownload(DOWNLOAD_URI, REFERRER_URI, Date.now() * 1000);
michael@0 199 });
michael@0 200
michael@0 201 /**
michael@0 202 * Tests that nsIDownloadHistory::AddDownload saves the additional download
michael@0 203 * details if the optional destination URL is specified.
michael@0 204 */
michael@0 205 add_test(function test_dh_details()
michael@0 206 {
michael@0 207 const REMOTE_URI = NetUtil.newURI("http://localhost/");
michael@0 208 const SOURCE_URI = NetUtil.newURI("http://example.com/test_dh_details");
michael@0 209 const DEST_FILE_NAME = "dest.txt";
michael@0 210
michael@0 211 // We must build a real, valid file URI for the destination.
michael@0 212 let destFileUri = NetUtil.newURI(FileUtils.getFile("TmpD", [DEST_FILE_NAME]));
michael@0 213
michael@0 214 let titleSet = false;
michael@0 215 let destinationFileUriSet = false;
michael@0 216 let destinationFileNameSet = false;
michael@0 217
michael@0 218 function checkFinished()
michael@0 219 {
michael@0 220 if (titleSet && destinationFileUriSet && destinationFileNameSet) {
michael@0 221 PlacesUtils.annotations.removeObserver(annoObserver);
michael@0 222 PlacesUtils.history.removeObserver(historyObserver);
michael@0 223
michael@0 224 promiseClearHistory().then(run_next_test);
michael@0 225 }
michael@0 226 };
michael@0 227
michael@0 228 let annoObserver = {
michael@0 229 onPageAnnotationSet: function AO_onPageAnnotationSet(aPage, aName)
michael@0 230 {
michael@0 231 if (aPage.equals(SOURCE_URI)) {
michael@0 232 let value = PlacesUtils.annotations.getPageAnnotation(aPage, aName);
michael@0 233 switch (aName)
michael@0 234 {
michael@0 235 case "downloads/destinationFileURI":
michael@0 236 destinationFileUriSet = true;
michael@0 237 do_check_eq(value, destFileUri.spec);
michael@0 238 break;
michael@0 239 case "downloads/destinationFileName":
michael@0 240 destinationFileNameSet = true;
michael@0 241 do_check_eq(value, DEST_FILE_NAME);
michael@0 242 break;
michael@0 243 }
michael@0 244 checkFinished();
michael@0 245 }
michael@0 246 },
michael@0 247 onItemAnnotationSet: function() {},
michael@0 248 onPageAnnotationRemoved: function() {},
michael@0 249 onItemAnnotationRemoved: function() {}
michael@0 250 }
michael@0 251
michael@0 252 let historyObserver = {
michael@0 253 onBeginUpdateBatch: function() {},
michael@0 254 onEndUpdateBatch: function() {},
michael@0 255 onVisit: function() {},
michael@0 256 onTitleChanged: function HO_onTitleChanged(aURI, aPageTitle)
michael@0 257 {
michael@0 258 if (aURI.equals(SOURCE_URI)) {
michael@0 259 titleSet = true;
michael@0 260 do_check_eq(aPageTitle, DEST_FILE_NAME);
michael@0 261 checkFinished();
michael@0 262 }
michael@0 263 },
michael@0 264 onDeleteURI: function() {},
michael@0 265 onClearHistory: function() {},
michael@0 266 onPageChanged: function() {},
michael@0 267 onDeleteVisits: function() {}
michael@0 268 };
michael@0 269
michael@0 270 PlacesUtils.annotations.addObserver(annoObserver, false);
michael@0 271 PlacesUtils.history.addObserver(historyObserver, false);
michael@0 272
michael@0 273 // Both null values and remote URIs should not cause errors.
michael@0 274 gDownloadHistory.addDownload(SOURCE_URI, null, Date.now() * 1000);
michael@0 275 gDownloadHistory.addDownload(SOURCE_URI, null, Date.now() * 1000, null);
michael@0 276 gDownloadHistory.addDownload(SOURCE_URI, null, Date.now() * 1000, REMOTE_URI);
michael@0 277
michael@0 278 // Valid local file URIs should cause the download details to be saved.
michael@0 279 gDownloadHistory.addDownload(SOURCE_URI, null, Date.now() * 1000,
michael@0 280 destFileUri);
michael@0 281 });

mercurial