|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <!-- |
|
4 Bug 898485 - [app manager] Implement an abstract connection manager |
|
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> |
|
15 |
|
16 window.onload = function() { |
|
17 SimpleTest.waitForExplicitFinish(); |
|
18 |
|
19 var Cu = Components.utils; |
|
20 |
|
21 Cu.import("resource://gre/modules/devtools/dbg-server.jsm"); |
|
22 Cu.import("resource://gre/modules/devtools/Loader.jsm"); |
|
23 Cu.import("resource://gre/modules/Services.jsm"); |
|
24 |
|
25 DebuggerServer.init(function () { return true; }); |
|
26 DebuggerServer.addBrowserActors(); |
|
27 |
|
28 var {ConnectionManager, Connection} = devtools.require("devtools/client/connection-manager"); |
|
29 |
|
30 var orgCount = ConnectionManager.connections.length; |
|
31 |
|
32 ConnectionManager.once("new", (event, c) => { |
|
33 is(ConnectionManager.connections[orgCount], c, "new event fired, with correct connection"); |
|
34 }); |
|
35 |
|
36 var c1 = ConnectionManager.createConnection(); |
|
37 var c2 = ConnectionManager.createConnection(); |
|
38 |
|
39 is(ConnectionManager.connections[orgCount], c1, "Connection 1 registered"); |
|
40 is(ConnectionManager.connections[orgCount + 1], c2, "Connection 2 registered"); |
|
41 |
|
42 c1.once(Connection.Events.DESTROYED, function() { |
|
43 is(ConnectionManager.connections.length, orgCount + 1, "Connection 1 destroyed"); |
|
44 |
|
45 var c = c2; |
|
46 |
|
47 var eventsRef = "connecting connected disconnecting disconnected host-changed disconnected timeout destroyed"; |
|
48 var events = []; |
|
49 |
|
50 var s = Connection.Status; |
|
51 |
|
52 is(c.status, s.DISCONNECTED, "disconnected"); |
|
53 |
|
54 c.once(Connection.Events.CONNECTING, function(e) { events.push(e); is(c.status, s.CONNECTING, "connecting"); }); |
|
55 c.once(Connection.Events.CONNECTED, function(e) { events.push(e); is(c.status, s.CONNECTED, "connected"); c.disconnect()}); |
|
56 c.once(Connection.Events.DISCONNECTING, function(e) { events.push(e); is(c.status, s.DISCONNECTING, "disconnecting"); }); |
|
57 c.once(Connection.Events.DISCONNECTED, function(e) { events.push(e); is(c.status, s.DISCONNECTED, "disconnected"); testError()}); |
|
58 c.once(Connection.Events.DESTROYED, function(e) { events.push(e); is(c.status, s.DESTROYED, "destroyed"); finish()}); |
|
59 |
|
60 c.connect(); |
|
61 |
|
62 function testStore() { |
|
63 c.store.on("set", function(e,path) { |
|
64 if (path.join(".") == "device.width") { |
|
65 is(c.store.object.device.width, window.screen.width, "Store is fed with valid data"); |
|
66 c.disconnect(); |
|
67 } |
|
68 }); |
|
69 } |
|
70 |
|
71 function testError() { |
|
72 c.once(Connection.Events.DISCONNECTED, function(e) { |
|
73 events.push(e); |
|
74 testKeepConnecting(); |
|
75 }); |
|
76 c.once(Connection.Events.HOST_CHANGED, function(e) { |
|
77 events.push(e); |
|
78 c.connect(); |
|
79 }); |
|
80 c.port = 1; |
|
81 c.host = "localhost"; |
|
82 } |
|
83 |
|
84 function testKeepConnecting() { |
|
85 // ensure that keepConnecting keep trying connecting |
|
86 // until the connection attempts timeout |
|
87 var originalTimeout = Services.prefs.getIntPref("devtools.debugger.remote-timeout"); |
|
88 Services.prefs.setIntPref("devtools.debugger.remote-timeout", 1000); |
|
89 c.once("timeout", function (e) { |
|
90 events.push(e); |
|
91 Services.prefs.setIntPref("devtools.debugger.remote-timeout", originalTimeout); |
|
92 ConnectionManager.destroyConnection(c); |
|
93 }); |
|
94 c.keepConnecting = true; |
|
95 var port = ConnectionManager.getFreeTCPPort(); |
|
96 ok(parseInt(port), "Free TCP port looks like a port number"); |
|
97 c.port = port; |
|
98 c.host = "locahost"; |
|
99 c.connect(); |
|
100 } |
|
101 |
|
102 function finish() { |
|
103 is(events.join(" "), eventsRef, "Events received in the right order"); |
|
104 DebuggerServer.destroy(); |
|
105 SimpleTest.finish(); |
|
106 } |
|
107 |
|
108 }); |
|
109 |
|
110 ConnectionManager.destroyConnection(c1); |
|
111 |
|
112 |
|
113 } |
|
114 </script> |
|
115 </pre> |
|
116 </body> |
|
117 </html> |