diff options
author | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
---|---|---|
committer | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
commit | 5f8de423f190bbb79a62f804151bc24824fa32d8 (patch) | |
tree | 10027f336435511475e392454359edea8e25895d /toolkit/components/extensions/test/mochitest/test_ext_contentscript_create_iframe.html | |
parent | 49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff) | |
download | UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip |
Add m-esr52 at 52.6.0
Diffstat (limited to 'toolkit/components/extensions/test/mochitest/test_ext_contentscript_create_iframe.html')
-rw-r--r-- | toolkit/components/extensions/test/mochitest/test_ext_contentscript_create_iframe.html | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/toolkit/components/extensions/test/mochitest/test_ext_contentscript_create_iframe.html b/toolkit/components/extensions/test/mochitest/test_ext_contentscript_create_iframe.html new file mode 100644 index 000000000..8aac3e213 --- /dev/null +++ b/toolkit/components/extensions/test/mochitest/test_ext_contentscript_create_iframe.html @@ -0,0 +1,165 @@ +<!DOCTYPE HTML> +<html> +<head> + <title>Test for content script</title> + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script> + <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script> + <script type="text/javascript" src="head.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> + +<!-- WORKAROUND: this textarea hack is used to contain the html page source without escaping it --> +<textarea id="test-asset"> + <!DOCTYPE HTML> + <html> + <head> + <meta charset="utf-8"> + <script type="text/javascript" src="content_script_iframe.js"></script> + </head> + </html> +</textarea> + +<script type="text/javascript"> +"use strict"; + +add_task(function* test_contentscript_create_iframe() { + function background() { + browser.runtime.onMessage.addListener((msg, sender) => { + let {name, availableAPIs, manifest, testGetManifest} = msg; + let hasExtTabsAPI = availableAPIs.indexOf("tabs") > 0; + let hasExtWindowsAPI = availableAPIs.indexOf("windows") > 0; + + browser.test.assertFalse(hasExtTabsAPI, "the created iframe should not be able to use privileged APIs (tabs)"); + browser.test.assertFalse(hasExtWindowsAPI, "the created iframe should not be able to use privileged APIs (windows)"); + + let {applications: {gecko: {id: expectedManifestGeckoId}}} = chrome.runtime.getManifest(); + let {applications: {gecko: {id: actualManifestGeckoId}}} = manifest; + + browser.test.assertEq(actualManifestGeckoId, expectedManifestGeckoId, + "the add-on manifest should be accessible from the created iframe" + ); + + let {applications: {gecko: {id: testGetManifestGeckoId}}} = testGetManifest; + + browser.test.assertEq(testGetManifestGeckoId, expectedManifestGeckoId, + "GET_MANIFEST() returns manifest data before extension unload" + ); + + browser.test.sendMessage(name); + }); + } + + function contentScript() { + let iframe = document.createElement("iframe"); + iframe.setAttribute("src", browser.runtime.getURL("content_script_iframe.html")); + document.body.appendChild(iframe); + } + + function contentScriptIframe() { + window.GET_MANIFEST = browser.runtime.getManifest.bind(null); + + window.testGetManifestException = () => { + try { + window.GET_MANIFEST(); + } catch (exception) { + return String(exception); + } + }; + + let testGetManifest = window.GET_MANIFEST(); + + let manifest = browser.runtime.getManifest(); + let availableAPIs = Object.keys(browser); + + browser.runtime.sendMessage({ + name: "content-script-iframe-loaded", + availableAPIs, + manifest, + testGetManifest, + }); + } + + const ID = "contentscript@tests.mozilla.org"; + let extensionData = { + manifest: { + applications: {gecko: {id: ID}}, + content_scripts: [ + { + "matches": ["http://mochi.test/*/file_sample.html"], + "js": ["content_script.js"], + "run_at": "document_idle", + }, + ], + web_accessible_resources: [ + "content_script_iframe.html", + ], + }, + + background, + + files: { + "content_script.js": contentScript, + "content_script_iframe.html": document.querySelector("#test-asset").textContent, + "content_script_iframe.js": contentScriptIframe, + }, + }; + + let extension = ExtensionTestUtils.loadExtension(extensionData); + + let contentScriptIframeCreatedPromise = new Promise(resolve => { + extension.onMessage("content-script-iframe-loaded", () => { resolve(); }); + }); + + yield extension.startup(); + info("extension loaded"); + + let win = window.open("file_sample.html"); + + yield Promise.all([waitForLoad(win), contentScriptIframeCreatedPromise]); + info("content script privileged iframe loaded and executed"); + + info("testing APIs availability once the extension is unloaded..."); + + let iframeWindow = SpecialPowers.wrap(win)[0]; + + ok(iframeWindow, "content script enabled iframe found"); + ok(/content_script_iframe\.html$/.test(iframeWindow.location), "the found iframe has the expected URL"); + + yield extension.unload(); + info("extension unloaded"); + + info("test content script APIs not accessible from the frame once the extension is unloaded"); + + let ww = SpecialPowers.Cu.waiveXrays(iframeWindow); + let isDeadWrapper = SpecialPowers.Cu.isDeadWrapper(ww.browser); + ok(!isDeadWrapper, "the API object should not be a dead object"); + + let manifest; + let manifestException; + + try { + manifest = ww.browser.runtime.getManifest(); + } catch (e) { + manifestException = e; + } + + ok(!manifest, "manifest should be undefined"); + + is(String(manifestException), "TypeError: ww.browser.runtime is undefined", + "expected exception received"); + + let getManifestException = ww.testGetManifestException(); + + is(getManifestException, "TypeError: can't access dead object", + "expected exception received"); + + win.close(); + + info("done"); +}); +</script> + +</body> +</html> |