Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(344)

Side by Side Diff: chrome/test/data/file_manager/unit_tests/mocks/mock_entry.js

Issue 762593006: Prototype implementation of MediaImportHandler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync to master. Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 */
(...skipping 12 matching lines...) Expand all
23 this.name = volumeId; 23 this.name = volumeId;
24 this.entries = {}; 24 this.entries = {};
25 this.rootURL = rootURL; 25 this.rootURL = rootURL;
26 } 26 }
27 27
28 MockFileSystem.prototype = { 28 MockFileSystem.prototype = {
29 get root() { return this.entries['/']; } 29 get root() { return this.entries['/']; }
30 }; 30 };
31 31
32 /** 32 /**
33 * Creates a new mock filesystem with a root dir and the given volume ID.
34 * @param {string} volumeId
35 * @return {!MockFileSystem}
36 */
37 MockFileSystem.create = function(volumeId) {
hirono 2014/12/04 04:44:23 It looks convenient. Could you please move the lin
Ben Kwa 2014/12/04 19:53:01 Done.
38 var fileSystem = new MockFileSystem(volumeId, 'filesystem:' + volumeId);
39 fileSystem.entries['/'] = new MockDirectoryEntry(fileSystem, '/');
40 return fileSystem;
41 };
42
43 /**
44 * Creates file and directory entries for all the given paths. Paths ending in
45 * slashes are interpreted as directories. All intermediate directories leading
46 * up to the files/directories to be created, are also created.
47 * @param {!Array<string>} paths An array of file paths to populate in this file
48 * system.
49 */
50 MockFileSystem.prototype.populate = function(paths) {
51 paths.forEach(function(path) {
52 var pathElements = path.split('/');
53 pathElements.forEach(function(_, i) {
54 var subpath = pathElements.slice(0, i).join('/');
55 if (subpath && !(subpath in this.entries)) {
hirono 2014/12/04 04:44:24 nit: Please drop {}
Ben Kwa 2014/12/04 19:53:01 Done.
56 this.entries[subpath] = new MockDirectoryEntry(this, subpath);
57 }
58 }.bind(this));
59
60 // If the path doesn't end in a slash, create a file.
61 if (!/\/$/.test(path)) {
hirono 2014/12/04 04:44:24 nit: Please drop {}
Ben Kwa 2014/12/04 19:53:01 Done.
62 this.entries[path] = new MockFileEntry(this, path);
hirono 2014/12/04 04:44:23 Entry should take metadata.
Ben Kwa 2014/12/04 19:53:01 Done.
63 }
64 }.bind(this));
65 };
66
67 /**
33 * Base class of mock entries. 68 * Base class of mock entries.
34 * 69 *
35 * @param {TestFileSystem} filesystem File system where the entry is localed. 70 * @param {TestFileSystem} filesystem File system where the entry is localed.
36 * @param {string} fullPath Full path of the entry. 71 * @param {string} fullPath Full path of the entry.
37 * @constructor 72 * @constructor
38 */ 73 */
39 function MockEntry(filesystem, fullPath) { 74 function MockEntry(filesystem, fullPath) {
40 this.filesystem = filesystem; 75 this.filesystem = filesystem;
41 this.fullPath = fullPath; 76 this.fullPath = fullPath;
42 } 77 }
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 * Returns a directory under the directory. 235 * Returns a directory under the directory.
201 * 236 *
202 * @param {string} path Path. 237 * @param {string} path Path.
203 * @param {Object} option Option. 238 * @param {Object} option Option.
204 * @param {callback(MockDirectoryEntry)} onSuccess Success callback. 239 * @param {callback(MockDirectoryEntry)} onSuccess Success callback.
205 * @param {callback(Object)} onError Failure callback; 240 * @param {callback(Object)} onError Failure callback;
206 */ 241 */
207 MockDirectoryEntry.prototype.getDirectory = 242 MockDirectoryEntry.prototype.getDirectory =
208 function(path, option, onSuccess, onError) { 243 function(path, option, onSuccess, onError) {
209 var fullPath = path[0] === '/' ? path : joinPath(this.fullPath, path); 244 var fullPath = path[0] === '/' ? path : joinPath(this.fullPath, path);
210 if (!this.filesystem.entries[fullPath]) 245 var result = this.filesystem.entries[fullPath];
hirono 2014/12/04 04:44:23 Thank you for implementing this!
Ben Kwa 2014/12/04 19:53:01 Acknowledged.
211 onError({name: util.FileError.NOT_FOUND_ERR}); 246 if (result) {
212 else if (!(this.filesystem.entries[fullPath] instanceof MockDirectoryEntry)) 247 if (!(result instanceof MockDirectoryEntry))
213 onError({name: util.FileError.TYPE_MISMATCH_ERR}); 248 onError({name: util.FileError.TYPE_MISMATCH_ERR});
214 else 249 else if (option['create'] && option['exclusive'])
215 onSuccess(this.filesystem.entries[fullPath]); 250 onError({name: util.FileError.PATH_EXISTS_ERR});
251 else
252 onSuccess(result);
253 } else {
254 if (!option['create']) {
255 onError({name: util.FileError.NOT_FOUND_ERR});
256 } else {
257 var newEntry = new MockDirectoryEntry(this.filesystem, fullPath);
258 this.filesystem.entries[fullPath] = newEntry;
259 onSuccess(newEntry);
260 }
261 }
216 }; 262 };
217 263
218 /** 264 /**
219 * Creates a MockDirectoryReader for the entry. 265 * Creates a MockDirectoryReader for the entry.
220 * @return {DirectoryReader} A directory reader. 266 * @return {DirectoryReader} A directory reader.
221 */ 267 */
222 MockDirectoryEntry.prototype.createReader = function() { 268 MockDirectoryEntry.prototype.createReader = function() {
223 return new MockDirectoryReader(); 269 return new MockDirectoryReader();
224 }; 270 };
225 271
226 /** 272 /**
227 * Mock class for DirectoryReader. 273 * Mock class for DirectoryReader.
228 */ 274 */
229 function MockDirectoryReader() {} 275 function MockDirectoryReader() {}
230 276
231 /** 277 /**
232 * Reads entries. 278 * Reads entries.
233 * Current implementation just calls success callback with an empty list. 279 * Current implementation just calls success callback with an empty list.
234 * 280 *
235 * @param {function(Array)} success Success callback. 281 * @param {function(Array)} success Success callback.
236 * @param {function} error Error callback. 282 * @param {function} error Error callback.
237 */ 283 */
238 MockDirectoryReader.prototype.readEntries = function(success, error) { 284 MockDirectoryReader.prototype.readEntries = function(success, error) {
239 success([]); 285 success([]);
240 }; 286 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698