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