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 = deepCopy(message); | |
Jamie
2015/01/28 18:57:47
Where is deepCopy defined? I don't see it in trunk
| |
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) { | 75 function deepCopy(value) { |
59 return JSON.parse(JSON.stringify(value)); | 76 return JSON.parse(JSON.stringify(value)); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 } | 144 } |
128 for (var components in originals_) { | 145 for (var components in originals_) { |
129 chrome[components] = originals_[components]; | 146 chrome[components] = originals_[components]; |
130 } | 147 } |
131 originals_ = null; | 148 originals_ = null; |
132 }; | 149 }; |
133 | 150 |
134 scope.chromeMocks = chromeMocks; | 151 scope.chromeMocks = chromeMocks; |
135 | 152 |
136 })(window); | 153 })(window); |
OLD | NEW |