OLD | NEW |
| (Empty) |
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. | |
4 | |
5 /** | |
6 * @fileoverview | |
7 * Mock implementation of chrome.identity. | |
8 */ | |
9 | |
10 'use strict'; | |
11 | |
12 /** @suppress {duplicate} */ | |
13 var remoting = remoting || {}; | |
14 | |
15 /** | |
16 * @constructor | |
17 */ | |
18 remoting.MockIdentity = function() { | |
19 /** | |
20 * @type {remoting.MockIdentity.AccessToken} | |
21 * @private | |
22 */ | |
23 this.accessToken_ = remoting.MockIdentity.AccessToken.NONE; | |
24 }; | |
25 | |
26 /** | |
27 * @param {Object} details | |
28 * @param {function(string)} callback | |
29 */ | |
30 remoting.MockIdentity.prototype.getAuthToken = function(details, callback) { | |
31 window.setTimeout(callback.bind(null, this.accessToken_), 0); | |
32 }; | |
33 | |
34 /** | |
35 * @param {Object} details | |
36 * @param {function()} callback | |
37 */ | |
38 remoting.MockIdentity.prototype.removeCachedAuthToken = | |
39 function(details, callback) { | |
40 window.setTimeout(callback, 0); | |
41 }; | |
42 | |
43 /** | |
44 * @param {Object} details | |
45 * @param {function()} callback | |
46 */ | |
47 remoting.MockIdentity.prototype.launchWebAuthFlow = | |
48 function(details, callback) { | |
49 // TODO(jamiewalch): Work out how to test third-party auth. | |
50 console.error('No mock implementation for launchWebAuthFlow.'); | |
51 }; | |
52 | |
53 | |
54 /** @enum {string} */ | |
55 remoting.MockIdentity.AccessToken = { | |
56 VALID: 'valid-token', | |
57 INVALID: 'invalid-token', | |
58 NONE: '' | |
59 }; | |
60 | |
61 /** | |
62 * @param {remoting.MockIdentity.AccessToken} accessToken | |
63 */ | |
64 remoting.MockIdentity.prototype.setAccessToken = function(accessToken) { | |
65 this.accessToken_ = accessToken; | |
66 }; | |
67 | |
68 /** | |
69 * @param {string} token | |
70 * @param {Function} onDone | |
71 * @param {function(!remoting.Error)} onError | |
72 * @param {Array<*>} values | |
73 */ | |
74 remoting.MockIdentity.validateTokenAndCall = | |
75 function(token, onDone, onError, values) { | |
76 if (token == remoting.MockIdentity.AccessToken.VALID) { | |
77 window.setTimeout( | |
78 onDone.apply.bind(onDone, null, values), | |
79 0); | |
80 } else { | |
81 window.setTimeout( | |
82 onError.bind(null, remoting.Error.AUTHENTICATION_FAILED), | |
83 0); | |
84 } | |
85 }; | |
86 | |
87 /** | |
88 * @param {Function} onDone | |
89 * @param {function(!remoting.Error)} onError | |
90 * @param {Array<*>} values | |
91 */ | |
92 remoting.MockIdentity.prototype.validateTokenAndCall = | |
93 function(onDone, onError, values) { | |
94 remoting.MockIdentity.validateTokenAndCall( | |
95 this.accessToken_, onDone, onError, values); | |
96 }; | |
97 | |
98 /** | |
99 * @param {boolean} active | |
100 */ | |
101 remoting.MockIdentity.setActive = function(active) { | |
102 chrome.identity = active ? remoting.mockIdentity | |
103 : remoting.savedIdentityApi; | |
104 }; | |
105 | |
106 /** @type {Object} */ | |
107 remoting.savedIdentityApi = chrome.identity; | |
108 | |
109 /** @type {remoting.MockIdentity} */ | |
110 remoting.mockIdentity = new remoting.MockIdentity(); | |
OLD | NEW |