Chromium Code Reviews| 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 55 function(listener){ | 55 function(listener){ |
| 56 listener.apply(null, params); | 56 listener.apply(null, params); |
| 57 }); | 57 }); |
| 58 }; | 58 }; |
| 59 | 59 |
| 60 /** @type {Object} */ | 60 /** @type {Object} */ |
| 61 chromeMocks.runtime = {}; | 61 chromeMocks.runtime = {}; |
| 62 | 62 |
| 63 /** @constructor */ | 63 /** @constructor */ |
| 64 chromeMocks.runtime.Port = function() { | 64 chromeMocks.runtime.Port = function() { |
| 65 /** @const */ | |
| 65 this.onMessage = new chromeMocks.Event(); | 66 this.onMessage = new chromeMocks.Event(); |
| 67 | |
| 68 /** @const */ | |
| 66 this.onDisconnect = new chromeMocks.Event(); | 69 this.onDisconnect = new chromeMocks.Event(); |
| 67 | 70 |
| 68 /** @type {string} */ | 71 /** @type {string} */ |
| 69 this.name = ''; | 72 this.name = ''; |
| 70 | 73 |
| 71 /** @type {chrome.runtime.MessageSender} */ | 74 /** @type {chrome.runtime.MessageSender} */ |
| 72 this.sender = null; | 75 this.sender = null; |
| 73 }; | 76 }; |
| 74 | 77 |
| 75 chromeMocks.runtime.Port.prototype.disconnect = function() {}; | 78 chromeMocks.runtime.Port.prototype.disconnect = function() {}; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 93 extensionId === null, | 96 extensionId === null, |
| 94 'The mock only supports sending messages to the same extension.'); | 97 'The mock only supports sending messages to the same extension.'); |
| 95 extensionId = chrome.runtime.id; | 98 extensionId = chrome.runtime.id; |
| 96 window.requestAnimationFrame(function() { | 99 window.requestAnimationFrame(function() { |
| 97 var message_copy = base.deepCopy(message); | 100 var message_copy = base.deepCopy(message); |
| 98 chromeMocks.runtime.onMessage.mock$fire( | 101 chromeMocks.runtime.onMessage.mock$fire( |
| 99 message_copy, {id: extensionId}, responseCallback); | 102 message_copy, {id: extensionId}, responseCallback); |
| 100 }); | 103 }); |
| 101 }; | 104 }; |
| 102 | 105 |
| 106 /** | |
| 107 * Always returns the same mock port for given application name. | |
| 108 * @param {string} application | |
| 109 * @return {chromeMocks.runtime.Port} | |
| 110 */ | |
| 111 chromeMocks.runtime.connectNative = function(application) { | |
| 112 var port = nativePorts[application]; | |
| 113 if (port == null) { | |
|
kelvinp
2015/04/06 17:25:10
use ===
John Williams
2015/04/06 19:14:59
Done.
| |
| 114 port = new chromeMocks.runtime.Port(); | |
| 115 port.name = application; | |
| 116 nativePorts[application] = port; | |
| 117 } | |
| 118 return port; | |
| 119 }; | |
| 120 | |
| 121 /** @const {Object<string,!chromeMocks.runtime.Port>} */ | |
| 122 var nativePorts = null; | |
| 123 | |
| 103 /** @type {string} */ | 124 /** @type {string} */ |
| 104 chromeMocks.runtime.id = 'extensionId'; | 125 chromeMocks.runtime.id = 'extensionId'; |
| 105 | 126 |
| 106 /** @type {Object} */ | 127 /** @type {Object} */ |
| 107 chromeMocks.runtime.lastError = { | 128 chromeMocks.runtime.lastError = { |
| 108 /** @type {string|undefined} */ | 129 /** @type {string|undefined} */ |
| 109 message: undefined | 130 message: undefined |
| 110 }; | 131 }; |
| 111 | 132 |
| 112 | 133 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 | 239 |
| 219 /** | 240 /** |
| 220 * Activates a list of Chrome components to mock | 241 * Activates a list of Chrome components to mock |
| 221 * @param {Array<string>} components | 242 * @param {Array<string>} components |
| 222 */ | 243 */ |
| 223 chromeMocks.activate = function(components) { | 244 chromeMocks.activate = function(components) { |
| 224 if (originals_) { | 245 if (originals_) { |
| 225 throw new Error('chromeMocks.activate() can only be called once.'); | 246 throw new Error('chromeMocks.activate() can only be called once.'); |
| 226 } | 247 } |
| 227 originals_ = {}; | 248 originals_ = {}; |
| 249 nativePorts = {}; | |
| 228 components.forEach(function(component) { | 250 components.forEach(function(component) { |
| 229 if (!chromeMocks[component]) { | 251 if (!chromeMocks[component]) { |
| 230 throw new Error('No mocks defined for chrome.' + component); | 252 throw new Error('No mocks defined for chrome.' + component); |
| 231 } | 253 } |
| 232 originals_[component] = chrome[component]; | 254 originals_[component] = chrome[component]; |
| 233 chrome[component] = chromeMocks[component]; | 255 chrome[component] = chromeMocks[component]; |
| 234 }); | 256 }); |
| 235 }; | 257 }; |
| 236 | 258 |
| 237 chromeMocks.restore = function() { | 259 chromeMocks.restore = function() { |
| 238 if (!originals_) { | 260 if (!originals_) { |
| 239 throw new Error('You must call activate() before restore().'); | 261 throw new Error('You must call activate() before restore().'); |
| 240 } | 262 } |
| 241 for (var components in originals_) { | 263 for (var components in originals_) { |
| 242 chrome[components] = originals_[components]; | 264 chrome[components] = originals_[components]; |
| 243 } | 265 } |
| 244 originals_ = null; | 266 originals_ = null; |
| 267 nativePorts = null; | |
| 245 }; | 268 }; |
| 246 | 269 |
| 247 })(); | 270 })(); |
| OLD | NEW |