Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / remoting / webapp / browser_test / mock_session_connector.js
blob090e344b6120b82e30329569e2db2568e99fed76
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.
5 /**
6 * @fileoverview
7 * Mock implementation of SessionConnector for testing.
8 * @suppress {checkTypes}
9 */
11 'use strict';
13 /** @suppress {duplicate} */
14 var remoting = remoting || {};
16 /**
17 * @param {HTMLElement} clientContainer Container element for the client view.
18 * @param {function(remoting.ClientSession):void} onConnected Callback on
19 * success.
20 * @param {function(remoting.Error):void} onError Callback on error.
21 * @param {function(string, string):boolean} onExtensionMessage The handler for
22 * protocol extension messages. Returns true if a message is recognized;
23 * false otherwise.
24 * @param {function(remoting.Error):void} onConnectionFailed Callback for when
25 * the connection fails.
26 * @param {Array<string>} requiredCapabilities Connector capabilities
27 * required by this application.
28 * @param {string} defaultRemapKeys The default set of key mappings for the
29 * client session to use.
30 * @constructor
31 * @implements {remoting.SessionConnector}
33 remoting.MockSessionConnector = function(clientContainer, onConnected, onError,
34 onExtensionMessage,
35 onConnectionFailed,
36 requiredCapabilities,
37 defaultRemapKeys) {
38 this.clientContainer_ = clientContainer;
39 /** @type {function(remoting.ClientSession):void} */
40 this.onConnected_ = onConnected;
41 this.onError = onError;
42 this.onExtensionMessage_ = onExtensionMessage;
43 this.onConnectionFailed_ = onConnectionFailed;
44 this.requiredCapabilities_ = requiredCapabilities;
45 this.defaultRemapKeys_ = defaultRemapKeys;
47 /** @type {remoting.DesktopConnectedView.Mode} */
48 this.mode_ = remoting.DesktopConnectedView.Mode.ME2ME;
50 this.reset();
53 remoting.MockSessionConnector.prototype.reset = function() {
54 /** @type {string} @private */
55 this.hostId_ = '';
58 remoting.MockSessionConnector.prototype.connectMe2Me =
59 function(host, fetchPin, fetchThirdPartyToken,
60 clientPairingId, clientPairedSecret) {
61 this.mode_ = remoting.DesktopConnectedView.Mode.ME2ME;
62 this.connect_();
65 remoting.MockSessionConnector.prototype.retryConnectMe2Me =
66 function(host, fetchPin, fetchThirdPartyToken,
67 clientPairingId, clientPairedSecret) {
68 this.mode_ = remoting.DesktopConnectedView.Mode.ME2ME;
69 this.connect_();
72 remoting.MockSessionConnector.prototype.connectMe2App =
73 function(host, fetchThirdPartyToken) {
74 this.mode_ = remoting.DesktopConnectedView.Mode.APP_REMOTING;
75 this.connect_();
78 remoting.MockSessionConnector.prototype.updatePairingInfo =
79 function(clientId, sharedSecret) {
82 remoting.MockSessionConnector.prototype.connectIT2Me =
83 function(accessCode) {
84 this.mode_ = remoting.DesktopConnectedView.Mode.ME2ME;
85 this.connect_();
88 remoting.MockSessionConnector.prototype.reconnect = function() {
89 base.debug.assert(this.mode_ == remoting.DesktopConnectedView.Mode.ME2ME);
90 this.connect_();
93 remoting.MockSessionConnector.prototype.cancel = function() {
96 remoting.MockSessionConnector.prototype.getConnectionMode = function() {
97 return this.mode_;
100 remoting.MockSessionConnector.prototype.getHostId = function() {
101 return this.hostId_;
104 remoting.MockSessionConnector.prototype.connect_ = function() {
105 var signalling = new remoting.MockSignalStrategy();
106 signalling.setStateForTesting(remoting.SignalStrategy.State.CONNECTED);
107 var hostName = 'Mock host';
108 var accessCode = '';
109 var authenticationMethods = '';
110 var hostId = '';
111 var hostJid = '';
112 var hostPublicKey = '';
113 var clientPairingId = '';
114 var clientPairedSecret = '';
117 * @param {boolean} offerPairing
118 * @param {function(string):void} callback
120 var fetchPin = function(offerPairing, callback) {
121 window.setTimeout(function() { callback('') }, 0);
125 * @param {string} tokenUrl
126 * @param {string} hostPublicKey
127 * @param {string} scope
128 * @param {function(string, string):void} callback
130 var fetchThirdPartyToken = function(tokenUrl, hostPublicKey, scope,
131 callback) {
132 window.setTimeout(function() { callback('', '') }, 0);
135 var clientSession = new remoting.ClientSession(
136 signalling, this.clientContainer_, hostName,
137 accessCode, fetchPin, fetchThirdPartyToken,
138 authenticationMethods, hostId, hostJid, hostPublicKey,
139 this.mode_, clientPairingId, clientPairedSecret);
141 var that = this;
142 /** @param {remoting.ClientSession.StateEvent} event */
143 var onStateChange = function(event) {
144 if (event.current == remoting.ClientSession.State.CONNECTED) {
145 that.onConnected_(clientSession);
149 clientSession.addEventListener(
150 remoting.ClientSession.Events.stateChanged,
151 onStateChange);
152 clientSession.createPluginAndConnect(this.onExtensionMessage_);
157 * @constructor
158 * @extends {remoting.SessionConnectorFactory}
160 remoting.MockSessionConnectorFactory = function() {};
163 * @param {HTMLElement} clientContainer Container element for the client view.
164 * @param {function(remoting.ClientSession):void} onConnected Callback on
165 * success.
166 * @param {function(remoting.Error):void} onError Callback on error.
167 * @param {function(string, string):boolean} onExtensionMessage The handler for
168 * protocol extension messages. Returns true if a message is recognized;
169 * false otherwise.
170 * @param {function(remoting.Error):void} onConnectionFailed Callback for when
171 * the connection fails.
172 * @param {Array<string>} requiredCapabilities Connector capabilities
173 * required by this application.
174 * @param {string} defaultRemapKeys The default set of key mappings to use
175 * in the client session.
176 * @return {remoting.MockSessionConnector}
178 remoting.MockSessionConnectorFactory.prototype.createConnector =
179 function(clientContainer, onConnected, onError, onExtensionMessage,
180 onConnectionFailed, requiredCapabilities, defaultRemapKeys) {
181 return new remoting.MockSessionConnector(
182 clientContainer, onConnected, onError, onExtensionMessage,
183 onConnectionFailed, requiredCapabilities, defaultRemapKeys);