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 // This file contains various mock objects for the chrome platform to make
6 // unit testing easier.
13 chrome.Event = function() {};
15 /** @param {Function} callback */
16 chrome.Event.prototype.addListener = function(callback) {};
18 /** @param {Function} callback */
19 chrome.Event.prototype.removeListener = function(callback) {};
26 * @extends {chrome.Event}
28 chromeMocks.Event = function() {
32 /** @param {Function} callback */
33 chromeMocks.Event.prototype.addListener = function(callback) {
34 this.listeners_.push(callback);
37 /** @param {Function} callback */
38 chromeMocks.Event.prototype.removeListener = function(callback) {
39 for (var i = 0; i < this.listeners_.length; i++) {
40 if (this.listeners_[i] === callback) {
41 this.listeners_.splice(i, 1);
48 * @param {...*} var_args
51 chromeMocks.Event.prototype.mock$fire = function(var_args) {
52 var params = Array.prototype.slice.call(arguments);
53 this.listeners_.forEach(
54 /** @param {Function} listener */
56 listener.apply(null, params);
61 chromeMocks.runtime = {};
64 chromeMocks.runtime.Port = function() {
65 this.onMessage = new chromeMocks.Event();
66 this.onDisconnect = new chromeMocks.Event();
71 /** @type {chrome.runtime.MessageSender} */
75 chromeMocks.runtime.Port.prototype.disconnect = function() {};
78 * @param {Object} message
80 chromeMocks.runtime.Port.prototype.postMessage = function(message) {};
82 /** @type {chromeMocks.Event} */
83 chromeMocks.runtime.onMessage = new chromeMocks.Event();
86 * @param {string?} extensionId
88 * @param {function(*)=} responseCallback
90 chromeMocks.runtime.sendMessage = function(extensionId, message,
94 'The mock only supports sending messages to the same extension.');
95 extensionId = chrome.runtime.id;
96 window.requestAnimationFrame(function() {
97 var message_copy = base.deepCopy(message);
98 chromeMocks.runtime.onMessage.mock$fire(
99 message_copy, {id: extensionId}, responseCallback);
103 /** @type {string} */
104 chromeMocks.runtime.id = 'extensionId';
106 /** @type {Object} */
107 chromeMocks.runtime.lastError = {
108 /** @type {string|undefined} */
113 // Sample implementation of chrome.StorageArea according to
114 // https://developer.chrome.com/apps/storage#type-StorageArea
116 chromeMocks.StorageArea = function() {
117 /** @type {Object} */
122 * @param {!Object} keys
123 * @return {Array<string>}
125 function getKeys(keys) {
126 if (typeof keys === 'string') {
128 } else if (typeof keys === 'object') {
129 return Object.keys(keys);
135 * @param {!Object} keys
136 * @param {Function} onDone
138 chromeMocks.StorageArea.prototype.get = function(keys, onDone) {
140 onDone(base.deepCopy(this.storage_));
144 var result = (typeof keys === 'object') ? keys : {};
145 getKeys(keys).forEach(
146 /** @param {string} key */
148 if (key in this.storage_) {
149 result[key] = base.deepCopy(this.storage_[key]);
155 /** @param {Object} value */
156 chromeMocks.StorageArea.prototype.set = function(value) {
157 for (var key in value) {
158 this.storage_[key] = base.deepCopy(value[key]);
163 * @param {!Object} keys
165 chromeMocks.StorageArea.prototype.remove = function(keys) {
166 getKeys(keys).forEach(
167 /** @param {string} key */
169 delete this.storage_[key];
173 chromeMocks.StorageArea.prototype.clear = function() {
174 this.storage_ = null;
177 /** @type {Object} */
178 chromeMocks.storage = {};
180 /** @type {chromeMocks.StorageArea} */
181 chromeMocks.storage.local = new chromeMocks.StorageArea();
185 chromeMocks.Identity = function() {
186 /** @private {string|undefined} */
187 this.token_ = undefined;
191 * @param {Object} options
192 * @param {function(string=):void} callback
194 chromeMocks.Identity.prototype.getAuthToken = function(options, callback) {
195 // Append the 'scopes' array, if present, to the dummy token.
196 var token = this.token_;
197 if (token !== undefined && options['scopes'] !== undefined) {
198 token += JSON.stringify(options['scopes']);
200 // Don't use setTimeout because sinon mocks it.
201 window.requestAnimationFrame(callback.bind(null, token));
204 /** @param {string} token */
205 chromeMocks.Identity.prototype.mock$setToken = function(token) {
209 chromeMocks.Identity.prototype.mock$clearToken = function() {
210 this.token_ = undefined;
213 /** @type {chromeMocks.Identity} */
214 chromeMocks.identity = new chromeMocks.Identity();
217 var originals_ = null;
220 * Activates a list of Chrome components to mock
221 * @param {Array<string>} components
223 chromeMocks.activate = function(components) {
225 throw new Error('chromeMocks.activate() can only be called once.');
228 components.forEach(function(component) {
229 if (!chromeMocks[component]) {
230 throw new Error('No mocks defined for chrome.' + component);
232 originals_[component] = chrome[component];
233 chrome[component] = chromeMocks[component];
237 chromeMocks.restore = function() {
239 throw new Error('You must call activate() before restore().');
241 for (var components in originals_) {
242 chrome[components] = originals_[components];