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(scope){ |
(...skipping 29 matching lines...) Expand all Loading... |
40 this.onMessage = new chromeMocks.Event(); | 40 this.onMessage = new chromeMocks.Event(); |
41 this.onDisconnect = new chromeMocks.Event(); | 41 this.onDisconnect = new chromeMocks.Event(); |
42 | 42 |
43 this.name = ''; | 43 this.name = ''; |
44 this.sender = null; | 44 this.sender = null; |
45 }; | 45 }; |
46 | 46 |
47 chromeMocks.runtime.Port.prototype.disconnect = function() {}; | 47 chromeMocks.runtime.Port.prototype.disconnect = function() {}; |
48 chromeMocks.runtime.Port.prototype.postMessage = function() {}; | 48 chromeMocks.runtime.Port.prototype.postMessage = function() {}; |
49 | 49 |
| 50 chromeMocks.storage = {}; |
| 51 |
| 52 // Sample implementation of chrome.StorageArea according to |
| 53 // https://developer.chrome.com/apps/storage#type-StorageArea |
| 54 chromeMocks.StorageArea = function() { |
| 55 this.storage_ = {}; |
| 56 }; |
| 57 |
| 58 function deepCopy(value) { |
| 59 return JSON.parse(JSON.stringify(value)); |
| 60 } |
| 61 |
| 62 function getKeys(keys) { |
| 63 if (typeof keys === 'string') { |
| 64 return [keys]; |
| 65 } else if (typeof keys === 'object') { |
| 66 return Object.keys(keys); |
| 67 } |
| 68 return []; |
| 69 } |
| 70 |
| 71 chromeMocks.StorageArea.prototype.get = function(keys, onDone) { |
| 72 if (!keys) { |
| 73 onDone(deepCopy(this.storage_)); |
| 74 return; |
| 75 } |
| 76 |
| 77 var result = (typeof keys === 'object') ? keys : {}; |
| 78 getKeys(keys).forEach(function(key) { |
| 79 if (key in this.storage_) { |
| 80 result[key] = deepCopy(this.storage_[key]); |
| 81 } |
| 82 }, this); |
| 83 onDone(result); |
| 84 }; |
| 85 |
| 86 chromeMocks.StorageArea.prototype.set = function(value) { |
| 87 for (var key in value) { |
| 88 this.storage_[key] = deepCopy(value[key]); |
| 89 } |
| 90 }; |
| 91 |
| 92 chromeMocks.StorageArea.prototype.remove = function(keys) { |
| 93 getKeys(keys).forEach(function(key) { |
| 94 delete this.storage_[key]; |
| 95 }, this); |
| 96 }; |
| 97 |
| 98 chromeMocks.StorageArea.prototype.clear = function() { |
| 99 this.storage_ = null; |
| 100 }; |
| 101 |
| 102 chromeMocks.storage.local = new chromeMocks.StorageArea(); |
| 103 |
| 104 var originals_ = null; |
| 105 |
| 106 /** |
| 107 * Activates a list of Chrome components to mock |
| 108 * @param {Array.<string>} components |
| 109 */ |
| 110 chromeMocks.activate = function(components) { |
| 111 if (originals_) { |
| 112 throw new Error('chromeMocks.activate() can only be called once.'); |
| 113 } |
| 114 originals_ = {}; |
| 115 components.forEach(function(component) { |
| 116 if (!chromeMocks[component]) { |
| 117 throw new Error('No mocks defined for chrome.' + component); |
| 118 } |
| 119 originals_[component] = chrome[component]; |
| 120 chrome[component] = chromeMocks[component]; |
| 121 }); |
| 122 }; |
| 123 |
| 124 chromeMocks.restore = function() { |
| 125 if (!originals_) { |
| 126 throw new Error('You must call activate() before restore().'); |
| 127 } |
| 128 for (var components in originals_) { |
| 129 chrome[components] = originals_[components]; |
| 130 } |
| 131 originals_ = null; |
| 132 }; |
| 133 |
50 scope.chromeMocks = chromeMocks; | 134 scope.chromeMocks = chromeMocks; |
51 | 135 |
52 })(window); | 136 })(window); |
OLD | NEW |