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.
4
5(function() {
6
7'use strict';
8
9var hostInstaller = null;
10var hangoutPort = null;
11var host = null;
12var helpeeChannel = null;
13var onDisposedCallback = null;
14
15module('It2MeHelpeeChannel', {
16  setup: function() {
17    // HangoutPort
18    hangoutPort = new chromeMocks.runtime.Port();
19    hangoutPort.postMessage = sinon.spy(hangoutPort, 'postMessage');
20    hangoutPort.disconnect = sinon.spy(hangoutPort, 'disconnect');
21
22    // onDisposedCallback callback
23    onDisposedCallback = sinon.spy();
24
25    // Host
26    host = {
27      initialize: function() {},
28      initialized: function() {},
29      connect: function() {},
30      disconnect: function() {},
31      getAccessCode: function() {},
32      unhookCallbacks: function() {}
33    };
34
35    // HostInstaller
36    hostInstaller = {
37      isInstalled: function() {},
38      download: function() {}
39    };
40
41    // HelpeeChannel
42    helpeeChannel = new remoting.It2MeHelpeeChannel(
43        hangoutPort,
44        host,
45        hostInstaller,
46        onDisposedCallback);
47    helpeeChannel.init();
48
49    // remoting.settings
50    remoting.settings = new remoting.Settings();
51  }
52});
53
54test('hello() should return supportedFeatures', function() {
55  hangoutPort.onMessage.mock$fire(
56      { method: remoting.It2MeHelpeeChannel.HangoutMessageTypes.HELLO });
57
58  sinon.assert.calledWith(hangoutPort.postMessage, {
59    method: remoting.It2MeHelpeeChannel.HangoutMessageTypes.HELLO_RESPONSE,
60    supportedFeatures: base.values(remoting.It2MeHelperChannel.Features)
61  });
62});
63
64QUnit.asyncTest(
65    'isHostInstalled() should return false if host is not installed',
66    function() {
67  sinon.stub(hostInstaller, 'isInstalled').returns(Promise.resolve(false));
68
69  var MessageTypes = remoting.It2MeHelpeeChannel.HangoutMessageTypes;
70  hangoutPort.onMessage.mock$fire({
71    method: MessageTypes.IS_HOST_INSTALLED
72  });
73
74  window.requestAnimationFrame(function() {
75    sinon.assert.calledWith(hangoutPort.postMessage, {
76      method: MessageTypes.IS_HOST_INSTALLED_RESPONSE,
77      result: false
78    });
79    QUnit.start();
80  });
81});
82
83QUnit.asyncTest('isHostInstalled() should return true if host is installed',
84    function() {
85  sinon.stub(hostInstaller, 'isInstalled').returns(Promise.resolve(true));
86
87  var MessageTypes = remoting.It2MeHelpeeChannel.HangoutMessageTypes;
88  hangoutPort.onMessage.mock$fire({
89    method: MessageTypes.IS_HOST_INSTALLED
90  });
91
92  window.requestAnimationFrame(function() {
93    sinon.assert.calledWith(hangoutPort.postMessage, {
94      method: MessageTypes.IS_HOST_INSTALLED_RESPONSE,
95      result: true
96    });
97    QUnit.start();
98  });
99});
100
101test('downloadHost() should trigger a host download',
102    function() {
103  sinon.stub(hostInstaller, 'download').returns(Promise.resolve(true));
104
105  hangoutPort.onMessage.mock$fire({
106    method: remoting.It2MeHelpeeChannel.HangoutMessageTypes.DOWNLOAD_HOST
107  });
108
109  sinon.assert.called(hostInstaller.download);
110});
111
112test('connect() should return error if email is missing',
113    function() {
114  var MessageTypes = remoting.It2MeHelpeeChannel.HangoutMessageTypes;
115
116  hangoutPort.onMessage.mock$fire({
117    method: MessageTypes.CONNECT
118  });
119
120  sinon.assert.calledWithMatch(hangoutPort.postMessage, {
121      method: MessageTypes.ERROR
122  });
123});
124
125QUnit.asyncTest('connect() should return access code',
126    function() {
127  // Stubs authentication.
128  sinon.stub(base, 'isAppsV2').returns(true);
129  sinon.stub(remoting.MessageWindow, 'showConfirmWindow')
130      .callsArgWith(4, 1 /* 1 for OK. */);
131  sinon.stub(chrome.identity, 'getAuthToken')
132      .callsArgWith(1, 'token');
133  // Stubs Host behavior.
134  sinon.stub(host, 'initialized').returns(true);
135  sinon.stub(host, 'connect')
136      .callsArgWith(2, remoting.HostSession.State.RECEIVED_ACCESS_CODE);
137  sinon.stub(host, 'getAccessCode').returns('accessCode');
138
139  var MessageTypes = remoting.It2MeHelpeeChannel.HangoutMessageTypes;
140  hangoutPort.onMessage.mock$fire({
141    method: MessageTypes.CONNECT,
142    email: 'test@chromium.org'
143  });
144
145  window.requestAnimationFrame(function(){
146    // Verify that access code is correct in the response.
147    sinon.assert.calledWithMatch(hangoutPort.postMessage, {
148        method: MessageTypes.CONNECT_RESPONSE,
149        accessCode: 'accessCode'
150    });
151
152    chrome.identity.getAuthToken.restore();
153    base.isAppsV2.restore();
154    remoting.MessageWindow.showConfirmWindow.restore();
155    QUnit.start();
156  });
157});
158
159test('should disconnect the session if Hangout crashes', function() {
160  sinon.spy(host, 'disconnect');
161  hangoutPort.onDisconnect.mock$fire();
162
163  sinon.assert.called(onDisposedCallback);
164  sinon.assert.called(host.disconnect);
165});
166
167})();
168