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

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:66d9844f217a
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/. */
4
5 var dbFile, oldSize;
6 var currentTestIndex = 0;
7
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 }
14
15 let checkExists = function(num) { do_check_true(num > 0); next_test(); }
16 let checkNotExists = function(num) { do_check_true(!num); next_test(); }
17
18 var TestObserver = {
19 QueryInterface : XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
20
21 observe : function (subject, topic, data) {
22 do_check_eq(topic, "satchel-storage-changed");
23
24 if (data == "formhistory-expireoldentries") {
25 next_test();
26 }
27 }
28 };
29
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");
34
35 do_test_finished();
36 }
37
38 let iter = tests();
39
40 function run_test()
41 {
42 do_test_pending();
43 iter.next();
44 }
45
46 function next_test()
47 {
48 iter.next();
49 }
50
51 function tests()
52 {
53 Services.obs.addObserver(TestObserver, "satchel-storage-changed", true);
54
55 // ===== test init =====
56 var testfile = do_get_file("asyncformhistory_expire.sqlite");
57 var profileDir = do_get_profile();
58
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);
64
65 testfile.copyTo(profileDir, "formhistory.sqlite");
66 do_check_true(dbFile.exists());
67
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"));
70
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
75
76 do_check_eq(CURRENT_SCHEMA, FormHistory.schemaVersion);
77
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);
82
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;
88
89 let changes = [ ];
90 for (let r = 0; r < results.length; r++) {
91 changes.push({ op: "update", lastUsed: lastUsed, guid: results[r].guid });
92 }
93
94 return changes;
95 }
96
97 let results = yield searchEntries(["guid"], { lastUsed: 181 }, iter);
98 yield updateFormHistory(updateLastUsed(results, 181), next_test);
99
100 results = yield searchEntries(["guid"], { lastUsed: 179 }, iter);
101 yield updateFormHistory(updateLastUsed(results, 179), next_test);
102
103 results = yield searchEntries(["guid"], { lastUsed: 31 }, iter);
104 yield updateFormHistory(updateLastUsed(results, 31), next_test);
105
106 results = yield searchEntries(["guid"], { lastUsed: 29 }, iter);
107 yield updateFormHistory(updateLastUsed(results, 29), next_test);
108
109 results = yield searchEntries(["guid"], { lastUsed: 9999 }, iter);
110 yield updateFormHistory(updateLastUsed(results, 11), next_test);
111
112 results = yield searchEntries(["guid"], { lastUsed: 9 }, iter);
113 yield updateFormHistory(updateLastUsed(results, 9), next_test);
114
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(); });
119
120 // 2 entries are expected to expire.
121 triggerExpiration();
122 yield;
123
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(); });
128
129 // And again. No change expected.
130 triggerExpiration();
131 yield;
132
133 yield countEntries(null, null, function(num) { do_check_eq(507, num); next_test(); });
134
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(); });
141
142 triggerExpiration();
143 yield;
144
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(); });
149
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);
153
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(); });
157
158 triggerExpiration();
159 yield;
160
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(); });
166
167 test_finished();
168 };

mercurial