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.
17 chromeMocks.Event = function() {
21 /** @param {Function} callback */
22 chromeMocks.Event.prototype.addListener = function(callback) {
23 this.listeners_.push(callback);
26 /** @param {Function} callback */
27 chromeMocks.Event.prototype.removeListener = function(callback) {
28 for (var i = 0; i < this.listeners_.length; i++) {
29 if (this.listeners_[i] === callback) {
30 this.listeners_.splice(i, 1);
37 * @param {...*} var_args
40 chromeMocks.Event.prototype.mock$fire = function(var_args) {
41 var params = Array.prototype.slice.call(arguments);
42 this.listeners_.forEach(
43 /** @param {Function} listener */
45 listener.apply(null, params);
50 chromeMocks.runtime = {};
53 chromeMocks.runtime.Port = function() {
54 this.onMessage = new chromeMocks.Event();
55 this.onDisconnect = new chromeMocks.Event();
60 /** @type {chrome.runtime.MessageSender} */
64 chromeMocks.runtime.Port.prototype.disconnect = function() {};
67 * @param {Object} message
69 chromeMocks.runtime.Port.prototype.postMessage = function(message) {};
71 /** @type {chromeMocks.Event} */
72 chromeMocks.runtime.onMessage = new chromeMocks.Event();
75 * @param {string?} extensionId
77 * @param {function(*)=} responseCallback
79 chromeMocks.runtime.sendMessage = function(extensionId, message,
83 'The mock only supports sending messages to the same extension.');
84 extensionId = chrome.runtime.id;
85 window.requestAnimationFrame(function() {
86 var message_copy = base.deepCopy(message);
87 chromeMocks.runtime.onMessage.mock$fire(
88 message_copy, {id: extensionId}, responseCallback);
93 chromeMocks.runtime.id = 'extensionId';
96 chromeMocks.storage = {};
98 // Sample implementation of chrome.StorageArea according to
99 // https://developer.chrome.com/apps/storage#type-StorageArea
101 chromeMocks.StorageArea = function() {
102 /** @type {Object} */
107 * @param {!Object} keys
108 * @return {Array<string>}
110 function getKeys(keys) {
111 if (typeof keys === 'string') {
113 } else if (typeof keys === 'object') {
114 return Object.keys(keys);
120 * @param {!Object} keys
121 * @param {Function} onDone
123 chromeMocks.StorageArea.prototype.get = function(keys, onDone) {
125 onDone(base.deepCopy(this.storage_));
129 var result = (typeof keys === 'object') ? keys : {};
130 getKeys(keys).forEach(
131 /** @param {string} key */
133 if (key in this.storage_) {
134 result[key] = base.deepCopy(this.storage_[key]);
140 /** @param {Object} value */
141 chromeMocks.StorageArea.prototype.set = function(value) {
142 for (var key in value) {
143 this.storage_[key] = base.deepCopy(value[key]);
148 * @param {!Object} keys
150 chromeMocks.StorageArea.prototype.remove = function(keys) {
151 getKeys(keys).forEach(
152 /** @param {string} key */
154 delete this.storage_[key];
158 chromeMocks.StorageArea.prototype.clear = function() {
159 this.storage_ = null;
162 /** @type {chromeMocks.StorageArea} */
163 chromeMocks.storage.local = new chromeMocks.StorageArea();
165 var originals_ = null;
168 * Activates a list of Chrome components to mock
169 * @param {Array<string>} components
171 chromeMocks.activate = function(components) {
173 throw new Error('chromeMocks.activate() can only be called once.');
176 components.forEach(function(component) {
177 if (!chromeMocks[component]) {
178 throw new Error('No mocks defined for chrome.' + component);
180 originals_[component] = chrome[component];
181 chrome[component] = chromeMocks[component];
185 chromeMocks.restore = function() {
187 throw new Error('You must call activate() before restore().');
189 for (var components in originals_) {
190 chrome[components] = originals_[components];