browser_shutdown_acc_reference.js (2024B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 add_task(async function () { 8 // Create a11y service. 9 const [a11yInitObserver, a11yInit] = initAccService(); 10 await a11yInitObserver; 11 12 let accService = Cc["@mozilla.org/accessibilityService;1"].getService( 13 Ci.nsIAccessibilityService 14 ); 15 16 await a11yInit; 17 ok(accService, "Service initialized"); 18 19 // Accessible object reference will live longer than the scope of this 20 // function. 21 let acc = await new Promise(resolve => { 22 let intervalId = setInterval(() => { 23 let tabAcc = accService.getAccessibleFor(gBrowser.selectedTab); 24 if (tabAcc) { 25 clearInterval(intervalId); 26 resolve(tabAcc); 27 } 28 }, 10); 29 }); 30 ok(acc, "Accessible object is created"); 31 32 let canShutdown = false; 33 // This promise will resolve only if canShutdown flag is set to true. If 34 // 'a11y-init-or-shutdown' event with '0' flag comes before it can be shut 35 // down, the promise will reject. 36 const [a11yShutdownObserver, a11yShutdownPromise] = shutdownAccService(); 37 await a11yShutdownObserver; 38 const a11yShutdown = new Promise((resolve, reject) => 39 a11yShutdownPromise.then(() => 40 canShutdown 41 ? resolve() 42 : reject("Accessible service was shut down incorrectly") 43 ) 44 ); 45 46 accService = null; 47 ok(!accService, "Service is removed"); 48 49 // Force garbage collection that should not trigger shutdown because there is 50 // a reference to an accessible object. 51 forceGC(); 52 // Have some breathing room when removing a11y service references. 53 await TestUtils.waitForTick(); 54 55 // Now allow a11y service to shutdown. 56 canShutdown = true; 57 // Remove a reference to an accessible object. 58 acc = null; 59 ok(!acc, "Accessible object is removed"); 60 61 // Force garbage collection that should now trigger shutdown. 62 forceGC(); 63 await a11yShutdown; 64 });