browser/components/places/tests/unit/test_clearHistory_shutdown.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 /**
michael@0 8 * Tests that requesting clear history at shutdown will really clear history.
michael@0 9 */
michael@0 10
michael@0 11 const URIS = [
michael@0 12 "http://a.example1.com/"
michael@0 13 , "http://b.example1.com/"
michael@0 14 , "http://b.example2.com/"
michael@0 15 , "http://c.example3.com/"
michael@0 16 ];
michael@0 17
michael@0 18 const TOPIC_CONNECTION_CLOSED = "places-connection-closed";
michael@0 19
michael@0 20 let EXPECTED_NOTIFICATIONS = [
michael@0 21 "places-shutdown"
michael@0 22 , "places-will-close-connection"
michael@0 23 , "places-expiration-finished"
michael@0 24 , "places-connection-closed"
michael@0 25 ];
michael@0 26
michael@0 27 const UNEXPECTED_NOTIFICATIONS = [
michael@0 28 "xpcom-shutdown"
michael@0 29 ];
michael@0 30
michael@0 31 const URL = "ftp://localhost/clearHistoryOnShutdown/";
michael@0 32
michael@0 33 // Send the profile-after-change notification to the form history component to ensure
michael@0 34 // that it has been initialized.
michael@0 35 var formHistoryStartup = Cc["@mozilla.org/satchel/form-history-startup;1"].
michael@0 36 getService(Ci.nsIObserver);
michael@0 37 formHistoryStartup.observe(null, "profile-after-change", null);
michael@0 38
michael@0 39 let notificationIndex = 0;
michael@0 40
michael@0 41 let notificationsObserver = {
michael@0 42 observe: function observe(aSubject, aTopic, aData) {
michael@0 43 print("Received notification: " + aTopic);
michael@0 44
michael@0 45 // Note that some of these notifications could arrive multiple times, for
michael@0 46 // example in case of sync, we allow that.
michael@0 47 if (EXPECTED_NOTIFICATIONS[notificationIndex] != aTopic)
michael@0 48 notificationIndex++;
michael@0 49 do_check_eq(EXPECTED_NOTIFICATIONS[notificationIndex], aTopic);
michael@0 50
michael@0 51 if (aTopic != TOPIC_CONNECTION_CLOSED)
michael@0 52 return;
michael@0 53
michael@0 54 getDistinctNotifications().forEach(
michael@0 55 function (topic) Services.obs.removeObserver(notificationsObserver, topic)
michael@0 56 );
michael@0 57
michael@0 58 print("Looking for uncleared stuff.");
michael@0 59
michael@0 60 let stmt = DBConn().createStatement(
michael@0 61 "SELECT id FROM moz_places WHERE url = :page_url "
michael@0 62 );
michael@0 63
michael@0 64 try {
michael@0 65 URIS.forEach(function(aUrl) {
michael@0 66 stmt.params.page_url = aUrl;
michael@0 67 do_check_false(stmt.executeStep());
michael@0 68 stmt.reset();
michael@0 69 });
michael@0 70 } finally {
michael@0 71 stmt.finalize();
michael@0 72 }
michael@0 73
michael@0 74 // Check cache.
michael@0 75 checkCache(URL);
michael@0 76 }
michael@0 77 }
michael@0 78
michael@0 79 let timeInMicroseconds = Date.now() * 1000;
michael@0 80
michael@0 81 function run_test() {
michael@0 82 run_next_test();
michael@0 83 }
michael@0 84
michael@0 85 add_task(function test_execute() {
michael@0 86 do_test_pending();
michael@0 87
michael@0 88 print("Initialize browserglue before Places");
michael@0 89
michael@0 90 // Avoid default bookmarks import.
michael@0 91 let glue = Cc["@mozilla.org/browser/browserglue;1"].
michael@0 92 getService(Ci.nsIObserver);
michael@0 93 glue.observe(null, "initial-migration-will-import-default-bookmarks", null);
michael@0 94
michael@0 95 Services.prefs.setBoolPref("privacy.clearOnShutdown.cache", true);
michael@0 96 Services.prefs.setBoolPref("privacy.clearOnShutdown.cookies", true);
michael@0 97 Services.prefs.setBoolPref("privacy.clearOnShutdown.offlineApps", true);
michael@0 98 Services.prefs.setBoolPref("privacy.clearOnShutdown.history", true);
michael@0 99 Services.prefs.setBoolPref("privacy.clearOnShutdown.downloads", true);
michael@0 100 Services.prefs.setBoolPref("privacy.clearOnShutdown.cookies", true);
michael@0 101 Services.prefs.setBoolPref("privacy.clearOnShutdown.formData", true);
michael@0 102 Services.prefs.setBoolPref("privacy.clearOnShutdown.passwords", true);
michael@0 103 Services.prefs.setBoolPref("privacy.clearOnShutdown.sessions", true);
michael@0 104 Services.prefs.setBoolPref("privacy.clearOnShutdown.siteSettings", true);
michael@0 105
michael@0 106 Services.prefs.setBoolPref("privacy.sanitize.sanitizeOnShutdown", true);
michael@0 107
michael@0 108 print("Add visits.");
michael@0 109 for (let aUrl of URIS) {
michael@0 110 yield promiseAddVisits({uri: uri(aUrl), visitDate: timeInMicroseconds++,
michael@0 111 transition: PlacesUtils.history.TRANSITION_TYPED})
michael@0 112 }
michael@0 113 print("Add cache.");
michael@0 114 storeCache(URL, "testData");
michael@0 115 });
michael@0 116
michael@0 117 function run_test_continue()
michael@0 118 {
michael@0 119 print("Simulate and wait shutdown.");
michael@0 120 getDistinctNotifications().forEach(
michael@0 121 function (topic)
michael@0 122 Services.obs.addObserver(notificationsObserver, topic, false)
michael@0 123 );
michael@0 124
michael@0 125 shutdownPlaces();
michael@0 126
michael@0 127 // Shutdown the download manager.
michael@0 128 Services.obs.notifyObservers(null, "quit-application", null);
michael@0 129 }
michael@0 130
michael@0 131 function getDistinctNotifications() {
michael@0 132 let ar = EXPECTED_NOTIFICATIONS.concat(UNEXPECTED_NOTIFICATIONS);
michael@0 133 return [ar[i] for (i in ar) if (ar.slice(0, i).indexOf(ar[i]) == -1)];
michael@0 134 }
michael@0 135
michael@0 136 function storeCache(aURL, aContent) {
michael@0 137 let cache = Services.cache2;
michael@0 138 let storage = cache.diskCacheStorage(LoadContextInfo.default, false);
michael@0 139
michael@0 140 var storeCacheListener = {
michael@0 141 onCacheEntryCheck: function (entry, appcache) {
michael@0 142 return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED;
michael@0 143 },
michael@0 144
michael@0 145 onCacheEntryAvailable: function (entry, isnew, appcache, status) {
michael@0 146 do_check_eq(status, Cr.NS_OK);
michael@0 147
michael@0 148 entry.setMetaDataElement("servertype", "0");
michael@0 149 var os = entry.openOutputStream(0);
michael@0 150
michael@0 151 var written = os.write(aContent, aContent.length);
michael@0 152 if (written != aContent.length) {
michael@0 153 do_throw("os.write has not written all data!\n" +
michael@0 154 " Expected: " + written + "\n" +
michael@0 155 " Actual: " + aContent.length + "\n");
michael@0 156 }
michael@0 157 os.close();
michael@0 158 entry.close();
michael@0 159 do_execute_soon(run_test_continue);
michael@0 160 }
michael@0 161 };
michael@0 162
michael@0 163 storage.asyncOpenURI(Services.io.newURI(aURL, null, null), "",
michael@0 164 Ci.nsICacheStorage.OPEN_NORMALLY,
michael@0 165 storeCacheListener);
michael@0 166 }
michael@0 167
michael@0 168
michael@0 169 function checkCache(aURL) {
michael@0 170 let cache = Services.cache2;
michael@0 171 let storage = cache.diskCacheStorage(LoadContextInfo.default, false);
michael@0 172
michael@0 173 var checkCacheListener = {
michael@0 174 onCacheEntryAvailable: function (entry, isnew, appcache, status) {
michael@0 175 do_check_eq(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
michael@0 176 do_test_finished();
michael@0 177 }
michael@0 178 };
michael@0 179
michael@0 180 storage.asyncOpenURI(Services.io.newURI(aURL, null, null), "",
michael@0 181 Ci.nsICacheStorage.OPEN_READONLY,
michael@0 182 checkCacheListener);
michael@0 183 }

mercurial