1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 var appLauncher = null;
10 var hangoutPort = null;
11 var webappPort = null;
12 var helperChannel = null;
13 var disconnectCallback = null;
15 module('It2MeHelperChannel', {
20 return promiseResolveSynchronous('tabId');
24 appLauncher.launch = sinon.spy(appLauncher, 'launch');
25 appLauncher.close = sinon.spy(appLauncher, 'close');
28 hangoutPort = new chromeMocks.runtime.Port();
29 hangoutPort.postMessage = sinon.spy(hangoutPort, 'postMessage');
30 hangoutPort.disconnect = sinon.spy(hangoutPort, 'disconnect');
33 webappPort = new chromeMocks.runtime.Port();
39 webappPort.postMessage = sinon.spy(webappPort, 'postMessage');
40 webappPort.disconnect = sinon.spy(webappPort, 'disconnect');
42 // disconnect callback
43 disconnectCallback = sinon.spy();
46 helperChannel = new remoting.It2MeHelperChannel(
47 appLauncher, hangoutPort, disconnectCallback);
49 hangoutPort.onMessage.mock$fire({
50 method: remoting.It2MeHelperChannel.HangoutMessageTypes.CONNECT,
51 accessCode: "123412341234"
56 function promiseResolveSynchronous(value) {
58 then: function(callback) {
64 test('onHangoutMessage_("hello") should return supportedFeatures', function() {
65 hangoutPort.onMessage.mock$fire(
66 { method: remoting.It2MeHelperChannel.HangoutMessageTypes.HELLO });
68 sinon.assert.calledWith(hangoutPort.postMessage, {
69 method: remoting.It2MeHelperChannel.HangoutMessageTypes.HELLO_RESPONSE,
70 supportedFeatures: base.values(remoting.It2MeHelperChannel.Features)
74 test('onHangoutMessage_(|connect|) should launch the webapp',
76 sinon.assert.called(appLauncher.launch);
77 QUnit.equal(helperChannel.instanceId(), 'tabId');
80 test('onWebappMessage() should forward messages to hangout', function() {
82 helperChannel.onWebappConnect(webappPort);
83 webappPort.onMessage.mock$fire({
84 method:'sessionStateChanged',
85 state:remoting.ClientSession.State.CONNECTING
87 webappPort.onMessage.mock$fire({
88 method:'sessionStateChanged',
89 state:remoting.ClientSession.State.CONNECTED
92 // Verify events are forwarded.
93 sinon.assert.calledWith(hangoutPort.postMessage, {
94 method:'sessionStateChanged',
95 state:remoting.ClientSession.State.CONNECTING
98 sinon.assert.calledWith(hangoutPort.postMessage, {
99 method:'sessionStateChanged',
100 state:remoting.ClientSession.State.CONNECTED
104 test('should notify hangout when the webapp crashes', function() {
106 helperChannel.onWebappConnect(webappPort);
107 webappPort.onDisconnect.mock$fire();
109 // Verify events are forwarded.
110 sinon.assert.calledWith(hangoutPort.postMessage, {
111 method:'sessionStateChanged',
112 state: remoting.ClientSession.State.FAILED
114 sinon.assert.called(hangoutPort.disconnect);
115 sinon.assert.calledOnce(disconnectCallback);
118 test('should notify hangout when the session is ended', function() {
120 helperChannel.onWebappConnect(webappPort);
121 webappPort.onMessage.mock$fire({
122 method:'sessionStateChanged',
123 state:remoting.ClientSession.State.CLOSED
126 webappPort.onDisconnect.mock$fire();
128 // Verify events are forwarded.
129 sinon.assert.calledWith(hangoutPort.postMessage, {
130 method:'sessionStateChanged',
131 state:remoting.ClientSession.State.CLOSED
133 sinon.assert.called(hangoutPort.disconnect);
134 sinon.assert.calledOnce(disconnectCallback);
137 test('should notify hangout when the session has error', function() {
138 helperChannel.onWebappConnect(webappPort);
139 webappPort.onMessage.mock$fire({
140 method:'sessionStateChanged',
141 state:remoting.ClientSession.State.FAILED
144 webappPort.onDisconnect.mock$fire();
146 // Verify events are forwarded.
147 sinon.assert.calledWith(hangoutPort.postMessage, {
148 method:'sessionStateChanged',
149 state:remoting.ClientSession.State.FAILED
151 sinon.assert.called(hangoutPort.disconnect);
152 sinon.assert.calledOnce(disconnectCallback);
156 test('onHangoutMessages_(disconnect) should close the webapp', function() {
158 helperChannel.onWebappConnect(webappPort);
159 hangoutPort.onMessage.mock$fire({
160 method: remoting.It2MeHelperChannel.HangoutMessageTypes.DISCONNECT
163 sinon.assert.calledOnce(appLauncher.close);
165 // Webapp will respond by disconnecting the port
166 webappPort.onDisconnect.mock$fire();
168 // Verify events are forwarded.
169 sinon.assert.calledWith(hangoutPort.postMessage, {
170 method:'sessionStateChanged',
171 state:remoting.ClientSession.State.CLOSED
173 sinon.assert.called(webappPort.disconnect);
174 sinon.assert.called(hangoutPort.disconnect);
177 test('should close the webapp when hangout crashes', function() {
179 helperChannel.onWebappConnect(webappPort);
180 hangoutPort.onDisconnect.mock$fire();
182 sinon.assert.calledOnce(appLauncher.close);
183 sinon.assert.calledOnce(disconnectCallback);
185 sinon.assert.called(hangoutPort.disconnect);
186 sinon.assert.called(webappPort.disconnect);