Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / remoting / webapp / unittests / it2me_service_unittest.js
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 it2meService = null;
13
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');
19
20   return port;
21 }
22
23 function promiseResolveSynchronous(value) {
24   return {
25     then: function(callback) {
26       callback(value);
27     }
28   };
29 }
30
31 module('It2MeService', {
32   setup: function() {
33     // App Launcher.
34     appLauncher = {
35       launch: function () {
36         return promiseResolveSynchronous('tabId');
37       },
38       close: function () {}
39     };
40     // HangoutPort.
41     hangoutPort = createPort('it2me.helper.hangout');
42     it2meService = new remoting.It2MeService(appLauncher);
43     it2meService.onConnectExternal_(hangoutPort);
44     webappPort = createPort('it2me.helper.webapp', 'tabId');
45   }
46 });
47
48 test('should establish a channel two way channel when the webapp connects',
49   function() {
50     // Hangout ---- connect ----> It2MeService.
51     hangoutPort.onMessage.mock$fire({
52       method: 'connect',
53       accessCode: "123412341234"
54     });
55
56     // Webapp ---- connect ----> It2MeService.
57     it2meService.onWebappConnect_(webappPort);
58
59     // Webapp ---- sessionStateChanged ----> It2MeService.
60     webappPort.onMessage.mock$fire({
61       method: 'sessionStateChanged',
62       state: remoting.ClientSession.State.CONNECTED
63     });
64
65     // verify that hangout can receive message events.
66     sinon.assert.calledWith(hangoutPort.postMessage, {
67       method: 'sessionStateChanged',
68       state: remoting.ClientSession.State.CONNECTED
69     });
70
71     hangoutPort.onDisconnect.mock$fire();
72     QUnit.equal(it2meService.helpers_.length, 0);
73 });
74
75 test('should handle multiple helper connections', function() {
76     // Hangout ---- connect ----> It2MeService.
77     hangoutPort.onMessage.mock$fire({
78       method: 'connect',
79       accessCode: "123412341234"
80     });
81
82     // Hangout2 ---- connect ----> It2MeService.
83     var hangoutPort2 = createPort('it2me.helper.hangout');
84     it2meService.onConnectExternal_(hangoutPort2);
85
86     appLauncher.launch = function () {
87       return promiseResolveSynchronous('tabId2');
88     };
89
90     hangoutPort2.onMessage.mock$fire({
91       method: 'connect',
92       accessCode: "123412341234"
93     });
94
95     it2meService.onWebappConnect_(webappPort);
96
97     var webappPort2 = createPort('it2me.helper.webapp', 'tabId2');
98     it2meService.onWebappConnect_(webappPort2);
99
100     webappPort.onMessage.mock$fire({
101       method: 'sessionStateChanged',
102       state: remoting.ClientSession.State.CONNECTED
103     });
104
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
109     });
110
111     webappPort2.onMessage.mock$fire({
112       method: 'sessionStateChanged',
113       state: remoting.ClientSession.State.CLOSED
114     });
115
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
120     });
121 });
122
123 test('should reject unknown connection', function() {
124   it2meService.onWebappConnect_(webappPort);
125   sinon.assert.called(webappPort.disconnect);
126
127   var randomPort = createPort('unsupported.port.name');
128   it2meService.onConnectExternal_(randomPort);
129   sinon.assert.called(randomPort.disconnect);
130 });
131
132 })();