Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /** |
michael@0 | 8 | * What this is aimed to test: |
michael@0 | 9 | * |
michael@0 | 10 | * Expiration relies on an interval, that is user-preffable setting |
michael@0 | 11 | * "places.history.expiration.interval_seconds". |
michael@0 | 12 | * On pref change it will stop current interval timer and fire a new one, |
michael@0 | 13 | * that will obey the new value. |
michael@0 | 14 | * If the pref is set to a number <= 0 we will use the default value. |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | // Default timer value for expiration in seconds. Must have same value as |
michael@0 | 18 | // PREF_INTERVAL_SECONDS_NOTSET in nsPlacesExpiration. |
michael@0 | 19 | const DEFAULT_TIMER_DELAY_SECONDS = 3 * 60; |
michael@0 | 20 | |
michael@0 | 21 | // Sync this with the const value in the component. |
michael@0 | 22 | const EXPIRE_AGGRESSIVITY_MULTIPLIER = 3; |
michael@0 | 23 | |
michael@0 | 24 | // Provide a mock timer implementation, so there is no need to wait seconds to |
michael@0 | 25 | // achieve test results. |
michael@0 | 26 | const Cm = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); |
michael@0 | 27 | const TIMER_CONTRACT_ID = "@mozilla.org/timer;1"; |
michael@0 | 28 | const kMockCID = Components.ID("52bdf457-4de3-48ae-bbbb-f3cbcbcad05f"); |
michael@0 | 29 | |
michael@0 | 30 | // Used to preserve the original timer factory. |
michael@0 | 31 | let gOriginalCID = Cm.contractIDToCID(TIMER_CONTRACT_ID); |
michael@0 | 32 | |
michael@0 | 33 | // The mock timer factory. |
michael@0 | 34 | let gMockTimerFactory = { |
michael@0 | 35 | createInstance: function MTF_createInstance(aOuter, aIID) { |
michael@0 | 36 | if (aOuter != null) |
michael@0 | 37 | throw Cr.NS_ERROR_NO_AGGREGATION; |
michael@0 | 38 | return mockTimerImpl.QueryInterface(aIID); |
michael@0 | 39 | }, |
michael@0 | 40 | |
michael@0 | 41 | QueryInterface: XPCOMUtils.generateQI([ |
michael@0 | 42 | Ci.nsIFactory, |
michael@0 | 43 | ]) |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | let mockTimerImpl = { |
michael@0 | 47 | initWithCallback: function MTI_initWithCallback(aCallback, aDelay, aType) { |
michael@0 | 48 | print("Checking timer delay equals expected interval value"); |
michael@0 | 49 | if (!currentTest) |
michael@0 | 50 | return; |
michael@0 | 51 | // History status is not dirty, so the timer is delayed. |
michael@0 | 52 | do_check_eq(aDelay, currentTest.expectedTimerDelay * 1000 * EXPIRE_AGGRESSIVITY_MULTIPLIER) |
michael@0 | 53 | |
michael@0 | 54 | do_execute_soon(runNextTest); |
michael@0 | 55 | }, |
michael@0 | 56 | |
michael@0 | 57 | cancel: function() {}, |
michael@0 | 58 | initWithFuncCallback: function() {}, |
michael@0 | 59 | init: function() {}, |
michael@0 | 60 | |
michael@0 | 61 | QueryInterface: XPCOMUtils.generateQI([ |
michael@0 | 62 | Ci.nsITimer, |
michael@0 | 63 | ]) |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | function replace_timer_factory() { |
michael@0 | 67 | Cm.registerFactory(kMockCID, |
michael@0 | 68 | "Mock timer", |
michael@0 | 69 | TIMER_CONTRACT_ID, |
michael@0 | 70 | gMockTimerFactory); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | do_register_cleanup(function() { |
michael@0 | 74 | // Shutdown expiration before restoring original timer, otherwise we could |
michael@0 | 75 | // leak due to the different implementation. |
michael@0 | 76 | shutdownExpiration(); |
michael@0 | 77 | |
michael@0 | 78 | // Restore original timer factory. |
michael@0 | 79 | Cm.unregisterFactory(kMockCID, |
michael@0 | 80 | gMockTimerFactory); |
michael@0 | 81 | Cm.registerFactory(gOriginalCID, |
michael@0 | 82 | "", |
michael@0 | 83 | TIMER_CONTRACT_ID, |
michael@0 | 84 | null); |
michael@0 | 85 | }); |
michael@0 | 86 | |
michael@0 | 87 | |
michael@0 | 88 | let tests = [ |
michael@0 | 89 | |
michael@0 | 90 | // This test should be the first, so the interval won't be influenced by |
michael@0 | 91 | // status of history. |
michael@0 | 92 | { desc: "Set interval to 1s.", |
michael@0 | 93 | interval: 1, |
michael@0 | 94 | expectedTimerDelay: 1 |
michael@0 | 95 | }, |
michael@0 | 96 | |
michael@0 | 97 | { desc: "Set interval to a negative value.", |
michael@0 | 98 | interval: -1, |
michael@0 | 99 | expectedTimerDelay: DEFAULT_TIMER_DELAY_SECONDS |
michael@0 | 100 | }, |
michael@0 | 101 | |
michael@0 | 102 | { desc: "Set interval to 0.", |
michael@0 | 103 | interval: 0, |
michael@0 | 104 | expectedTimerDelay: DEFAULT_TIMER_DELAY_SECONDS |
michael@0 | 105 | }, |
michael@0 | 106 | |
michael@0 | 107 | { desc: "Set interval to a large value.", |
michael@0 | 108 | interval: 100, |
michael@0 | 109 | expectedTimerDelay: 100 |
michael@0 | 110 | }, |
michael@0 | 111 | |
michael@0 | 112 | ]; |
michael@0 | 113 | |
michael@0 | 114 | let currentTest; |
michael@0 | 115 | |
michael@0 | 116 | function run_test() { |
michael@0 | 117 | // The pref should not exist by default. |
michael@0 | 118 | try { |
michael@0 | 119 | getInterval(); |
michael@0 | 120 | do_throw("interval pref should not exist by default"); |
michael@0 | 121 | } |
michael@0 | 122 | catch (ex) {} |
michael@0 | 123 | |
michael@0 | 124 | // Use our own mock timer implementation. |
michael@0 | 125 | replace_timer_factory(); |
michael@0 | 126 | |
michael@0 | 127 | // Force the component, so it will start observing preferences. |
michael@0 | 128 | force_expiration_start(); |
michael@0 | 129 | |
michael@0 | 130 | runNextTest(); |
michael@0 | 131 | do_test_pending(); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | function runNextTest() { |
michael@0 | 135 | if (tests.length) { |
michael@0 | 136 | currentTest = tests.shift(); |
michael@0 | 137 | print(currentTest.desc); |
michael@0 | 138 | setInterval(currentTest.interval); |
michael@0 | 139 | } |
michael@0 | 140 | else { |
michael@0 | 141 | clearInterval(); |
michael@0 | 142 | do_test_finished(); |
michael@0 | 143 | } |
michael@0 | 144 | } |