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 | 5 |
6 var TestConstants = { | 6 var TestConstants = { |
7 wallpaperURL: 'https://test.com/test.jpg', | 7 wallpaperURL: 'https://test.com/test.jpg', |
8 // A dummy string which is used to mock an image. | 8 // A dummy string which is used to mock an image. |
9 image: '*#*@#&' | 9 IMAGE: '*#*@#&', |
| 10 // A dummy array which is used to mock the file content. |
| 11 FILESTRING: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] |
| 12 }; |
| 13 |
| 14 // mock FileReader object in HTML5 File System |
| 15 function FileReader() { |
| 16 this.result = ''; |
| 17 this.onloadend = function() { |
| 18 }; |
| 19 this.readAsArrayBuffer = function(mockFile) { |
| 20 this.result = mockFile; |
| 21 this.onloadend(); |
| 22 } |
| 23 } |
| 24 |
| 25 // Mock localFS handler |
| 26 var mockLocalFS = { |
| 27 root: { |
| 28 dirList: [], |
| 29 rootFileList: [], |
| 30 getDirectory: function(dir, isCreate, success, failure) { |
| 31 for(var i = 0; i < this.dirList.length; i++) { |
| 32 if (this.dirList[i].name == dir) { |
| 33 success(this.dirList[i]); |
| 34 return; |
| 35 } |
| 36 } |
| 37 if (!isCreate.create) { |
| 38 if (failure) |
| 39 failure('DIR_NOT_FOUND'); |
| 40 } else { |
| 41 this.dirList.push(new DirEntry(dir)); |
| 42 success(this.dirList[this.dirList.length - 1]); |
| 43 } |
| 44 }, |
| 45 getFile: function(fileName, isCreate, success, failure) { |
| 46 if (fileName[0] == '/') |
| 47 fileName = fileName.substr(1); |
| 48 if (fileName.split('/').length == 1) { |
| 49 for(var i = 0; i < this.rootFileList.length; i++) { |
| 50 if (fileName == this.rootFileList[i].name) { |
| 51 success(this.rootFileList[i]); |
| 52 return; |
| 53 } |
| 54 } |
| 55 if (!isCreate.create) { |
| 56 if (failure) |
| 57 failure('FILE_NOT_FOUND'); |
| 58 } else { |
| 59 this.rootFileList.push(new FileEntry(fileName)); |
| 60 success(this.rootFileList[this.rootFileList.length - 1]); |
| 61 } |
| 62 } else if (fileName.split('/').length == 2) { |
| 63 var realDirName = fileName.split('/')[0]; |
| 64 var realFileName = fileName.split('/')[1]; |
| 65 var getDirSuccess = function(dirEntry) { |
| 66 dirEntry.getFile(realFileName, isCreate, success, failure); |
| 67 }; |
| 68 this.getDirectory(realDirName, {create: false}, |
| 69 getDirSuccess, failure); |
| 70 } else { |
| 71 console.error('Only support one level deep subdirectory') |
| 72 } |
| 73 } |
| 74 }, |
| 75 /** |
| 76 * Create a new file in mockLocalFS. |
| 77 * @param {string} fileName File name that to be created. |
| 78 * @return {FileEntry} Handle of the new file |
| 79 */ |
| 80 mockTestFile: function(fileName) { |
| 81 var mockFile; |
| 82 if (fileName[0] == '/') |
| 83 fileName = fileName.substr(1); |
| 84 if (fileName.split('/').length == 1) { |
| 85 mockFile = new FileEntry(fileName); |
| 86 this.root.rootFileList.push(mockFile); |
| 87 } else if (fileName.split('/').length == 2) { |
| 88 var realDirName = fileName.split('/')[0]; |
| 89 var realFileName = fileName.split('/')[1]; |
| 90 var getDirSuccess = function(dirEntry) { |
| 91 dirEntry.getFile(realFileName, {create: true}, |
| 92 function(fe) {mockFile = fe;} ); |
| 93 }; |
| 94 this.root.getDirectory(realDirName, {create: true}, getDirSuccess); |
| 95 } else { |
| 96 console.error('Only support one-level fileSystem mock') |
| 97 } |
| 98 return mockFile; |
| 99 }, |
| 100 /** |
| 101 * Delete all files and directories in mockLocalFS. |
| 102 */ |
| 103 reset: function() { |
| 104 this.root.dirList = []; |
| 105 this.root.rootFileList = []; |
| 106 } |
| 107 }; |
| 108 |
| 109 function DirEntry(dirname) { |
| 110 this.name = dirname; |
| 111 this.fileList = []; |
| 112 this.getFile = function(fileName, isCreate, success, failure) { |
| 113 for(var i = 0; i < this.fileList.length; i++) { |
| 114 if (fileName == this.fileList[i].name) { |
| 115 success(this.fileList[i]); |
| 116 return; |
| 117 } |
| 118 } |
| 119 if (!isCreate.create) { |
| 120 if (failure) |
| 121 failure('FILE_NOT_FOUND'); |
| 122 } else { |
| 123 this.fileList.push( new FileEntry(fileName) ); |
| 124 success(this.fileList[this.fileList.length - 1]); |
| 125 } |
| 126 } |
| 127 } |
| 128 |
| 129 window.webkitRequestFileSystem = function(type, size, callback) { |
| 130 callback(mockLocalFS); |
| 131 } |
| 132 |
| 133 function Blob(arg) { |
| 134 var data = arg[0]; |
| 135 this.content = ''; |
| 136 if (typeof data == 'string') |
| 137 this.content = data; |
| 138 else |
| 139 this.content = Array.prototype.join.call(data); |
| 140 } |
| 141 |
| 142 var mockWriter = { |
| 143 write: function(blobData) { |
| 144 } |
| 145 }; |
| 146 |
| 147 function FileEntry(filename) { |
| 148 this.name = filename; |
| 149 this.file = function(callback) { |
| 150 callback(TestConstants.FILESTRING); |
| 151 }; |
| 152 this.createWriter = function(callback) { |
| 153 callback(mockWriter); |
| 154 }; |
| 155 this.remove = function(success, failure) { |
| 156 }; |
| 157 } |
| 158 |
| 159 // Mock chrome syncFS handler |
| 160 var mockSyncFS = { |
| 161 root: { |
| 162 fileList: [], |
| 163 getFile: function(fileName, isCreate, success, failure) { |
| 164 for(var i = 0; i < this.fileList.length; i++) { |
| 165 if (fileName == this.fileList[i].name) { |
| 166 success(this.fileList[i]); |
| 167 return; |
| 168 } |
| 169 } |
| 170 if (!isCreate.create) { |
| 171 if (failure) |
| 172 failure('FILE_NOT_FOUND'); |
| 173 } else { |
| 174 this.fileList.push(new FileEntry(fileName)); |
| 175 success(this.fileList[this.fileList.length - 1]); |
| 176 } |
| 177 }, |
| 178 }, |
| 179 /** |
| 180 * Create a new file in mockSyncFS. |
| 181 * @param {string} fileName File name that to be created. |
| 182 * @return {FileEntry} Handle of the new file |
| 183 */ |
| 184 mockTestFile: function(fileName) { |
| 185 var mockFile = new FileEntry(fileName); |
| 186 this.root.fileList.push(mockFile); |
| 187 return mockFile; |
| 188 }, |
| 189 /** |
| 190 * Delete all files in mockSyncFS. |
| 191 */ |
| 192 reset: function() { |
| 193 this.root.fileList = []; |
| 194 } |
10 }; | 195 }; |
11 | 196 |
12 // Mock a few chrome apis. | 197 // Mock a few chrome apis. |
13 var chrome = { | 198 var chrome = { |
14 storage: { | 199 storage: { |
15 local: { | 200 local: { |
16 get: function(key, callback) { | 201 get: function(key, callback) { |
17 var items = {}; | 202 var items = {}; |
18 switch (key) { | 203 switch (key) { |
19 case Constants.AccessLocalWallpaperInfoKey: | 204 case Constants.AccessLocalWallpaperInfoKey: |
20 items[Constants.AccessLocalWallpaperInfoKey] = { | 205 items[Constants.AccessLocalWallpaperInfoKey] = { |
21 'url': 'dummy', | 206 'url': 'dummy', |
22 'layout': 'dummy', | 207 'layout': 'dummy', |
23 'source': 'dummy' | 208 'source': Constants.WallpaperSourceEnum.Custom |
24 }; | 209 }; |
25 } | 210 } |
26 callback(items); | 211 callback(items); |
27 }, | 212 }, |
28 set: function(items, callback) { | 213 set: function(items, callback) { |
29 } | 214 } |
30 }, | 215 }, |
31 sync: { | 216 sync: { |
32 get: function(key, callback) { | 217 get: function(key, callback) { |
33 }, | 218 }, |
34 set: function(items, callback) { | 219 set: function(items, callback) { |
35 } | 220 } |
36 }, | 221 }, |
37 onChanged: { | 222 onChanged: { |
38 addListener: function(listener) { | 223 addListener: function(listener) { |
39 this.dispatch = listener; | 224 this.dispatch = listener; |
40 } | 225 } |
41 } | 226 } |
42 }, | 227 }, |
43 syncFileSystem: { | 228 syncFileSystem: { |
44 requestFileSystem: function(fs) { | 229 requestFileSystem: function(callback) { |
| 230 callback(mockSyncFS); |
45 }, | 231 }, |
46 onFileStatusChanged: { | 232 onFileStatusChanged: { |
47 addListener: function(listener) { | 233 addListener: function(listener) { |
48 this.dispatch = listener; | 234 this.dispatch = listener; |
49 } | 235 } |
50 } | 236 } |
51 }, | 237 }, |
52 app: { | 238 app: { |
53 runtime: { | 239 runtime: { |
54 onLaunched: { | 240 onLaunched: { |
55 addListener: function(listener) { | 241 addListener: function(listener) { |
56 } | 242 } |
57 } | 243 } |
58 } | 244 } |
59 }, | 245 }, |
60 alarms: { | 246 alarms: { |
61 onAlarm: { | 247 onAlarm: { |
62 addListener: function(listener) { | 248 addListener: function(listener) { |
63 } | 249 } |
64 } | 250 } |
65 }, | 251 }, |
66 wallpaperPrivate: { | 252 wallpaperPrivate: { |
67 getStrings: function(callback) { | 253 getStrings: function(callback) { |
68 callback({isExperimental: false}); | 254 callback({isExperimental: false}); |
69 }, | 255 }, |
| 256 setCustomWallpaper: function(data, layout, isGenerateThumbnail, fileName, |
| 257 callback) { |
| 258 }, |
70 getSyncSetting: function(callback) { | 259 getSyncSetting: function(callback) { |
71 callback({syncThemes: true}); | 260 callback({syncThemes: true}); |
72 } | 261 } |
| 262 }, |
| 263 runtime: { |
| 264 lastError: null |
73 } | 265 } |
74 }; | 266 }; |
75 | 267 |
76 (function (exports) { | 268 (function (exports) { |
77 var originalXMLHttpRequest = window.XMLHttpRequest; | 269 var originalXMLHttpRequest = window.XMLHttpRequest; |
78 | 270 |
79 // Mock XMLHttpRequest. It dispatches a 'load' event immediately with status | 271 // Mock XMLHttpRequest. It dispatches a 'load' event immediately with status |
80 // equals to 200 in function |send|. | 272 // equals to 200 in function |send|. |
81 function MockXMLHttpRequest() { | 273 function MockXMLHttpRequest() { |
82 } | 274 } |
(...skipping 10 matching lines...) Expand all Loading... |
93 addEventListener: function(type, listener) { | 285 addEventListener: function(type, listener) { |
94 this.eventListeners = this.eventListeners || {}; | 286 this.eventListeners = this.eventListeners || {}; |
95 this.eventListeners[type] = this.eventListeners[type] || []; | 287 this.eventListeners[type] = this.eventListeners[type] || []; |
96 this.eventListeners[type].push(listener); | 288 this.eventListeners[type].push(listener); |
97 }, | 289 }, |
98 | 290 |
99 removeEventListener: function (type, listener) { | 291 removeEventListener: function (type, listener) { |
100 var listeners = this.eventListeners && this.eventListeners[type] || []; | 292 var listeners = this.eventListeners && this.eventListeners[type] || []; |
101 | 293 |
102 for (var i = 0; i < listeners.length; ++i) { | 294 for (var i = 0; i < listeners.length; ++i) { |
103 if (listeners[i] == listener) { | 295 if (listeners[i] == listener) |
104 return listeners.splice(i, 1); | 296 return listeners.splice(i, 1); |
105 } | |
106 } | 297 } |
107 }, | 298 }, |
108 | 299 |
109 dispatchEvent: function(type) { | 300 dispatchEvent: function(type) { |
110 var listeners = this.eventListeners && this.eventListeners[type] || []; | 301 var listeners = this.eventListeners && this.eventListeners[type] || []; |
111 | 302 |
112 if (/test.jpg$/g.test(this.url)) { | 303 if (/test.jpg$/g.test(this.url)) |
113 this.response = TestConstants.image; | 304 this.response = TestConstants.IMAGE; |
114 } else { | 305 else |
115 this.response = ''; | 306 this.response = ''; |
116 } | |
117 | 307 |
118 for (var i = 0; i < listeners.length; ++i) | 308 for (var i = 0; i < listeners.length; ++i) |
119 listeners[i].call(this, new Event(type)); | 309 listeners[i].call(this, new Event(type)); |
120 }, | 310 }, |
121 | 311 |
122 open: function(method, url, async) { | 312 open: function(method, url, async) { |
123 this.url = url; | 313 this.url = url; |
124 } | 314 } |
125 }; | 315 }; |
126 | 316 |
127 function installMockXMLHttpRequest() { | 317 function installMockXMLHttpRequest() { |
128 window['XMLHttpRequest'] = MockXMLHttpRequest; | 318 window['XMLHttpRequest'] = MockXMLHttpRequest; |
129 }; | 319 }; |
130 | 320 |
131 function uninstallMockXMLHttpRequest() { | 321 function uninstallMockXMLHttpRequest() { |
132 window['XMLHttpRequest'] = originalXMLHttpRequest; | 322 window['XMLHttpRequest'] = originalXMLHttpRequest; |
133 }; | 323 }; |
134 | 324 |
135 exports.installMockXMLHttpRequest = installMockXMLHttpRequest; | 325 exports.installMockXMLHttpRequest = installMockXMLHttpRequest; |
136 exports.uninstallMockXMLHttpRequest = uninstallMockXMLHttpRequest; | 326 exports.uninstallMockXMLHttpRequest = uninstallMockXMLHttpRequest; |
137 | 327 |
138 })(window); | 328 })(window); |
OLD | NEW |