Chromium Code Reviews| Index: remoting/webapp/unittests/it2me_helper_channel_unittest.js |
| diff --git a/remoting/webapp/unittests/it2me_helper_channel_unittest.js b/remoting/webapp/unittests/it2me_helper_channel_unittest.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b5511c33058c9c9a0ef7d3ceb1aea7589e0561d1 |
| --- /dev/null |
| +++ b/remoting/webapp/unittests/it2me_helper_channel_unittest.js |
| @@ -0,0 +1,179 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +(function() { |
| + |
| +'use strict'; |
| + |
| +var appLauncher = null; |
| +var hangoutPort = null; |
| +var webappPort = null; |
| +var helperChannel = null; |
| +var disconnectCallback = null; |
| + |
| +module('It2MeHelperChannel', { |
| + setup: function() { |
| + // App Launcher. |
| + appLauncher = { |
| + launch: function () { |
| + return promiseResolveSynchronous('tabId'); |
|
Jamie
2014/08/14 00:25:06
Why does this need to be synchronous? Don't we run
Jamie
2014/08/14 00:25:06
'tabId' is used twice; if it's important that the
|
| + }, |
| + close: function () {} |
| + }; |
| + appLauncher.launch = sinon.spy(appLauncher, 'launch'); |
| + appLauncher.close = sinon.spy(appLauncher, 'close'); |
| + |
| + // HangoutPort. |
| + hangoutPort = new chromeMocks.runtime.Port(); |
| + hangoutPort.postMessage = sinon.spy(hangoutPort, 'postMessage'); |
| + hangoutPort.disconnect = sinon.spy(hangoutPort, 'disconnect'); |
| + |
| + // WebappPort. |
| + webappPort = new chromeMocks.runtime.Port(); |
| + webappPort.sender = { |
| + tab : { |
| + id : 'tabId' |
| + } |
| + }; |
| + webappPort.postMessage = sinon.spy(webappPort, 'postMessage'); |
| + webappPort.disconnect = sinon.spy(webappPort, 'disconnect'); |
| + |
| + // disconnect callback |
| + disconnectCallback = sinon.spy(); |
| + |
| + // HelperChannel. |
| + helperChannel = new remoting.It2MeHelperChannel( |
| + appLauncher, hangoutPort, disconnectCallback); |
| + helperChannel.init(); |
| + hangoutPort.onMessage.mock$fire({ |
| + method: remoting.It2MeHelperChannel.HangoutMessageTypes.CONNECT, |
| + accessCode: "123412341234" |
| + }); |
| + }, |
| +}); |
| + |
| +function promiseResolveSynchronous(value) { |
| + return { |
| + then: function(callback) { |
| + callback('tabId'); |
| + } |
| + }; |
| +} |
| + |
| +test('onHangoutMessage_(|connect|) should launch the webapp', |
| + function() { |
| + sinon.assert.called(appLauncher.launch); |
| + QUnit.equal(helperChannel.instanceId(), 'tabId'); |
| +}); |
| + |
| +test('onWebappMessage() should forward messages to hangout', function() { |
| + // Execute. |
| + helperChannel.onWebappConnect(webappPort); |
| + webappPort.onMessage.mock$fire({ |
| + method:'sessionStateChanged', |
| + state:remoting.ClientSession.State.CONNECTING |
| + }); |
| + webappPort.onMessage.mock$fire({ |
| + method:'sessionStateChanged', |
| + state:remoting.ClientSession.State.CONNECTED |
| + }); |
| + |
| + // Verify events are forwarded. |
| + sinon.assert.calledWith(hangoutPort.postMessage, { |
| + method:'sessionStateChanged', |
| + state:remoting.ClientSession.State.CONNECTING |
| + }); |
| + |
| + sinon.assert.calledWith(hangoutPort.postMessage, { |
| + method:'sessionStateChanged', |
| + state:remoting.ClientSession.State.CONNECTED |
| + }); |
| +}); |
| + |
| +test('should notify hangout when the webapp crashes', function() { |
| + // Execute. |
| + helperChannel.onWebappConnect(webappPort); |
| + webappPort.onDisconnect.mock$fire(); |
| + |
| + // Verify events are forwarded. |
| + sinon.assert.calledWith(hangoutPort.postMessage, { |
| + method:'sessionStateChanged', |
| + state: remoting.ClientSession.State.FAILED |
| + }); |
| + sinon.assert.called(hangoutPort.disconnect); |
| + sinon.assert.calledOnce(disconnectCallback); |
| +}); |
| + |
| +test('should notify hangout when the session is ended', function() { |
| + // Execute. |
| + helperChannel.onWebappConnect(webappPort); |
| + webappPort.onMessage.mock$fire({ |
| + method:'sessionStateChanged', |
| + state:remoting.ClientSession.State.CLOSED |
| + }); |
| + |
| + webappPort.onDisconnect.mock$fire(); |
| + |
| + // Verify events are forwarded. |
| + sinon.assert.calledWith(hangoutPort.postMessage, { |
| + method:'sessionStateChanged', |
| + state:remoting.ClientSession.State.CLOSED |
| + }); |
|
Jamie
2014/08/14 00:25:06
Can you also assert that it's not called with stat
|
| + sinon.assert.called(hangoutPort.disconnect); |
| + sinon.assert.calledOnce(disconnectCallback); |
| +}); |
| + |
| +test('should notify hangout when the session has error', function() { |
| + helperChannel.onWebappConnect(webappPort); |
| + webappPort.onMessage.mock$fire({ |
| + method:'sessionStateChanged', |
| + state:remoting.ClientSession.State.FAILED |
| + }); |
| + |
| + webappPort.onDisconnect.mock$fire(); |
| + |
| + // Verify events are forwarded. |
| + sinon.assert.calledWith(hangoutPort.postMessage, { |
| + method:'sessionStateChanged', |
| + state:remoting.ClientSession.State.FAILED |
| + }); |
| + sinon.assert.called(hangoutPort.disconnect); |
| + sinon.assert.calledOnce(disconnectCallback); |
| +}); |
| + |
| + |
| +test('onHangoutMessages_(disconnect) should close the webapp', function() { |
| + // Execute. |
| + helperChannel.onWebappConnect(webappPort); |
| + hangoutPort.onMessage.mock$fire({ |
| + method: remoting.It2MeHelperChannel.HangoutMessageTypes.DISCONNECT |
| + }); |
| + |
| + sinon.assert.calledOnce(appLauncher.close); |
| + |
| + // Webapp will respond by disconnecting the port |
| + webappPort.onDisconnect.mock$fire(); |
| + |
| + // Verify events are forwarded. |
| + sinon.assert.calledWith(hangoutPort.postMessage, { |
| + method:'sessionStateChanged', |
| + state:remoting.ClientSession.State.CLOSED |
| + }); |
| + sinon.assert.called(webappPort.disconnect); |
| + sinon.assert.called(hangoutPort.disconnect); |
| +}); |
| + |
| +test('should close the webapp when hangout crashes', function() { |
| + // Execute. |
| + helperChannel.onWebappConnect(webappPort); |
| + hangoutPort.onDisconnect.mock$fire(); |
| + |
| + sinon.assert.calledOnce(appLauncher.close); |
| + sinon.assert.calledOnce(disconnectCallback); |
| + |
| + sinon.assert.called(hangoutPort.disconnect); |
| + sinon.assert.called(webappPort.disconnect); |
| +}); |
| + |
| +})(); |