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: '*#*@#&', |
kevers
2014/10/30 14:17:36
Constants should be in all caps.
var TestContants
Ran
2014/10/30 19:23:43
Done.
| |
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 function FileReader() { | |
kevers
2014/10/30 14:17:36
Please add jsdoc for all methods that are not API
Ran
2014/10/30 19:23:43
Done.
| |
15 this.result = ''; | |
16 this.onloadend = function() { | |
17 }; | |
18 this.readAsArrayBuffer = function(mockFile) { | |
19 this.result = mockFile; | |
20 this.onloadend(); | |
21 } | |
22 } | |
23 | |
24 // Mock localFS handler | |
25 var mockLocalFS = { | |
26 root: { | |
27 dirList: [], | |
28 rootFileList: [], | |
29 getDirectory: function(dir, isCreate, success, failure) { | |
30 for(var i = 0; i < this.dirList.length; i++) { | |
31 if (this.dirList[i].name == dir) { | |
32 success(this.dirList[i]); | |
33 return; | |
34 } | |
35 } | |
36 if (!isCreate.create) { | |
37 if (failure) { | |
kevers
2014/10/30 14:17:36
No need for brackets on single line if.
Ran
2014/10/30 19:23:43
Done.
| |
38 failure('DIR_NOT_FOUND'); | |
39 } | |
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 mockTestFile: function(fileName) { | |
76 var mockFile; | |
77 if (fileName[0] == '/') | |
78 fileName = fileName.substr(1); | |
79 if (fileName.split('/').length == 1) { | |
80 mockFile = new FileEntry(fileName); | |
81 this.root.rootFileList.push(mockFile); | |
82 } else if (fileName.split('/').length == 2) { | |
83 var realDirName = fileName.split('/')[0]; | |
84 var realFileName = fileName.split('/')[1]; | |
85 var getDirSuccess = function(dirEntry) { | |
86 dirEntry.getFile(realFileName, {create: true}, | |
87 function(fe) {mockFile = fe;} ); | |
88 }; | |
89 this.root.getDirectory(realDirName, {create: true}, getDirSuccess); | |
90 } else { | |
91 console.error('Only support one-level fileSystem mock') | |
92 } | |
93 return mockFile; | |
94 }, | |
95 reset: function() { | |
96 this.root.dirList = []; | |
97 this.root.rootFileList = []; | |
98 } | |
99 }; | |
100 | |
101 function DirEntry(dirname) { | |
102 this.name = dirname; | |
103 this.fileList = []; | |
104 this.getFile = function(fileName, isCreate, success, failure) { | |
105 for(var i = 0; i < this.fileList.length; i++) { | |
106 if (fileName == this.fileList[i].name) { | |
107 success(this.fileList[i]); | |
108 return; | |
109 } | |
110 } | |
111 if (!isCreate.create) { | |
112 if (failure) { | |
kevers
2014/10/30 14:17:36
Remove brackets on single line if.
Ran
2014/10/30 19:23:43
Done.
| |
113 failure('FILE_NOT_FOUND'); | |
114 } | |
115 } else { | |
116 this.fileList.push( new FileEntry(fileName) ); | |
117 success(this.fileList[this.fileList.length - 1]); | |
118 } | |
119 } | |
120 } | |
121 | |
122 window.webkitRequestFileSystem = function(type, size, callback) { | |
123 callback(mockLocalFS); | |
124 } | |
125 | |
126 function Blob(arg) { | |
127 var data = arg[0]; | |
128 this.content = ''; | |
129 if (typeof data == 'string') { | |
kevers
2014/10/30 14:17:36
Remove brackets on single line if-else.
Ran
2014/10/30 19:23:43
Done.
| |
130 this.content = data; | |
131 } else { | |
132 this.content = Array.prototype.join.call(data); | |
133 } | |
134 } | |
135 | |
136 var mockWriter = { | |
137 write: function(blobData) { | |
138 } | |
139 }; | |
140 | |
141 function FileEntry(filename) { | |
142 this.name = filename; | |
143 this.file = function(callback) { | |
144 callback(TestConstants.fileString); | |
145 }; | |
146 this.createWriter = function(callback) { | |
147 callback(mockWriter); | |
148 }; | |
149 this.remove = function(success, failure) { | |
150 }; | |
151 } | |
152 | |
153 // Mock chrome syncFS handler | |
154 var mockSyncFS = { | |
155 root: { | |
156 fileList: [], | |
157 getFile: function(fileName, isCreate, success, failure) { | |
158 for(var i = 0; i < this.fileList.length; i++) { | |
159 if (fileName == this.fileList[i].name) { | |
160 success(this.fileList[i]); | |
161 return; | |
162 } | |
163 } | |
164 if (!isCreate.create) { | |
165 if (failure) | |
166 failure('FILE_NOT_FOUND'); | |
167 } else { | |
168 this.fileList.push(new FileEntry(fileName)); | |
169 success(this.fileList[this.fileList.length - 1]); | |
170 } | |
171 }, | |
172 }, | |
173 mockTestFile: function(fileName) { | |
174 var mockFile = new FileEntry(fileName); | |
175 this.root.fileList.push(mockFile); | |
176 return mockFile; | |
177 }, | |
178 reset: function() { | |
179 this.root.fileList = []; | |
180 } | |
10 }; | 181 }; |
11 | 182 |
12 // Mock a few chrome apis. | 183 // Mock a few chrome apis. |
13 var chrome = { | 184 var chrome = { |
14 storage: { | 185 storage: { |
15 local: { | 186 local: { |
16 get: function(key, callback) { | 187 get: function(key, callback) { |
17 var items = {}; | 188 var items = {}; |
18 switch (key) { | 189 switch (key) { |
19 case Constants.AccessLocalWallpaperInfoKey: | 190 case Constants.AccessLocalWallpaperInfoKey: |
20 items[Constants.AccessLocalWallpaperInfoKey] = { | 191 items[Constants.AccessLocalWallpaperInfoKey] = { |
21 'url': 'dummy', | 192 'url': 'dummy', |
22 'layout': 'dummy', | 193 'layout': 'dummy', |
23 'source': 'dummy' | 194 'source': Constants.WallpaperSourceEnum.Custom |
24 }; | 195 }; |
25 } | 196 } |
26 callback(items); | 197 callback(items); |
27 }, | 198 }, |
28 set: function(items, callback) { | 199 set: function(items, callback) { |
29 } | 200 } |
30 }, | 201 }, |
31 sync: { | 202 sync: { |
32 get: function(key, callback) { | 203 get: function(key, callback) { |
33 }, | 204 }, |
34 set: function(items, callback) { | 205 set: function(items, callback) { |
35 } | 206 } |
36 }, | 207 }, |
37 onChanged: { | 208 onChanged: { |
38 addListener: function(listener) { | 209 addListener: function(listener) { |
39 this.dispatch = listener; | 210 this.dispatch = listener; |
40 } | 211 } |
41 } | 212 } |
42 }, | 213 }, |
43 syncFileSystem: { | 214 syncFileSystem: { |
44 requestFileSystem: function(fs) { | 215 requestFileSystem: function(callback) { |
216 callback(mockSyncFS); | |
45 }, | 217 }, |
46 onFileStatusChanged: { | 218 onFileStatusChanged: { |
47 addListener: function(listener) { | 219 addListener: function(listener) { |
48 this.dispatch = listener; | 220 this.dispatch = listener; |
49 } | 221 } |
50 } | 222 } |
51 }, | 223 }, |
52 app: { | 224 app: { |
53 runtime: { | 225 runtime: { |
54 onLaunched: { | 226 onLaunched: { |
55 addListener: function(listener) { | 227 addListener: function(listener) { |
56 } | 228 } |
57 } | 229 } |
58 } | 230 } |
59 }, | 231 }, |
60 alarms: { | 232 alarms: { |
61 onAlarm: { | 233 onAlarm: { |
62 addListener: function(listener) { | 234 addListener: function(listener) { |
63 } | 235 } |
64 } | 236 } |
65 }, | 237 }, |
66 wallpaperPrivate: { | 238 wallpaperPrivate: { |
67 getStrings: function(callback) { | 239 getStrings: function(callback) { |
68 callback({isExperimental: false}); | 240 callback({isExperimental: false}); |
241 }, | |
242 setCustomWallpaper: function(data, layout, isGenerateThumbnail, fileName, | |
243 callback) { | |
69 } | 244 } |
245 }, | |
246 runtime: { | |
247 lastError: null | |
70 } | 248 } |
71 }; | 249 }; |
72 | 250 |
73 (function (exports) { | 251 (function (exports) { |
74 var originalXMLHttpRequest = window.XMLHttpRequest; | 252 var originalXMLHttpRequest = window.XMLHttpRequest; |
75 | 253 |
76 // Mock XMLHttpRequest. It dispatches a 'load' event immediately with status | 254 // Mock XMLHttpRequest. It dispatches a 'load' event immediately with status |
77 // equals to 200 in function |send|. | 255 // equals to 200 in function |send|. |
78 function MockXMLHttpRequest() { | 256 function MockXMLHttpRequest() { |
79 } | 257 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
126 }; | 304 }; |
127 | 305 |
128 function uninstallMockXMLHttpRequest() { | 306 function uninstallMockXMLHttpRequest() { |
129 window['XMLHttpRequest'] = originalXMLHttpRequest; | 307 window['XMLHttpRequest'] = originalXMLHttpRequest; |
130 }; | 308 }; |
131 | 309 |
132 exports.installMockXMLHttpRequest = installMockXMLHttpRequest; | 310 exports.installMockXMLHttpRequest = installMockXMLHttpRequest; |
133 exports.uninstallMockXMLHttpRequest = uninstallMockXMLHttpRequest; | 311 exports.uninstallMockXMLHttpRequest = uninstallMockXMLHttpRequest; |
134 | 312 |
135 })(window); | 313 })(window); |
OLD | NEW |