Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / remoting / webapp / crd / js / mock_host_list_api.js
blob8d5452a7f0282045ff1d5a3842c456c14006e7b3
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 remoting.HostList
8 */
10 'use strict';
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
15 /**
16 * @constructor
17 * @implements {remoting.HostListApi}
19 remoting.MockHostListApi = function() {
20 /**
21 * The auth code value for the |register| method to return, or null
22 * if it should fail.
23 * @type {?string}
25 this.authCodeFromRegister = null;
27 /**
28 * The email value for the |register| method to return, or null if
29 * it should fail.
30 * @type {?string}
32 this.emailFromRegister = null;
34 /**
35 * The host ID to return from register(), or null if it should fail.
36 * @type {?string}
38 this.hostIdFromRegister = null;
40 /** @type {!Array<!remoting.Host>} */
41 this.hosts = [];
44 /**
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);
53 return newHost;
56 /** @override */
57 remoting.MockHostListApi.prototype.register = function(
58 hostName, publicKey, hostClientId) {
59 if (this.authCodeFromRegister === null || this.emailFromRegister === null) {
60 return Promise.reject(
61 new remoting.Error(
62 remoting.Error.Tag.REGISTRATION_FAILED,
63 'MockHostListApi.register'));
64 } else {
65 return Promise.resolve({
66 authCode: this.authCodeFromRegister,
67 email: this.emailFromRegister,
68 hostId: this.hostIdFromRegister
69 });
73 /** @override */
74 remoting.MockHostListApi.prototype.get = function() {
75 return Promise.resolve(this.hosts);
78 /**
79 * @override
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} */
87 var that = this;
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;
95 resolve(undefined);
96 return;
99 console.error('PUT request for unknown host: ' + hostId +
100 ' (' + hostName + ')');
101 reject(remoting.Error.unexpected());
106 * @override
107 * @param {string} hostId
109 remoting.MockHostListApi.prototype.remove = function(hostId) {
110 /** @type {remoting.MockHostListApi} */
111 var that = this;
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);
117 resolve(undefined);
118 return;
121 console.error('DELETE request for unknown host: ' + hostId);
122 reject(remoting.Error.unexpected());
126 /** @override */
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);