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 it2meService = null;
14 function createPort(name, senderId) {
15 var port = new chromeMocks.runtime.Port();
16 port.name = (senderId) ? name +'@' + senderId : name;
17 port.postMessage = sinon.spy(port, 'postMessage');
18 port.disconnect = sinon.spy(port, 'disconnect');
23 function promiseResolveSynchronous(value) {
25 then: function(callback) {
31 module('It2MeService', {
36 return promiseResolveSynchronous('tabId');
41 hangoutPort = createPort('it2me.helper.hangout');
42 it2meService = new remoting.It2MeService(appLauncher);
43 it2meService.onConnectExternal_(hangoutPort);
44 webappPort = createPort('it2me.helper.webapp', 'tabId');
48 test('should establish a channel two way channel when the webapp connects',
50 // Hangout ---- connect ----> It2MeService.
51 hangoutPort.onMessage.mock$fire({
53 accessCode: "123412341234"
56 // Webapp ---- connect ----> It2MeService.
57 it2meService.onWebappConnect_(webappPort);
59 // Webapp ---- sessionStateChanged ----> It2MeService.
60 webappPort.onMessage.mock$fire({
61 method: 'sessionStateChanged',
62 state: remoting.ClientSession.State.CONNECTED
65 // verify that hangout can receive message events.
66 sinon.assert.calledWith(hangoutPort.postMessage, {
67 method: 'sessionStateChanged',
68 state: remoting.ClientSession.State.CONNECTED
71 hangoutPort.onDisconnect.mock$fire();
72 QUnit.equal(it2meService.helpers_.length, 0);
75 test('should handle multiple helper connections', function() {
76 // Hangout ---- connect ----> It2MeService.
77 hangoutPort.onMessage.mock$fire({
79 accessCode: "123412341234"
82 // Hangout2 ---- connect ----> It2MeService.
83 var hangoutPort2 = createPort('it2me.helper.hangout');
84 it2meService.onConnectExternal_(hangoutPort2);
86 appLauncher.launch = function () {
87 return promiseResolveSynchronous('tabId2');
90 hangoutPort2.onMessage.mock$fire({
92 accessCode: "123412341234"
95 it2meService.onWebappConnect_(webappPort);
97 var webappPort2 = createPort('it2me.helper.webapp', 'tabId2');
98 it2meService.onWebappConnect_(webappPort2);
100 webappPort.onMessage.mock$fire({
101 method: 'sessionStateChanged',
102 state: remoting.ClientSession.State.CONNECTED
105 // verify that hangout can receive message events from webapp 1
106 sinon.assert.calledWith(hangoutPort.postMessage, {
107 method: 'sessionStateChanged',
108 state: remoting.ClientSession.State.CONNECTED
111 webappPort2.onMessage.mock$fire({
112 method: 'sessionStateChanged',
113 state: remoting.ClientSession.State.CLOSED
116 // verify that hangout can receive message events from webapp 2.
117 sinon.assert.calledWith(hangoutPort2.postMessage, {
118 method: 'sessionStateChanged',
119 state: remoting.ClientSession.State.CLOSED
123 test('should reject unknown connection', function() {
124 it2meService.onWebappConnect_(webappPort);
125 sinon.assert.called(webappPort.disconnect);
127 var randomPort = createPort('unsupported.port.name');
128 it2meService.onConnectExternal_(randomPort);
129 sinon.assert.called(randomPort.disconnect);