Commit c48bc24e authored by hirono@chromium.org's avatar hirono@chromium.org

Files.app: Add units test of FileOperationManager#paste.

BUG=315439
TEST=manually

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

Cr-Commit-Position: refs/heads/master@{#288317}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@288317 0039d316-1c4b-4281-b951-d872f2087c98
parent 6a2585b6
...@@ -3,6 +3,74 @@ ...@@ -3,6 +3,74 @@
// found in the LICENSE file. // found in the LICENSE file.
'use strict'; 'use strict';
/**
* Mock of chrome.runtime.
* @type {Object}
* @const
*/
chrome.runtime = {
lastError: null
};
/**
* Mock of chrome.power.
* @type {Object}
* @const
*/
chrome.power = {
requestKeepAwake: function() {
chrome.power.keepAwakeRequested = true;
},
releaseKeepAwake: function() {
chrome.power.keepAwakeRequested = false;
},
keepAwakeRequested: false
};
/**
* Mock of chrome.fileBrowserPrivate.
* @type {Object}
* @const
*/
chrome.fileBrowserPrivate = {
onCopyProgress: {
addListener: function(callback) {
chrome.fileBrowserPrivate.onCopyProgress.listener_ = callback;
},
removeListener: function() {
chrome.fileBrowserPrivate.onCopyProgress.listener_ = null;
},
listener_: null
},
startCopy: function(source, destination, newName, callback) {
var id = 1;
var events = [
'begin_copy_entry',
'progress',
'end_copy_entry',
'success'
].map(function(type) {
return [id, {type: type, sourceUrl: source, destinationUrl: destination}];
});
var sendEvent = function(index) {
// Call the function asynchronously.
return Promise.resolve().then(function() {
chrome.fileBrowserPrivate.onCopyProgress.listener_.apply(
null, events[index]);
if (index + 1 < events.length)
return sendEvent(index + 1);
else
return null;
}.bind(this));
}.bind(this);
callback(id);
sendEvent(0).catch(function(error) {
console.log(error.stack || error);
window.onerror();
});
}
};
/** /**
* Reports the result of promise to the test system. * Reports the result of promise to the test system.
* @param {Promise} promise Promise to be fulfilled or rejected. * @param {Promise} promise Promise to be fulfilled or rejected.
...@@ -22,6 +90,19 @@ function reportPromise(promise, callback) { ...@@ -22,6 +90,19 @@ function reportPromise(promise, callback) {
}); });
} }
/**
* Test target.
* @type {FileOperationManager}
*/
var fileOperationManager;
/**
* Initializes the test environment.
*/
function setUp() {
fileOperationManager = new FileOperationManager();
}
/** /**
* Tests the fileOperationUtil.resolvePath function. * Tests the fileOperationUtil.resolvePath function.
* @param {function(boolean:hasError)} callback Callback to be passed true on * @param {function(boolean:hasError)} callback Callback to be passed true on
...@@ -112,3 +193,56 @@ function testDeduplicatePath(callback) { ...@@ -112,3 +193,56 @@ function testDeduplicatePath(callback) {
]); ]);
reportPromise(testPromise, callback); reportPromise(testPromise, callback);
} }
/**
* Tests the fileOperationUtil.paste.
*/
function testCopy(callback) {
// Prepare entries and their resolver.
var sourceEntries =
[new MockFileEntry('testVolume', '/test.txt', {size: 10})];
var targetEntry = new MockDirectoryEntry('testVolume', '/', {});
window.webkitResolveLocalFileSystemURL = function(url, success, failure) {
if (url === sourceEntries[0].toURL())
success(sourceEntries[0]);
else if (url === targetEntry.toURL())
success(targetEntry);
else
failure();
};
// Observing manager's events.
var eventsPromise = new Promise(function(fulfill) {
var events = [];
fileOperationManager.addEventListener('copy-progress', function(event) {
events.push(event);
if (event.reason === 'SUCCESS')
fulfill(events);
});
fileOperationManager.addEventListener('entry-changed', function(event) {
events.push(event);
});
});
// Verify the events.
reportPromise(eventsPromise.then(function(events) {
var firstEvent = events[0];
assertEquals('BEGIN', firstEvent.reason);
assertEquals(1, firstEvent.status.numRemainingItems);
assertEquals(0, firstEvent.status.processedBytes);
assertEquals(1, firstEvent.status.totalBytes);
var lastEvent = events[events.length - 1];
assertEquals('SUCCESS', lastEvent.reason);
assertEquals(0, lastEvent.status.numRemainingItems);
assertEquals(10, lastEvent.status.processedBytes);
assertEquals(10, lastEvent.status.totalBytes);
assertTrue(events.some(function(event) {
return event.type === 'entry-changed' &&
event.entry === targetEntry;
}));
}), callback);
fileOperationManager.paste(sourceEntries, targetEntry, false);
}
...@@ -3,13 +3,13 @@ ...@@ -3,13 +3,13 @@
// found in the LICENSE file. // found in the LICENSE file.
/** /**
* Mock class for FileEntry. * Base class of mock entries.
* *
* @param {string} volumeId Id of the volume containing the entry. * @param {string} volumeId ID of the volume that contains the entry.
* @param {string} fullPath Full path for the entry. * @param {string} fullpath Full path of the entry.
* @constructor * @constructor
*/ */
function MockFileEntry(volumeId, fullPath) { function MockEntry(volumeId, fullPath) {
this.volumeId = volumeId; this.volumeId = volumeId;
this.fullPath = fullPath; this.fullPath = fullPath;
} }
...@@ -19,10 +19,40 @@ function MockFileEntry(volumeId, fullPath) { ...@@ -19,10 +19,40 @@ function MockFileEntry(volumeId, fullPath) {
* *
* @return {string} Fake URL. * @return {string} Fake URL.
*/ */
MockFileEntry.prototype.toURL = function() { MockEntry.prototype.toURL = function() {
return 'filesystem:' + this.volumeId + this.fullPath; return 'filesystem:' + this.volumeId + this.fullPath;
}; };
/**
* Mock class for FileEntry.
*
* @param {string} volumeId Id of the volume containing the entry.
* @param {string} fullPath Full path for the entry.
* @extends {MockEntry}
* @constructor
*/
function MockFileEntry(volumeId, fullPath, metadata) {
MockEntry.call(this, volumeId, fullPath);
this.volumeId = volumeId;
this.fullPath = fullPath;
this.metadata_ = metadata;
}
MockFileEntry.prototype = {
__proto__: MockEntry.prototype
};
/**
* Obtains metadata of the entry.
* @param {function(Object)} callback Function to take the metadata.
*/
MockFileEntry.prototype.getMetadata = function(callback) {
Promise.resolve(this.metadata_).then(callback).catch(function(error) {
console.error(error.stack || error);
window.onerror();
});
};
/** /**
* Mock class for DirectoryEntry. * Mock class for DirectoryEntry.
* *
...@@ -30,12 +60,18 @@ MockFileEntry.prototype.toURL = function() { ...@@ -30,12 +60,18 @@ MockFileEntry.prototype.toURL = function() {
* @param {string} fullPath Full path for the entry. * @param {string} fullPath Full path for the entry.
* @param {Object.<String, MockFileEntry|MockDirectoryEntry>} contents Map of * @param {Object.<String, MockFileEntry|MockDirectoryEntry>} contents Map of
* path and MockEntry contained in the directory. * path and MockEntry contained in the directory.
* @extends {MockEntry}
* @constructor * @constructor
*/ */
function MockDirectoryEntry(volumeId, fullPath, contents) { function MockDirectoryEntry(volumeId, fullPath, contents) {
MockEntry.call(this, volumeId, fullPath);
this.contents_ = contents; this.contents_ = contents;
} }
MockDirectoryEntry.prototype = {
__proto__: MockEntry.prototype
};
/** /**
* Returns a file under the directory. * Returns a file under the directory.
* *
......
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