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 * Joins paths so that the two paths are connected by only 1 '/'. | 6 * Joins paths so that the two paths are connected by only 1 '/'. |
7 * @param {string} a Path. | 7 * @param {string} a Path. |
8 * @param {string} b Path. | 8 * @param {string} b Path. |
9 * @return {string} Joined path. | 9 * @return {string} Joined path. |
10 */ | 10 */ |
11 function joinPath(a, b) { | 11 function joinPath(a, b) { |
12 return a.replace(/\/+$/, '') + '/' + b.replace(/^\/+/, ''); | 12 return a.replace(/\/+$/, '') + '/' + b.replace(/^\/+/, ''); |
13 } | 13 } |
14 | 14 |
15 /** | 15 /** |
16 * Mock class for DOMFileSystem. | 16 * Mock class for DOMFileSystem. |
17 * | 17 * |
18 * @param {string} volumeId Volume ID. | 18 * @param {string} volumeId Volume ID. |
19 * @param {string} rootURL URL string of root which is used in MockEntry.toURL. | 19 * @param {string=} opt_rootURL URL string of root which is used in |
| 20 * MockEntry.toURL. |
20 * @constructor | 21 * @constructor |
21 */ | 22 */ |
22 function MockFileSystem(volumeId, rootURL) { | 23 function MockFileSystem(volumeId, opt_rootURL) { |
| 24 /** @type {string} */ |
23 this.name = volumeId; | 25 this.name = volumeId; |
| 26 |
| 27 /** @type {!Object<string, !Entry>} */ |
24 this.entries = {}; | 28 this.entries = {}; |
25 this.rootURL = rootURL; | 29 this.entries['/'] = new MockDirectoryEntry(this, '/'); |
| 30 |
| 31 /** @type {string} */ |
| 32 this.rootURL = opt_rootURL || 'filesystem:' + volumeId; |
26 } | 33 } |
27 | 34 |
28 MockFileSystem.prototype = { | 35 MockFileSystem.prototype = { |
29 get root() { return this.entries['/']; } | 36 get root() { return this.entries['/']; } |
30 }; | 37 }; |
31 | 38 |
32 /** | 39 /** |
| 40 * Creates file and directory entries for all the given paths. Paths ending in |
| 41 * slashes are interpreted as directories. All intermediate directories leading |
| 42 * up to the files/directories to be created, are also created. |
| 43 * @param {!Array<string>} paths An array of file paths to populate in this file |
| 44 * system. |
| 45 */ |
| 46 MockFileSystem.prototype.populate = function(paths) { |
| 47 paths.forEach(function(path) { |
| 48 var pathElements = path.split('/'); |
| 49 pathElements.forEach(function(_, i) { |
| 50 var subpath = pathElements.slice(0, i).join('/'); |
| 51 if (subpath && !(subpath in this.entries)) |
| 52 this.entries[subpath] = new MockDirectoryEntry(this, subpath); |
| 53 }.bind(this)); |
| 54 |
| 55 // If the path doesn't end in a slash, create a file. |
| 56 if (!/\/$/.test(path)) |
| 57 this.entries[path] = new MockFileEntry(this, path, {size: 0}); |
| 58 }.bind(this)); |
| 59 }; |
| 60 |
| 61 /** |
33 * Base class of mock entries. | 62 * Base class of mock entries. |
34 * | 63 * |
35 * @param {TestFileSystem} filesystem File system where the entry is localed. | 64 * @param {TestFileSystem} filesystem File system where the entry is localed. |
36 * @param {string} fullPath Full path of the entry. | 65 * @param {string} fullPath Full path of the entry. |
37 * @constructor | 66 * @constructor |
38 */ | 67 */ |
39 function MockEntry(filesystem, fullPath) { | 68 function MockEntry(filesystem, fullPath) { |
40 this.filesystem = filesystem; | 69 this.filesystem = filesystem; |
41 this.fullPath = fullPath; | 70 this.fullPath = fullPath; |
42 } | 71 } |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 * Returns a directory under the directory. | 229 * Returns a directory under the directory. |
201 * | 230 * |
202 * @param {string} path Path. | 231 * @param {string} path Path. |
203 * @param {Object} option Option. | 232 * @param {Object} option Option. |
204 * @param {callback(MockDirectoryEntry)} onSuccess Success callback. | 233 * @param {callback(MockDirectoryEntry)} onSuccess Success callback. |
205 * @param {callback(Object)} onError Failure callback; | 234 * @param {callback(Object)} onError Failure callback; |
206 */ | 235 */ |
207 MockDirectoryEntry.prototype.getDirectory = | 236 MockDirectoryEntry.prototype.getDirectory = |
208 function(path, option, onSuccess, onError) { | 237 function(path, option, onSuccess, onError) { |
209 var fullPath = path[0] === '/' ? path : joinPath(this.fullPath, path); | 238 var fullPath = path[0] === '/' ? path : joinPath(this.fullPath, path); |
210 if (!this.filesystem.entries[fullPath]) | 239 var result = this.filesystem.entries[fullPath]; |
211 onError({name: util.FileError.NOT_FOUND_ERR}); | 240 if (result) { |
212 else if (!(this.filesystem.entries[fullPath] instanceof MockDirectoryEntry)) | 241 if (!(result instanceof MockDirectoryEntry)) |
213 onError({name: util.FileError.TYPE_MISMATCH_ERR}); | 242 onError({name: util.FileError.TYPE_MISMATCH_ERR}); |
214 else | 243 else if (option['create'] && option['exclusive']) |
215 onSuccess(this.filesystem.entries[fullPath]); | 244 onError({name: util.FileError.PATH_EXISTS_ERR}); |
| 245 else |
| 246 onSuccess(result); |
| 247 } else { |
| 248 if (!option['create']) { |
| 249 onError({name: util.FileError.NOT_FOUND_ERR}); |
| 250 } else { |
| 251 var newEntry = new MockDirectoryEntry(this.filesystem, fullPath); |
| 252 this.filesystem.entries[fullPath] = newEntry; |
| 253 onSuccess(newEntry); |
| 254 } |
| 255 } |
216 }; | 256 }; |
217 | 257 |
218 /** | 258 /** |
219 * Creates a MockDirectoryReader for the entry. | 259 * Creates a MockDirectoryReader for the entry. |
220 * @return {DirectoryReader} A directory reader. | 260 * @return {DirectoryReader} A directory reader. |
221 */ | 261 */ |
222 MockDirectoryEntry.prototype.createReader = function() { | 262 MockDirectoryEntry.prototype.createReader = function() { |
223 return new MockDirectoryReader(); | 263 return new MockDirectoryReader(); |
224 }; | 264 }; |
225 | 265 |
226 /** | 266 /** |
227 * Mock class for DirectoryReader. | 267 * Mock class for DirectoryReader. |
228 */ | 268 */ |
229 function MockDirectoryReader() {} | 269 function MockDirectoryReader() {} |
230 | 270 |
231 /** | 271 /** |
232 * Reads entries. | 272 * Reads entries. |
233 * Current implementation just calls success callback with an empty list. | 273 * Current implementation just calls success callback with an empty list. |
234 * | 274 * |
235 * @param {function(Array)} success Success callback. | 275 * @param {function(Array)} success Success callback. |
236 * @param {function} error Error callback. | 276 * @param {function} error Error callback. |
237 */ | 277 */ |
238 MockDirectoryReader.prototype.readEntries = function(success, error) { | 278 MockDirectoryReader.prototype.readEntries = function(success, error) { |
239 success([]); | 279 success([]); |
240 }; | 280 }; |
OLD | NEW |