browser_cancel_content_js.js (2064B)
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 requestLongerTimeout(10); 8 9 const TEST_PAGE = 10 "http://mochi.test:8888/browser/dom/ipc/tests/file_cancel_content_js.html"; 11 const NEXT_PAGE = "http://mochi.test:8888/browser/dom/ipc/tests/"; 12 const JS_URI = "javascript:void(document.title = 'foo')"; 13 14 async function test_navigation(nextPage, shouldCancel) { 15 await SpecialPowers.pushPrefEnv({ 16 set: [ 17 ["dom.max_script_run_time", 20], 18 // Force a single process so that the navigation will complete in the same 19 // process as the previous page which is running the long-running script. 20 ["dom.ipc.processCount", 1], 21 ["dom.ipc.processCount.webIsolated", 1], 22 ], 23 }); 24 let tab = await BrowserTestUtils.openNewForegroundTab({ 25 gBrowser, 26 opening: TEST_PAGE, 27 }); 28 29 const loopEnded = ContentTask.spawn(tab.linkedBrowser, [], async function () { 30 await new Promise(resolve => { 31 content.addEventListener("LongLoopEnded", resolve, { 32 once: true, 33 }); 34 }); 35 }); 36 37 // Wait for the test page's long-running JS loop to start. 38 await ContentTask.spawn(tab.linkedBrowser, [], function () { 39 content.dispatchEvent(new content.Event("StartLongLoop")); 40 }); 41 42 info(`navigating to ${nextPage}`); 43 const nextPageLoaded = BrowserTestUtils.waitForContentEvent( 44 tab.linkedBrowser, 45 "DOMTitleChanged" 46 ); 47 BrowserTestUtils.startLoadingURIString(gBrowser, nextPage); 48 49 const result = await Promise.race([ 50 nextPageLoaded, 51 loopEnded.then(() => "timeout"), 52 ]); 53 54 const timedOut = result === "timeout"; 55 if (shouldCancel) { 56 Assert.strictEqual(timedOut, false, "expected next page to be loaded"); 57 } else { 58 Assert.strictEqual(timedOut, true, "expected timeout"); 59 } 60 61 BrowserTestUtils.removeTab(tab); 62 } 63 64 add_task(async () => test_navigation(NEXT_PAGE, true)); 65 add_task(async () => test_navigation(JS_URI, false));