toolkit/components/satchel/test/unit/test_async_expire.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

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 var dbFile, oldSize;
     6 var currentTestIndex = 0;
     8 function triggerExpiration() {
     9   // We can't easily fake a "daily idle" event, so for testing purposes form
    10   // history listens for another notification to trigger an immediate
    11   // expiration.
    12   Services.obs.notifyObservers(null, "formhistory-expire-now", null);
    13 }
    15 let checkExists = function(num) { do_check_true(num > 0); next_test(); }
    16 let checkNotExists = function(num) { do_check_true(!num); next_test(); }
    18 var TestObserver = {
    19   QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
    21   observe : function (subject, topic, data) {
    22     do_check_eq(topic, "satchel-storage-changed");
    24     if (data == "formhistory-expireoldentries") {
    25       next_test();
    26     }
    27   }
    28 };
    30 function test_finished() {
    31   // Make sure we always reset prefs.
    32   if (Services.prefs.prefHasUserValue("browser.formfill.expire_days"))
    33     Services.prefs.clearUserPref("browser.formfill.expire_days");
    35   do_test_finished();
    36 }
    38 let iter = tests();
    40 function run_test()
    41 {
    42   do_test_pending();
    43   iter.next();
    44 }
    46 function next_test()
    47 {
    48   iter.next();
    49 }
    51 function tests()
    52 {
    53   Services.obs.addObserver(TestObserver, "satchel-storage-changed", true);
    55   // ===== test init =====
    56   var testfile = do_get_file("asyncformhistory_expire.sqlite");
    57   var profileDir = do_get_profile();
    59   // Cleanup from any previous tests or failures.
    60   dbFile = profileDir.clone();
    61   dbFile.append("formhistory.sqlite");
    62   if (dbFile.exists())
    63     dbFile.remove(false);
    65   testfile.copyTo(profileDir, "formhistory.sqlite");
    66   do_check_true(dbFile.exists());
    68   // We're going to clear this at the end, so it better have the default value now.
    69   do_check_false(Services.prefs.prefHasUserValue("browser.formfill.expire_days"));
    71   // Sanity check initial state
    72   yield countEntries(null, null, function(num) { do_check_eq(508, num); next_test(); });
    73   yield countEntries("name-A", "value-A", checkExists); // lastUsed == distant past
    74   yield countEntries("name-B", "value-B", checkExists); // lastUsed == distant future
    76   do_check_eq(CURRENT_SCHEMA, FormHistory.schemaVersion);
    78   // Add a new entry
    79   yield countEntries("name-C", "value-C", checkNotExists);
    80   yield addEntry("name-C", "value-C", next_test);
    81   yield countEntries("name-C", "value-C", checkExists);
    83   // Update some existing entries to have ages relative to when the test runs.
    84   var now = 1000 * Date.now();
    85   let updateLastUsed = function updateLastUsedFn(results, age)
    86   {
    87     let lastUsed = now - age * 24 * PR_HOURS;
    89     let changes = [ ];
    90     for (let r = 0; r < results.length; r++) {
    91       changes.push({ op: "update", lastUsed: lastUsed, guid: results[r].guid });
    92     }
    94     return changes;
    95   }
    97   let results = yield searchEntries(["guid"], { lastUsed: 181 }, iter);
    98   yield updateFormHistory(updateLastUsed(results, 181), next_test);
   100   results = yield searchEntries(["guid"], { lastUsed: 179 }, iter);
   101   yield updateFormHistory(updateLastUsed(results, 179), next_test);
   103   results = yield searchEntries(["guid"], { lastUsed: 31 }, iter);
   104   yield updateFormHistory(updateLastUsed(results, 31), next_test);
   106   results = yield searchEntries(["guid"], { lastUsed: 29 }, iter);
   107   yield updateFormHistory(updateLastUsed(results, 29), next_test);
   109   results = yield searchEntries(["guid"], { lastUsed: 9999 }, iter);
   110   yield updateFormHistory(updateLastUsed(results, 11), next_test);
   112   results = yield searchEntries(["guid"], { lastUsed: 9 }, iter);
   113   yield updateFormHistory(updateLastUsed(results, 9), next_test);
   115   yield countEntries("name-A", "value-A", checkExists);
   116   yield countEntries("181DaysOld", "foo", checkExists);
   117   yield countEntries("179DaysOld", "foo", checkExists);
   118   yield countEntries(null, null, function(num) { do_check_eq(509, num); next_test(); });
   120   // 2 entries are expected to expire.
   121   triggerExpiration();
   122   yield;
   124   yield countEntries("name-A", "value-A", checkNotExists);
   125   yield countEntries("181DaysOld", "foo", checkNotExists);
   126   yield countEntries("179DaysOld", "foo", checkExists);
   127   yield countEntries(null, null, function(num) { do_check_eq(507, num); next_test(); });
   129   // And again. No change expected.
   130   triggerExpiration();
   131   yield;
   133   yield countEntries(null, null, function(num) { do_check_eq(507, num); next_test(); });
   135   // Set formfill pref to 30 days.
   136   Services.prefs.setIntPref("browser.formfill.expire_days", 30);
   137   yield countEntries("179DaysOld", "foo", checkExists);
   138   yield countEntries("bar", "31days", checkExists);
   139   yield countEntries("bar", "29days", checkExists);
   140   yield countEntries(null, null, function(num) { do_check_eq(507, num); next_test(); });
   142   triggerExpiration();
   143   yield;
   145   yield countEntries("179DaysOld", "foo", checkNotExists);
   146   yield countEntries("bar", "31days", checkNotExists);
   147   yield countEntries("bar", "29days", checkExists);
   148   yield countEntries(null, null, function(num) { do_check_eq(505, num); next_test(); });
   150   // Set override pref to 10 days and expire. This expires a large batch of
   151   // entries, and should trigger a VACCUM to reduce file size.
   152   Services.prefs.setIntPref("browser.formfill.expire_days", 10);
   154   yield countEntries("bar", "29days", checkExists);
   155   yield countEntries("9DaysOld", "foo", checkExists);
   156   yield countEntries(null, null, function(num) { do_check_eq(505, num); next_test(); });
   158   triggerExpiration();
   159   yield;
   161   yield countEntries("bar", "29days", checkNotExists);
   162   yield countEntries("9DaysOld", "foo", checkExists);
   163   yield countEntries("name-B", "value-B", checkExists);
   164   yield countEntries("name-C", "value-C", checkExists);
   165   yield countEntries(null, null, function(num) { do_check_eq(3, num); next_test(); });
   167   test_finished();
   168 };

mercurial