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 * This host ID to return from register(), or null if it should fail.
38 this.hostIdFromRegister
= null;
40 /** @type {Array<remoting.Host>} */
43 'hostName': 'Online host',
44 'hostId': 'online-host-id',
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',
55 'jabberId': 'offline-jid',
56 'publicKey': 'offline-public-key',
57 'tokenUrlPatterns': [],
58 'updatedTime': new Date(1970, 1, 1).toISOString()
64 remoting
.MockHostListApi
.prototype.register = function(
65 hostName
, publicKey
, hostClientId
) {
66 if (this.authCodeFromRegister
=== null || this.emailFromRegister
=== null) {
67 return Promise
.reject(
69 remoting
.Error
.Tag
.REGISTRATION_FAILED
,
70 'MockHostListApi.register'));
72 return Promise
.resolve({
73 authCode
: this.authCodeFromRegister
,
74 email
: this.emailFromRegister
,
75 hostId
: this.hostIdFromRegister
81 remoting
.MockHostListApi
.prototype.get = function() {
83 return new Promise(function(resolve
, reject
) {
84 remoting
.mockIdentity
.validateTokenAndCall(
85 resolve
, remoting
.Error
.handler(reject
), [that
.hosts
]);
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} */
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
;
111 console
.error('PUT request for unknown host: ' + hostId
+
112 ' (' + hostName
+ ')');
113 reject(remoting
.Error
.unexpected());
115 remoting
.mockIdentity
.validateTokenAndCall(onTokenValid
, reject
, []);
121 * @param {string} hostId
123 remoting
.MockHostListApi
.prototype.remove = function(hostId
) {
124 /** @type {remoting.MockHostListApi} */
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);
136 console
.error('DELETE request for unknown host: ' + hostId
);
137 reject(remoting
.Error
.unexpected());
139 remoting
.mockIdentity
.validateTokenAndCall(onTokenValid
, reject
, []);
144 remoting
.MockHostListApi
.prototype.getSupportHost = function(supportId
) {
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);