1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/expiration/test_pref_interval.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,144 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +/** 1.11 + * What this is aimed to test: 1.12 + * 1.13 + * Expiration relies on an interval, that is user-preffable setting 1.14 + * "places.history.expiration.interval_seconds". 1.15 + * On pref change it will stop current interval timer and fire a new one, 1.16 + * that will obey the new value. 1.17 + * If the pref is set to a number <= 0 we will use the default value. 1.18 + */ 1.19 + 1.20 +// Default timer value for expiration in seconds. Must have same value as 1.21 +// PREF_INTERVAL_SECONDS_NOTSET in nsPlacesExpiration. 1.22 +const DEFAULT_TIMER_DELAY_SECONDS = 3 * 60; 1.23 + 1.24 +// Sync this with the const value in the component. 1.25 +const EXPIRE_AGGRESSIVITY_MULTIPLIER = 3; 1.26 + 1.27 +// Provide a mock timer implementation, so there is no need to wait seconds to 1.28 +// achieve test results. 1.29 +const Cm = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); 1.30 +const TIMER_CONTRACT_ID = "@mozilla.org/timer;1"; 1.31 +const kMockCID = Components.ID("52bdf457-4de3-48ae-bbbb-f3cbcbcad05f"); 1.32 + 1.33 +// Used to preserve the original timer factory. 1.34 +let gOriginalCID = Cm.contractIDToCID(TIMER_CONTRACT_ID); 1.35 + 1.36 +// The mock timer factory. 1.37 +let gMockTimerFactory = { 1.38 + createInstance: function MTF_createInstance(aOuter, aIID) { 1.39 + if (aOuter != null) 1.40 + throw Cr.NS_ERROR_NO_AGGREGATION; 1.41 + return mockTimerImpl.QueryInterface(aIID); 1.42 + }, 1.43 + 1.44 + QueryInterface: XPCOMUtils.generateQI([ 1.45 + Ci.nsIFactory, 1.46 + ]) 1.47 +} 1.48 + 1.49 +let mockTimerImpl = { 1.50 + initWithCallback: function MTI_initWithCallback(aCallback, aDelay, aType) { 1.51 + print("Checking timer delay equals expected interval value"); 1.52 + if (!currentTest) 1.53 + return; 1.54 + // History status is not dirty, so the timer is delayed. 1.55 + do_check_eq(aDelay, currentTest.expectedTimerDelay * 1000 * EXPIRE_AGGRESSIVITY_MULTIPLIER) 1.56 + 1.57 + do_execute_soon(runNextTest); 1.58 + }, 1.59 + 1.60 + cancel: function() {}, 1.61 + initWithFuncCallback: function() {}, 1.62 + init: function() {}, 1.63 + 1.64 + QueryInterface: XPCOMUtils.generateQI([ 1.65 + Ci.nsITimer, 1.66 + ]) 1.67 +} 1.68 + 1.69 +function replace_timer_factory() { 1.70 + Cm.registerFactory(kMockCID, 1.71 + "Mock timer", 1.72 + TIMER_CONTRACT_ID, 1.73 + gMockTimerFactory); 1.74 +} 1.75 + 1.76 +do_register_cleanup(function() { 1.77 + // Shutdown expiration before restoring original timer, otherwise we could 1.78 + // leak due to the different implementation. 1.79 + shutdownExpiration(); 1.80 + 1.81 + // Restore original timer factory. 1.82 + Cm.unregisterFactory(kMockCID, 1.83 + gMockTimerFactory); 1.84 + Cm.registerFactory(gOriginalCID, 1.85 + "", 1.86 + TIMER_CONTRACT_ID, 1.87 + null); 1.88 +}); 1.89 + 1.90 + 1.91 +let tests = [ 1.92 + 1.93 + // This test should be the first, so the interval won't be influenced by 1.94 + // status of history. 1.95 + { desc: "Set interval to 1s.", 1.96 + interval: 1, 1.97 + expectedTimerDelay: 1 1.98 + }, 1.99 + 1.100 + { desc: "Set interval to a negative value.", 1.101 + interval: -1, 1.102 + expectedTimerDelay: DEFAULT_TIMER_DELAY_SECONDS 1.103 + }, 1.104 + 1.105 + { desc: "Set interval to 0.", 1.106 + interval: 0, 1.107 + expectedTimerDelay: DEFAULT_TIMER_DELAY_SECONDS 1.108 + }, 1.109 + 1.110 + { desc: "Set interval to a large value.", 1.111 + interval: 100, 1.112 + expectedTimerDelay: 100 1.113 + }, 1.114 + 1.115 +]; 1.116 + 1.117 +let currentTest; 1.118 + 1.119 +function run_test() { 1.120 + // The pref should not exist by default. 1.121 + try { 1.122 + getInterval(); 1.123 + do_throw("interval pref should not exist by default"); 1.124 + } 1.125 + catch (ex) {} 1.126 + 1.127 + // Use our own mock timer implementation. 1.128 + replace_timer_factory(); 1.129 + 1.130 + // Force the component, so it will start observing preferences. 1.131 + force_expiration_start(); 1.132 + 1.133 + runNextTest(); 1.134 + do_test_pending(); 1.135 +} 1.136 + 1.137 +function runNextTest() { 1.138 + if (tests.length) { 1.139 + currentTest = tests.shift(); 1.140 + print(currentTest.desc); 1.141 + setInterval(currentTest.interval); 1.142 + } 1.143 + else { 1.144 + clearInterval(); 1.145 + do_test_finished(); 1.146 + } 1.147 +}