| 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){ | |
| 11 | |
| 12 var chromeMocks = {}; | 10 var chromeMocks = {}; |
| 13 | 11 |
| 12 (function(){ |
| 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(); |
| 73 |
| 74 /** |
| 75 * @param {string?} extensionId |
| 76 * @param {*} message |
| 77 * @param {function(*)=} responseCallback |
| 78 */ |
| 52 chromeMocks.runtime.sendMessage = function(extensionId, message, | 79 chromeMocks.runtime.sendMessage = function(extensionId, message, |
| 53 responseCallback) { | 80 responseCallback) { |
| 54 base.debug.assert( | 81 base.debug.assert( |
| 55 extensionId === null, | 82 extensionId === null, |
| 56 'The mock only supports sending messages to the same extension.'); | 83 'The mock only supports sending messages to the same extension.'); |
| 57 extensionId = chrome.runtime.id; | 84 extensionId = chrome.runtime.id; |
| 58 window.requestAnimationFrame(function() { | 85 window.requestAnimationFrame(function() { |
| 59 var message_copy = base.deepCopy(message); | 86 var message_copy = base.deepCopy(message); |
| 60 chromeMocks.runtime.onMessage.mock$fire( | 87 chromeMocks.runtime.onMessage.mock$fire( |
| 61 message_copy, {id: extensionId}, responseCallback); | 88 message_copy, {id: extensionId}, responseCallback); |
| 62 }); | 89 }); |
| 63 }; | 90 }; |
| 64 | 91 |
| 92 /** @type {string} */ |
| 65 chromeMocks.runtime.id = 'extensionId'; | 93 chromeMocks.runtime.id = 'extensionId'; |
| 66 | 94 |
| 95 /** @type {Object} */ |
| 67 chromeMocks.storage = {}; | 96 chromeMocks.storage = {}; |
| 68 | 97 |
| 69 // Sample implementation of chrome.StorageArea according to | 98 // Sample implementation of chrome.StorageArea according to |
| 70 // https://developer.chrome.com/apps/storage#type-StorageArea | 99 // https://developer.chrome.com/apps/storage#type-StorageArea |
| 100 /** @constructor */ |
| 71 chromeMocks.StorageArea = function() { | 101 chromeMocks.StorageArea = function() { |
| 102 /** @type {Object} */ |
| 72 this.storage_ = {}; | 103 this.storage_ = {}; |
| 73 }; | 104 }; |
| 74 | 105 |
| 106 /** |
| 107 * @param {!Object} keys |
| 108 * @return {Array<string>} |
| 109 */ |
| 75 function getKeys(keys) { | 110 function getKeys(keys) { |
| 76 if (typeof keys === 'string') { | 111 if (typeof keys === 'string') { |
| 77 return [keys]; | 112 return [keys]; |
| 78 } else if (typeof keys === 'object') { | 113 } else if (typeof keys === 'object') { |
| 79 return Object.keys(keys); | 114 return Object.keys(keys); |
| 80 } | 115 } |
| 81 return []; | 116 return []; |
| 82 } | 117 } |
| 83 | 118 |
| 119 /** |
| 120 * @param {!Object} keys |
| 121 * @param {Function} onDone |
| 122 */ |
| 84 chromeMocks.StorageArea.prototype.get = function(keys, onDone) { | 123 chromeMocks.StorageArea.prototype.get = function(keys, onDone) { |
| 85 if (!keys) { | 124 if (!keys) { |
| 86 onDone(base.deepCopy(this.storage_)); | 125 onDone(base.deepCopy(this.storage_)); |
| 87 return; | 126 return; |
| 88 } | 127 } |
| 89 | 128 |
| 90 var result = (typeof keys === 'object') ? keys : {}; | 129 var result = (typeof keys === 'object') ? keys : {}; |
| 91 getKeys(keys).forEach(function(key) { | 130 getKeys(keys).forEach( |
| 92 if (key in this.storage_) { | 131 /** @param {string} key */ |
| 93 result[key] = base.deepCopy(this.storage_[key]); | 132 function(key) { |
| 94 } | 133 if (key in this.storage_) { |
| 95 }, this); | 134 result[key] = base.deepCopy(this.storage_[key]); |
| 135 } |
| 136 }, this); |
| 96 onDone(result); | 137 onDone(result); |
| 97 }; | 138 }; |
| 98 | 139 |
| 140 /** @param {Object} value */ |
| 99 chromeMocks.StorageArea.prototype.set = function(value) { | 141 chromeMocks.StorageArea.prototype.set = function(value) { |
| 100 for (var key in value) { | 142 for (var key in value) { |
| 101 this.storage_[key] = base.deepCopy(value[key]); | 143 this.storage_[key] = base.deepCopy(value[key]); |
| 102 } | 144 } |
| 103 }; | 145 }; |
| 104 | 146 |
| 147 /** |
| 148 * @param {!Object} keys |
| 149 */ |
| 105 chromeMocks.StorageArea.prototype.remove = function(keys) { | 150 chromeMocks.StorageArea.prototype.remove = function(keys) { |
| 106 getKeys(keys).forEach(function(key) { | 151 getKeys(keys).forEach( |
| 107 delete this.storage_[key]; | 152 /** @param {string} key */ |
| 108 }, this); | 153 function(key) { |
| 154 delete this.storage_[key]; |
| 155 }, this); |
| 109 }; | 156 }; |
| 110 | 157 |
| 111 chromeMocks.StorageArea.prototype.clear = function() { | 158 chromeMocks.StorageArea.prototype.clear = function() { |
| 112 this.storage_ = null; | 159 this.storage_ = null; |
| 113 }; | 160 }; |
| 114 | 161 |
| 162 /** @type {chromeMocks.StorageArea} */ |
| 115 chromeMocks.storage.local = new chromeMocks.StorageArea(); | 163 chromeMocks.storage.local = new chromeMocks.StorageArea(); |
| 116 | 164 |
| 117 var originals_ = null; | 165 var originals_ = null; |
| 118 | 166 |
| 119 /** | 167 /** |
| 120 * Activates a list of Chrome components to mock | 168 * Activates a list of Chrome components to mock |
| 121 * @param {Array<string>} components | 169 * @param {Array<string>} components |
| 122 */ | 170 */ |
| 123 chromeMocks.activate = function(components) { | 171 chromeMocks.activate = function(components) { |
| 124 if (originals_) { | 172 if (originals_) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 137 chromeMocks.restore = function() { | 185 chromeMocks.restore = function() { |
| 138 if (!originals_) { | 186 if (!originals_) { |
| 139 throw new Error('You must call activate() before restore().'); | 187 throw new Error('You must call activate() before restore().'); |
| 140 } | 188 } |
| 141 for (var components in originals_) { | 189 for (var components in originals_) { |
| 142 chrome[components] = originals_[components]; | 190 chrome[components] = originals_[components]; |
| 143 } | 191 } |
| 144 originals_ = null; | 192 originals_ = null; |
| 145 }; | 193 }; |
| 146 | 194 |
| 147 scope.chromeMocks = chromeMocks; | 195 })(); |
| 148 | |
| 149 })(window); | |
| OLD | NEW |