1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/devtools/server/tests/mochitest/test_connection-manager.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,117 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<!-- 1.7 +Bug 898485 - [app manager] Implement an abstract connection manager 1.8 +--> 1.9 +<head> 1.10 + <meta charset="utf-8"> 1.11 + <title>Mozilla Bug</title> 1.12 + <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 1.13 + <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"> 1.14 +</head> 1.15 +<body> 1.16 +<pre id="test"> 1.17 +<script> 1.18 + 1.19 +window.onload = function() { 1.20 + SimpleTest.waitForExplicitFinish(); 1.21 + 1.22 + var Cu = Components.utils; 1.23 + 1.24 + Cu.import("resource://gre/modules/devtools/dbg-server.jsm"); 1.25 + Cu.import("resource://gre/modules/devtools/Loader.jsm"); 1.26 + Cu.import("resource://gre/modules/Services.jsm"); 1.27 + 1.28 + DebuggerServer.init(function () { return true; }); 1.29 + DebuggerServer.addBrowserActors(); 1.30 + 1.31 + var {ConnectionManager, Connection} = devtools.require("devtools/client/connection-manager"); 1.32 + 1.33 + var orgCount = ConnectionManager.connections.length; 1.34 + 1.35 + ConnectionManager.once("new", (event, c) => { 1.36 + is(ConnectionManager.connections[orgCount], c, "new event fired, with correct connection"); 1.37 + }); 1.38 + 1.39 + var c1 = ConnectionManager.createConnection(); 1.40 + var c2 = ConnectionManager.createConnection(); 1.41 + 1.42 + is(ConnectionManager.connections[orgCount], c1, "Connection 1 registered"); 1.43 + is(ConnectionManager.connections[orgCount + 1], c2, "Connection 2 registered"); 1.44 + 1.45 + c1.once(Connection.Events.DESTROYED, function() { 1.46 + is(ConnectionManager.connections.length, orgCount + 1, "Connection 1 destroyed"); 1.47 + 1.48 + var c = c2; 1.49 + 1.50 + var eventsRef = "connecting connected disconnecting disconnected host-changed disconnected timeout destroyed"; 1.51 + var events = []; 1.52 + 1.53 + var s = Connection.Status; 1.54 + 1.55 + is(c.status, s.DISCONNECTED, "disconnected"); 1.56 + 1.57 + c.once(Connection.Events.CONNECTING, function(e) { events.push(e); is(c.status, s.CONNECTING, "connecting"); }); 1.58 + c.once(Connection.Events.CONNECTED, function(e) { events.push(e); is(c.status, s.CONNECTED, "connected"); c.disconnect()}); 1.59 + c.once(Connection.Events.DISCONNECTING, function(e) { events.push(e); is(c.status, s.DISCONNECTING, "disconnecting"); }); 1.60 + c.once(Connection.Events.DISCONNECTED, function(e) { events.push(e); is(c.status, s.DISCONNECTED, "disconnected"); testError()}); 1.61 + c.once(Connection.Events.DESTROYED, function(e) { events.push(e); is(c.status, s.DESTROYED, "destroyed"); finish()}); 1.62 + 1.63 + c.connect(); 1.64 + 1.65 + function testStore() { 1.66 + c.store.on("set", function(e,path) { 1.67 + if (path.join(".") == "device.width") { 1.68 + is(c.store.object.device.width, window.screen.width, "Store is fed with valid data"); 1.69 + c.disconnect(); 1.70 + } 1.71 + }); 1.72 + } 1.73 + 1.74 + function testError() { 1.75 + c.once(Connection.Events.DISCONNECTED, function(e) { 1.76 + events.push(e); 1.77 + testKeepConnecting(); 1.78 + }); 1.79 + c.once(Connection.Events.HOST_CHANGED, function(e) { 1.80 + events.push(e); 1.81 + c.connect(); 1.82 + }); 1.83 + c.port = 1; 1.84 + c.host = "localhost"; 1.85 + } 1.86 + 1.87 + function testKeepConnecting() { 1.88 + // ensure that keepConnecting keep trying connecting 1.89 + // until the connection attempts timeout 1.90 + var originalTimeout = Services.prefs.getIntPref("devtools.debugger.remote-timeout"); 1.91 + Services.prefs.setIntPref("devtools.debugger.remote-timeout", 1000); 1.92 + c.once("timeout", function (e) { 1.93 + events.push(e); 1.94 + Services.prefs.setIntPref("devtools.debugger.remote-timeout", originalTimeout); 1.95 + ConnectionManager.destroyConnection(c); 1.96 + }); 1.97 + c.keepConnecting = true; 1.98 + var port = ConnectionManager.getFreeTCPPort(); 1.99 + ok(parseInt(port), "Free TCP port looks like a port number"); 1.100 + c.port = port; 1.101 + c.host = "locahost"; 1.102 + c.connect(); 1.103 + } 1.104 + 1.105 + function finish() { 1.106 + is(events.join(" "), eventsRef, "Events received in the right order"); 1.107 + DebuggerServer.destroy(); 1.108 + SimpleTest.finish(); 1.109 + } 1.110 + 1.111 + }); 1.112 + 1.113 + ConnectionManager.destroyConnection(c1); 1.114 + 1.115 + 1.116 +} 1.117 +</script> 1.118 +</pre> 1.119 +</body> 1.120 +</html>