browser_closewatcher_integration.js (2043B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const TEST_PAGE = 7 getRootDirectory(gTestPath).replace( 8 "chrome://mochitests/content", 9 "https://example.com" 10 ) + "dummy_page.html"; 11 const CLOSEWATCHER_PAGE = 12 getRootDirectory(gTestPath).replace( 13 "chrome://mochitests/content", 14 "https://example.com" 15 ) + "page_with_closewatcher.html"; 16 17 const runTest = 18 (bool, baseURL = TEST_PAGE) => 19 async () => { 20 // Wait for the session data to be flushed before continuing the test 21 Services.prefs.setBoolPref("dom.closewatcher.enabled", true); 22 23 let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, baseURL); 24 25 await new Promise(resolve => 26 SessionStore.getSessionHistory(gBrowser.selectedTab, resolve) 27 ); 28 29 // Assert the hasActiveCloseWatcher property 30 is( 31 gBrowser.selectedBrowser.hasActiveCloseWatcher, 32 bool, 33 `hasActiveCloseWatcher is ${bool}` 34 ); 35 36 gBrowser.selectedBrowser.processCloseRequest(); 37 38 // CloseWatcher may not be immediately closed as the request is over IPC, so allow some grace 39 // by checking every 100ms to see if hasActiveCloseWatcher flips to false. 40 { 41 // eslint-disable-next-line mozilla/no-arbitrary-setTimeout 42 const sleep = ms => new Promise(r => setTimeout(r, ms)); 43 const hasActiveCloseWatcherEventuallyFalse = (async () => { 44 while (gBrowser.selectedBrowser.hasActiveCloseWatcher) { 45 await sleep(50); 46 } 47 })(); 48 await Promise.race([hasActiveCloseWatcherEventuallyFalse, sleep(3000)]); 49 } 50 51 // Assert the hasActiveCloseWatcher property is false after a close request 52 is( 53 gBrowser.selectedBrowser.hasActiveCloseWatcher, 54 false, 55 `hasActiveCloseWatcher is false after processCloseRequest` 56 ); 57 58 BrowserTestUtils.removeTab(tab); 59 60 Services.prefs.clearUserPref("dom.closewatcher.enabled"); 61 }; 62 63 add_task(runTest(false, TEST_PAGE)); 64 add_task(runTest(true, CLOSEWATCHER_PAGE));