mirror of
https://github.com/roytam1/basilisk55.git
synced 2026-05-26 15:02:46 +00:00
3a4eb9ff7e
bug1346389, bug1382303, bug1383000, bug1339931, bug1346620, bug1351349, bug546387, bug1368150, bug1361132, bug1345781, bug1343781, bug1390980, bug1387918, bug1373222, bug1385272, bug1390002, bug1379539, bug1371657, bug1386905, bug1379540, bug1379536, bug1384308, bug1317900, bug1279171, bug1384801, bug1396320, bug1396570, bug1368269, bug1394024, bug1400721, bug1367482, bug1359624, bug1376163, bug1392988, bug1389908
171 lines
5.9 KiB
HTML
171 lines
5.9 KiB
HTML
<!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>
|