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.
7 wallpaperURL: 'https://test.com/test.jpg',
8 // A dummy string which is used to mock an 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]
14 // mock FileReader object in HTML5 File System
15 function FileReader() {
17 this.onloadend = function() {
19 this.readAsArrayBuffer = function(mockFile) {
20 this.result = mockFile;
25 // Mock localFS handler
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]);
37 if (!isCreate.create) {
39 failure('DIR_NOT_FOUND');
41 this.dirList.push(new DirEntry(dir));
42 success(this.dirList[this.dirList.length - 1]);
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]);
55 if (!isCreate.create) {
57 failure('FILE_NOT_FOUND');
59 this.rootFileList.push(new FileEntry(fileName));
60 success(this.rootFileList[this.rootFileList.length - 1]);
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);
68 this.getDirectory(realDirName, {create: false},
69 getDirSuccess, failure);
71 console.error('Only support one level deep subdirectory')
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
80 mockTestFile: function(fileName) {
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;} );
94 this.root.getDirectory(realDirName, {create: true}, getDirSuccess);
96 console.error('Only support one-level fileSystem mock')
101 * Delete all files and directories in mockLocalFS.
104 this.root.dirList = [];
105 this.root.rootFileList = [];
109 function DirEntry(dirname) {
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]);
119 if (!isCreate.create) {
121 failure('FILE_NOT_FOUND');
123 this.fileList.push( new FileEntry(fileName) );
124 success(this.fileList[this.fileList.length - 1]);
129 window.webkitRequestFileSystem = function(type, size, callback) {
130 callback(mockLocalFS);
136 if (typeof data == 'string')
139 this.content = Array.prototype.join.call(data);
143 write: function(blobData) {
147 function FileEntry(filename) {
148 this.name = filename;
149 this.file = function(callback) {
150 callback(TestConstants.FILESTRING);
152 this.createWriter = function(callback) {
153 callback(mockWriter);
155 this.remove = function(success, failure) {
159 // Mock chrome syncFS handler
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]);
170 if (!isCreate.create) {
172 failure('FILE_NOT_FOUND');
174 this.fileList.push(new FileEntry(fileName));
175 success(this.fileList[this.fileList.length - 1]);
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
184 mockTestFile: function(fileName) {
185 var mockFile = new FileEntry(fileName);
186 this.root.fileList.push(mockFile);
190 * Delete all files in mockSyncFS.
193 this.root.fileList = [];
197 // Mock a few chrome apis.
201 get: function(key, callback) {
204 case Constants.AccessLocalWallpaperInfoKey:
205 items[Constants.AccessLocalWallpaperInfoKey] = {
208 'source': Constants.WallpaperSourceEnum.Custom
213 set: function(items, callback) {
217 get: function(key, callback) {
219 set: function(items, callback) {
223 addListener: function(listener) {
224 this.dispatch = listener;
229 requestFileSystem: function(callback) {
230 callback(mockSyncFS);
232 onFileStatusChanged: {
233 addListener: function(listener) {
234 this.dispatch = listener;
241 addListener: function(listener) {
248 addListener: function(listener) {
253 getStrings: function(callback) {
254 callback({isExperimental: false});
256 setCustomWallpaper: function(data, layout, isGenerateThumbnail, fileName,
259 getSyncSetting: function(callback) {
260 callback({syncThemes: true});
268 (function (exports) {
269 var originalXMLHttpRequest = window.XMLHttpRequest;
271 // Mock XMLHttpRequest. It dispatches a 'load' event immediately with status
272 // equals to 200 in function |send|.
273 function MockXMLHttpRequest() {
276 MockXMLHttpRequest.prototype = {
280 send: function(data) {
282 this.dispatchEvent('load');
285 addEventListener: function(type, listener) {
286 this.eventListeners = this.eventListeners || {};
287 this.eventListeners[type] = this.eventListeners[type] || [];
288 this.eventListeners[type].push(listener);
291 removeEventListener: function (type, listener) {
292 var listeners = this.eventListeners && this.eventListeners[type] || [];
294 for (var i = 0; i < listeners.length; ++i) {
295 if (listeners[i] == listener)
296 return listeners.splice(i, 1);
300 dispatchEvent: function(type) {
301 var listeners = this.eventListeners && this.eventListeners[type] || [];
303 if (/test.jpg$/g.test(this.url))
304 this.response = TestConstants.IMAGE;
308 for (var i = 0; i < listeners.length; ++i)
309 listeners[i].call(this, new Event(type));
312 open: function(method, url, async) {
317 function installMockXMLHttpRequest() {
318 window['XMLHttpRequest'] = MockXMLHttpRequest;
321 function uninstallMockXMLHttpRequest() {
322 window['XMLHttpRequest'] = originalXMLHttpRequest;
325 exports.installMockXMLHttpRequest = installMockXMLHttpRequest;
326 exports.uninstallMockXMLHttpRequest = uninstallMockXMLHttpRequest;