toolkit/components/satchel/test/unit/test_async_expire.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/satchel/test/unit/test_async_expire.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,168 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +var dbFile, oldSize;
     1.9 +var currentTestIndex = 0;
    1.10 +
    1.11 +function triggerExpiration() {
    1.12 +  // We can't easily fake a "daily idle" event, so for testing purposes form
    1.13 +  // history listens for another notification to trigger an immediate
    1.14 +  // expiration.
    1.15 +  Services.obs.notifyObservers(null, "formhistory-expire-now", null);
    1.16 +}
    1.17 +
    1.18 +let checkExists = function(num) { do_check_true(num > 0); next_test(); }
    1.19 +let checkNotExists = function(num) { do_check_true(!num); next_test(); }
    1.20 +
    1.21 +var TestObserver = {
    1.22 +  QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
    1.23 +
    1.24 +  observe : function (subject, topic, data) {
    1.25 +    do_check_eq(topic, "satchel-storage-changed");
    1.26 +
    1.27 +    if (data == "formhistory-expireoldentries") {
    1.28 +      next_test();
    1.29 +    }
    1.30 +  }
    1.31 +};
    1.32 +
    1.33 +function test_finished() {
    1.34 +  // Make sure we always reset prefs.
    1.35 +  if (Services.prefs.prefHasUserValue("browser.formfill.expire_days"))
    1.36 +    Services.prefs.clearUserPref("browser.formfill.expire_days");
    1.37 +
    1.38 +  do_test_finished();
    1.39 +}
    1.40 +
    1.41 +let iter = tests();
    1.42 +
    1.43 +function run_test()
    1.44 +{
    1.45 +  do_test_pending();
    1.46 +  iter.next();
    1.47 +}
    1.48 +
    1.49 +function next_test()
    1.50 +{
    1.51 +  iter.next();
    1.52 +}
    1.53 +
    1.54 +function tests()
    1.55 +{
    1.56 +  Services.obs.addObserver(TestObserver, "satchel-storage-changed", true);
    1.57 +
    1.58 +  // ===== test init =====
    1.59 +  var testfile = do_get_file("asyncformhistory_expire.sqlite");
    1.60 +  var profileDir = do_get_profile();
    1.61 +
    1.62 +  // Cleanup from any previous tests or failures.
    1.63 +  dbFile = profileDir.clone();
    1.64 +  dbFile.append("formhistory.sqlite");
    1.65 +  if (dbFile.exists())
    1.66 +    dbFile.remove(false);
    1.67 +
    1.68 +  testfile.copyTo(profileDir, "formhistory.sqlite");
    1.69 +  do_check_true(dbFile.exists());
    1.70 +
    1.71 +  // We're going to clear this at the end, so it better have the default value now.
    1.72 +  do_check_false(Services.prefs.prefHasUserValue("browser.formfill.expire_days"));
    1.73 +
    1.74 +  // Sanity check initial state
    1.75 +  yield countEntries(null, null, function(num) { do_check_eq(508, num); next_test(); });
    1.76 +  yield countEntries("name-A", "value-A", checkExists); // lastUsed == distant past
    1.77 +  yield countEntries("name-B", "value-B", checkExists); // lastUsed == distant future
    1.78 +
    1.79 +  do_check_eq(CURRENT_SCHEMA, FormHistory.schemaVersion);
    1.80 +
    1.81 +  // Add a new entry
    1.82 +  yield countEntries("name-C", "value-C", checkNotExists);
    1.83 +  yield addEntry("name-C", "value-C", next_test);
    1.84 +  yield countEntries("name-C", "value-C", checkExists);
    1.85 +
    1.86 +  // Update some existing entries to have ages relative to when the test runs.
    1.87 +  var now = 1000 * Date.now();
    1.88 +  let updateLastUsed = function updateLastUsedFn(results, age)
    1.89 +  {
    1.90 +    let lastUsed = now - age * 24 * PR_HOURS;
    1.91 +
    1.92 +    let changes = [ ];
    1.93 +    for (let r = 0; r < results.length; r++) {
    1.94 +      changes.push({ op: "update", lastUsed: lastUsed, guid: results[r].guid });
    1.95 +    }
    1.96 +
    1.97 +    return changes;
    1.98 +  }
    1.99 +
   1.100 +  let results = yield searchEntries(["guid"], { lastUsed: 181 }, iter);
   1.101 +  yield updateFormHistory(updateLastUsed(results, 181), next_test);
   1.102 +
   1.103 +  results = yield searchEntries(["guid"], { lastUsed: 179 }, iter);
   1.104 +  yield updateFormHistory(updateLastUsed(results, 179), next_test);
   1.105 +
   1.106 +  results = yield searchEntries(["guid"], { lastUsed: 31 }, iter);
   1.107 +  yield updateFormHistory(updateLastUsed(results, 31), next_test);
   1.108 +
   1.109 +  results = yield searchEntries(["guid"], { lastUsed: 29 }, iter);
   1.110 +  yield updateFormHistory(updateLastUsed(results, 29), next_test);
   1.111 +
   1.112 +  results = yield searchEntries(["guid"], { lastUsed: 9999 }, iter);
   1.113 +  yield updateFormHistory(updateLastUsed(results, 11), next_test);
   1.114 +
   1.115 +  results = yield searchEntries(["guid"], { lastUsed: 9 }, iter);
   1.116 +  yield updateFormHistory(updateLastUsed(results, 9), next_test);
   1.117 +
   1.118 +  yield countEntries("name-A", "value-A", checkExists);
   1.119 +  yield countEntries("181DaysOld", "foo", checkExists);
   1.120 +  yield countEntries("179DaysOld", "foo", checkExists);
   1.121 +  yield countEntries(null, null, function(num) { do_check_eq(509, num); next_test(); });
   1.122 +
   1.123 +  // 2 entries are expected to expire.
   1.124 +  triggerExpiration();
   1.125 +  yield;
   1.126 +
   1.127 +  yield countEntries("name-A", "value-A", checkNotExists);
   1.128 +  yield countEntries("181DaysOld", "foo", checkNotExists);
   1.129 +  yield countEntries("179DaysOld", "foo", checkExists);
   1.130 +  yield countEntries(null, null, function(num) { do_check_eq(507, num); next_test(); });
   1.131 +
   1.132 +  // And again. No change expected.
   1.133 +  triggerExpiration();
   1.134 +  yield;
   1.135 +
   1.136 +  yield countEntries(null, null, function(num) { do_check_eq(507, num); next_test(); });
   1.137 +
   1.138 +  // Set formfill pref to 30 days.
   1.139 +  Services.prefs.setIntPref("browser.formfill.expire_days", 30);
   1.140 +  yield countEntries("179DaysOld", "foo", checkExists);
   1.141 +  yield countEntries("bar", "31days", checkExists);
   1.142 +  yield countEntries("bar", "29days", checkExists);
   1.143 +  yield countEntries(null, null, function(num) { do_check_eq(507, num); next_test(); });
   1.144 +
   1.145 +  triggerExpiration();
   1.146 +  yield;
   1.147 +
   1.148 +  yield countEntries("179DaysOld", "foo", checkNotExists);
   1.149 +  yield countEntries("bar", "31days", checkNotExists);
   1.150 +  yield countEntries("bar", "29days", checkExists);
   1.151 +  yield countEntries(null, null, function(num) { do_check_eq(505, num); next_test(); });
   1.152 +
   1.153 +  // Set override pref to 10 days and expire. This expires a large batch of
   1.154 +  // entries, and should trigger a VACCUM to reduce file size.
   1.155 +  Services.prefs.setIntPref("browser.formfill.expire_days", 10);
   1.156 +
   1.157 +  yield countEntries("bar", "29days", checkExists);
   1.158 +  yield countEntries("9DaysOld", "foo", checkExists);
   1.159 +  yield countEntries(null, null, function(num) { do_check_eq(505, num); next_test(); });
   1.160 +
   1.161 +  triggerExpiration();
   1.162 +  yield;
   1.163 +
   1.164 +  yield countEntries("bar", "29days", checkNotExists);
   1.165 +  yield countEntries("9DaysOld", "foo", checkExists);
   1.166 +  yield countEntries("name-B", "value-B", checkExists);
   1.167 +  yield countEntries("name-C", "value-C", checkExists);
   1.168 +  yield countEntries(null, null, function(num) { do_check_eq(3, num); next_test(); });
   1.169 +
   1.170 +  test_finished();
   1.171 +};

mercurial