browser_not_collect_when_idle.js (3622B)
1 /** Test for Bug 1305950 */ 2 3 const { MockRegistrar } = ChromeUtils.importESModule( 4 "resource://testing-common/MockRegistrar.sys.mjs" 5 ); 6 7 // The mock idle service. 8 var idleService = { 9 _observers: new Set(), 10 _activity: { 11 addCalls: [], 12 removeCalls: [], 13 observerFires: [], 14 }, 15 16 _reset() { 17 this._observers.clear(); 18 this._activity.addCalls = []; 19 this._activity.removeCalls = []; 20 this._activity.observerFires = []; 21 }, 22 23 _fireObservers(state) { 24 for (let observer of this._observers.values()) { 25 observer.observe(observer, state, null); 26 this._activity.observerFires.push(state); 27 } 28 }, 29 30 QueryInterface: ChromeUtils.generateQI(["nsIUserIdleService"]), 31 idleTime: 19999, 32 33 addIdleObserver(observer, time) { 34 this._observers.add(observer); 35 this._activity.addCalls.push(time); 36 }, 37 38 removeIdleObserver(observer, time) { 39 this._observers.delete(observer); 40 this._activity.removeCalls.push(time); 41 }, 42 }; 43 44 add_task(async function testIntervalChanges() { 45 const PREF_SS_INTERVAL = 2000; 46 47 // We speed up the interval between session saves to ensure that the test 48 // runs quickly. 49 Services.prefs.setIntPref("browser.sessionstore.interval", PREF_SS_INTERVAL); 50 51 // Increase `idleDelay` to 1 day to update the pre-registered idle observer 52 // in "real" idle service to avoid possible interference, especially for the 53 // CI server environment. 54 Services.prefs.setIntPref("browser.sessionstore.idleDelay", 86400); 55 56 // Mock an idle service. 57 let fakeIdleService = MockRegistrar.register( 58 "@mozilla.org/widget/useridleservice;1", 59 idleService 60 ); 61 idleService._reset(); 62 63 registerCleanupFunction(function () { 64 Services.prefs.clearUserPref("browser.sessionstore.interval"); 65 MockRegistrar.unregister(fakeIdleService); 66 }); 67 68 // Hook idle/active observer to mock idle service by changing pref `idleDelay` 69 // to a whatever value, which will not be used. 70 Services.prefs.setIntPref("browser.sessionstore.idleDelay", 5000); 71 72 // Wait a `sessionstore-state-write-complete` event from any previous 73 // scheduled state write. This is needed since the `_lastSaveTime` in 74 // runDelayed() should be set at least once, or the `_isIdle` flag will not 75 // become effective. 76 info("Waiting for sessionstore-state-write-complete notification"); 77 await TestUtils.topicObserved("sessionstore-state-write-complete"); 78 79 info( 80 "Got the sessionstore-state-write-complete notification, now testing idle mode" 81 ); 82 83 // Enter the "idle mode" (raise the `_isIdle` flag) by firing idle 84 // observer of mock idle service. 85 idleService._fireObservers("idle"); 86 87 // Cancel any possible state save, which is not related with this test to 88 // avoid interference. 89 SessionSaver.cancel(); 90 91 let p1 = promiseSaveState(); 92 93 // Schedule a state write, which is expeced to be postponed after about 94 // `browser.sessionstore.interval.idle` ms, since the idle flag was just set. 95 SessionSaver.runDelayed(0); 96 97 // We expect `p1` hits the timeout. 98 await Assert.rejects( 99 p1, 100 /Save state timeout/, 101 "[Test 1A] No state write during idle." 102 ); 103 104 // Test again for better reliability. Same, we expect following promise hits 105 // the timeout. 106 await Assert.rejects( 107 promiseSaveState(), 108 /Save state timeout/, 109 "[Test 1B] Again: No state write during idle." 110 ); 111 112 // Back to the active mode. 113 info("Start to test active mode..."); 114 idleService._fireObservers("active"); 115 116 info("[Test 2] Waiting for sessionstore-state-write-complete during active"); 117 await TestUtils.topicObserved("sessionstore-state-write-complete"); 118 });