| OLD | NEW |
| (Empty) |
| 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 /** | |
| 6 * @fileoverview | |
| 7 * @suppress {checkTypes|checkVars|reportUnknownTypes|visibility} | |
| 8 */ | |
| 9 | |
| 10 (function() { | |
| 11 | |
| 12 'use strict'; | |
| 13 | |
| 14 var appLauncher = null; | |
| 15 var hangoutPort = null; | |
| 16 var webappPort = null; | |
| 17 var it2meService = null; | |
| 18 | |
| 19 function createPort(name, senderId) { | |
| 20 var port = new chromeMocks.runtime.Port(); | |
| 21 port.name = (senderId) ? name +'@' + senderId : name; | |
| 22 port.postMessage = sinon.spy(port, 'postMessage'); | |
| 23 port.disconnect = sinon.spy(port, 'disconnect'); | |
| 24 | |
| 25 return port; | |
| 26 } | |
| 27 | |
| 28 function promiseResolveSynchronous(value) { | |
| 29 return { | |
| 30 then: function(callback) { | |
| 31 callback(value); | |
| 32 } | |
| 33 }; | |
| 34 } | |
| 35 | |
| 36 module('It2MeService', { | |
| 37 setup: function() { | |
| 38 // App Launcher. | |
| 39 appLauncher = { | |
| 40 launch: function () { | |
| 41 return promiseResolveSynchronous('tabId'); | |
| 42 }, | |
| 43 close: function () {} | |
| 44 }; | |
| 45 // HangoutPort. | |
| 46 hangoutPort = createPort('it2me.helper.hangout'); | |
| 47 it2meService = new remoting.It2MeService(appLauncher); | |
| 48 it2meService.onConnectExternal_(hangoutPort); | |
| 49 webappPort = createPort('it2me.helper.webapp', 'tabId'); | |
| 50 } | |
| 51 }); | |
| 52 | |
| 53 test('should establish a channel two way channel when the webapp connects', | |
| 54 function() { | |
| 55 // Hangout ---- connect ----> It2MeService. | |
| 56 hangoutPort.onMessage.mock$fire({ | |
| 57 method: 'connect', | |
| 58 accessCode: "123412341234" | |
| 59 }); | |
| 60 | |
| 61 // Webapp ---- connect ----> It2MeService. | |
| 62 it2meService.onWebappConnect_(webappPort); | |
| 63 | |
| 64 // Webapp ---- sessionStateChanged ----> It2MeService. | |
| 65 webappPort.onMessage.mock$fire({ | |
| 66 method: 'sessionStateChanged', | |
| 67 state: remoting.ClientSession.State.CONNECTED | |
| 68 }); | |
| 69 | |
| 70 // verify that hangout can receive message events. | |
| 71 sinon.assert.calledWith(hangoutPort.postMessage, { | |
| 72 method: 'sessionStateChanged', | |
| 73 state: remoting.ClientSession.State.CONNECTED | |
| 74 }); | |
| 75 | |
| 76 hangoutPort.onDisconnect.mock$fire(); | |
| 77 QUnit.equal(it2meService.helpers_.length, 0); | |
| 78 }); | |
| 79 | |
| 80 test('should handle multiple helper connections', function() { | |
| 81 // Hangout ---- connect ----> It2MeService. | |
| 82 hangoutPort.onMessage.mock$fire({ | |
| 83 method: 'connect', | |
| 84 accessCode: "123412341234" | |
| 85 }); | |
| 86 | |
| 87 // Hangout2 ---- connect ----> It2MeService. | |
| 88 var hangoutPort2 = createPort('it2me.helper.hangout'); | |
| 89 it2meService.onConnectExternal_(hangoutPort2); | |
| 90 | |
| 91 appLauncher.launch = function () { | |
| 92 return promiseResolveSynchronous('tabId2'); | |
| 93 }; | |
| 94 | |
| 95 hangoutPort2.onMessage.mock$fire({ | |
| 96 method: 'connect', | |
| 97 accessCode: "123412341234" | |
| 98 }); | |
| 99 | |
| 100 it2meService.onWebappConnect_(webappPort); | |
| 101 | |
| 102 var webappPort2 = createPort('it2me.helper.webapp', 'tabId2'); | |
| 103 it2meService.onWebappConnect_(webappPort2); | |
| 104 | |
| 105 webappPort.onMessage.mock$fire({ | |
| 106 method: 'sessionStateChanged', | |
| 107 state: remoting.ClientSession.State.CONNECTED | |
| 108 }); | |
| 109 | |
| 110 // verify that hangout can receive message events from webapp 1 | |
| 111 sinon.assert.calledWith(hangoutPort.postMessage, { | |
| 112 method: 'sessionStateChanged', | |
| 113 state: remoting.ClientSession.State.CONNECTED | |
| 114 }); | |
| 115 | |
| 116 webappPort2.onMessage.mock$fire({ | |
| 117 method: 'sessionStateChanged', | |
| 118 state: remoting.ClientSession.State.CLOSED | |
| 119 }); | |
| 120 | |
| 121 // verify that hangout can receive message events from webapp 2. | |
| 122 sinon.assert.calledWith(hangoutPort2.postMessage, { | |
| 123 method: 'sessionStateChanged', | |
| 124 state: remoting.ClientSession.State.CLOSED | |
| 125 }); | |
| 126 }); | |
| 127 | |
| 128 test('should reject unknown connection', function() { | |
| 129 it2meService.onWebappConnect_(webappPort); | |
| 130 sinon.assert.called(webappPort.disconnect); | |
| 131 | |
| 132 var randomPort = createPort('unsupported.port.name'); | |
| 133 it2meService.onConnectExternal_(randomPort); | |
| 134 sinon.assert.called(randomPort.disconnect); | |
| 135 }); | |
| 136 | |
| 137 })(); | |
| OLD | NEW |