Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / remoting / webapp / unittests / it2me_helpee_channel_unittest.js
blob85bee2bbeb79d8fcefc70013ae4d7d4228ef2a5e
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.
5 (function() {
7 'use strict';
9 var hostInstaller = null;
10 var hangoutPort = null;
11 var host = null;
12 var helpeeChannel = null;
13 var onDisposedCallback = null;
15 module('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');
22     // onDisposedCallback callback
23     onDisposedCallback = sinon.spy();
25     // Host
26     host = {
27       initialize: function() {},
28       initialized: function() {},
29       connect: function() {},
30       disconnect: function() {},
31       getAccessCode: function() {},
32       unhookCallbacks: function() {}
33     };
35     // HostInstaller
36     hostInstaller = {
37       download: function() {}
38     };
40     // HelpeeChannel
41     helpeeChannel = new remoting.It2MeHelpeeChannel(
42         hangoutPort,
43         host,
44         hostInstaller,
45         onDisposedCallback);
46     helpeeChannel.init();
48     // remoting.settings
49     remoting.settings = new remoting.Settings();
50     remoting.identity = new remoting.Identity();
51   },
52   tearDown: function() {
53     remoting.settings = null;
54     remoting.identity = null;
55   }
56 });
58 test('hello() should return supportedFeatures', function() {
59   hangoutPort.onMessage.mock$fire(
60       { method: remoting.It2MeHelpeeChannel.HangoutMessageTypes.HELLO });
62   sinon.assert.calledWith(hangoutPort.postMessage, {
63     method: remoting.It2MeHelpeeChannel.HangoutMessageTypes.HELLO_RESPONSE,
64     supportedFeatures: base.values(remoting.It2MeHelperChannel.Features)
65   });
66 });
68 QUnit.asyncTest(
69     'isHostInstalled() should return false if host is not installed',
70     function() {
71   sinon.stub(remoting.HostInstaller, 'isInstalled')
72       .returns(Promise.resolve(false));
74   var MessageTypes = remoting.It2MeHelpeeChannel.HangoutMessageTypes;
75   hangoutPort.onMessage.mock$fire({
76     method: MessageTypes.IS_HOST_INSTALLED
77   });
79   window.requestAnimationFrame(function() {
80     remoting.HostInstaller.isInstalled.restore();
81     sinon.assert.calledWith(hangoutPort.postMessage, {
82       method: MessageTypes.IS_HOST_INSTALLED_RESPONSE,
83       result: false
84     });
85     QUnit.start();
86   });
87 });
89 QUnit.asyncTest('isHostInstalled() should return true if host is installed',
90     function() {
91   sinon.stub(remoting.HostInstaller, 'isInstalled')
92       .returns(Promise.resolve(true));
94   var MessageTypes = remoting.It2MeHelpeeChannel.HangoutMessageTypes;
95   hangoutPort.onMessage.mock$fire({
96     method: MessageTypes.IS_HOST_INSTALLED
97   });
99   window.requestAnimationFrame(function() {
100     remoting.HostInstaller.isInstalled.restore();
101     sinon.assert.calledWith(hangoutPort.postMessage, {
102       method: MessageTypes.IS_HOST_INSTALLED_RESPONSE,
103       result: true
104     });
105     QUnit.start();
106   });
109 test('downloadHost() should trigger a host download',
110     function() {
111   sinon.stub(hostInstaller, 'download').returns(Promise.resolve(true));
113   hangoutPort.onMessage.mock$fire({
114     method: remoting.It2MeHelpeeChannel.HangoutMessageTypes.DOWNLOAD_HOST
115   });
117   sinon.assert.called(hostInstaller.download);
120 QUnit.asyncTest('connect() should return access code',
121     function() {
122   // Stubs authentication.
123   sinon.stub(base, 'isAppsV2').returns(true);
124   sinon.stub(remoting.HangoutConsentDialog, 'getInstance').returns({
125     show : function() {
126       return Promise.resolve();
127     }
128   });
129   sinon.stub(chrome.identity, 'getAuthToken')
130       .callsArgWith(1, 'token');
131   sinon.stub(remoting.identity, 'getToken')
132       .returns(Promise.resolve('token'));
133   sinon.stub(remoting.identity, 'getEmail')
134       .returns(Promise.resolve('test@chromium.org'));
135   // Stubs Host behavior.
136   sinon.stub(host, 'initialized').returns(true);
137   sinon.stub(host, 'connect')
138       .callsArgWith(2, remoting.HostSession.State.RECEIVED_ACCESS_CODE);
139   sinon.stub(host, 'getAccessCode').returns('accessCode');
141   var MessageTypes = remoting.It2MeHelpeeChannel.HangoutMessageTypes;
142   hangoutPort.onMessage.mock$fire({
143     method: MessageTypes.CONNECT,
144     hangoutBounds: {widht: 10, height: 10, left:10, top: 10}
145   });
147   window.requestAnimationFrame(function(){
148     // Verify that access code is correct in the response.
149     sinon.assert.calledWithMatch(hangoutPort.postMessage, {
150         method: MessageTypes.CONNECT_RESPONSE,
151         accessCode: 'accessCode'
152     });
154     chrome.identity.getAuthToken.restore();
155     base.isAppsV2.restore();
156     QUnit.start();
157   });
160 test('should disconnect the session if Hangout crashes', function() {
161   sinon.spy(host, 'disconnect');
162   hangoutPort.onDisconnect.mock$fire();
164   sinon.assert.called(onDisposedCallback);
165   sinon.assert.called(host.disconnect);
168 })();