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 // This file contains various mock objects for the chrome platform to make | 5 // This file contains various mock objects for the chrome platform to make |
6 // unit testing easier. | 6 // unit testing easier. |
7 | 7 |
8 Entry = function() {}; | 8 Entry = function() {}; |
9 | 9 |
10 (function(scope){ | 10 (function(){ |
11 | 11 |
12 var chromeMocks = {}; | 12 var chromeMocks = {}; |
13 | 13 |
14 /** | |
15 * @constructor | |
16 */ | |
14 chromeMocks.Event = function() { | 17 chromeMocks.Event = function() { |
15 this.listeners_ = []; | 18 this.listeners_ = []; |
16 }; | 19 }; |
17 | 20 |
21 /** @param {Function} callback */ | |
18 chromeMocks.Event.prototype.addListener = function(callback) { | 22 chromeMocks.Event.prototype.addListener = function(callback) { |
19 this.listeners_.push(callback); | 23 this.listeners_.push(callback); |
20 }; | 24 }; |
21 | 25 |
26 /** @param {Function} callback */ | |
22 chromeMocks.Event.prototype.removeListener = function(callback) { | 27 chromeMocks.Event.prototype.removeListener = function(callback) { |
23 for (var i = 0; i < this.listeners_.length; i++) { | 28 for (var i = 0; i < this.listeners_.length; i++) { |
24 if (this.listeners_[i] === callback) { | 29 if (this.listeners_[i] === callback) { |
25 this.listeners_.splice(i, 1); | 30 this.listeners_.splice(i, 1); |
26 break; | 31 break; |
27 } | 32 } |
28 } | 33 } |
29 }; | 34 }; |
30 | 35 |
36 /** | |
37 * @param {...*} var_args | |
38 * @return {void} | |
39 */ | |
31 chromeMocks.Event.prototype.mock$fire = function(var_args) { | 40 chromeMocks.Event.prototype.mock$fire = function(var_args) { |
32 var params = Array.prototype.slice.call(arguments); | 41 var params = Array.prototype.slice.call(arguments); |
33 this.listeners_.forEach(function(listener){ | 42 this.listeners_.forEach( |
34 listener.apply(null, params); | 43 /** @param {Function} listener */ |
35 }); | 44 function(listener){ |
45 listener.apply(null, params); | |
46 }); | |
36 }; | 47 }; |
37 | 48 |
49 /** @type {Object} */ | |
38 chromeMocks.runtime = {}; | 50 chromeMocks.runtime = {}; |
39 | 51 |
52 /** @constructor */ | |
40 chromeMocks.runtime.Port = function() { | 53 chromeMocks.runtime.Port = function() { |
41 this.onMessage = new chromeMocks.Event(); | 54 this.onMessage = new chromeMocks.Event(); |
42 this.onDisconnect = new chromeMocks.Event(); | 55 this.onDisconnect = new chromeMocks.Event(); |
43 | 56 |
57 /** @type {string} */ | |
44 this.name = ''; | 58 this.name = ''; |
59 | |
60 /** @type {chrome.runtime.MessageSender} */ | |
45 this.sender = null; | 61 this.sender = null; |
46 }; | 62 }; |
47 | 63 |
48 chromeMocks.runtime.Port.prototype.disconnect = function() {}; | 64 chromeMocks.runtime.Port.prototype.disconnect = function() {}; |
49 chromeMocks.runtime.Port.prototype.postMessage = function() {}; | |
50 | 65 |
66 /** | |
67 * @param {Object} message | |
68 */ | |
69 chromeMocks.runtime.Port.prototype.postMessage = function(message) {}; | |
70 | |
71 /** @type {chromeMocks.Event} */ | |
51 chromeMocks.runtime.onMessage = new chromeMocks.Event(); | 72 chromeMocks.runtime.onMessage = new chromeMocks.Event(); |
52 chromeMocks.runtime.sendMessage = function(extensionId, message, | 73 |
74 /** | |
75 * @param {string?} extensionId | |
76 * @param {*} message | |
77 * @param {Object=} options | |
78 * @param {function(*)=} responseCallback | |
79 */ | |
80 chromeMocks.runtime.sendMessage = function(extensionId, message, options, | |
53 responseCallback) { | 81 responseCallback) { |
54 base.debug.assert( | 82 base.debug.assert( |
55 extensionId === null, | 83 extensionId === null, |
56 'The mock only supports sending messages to the same extension.'); | 84 'The mock only supports sending messages to the same extension.'); |
57 extensionId = chrome.runtime.id; | 85 extensionId = chrome.runtime.id; |
58 window.requestAnimationFrame(function() { | 86 window.requestAnimationFrame(function() { |
59 var message_copy = base.deepCopy(message); | 87 var message_copy = base.deepCopy(message); |
60 chromeMocks.runtime.onMessage.mock$fire( | 88 chromeMocks.runtime.onMessage.mock$fire( |
61 message_copy, {id: extensionId}, responseCallback); | 89 message_copy, {id: extensionId}, responseCallback); |
62 }); | 90 }); |
63 }; | 91 }; |
64 | 92 |
93 /** @type {string} */ | |
65 chromeMocks.runtime.id = 'extensionId'; | 94 chromeMocks.runtime.id = 'extensionId'; |
66 | 95 |
96 /** @type {Object} */ | |
67 chromeMocks.storage = {}; | 97 chromeMocks.storage = {}; |
68 | 98 |
69 // Sample implementation of chrome.StorageArea according to | 99 // Sample implementation of chrome.StorageArea according to |
70 // https://developer.chrome.com/apps/storage#type-StorageArea | 100 // https://developer.chrome.com/apps/storage#type-StorageArea |
101 /** @constructor */ | |
71 chromeMocks.StorageArea = function() { | 102 chromeMocks.StorageArea = function() { |
103 /** @type {Object} */ | |
72 this.storage_ = {}; | 104 this.storage_ = {}; |
73 }; | 105 }; |
74 | 106 |
107 /** | |
108 * @param {!Object} keys | |
109 * @return {Array.<string>} | |
kelvinp
2015/02/18 18:41:30
dbeam recently switch all Array.<string> into Arra
garykac
2015/02/19 01:48:45
Done.
| |
110 */ | |
75 function getKeys(keys) { | 111 function getKeys(keys) { |
76 if (typeof keys === 'string') { | 112 if (typeof keys === 'string') { |
77 return [keys]; | 113 return [keys]; |
78 } else if (typeof keys === 'object') { | 114 } else if (typeof keys === 'object') { |
79 return Object.keys(keys); | 115 return Object.keys(keys); |
80 } | 116 } |
81 return []; | 117 return []; |
82 } | 118 } |
83 | 119 |
120 /** | |
121 * @param {!Object} keys | |
122 * @param {Function} onDone | |
123 */ | |
84 chromeMocks.StorageArea.prototype.get = function(keys, onDone) { | 124 chromeMocks.StorageArea.prototype.get = function(keys, onDone) { |
85 if (!keys) { | 125 if (!keys) { |
86 onDone(base.deepCopy(this.storage_)); | 126 onDone(base.deepCopy(this.storage_)); |
87 return; | 127 return; |
88 } | 128 } |
89 | 129 |
90 var result = (typeof keys === 'object') ? keys : {}; | 130 var result = (typeof keys === 'object') ? keys : {}; |
91 getKeys(keys).forEach(function(key) { | 131 getKeys(keys).forEach( |
92 if (key in this.storage_) { | 132 /** @param {string} key */ |
93 result[key] = base.deepCopy(this.storage_[key]); | 133 function(key) { |
94 } | 134 if (key in this.storage_) { |
95 }, this); | 135 result[key] = base.deepCopy(this.storage_[key]); |
136 } | |
137 }, this); | |
96 onDone(result); | 138 onDone(result); |
97 }; | 139 }; |
98 | 140 |
141 /** @param {Object} value */ | |
99 chromeMocks.StorageArea.prototype.set = function(value) { | 142 chromeMocks.StorageArea.prototype.set = function(value) { |
100 for (var key in value) { | 143 for (var key in value) { |
101 this.storage_[key] = base.deepCopy(value[key]); | 144 this.storage_[key] = base.deepCopy(value[key]); |
102 } | 145 } |
103 }; | 146 }; |
104 | 147 |
148 /** | |
149 * @param {!Object} keys | |
150 */ | |
105 chromeMocks.StorageArea.prototype.remove = function(keys) { | 151 chromeMocks.StorageArea.prototype.remove = function(keys) { |
106 getKeys(keys).forEach(function(key) { | 152 getKeys(keys).forEach( |
107 delete this.storage_[key]; | 153 /** @param {string} key */ |
108 }, this); | 154 function(key) { |
155 delete this.storage_[key]; | |
156 }, this); | |
109 }; | 157 }; |
110 | 158 |
111 chromeMocks.StorageArea.prototype.clear = function() { | 159 chromeMocks.StorageArea.prototype.clear = function() { |
112 this.storage_ = null; | 160 this.storage_ = null; |
113 }; | 161 }; |
114 | 162 |
163 /** @type {chromeMocks.StorageArea} */ | |
115 chromeMocks.storage.local = new chromeMocks.StorageArea(); | 164 chromeMocks.storage.local = new chromeMocks.StorageArea(); |
116 | 165 |
117 var originals_ = null; | 166 var originals_ = null; |
118 | 167 |
119 /** | 168 /** |
120 * Activates a list of Chrome components to mock | 169 * Activates a list of Chrome components to mock |
121 * @param {Array<string>} components | 170 * @param {Array<string>} components |
122 */ | 171 */ |
123 chromeMocks.activate = function(components) { | 172 chromeMocks.activate = function(components) { |
124 if (originals_) { | 173 if (originals_) { |
(...skipping 12 matching lines...) Expand all Loading... | |
137 chromeMocks.restore = function() { | 186 chromeMocks.restore = function() { |
138 if (!originals_) { | 187 if (!originals_) { |
139 throw new Error('You must call activate() before restore().'); | 188 throw new Error('You must call activate() before restore().'); |
140 } | 189 } |
141 for (var components in originals_) { | 190 for (var components in originals_) { |
142 chrome[components] = originals_[components]; | 191 chrome[components] = originals_[components]; |
143 } | 192 } |
144 originals_ = null; | 193 originals_ = null; |
145 }; | 194 }; |
146 | 195 |
147 scope.chromeMocks = chromeMocks; | 196 })(); |
148 | |
149 })(window); | |
OLD | NEW |