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.
7 * Mock implementation of remoting.HostList
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
17 * @implements {remoting.HostListApi}
19 remoting.MockHostListApi = function() {
21 * The auth code value for the |register| method to return, or null
25 this.authCodeFromRegister = null;
28 * The email value for the |register| method to return, or null if
32 this.emailFromRegister = null;
35 * The host ID to return from register(), or null if it should fail.
38 this.hostIdFromRegister = null;
40 /** @type {!Array<!remoting.Host>} */
45 * Creates and adds a new mock host.
47 * @param {string} hostId The ID of the new host to add.
48 * @return {!remoting.Host} the new mock host
50 remoting.MockHostListApi.prototype.addMockHost = function(hostId) {
51 var newHost = new remoting.Host(hostId);
52 this.hosts.push(newHost);
57 remoting.MockHostListApi.prototype.register = function(
58 hostName, publicKey, hostClientId) {
59 if (this.authCodeFromRegister === null || this.emailFromRegister === null) {
60 return Promise.reject(
62 remoting.Error.Tag.REGISTRATION_FAILED,
63 'MockHostListApi.register'));
65 return Promise.resolve({
66 authCode: this.authCodeFromRegister,
67 email: this.emailFromRegister,
68 hostId: this.hostIdFromRegister
74 remoting.MockHostListApi.prototype.get = function() {
75 return Promise.resolve(this.hosts);
80 * @param {string} hostId
81 * @param {string} hostName
82 * @param {string} hostPublicKey
84 remoting.MockHostListApi.prototype.put =
85 function(hostId, hostName, hostPublicKey) {
86 /** @type {remoting.MockHostListApi} */
88 return new Promise(function(resolve, reject) {
89 for (var i = 0; i < that.hosts.length; ++i) {
90 /** type {remoting.Host} */
91 var host = that.hosts[i];
92 if (host.hostId == hostId) {
93 host.hostName = hostName;
94 host.hostPublicKey = hostPublicKey;
99 console.error('PUT request for unknown host: ' + hostId +
100 ' (' + hostName + ')');
101 reject(remoting.Error.unexpected());
107 * @param {string} hostId
109 remoting.MockHostListApi.prototype.remove = function(hostId) {
110 /** @type {remoting.MockHostListApi} */
112 return new Promise(function(resolve, reject) {
113 for (var i = 0; i < that.hosts.length; ++i) {
114 var host = that.hosts[i];
115 if (host.hostId == hostId) {
116 that.hosts.splice(i, 1);
121 console.error('DELETE request for unknown host: ' + hostId);
122 reject(remoting.Error.unexpected());
127 remoting.MockHostListApi.prototype.getSupportHost = function(supportId) {
128 return Promise.resolve(this.hosts[0]);
132 * @param {boolean} active
134 remoting.MockHostListApi.setActive = function(active) {
135 remoting.HostListApi.setInstance(
136 active ? new remoting.MockHostListApi() : null);