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 syncFileSystem: { |
| 44 onFileStatusChanged: { |
| 45 addListener: function(listener) { |
| 46 this.dispatch = listener; |
| 47 } |
| 48 } |
| 49 }, |
| 50 app: { |
| 51 runtime: { |
| 52 onLaunched: { |
| 53 addListener: function(listener) { |
| 54 } |
| 55 } |
| 56 } |
| 57 }, |
| 58 alarms: { |
| 59 onAlarm: { |
| 60 addListener: function(listener) { |
| 61 } |
| 62 } |
| 63 }, |
| 64 wallpaperPrivate: { |
| 65 getStrings: function(callback) { |
| 66 callback({isExperimental: false}); |
| 67 } |
| 68 } |
| 69 }; |
| 70 |
| 71 (function (exports) { |
| 72 var originalXMLHttpRequest = window.XMLHttpRequest; |
| 73 |
| 74 // Mock XMLHttpRequest. It dispatches a 'load' event immediately with status |
| 75 // equals to 200 in function |send|. |
| 76 function MockXMLHttpRequest() { |
| 77 } |
| 78 |
| 79 MockXMLHttpRequest.prototype = { |
| 80 responseType: null, |
| 81 url: null, |
| 82 |
| 83 send: function(data) { |
| 84 this.status = 200; |
| 85 this.dispatchEvent('load'); |
| 86 }, |
| 87 |
| 88 addEventListener: function(type, listener) { |
| 89 this.eventListeners = this.eventListeners || {}; |
| 90 this.eventListeners[type] = this.eventListeners[type] || []; |
| 91 this.eventListeners[type].push(listener); |
| 92 }, |
| 93 |
| 94 removeEventListener: function (type, listener) { |
| 95 var listeners = this.eventListeners && this.eventListeners[type] || []; |
| 96 |
| 97 for (var i = 0; i < listeners.length; ++i) { |
| 98 if (listeners[i] == listener) { |
| 99 return listeners.splice(i, 1); |
| 100 } |
| 101 } |
| 102 }, |
| 103 |
| 104 dispatchEvent: function(type) { |
| 105 var listeners = this.eventListeners && this.eventListeners[type] || []; |
| 106 |
| 107 if (/test.jpg$/g.test(this.url)) { |
| 108 this.response = TestConstants.image; |
| 109 } else { |
| 110 this.response = ''; |
| 111 } |
| 112 |
| 113 for (var i = 0; i < listeners.length; ++i) |
| 114 listeners[i].call(this, new Event(type)); |
| 115 }, |
| 116 |
| 117 open: function(method, url, async) { |
| 118 this.url = url; |
| 119 } |
| 120 }; |
| 121 |
| 122 function installMockXMLHttpRequest() { |
| 123 window['XMLHttpRequest'] = MockXMLHttpRequest; |
| 124 }; |
| 125 |
| 126 function uninstallMockXMLHttpRequest() { |
| 127 window['XMLHttpRequest'] = originalXMLHttpRequest; |
| 128 }; |
| 129 |
| 130 exports.installMockXMLHttpRequest = installMockXMLHttpRequest; |
| 131 exports.uninstallMockXMLHttpRequest = uninstallMockXMLHttpRequest; |
| 132 |
| 133 })(window); |
OLD | NEW |