Roll src/third_party/WebKit 3529d49:06e8485 (svn 202554:202555)
[chromium-blink-merge.git] / remoting / webapp / crd / js / mock_signal_strategy.js
blob5a36a541aa9aef08371da47aa4942031313df4f2
1 // Copyright 2015 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 'use strict';
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
11 /**
12  * @param {string=} jid
13  * @param {remoting.SignalStrategy.Type=} type
14  *
15  * @implements {remoting.SignalStrategy}
16  * @constructor
17  */
18 remoting.MockSignalStrategy = function(jid, type) {
19   this.jid_ = (jid != undefined) ? jid : "jid@example.com";
20   this.type_ = (type != undefined) ? type : remoting.SignalStrategy.Type.XMPP;
21   this.onStateChangedCallback_ = null;
23   /** @type {remoting.SignalStrategy.State} */
24   this.state_ = remoting.SignalStrategy.State.NOT_CONNECTED;
26   /** @type {!remoting.Error} */
27   this.error_ = remoting.Error.none();
29   this.onIncomingStanzaCallback_ = function() {};
32 /** @override */
33 remoting.MockSignalStrategy.prototype.dispose = function() {
36 /** @override */
37 remoting.MockSignalStrategy.prototype.connect = function() {
38   var that = this;
39   Promise.resolve().then(function() {
40     that.setStateForTesting(remoting.SignalStrategy.State.CONNECTED);
41   });
44 /** @override */
45 remoting.MockSignalStrategy.prototype.sendMessage = function() {
48 /**
49  * @param {function(remoting.SignalStrategy.State):void} onStateChangedCallback
50  *   Callback to call on state change.
51  */
52 remoting.MockSignalStrategy.prototype.setStateChangedCallback = function(
53     onStateChangedCallback) {
54   this.onStateChangedCallback_ = onStateChangedCallback;
57 /**
58  * @param {?function(Element):void} onIncomingStanzaCallback Callback to call on
59  *     incoming messages.
60  */
61 remoting.MockSignalStrategy.prototype.setIncomingStanzaCallback =
62     function(onIncomingStanzaCallback) {
63   this.onIncomingStanzaCallback_ =
64       onIncomingStanzaCallback ? onIncomingStanzaCallback
65                                : function() {};
68 /** @param {Element} stanza */
69 remoting.MockSignalStrategy.prototype.mock$onIncomingStanza = function(stanza) {
70   this.onIncomingStanzaCallback_(stanza);
73 /** @return {remoting.SignalStrategy.State} */
74 remoting.MockSignalStrategy.prototype.getState = function() {
75   return this.state_;
78 /** @return {!remoting.Error} */
79 remoting.MockSignalStrategy.prototype.getError = function() {
80   return this.error_;
83 /** @return {string} */
84 remoting.MockSignalStrategy.prototype.getJid = function() {
85   return this.jid_;
88 /** @return {remoting.SignalStrategy.Type} */
89 remoting.MockSignalStrategy.prototype.getType = function() {
90   return this.type_;
93 /**
94  * @param {remoting.SignalStrategy.State} state
95  */
96 remoting.MockSignalStrategy.prototype.setStateForTesting = function(state) {
97   this.state_ = state;
98   if (state == remoting.SignalStrategy.State.FAILED) {
99     this.error_ = remoting.Error.unexpected('setStateForTesting');
100   } else {
101     this.error_ = remoting.Error.none();
102   }
103   this.onStateChangedCallback_(state);