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 var chromeMocks = {}; | 10 var chromeMocks = {}; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 window.requestAnimationFrame(function() { | 96 window.requestAnimationFrame(function() { |
97 var message_copy = base.deepCopy(message); | 97 var message_copy = base.deepCopy(message); |
98 chromeMocks.runtime.onMessage.mock$fire( | 98 chromeMocks.runtime.onMessage.mock$fire( |
99 message_copy, {id: extensionId}, responseCallback); | 99 message_copy, {id: extensionId}, responseCallback); |
100 }); | 100 }); |
101 }; | 101 }; |
102 | 102 |
103 /** @type {string} */ | 103 /** @type {string} */ |
104 chromeMocks.runtime.id = 'extensionId'; | 104 chromeMocks.runtime.id = 'extensionId'; |
105 | 105 |
106 /** @type {Object} */ | 106 chromeMocks.runtime.lastError = { |
107 chromeMocks.storage = {}; | 107 /** @type {string|undefined} */ |
| 108 message: undefined |
| 109 }; |
| 110 |
108 | 111 |
109 // Sample implementation of chrome.StorageArea according to | 112 // Sample implementation of chrome.StorageArea according to |
110 // https://developer.chrome.com/apps/storage#type-StorageArea | 113 // https://developer.chrome.com/apps/storage#type-StorageArea |
111 /** @constructor */ | 114 /** @constructor */ |
112 chromeMocks.StorageArea = function() { | 115 chromeMocks.StorageArea = function() { |
113 /** @type {Object} */ | 116 /** @type {Object} */ |
114 this.storage_ = {}; | 117 this.storage_ = {}; |
115 }; | 118 }; |
116 | 119 |
117 /** | 120 /** |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 /** @param {string} key */ | 166 /** @param {string} key */ |
164 function(key) { | 167 function(key) { |
165 delete this.storage_[key]; | 168 delete this.storage_[key]; |
166 }, this); | 169 }, this); |
167 }; | 170 }; |
168 | 171 |
169 chromeMocks.StorageArea.prototype.clear = function() { | 172 chromeMocks.StorageArea.prototype.clear = function() { |
170 this.storage_ = null; | 173 this.storage_ = null; |
171 }; | 174 }; |
172 | 175 |
| 176 /** @type {Object} */ |
| 177 chromeMocks.storage = {}; |
| 178 |
173 /** @type {chromeMocks.StorageArea} */ | 179 /** @type {chromeMocks.StorageArea} */ |
174 chromeMocks.storage.local = new chromeMocks.StorageArea(); | 180 chromeMocks.storage.local = new chromeMocks.StorageArea(); |
175 | 181 |
| 182 |
| 183 /** @constructor */ |
| 184 chromeMocks.Identity = function() { |
| 185 /** @private {string|undefined} */ |
| 186 this.token_ = undefined; |
| 187 }; |
| 188 |
| 189 chromeMocks.Identity.prototype.getAuthToken = function(options, callback) { |
| 190 // Don't use setTimeout because sinon mocks it. |
| 191 window.requestAnimationFrame(callback.bind(null, this.token_)); |
| 192 }; |
| 193 |
| 194 /** @param {string} token */ |
| 195 chromeMocks.Identity.prototype.mock$setToken = function(token) { |
| 196 this.token_ = token; |
| 197 }; |
| 198 |
| 199 chromeMocks.Identity.prototype.mock$clearToken = function() { |
| 200 this.token_ = undefined; |
| 201 }; |
| 202 |
| 203 /** @type {chromeMocks.Identity} */ |
| 204 chromeMocks.identity = new chromeMocks.Identity(); |
| 205 |
| 206 |
176 var originals_ = null; | 207 var originals_ = null; |
177 | 208 |
178 /** | 209 /** |
179 * Activates a list of Chrome components to mock | 210 * Activates a list of Chrome components to mock |
180 * @param {Array<string>} components | 211 * @param {Array<string>} components |
181 */ | 212 */ |
182 chromeMocks.activate = function(components) { | 213 chromeMocks.activate = function(components) { |
183 if (originals_) { | 214 if (originals_) { |
184 throw new Error('chromeMocks.activate() can only be called once.'); | 215 throw new Error('chromeMocks.activate() can only be called once.'); |
185 } | 216 } |
(...skipping 11 matching lines...) Expand all Loading... |
197 if (!originals_) { | 228 if (!originals_) { |
198 throw new Error('You must call activate() before restore().'); | 229 throw new Error('You must call activate() before restore().'); |
199 } | 230 } |
200 for (var components in originals_) { | 231 for (var components in originals_) { |
201 chrome[components] = originals_[components]; | 232 chrome[components] = originals_[components]; |
202 } | 233 } |
203 originals_ = null; | 234 originals_ = null; |
204 }; | 235 }; |
205 | 236 |
206 })(); | 237 })(); |
OLD | NEW |