browser/components/places/tests/unit/test_clearHistory_shutdown.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/places/tests/unit/test_clearHistory_shutdown.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,183 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/**
    1.11 + * Tests that requesting clear history at shutdown will really clear history.
    1.12 + */
    1.13 +
    1.14 +const URIS = [
    1.15 +  "http://a.example1.com/"
    1.16 +, "http://b.example1.com/"
    1.17 +, "http://b.example2.com/"
    1.18 +, "http://c.example3.com/"
    1.19 +];
    1.20 +
    1.21 +const TOPIC_CONNECTION_CLOSED = "places-connection-closed";
    1.22 +
    1.23 +let EXPECTED_NOTIFICATIONS = [
    1.24 +  "places-shutdown"
    1.25 +, "places-will-close-connection"
    1.26 +, "places-expiration-finished"
    1.27 +, "places-connection-closed"
    1.28 +];
    1.29 +
    1.30 +const UNEXPECTED_NOTIFICATIONS = [
    1.31 +  "xpcom-shutdown"
    1.32 +];
    1.33 +
    1.34 +const URL = "ftp://localhost/clearHistoryOnShutdown/";
    1.35 +
    1.36 +// Send the profile-after-change notification to the form history component to ensure
    1.37 +// that it has been initialized.
    1.38 +var formHistoryStartup = Cc["@mozilla.org/satchel/form-history-startup;1"].
    1.39 +                         getService(Ci.nsIObserver);
    1.40 +formHistoryStartup.observe(null, "profile-after-change", null);
    1.41 +
    1.42 +let notificationIndex = 0;
    1.43 +
    1.44 +let notificationsObserver = {
    1.45 +  observe: function observe(aSubject, aTopic, aData) {
    1.46 +    print("Received notification: " + aTopic);
    1.47 +
    1.48 +    // Note that some of these notifications could arrive multiple times, for
    1.49 +    // example in case of sync, we allow that.
    1.50 +    if (EXPECTED_NOTIFICATIONS[notificationIndex] != aTopic)
    1.51 +      notificationIndex++;
    1.52 +    do_check_eq(EXPECTED_NOTIFICATIONS[notificationIndex], aTopic);
    1.53 +
    1.54 +    if (aTopic != TOPIC_CONNECTION_CLOSED)
    1.55 +      return;
    1.56 +
    1.57 +    getDistinctNotifications().forEach(
    1.58 +      function (topic) Services.obs.removeObserver(notificationsObserver, topic)
    1.59 +    );
    1.60 +
    1.61 +    print("Looking for uncleared stuff.");
    1.62 +
    1.63 +    let stmt = DBConn().createStatement(
    1.64 +      "SELECT id FROM moz_places WHERE url = :page_url "
    1.65 +    );
    1.66 +
    1.67 +    try {
    1.68 +      URIS.forEach(function(aUrl) {
    1.69 +        stmt.params.page_url = aUrl;
    1.70 +        do_check_false(stmt.executeStep());
    1.71 +        stmt.reset();
    1.72 +      });
    1.73 +    } finally {
    1.74 +      stmt.finalize();
    1.75 +    }
    1.76 +
    1.77 +    // Check cache.
    1.78 +    checkCache(URL);
    1.79 +  }
    1.80 +}
    1.81 +
    1.82 +let timeInMicroseconds = Date.now() * 1000;
    1.83 +
    1.84 +function run_test() {
    1.85 +  run_next_test();
    1.86 +}
    1.87 +
    1.88 +add_task(function test_execute() {
    1.89 +  do_test_pending();
    1.90 +
    1.91 +  print("Initialize browserglue before Places");
    1.92 +
    1.93 +  // Avoid default bookmarks import.
    1.94 +  let glue = Cc["@mozilla.org/browser/browserglue;1"].
    1.95 +             getService(Ci.nsIObserver);
    1.96 +  glue.observe(null, "initial-migration-will-import-default-bookmarks", null);
    1.97 +
    1.98 +  Services.prefs.setBoolPref("privacy.clearOnShutdown.cache", true);
    1.99 +  Services.prefs.setBoolPref("privacy.clearOnShutdown.cookies", true);
   1.100 +  Services.prefs.setBoolPref("privacy.clearOnShutdown.offlineApps", true);
   1.101 +  Services.prefs.setBoolPref("privacy.clearOnShutdown.history", true);
   1.102 +  Services.prefs.setBoolPref("privacy.clearOnShutdown.downloads", true);
   1.103 +  Services.prefs.setBoolPref("privacy.clearOnShutdown.cookies", true);
   1.104 +  Services.prefs.setBoolPref("privacy.clearOnShutdown.formData", true);
   1.105 +  Services.prefs.setBoolPref("privacy.clearOnShutdown.passwords", true);
   1.106 +  Services.prefs.setBoolPref("privacy.clearOnShutdown.sessions", true);
   1.107 +  Services.prefs.setBoolPref("privacy.clearOnShutdown.siteSettings", true);
   1.108 +
   1.109 +  Services.prefs.setBoolPref("privacy.sanitize.sanitizeOnShutdown", true);
   1.110 +
   1.111 +  print("Add visits.");
   1.112 +  for (let aUrl of URIS) {
   1.113 +    yield promiseAddVisits({uri: uri(aUrl), visitDate: timeInMicroseconds++,
   1.114 +                            transition: PlacesUtils.history.TRANSITION_TYPED})
   1.115 +  }
   1.116 +  print("Add cache.");
   1.117 +  storeCache(URL, "testData");
   1.118 +});
   1.119 +
   1.120 +function run_test_continue()
   1.121 +{
   1.122 +  print("Simulate and wait shutdown.");
   1.123 +  getDistinctNotifications().forEach(
   1.124 +    function (topic)
   1.125 +      Services.obs.addObserver(notificationsObserver, topic, false)
   1.126 +  );
   1.127 +
   1.128 +  shutdownPlaces();
   1.129 +
   1.130 +  // Shutdown the download manager.
   1.131 +  Services.obs.notifyObservers(null, "quit-application", null);
   1.132 +}
   1.133 +
   1.134 +function getDistinctNotifications() {
   1.135 +  let ar = EXPECTED_NOTIFICATIONS.concat(UNEXPECTED_NOTIFICATIONS);
   1.136 +  return [ar[i] for (i in ar) if (ar.slice(0, i).indexOf(ar[i]) == -1)];
   1.137 +}
   1.138 +
   1.139 +function storeCache(aURL, aContent) {
   1.140 +  let cache = Services.cache2;
   1.141 +  let storage = cache.diskCacheStorage(LoadContextInfo.default, false);
   1.142 +
   1.143 +  var storeCacheListener = {
   1.144 +    onCacheEntryCheck: function (entry, appcache) {
   1.145 +      return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED;
   1.146 +    },
   1.147 +
   1.148 +    onCacheEntryAvailable: function (entry, isnew, appcache, status) {
   1.149 +      do_check_eq(status, Cr.NS_OK);
   1.150 +
   1.151 +      entry.setMetaDataElement("servertype", "0");
   1.152 +      var os = entry.openOutputStream(0);
   1.153 +
   1.154 +      var written = os.write(aContent, aContent.length);
   1.155 +      if (written != aContent.length) {
   1.156 +        do_throw("os.write has not written all data!\n" +
   1.157 +                 "  Expected: " + written  + "\n" +
   1.158 +                 "  Actual: " + aContent.length + "\n");
   1.159 +      }
   1.160 +      os.close();
   1.161 +      entry.close();
   1.162 +      do_execute_soon(run_test_continue);
   1.163 +    }
   1.164 +  };
   1.165 +
   1.166 +  storage.asyncOpenURI(Services.io.newURI(aURL, null, null), "",
   1.167 +                       Ci.nsICacheStorage.OPEN_NORMALLY,
   1.168 +                       storeCacheListener);
   1.169 +}
   1.170 +
   1.171 +
   1.172 +function checkCache(aURL) {
   1.173 +  let cache = Services.cache2;
   1.174 +  let storage = cache.diskCacheStorage(LoadContextInfo.default, false);
   1.175 +
   1.176 +  var checkCacheListener = {
   1.177 +    onCacheEntryAvailable: function (entry, isnew, appcache, status) {
   1.178 +      do_check_eq(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
   1.179 +      do_test_finished();
   1.180 +    }
   1.181 +  };
   1.182 +
   1.183 +  storage.asyncOpenURI(Services.io.newURI(aURL, null, null), "",
   1.184 +                       Ci.nsICacheStorage.OPEN_READONLY,
   1.185 +                       checkCacheListener);
   1.186 +}

mercurial