michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * What this is aimed to test: michael@0: * michael@0: * Expiration relies on an interval, that is user-preffable setting michael@0: * "places.history.expiration.interval_seconds". michael@0: * On pref change it will stop current interval timer and fire a new one, michael@0: * that will obey the new value. michael@0: * If the pref is set to a number <= 0 we will use the default value. michael@0: */ michael@0: michael@0: // Default timer value for expiration in seconds. Must have same value as michael@0: // PREF_INTERVAL_SECONDS_NOTSET in nsPlacesExpiration. michael@0: const DEFAULT_TIMER_DELAY_SECONDS = 3 * 60; michael@0: michael@0: // Sync this with the const value in the component. michael@0: const EXPIRE_AGGRESSIVITY_MULTIPLIER = 3; michael@0: michael@0: // Provide a mock timer implementation, so there is no need to wait seconds to michael@0: // achieve test results. michael@0: const Cm = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); michael@0: const TIMER_CONTRACT_ID = "@mozilla.org/timer;1"; michael@0: const kMockCID = Components.ID("52bdf457-4de3-48ae-bbbb-f3cbcbcad05f"); michael@0: michael@0: // Used to preserve the original timer factory. michael@0: let gOriginalCID = Cm.contractIDToCID(TIMER_CONTRACT_ID); michael@0: michael@0: // The mock timer factory. michael@0: let gMockTimerFactory = { michael@0: createInstance: function MTF_createInstance(aOuter, aIID) { michael@0: if (aOuter != null) michael@0: throw Cr.NS_ERROR_NO_AGGREGATION; michael@0: return mockTimerImpl.QueryInterface(aIID); michael@0: }, michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([ michael@0: Ci.nsIFactory, michael@0: ]) michael@0: } michael@0: michael@0: let mockTimerImpl = { michael@0: initWithCallback: function MTI_initWithCallback(aCallback, aDelay, aType) { michael@0: print("Checking timer delay equals expected interval value"); michael@0: if (!currentTest) michael@0: return; michael@0: // History status is not dirty, so the timer is delayed. michael@0: do_check_eq(aDelay, currentTest.expectedTimerDelay * 1000 * EXPIRE_AGGRESSIVITY_MULTIPLIER) michael@0: michael@0: do_execute_soon(runNextTest); michael@0: }, michael@0: michael@0: cancel: function() {}, michael@0: initWithFuncCallback: function() {}, michael@0: init: function() {}, michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([ michael@0: Ci.nsITimer, michael@0: ]) michael@0: } michael@0: michael@0: function replace_timer_factory() { michael@0: Cm.registerFactory(kMockCID, michael@0: "Mock timer", michael@0: TIMER_CONTRACT_ID, michael@0: gMockTimerFactory); michael@0: } michael@0: michael@0: do_register_cleanup(function() { michael@0: // Shutdown expiration before restoring original timer, otherwise we could michael@0: // leak due to the different implementation. michael@0: shutdownExpiration(); michael@0: michael@0: // Restore original timer factory. michael@0: Cm.unregisterFactory(kMockCID, michael@0: gMockTimerFactory); michael@0: Cm.registerFactory(gOriginalCID, michael@0: "", michael@0: TIMER_CONTRACT_ID, michael@0: null); michael@0: }); michael@0: michael@0: michael@0: let tests = [ michael@0: michael@0: // This test should be the first, so the interval won't be influenced by michael@0: // status of history. michael@0: { desc: "Set interval to 1s.", michael@0: interval: 1, michael@0: expectedTimerDelay: 1 michael@0: }, michael@0: michael@0: { desc: "Set interval to a negative value.", michael@0: interval: -1, michael@0: expectedTimerDelay: DEFAULT_TIMER_DELAY_SECONDS michael@0: }, michael@0: michael@0: { desc: "Set interval to 0.", michael@0: interval: 0, michael@0: expectedTimerDelay: DEFAULT_TIMER_DELAY_SECONDS michael@0: }, michael@0: michael@0: { desc: "Set interval to a large value.", michael@0: interval: 100, michael@0: expectedTimerDelay: 100 michael@0: }, michael@0: michael@0: ]; michael@0: michael@0: let currentTest; michael@0: michael@0: function run_test() { michael@0: // The pref should not exist by default. michael@0: try { michael@0: getInterval(); michael@0: do_throw("interval pref should not exist by default"); michael@0: } michael@0: catch (ex) {} michael@0: michael@0: // Use our own mock timer implementation. michael@0: replace_timer_factory(); michael@0: michael@0: // Force the component, so it will start observing preferences. michael@0: force_expiration_start(); michael@0: michael@0: runNextTest(); michael@0: do_test_pending(); michael@0: } michael@0: michael@0: function runNextTest() { michael@0: if (tests.length) { michael@0: currentTest = tests.shift(); michael@0: print(currentTest.desc); michael@0: setInterval(currentTest.interval); michael@0: } michael@0: else { michael@0: clearInterval(); michael@0: do_test_finished(); michael@0: } michael@0: }