| 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 helperChannel = null; | |
| 18 var disconnectCallback = null; | |
| 19 | |
| 20 module('It2MeHelperChannel', { | |
| 21 setup: function() { | |
| 22 // App Launcher. | |
| 23 appLauncher = { | |
| 24 launch: function () { | |
| 25 return promiseResolveSynchronous('tabId'); | |
| 26 }, | |
| 27 close: function () {} | |
| 28 }; | |
| 29 appLauncher.launch = sinon.spy(appLauncher, 'launch'); | |
| 30 appLauncher.close = sinon.spy(appLauncher, 'close'); | |
| 31 | |
| 32 // HangoutPort. | |
| 33 hangoutPort = new chromeMocks.runtime.Port(); | |
| 34 hangoutPort.postMessage = sinon.spy(hangoutPort, 'postMessage'); | |
| 35 hangoutPort.disconnect = sinon.spy(hangoutPort, 'disconnect'); | |
| 36 | |
| 37 // WebappPort. | |
| 38 webappPort = new chromeMocks.runtime.Port(); | |
| 39 webappPort.sender = { | |
| 40 tab : { | |
| 41 id : 'tabId' | |
| 42 } | |
| 43 }; | |
| 44 webappPort.postMessage = sinon.spy(webappPort, 'postMessage'); | |
| 45 webappPort.disconnect = sinon.spy(webappPort, 'disconnect'); | |
| 46 | |
| 47 // disconnect callback | |
| 48 disconnectCallback = sinon.spy(); | |
| 49 | |
| 50 // HelperChannel. | |
| 51 helperChannel = new remoting.It2MeHelperChannel( | |
| 52 appLauncher, hangoutPort, disconnectCallback); | |
| 53 helperChannel.init(); | |
| 54 hangoutPort.onMessage.mock$fire({ | |
| 55 method: remoting.It2MeHelperChannel.HangoutMessageTypes.CONNECT, | |
| 56 accessCode: "123412341234" | |
| 57 }); | |
| 58 }, | |
| 59 }); | |
| 60 | |
| 61 function promiseResolveSynchronous(value) { | |
| 62 return { | |
| 63 then: function(callback) { | |
| 64 callback('tabId'); | |
| 65 } | |
| 66 }; | |
| 67 } | |
| 68 | |
| 69 test('onHangoutMessage_("hello") should return supportedFeatures', function() { | |
| 70 hangoutPort.onMessage.mock$fire( | |
| 71 { method: remoting.It2MeHelperChannel.HangoutMessageTypes.HELLO }); | |
| 72 | |
| 73 sinon.assert.calledWith(hangoutPort.postMessage, { | |
| 74 method: remoting.It2MeHelperChannel.HangoutMessageTypes.HELLO_RESPONSE, | |
| 75 supportedFeatures: base.values(remoting.It2MeHelperChannel.Features) | |
| 76 }); | |
| 77 }); | |
| 78 | |
| 79 test('onHangoutMessage_(|connect|) should launch the webapp', | |
| 80 function() { | |
| 81 sinon.assert.called(appLauncher.launch); | |
| 82 QUnit.equal(helperChannel.instanceId(), 'tabId'); | |
| 83 }); | |
| 84 | |
| 85 test('onWebappMessage() should forward messages to hangout', function() { | |
| 86 // Execute. | |
| 87 helperChannel.onWebappConnect(webappPort); | |
| 88 webappPort.onMessage.mock$fire({ | |
| 89 method:'sessionStateChanged', | |
| 90 state:remoting.ClientSession.State.CONNECTING | |
| 91 }); | |
| 92 webappPort.onMessage.mock$fire({ | |
| 93 method:'sessionStateChanged', | |
| 94 state:remoting.ClientSession.State.CONNECTED | |
| 95 }); | |
| 96 | |
| 97 // Verify events are forwarded. | |
| 98 sinon.assert.calledWith(hangoutPort.postMessage, { | |
| 99 method:'sessionStateChanged', | |
| 100 state:remoting.ClientSession.State.CONNECTING | |
| 101 }); | |
| 102 | |
| 103 sinon.assert.calledWith(hangoutPort.postMessage, { | |
| 104 method:'sessionStateChanged', | |
| 105 state:remoting.ClientSession.State.CONNECTED | |
| 106 }); | |
| 107 }); | |
| 108 | |
| 109 test('should notify hangout when the webapp crashes', function() { | |
| 110 // Execute. | |
| 111 helperChannel.onWebappConnect(webappPort); | |
| 112 webappPort.onDisconnect.mock$fire(); | |
| 113 | |
| 114 // Verify events are forwarded. | |
| 115 sinon.assert.calledWith(hangoutPort.postMessage, { | |
| 116 method:'sessionStateChanged', | |
| 117 state: remoting.ClientSession.State.FAILED | |
| 118 }); | |
| 119 sinon.assert.called(hangoutPort.disconnect); | |
| 120 sinon.assert.calledOnce(disconnectCallback); | |
| 121 }); | |
| 122 | |
| 123 test('should notify hangout when the session is ended', function() { | |
| 124 // Execute. | |
| 125 helperChannel.onWebappConnect(webappPort); | |
| 126 webappPort.onMessage.mock$fire({ | |
| 127 method:'sessionStateChanged', | |
| 128 state:remoting.ClientSession.State.CLOSED | |
| 129 }); | |
| 130 | |
| 131 webappPort.onDisconnect.mock$fire(); | |
| 132 | |
| 133 // Verify events are forwarded. | |
| 134 sinon.assert.calledWith(hangoutPort.postMessage, { | |
| 135 method:'sessionStateChanged', | |
| 136 state:remoting.ClientSession.State.CLOSED | |
| 137 }); | |
| 138 sinon.assert.called(hangoutPort.disconnect); | |
| 139 sinon.assert.calledOnce(disconnectCallback); | |
| 140 }); | |
| 141 | |
| 142 test('should notify hangout when the session has error', function() { | |
| 143 helperChannel.onWebappConnect(webappPort); | |
| 144 webappPort.onMessage.mock$fire({ | |
| 145 method:'sessionStateChanged', | |
| 146 state:remoting.ClientSession.State.FAILED | |
| 147 }); | |
| 148 | |
| 149 webappPort.onDisconnect.mock$fire(); | |
| 150 | |
| 151 // Verify events are forwarded. | |
| 152 sinon.assert.calledWith(hangoutPort.postMessage, { | |
| 153 method:'sessionStateChanged', | |
| 154 state:remoting.ClientSession.State.FAILED | |
| 155 }); | |
| 156 sinon.assert.called(hangoutPort.disconnect); | |
| 157 sinon.assert.calledOnce(disconnectCallback); | |
| 158 }); | |
| 159 | |
| 160 | |
| 161 test('onHangoutMessages_(disconnect) should close the webapp', function() { | |
| 162 // Execute. | |
| 163 helperChannel.onWebappConnect(webappPort); | |
| 164 hangoutPort.onMessage.mock$fire({ | |
| 165 method: remoting.It2MeHelperChannel.HangoutMessageTypes.DISCONNECT | |
| 166 }); | |
| 167 | |
| 168 sinon.assert.calledOnce(appLauncher.close); | |
| 169 | |
| 170 // Webapp will respond by disconnecting the port | |
| 171 webappPort.onDisconnect.mock$fire(); | |
| 172 | |
| 173 // Verify events are forwarded. | |
| 174 sinon.assert.calledWith(hangoutPort.postMessage, { | |
| 175 method:'sessionStateChanged', | |
| 176 state:remoting.ClientSession.State.CLOSED | |
| 177 }); | |
| 178 sinon.assert.called(webappPort.disconnect); | |
| 179 sinon.assert.called(hangoutPort.disconnect); | |
| 180 }); | |
| 181 | |
| 182 test('should close the webapp when hangout crashes', function() { | |
| 183 // Execute. | |
| 184 helperChannel.onWebappConnect(webappPort); | |
| 185 hangoutPort.onDisconnect.mock$fire(); | |
| 186 | |
| 187 sinon.assert.calledOnce(appLauncher.close); | |
| 188 sinon.assert.calledOnce(disconnectCallback); | |
| 189 | |
| 190 sinon.assert.called(hangoutPort.disconnect); | |
| 191 sinon.assert.called(webappPort.disconnect); | |
| 192 }); | |
| 193 | |
| 194 })(); | |
| OLD | NEW |