OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 | |
6 var TestConstants = { | |
7 wallpaperURL: 'https://test.com/test.jpg', | |
8 // A dummy string which is used to mock an image. | |
9 image: '*#*@#&' | |
10 }; | |
11 | |
12 // Mock a few chrome apis. | |
13 var chrome = { | |
14 storage: { | |
15 local: { | |
16 get: function(key, callback) { | |
17 var items = {}; | |
18 switch (key) { | |
19 case Constants.AccessLocalWallpaperInfoKey: | |
20 items[Constants.AccessLocalWallpaperInfoKey] = { | |
21 'url': 'dummy', | |
22 'layout': 'dummy', | |
23 'source': 'dummy' | |
24 }; | |
25 } | |
26 callback(items); | |
27 }, | |
28 set: function(items, callback) { | |
29 } | |
30 }, | |
31 sync: { | |
32 get: function(key, callback) { | |
33 }, | |
34 set: function(items, callback) { | |
35 } | |
36 }, | |
37 onChanged: { | |
38 addListener: function(listener) { | |
39 this.dispatch = listener; | |
40 } | |
41 } | |
42 }, | |
43 app: { | |
44 runtime: { | |
45 onLaunched: { | |
46 addListener: function(listener) { | |
47 } | |
48 } | |
49 } | |
50 }, | |
51 alarms: { | |
52 onAlarm: { | |
53 addListener: function(listener) { | |
54 } | |
55 } | |
56 } | |
57 }; | |
58 | |
59 (function (exports) { | |
60 var originalXMLHttpRequest = window.XMLHttpRequest; | |
61 | |
62 // Mock XMLHttpRequest. It dispatches a 'load' event immediately with status | |
63 // equals to 200 in function |send|. | |
64 function MockXMLHttpRequest() { | |
65 } | |
66 | |
67 MockXMLHttpRequest.prototype = { | |
68 responseType: null, | |
69 url: null, | |
70 | |
71 send: function(data) { | |
72 this.status = 200; | |
73 this.dispatchEvent('load'); | |
74 }, | |
75 | |
76 addEventListener: function(type, listener) { | |
77 this.eventListeners = this.eventListeners || {}; | |
78 this.eventListeners[type] = this.eventListeners[type] || []; | |
79 this.eventListeners[type].push(listener); | |
80 }, | |
81 | |
82 removeEventListener: function (type, listener) { | |
83 var listeners = this.eventListeners && this.eventListeners[type] || []; | |
84 | |
85 for (var i = 0; i < listeners.length; ++i) { | |
86 if (listeners[i] == listener) { | |
87 return listeners.splice(i, 1); | |
88 } | |
89 } | |
90 }, | |
91 | |
92 dispatchEvent: function(type) { | |
93 var listeners = this.eventListeners && this.eventListeners[type] || []; | |
94 | |
95 if (/test.jpg$/g.test(this.url)) { | |
96 this.response = TestConstants.image; | |
97 } else { | |
98 this.response = ''; | |
99 } | |
100 | |
101 for (var i = 0; i < listeners.length; ++i) | |
102 listeners[i].call(this, new Event(type)); | |
103 }, | |
104 | |
105 open: function(method, url, async) { | |
106 this.url = url; | |
107 } | |
108 }; | |
109 | |
110 function installMockXMLHttpRequest() { | |
111 window['XMLHttpRequest'] = MockXMLHttpRequest; | |
112 }; | |
113 | |
114 function uninstallMockXMLHttpRequest() { | |
115 window['XMLHttpRequest'] = originalXMLHttpRequest; | |
116 }; | |
117 | |
118 exports.installMockXMLHttpRequest = installMockXMLHttpRequest; | |
119 exports.uninstallMockXMLHttpRequest = uninstallMockXMLHttpRequest; | |
120 | |
121 })(window); | |
OLD | NEW |