Commit d96084d3 authored by Noel Gordon's avatar Noel Gordon Committed by Commit Bot

Closure compile file_operation_manager_unittest

 - remove file_operation_manager_unittest.html
 - add BUILD rules to auto-generate and compile this unittest
 - define mockChrome and install it in unittest Setup()
 - fix Closure compile errors

No change in test behavior, no new tests.

Bug: 907967
Change-Id: I523218dd179c5e9b0ae55b81d4a5480eea279f8c
Reviewed-on: https://chromium-review.googlesource.com/c/1352115Reviewed-by: default avatarStuart Langley <slangley@chromium.org>
Commit-Queue: Noel Gordon <noel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#611501}
parent c362c846
...@@ -39,8 +39,7 @@ IN_PROC_BROWSER_TEST_F(FileManagerJsTest, DeviceHandlerTest) { ...@@ -39,8 +39,7 @@ IN_PROC_BROWSER_TEST_F(FileManagerJsTest, DeviceHandlerTest) {
} }
IN_PROC_BROWSER_TEST_F(FileManagerJsTest, FileOperationManagerTest) { IN_PROC_BROWSER_TEST_F(FileManagerJsTest, FileOperationManagerTest) {
RunTest(base::FilePath( RunGeneratedTest("/background/js/file_operation_manager_unittest.html");
FILE_PATH_LITERAL("background/js/file_operation_manager_unittest.html")));
} }
IN_PROC_BROWSER_TEST_F(FileManagerJsTest, DriveSyncHandlerTest) { IN_PROC_BROWSER_TEST_F(FileManagerJsTest, DriveSyncHandlerTest) {
......
...@@ -242,6 +242,7 @@ js_library("mock_file_operation_manager") { ...@@ -242,6 +242,7 @@ js_library("mock_file_operation_manager") {
testonly = true testonly = true
deps = [ deps = [
":file_operation_manager", ":file_operation_manager",
"//ui/webui/resources/js/cr:event_target",
] ]
externs_list = [ "../../../externs/background/file_operation_manager.js" ] externs_list = [ "../../../externs/background/file_operation_manager.js" ]
} }
...@@ -253,6 +254,17 @@ js_library("file_operation_manager") { ...@@ -253,6 +254,17 @@ js_library("file_operation_manager") {
"//ui/webui/resources/js:cr", "//ui/webui/resources/js:cr",
"//ui/webui/resources/js/cr:event_target", "//ui/webui/resources/js/cr:event_target",
] ]
externs_list = [ "../../../externs/background/file_operation_manager.js" ]
}
js_unittest("file_operation_manager_unittest") {
deps = [
":file_operation_manager",
":metadata_proxy",
"//ui/file_manager/base/js:mock_chrome",
"//ui/file_manager/base/js:test_error_reporting",
"//ui/file_manager/file_manager/common/js:mock_entry",
]
} }
js_library("file_operation_util") { js_library("file_operation_util") {
...@@ -476,6 +488,7 @@ js_unit_tests("unit_tests") { ...@@ -476,6 +488,7 @@ js_unit_tests("unit_tests") {
":device_handler_unittest", ":device_handler_unittest",
":drive_sync_handler_unittest", ":drive_sync_handler_unittest",
":duplicate_finder_unittest", ":duplicate_finder_unittest",
":file_operation_manager_unittest",
":import_history_unittest", ":import_history_unittest",
":media_scanner_unittest", ":media_scanner_unittest",
":task_queue_unittest", ":task_queue_unittest",
......
<!DOCTYPE html>
<!-- Copyright 2014 The Chromium Authors. All rights reserved.
-- Use of this source code is governed by a BSD-style license that can be
-- found in the LICENSE file.
-->
<html>
<body>
<script src="../../../../../ui/webui/resources/js/cr.js"></script>
<script src="../../../../../ui/webui/resources/js/cr/event_target.js"></script>
<script src="../../common/js/async_util.js"></script>
<script src="../../../base/js/test_error_reporting.js"></script>
<script src="../../common/js/util.js"></script>
<script src="../../common/js/lru_cache.js"></script>
<script src="../../common/js/mock_entry.js"></script>
<script src="metadata_proxy.js"></script>
<script src="file_operation_manager.js"></script>
<script src="file_operation_util.js"></script>
<script src="file_operation_manager_unittest.js"></script>
</body>
</html>
...@@ -4,50 +4,41 @@ ...@@ -4,50 +4,41 @@
'use strict'; 'use strict';
/** /**
* Mock of chrome.runtime. * Mock chrome APIs.
* @type {Object} * @type {Object}
* @const
*/ */
chrome.runtime = { var mockChrome = {};
mockChrome.runtime = {
lastError: null lastError: null
}; };
/** mockChrome.power = {
* Mock of chrome.power.
* @type {Object}
* @const
*/
chrome.power = {
requestKeepAwake: function() { requestKeepAwake: function() {
chrome.power.keepAwakeRequested = true; mockChrome.power.keepAwakeRequested = true;
}, },
releaseKeepAwake: function() { releaseKeepAwake: function() {
chrome.power.keepAwakeRequested = false; mockChrome.power.keepAwakeRequested = false;
}, },
keepAwakeRequested: false keepAwakeRequested: false
}; };
/** mockChrome.fileManagerPrivate = {
* Mock of chrome.fileManagerPrivate.
* @type {Object}
* @const
*/
chrome.fileManagerPrivate = {
onCopyProgress: { onCopyProgress: {
addListener: function(callback) { addListener: function(callback) {
chrome.fileManagerPrivate.onCopyProgress.listener_ = callback; mockChrome.fileManagerPrivate.onCopyProgress.listener_ = callback;
}, },
removeListener: function() { removeListener: function() {
chrome.fileManagerPrivate.onCopyProgress.listener_ = null; mockChrome.fileManagerPrivate.onCopyProgress.listener_ = null;
}, },
listener_: null listener_: null
} }
}; };
/** /**
* Logs events of file operation manager. * Logs copy-progress events from a file operation manager.
* @param {!FileOperationManager} fileOperationManager A target file operation * @param {!FileOperationManager} fileOperationManager The target file
* manager. * operation manager.
* @constructor * @constructor
* @struct * @struct
*/ */
...@@ -61,11 +52,12 @@ function EventLogger(fileOperationManager) { ...@@ -61,11 +52,12 @@ function EventLogger(fileOperationManager) {
} }
/** /**
* Handles copy-progress event. * Log file operation manager copy-progress event details.
* @param {Event} event An event. * @param {Event} event An event.
* @private * @private
*/ */
EventLogger.prototype.onCopyProgress_ = function(event) { EventLogger.prototype.onCopyProgress_ = function(event) {
event = /** @type {FileOperationProgressEvent} */ (event);
if (event.reason === 'BEGIN') { if (event.reason === 'BEGIN') {
this.events.push(event); this.events.push(event);
this.numberOfBeginEvents++; this.numberOfBeginEvents++;
...@@ -85,7 +77,7 @@ EventLogger.prototype.onCopyProgress_ = function(event) { ...@@ -85,7 +77,7 @@ EventLogger.prototype.onCopyProgress_ = function(event) {
* @param {string} blockedDestination Destination url of an entry whose request * @param {string} blockedDestination Destination url of an entry whose request
* should be blocked. * should be blocked.
* @param {!Entry} sourceEntry Source entry. Single source entry is supported. * @param {!Entry} sourceEntry Source entry. Single source entry is supported.
* @param {!Array<!FakeFileSystem>} fileSystems File systems. * @param {!Array<!MockFileSystem>} fileSystems File systems array.
* @constructor * @constructor
* @struct * @struct
*/ */
...@@ -98,7 +90,7 @@ function BlockableFakeStartCopy(blockedDestination, sourceEntry, fileSystems) { ...@@ -98,7 +90,7 @@ function BlockableFakeStartCopy(blockedDestination, sourceEntry, fileSystems) {
} }
/** /**
* A fake implemencation of startCopy function. * Fake implementation of startCopy function.
* @param {!Entry} source * @param {!Entry} source
* @param {!Entry} destination * @param {!Entry} destination
* @param {string} newName * @param {string} newName
...@@ -118,7 +110,9 @@ BlockableFakeStartCopy.prototype.startCopyFunc = function( ...@@ -118,7 +110,9 @@ BlockableFakeStartCopy.prototype.startCopyFunc = function(
var newPath = joinPath('/', newName); var newPath = joinPath('/', newName);
var fileSystem = getFileSystemForURL( var fileSystem = getFileSystemForURL(
this.fileSystems_, destination.toURL()); this.fileSystems_, destination.toURL());
fileSystem.entries[newPath] = this.sourceEntry_.clone(newPath); var mockEntry = /** @type {!MockEntry} */ (this.sourceEntry_);
fileSystem.entries[newPath] =
/** @type {!MockEntry} */ (mockEntry.clone(newPath));
listener(copyId, makeStatus('end_copy_entry')); listener(copyId, makeStatus('end_copy_entry'));
listener(copyId, makeStatus('success')); listener(copyId, makeStatus('success'));
}.bind(this); }.bind(this);
...@@ -126,7 +120,7 @@ BlockableFakeStartCopy.prototype.startCopyFunc = function( ...@@ -126,7 +120,7 @@ BlockableFakeStartCopy.prototype.startCopyFunc = function(
this.startCopyId_++; this.startCopyId_++;
callback(this.startCopyId_); callback(this.startCopyId_);
var listener = chrome.fileManagerPrivate.onCopyProgress.listener_; var listener = mockChrome.fileManagerPrivate.onCopyProgress.listener_;
listener(this.startCopyId_, makeStatus('begin_copy_entry')); listener(this.startCopyId_, makeStatus('begin_copy_entry'));
listener(this.startCopyId_, makeStatus('progress')); listener(this.startCopyId_, makeStatus('progress'));
...@@ -141,14 +135,14 @@ BlockableFakeStartCopy.prototype.startCopyFunc = function( ...@@ -141,14 +135,14 @@ BlockableFakeStartCopy.prototype.startCopyFunc = function(
/** /**
* Fake volume manager. * Fake volume manager.
* @constructor * @constructor
* @structs * @struct
*/ */
function FakeVolumeManager() {} function FakeVolumeManager() {}
/** /**
* Returns fake volume info. * Returns fake volume info.
* @param {!Entry} entry * @param {!Entry} entry
* @return {VolumeInfo} A fake volume info. * @return {!Object}
*/ */
FakeVolumeManager.prototype.getVolumeInfo = function(entry) { FakeVolumeManager.prototype.getVolumeInfo = function(entry) {
return { volumeId: entry.filesystem.name }; return { volumeId: entry.filesystem.name };
...@@ -156,16 +150,18 @@ FakeVolumeManager.prototype.getVolumeInfo = function(entry) { ...@@ -156,16 +150,18 @@ FakeVolumeManager.prototype.getVolumeInfo = function(entry) {
/** /**
* Returns file system of the url. * Returns file system of the url.
* @param {!Array<!FakeFileSystem>} fileSystems * @param {!Array<!MockFileSystem>} fileSystems
* @param {string} url * @param {string} url
* @return {!FakeFileSystem} * @return {!MockFileSystem}
*/ */
function getFileSystemForURL(fileSystems, url) { function getFileSystemForURL(fileSystems, url) {
for (var i = 0; i < fileSystems.length; i++) { for (var i = 0; i < fileSystems.length; i++) {
if (new RegExp('^filesystem:' + fileSystems[i].name + '/').test(url)) if (new RegExp('^filesystem:' + fileSystems[i].name + '/').test(url)) {
return fileSystems[i]; return fileSystems[i];
}
} }
throw new Error('Unexpected url: ' + url + '.');
throw new Error('Unexpected url: ' + url);
} }
/** /**
...@@ -177,9 +173,10 @@ var DIRECTORY_SIZE = -1; ...@@ -177,9 +173,10 @@ var DIRECTORY_SIZE = -1;
/** /**
* Creates test file system. * Creates test file system.
* @param {string} id File system ID. * @param {string} id File system Id.
* @param {Object<number>} entries Map of entries' paths and their size. * @param {Object<number>} entries Map of entry paths and their size.
* If the size is equals to DIRECTORY_SIZE, the entry is directory. * If the entry size is DIRECTORY_SIZE, the entry is a directory.
* @return {!MockFileSystem}
*/ */
function createTestFileSystem(id, entries) { function createTestFileSystem(id, entries) {
var fileSystem = new MockFileSystem(id, 'filesystem:' + id); var fileSystem = new MockFileSystem(id, 'filesystem:' + id);
...@@ -187,8 +184,8 @@ function createTestFileSystem(id, entries) { ...@@ -187,8 +184,8 @@ function createTestFileSystem(id, entries) {
if (entries[path] === DIRECTORY_SIZE) { if (entries[path] === DIRECTORY_SIZE) {
fileSystem.entries[path] = new MockDirectoryEntry(fileSystem, path); fileSystem.entries[path] = new MockDirectoryEntry(fileSystem, path);
} else { } else {
fileSystem.entries[path] = var metadata = /** @type {!Metadata} */ ({size: entries[path]});
new MockFileEntry(fileSystem, path, {size: entries[path]}); fileSystem.entries[path] = new MockFileEntry(fileSystem, path, metadata);
} }
} }
return fileSystem; return fileSystem;
...@@ -196,12 +193,12 @@ function createTestFileSystem(id, entries) { ...@@ -196,12 +193,12 @@ function createTestFileSystem(id, entries) {
/** /**
* Resolves URL on the file system. * Resolves URL on the file system.
* @param {FakeFileSystem} fileSystem Fake file system. * @param {!MockFileSystem} fileSystem File system.
* @param {string} url URL. * @param {string} url URL.
* @param {function(MockEntry)} success Success callback. * @param {function(!Entry)} success Success callback.
* @param {function()} failure Failure callback. * @param {function(!FileError)=} opt_failure Failure callback.
*/ */
function resolveTestFileSystemURL(fileSystem, url, success, failure) { function resolveTestFileSystemURL(fileSystem, url, success, opt_failure) {
for (var name in fileSystem.entries) { for (var name in fileSystem.entries) {
var entry = fileSystem.entries[name]; var entry = fileSystem.entries[name];
if (entry.toURL() == url) { if (entry.toURL() == url) {
...@@ -209,7 +206,10 @@ function resolveTestFileSystemURL(fileSystem, url, success, failure) { ...@@ -209,7 +206,10 @@ function resolveTestFileSystemURL(fileSystem, url, success, failure) {
return; return;
} }
} }
failure();
if (opt_failure) {
opt_failure(new FileError());
}
} }
/** /**
...@@ -221,14 +221,17 @@ function waitForEvents(fileOperationManager) { ...@@ -221,14 +221,17 @@ function waitForEvents(fileOperationManager) {
return new Promise(function(fulfill) { return new Promise(function(fulfill) {
var events = []; var events = [];
fileOperationManager.addEventListener('copy-progress', function(event) { fileOperationManager.addEventListener('copy-progress', function(event) {
event = /** @type {FileOperationProgressEvent} */ (event);
events.push(event); events.push(event);
if (event.reason === 'SUCCESS') if (event.reason === 'SUCCESS')
fulfill(events); fulfill(events);
}); });
fileOperationManager.addEventListener('entries-changed', function(event) { fileOperationManager.addEventListener('entries-changed', function(event) {
event = /** @type {FileOperationProgressEvent} */ (event);
events.push(event); events.push(event);
}); });
fileOperationManager.addEventListener('delete', function(event) { fileOperationManager.addEventListener('delete', function(event) {
event = /** @type {FileOperationProgressEvent} */ (event);
events.push(event); events.push(event);
if (event.reason === 'SUCCESS') if (event.reason === 'SUCCESS')
fulfill(events); fulfill(events);
...@@ -245,9 +248,9 @@ var volumeManager; ...@@ -245,9 +248,9 @@ var volumeManager;
var volumeManagerFactory = {}; var volumeManagerFactory = {};
/** /**
* Provide VolumeManager.getInstande() for FileOperationManager using mocked * Provide VolumeManager.getInstance() for FileOperationManager using mocked
* volume manager instance. * volume manager instance.
* @type {!Promise<(FakeVolumeManager|{getVolumeInfo: function()}?)>} * @return {Promise}
*/ */
volumeManagerFactory.getInstance = function() { volumeManagerFactory.getInstance = function() {
return Promise.resolve(volumeManager); return Promise.resolve(volumeManager);
...@@ -263,12 +266,13 @@ var fileOperationManager; ...@@ -263,12 +266,13 @@ var fileOperationManager;
* Initializes the test environment. * Initializes the test environment.
*/ */
function setUp() { function setUp() {
// Install mock chrome APIs.
installMockChrome(mockChrome);
} }
/** /**
* Tests the fileOperationUtil.resolvePath function. * Tests the fileOperationUtil.resolvePath function.
* @param {function(boolean:hasError)} callback Callback to be passed true on * @param {function(boolean)} callback Callback to be passed true on error.
* error.
*/ */
function testResolvePath(callback) { function testResolvePath(callback) {
var fileSystem = createTestFileSystem('testVolume', { var fileSystem = createTestFileSystem('testVolume', {
...@@ -299,8 +303,7 @@ function testResolvePath(callback) { ...@@ -299,8 +303,7 @@ function testResolvePath(callback) {
} }
/** /**
* @param {function(boolean)} callback Callback to be passed true on * @param {function(boolean)} callback Callback to be passed true on error.
* error.
*/ */
function testFindEntriesRecursively(callback) { function testFindEntriesRecursively(callback) {
var fileSystem = createTestFileSystem('testVolume', { var fileSystem = createTestFileSystem('testVolume', {
...@@ -319,22 +322,24 @@ function testFindEntriesRecursively(callback) { ...@@ -319,22 +322,24 @@ function testFindEntriesRecursively(callback) {
}); });
var foundFiles = []; var foundFiles = [];
fileOperationUtil.findEntriesRecursively( fileOperationUtil
fileSystem.root, .findEntriesRecursively(
function(fileEntry) { fileSystem.root,
foundFiles.push(fileEntry); function(fileEntry) {
}) foundFiles.push(fileEntry);
.then(
function() {
assertEquals(12, foundFiles.length);
callback(false);
}) })
.catch(callback); .then(function() {
assertEquals(12, foundFiles.length);
callback(false);
})
.catch(function() {
var error = true;
callback(error);
});
} }
/** /**
* @param {function(boolean)} callback Callback to be passed true on * @param {function(boolean)} callback Callback to be passed true on error.
* error.
*/ */
function testFindFilesRecursively(callback) { function testFindFilesRecursively(callback) {
var fileSystem = createTestFileSystem('testVolume', { var fileSystem = createTestFileSystem('testVolume', {
...@@ -353,11 +358,12 @@ function testFindFilesRecursively(callback) { ...@@ -353,11 +358,12 @@ function testFindFilesRecursively(callback) {
}); });
var foundFiles = []; var foundFiles = [];
fileOperationUtil.findFilesRecursively( fileOperationUtil
fileSystem.root, .findFilesRecursively(
function(fileEntry) { fileSystem.root,
foundFiles.push(fileEntry); function(fileEntry) {
}) foundFiles.push(fileEntry);
})
.then( .then(
function() { function() {
assertEquals(10, foundFiles.length); assertEquals(10, foundFiles.length);
...@@ -367,12 +373,14 @@ function testFindFilesRecursively(callback) { ...@@ -367,12 +373,14 @@ function testFindFilesRecursively(callback) {
}); });
callback(false); callback(false);
}) })
.catch(callback); .catch(function() {
var error = true;
callback(error);
});
} }
/** /**
* @param {function(boolean)} callback Callback to be passed true on * @param {function(boolean)} callback Callback to be passed true on error.
* error.
*/ */
function testGatherEntriesRecursively(callback) { function testGatherEntriesRecursively(callback) {
var fileSystem = createTestFileSystem('testVolume', { var fileSystem = createTestFileSystem('testVolume', {
...@@ -391,18 +399,19 @@ function testGatherEntriesRecursively(callback) { ...@@ -391,18 +399,19 @@ function testGatherEntriesRecursively(callback) {
}); });
fileOperationUtil.gatherEntriesRecursively(fileSystem.root) fileOperationUtil.gatherEntriesRecursively(fileSystem.root)
.then( .then(function(gatheredFiles) {
function(gatheredFiles) { assertEquals(12, gatheredFiles.length);
assertEquals(12, gatheredFiles.length); callback(false);
callback(false); })
}) .catch(function() {
.catch(callback); var error = true;
callback(error);
});
} }
/** /**
* Tests the fileOperationUtil.deduplicatePath * Tests the fileOperationUtil.deduplicatePath
* @param {function(boolean:hasError)} callback Callback to be passed true on * @param {function(boolean)} callback Callback to be passed true on error.
* error.
*/ */
function testDeduplicatePath(callback) { function testDeduplicatePath(callback) {
var fileSystem1 = createTestFileSystem('testVolume', {'/': DIRECTORY_SIZE}); var fileSystem1 = createTestFileSystem('testVolume', {'/': DIRECTORY_SIZE});
...@@ -449,9 +458,8 @@ function testDeduplicatePath(callback) { ...@@ -449,9 +458,8 @@ function testDeduplicatePath(callback) {
} }
/** /**
* Tests the fileOperationUtil.paste. * Tests fileOperationManager copy.
* @param {function(boolean:hasError)} callback Callback to be passed true on * @param {function(boolean)} callback Callback to be passed true on error.
* error.
*/ */
function testCopy(callback) { function testCopy(callback) {
// Prepare entries and their resolver. // Prepare entries and their resolver.
...@@ -459,33 +467,36 @@ function testCopy(callback) { ...@@ -459,33 +467,36 @@ function testCopy(callback) {
'/': DIRECTORY_SIZE, '/': DIRECTORY_SIZE,
'/test.txt': 10, '/test.txt': 10,
}); });
window.webkitResolveLocalFileSystemURL = window.webkitResolveLocalFileSystemURL = function(url, success, failure) {
resolveTestFileSystemURL.bind(null, fileSystem); resolveTestFileSystemURL(fileSystem, url, success, failure);
};
chrome.fileManagerPrivate.startCopy =
function(source, destination, newName, callback) { mockChrome.fileManagerPrivate.startCopy = function(
var makeStatus = function(type) { source, destination, newName, callback) {
return { var makeStatus = function(type) {
type: type, return {
sourceUrl: source.toURL(), type: type,
destinationUrl: destination.toURL() sourceUrl: source.toURL(),
}; destinationUrl: destination.toURL()
};
callback(1);
var listener = chrome.fileManagerPrivate.onCopyProgress.listener_;
listener(1, makeStatus('begin_copy_entry'));
listener(1, makeStatus('progress'));
var newPath = joinPath('/', newName);
fileSystem.entries[newPath] =
fileSystem.entries['/test.txt'].clone(newPath);
listener(1, makeStatus('end_copy_entry'));
listener(1, makeStatus('success'));
}; };
};
callback(1);
var listener = mockChrome.fileManagerPrivate.onCopyProgress.listener_;
listener(1, makeStatus('begin_copy_entry'));
listener(1, makeStatus('progress'));
var newPath = joinPath('/', newName);
var entry = /** @type {!MockEntry} */
(fileSystem.entries['/test.txt']);
fileSystem.entries[newPath] =
/** @type {!MockEntry} */ (entry.clone(newPath));
listener(1, makeStatus('end_copy_entry'));
listener(1, makeStatus('success'));
};
volumeManager = new FakeVolumeManager(); volumeManager = new FakeVolumeManager();
fileOperationManager = new FileOperationManagerImpl(); fileOperationManager = new FileOperationManagerImpl();
// Observing manager's events. // Observe the file operation manager's events.
var eventsPromise = waitForEvents(fileOperationManager); var eventsPromise = waitForEvents(fileOperationManager);
// Verify the events. // Verify the events.
...@@ -515,40 +526,40 @@ function testCopy(callback) { ...@@ -515,40 +526,40 @@ function testCopy(callback) {
fileOperationManager.paste( fileOperationManager.paste(
[fileSystem.entries['/test.txt']], [fileSystem.entries['/test.txt']],
fileSystem.entries['/'], /** @type {!DirectoryEntry} */ (fileSystem.entries['/']), false);
false);
} }
/** /**
* Tests the fileOperationUtil.paste for copying files in sequential. When * Tests copying files when the destination volumes are same: the copy
* destination volumes are same, copy operations should run in sequential. * operations should be run sequentially.
*/ */
function testCopyInSequential(callback) { function testCopyInSequential(callback) {
// Prepare entries and their resolver.
var fileSystem = createTestFileSystem('testVolume', { var fileSystem = createTestFileSystem('testVolume', {
'/': DIRECTORY_SIZE, '/': DIRECTORY_SIZE,
'/dest': DIRECTORY_SIZE, '/dest': DIRECTORY_SIZE,
'/test.txt': 10 '/test.txt': 10
}); });
window.webkitResolveLocalFileSystemURL = function(url, success, failure) {
window.webkitResolveLocalFileSystemURL = resolveTestFileSystemURL(fileSystem, url, success, failure);
resolveTestFileSystemURL.bind(null, fileSystem); };
var blockableFakeStartCopy = new BlockableFakeStartCopy( var blockableFakeStartCopy = new BlockableFakeStartCopy(
'filesystem:testVolume/dest', 'filesystem:testVolume/dest', fileSystem.entries['/test.txt'],
fileSystem.entries['/test.txt'],
[fileSystem]); [fileSystem]);
chrome.fileManagerPrivate.startCopy = mockChrome.fileManagerPrivate.startCopy =
blockableFakeStartCopy.startCopyFunc.bind(blockableFakeStartCopy); blockableFakeStartCopy.startCopyFunc.bind(blockableFakeStartCopy);
volumeManager = new FakeVolumeManager(); volumeManager = new FakeVolumeManager();
fileOperationManager = new FileOperationManagerImpl(); fileOperationManager = new FileOperationManagerImpl();
// Observe the file operation manager's events.
var eventLogger = new EventLogger(fileOperationManager); var eventLogger = new EventLogger(fileOperationManager);
// Copy test.txt to /dest. This operation will be blocked. // Copy test.txt to /dest. This operation should be blocked.
fileOperationManager.paste([fileSystem.entries['/test.txt']], fileOperationManager.paste(
fileSystem.entries['/dest'], [fileSystem.entries['/test.txt']],
false); /** @type {!DirectoryEntry} */ (fileSystem.entries['/dest']), false);
var firstOperationTaskId; var firstOperationTaskId;
reportPromise(waitUntil(function() { reportPromise(waitUntil(function() {
...@@ -560,9 +571,9 @@ function testCopyInSequential(callback) { ...@@ -560,9 +571,9 @@ function testCopyInSequential(callback) {
firstOperationTaskId = eventLogger.events[0].taskId; firstOperationTaskId = eventLogger.events[0].taskId;
// Copy test.txt to /. This operation should be blocked. // Copy test.txt to /. This operation should be blocked.
fileOperationManager.paste([fileSystem.entries['/test.txt']], fileOperationManager.paste(
fileSystem.entries['/'], [fileSystem.entries['/test.txt']],
false); /** @type {!DirectoryEntry} */ (fileSystem.entries['/']), false);
return waitUntil(function() { return waitUntil(function() {
return fileOperationManager.getPendingCopyTasksForTesting().length === 1; return fileOperationManager.getPendingCopyTasksForTesting().length === 1;
...@@ -600,10 +611,11 @@ function testCopyInSequential(callback) { ...@@ -600,10 +611,11 @@ function testCopyInSequential(callback) {
} }
/** /**
* Tests the fileOperationUtil.paste for copying files in paralell. When * Tests copying files when the destination volumes are different: the copy
* destination volumes are different, copy operations can run in paralell. * operations should be run in parallel.
*/ */
function testCopyInParallel(callback) { function testCopyInParallel(callback) {
// Prepare entries and their resolver.
var fileSystemA = createTestFileSystem('volumeA', { var fileSystemA = createTestFileSystem('volumeA', {
'/': DIRECTORY_SIZE, '/': DIRECTORY_SIZE,
'/test.txt': 10 '/test.txt': 10
...@@ -614,26 +626,27 @@ function testCopyInParallel(callback) { ...@@ -614,26 +626,27 @@ function testCopyInParallel(callback) {
var fileSystems = [fileSystemA, fileSystemB]; var fileSystems = [fileSystemA, fileSystemB];
window.webkitResolveLocalFileSystemURL = function(url, success, failure) { window.webkitResolveLocalFileSystemURL = function(url, success, failure) {
return resolveTestFileSystemURL( var system = getFileSystemForURL(fileSystems, url);
getFileSystemForURL(fileSystems, url), url, success, failure); resolveTestFileSystemURL(system, url, success, failure);
}; };
var blockableFakeStartCopy = new BlockableFakeStartCopy( var blockableFakeStartCopy = new BlockableFakeStartCopy(
'filesystem:volumeB/', 'filesystem:volumeB/',
fileSystemA.entries['/test.txt'], fileSystemA.entries['/test.txt'],
fileSystems); fileSystems);
chrome.fileManagerPrivate.startCopy = mockChrome.fileManagerPrivate.startCopy =
blockableFakeStartCopy.startCopyFunc.bind(blockableFakeStartCopy); blockableFakeStartCopy.startCopyFunc.bind(blockableFakeStartCopy);
volumeManager = new FakeVolumeManager(); volumeManager = new FakeVolumeManager();
fileOperationManager = new FileOperationManagerImpl(); fileOperationManager = new FileOperationManagerImpl();
// Observe the file operation manager's events.
var eventLogger = new EventLogger(fileOperationManager); var eventLogger = new EventLogger(fileOperationManager);
// Copy test.txt from volume A to volume B. // Copy test.txt from volume A to volume B.
fileOperationManager.paste([fileSystemA.entries['/test.txt']], fileOperationManager.paste(
fileSystemB.entries['/'], [fileSystemA.entries['/test.txt']],
false); /** @type {!DirectoryEntry} */ (fileSystemB.entries['/']), false);
var firstOperationTaskId; var firstOperationTaskId;
reportPromise(waitUntil(function() { reportPromise(waitUntil(function() {
...@@ -645,9 +658,9 @@ function testCopyInParallel(callback) { ...@@ -645,9 +658,9 @@ function testCopyInParallel(callback) {
// Copy test.txt from volume A to volume A. This should not be blocked by // Copy test.txt from volume A to volume A. This should not be blocked by
// the previous operation. // the previous operation.
fileOperationManager.paste([fileSystemA.entries['/test.txt']], fileOperationManager.paste(
fileSystemA.entries['/'], [fileSystemA.entries['/test.txt']],
false); /** @type {!DirectoryEntry} */ (fileSystemA.entries['/']), false);
// Wait until the second operation is completed. // Wait until the second operation is completed.
return waitUntil(function() { return waitUntil(function() {
...@@ -681,9 +694,11 @@ function testCopyInParallel(callback) { ...@@ -681,9 +694,11 @@ function testCopyInParallel(callback) {
} }
/** /**
* Test case that a copy fails since destination volume is not available. * Tests that copy operations fail when the destination volume is not
* available.
*/ */
function testCopyFails(callback) { function testCopyFails(callback) {
// Prepare entries.
var fileSystem = createTestFileSystem('testVolume', { var fileSystem = createTestFileSystem('testVolume', {
'/': DIRECTORY_SIZE, '/': DIRECTORY_SIZE,
'/test.txt': 10 '/test.txt': 10
...@@ -692,18 +707,19 @@ function testCopyFails(callback) { ...@@ -692,18 +707,19 @@ function testCopyFails(callback) {
volumeManager = { volumeManager = {
/* Mocking volume manager. */ /* Mocking volume manager. */
getVolumeInfo: function() { getVolumeInfo: function() {
// Return null to simulate that the volume info is not available. // Returns null to indicate that the volume is not available.
return null; return null;
} }
}; };
fileOperationManager = new FileOperationManagerImpl(); fileOperationManager = new FileOperationManagerImpl();
// Observe the file operation manager's events.
var eventLogger = new EventLogger(fileOperationManager); var eventLogger = new EventLogger(fileOperationManager);
// Copy test.txt to /. // Copy test.txt to /, which should fail.
fileOperationManager.paste([fileSystem.entries['/test.txt']], fileOperationManager.paste(
fileSystem.entries['/'], [fileSystem.entries['/test.txt']],
false); /** @type {!DirectoryEntry} */ (fileSystem.entries['/']), false);
reportPromise(waitUntil(function() { reportPromise(waitUntil(function() {
return eventLogger.numberOfErrorEvents === 1; return eventLogger.numberOfErrorEvents === 1;
...@@ -723,8 +739,7 @@ function testCopyFails(callback) { ...@@ -723,8 +739,7 @@ function testCopyFails(callback) {
/** /**
* Tests the fileOperationUtil.paste for move. * Tests the fileOperationUtil.paste for move.
* @param {function(boolean:hasError)} callback Callback to be passed true on * @param {function(boolean)} callback Callback to be passed true on error.
* error.
*/ */
function testMove(callback) { function testMove(callback) {
// Prepare entries and their resolver. // Prepare entries and their resolver.
...@@ -733,13 +748,14 @@ function testMove(callback) { ...@@ -733,13 +748,14 @@ function testMove(callback) {
'/directory': DIRECTORY_SIZE, '/directory': DIRECTORY_SIZE,
'/test.txt': 10, '/test.txt': 10,
}); });
window.webkitResolveLocalFileSystemURL = window.webkitResolveLocalFileSystemURL = function(url, success, failure) {
resolveTestFileSystemURL.bind(null, fileSystem); resolveTestFileSystemURL(fileSystem, url, success, failure);
};
volumeManager = new FakeVolumeManager(); volumeManager = new FakeVolumeManager();
fileOperationManager = new FileOperationManagerImpl(); fileOperationManager = new FileOperationManagerImpl();
// Observing manager's events. // Observe the file operation manager's events.
var eventsPromise = waitForEvents(fileOperationManager); var eventsPromise = waitForEvents(fileOperationManager);
// Verify the events. // Verify the events.
...@@ -775,14 +791,12 @@ function testMove(callback) { ...@@ -775,14 +791,12 @@ function testMove(callback) {
fileOperationManager.paste( fileOperationManager.paste(
[fileSystem.entries['/test.txt']], [fileSystem.entries['/test.txt']],
fileSystem.entries['/directory'], /** @type {!DirectoryEntry} */ (fileSystem.entries['/directory']), true);
true);
} }
/** /**
* Tests the fileOperationUtil.deleteEntries. * Tests fileOperationManager.deleteEntries.
* @param {function(boolean:hasError)} callback Callback to be passed true on * @param {function(boolean)} callback Callback to be passed true on error.
* error.
*/ */
function testDelete(callback) { function testDelete(callback) {
// Prepare entries and their resolver. // Prepare entries and their resolver.
...@@ -790,34 +804,36 @@ function testDelete(callback) { ...@@ -790,34 +804,36 @@ function testDelete(callback) {
'/': DIRECTORY_SIZE, '/': DIRECTORY_SIZE,
'/test.txt': 10, '/test.txt': 10,
}); });
window.webkitResolveLocalFileSystemURL = window.webkitResolveLocalFileSystemURL = function(url, success, failure) {
resolveTestFileSystemURL.bind(null, fileSystem); resolveTestFileSystemURL(fileSystem, url, success, failure);
};
// Observing manager's events. // Observing manager's events.
reportPromise(waitForEvents(fileOperationManager).then(function(events) { reportPromise(
assertEquals('delete', events[0].type); waitForEvents(fileOperationManager).then(function(events) {
assertEquals('BEGIN', events[0].reason); assertEquals('delete', events[0].type);
assertEquals(10, events[0].totalBytes); assertEquals('BEGIN', events[0].reason);
assertEquals(0, events[0].processedBytes); assertEquals(10, events[0].totalBytes);
assertEquals(0, events[0].processedBytes);
var lastEvent = events[events.length - 1];
assertEquals('delete', lastEvent.type); var lastEvent = events[events.length - 1];
assertEquals('SUCCESS', lastEvent.reason); assertEquals('delete', lastEvent.type);
assertEquals(10, lastEvent.totalBytes); assertEquals('SUCCESS', lastEvent.reason);
assertEquals(10, lastEvent.processedBytes); assertEquals(10, lastEvent.totalBytes);
assertEquals(10, lastEvent.processedBytes);
assertFalse(events.some(function(event) {
return event.type === 'copy-progress'; assertFalse(events.some(function(event) {
})); return event.type === 'copy-progress';
}), callback); }));
}),
callback);
fileOperationManager.deleteEntries([fileSystem.entries['/test.txt']]); fileOperationManager.deleteEntries([fileSystem.entries['/test.txt']]);
} }
/** /**
* Tests the fileOperationUtil.zipSelection. * Tests fileOperationManager.zipSelection.
* @param {function(boolean:hasError)} callback Callback to be passed true on * @param {function(boolean)} callback Callback to be passed true on error.
* error.
*/ */
function testZip(callback) { function testZip(callback) {
// Prepare entries and their resolver. // Prepare entries and their resolver.
...@@ -825,12 +841,15 @@ function testZip(callback) { ...@@ -825,12 +841,15 @@ function testZip(callback) {
'/': DIRECTORY_SIZE, '/': DIRECTORY_SIZE,
'/test.txt': 10, '/test.txt': 10,
}); });
window.webkitResolveLocalFileSystemURL = window.webkitResolveLocalFileSystemURL = function(url, success, failure) {
resolveTestFileSystemURL.bind(null, fileSystem); resolveTestFileSystemURL(fileSystem, url, success, failure);
chrome.fileManagerPrivate.zipSelection = function( };
mockChrome.fileManagerPrivate.zipSelection = function(
sources, parent, newName, success, error) { sources, parent, newName, success, error) {
var newPath = joinPath('/', newName); var newPath = joinPath('/', newName);
var newEntry = new MockFileEntry(fileSystem, newPath, {size: 10}); var newEntry = new MockFileEntry(
fileSystem, newPath, /** @type {!Metadata} */ ({size: 10}));
fileSystem.entries[newPath] = newEntry; fileSystem.entries[newPath] = newEntry;
success(newEntry); success(newEntry);
}; };
...@@ -862,5 +881,6 @@ function testZip(callback) { ...@@ -862,5 +881,6 @@ function testZip(callback) {
}), callback); }), callback);
fileOperationManager.zipSelection( fileOperationManager.zipSelection(
[fileSystem.entries['/test.txt']], fileSystem.entries['/']); [fileSystem.entries['/test.txt']],
/** @type {!DirectoryEntry} */ (fileSystem.entries['/']));
} }
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