Files
firefox/devtools/server/tests/mochitest/test_connectToFrame.html
T

138 lines
4.1 KiB
HTML

<!DOCTYPE HTML>
<html>
<!--
Bug 966991 - Test DebuggerServer.connectToFrame
-->
<head>
<meta charset="utf-8">
<title>Mozilla Bug</title>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
</head>
<body>
<pre id="test">
<script type="application/javascript">
"use strict";
// Part of this is a frame script.
/* eslint-env mozilla/frame-script */
const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm", {});
const { DebuggerClient } = require("devtools/shared/client/debugger-client");
const { DebuggerServer } = require("devtools/server/main");
window.onload = function() {
SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({
"set": [
// Always log packets when running tests.
["devtools.debugger.log", true],
["dom.mozBrowserFramesEnabled", true],
],
}, runTests);
};
function runTests() {
// Create a minimal iframe with a message manager
const iframe = document.createElement("iframe");
iframe.mozbrowser = true;
document.body.appendChild(iframe);
const mm = iframe.QueryInterface(Ci.nsIFrameLoaderOwner).frameLoader.messageManager;
// Register a test actor in the child process so that we can know if and when
// this fake actor is destroyed.
mm.loadFrameScript("data:text/javascript,new " + function FrameScriptScope() {
/* eslint-disable no-shadow */
const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm", {});
const { DebuggerServer } = require("devtools/server/main");
const { ActorRegistry } = require("devtools/server/actors/utils/actor-registry");
/* eslint-enable no-shadow */
DebuggerServer.init();
function TestActor() {
dump("instanciate test actor\n");
}
TestActor.prototype = {
actorPrefix: "test",
destroy: function() {
sendAsyncMessage("test-actor-destroyed", null);
},
hello: function() {
return {msg: "world"};
},
};
TestActor.prototype.requestTypes = {
"hello": TestActor.prototype.hello,
};
ActorRegistry.addTargetScopedActor({
constructorName: "TestActor",
constructorFun: TestActor,
}, "testActor");
}, false);
// Instantiate a minimal server
DebuggerServer.init();
if (!DebuggerServer.createRootActor) {
DebuggerServer.registerAllActors();
}
function firstClient() {
// Fake a first connection to an iframe
const transport = DebuggerServer.connectPipe();
const conn = transport._serverConnection;
const client = new DebuggerClient(transport);
DebuggerServer.connectToFrame(conn, iframe).then(actor => {
ok(actor.testActor, "Got the test actor");
// Ensure sending at least one request to our actor,
// otherwise it won't be instantiated, nor be destroyed...
client.request({
to: actor.testActor,
type: "hello",
}, function(response) {
// Then close the client. That should end up cleaning our test actor
client.close();
// Ensure that our test actor got cleaned up;
// its destroy method should be called
mm.addMessageListener("test-actor-destroyed", function listener() {
mm.removeMessageListener("test-actor-destroyed", listener);
ok(true, "Actor is cleaned up");
secondClient(actor.testActor);
});
});
});
}
function secondClient(firstActor) {
// Then fake a second one, that should spawn a new set of target-scoped actors
const transport = DebuggerServer.connectPipe();
const conn = transport._serverConnection;
const client = new DebuggerClient(transport);
DebuggerServer.connectToFrame(conn, iframe).then(actor => {
ok(actor.testActor, "Got a test actor for the second connection");
isnot(actor.testActor, firstActor,
"We get different actor instances between two connections");
client.close(cleanup);
});
}
function cleanup() {
DebuggerServer.destroy();
iframe.remove();
SimpleTest.finish();
}
firstClient();
}
</script>
</pre>
</body>
</html>