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 '/'. |
| 7 * @param {string} a Path. |
| 8 * @param {string} b Path. |
| 9 * @return {string} Joined path. |
| 10 */ |
| 11 function joinPath(a, b) { |
| 12 return a.replace(/\/+$/, '') + '/' + b.replace(/^\/+/, ''); |
| 13 }; |
| 14 |
| 15 /** |
| 16 * Test file system. |
| 17 * |
| 18 * @param {string} fileSystemId File system ID. |
| 19 * @constructor |
| 20 */ |
| 21 function TestFileSystem(fileSystemId) { |
| 22 this.fileSystemId = fileSystemId; |
| 23 this.entries = {}; |
| 24 }; |
| 25 |
| 26 TestFileSystem.prototype = { |
| 27 get root() { return this.entries['/']; } |
| 28 }; |
| 29 |
| 30 /** |
6 * Base class of mock entries. | 31 * Base class of mock entries. |
7 * | 32 * |
8 * @param {string} volumeId ID of the volume that contains the entry. | 33 * @param {TestFileSystem} filesystem File system where the entry is localed. |
9 * @param {string} fullpath Full path of the entry. | 34 * @param {string} fullpath Full path of the entry. |
10 * @constructor | 35 * @constructor |
11 */ | 36 */ |
12 function MockEntry(volumeId, fullPath) { | 37 function MockEntry(filesystem, fullPath) { |
13 this.volumeId = volumeId; | 38 this.filesystem = filesystem; |
14 this.fullPath = fullPath; | 39 this.fullPath = fullPath; |
15 } | 40 } |
16 | 41 |
| 42 MockEntry.prototype = { |
| 43 /** |
| 44 * @return {string} Name of the entry. |
| 45 */ |
| 46 get name() { |
| 47 return this.fullPath.replace(/^.*\//, ''); |
| 48 } |
| 49 }; |
| 50 |
17 /** | 51 /** |
18 * Returns fake URL. | 52 * Returns fake URL. |
19 * | 53 * |
20 * @return {string} Fake URL. | 54 * @return {string} Fake URL. |
21 */ | 55 */ |
22 MockEntry.prototype.toURL = function() { | 56 MockEntry.prototype.toURL = function() { |
23 return 'filesystem:' + this.volumeId + this.fullPath; | 57 return 'filesystem:' + this.filesystem.fileSystemId + this.fullPath; |
| 58 }; |
| 59 |
| 60 /** |
| 61 * Obtains parent directory. |
| 62 * |
| 63 * @param {function(MockDirectoryEntry)} onSuccess Callback invoked with |
| 64 * the parent directory. |
| 65 * @param {function(Object)} onError Callback invoked with an error |
| 66 * object. |
| 67 */ |
| 68 MockEntry.prototype.getParent = function( |
| 69 onSuccess, onError) { |
| 70 var path = this.fullPath.replace(/\/[^\/]+$/, '') || '/'; |
| 71 if (this.filesystem.entries[path]) |
| 72 onSuccess(this.filesystem.entries[path]); |
| 73 else |
| 74 onError({name: util.FileError.NOT_FOUND_ERR}); |
| 75 }; |
| 76 |
| 77 /** |
| 78 * Moves the entry to the directory. |
| 79 * |
| 80 * @param {MockDirectoryEntry} parent Destination directory. |
| 81 * @param {string=} opt_newName New name. |
| 82 * @param {function(MockDirectoryEntry)} onSuccess Callback invoked with the |
| 83 * moved entry. |
| 84 * @param {function(Object)} onError Callback invoked with an error object. |
| 85 */ |
| 86 MockEntry.prototype.moveTo = function(parent, opt_newName, onSuccess, onError) { |
| 87 Promise.resolve().then(function() { |
| 88 this.filesystem.entries[this.fullPath] = null; |
| 89 return this.clone(joinPath(parent.fullPath, opt_newName || this.name)); |
| 90 }.bind(this)).then(onSuccess, onError); |
| 91 }; |
| 92 |
| 93 /** |
| 94 * Removes the entry. |
| 95 * |
| 96 * @param {function()} onSuccess Success callback. |
| 97 * @param {function(Object)} onError Callback invoked with an error object. |
| 98 */ |
| 99 MockEntry.prototype.remove = function(onSuccess, onError) { |
| 100 Promise.resolve().then(function() { |
| 101 this.filesystem.entries[this.fullPath] = null; |
| 102 }.bind(this)).then(onSuccess, onError); |
| 103 }; |
| 104 |
| 105 /** |
| 106 * Clones the entry with the new fullpath. |
| 107 * |
| 108 * @param {string} fullpath New fullpath. |
| 109 * @return {MockEntry} Cloned entry. |
| 110 */ |
| 111 MockEntry.prototype.clone = function(fullpath) { |
| 112 throw new Error('Not implemented.'); |
24 }; | 113 }; |
25 | 114 |
26 /** | 115 /** |
27 * Mock class for FileEntry. | 116 * Mock class for FileEntry. |
28 * | 117 * |
29 * @param {string} volumeId Id of the volume containing the entry. | 118 * @param {FileSystem} filesystem File system where the entry is localed. |
30 * @param {string} fullPath Full path for the entry. | 119 * @param {string} fullPath Full path for the entry. |
| 120 * @param {Object} metadata Metadata. |
31 * @extends {MockEntry} | 121 * @extends {MockEntry} |
32 * @constructor | 122 * @constructor |
33 */ | 123 */ |
34 function MockFileEntry(volumeId, fullPath, metadata) { | 124 function MockFileEntry(filesystem, fullPath, metadata) { |
35 MockEntry.call(this, volumeId, fullPath); | 125 MockEntry.call(this, filesystem, fullPath); |
36 this.volumeId = volumeId; | |
37 this.fullPath = fullPath; | |
38 this.metadata_ = metadata; | 126 this.metadata_ = metadata; |
| 127 this.isFile = true; |
| 128 this.isDirectory = false; |
39 } | 129 } |
40 | 130 |
41 MockFileEntry.prototype = { | 131 MockFileEntry.prototype = { |
42 __proto__: MockEntry.prototype | 132 __proto__: MockEntry.prototype |
43 }; | 133 }; |
44 | 134 |
45 /** | 135 /** |
46 * Obtains metadata of the entry. | 136 * Obtains metadata of the entry. |
47 * @param {function(Object)} callback Function to take the metadata. | 137 * @param {function(Object)} callback Function to take the metadata. |
48 */ | 138 */ |
49 MockFileEntry.prototype.getMetadata = function(callback) { | 139 MockFileEntry.prototype.getMetadata = function(callback) { |
50 Promise.resolve(this.metadata_).then(callback).catch(function(error) { | 140 Promise.resolve(this.metadata_).then(callback).catch(function(error) { |
51 console.error(error.stack || error); | 141 console.error(error.stack || error); |
52 window.onerror(); | 142 window.onerror(); |
53 }); | 143 }); |
54 }; | 144 }; |
55 | 145 |
56 /** | 146 /** |
| 147 * @override |
| 148 */ |
| 149 MockFileEntry.prototype.clone = function(path) { |
| 150 return new MockFileEntry(this.filesystem, path, this.metadata); |
| 151 }; |
| 152 |
| 153 /** |
57 * Mock class for DirectoryEntry. | 154 * Mock class for DirectoryEntry. |
58 * | 155 * |
59 * @param {string} volumeId Id of the volume containing the entry. | 156 * @param {FileSystem} filesystem File system where the entry is localed. |
60 * @param {string} fullPath Full path for the entry. | 157 * @param {string} fullPath Full path for the entry. |
61 * @param {Object.<String, MockFileEntry|MockDirectoryEntry>} contents Map of | |
62 * path and MockEntry contained in the directory. | |
63 * @extends {MockEntry} | 158 * @extends {MockEntry} |
64 * @constructor | 159 * @constructor |
65 */ | 160 */ |
66 function MockDirectoryEntry(volumeId, fullPath, contents) { | 161 function MockDirectoryEntry(filesystem, fullPath) { |
67 MockEntry.call(this, volumeId, fullPath); | 162 MockEntry.call(this, filesystem, fullPath); |
68 this.contents_ = contents; | 163 this.isFile = false; |
| 164 this.isDirectory = true; |
69 } | 165 } |
70 | 166 |
71 MockDirectoryEntry.prototype = { | 167 MockDirectoryEntry.prototype = { |
72 __proto__: MockEntry.prototype | 168 __proto__: MockEntry.prototype |
73 }; | 169 }; |
74 | 170 |
75 /** | 171 /** |
| 172 * @override |
| 173 */ |
| 174 MockDirectoryEntry.prototype.clone = function(path) { |
| 175 return new MockDirectoryEntry(this.filesystem, path); |
| 176 }; |
| 177 |
| 178 /** |
76 * Returns a file under the directory. | 179 * Returns a file under the directory. |
77 * | 180 * |
78 * @param {string} path Path. | 181 * @param {string} path Path. |
79 * @param {Object} option Option. | 182 * @param {Object} option Option. |
80 * @param {callback(MockFileEntry)} successCallback Success callback. | 183 * @param {callback(MockFileEntry)} onSuccess Success callback. |
81 * @param {callback(Object)} failureCallback Failure callback; | 184 * @param {callback(Object)} onError Failure callback; |
82 */ | 185 */ |
83 MockDirectoryEntry.prototype.getFile = function( | 186 MockDirectoryEntry.prototype.getFile = function( |
84 path, option, successCallback, failureCallback) { | 187 path, option, onSuccess, onError) { |
85 if (!this.contents_[path]) | 188 var fullPath = path[0] === '/' ? path : joinPath(this.fullPath, path); |
86 failureCallback({name: util.FileError.NOT_FOUND_ERR}); | 189 if (!this.filesystem.entries[fullPath]) |
87 else if (!(this.contents_[path] instanceof MockFileEntry)) | 190 onError({name: util.FileError.NOT_FOUND_ERR}); |
88 failureCallback({name: util.FileError.TYPE_MISMATCH_ERR}); | 191 else if (!(this.filesystem.entries[fullPath] instanceof MockFileEntry)) |
| 192 onError({name: util.FileError.TYPE_MISMATCH_ERR}); |
89 else | 193 else |
90 successCallback(this.contents_[path]); | 194 onSuccess(this.filesystem.entries[fullPath]); |
91 }; | 195 }; |
92 | 196 |
93 /** | 197 /** |
94 * Returns a directory under the directory. | 198 * Returns a directory under the directory. |
95 * | 199 * |
96 * @param {string} path Path. | 200 * @param {string} path Path. |
97 * @param {Object} option Option. | 201 * @param {Object} option Option. |
98 * @param {callback(MockDirectoryEntry)} successCallback Success callback. | 202 * @param {callback(MockDirectoryEntry)} onSuccess Success callback. |
99 * @param {callback(Object)} failureCallback Failure callback; | 203 * @param {callback(Object)} onError Failure callback; |
100 */ | 204 */ |
101 MockDirectoryEntry.prototype.getDirectory = | 205 MockDirectoryEntry.prototype.getDirectory = |
102 function(path, option, successCallback, failureCallback) { | 206 function(path, option, onSuccess, onError) { |
103 if (!this.contents_[path]) | 207 var fullPath = path[0] === '/' ? path : joinPath(this.fullPath, path); |
104 failureCallback({name: util.FileError.NOT_FOUND_ERR}); | 208 if (!this.filesystem.entries[fullPath]) |
105 else if (!(this.contents_[path] instanceof MockDirectoryEntry)) | 209 onError({name: util.FileError.NOT_FOUND_ERR}); |
106 failureCallback({name: util.FileError.TYPE_MISMATCH_ERR}); | 210 else if (!(this.filesystem.entries[fullPath] instanceof MockDirectoryEntry)) |
| 211 onError({name: util.FileError.TYPE_MISMATCH_ERR}); |
107 else | 212 else |
108 successCallback(this.contents_[path]); | 213 onSuccess(this.filesystem.entries[fullPath]); |
109 }; | 214 }; |
OLD | NEW |