From c243bc0afd44fecedd8773d45ea73e2d8fd79890 Mon Sep 17 00:00:00 2001 From: Junior Hsu Date: Wed, 27 May 2015 02:19:00 +0200 Subject: [PATCH] Bug 1166599 - make |_devices| init appropriately. r=fabrice Also remove the redundant |TCPPresentationServer.close| --HG-- extra : rebase_source : b4b44213bb105dea75b6cf7a0371bfe7f2c5efd5 --- .../provider/TCPPresentationServer.js | 15 ++-- .../xpcshell/test_tcp_control_channel.js | 85 +++++++++++-------- 2 files changed, 55 insertions(+), 45 deletions(-) diff --git a/dom/presentation/provider/TCPPresentationServer.js b/dom/presentation/provider/TCPPresentationServer.js index 063cf0aec844..66ce63b7b2fb 100644 --- a/dom/presentation/provider/TCPPresentationServer.js +++ b/dom/presentation/provider/TCPPresentationServer.js @@ -26,7 +26,7 @@ function TCPPresentationServer() { this._id = null; this._port = 0; this._serverSocket = null; - this._devices = new Map(); // id -> device + this._devices = null; } TCPPresentationServer.prototype = { @@ -78,6 +78,7 @@ TCPPresentationServer.prototype = { */ this.id = aId; this._port = this._serverSocket.port; + this._devices = new Map(); // id -> device }, get id() { @@ -113,16 +114,8 @@ TCPPresentationServer.prototype = { return this._id !== null && this._serverSocket !== null; }, - close: function() { - DEBUG && log("TCPPresentationServer - close"); - if (this._serverSocket) { - this._serverSocket.close(); - } - this._id = null; - this._port = 0; - }, - createTCPDevice: function(aId, aName, aType, aHost, aPort) { + DEBUG && log("TCPPresentationServer - createTCPDevice with id: " + aId); if (this._devices.has(aId)) { throw Cr.NS_ERROR_INVALID_ARG; } @@ -136,6 +129,7 @@ TCPPresentationServer.prototype = { }, getTCPDevice: function(aId) { + DEBUG && log("TCPPresentationServer - getTCPDevice with id: " + aId); if (!this._devices.has(aId)) { throw Cr.NS_ERROR_INVALID_ARG; } @@ -143,6 +137,7 @@ TCPPresentationServer.prototype = { }, removeTCPDevice: function(aId) { + DEBUG && log("TCPPresentationServer - removeTCPDevice with id: " + aId); if (!this._devices.has(aId)) { throw Cr.NS_ERROR_INVALID_ARG; } diff --git a/dom/presentation/tests/xpcshell/test_tcp_control_channel.js b/dom/presentation/tests/xpcshell/test_tcp_control_channel.js index 617752e5e21e..d3435c4d7698 100644 --- a/dom/presentation/tests/xpcshell/test_tcp_control_channel.js +++ b/dom/presentation/tests/xpcshell/test_tcp_control_channel.js @@ -41,27 +41,34 @@ TestDescription.prototype = { QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationChannelDescription]), } +const CONTROLLER_CONTROL_CHANNEL_PORT = 36777; +const PRESENTER_CONTROL_CHANNEL_PORT = 36888; + +// presenter's presentation channel description +const OFFER_ADDRESS = '192.168.123.123'; +const OFFER_PORT = 123; + +// controller's presentation channel description +const ANSWER_ADDRESS = '192.168.321.321'; +const ANSWER_PORT = 321; + function loopOfferAnser() { - const CONTROLLER_CONTROL_CHANNEL_PORT = 36777; - const PRESENTER_CONTROL_CHANNEL_PORT = 36888; + tps = Cc["@mozilla.org/presentation-device/tcp-presentation-server;1"] + .createInstance(Ci.nsITCPPresentationServer); + tps.init(null, PRESENTER_CONTROL_CHANNEL_PORT); + tps.id = 'controllerID'; - // presenter's presentation channel description - const OFFER_ADDRESS = '192.168.123.123'; - const OFFER_PORT = 123; + testPresentationServer(); +} - // controller's presentation channel description - const ANSWER_ADDRESS = '192.168.321.321'; - const ANSWER_PORT = 321; +function testPresentationServer() { let yayFuncs = makeJointSuccess(['controllerControlChannelClose', 'presenterControlChannelClose']); let controllerDevice, controllerControlChannel; let presenterDevice, presenterControlChannel; - tps = Cc["@mozilla.org/presentation-device/tcp-presentation-server;1"] - .createInstance(Ci.nsITCPPresentationServer); - tps.init(null, PRESENTER_CONTROL_CHANNEL_PORT); - tps.id = 'controllerID'; + controllerDevice = tps.createTCPDevice('controllerID', 'controllerName', 'testType', @@ -168,34 +175,40 @@ function loopOfferAnser() { }; } -function shutdownAndOneMoreLoop() { - const PRESENTER_CONTROL_CHANNEL_PORT = 36888; - - let isFirstClose = true; +function setOffline() { let expectedReason; tps.listener = { onClose: function(aReason) { - expectedReason = isFirstClose ? Cr.NS_ERROR_ABORT // non-manually closed - : Cr.NS_OK; // manually closed - Assert.equal(aReason, expectedReason, 'TCPPresentationServer close as expected'); - if (isFirstClose){ - isFirstClose = false; - try { - tps.init('controllerID', PRESENTER_CONTROL_CHANNEL_PORT); - tps.close(); - } catch (e) { - Assert.ok(false, 'TCP presentation init fail:' + e); - run_next_test(); - } - } else { - run_next_test(); - } + Assert.equal(aReason, Cr.NS_ERROR_ABORT, 'TCPPresentationServer close as expected'); + Services.io.offline = false; + run_next_test(); }, } - // A fake event to pretend the server socket is closed non-manually - tps.QueryInterface(Ci.nsIServerSocketListener) - .onStopListening(null, Cr.NS_ERROR_ABORT); + // Let the server socket be closed non-manually + Services.io.offline = true; +} + +function oneMoreLoop() { + try { + tps.init('controllerID', PRESENTER_CONTROL_CHANNEL_PORT); + testPresentationServer(); + } catch (e) { + Assert.ok(false, 'TCP presentation init fail:' + e); + run_next_test(); + } +} + + +function shutdown() +{ + tps.listener = { + onClose: function(aReason) { + Assert.equal(aReason, Cr.NS_OK, 'TCPPresentationServer close success'); + run_next_test(); + }, + } + tps.close(); } function setPref() { @@ -210,7 +223,9 @@ function clearPref() { add_test(setPref); add_test(loopOfferAnser); -add_test(shutdownAndOneMoreLoop); +add_test(setOffline); +add_test(oneMoreLoop); +add_test(shutdown); add_test(clearPref); function run_test() {