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 chrome.identity.
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
18 remoting.MockIdentity = function() {
20 * @type {remoting.MockIdentity.AccessToken}
23 this.accessToken_ = remoting.MockIdentity.AccessToken.NONE;
27 * @param {Object} details
28 * @param {function(string)} callback
30 remoting.MockIdentity.prototype.getAuthToken = function(details, callback) {
31 window.setTimeout(callback.bind(null, this.accessToken_), 0);
35 * @param {Object} details
36 * @param {function()} callback
38 remoting.MockIdentity.prototype.removeCachedAuthToken =
39 function(details, callback) {
40 window.setTimeout(callback, 0);
44 * @param {Object} details
45 * @param {function()} callback
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.');
55 remoting.MockIdentity.AccessToken = {
57 INVALID: 'invalid-token',
62 * @param {remoting.MockIdentity.AccessToken} accessToken
64 remoting.MockIdentity.prototype.setAccessToken = function(accessToken) {
65 this.accessToken_ = accessToken;
69 * @param {string} token
70 * @param {Function} onDone
71 * @param {function(!remoting.Error)} onError
72 * @param {Array<*>} values
74 remoting.MockIdentity.validateTokenAndCall =
75 function(token, onDone, onError, values) {
76 if (token == remoting.MockIdentity.AccessToken.VALID) {
78 onDone.apply.bind(onDone, null, values),
82 onError.bind(null, new remoting.Error(
83 remoting.Error.Tag.AUTHENTICATION_FAILED)),
89 * @param {Function} onDone
90 * @param {function(!remoting.Error)} onError
91 * @param {Array<*>} values
93 remoting.MockIdentity.prototype.validateTokenAndCall =
94 function(onDone, onError, values) {
95 remoting.MockIdentity.validateTokenAndCall(
96 this.accessToken_, onDone, onError, values);
100 * @param {boolean} active
102 remoting.MockIdentity.setActive = function(active) {
104 active ? remoting.mockIdentity : remoting.savedIdentityApi;
107 /** @type {Object} */
108 remoting.savedIdentityApi = chrome.identity;
110 /** @type {remoting.MockIdentity} */
111 remoting.mockIdentity = new remoting.MockIdentity();