Convert cacheinvalidation_unittests to run exclusively on Swarming
[chromium-blink-merge.git] / remoting / webapp / crd / js / mock_host_list_api.js
blob3cead52c60e328123182855d86fd0e03f74ba8ea
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 * This 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 = [
43 'hostName': 'Online host',
44 'hostId': 'online-host-id',
45 'status': 'ONLINE',
46 'jabberId': 'online-jid',
47 'publicKey': 'online-public-key',
48 'tokenUrlPatterns': [],
49 'updatedTime': new Date().toISOString()
52 'hostName': 'Offline host',
53 'hostId': 'offline-host-id',
54 'status': 'OFFLINE',
55 'jabberId': 'offline-jid',
56 'publicKey': 'offline-public-key',
57 'tokenUrlPatterns': [],
58 'updatedTime': new Date(1970, 1, 1).toISOString()
63 /** @override */
64 remoting.MockHostListApi.prototype.register = function(
65 hostName, publicKey, hostClientId) {
66 if (this.authCodeFromRegister === null || this.emailFromRegister === null) {
67 return Promise.reject(
68 new remoting.Error(
69 remoting.Error.Tag.REGISTRATION_FAILED,
70 'MockHostListApi.register'));
71 } else {
72 return Promise.resolve({
73 authCode: this.authCodeFromRegister,
74 email: this.emailFromRegister,
75 hostId: this.hostIdFromRegister
76 });
80 /** @override */
81 remoting.MockHostListApi.prototype.get = function() {
82 var that = this;
83 return new Promise(function(resolve, reject) {
84 remoting.mockIdentity.validateTokenAndCall(
85 resolve, remoting.Error.handler(reject), [that.hosts]);
86 });
89 /**
90 * @override
91 * @param {string} hostId
92 * @param {string} hostName
93 * @param {string} hostPublicKey
95 remoting.MockHostListApi.prototype.put =
96 function(hostId, hostName, hostPublicKey) {
97 /** @type {remoting.MockHostListApi} */
98 var that = this;
99 return new Promise(function(resolve, reject) {
100 var onTokenValid = function() {
101 for (var i = 0; i < that.hosts.length; ++i) {
102 /** type {remoting.Host} */
103 var host = that.hosts[i];
104 if (host.hostId == hostId) {
105 host.hostName = hostName;
106 host.hostPublicKey = hostPublicKey;
107 resolve(undefined);
108 return;
111 console.error('PUT request for unknown host: ' + hostId +
112 ' (' + hostName + ')');
113 reject(remoting.Error.unexpected());
115 remoting.mockIdentity.validateTokenAndCall(onTokenValid, reject, []);
120 * @override
121 * @param {string} hostId
123 remoting.MockHostListApi.prototype.remove = function(hostId) {
124 /** @type {remoting.MockHostListApi} */
125 var that = this;
126 return new Promise(function(resolve, reject) {
127 var onTokenValid = function() {
128 for (var i = 0; i < that.hosts.length; ++i) {
129 var host = that.hosts[i];
130 if (host.hostId == hostId) {
131 that.hosts.splice(i, 1);
132 resolve(undefined);
133 return;
136 console.error('DELETE request for unknown host: ' + hostId);
137 reject(remoting.Error.unexpected());
139 remoting.mockIdentity.validateTokenAndCall(onTokenValid, reject, []);
143 /** @override */
144 remoting.MockHostListApi.prototype.getSupportHost = function(supportId) {
145 var that = this;
146 return new Promise(function(resolve, reject) {
147 remoting.mockIdentity.validateTokenAndCall(
148 resolve, remoting.Error.handler(reject), [that.hosts[0]]);
154 * @param {boolean} active
156 remoting.MockHostListApi.setActive = function(active) {
157 remoting.HostListApi.setInstance(
158 active ? new remoting.MockHostListApi() : null);