diff options
author | Matt A. Tobin <email@mattatobin.com> | 2018-02-09 11:10:00 -0500 |
---|---|---|
committer | Matt A. Tobin <email@mattatobin.com> | 2018-02-09 11:10:00 -0500 |
commit | f164d9124708b50789dbb6959e1de96cc5697c48 (patch) | |
tree | 6dffd12e08c5383130df0252fb69cd6d6330794f /toolkit/components/webextensions/test/mochitest/test_ext_unload_frame.html | |
parent | 30de4018913f0cdaea19d1dd12ecd8209e2ed08e (diff) | |
download | UXP-f164d9124708b50789dbb6959e1de96cc5697c48.tar UXP-f164d9124708b50789dbb6959e1de96cc5697c48.tar.gz UXP-f164d9124708b50789dbb6959e1de96cc5697c48.tar.lz UXP-f164d9124708b50789dbb6959e1de96cc5697c48.tar.xz UXP-f164d9124708b50789dbb6959e1de96cc5697c48.zip |
Rename Toolkit's webextensions component directory to better reflect what it is.
Diffstat (limited to 'toolkit/components/webextensions/test/mochitest/test_ext_unload_frame.html')
-rw-r--r-- | toolkit/components/webextensions/test/mochitest/test_ext_unload_frame.html | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/toolkit/components/webextensions/test/mochitest/test_ext_unload_frame.html b/toolkit/components/webextensions/test/mochitest/test_ext_unload_frame.html new file mode 100644 index 000000000..5572de281 --- /dev/null +++ b/toolkit/components/webextensions/test/mochitest/test_ext_unload_frame.html @@ -0,0 +1,170 @@ +<!DOCTYPE HTML> +<html> +<head> + <title>WebExtensions test</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> + +<script> +"use strict"; + +/* globals delayedNotifyPass */ // Available in the background page of the test extensions. + +// Background and content script for testSendMessage_* +function sendMessage_background() { + browser.runtime.onMessage.addListener((msg, sender, sendResponse) => { + browser.test.assertEq("from frame", msg, "Expected message from frame"); + sendResponse("msg from back"); // Should not throw or anything like that. + delayedNotifyPass("Received sendMessage from closing frame"); + }); +} +function sendMessage_contentScript(testType) { + browser.runtime.sendMessage("from frame", reply => { + // The frame has been removed, so we should not get this callback! + browser.test.fail(`Unexpected reply: ${reply}`); + }); + if (testType == "frame") { + frameElement.remove(); + } else { + window.close(); + } +} + +// Background and content script for testConnect_* +function connect_background() { + browser.runtime.onConnect.addListener(port => { + browser.test.assertEq("port from frame", port.name); + + let disconnected = false; + let hasMessage = false; + port.onDisconnect.addListener(() => { + browser.test.assertFalse(disconnected, "onDisconnect should fire once"); + disconnected = true; + browser.test.assertTrue(hasMessage, "Expected onMessage before onDisconnect"); + browser.test.assertEq(null, port.error, "The port is implicitly closed without errors when the other context unloads"); + delayedNotifyPass("Received onDisconnect from closing frame"); + }); + port.onMessage.addListener(msg => { + browser.test.assertFalse(hasMessage, "onMessage should fire once"); + hasMessage = true; + browser.test.assertFalse(disconnected, "Should get message before disconnect"); + browser.test.assertEq("from frame", msg, "Expected message from frame"); + }); + + port.postMessage("reply to closing frame"); + }); +} +function connect_contentScript(testType) { + let isUnloading = false; + addEventListener("pagehide", () => { isUnloading = true; }, {once: true}); + + let port = browser.runtime.connect({name: "port from frame"}); + port.onMessage.addListener(msg => { + // The background page sends a reply as soon as we call runtime.connect(). + // It is possible that the reply reaches this frame before the + // window.close() request has been processed. + if (!isUnloading) { + browser.test.log(`Ignorting unexpected reply ("${msg}") because the page is not being unloaded.`); + return; + } + + // The frame has been removed, so we should not get a reply. + browser.test.fail(`Unexpected reply: ${msg}`); + }); + port.postMessage("from frame"); + + // Removing the frame or window should disconnect the port. + if (testType == "frame") { + frameElement.remove(); + } else { + window.close(); + } +} + +// `testType` is "window" or "frame". +function createTestExtension(testType, backgroundScript, contentScript) { + // Make a roundtrip between the background page and the test runner (which is + // in the same process as the content script) to make sure that we record a + // failure in case the content script's sendMessage or onMessage handlers are + // called even after the frame or window was removed. + function delayedNotifyPass(msg) { + browser.test.onMessage.addListener((type, echoMsg) => { + if (type == "pong") { + browser.test.assertEq(msg, echoMsg, "Echoed reply should be the same"); + browser.test.notifyPass(msg); + } + }); + browser.test.log("Starting ping-pong to flush messages..."); + browser.test.sendMessage("ping", msg); + } + let extension = ExtensionTestUtils.loadExtension({ + background: `${delayedNotifyPass};(${backgroundScript})();`, + manifest: { + content_scripts: [{ + js: ["contentscript.js"], + all_frames: testType == "frame", + matches: ["http://mochi.test/*/file_sample.html"], + }], + }, + files: { + "contentscript.js": `(${contentScript})("${testType}");`, + }, + }); + extension.awaitMessage("ping").then(msg => { + extension.sendMessage("pong", msg); + }); + return extension; +} + +add_task(function* testSendMessage_and_remove_frame() { + let extension = createTestExtension("frame", sendMessage_background, sendMessage_contentScript); + yield extension.startup(); + + let frame = document.createElement("iframe"); + frame.src = "file_sample.html"; + document.body.appendChild(frame); + + yield extension.awaitFinish("Received sendMessage from closing frame"); + yield extension.unload(); +}); + +add_task(function* testConnect_and_remove_frame() { + let extension = createTestExtension("frame", connect_background, connect_contentScript); + yield extension.startup(); + + let frame = document.createElement("iframe"); + frame.src = "file_sample.html"; + document.body.appendChild(frame); + + yield extension.awaitFinish("Received onDisconnect from closing frame"); + yield extension.unload(); +}); + +add_task(function* testSendMessage_and_remove_window() { + let extension = createTestExtension("window", sendMessage_background, sendMessage_contentScript); + yield extension.startup(); + + window.open("file_sample.html"); + + yield extension.awaitFinish("Received sendMessage from closing frame"); + yield extension.unload(); +}); + +add_task(function* testConnect_and_remove_window() { + let extension = createTestExtension("window", connect_background, connect_contentScript); + yield extension.startup(); + + window.open("file_sample.html"); + + yield extension.awaitFinish("Received onDisconnect from closing frame"); + yield extension.unload(); +}); + +</script> +</body> +</html> |