diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-15 03:34:42 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-15 03:34:42 +0000 |
commit | da4c7e7ed675c3bf405668739c3012d140856109 (patch) | |
tree | cdd868dba063fecba609a1d819de271f0d51b23e /toolkit/components/extensions/test/mochitest/test_ext_tabs_sendMessage.html | |
parent | Adding upstream version 125.0.3. (diff) | |
download | firefox-da4c7e7ed675c3bf405668739c3012d140856109.tar.xz firefox-da4c7e7ed675c3bf405668739c3012d140856109.zip |
Adding upstream version 126.0.upstream/126.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/extensions/test/mochitest/test_ext_tabs_sendMessage.html')
-rw-r--r-- | toolkit/components/extensions/test/mochitest/test_ext_tabs_sendMessage.html | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/toolkit/components/extensions/test/mochitest/test_ext_tabs_sendMessage.html b/toolkit/components/extensions/test/mochitest/test_ext_tabs_sendMessage.html index 4b230c258c..8c6dfeee7c 100644 --- a/toolkit/components/extensions/test/mochitest/test_ext_tabs_sendMessage.html +++ b/toolkit/components/extensions/test/mochitest/test_ext_tabs_sendMessage.html @@ -11,6 +11,10 @@ <script> "use strict"; +const { + WebExtensionPolicy +} = SpecialPowers.Cu.getGlobalForObject(SpecialPowers.Services); + add_task(async function test_tabs_sendMessage_to_extension_page_frame() { let extension = ExtensionTestUtils.loadExtension({ manifest: { @@ -27,6 +31,10 @@ add_task(async function test_tabs_sendMessage_to_extension_page_frame() { browser.runtime.onMessage.addListener(async (msg, sender) => { browser.test.assertEq(msg, "page-script-ready"); browser.test.assertEq(sender.url, browser.runtime.getURL("page.html")); + browser.test.assertEq( + sender.origin, + new URL(browser.runtime.getURL("/")).origin + ); let tabId = sender.tab.id; let response = await browser.tabs.sendMessage(tabId, "tab-sendMessage"); @@ -147,6 +155,178 @@ add_task(async function test_tabs_sendMessage_using_frameId() { await extension.unload(); }); +add_task(async function test_tabs_sendMessage_aboutBlank() { + const PATH = "tests/toolkit/components/extensions/test/mochitest/file_with_about_blank.html"; + + let extension = ExtensionTestUtils.loadExtension({ + manifest: { + content_scripts: [ + { + match_about_blank: true, + matches: ["*://mochi.test/*/file_with_about_blank.html"], + all_frames: true, + js: ["cs.js"], + }, + ], + }, + + background() { + browser.runtime.onMessage.addListener((msg, { url, origin }) => { + browser.test.assertEq("cs", msg, "expected message from cs.js"); + + const kind = url.startsWith("about:") ? url : "top"; + switch (kind) { + case "top": + browser.test.assertTrue( + url.endsWith("/file_with_about_blank.html"), + "expected correct url" + ); + browser.test.assertEq( + "http://mochi.test:8888", + origin, + "expected correct origin" + ); + break; + + case "about:blank": + browser.test.assertEq("about:blank", url, "expected correct url"); + browser.test.assertEq( + "http://mochi.test:8888", + origin, + "expected correct origin" + ); + break; + + case "about:srcdoc": + browser.test.assertEq("about:srcdoc", url, "expected correct url"); + browser.test.assertEq( + "http://mochi.test:8888", + origin, + "expected correct origin" + ); + break; + + default: + browser.test.fail(`Unexpected kind: ${kind}`); + } + + browser.test.sendMessage(`done:${kind}`); + }); + + browser.test.sendMessage("ready"); + }, + + files: { + "cs.js"() { + browser.runtime.sendMessage("cs"); + }, + }, + }); + + await extension.startup(); + await extension.awaitMessage("ready"); + + let win = window.open("http://mochi.test:8888/" + PATH); + await extension.awaitMessage("done:top"); + await extension.awaitMessage("done:about:blank"); + await extension.awaitMessage("done:about:srcdoc"); + win.close(); + + await extension.unload(); +}); + +add_task(async function test_tabs_sendMessage_opaqueOrigin() { + let extension = ExtensionTestUtils.loadExtension({ + manifest: { + content_scripts: [ + { + match_about_blank: true, + // The combination of `matches` and `exclude_matches` below allows us + // to only inject in the top-level about:blank. + matches: ["*://*/*"], + exclude_matches: ["<all_urls>"], + js: ["cs.js"], + }, + ], + }, + + async background() { + let tab; + + browser.runtime.onMessage.addListener(async (msg, { url, origin }) => { + browser.test.assertEq("cs", msg, "expected message from cs.js"); + browser.test.assertEq("about:blank", url, "expected correct url"); + browser.test.assertEq("null", origin, "expected correct origin"); + + await browser.tabs.remove(tab.id); + browser.test.sendMessage("done"); + }); + + tab = await browser.tabs.create({ url: "about:blank" }); + }, + + files: { + "cs.js"() { + browser.runtime.sendMessage("cs"); + }, + }, + }); + + await extension.startup(); + await extension.awaitMessage("done"); + await extension.unload(); +}); + +add_task(async function test_tabs_sendMessage_blob() { + // See: https://bugzilla.mozilla.org/show_bug.cgi?id=1724099 + if (!WebExtensionPolicy.useRemoteWebExtensions) { + await SpecialPowers.pushPrefEnv({ + set: [["security.allow_unsafe_parent_loads", true]], + }); + } + + let extension = ExtensionTestUtils.loadExtension({ + async background() { + browser.runtime.onMessage.addListener(async (msg, { url, origin }) => { + browser.test.assertEq("script", msg, "expected message from script.js"); + browser.test.assertTrue( + url.startsWith("blob:moz-extension://"), + "expected correct url" + ); + browser.test.assertEq( + new URL(browser.runtime.getURL("/")).origin, + origin, + "expected correct origin" + ); + + browser.test.sendMessage("done"); + }); + + const blob = new Blob( + [`<script src="${browser.runtime.getURL("script.js")}"><\/script>`], + { type: "text/html" } + ); + const iframe = document.createElement("iframe"); + iframe.src = URL.createObjectURL(blob); + document.body.appendChild(iframe); + }, + + files: { + "script.js"() { + browser.runtime.sendMessage("script"); + }, + }, + }); + + await extension.startup(); + await extension.awaitMessage("done"); + await extension.unload(); + + if (!WebExtensionPolicy.useRemoteWebExtensions) { + await SpecialPowers.popPrefEnv(); + } +}); + </script> </body> </html> |