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