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
3 // found in the LICENSE file.
5 var kSecondaryDriveMountPointName = "drive-fileBrowserApiTestProfile2";
8 * Returns a callback that works as an error handler in file system API
9 * functions and invokes |callback| with specified message string.
11 * @param {function(string)} callback Wrapped callback function object.
12 * @param {string} message Error message.
13 * @return {function(DOMError)} Resulting callback function object.
15 function fileErrorCallback(callback, message) {
16 return function(error){
17 callback(message + ": " + error.name);
22 * Copies an entry using chrome.fileManagerPrivate.startCopy().
24 * @param {Entry} fromRoot Root entry of the copy source file system.
25 * @param {string} fromPath Relative path from fromRoot of the source entry.
26 * @param {Entry} toRoot Root entry of the copy destination file system.
27 * @param {string} toPath Relative path from toRoot of the target directory.
28 * @param {string} newName Name of the new copied entry.
29 * @param {function()} successCallback Callback invoked when copy succeed.
30 * @param {function(string)} errorCallback Callback invoked in error case.
32 function fileCopy(fromRoot, fromPath, toRoot, toPath, newName,
33 successCallback, errorCallback) {
34 fromRoot.getFile(fromPath, {create: false}, function(from) {
35 toRoot.getDirectory(toPath, {create: false}, function(to) {
37 var onProgress = function(id, status) {
39 errorCallback('Unknown copy id.');
42 if (status.type == 'error') {
43 chrome.fileManagerPrivate.onCopyProgress.removeListener(onProgress);
44 errorCallback('Copy failed.');
47 if (status.type == 'success') {
48 chrome.fileManagerPrivate.onCopyProgress.removeListener(onProgress);
52 chrome.fileManagerPrivate.onCopyProgress.addListener(onProgress);
53 chrome.fileManagerPrivate.startCopy(
54 from.toURL(), to.toURL(), newName, function(startCopyId) {
55 if (chrome.runtime.lastError) {
56 errorCallback('Error starting to copy.');
61 }, fileErrorCallback(errorCallback, 'Error getting destination entry'));
62 }, fileErrorCallback(errorCallback, 'Error getting source entry'));
66 * Verifies that a file exists on the specified location.
68 * @param {Entry} root Root entry of the file system.
69 * @param {string} path Relative path of the file from the root entry,
70 * @param {function()} successCallback Callback invoked when the file exists.
71 * @param {function(string)} errorCallback Callback invoked in error case.
73 function verifyFileExists(root, path, successCallback, errorCallback) {
74 root.getFile(path, {create: false},
76 fileErrorCallback(errorCallback, path + ' does not exist.'));
80 * Collects all tests that should be run for the test volume.
82 * @param {Entry} firstRoot Root entry of the first volume.
83 * @param {Entry} secondRoot Root entry of the second volume.
84 * @return {Array.<function()>} The list of tests that should be run.
86 function collectTests(firstRoot, secondRoot) {
89 testsToRun.push(function crossProfileNormalFileCopyTest() {
90 fileCopy(secondRoot, 'root/test_dir/test_file.tiff',
93 verifyFileExists.bind(null, firstRoot, 'root/newname.tiff',
94 chrome.test.succeed, chrome.test.fail),
98 testsToRun.push(function crossProfileHostedDocumentCopyTest() {
99 fileCopy(secondRoot, 'root/test_dir/hosted_doc.gdoc',
102 verifyFileExists.bind(null, firstRoot, 'root/newname.gdoc',
103 chrome.test.succeed, chrome.test.fail),
111 * Initializes testParams.
112 * Gets test volume and creates list of tests that should be run for it.
114 * @param {function(Array, string)} callback. Called with an array containing
115 * the list of the tests to run and an error message. On error list of tests
116 * to run will be null.
118 function initTests(callback) {
119 chrome.fileManagerPrivate.getVolumeMetadataList(function(volumeMetadataList) {
120 var driveVolumes = volumeMetadataList.filter(function(volume) {
121 return volume.volumeType == 'drive';
124 if (driveVolumes.length != 1) {
125 callback(null, 'Unexpected number of Drive volumes.');
129 chrome.fileManagerPrivate.requestFileSystem(
130 driveVolumes[0].volumeId,
131 function(primaryFileSystem) {
132 if (!primaryFileSystem) {
133 callback(null, 'Failed to acquire the testing volume.');
137 var url = primaryFileSystem.root.toURL().replace(
138 /[^\/]*\/?$/, kSecondaryDriveMountPointName);
140 webkitResolveLocalFileSystemURL(url, function(entry) {
142 callback(null, 'Failed to acquire secondary profile\'s volume.');
146 callback(collectTests(primaryFileSystem.root, entry), 'Success.');
152 // Trigger the tests.
153 initTests(function(testsToRun, errorMessage) {
155 chrome.test.notifyFail('Failed to initialize tests: ' + errorMessage);
158 chrome.test.runTests(testsToRun);