OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * Mock implementation of remoting.HostList | 7 * Mock implementation of remoting.HostList |
8 */ | 8 */ |
9 | 9 |
10 'use strict'; | 10 'use strict'; |
(...skipping 14 matching lines...) Expand all Loading... |
25 this.authCodeFromRegister = null; | 25 this.authCodeFromRegister = null; |
26 | 26 |
27 /** | 27 /** |
28 * The email value for the |register| method to return, or null if | 28 * The email value for the |register| method to return, or null if |
29 * it should fail. | 29 * it should fail. |
30 * @type {?string} | 30 * @type {?string} |
31 */ | 31 */ |
32 this.emailFromRegister = null; | 32 this.emailFromRegister = null; |
33 | 33 |
34 /** | 34 /** |
35 * This host ID to return from register(), or null if it should fail. | 35 * The host ID to return from register(), or null if it should fail. |
36 * @type {?string} | 36 * @type {?string} |
37 */ | 37 */ |
38 this.hostIdFromRegister = null; | 38 this.hostIdFromRegister = null; |
39 | 39 |
40 /** @type {Array<remoting.Host>} */ | 40 /** @type {!Array<!remoting.Host>} */ |
41 this.hosts = [ | 41 this.hosts = []; |
42 { | 42 }; |
43 'hostName': 'Online host', | 43 |
44 'hostId': 'online-host-id', | 44 /** |
45 'status': 'ONLINE', | 45 * Creates and adds a new mock host. |
46 'jabberId': 'online-jid', | 46 * |
47 'publicKey': 'online-public-key', | 47 * @param {string} hostId The ID of the new host to add. |
48 'tokenUrlPatterns': [], | 48 * @return {!remoting.Host} the new mock host |
49 'updatedTime': new Date().toISOString() | 49 */ |
50 }, | 50 remoting.MockHostListApi.prototype.addMockHost = function(hostId) { |
51 { | 51 var newHost = new remoting.Host(hostId); |
52 'hostName': 'Offline host', | 52 this.hosts.push(newHost); |
53 'hostId': 'offline-host-id', | 53 return newHost; |
54 'status': 'OFFLINE', | |
55 'jabberId': 'offline-jid', | |
56 'publicKey': 'offline-public-key', | |
57 'tokenUrlPatterns': [], | |
58 'updatedTime': new Date(1970, 1, 1).toISOString() | |
59 } | |
60 ]; | |
61 }; | 54 }; |
62 | 55 |
63 /** @override */ | 56 /** @override */ |
64 remoting.MockHostListApi.prototype.register = function( | 57 remoting.MockHostListApi.prototype.register = function( |
65 hostName, publicKey, hostClientId) { | 58 hostName, publicKey, hostClientId) { |
66 if (this.authCodeFromRegister === null || this.emailFromRegister === null) { | 59 if (this.authCodeFromRegister === null || this.emailFromRegister === null) { |
67 return Promise.reject( | 60 return Promise.reject( |
68 new remoting.Error( | 61 new remoting.Error( |
69 remoting.Error.Tag.REGISTRATION_FAILED, | 62 remoting.Error.Tag.REGISTRATION_FAILED, |
70 'MockHostListApi.register')); | 63 'MockHostListApi.register')); |
71 } else { | 64 } else { |
72 return Promise.resolve({ | 65 return Promise.resolve({ |
73 authCode: this.authCodeFromRegister, | 66 authCode: this.authCodeFromRegister, |
74 email: this.emailFromRegister, | 67 email: this.emailFromRegister, |
75 hostId: this.hostIdFromRegister | 68 hostId: this.hostIdFromRegister |
76 }); | 69 }); |
77 } | 70 } |
78 }; | 71 }; |
79 | 72 |
80 /** @override */ | 73 /** @override */ |
81 remoting.MockHostListApi.prototype.get = function() { | 74 remoting.MockHostListApi.prototype.get = function() { |
82 var that = this; | 75 return Promise.resolve(this.hosts); |
83 return new Promise(function(resolve, reject) { | |
84 remoting.mockIdentity.validateTokenAndCall( | |
85 resolve, remoting.Error.handler(reject), [that.hosts]); | |
86 }); | |
87 }; | 76 }; |
88 | 77 |
89 /** | 78 /** |
90 * @override | 79 * @override |
91 * @param {string} hostId | 80 * @param {string} hostId |
92 * @param {string} hostName | 81 * @param {string} hostName |
93 * @param {string} hostPublicKey | 82 * @param {string} hostPublicKey |
94 */ | 83 */ |
95 remoting.MockHostListApi.prototype.put = | 84 remoting.MockHostListApi.prototype.put = |
96 function(hostId, hostName, hostPublicKey) { | 85 function(hostId, hostName, hostPublicKey) { |
97 /** @type {remoting.MockHostListApi} */ | 86 /** @type {remoting.MockHostListApi} */ |
98 var that = this; | 87 var that = this; |
99 return new Promise(function(resolve, reject) { | 88 return new Promise(function(resolve, reject) { |
100 var onTokenValid = function() { | 89 for (var i = 0; i < that.hosts.length; ++i) { |
101 for (var i = 0; i < that.hosts.length; ++i) { | 90 /** type {remoting.Host} */ |
102 /** type {remoting.Host} */ | 91 var host = that.hosts[i]; |
103 var host = that.hosts[i]; | 92 if (host.hostId == hostId) { |
104 if (host.hostId == hostId) { | 93 host.hostName = hostName; |
105 host.hostName = hostName; | 94 host.hostPublicKey = hostPublicKey; |
106 host.hostPublicKey = hostPublicKey; | 95 resolve(undefined); |
107 resolve(undefined); | 96 return; |
108 return; | |
109 } | |
110 } | 97 } |
111 console.error('PUT request for unknown host: ' + hostId + | 98 } |
112 ' (' + hostName + ')'); | 99 console.error('PUT request for unknown host: ' + hostId + |
113 reject(remoting.Error.unexpected()); | 100 ' (' + hostName + ')'); |
114 }; | 101 reject(remoting.Error.unexpected()); |
115 remoting.mockIdentity.validateTokenAndCall(onTokenValid, reject, []); | |
116 }); | 102 }); |
117 }; | 103 }; |
118 | 104 |
119 /** | 105 /** |
120 * @override | 106 * @override |
121 * @param {string} hostId | 107 * @param {string} hostId |
122 */ | 108 */ |
123 remoting.MockHostListApi.prototype.remove = function(hostId) { | 109 remoting.MockHostListApi.prototype.remove = function(hostId) { |
124 /** @type {remoting.MockHostListApi} */ | 110 /** @type {remoting.MockHostListApi} */ |
125 var that = this; | 111 var that = this; |
126 return new Promise(function(resolve, reject) { | 112 return new Promise(function(resolve, reject) { |
127 var onTokenValid = function() { | 113 for (var i = 0; i < that.hosts.length; ++i) { |
128 for (var i = 0; i < that.hosts.length; ++i) { | 114 var host = that.hosts[i]; |
129 var host = that.hosts[i]; | 115 if (host.hostId == hostId) { |
130 if (host.hostId == hostId) { | 116 that.hosts.splice(i, 1); |
131 that.hosts.splice(i, 1); | 117 resolve(undefined); |
132 resolve(undefined); | 118 return; |
133 return; | |
134 } | |
135 } | 119 } |
136 console.error('DELETE request for unknown host: ' + hostId); | 120 } |
137 reject(remoting.Error.unexpected()); | 121 console.error('DELETE request for unknown host: ' + hostId); |
138 }; | 122 reject(remoting.Error.unexpected()); |
139 remoting.mockIdentity.validateTokenAndCall(onTokenValid, reject, []); | |
140 }); | 123 }); |
141 }; | 124 }; |
142 | 125 |
143 /** @override */ | 126 /** @override */ |
144 remoting.MockHostListApi.prototype.getSupportHost = function(supportId) { | 127 remoting.MockHostListApi.prototype.getSupportHost = function(supportId) { |
145 var that = this; | 128 return Promise.resolve(this.hosts[0]); |
146 return new Promise(function(resolve, reject) { | |
147 remoting.mockIdentity.validateTokenAndCall( | |
148 resolve, remoting.Error.handler(reject), [that.hosts[0]]); | |
149 }); | |
150 | |
151 }; | 129 }; |
152 | 130 |
153 /** | 131 /** |
154 * @param {boolean} active | 132 * @param {boolean} active |
155 */ | 133 */ |
156 remoting.MockHostListApi.setActive = function(active) { | 134 remoting.MockHostListApi.setActive = function(active) { |
157 remoting.HostListApi.setInstance( | 135 remoting.HostListApi.setInstance( |
158 active ? new remoting.MockHostListApi() : null); | 136 active ? new remoting.MockHostListApi() : null); |
159 }; | 137 }; |
OLD | NEW |