it2me_helpee_channel_unittest.js revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// Copyright 2014 The Chromium Authors. All rights reserved.
236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// Use of this source code is governed by a BSD-style license that can be
336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// found in the LICENSE file.
436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines(function() {
636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines'use strict';
836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesvar hostInstaller = null;
1036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesvar hangoutPort = null;
1136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesvar host = null;
1236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesvar helpeeChannel = null;
1336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesvar onDisposedCallback = null;
1436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
1536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesmodule('It2MeHelpeeChannel', {
1636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  setup: function() {
1736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // HangoutPort
1836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    hangoutPort = new chromeMocks.runtime.Port();
1936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    hangoutPort.postMessage = sinon.spy(hangoutPort, 'postMessage');
2036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    hangoutPort.disconnect = sinon.spy(hangoutPort, 'disconnect');
2136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
2236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // onDisposedCallback callback
2336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    onDisposedCallback = sinon.spy();
2436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
2536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // Host
2636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    host = {
2736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      initialize: function() {},
2836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      initialized: function() {},
2936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      connect: function() {},
3036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      disconnect: function() {},
3136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      getAccessCode: function() {},
3236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      unhookCallbacks: function() {}
3336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    };
3436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
3536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // HostInstaller
3636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    hostInstaller = {
3736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      isInstalled: function() {},
3836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      download: function() {}
3936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    };
4036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
4136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // HelpeeChannel
4236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    helpeeChannel = new remoting.It2MeHelpeeChannel(
4336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        hangoutPort,
4436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        host,
4536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        hostInstaller,
4636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        onDisposedCallback);
4736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    helpeeChannel.init();
4836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
4936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // remoting.settings
5036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    remoting.settings = new remoting.Settings();
5136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
5236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines});
5336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
5436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinestest('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(chrome.identity, "getAuthToken")
130      .callsArgWith(1, 'token');
131  // Stubs Host behavior.
132  sinon.stub(host, "initialized").returns(true);
133  sinon.stub(host, "connect")
134      .callsArgWith(2, remoting.HostSession.State.RECEIVED_ACCESS_CODE);
135  sinon.stub(host, "getAccessCode").returns('accessCode');
136
137  var MessageTypes = remoting.It2MeHelpeeChannel.HangoutMessageTypes;
138  hangoutPort.onMessage.mock$fire({
139    method: MessageTypes.CONNECT,
140    email: 'test@chromium.org'
141  });
142
143  window.requestAnimationFrame(function(){
144    // Verify that access code is correct in the response.
145    sinon.assert.calledWithMatch(hangoutPort.postMessage, {
146        method: MessageTypes.CONNECT_RESPONSE,
147        accessCode: 'accessCode'
148    });
149
150    chrome.identity.getAuthToken.restore();
151    base.isAppsV2.restore();
152    QUnit.start();
153  });
154});
155
156test('should disconnect the session if Hangout crashes', function() {
157  sinon.spy(host, "disconnect");
158  hangoutPort.onDisconnect.mock$fire();
159
160  sinon.assert.called(onDisposedCallback);
161  sinon.assert.called(host.disconnect);
162});
163
164})();
165