|
1 <SDOCTYPv HTM.> |
|
2 <html> |
|
3 <!-- |
|
4 Bug 966991 - Test DebuggerServer.connectToChild |
|
5 --> |
|
6 <head> |
|
7 <meta charset="utf-8"> |
|
8 <title>Mozilla Bug</title> |
|
9 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> |
|
10 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"> |
|
11 </head> |
|
12 <body> |
|
13 <pre id="test"> |
|
14 <script type="application/javascript;version=1.8"> |
|
15 |
|
16 let Cu = Components.utils; |
|
17 let Cc = Components.classes; |
|
18 let Ci = Components.interfaces; |
|
19 |
|
20 Cu.import("resource://gre/modules/devtools/dbg-client.jsm"); |
|
21 Cu.import("resource://gre/modules/devtools/dbg-server.jsm"); |
|
22 |
|
23 window.onload = function() { |
|
24 SimpleTest.waitForExplicitFinish(); |
|
25 |
|
26 SpecialPowers.pushPrefEnv({ |
|
27 "set": [ |
|
28 // Always log packets when running tests. |
|
29 ["devtools.debugger.log", true], |
|
30 ["dom.mozBrowserFramesEnabled", true] |
|
31 ] |
|
32 }, runTests); |
|
33 } |
|
34 |
|
35 function runTests() { |
|
36 // Create a minimal iframe with a message manager |
|
37 let iframe = document.createElement("iframe"); |
|
38 iframe.mozbrowser = true; |
|
39 document.body.appendChild(iframe); |
|
40 |
|
41 let mm = iframe.QueryInterface(Ci.nsIFrameLoaderOwner).frameLoader.messageManager; |
|
42 |
|
43 // Register a test actor in the child process so that we can know if and when |
|
44 // this fake actor is disconnected. |
|
45 mm.loadFrameScript("data:text/javascript,new " + function FrameScriptScope() { |
|
46 const {DebuggerServer} = Cu.import("resource://gre/modules/devtools/dbg-server.jsm", {}); |
|
47 |
|
48 if (!DebuggerServer.initialized) { |
|
49 DebuggerServer.init(); |
|
50 } |
|
51 |
|
52 function TestActor() {dump("instanciate test actor");} |
|
53 TestActor.prototype = { |
|
54 actorPrefix: "test", |
|
55 |
|
56 disconnect: function () { |
|
57 sendAsyncMessage("test-actor-disconnected", null); |
|
58 }, |
|
59 hello: function () { |
|
60 return {msg:"world"}; |
|
61 } |
|
62 }; |
|
63 TestActor.prototype.requestTypes = { |
|
64 "hello": TestActor.prototype.hello |
|
65 }; |
|
66 DebuggerServer.addTabActor(TestActor, "testActor"); |
|
67 }, false); |
|
68 |
|
69 // Instantiate a minimal server |
|
70 DebuggerServer.init(function () { return true; }); |
|
71 DebuggerServer.addBrowserActors(); |
|
72 |
|
73 function firstClient() { |
|
74 // Fake a first connection to an iframe |
|
75 let transport = DebuggerServer.connectPipe(); |
|
76 let conn = transport._serverConnection; |
|
77 let client = new DebuggerClient(transport); |
|
78 DebuggerServer.connectToChild(conn, iframe).then(actor => { |
|
79 ok(actor.testActor, "Got the test actor"); |
|
80 |
|
81 // Ensure sending at least one request to our actor, |
|
82 // otherwise it won't be instanciated, nor be disconnected... |
|
83 client.request({ |
|
84 to: actor.testActor, |
|
85 type: "hello", |
|
86 }, function (response) { |
|
87 |
|
88 // Then close the client. That should end up cleaning our test actor |
|
89 client.close(); |
|
90 |
|
91 // Ensure that our test actor got cleaned up; |
|
92 // its disconnect method should be called |
|
93 mm.addMessageListener("test-actor-disconnected", function listener() { |
|
94 mm.removeMessageListener("test-actor-disconnected", listener); |
|
95 ok(true, "Actor is cleaned up"); |
|
96 |
|
97 secondClient(actor.testActor); |
|
98 }); |
|
99 }); |
|
100 }); |
|
101 } |
|
102 |
|
103 function secondClient(firstActor) { |
|
104 // Then fake a second one, that should spawn a new set of tab actors |
|
105 let transport = DebuggerServer.connectPipe(); |
|
106 let conn = transport._serverConnection; |
|
107 let client = new DebuggerClient(transport); |
|
108 DebuggerServer.connectToChild(conn, iframe).then(actor => { |
|
109 ok(actor.testActor, "Got a test actor for the second connection"); |
|
110 isnot(actor.testActor, firstActor, "We get different actor instances between two connections"); |
|
111 |
|
112 client.close(cleanup); |
|
113 }); |
|
114 } |
|
115 |
|
116 function cleanup() { |
|
117 DebuggerServer.destroy(); |
|
118 iframe.remove(); |
|
119 SimpleTest.finish() |
|
120 } |
|
121 |
|
122 firstClient(); |
|
123 } |
|
124 |
|
125 </script> |
|
126 </pre> |
|
127 </body> |
|
128 </html> |