Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_browser / multi_profile_copy / test.js
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.
4
5 var kSecondaryDriveMountPointName = "drive-fileBrowserApiTestProfile2";
6
7 /**
8  * Returns a callback that works as an error handler in file system API
9  * functions and invokes |callback| with specified message string.
10  *
11  * @param {function(string)} callback Wrapped callback function object.
12  * @param {string} message Error message.
13  * @return {function(DOMError)} Resulting callback function object.
14  */
15 function fileErrorCallback(callback, message) {
16   return function(error){
17     callback(message + ": " + error.name);
18   };
19 }
20
21 /**
22  * Copies an entry using chrome.fileManagerPrivate.startCopy().
23  *
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.
31  */
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) {
36       var copyId = null;
37       var onProgress = function(id, status) {
38         if (id != copyId) {
39           errorCallback('Unknown copy id.');
40           return;
41         }
42         if (status.type == 'error') {
43           chrome.fileManagerPrivate.onCopyProgress.removeListener(onProgress);
44           errorCallback('Copy failed.');
45           return;
46         }
47         if (status.type == 'success') {
48           chrome.fileManagerPrivate.onCopyProgress.removeListener(onProgress);
49           successCallback();
50         }
51       };
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.');
57             return;
58           }
59           copyId = startCopyId;
60         });
61     }, fileErrorCallback(errorCallback, 'Error getting destination entry'));
62   }, fileErrorCallback(errorCallback, 'Error getting source entry'));
63 }
64
65 /**
66  * Verifies that a file exists on the specified location.
67  *
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.
72  */
73 function verifyFileExists(root, path, successCallback, errorCallback) {
74   root.getFile(path, {create: false},
75                successCallback,
76                fileErrorCallback(errorCallback, path + ' does not exist.'));
77 }
78
79 /**
80  * Collects all tests that should be run for the test volume.
81  *
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.
85  */
86 function collectTests(firstRoot, secondRoot) {
87   var testsToRun = [];
88
89   testsToRun.push(function crossProfileNormalFileCopyTest() {
90     fileCopy(secondRoot, 'root/test_dir/test_file.tiff',
91              firstRoot, 'root/',
92              'newname.tiff',
93              verifyFileExists.bind(null, firstRoot, 'root/newname.tiff',
94                                    chrome.test.succeed, chrome.test.fail),
95              chrome.test.fail);
96   });
97
98   testsToRun.push(function crossProfileHostedDocumentCopyTest() {
99     fileCopy(secondRoot, 'root/test_dir/hosted_doc.gdoc',
100              firstRoot, 'root/',
101              'newname.gdoc',
102              verifyFileExists.bind(null, firstRoot, 'root/newname.gdoc',
103                                    chrome.test.succeed, chrome.test.fail),
104              chrome.test.fail);
105   });
106
107   return testsToRun;
108 }
109
110 /**
111  * Initializes testParams.
112  * Gets test volume and creates list of tests that should be run for it.
113  *
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.
117  */
118 function initTests(callback) {
119   chrome.fileManagerPrivate.getVolumeMetadataList(function(volumeMetadataList) {
120     var driveVolumes = volumeMetadataList.filter(function(volume) {
121       return volume.volumeType == 'drive';
122     });
123
124     if (driveVolumes.length != 1) {
125       callback(null, 'Unexpected number of Drive volumes.');
126       return;
127     }
128
129     chrome.fileManagerPrivate.requestFileSystem(
130         driveVolumes[0].volumeId,
131         function(primaryFileSystem) {
132           if (!primaryFileSystem) {
133             callback(null, 'Failed to acquire the testing volume.');
134             return;
135           }
136
137           var url = primaryFileSystem.root.toURL().replace(
138               /[^\/]*\/?$/, kSecondaryDriveMountPointName);
139
140           webkitResolveLocalFileSystemURL(url, function(entry) {
141             if (!entry) {
142               callback(null, 'Failed to acquire secondary profile\'s volume.');
143               return;
144             }
145
146             callback(collectTests(primaryFileSystem.root, entry), 'Success.');
147           });
148         });
149   });
150 }
151
152 // Trigger the tests.
153 initTests(function(testsToRun, errorMessage) {
154   if (!testsToRun) {
155     chrome.test.notifyFail('Failed to initialize tests: ' + errorMessage);
156     return;
157   }
158   chrome.test.runTests(testsToRun);
159 });