Implement basic mocks for testing.
[chromium-blink-merge.git] / remoting / webapp / browser_test / mock_session_connector.js
blob7ef5b50d64f174ea22c6ffce91d3825106be41ce
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  * @constructor
18  * @implements {remoting.SessionConnector}
19  */
20 remoting.MockSessionConnector = function(clientContainer, onConnected, onError,
21                                          onExtensionMessage) {
22   this.clientContainer_ = clientContainer;
23   this.onConnected_ = onConnected;
24   this.onError = onError;
25   this.onExtensionMessage_ = onExtensionMessage;
26   this.reset();
29 remoting.MockSessionConnector.prototype.reset = function() {
30   this.pairingRequested_ = false;
33 remoting.MockSessionConnector.prototype.connectMe2Me =
34     function(host, fetchPin, fetchThirdPartyToken,
35              clientPairingId, clientPairedSecret) {
36   this.mode_ = remoting.ClientSession.Mode.ME2ME;
37   this.connect_();
40 remoting.MockSessionConnector.prototype.updatePairingInfo =
41     function(clientId, sharedSecret) {
44 remoting.MockSessionConnector.prototype.connectIT2Me =
45     function(accessCode) {
46   this.mode_ = remoting.ClientSession.Mode.ME2ME;
47   this.connect_();
50 remoting.MockSessionConnector.prototype.reconnect = function() {
51   base.debug.assert(this.mode_ == remoting.ClientSession.Mode.ME2ME);
52   this.connect_();
55 remoting.MockSessionConnector.prototype.cancel = function() {
58 remoting.MockSessionConnector.prototype.getConnectionMode = function() {
59   return this.mode_;
62 remoting.MockSessionConnector.prototype.getHostId = function() {
63   return this.hostId_;
66 remoting.MockSessionConnector.prototype.requestPairing = function() {
67   this.pairingRequested_ = true;
70 remoting.MockSessionConnector.prototype.pairingRequested = function() {
71   return this.pairingRequested_;
74 remoting.MockSessionConnector.prototype.connect_ = function() {
75   var signalling = new remoting.MockSignalStrategy();
76   var hostName = 'Mock host';
77   var accessCode = '';
78   var authenticationMethods = '';
79   var hostId = '';
80   var hostJid = '';
81   var hostPublicKey = '';
82   var clientPairingId = '';
83   var clientPairedSecret = '';
84   var fetchPin = function(offerPairing, callback) {
85     window.setTimeout(function() { callback('') }, 0);
86   };
87   var fetchThirdPartyToken = function(tokenUrl, scope, callback) {
88     window.setTimeout(function() { callback('', '') }, 0);
89   };
91   var clientSession = new remoting.ClientSession(
92       signalling, this.clientContainer_, hostName,
93       accessCode, fetchPin, fetchThirdPartyToken,
94       authenticationMethods, hostId, hostJid, hostPublicKey,
95       this.mode_, clientPairingId, clientPairedSecret);
97   var onStateChange = function(event) {
98     if (event.current == remoting.ClientSession.State.CONNECTED) {
99       this.onConnected_(clientSession);
100     }
101   }.bind(this);
103   clientSession.addEventListener(
104       remoting.ClientSession.Events.stateChanged,
105       onStateChange);
106   clientSession.createPluginAndConnect(this.onExtensionMessage_);
111  * @constructor
112  * @extends {remoting.SessionConnectorFactory}
113  */
114 remoting.MockSessionConnectorFactory = function() {};
116 remoting.MockSessionConnectorFactory.prototype.createConnector =
117     function(clientContainer, onConnected, onError, onExtensionMessage) {
118   return new remoting.MockSessionConnector(
119       clientContainer, onConnected, onError, onExtensionMessage);