diff --git a/dom/tests/browser/.eslintrc.js b/dom/tests/browser/.eslintrc.js new file mode 100644 index 000000000000..6596de0282f2 --- /dev/null +++ b/dom/tests/browser/.eslintrc.js @@ -0,0 +1,18 @@ +"use strict"; + +module.exports = { + overrides: [ + { + files: [ + "file_module_loaded.js", + "file_module_loaded2.js", + ], + parserOptions: { + sourceType: "module", + }, + }, + ], + "extends": [ + "plugin:mozilla/browser-test" + ] +}; diff --git a/dom/tests/browser/browser.ini b/dom/tests/browser/browser.ini index c92fdb7bbd54..24c8d5ad874e 100644 --- a/dom/tests/browser/browser.ini +++ b/dom/tests/browser/browser.ini @@ -79,6 +79,11 @@ support-files = [browser_test_toolbars_visibility.js] support-files = test_new_window_from_content_child.html +[browser_unlinkable_about_page_can_load_module_scripts.js] +support-files = + file_load_module_script.html + file_module_loaded.js + file_module_loaded2.js [browser_xhr_sandbox.js] [browser_noopener.js] skip-if = (verify && debug && (os == 'linux')) diff --git a/dom/tests/browser/browser_unlinkable_about_page_can_load_module_scripts.js b/dom/tests/browser/browser_unlinkable_about_page_can_load_module_scripts.js new file mode 100644 index 000000000000..78d176756225 --- /dev/null +++ b/dom/tests/browser/browser_unlinkable_about_page_can_load_module_scripts.js @@ -0,0 +1,59 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +"use strict"; + +const kTestPage = getRootDirectory(gTestPath) + "file_load_module_script.html"; + +const kDefaultFlags = Ci.nsIAboutModule.ALLOW_SCRIPT | + Ci.nsIAboutModule.URI_MUST_LOAD_IN_CHILD | + Ci.nsIAboutModule.URI_CAN_LOAD_IN_PRIVILEGED_CHILD | + Ci.nsIAboutModule.URI_SAFE_FOR_UNTRUSTED_CONTENT; + +const kAboutPagesRegistered = Promise.all([ + BrowserTestUtils.registerAboutPage( + registerCleanupFunction, "test-linkable-page", kTestPage, + kDefaultFlags | Ci.nsIAboutModule.MAKE_LINKABLE), + BrowserTestUtils.registerAboutPage( + registerCleanupFunction, "test-unlinkable-page", kTestPage, + kDefaultFlags), +]); + +add_task(async function() { + await kAboutPagesRegistered; + + let consoleListener = { + observe() { + errorCount++; + if (shouldHaveJSError) { + ok(true, "JS error is expected and received"); + } else { + ok(false, "Unexpected JS error was received"); + } + }, + }; + Services.console.registerListener(consoleListener); + registerCleanupFunction(() => Services.console.unregisterListener(consoleListener)); + + let shouldHaveJSError = true; + let errorCount = 0; + await BrowserTestUtils.withNewTab({gBrowser, url: "about:test-linkable-page"}, async (browser) => { + await ContentTask.spawn(browser, null, () => { + isnot(content.document.body.textContent, "scriptLoaded", + "The page content shouldn't be changed on the linkable page"); + }); + }); + ok(errorCount > 0, "Should have an error when loading test-linkable-page, got " + errorCount); + + shouldHaveJSError = false; + errorCount = 0; + await BrowserTestUtils.withNewTab({gBrowser, url: "about:test-unlinkable-page"}, async (browser) => { + await ContentTask.spawn(browser, null, () => { + is(content.document.body.textContent, "scriptLoaded", + "The page content should be changed on the unlinkable page"); + }); + }); + is(errorCount, 0, "Should have no errors when loading test-unlinkable-page"); +}); diff --git a/dom/tests/browser/file_load_module_script.html b/dom/tests/browser/file_load_module_script.html new file mode 100644 index 000000000000..e1ba08d8df32 --- /dev/null +++ b/dom/tests/browser/file_load_module_script.html @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/dom/tests/browser/file_module_loaded.js b/dom/tests/browser/file_module_loaded.js new file mode 100644 index 000000000000..8763889e1f5d --- /dev/null +++ b/dom/tests/browser/file_module_loaded.js @@ -0,0 +1,6 @@ +"use strict"; + +import setBodyText from "chrome://mochitests/content/browser/dom/tests/browser/file_module_loaded2.js"; +document.addEventListener("DOMContentLoaded", () => { + setBodyText(); +}); diff --git a/dom/tests/browser/file_module_loaded2.js b/dom/tests/browser/file_module_loaded2.js new file mode 100644 index 000000000000..28b26cd16e55 --- /dev/null +++ b/dom/tests/browser/file_module_loaded2.js @@ -0,0 +1,3 @@ +export default function setBodyText() { + document.body.textContent = "scriptLoaded"; +}