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 10 matching lines...) Expand all Loading... |
21 | 21 |
22 chromeMocks.Event.prototype.removeListener = function(callback) { | 22 chromeMocks.Event.prototype.removeListener = function(callback) { |
23 for (var i = 0; i < this.listeners_.length; i++) { | 23 for (var i = 0; i < this.listeners_.length; i++) { |
24 if (this.listeners_[i] === callback) { | 24 if (this.listeners_[i] === callback) { |
25 this.listeners_.splice(i, 1); | 25 this.listeners_.splice(i, 1); |
26 break; | 26 break; |
27 } | 27 } |
28 } | 28 } |
29 }; | 29 }; |
30 | 30 |
31 chromeMocks.Event.prototype.mock$fire = function(data) { | 31 chromeMocks.Event.prototype.mock$fire = function(var_args) { |
| 32 var params = Array.prototype.slice.call(arguments); |
32 this.listeners_.forEach(function(listener){ | 33 this.listeners_.forEach(function(listener){ |
33 listener(data); | 34 listener.apply(null, params); |
34 }); | 35 }); |
35 }; | 36 }; |
36 | 37 |
37 chromeMocks.runtime = {}; | 38 chromeMocks.runtime = {}; |
38 | 39 |
39 chromeMocks.runtime.Port = function() { | 40 chromeMocks.runtime.Port = function() { |
40 this.onMessage = new chromeMocks.Event(); | 41 this.onMessage = new chromeMocks.Event(); |
41 this.onDisconnect = new chromeMocks.Event(); | 42 this.onDisconnect = new chromeMocks.Event(); |
42 | 43 |
43 this.name = ''; | 44 this.name = ''; |
44 this.sender = null; | 45 this.sender = null; |
45 }; | 46 }; |
46 | 47 |
47 chromeMocks.runtime.Port.prototype.disconnect = function() {}; | 48 chromeMocks.runtime.Port.prototype.disconnect = function() {}; |
48 chromeMocks.runtime.Port.prototype.postMessage = function() {}; | 49 chromeMocks.runtime.Port.prototype.postMessage = function() {}; |
49 | 50 |
| 51 chromeMocks.runtime.onMessage = new chromeMocks.Event(); |
| 52 chromeMocks.runtime.sendMessage = function(extensionId, message, |
| 53 responseCallback) { |
| 54 base.debug.assert( |
| 55 extensionId === null, |
| 56 'The mock only supports sending messages to the same extension.'); |
| 57 extensionId = chrome.runtime.id; |
| 58 window.requestAnimationFrame(function() { |
| 59 var message_copy = base.deepCopy(message); |
| 60 chromeMocks.runtime.onMessage.mock$fire( |
| 61 message_copy, {id: extensionId}, responseCallback); |
| 62 }); |
| 63 }; |
| 64 |
| 65 chromeMocks.runtime.id = 'extensionId'; |
| 66 |
50 chromeMocks.storage = {}; | 67 chromeMocks.storage = {}; |
51 | 68 |
52 // Sample implementation of chrome.StorageArea according to | 69 // Sample implementation of chrome.StorageArea according to |
53 // https://developer.chrome.com/apps/storage#type-StorageArea | 70 // https://developer.chrome.com/apps/storage#type-StorageArea |
54 chromeMocks.StorageArea = function() { | 71 chromeMocks.StorageArea = function() { |
55 this.storage_ = {}; | 72 this.storage_ = {}; |
56 }; | 73 }; |
57 | 74 |
58 function deepCopy(value) { | |
59 return JSON.parse(JSON.stringify(value)); | |
60 } | |
61 | |
62 function getKeys(keys) { | 75 function getKeys(keys) { |
63 if (typeof keys === 'string') { | 76 if (typeof keys === 'string') { |
64 return [keys]; | 77 return [keys]; |
65 } else if (typeof keys === 'object') { | 78 } else if (typeof keys === 'object') { |
66 return Object.keys(keys); | 79 return Object.keys(keys); |
67 } | 80 } |
68 return []; | 81 return []; |
69 } | 82 } |
70 | 83 |
71 chromeMocks.StorageArea.prototype.get = function(keys, onDone) { | 84 chromeMocks.StorageArea.prototype.get = function(keys, onDone) { |
72 if (!keys) { | 85 if (!keys) { |
73 onDone(deepCopy(this.storage_)); | 86 onDone(base.deepCopy(this.storage_)); |
74 return; | 87 return; |
75 } | 88 } |
76 | 89 |
77 var result = (typeof keys === 'object') ? keys : {}; | 90 var result = (typeof keys === 'object') ? keys : {}; |
78 getKeys(keys).forEach(function(key) { | 91 getKeys(keys).forEach(function(key) { |
79 if (key in this.storage_) { | 92 if (key in this.storage_) { |
80 result[key] = deepCopy(this.storage_[key]); | 93 result[key] = base.deepCopy(this.storage_[key]); |
81 } | 94 } |
82 }, this); | 95 }, this); |
83 onDone(result); | 96 onDone(result); |
84 }; | 97 }; |
85 | 98 |
86 chromeMocks.StorageArea.prototype.set = function(value) { | 99 chromeMocks.StorageArea.prototype.set = function(value) { |
87 for (var key in value) { | 100 for (var key in value) { |
88 this.storage_[key] = deepCopy(value[key]); | 101 this.storage_[key] = base.deepCopy(value[key]); |
89 } | 102 } |
90 }; | 103 }; |
91 | 104 |
92 chromeMocks.StorageArea.prototype.remove = function(keys) { | 105 chromeMocks.StorageArea.prototype.remove = function(keys) { |
93 getKeys(keys).forEach(function(key) { | 106 getKeys(keys).forEach(function(key) { |
94 delete this.storage_[key]; | 107 delete this.storage_[key]; |
95 }, this); | 108 }, this); |
96 }; | 109 }; |
97 | 110 |
98 chromeMocks.StorageArea.prototype.clear = function() { | 111 chromeMocks.StorageArea.prototype.clear = function() { |
(...skipping 28 matching lines...) Expand all Loading... |
127 } | 140 } |
128 for (var components in originals_) { | 141 for (var components in originals_) { |
129 chrome[components] = originals_[components]; | 142 chrome[components] = originals_[components]; |
130 } | 143 } |
131 originals_ = null; | 144 originals_ = null; |
132 }; | 145 }; |
133 | 146 |
134 scope.chromeMocks = chromeMocks; | 147 scope.chromeMocks = chromeMocks; |
135 | 148 |
136 })(window); | 149 })(window); |
OLD | NEW |