|
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/. */ |
|
6 |
|
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 */ |
|
16 |
|
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; |
|
20 |
|
21 // Sync this with the const value in the component. |
|
22 const EXPIRE_AGGRESSIVITY_MULTIPLIER = 3; |
|
23 |
|
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"); |
|
29 |
|
30 // Used to preserve the original timer factory. |
|
31 let gOriginalCID = Cm.contractIDToCID(TIMER_CONTRACT_ID); |
|
32 |
|
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 }, |
|
40 |
|
41 QueryInterface: XPCOMUtils.generateQI([ |
|
42 Ci.nsIFactory, |
|
43 ]) |
|
44 } |
|
45 |
|
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) |
|
53 |
|
54 do_execute_soon(runNextTest); |
|
55 }, |
|
56 |
|
57 cancel: function() {}, |
|
58 initWithFuncCallback: function() {}, |
|
59 init: function() {}, |
|
60 |
|
61 QueryInterface: XPCOMUtils.generateQI([ |
|
62 Ci.nsITimer, |
|
63 ]) |
|
64 } |
|
65 |
|
66 function replace_timer_factory() { |
|
67 Cm.registerFactory(kMockCID, |
|
68 "Mock timer", |
|
69 TIMER_CONTRACT_ID, |
|
70 gMockTimerFactory); |
|
71 } |
|
72 |
|
73 do_register_cleanup(function() { |
|
74 // Shutdown expiration before restoring original timer, otherwise we could |
|
75 // leak due to the different implementation. |
|
76 shutdownExpiration(); |
|
77 |
|
78 // Restore original timer factory. |
|
79 Cm.unregisterFactory(kMockCID, |
|
80 gMockTimerFactory); |
|
81 Cm.registerFactory(gOriginalCID, |
|
82 "", |
|
83 TIMER_CONTRACT_ID, |
|
84 null); |
|
85 }); |
|
86 |
|
87 |
|
88 let tests = [ |
|
89 |
|
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 }, |
|
96 |
|
97 { desc: "Set interval to a negative value.", |
|
98 interval: -1, |
|
99 expectedTimerDelay: DEFAULT_TIMER_DELAY_SECONDS |
|
100 }, |
|
101 |
|
102 { desc: "Set interval to 0.", |
|
103 interval: 0, |
|
104 expectedTimerDelay: DEFAULT_TIMER_DELAY_SECONDS |
|
105 }, |
|
106 |
|
107 { desc: "Set interval to a large value.", |
|
108 interval: 100, |
|
109 expectedTimerDelay: 100 |
|
110 }, |
|
111 |
|
112 ]; |
|
113 |
|
114 let currentTest; |
|
115 |
|
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) {} |
|
123 |
|
124 // Use our own mock timer implementation. |
|
125 replace_timer_factory(); |
|
126 |
|
127 // Force the component, so it will start observing preferences. |
|
128 force_expiration_start(); |
|
129 |
|
130 runNextTest(); |
|
131 do_test_pending(); |
|
132 } |
|
133 |
|
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 } |