Commit 8a07e58b authored by hirono@chromium.org's avatar hirono@chromium.org

Files.app: Add more unit tests for FileOperationManager class.

The CL adds tests for 

 * Copy
 * Delete
 * Zip

operations of the class.

BUG=315439
TEST=run the test

Review URL: https://codereview.chromium.org/460963002

Cr-Commit-Position: refs/heads/master@{#289622}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@289622 0039d316-1c4b-4281-b951-d872f2087c98
parent b5ed0496
...@@ -2,40 +2,130 @@ ...@@ -2,40 +2,130 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
/**
* Joins paths so that the two paths are connected by only 1 '/'.
* @param {string} a Path.
* @param {string} b Path.
* @return {string} Joined path.
*/
function joinPath(a, b) {
return a.replace(/\/+$/, '') + '/' + b.replace(/^\/+/, '');
};
/**
* Test file system.
*
* @param {string} fileSystemId File system ID.
* @constructor
*/
function TestFileSystem(fileSystemId) {
this.fileSystemId = fileSystemId;
this.entries = {};
};
TestFileSystem.prototype = {
get root() { return this.entries['/']; }
};
/** /**
* Base class of mock entries. * Base class of mock entries.
* *
* @param {string} volumeId ID of the volume that contains the entry. * @param {TestFileSystem} filesystem File system where the entry is localed.
* @param {string} fullpath Full path of the entry. * @param {string} fullpath Full path of the entry.
* @constructor * @constructor
*/ */
function MockEntry(volumeId, fullPath) { function MockEntry(filesystem, fullPath) {
this.volumeId = volumeId; this.filesystem = filesystem;
this.fullPath = fullPath; this.fullPath = fullPath;
} }
MockEntry.prototype = {
/**
* @return {string} Name of the entry.
*/
get name() {
return this.fullPath.replace(/^.*\//, '');
}
};
/** /**
* Returns fake URL. * Returns fake URL.
* *
* @return {string} Fake URL. * @return {string} Fake URL.
*/ */
MockEntry.prototype.toURL = function() { MockEntry.prototype.toURL = function() {
return 'filesystem:' + this.volumeId + this.fullPath; return 'filesystem:' + this.filesystem.fileSystemId + this.fullPath;
};
/**
* Obtains parent directory.
*
* @param {function(MockDirectoryEntry)} onSuccess Callback invoked with
* the parent directory.
* @param {function(Object)} onError Callback invoked with an error
* object.
*/
MockEntry.prototype.getParent = function(
onSuccess, onError) {
var path = this.fullPath.replace(/\/[^\/]+$/, '') || '/';
if (this.filesystem.entries[path])
onSuccess(this.filesystem.entries[path]);
else
onError({name: util.FileError.NOT_FOUND_ERR});
};
/**
* Moves the entry to the directory.
*
* @param {MockDirectoryEntry} parent Destination directory.
* @param {string=} opt_newName New name.
* @param {function(MockDirectoryEntry)} onSuccess Callback invoked with the
* moved entry.
* @param {function(Object)} onError Callback invoked with an error object.
*/
MockEntry.prototype.moveTo = function(parent, opt_newName, onSuccess, onError) {
Promise.resolve().then(function() {
this.filesystem.entries[this.fullPath] = null;
return this.clone(joinPath(parent.fullPath, opt_newName || this.name));
}.bind(this)).then(onSuccess, onError);
};
/**
* Removes the entry.
*
* @param {function()} onSuccess Success callback.
* @param {function(Object)} onError Callback invoked with an error object.
*/
MockEntry.prototype.remove = function(onSuccess, onError) {
Promise.resolve().then(function() {
this.filesystem.entries[this.fullPath] = null;
}.bind(this)).then(onSuccess, onError);
};
/**
* Clones the entry with the new fullpath.
*
* @param {string} fullpath New fullpath.
* @return {MockEntry} Cloned entry.
*/
MockEntry.prototype.clone = function(fullpath) {
throw new Error('Not implemented.');
}; };
/** /**
* Mock class for FileEntry. * Mock class for FileEntry.
* *
* @param {string} volumeId Id of the volume containing the entry. * @param {FileSystem} filesystem File system where the entry is localed.
* @param {string} fullPath Full path for the entry. * @param {string} fullPath Full path for the entry.
* @param {Object} metadata Metadata.
* @extends {MockEntry} * @extends {MockEntry}
* @constructor * @constructor
*/ */
function MockFileEntry(volumeId, fullPath, metadata) { function MockFileEntry(filesystem, fullPath, metadata) {
MockEntry.call(this, volumeId, fullPath); MockEntry.call(this, filesystem, fullPath);
this.volumeId = volumeId;
this.fullPath = fullPath;
this.metadata_ = metadata; this.metadata_ = metadata;
this.isFile = true;
this.isDirectory = false;
} }
MockFileEntry.prototype = { MockFileEntry.prototype = {
...@@ -53,41 +143,55 @@ MockFileEntry.prototype.getMetadata = function(callback) { ...@@ -53,41 +143,55 @@ MockFileEntry.prototype.getMetadata = function(callback) {
}); });
}; };
/**
* @override
*/
MockFileEntry.prototype.clone = function(path) {
return new MockFileEntry(this.filesystem, path, this.metadata);
};
/** /**
* Mock class for DirectoryEntry. * Mock class for DirectoryEntry.
* *
* @param {string} volumeId Id of the volume containing the entry. * @param {FileSystem} filesystem File system where the entry is localed.
* @param {string} fullPath Full path for the entry. * @param {string} fullPath Full path for the entry.
* @param {Object.<String, MockFileEntry|MockDirectoryEntry>} contents Map of
* path and MockEntry contained in the directory.
* @extends {MockEntry} * @extends {MockEntry}
* @constructor * @constructor
*/ */
function MockDirectoryEntry(volumeId, fullPath, contents) { function MockDirectoryEntry(filesystem, fullPath) {
MockEntry.call(this, volumeId, fullPath); MockEntry.call(this, filesystem, fullPath);
this.contents_ = contents; this.isFile = false;
this.isDirectory = true;
} }
MockDirectoryEntry.prototype = { MockDirectoryEntry.prototype = {
__proto__: MockEntry.prototype __proto__: MockEntry.prototype
}; };
/**
* @override
*/
MockDirectoryEntry.prototype.clone = function(path) {
return new MockDirectoryEntry(this.filesystem, path);
};
/** /**
* Returns a file under the directory. * Returns a file under the directory.
* *
* @param {string} path Path. * @param {string} path Path.
* @param {Object} option Option. * @param {Object} option Option.
* @param {callback(MockFileEntry)} successCallback Success callback. * @param {callback(MockFileEntry)} onSuccess Success callback.
* @param {callback(Object)} failureCallback Failure callback; * @param {callback(Object)} onError Failure callback;
*/ */
MockDirectoryEntry.prototype.getFile = function( MockDirectoryEntry.prototype.getFile = function(
path, option, successCallback, failureCallback) { path, option, onSuccess, onError) {
if (!this.contents_[path]) var fullPath = path[0] === '/' ? path : joinPath(this.fullPath, path);
failureCallback({name: util.FileError.NOT_FOUND_ERR}); if (!this.filesystem.entries[fullPath])
else if (!(this.contents_[path] instanceof MockFileEntry)) onError({name: util.FileError.NOT_FOUND_ERR});
failureCallback({name: util.FileError.TYPE_MISMATCH_ERR}); else if (!(this.filesystem.entries[fullPath] instanceof MockFileEntry))
onError({name: util.FileError.TYPE_MISMATCH_ERR});
else else
successCallback(this.contents_[path]); onSuccess(this.filesystem.entries[fullPath]);
}; };
/** /**
...@@ -95,15 +199,16 @@ MockDirectoryEntry.prototype.getFile = function( ...@@ -95,15 +199,16 @@ MockDirectoryEntry.prototype.getFile = function(
* *
* @param {string} path Path. * @param {string} path Path.
* @param {Object} option Option. * @param {Object} option Option.
* @param {callback(MockDirectoryEntry)} successCallback Success callback. * @param {callback(MockDirectoryEntry)} onSuccess Success callback.
* @param {callback(Object)} failureCallback Failure callback; * @param {callback(Object)} onError Failure callback;
*/ */
MockDirectoryEntry.prototype.getDirectory = MockDirectoryEntry.prototype.getDirectory =
function(path, option, successCallback, failureCallback) { function(path, option, onSuccess, onError) {
if (!this.contents_[path]) var fullPath = path[0] === '/' ? path : joinPath(this.fullPath, path);
failureCallback({name: util.FileError.NOT_FOUND_ERR}); if (!this.filesystem.entries[fullPath])
else if (!(this.contents_[path] instanceof MockDirectoryEntry)) onError({name: util.FileError.NOT_FOUND_ERR});
failureCallback({name: util.FileError.TYPE_MISMATCH_ERR}); else if (!(this.filesystem.entries[fullPath] instanceof MockDirectoryEntry))
onError({name: util.FileError.TYPE_MISMATCH_ERR});
else else
successCallback(this.contents_[path]); onSuccess(this.filesystem.entries[fullPath]);
}; };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment