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