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.
8 * Waits until a dialog with an OK button is shown and accepts it.
10 * @param {string} windowId Target window ID.
11 * @return {Promise} Promise to be fulfilled after clicking the OK button in the
14 function waitAndAcceptDialog(windowId) {
15 return remoteCall.waitForElement(windowId, '.cr-dialog-ok').
16 then(remoteCall.callRemoteTestUtil.bind(remoteCall,
21 then(function(result) {
22 chrome.test.assertTrue(result);
23 return remoteCall.waitForElementLost(windowId, '.cr-dialog-container');
28 * Tests copying a file to the same directory and waits until the file lists
31 * @param {string} path Directory path to be tested.
33 function keyboardCopy(path, callback) {
34 var filename = 'world.ogv';
35 var expectedFilesBefore =
36 TestEntryInfo.getExpectedRows(path == RootPath.DRIVE ?
37 BASIC_DRIVE_ENTRY_SET : BASIC_LOCAL_ENTRY_SET).sort();
38 var expectedFilesAfter =
39 expectedFilesBefore.concat([['world (1).ogv', '59 KB', 'OGG video']]);
41 var appId, fileListBefore;
43 // Set up File Manager.
45 setupAndWaitUntilReady(null, path, this.next);
48 function(inAppId, inFileListBefore) {
50 fileListBefore = inFileListBefore;
51 chrome.test.assertEq(expectedFilesBefore, inFileListBefore);
52 remoteCall.callRemoteTestUtil('copyFile', appId, [filename], this.next);
54 // Wait for a file list change.
56 chrome.test.assertTrue(result);
57 remoteCall.waitForFiles(
58 appId, expectedFilesAfter, {ignoreLastModifiedTime: true}).
63 checkIfNoErrorsOccured(this.next);
69 * Tests deleting a file and and waits until the file lists changes.
70 * @param {string} path Directory path to be tested.
72 function keyboardDelete(path) {
73 // Returns true if |fileList| contains |filename|.
74 var isFilePresent = function(filename, fileList) {
75 for (var i = 0; i < fileList.length; i++) {
76 if (getFileName(fileList[i]) == filename)
82 var filename = 'world.ogv';
83 var directoryName = 'photos';
84 var appId, fileListBefore;
86 // Set up File Manager.
88 setupAndWaitUntilReady(null, path, this.next);
91 function(inAppId, inFileListBefore) {
93 fileListBefore = inFileListBefore;
94 chrome.test.assertTrue(isFilePresent(filename, fileListBefore));
95 remoteCall.callRemoteTestUtil(
96 'deleteFile', appId, [filename], this.next);
100 chrome.test.assertTrue(result);
101 waitAndAcceptDialog(appId).then(this.next);
103 // Wait for a file list change.
105 remoteCall.waitForFileListChange(appId, fileListBefore.length).
108 // Delete the directory.
110 fileListBefore = fileList;
111 chrome.test.assertFalse(isFilePresent(filename, fileList));
112 chrome.test.assertTrue(isFilePresent(directoryName, fileList));
113 remoteCall.callRemoteTestUtil(
114 'deleteFile', appId, [directoryName], this.next);
116 // Reply to a dialog.
118 chrome.test.assertTrue(result);
119 waitAndAcceptDialog(appId).then(this.next);
121 // Wait for a file list change.
123 remoteCall.waitForFileListChange(
124 appId, fileListBefore.length).then(this.next);
126 // Verify the result.
128 chrome.test.assertFalse(isFilePresent(directoryName, fileList));
129 checkIfNoErrorsOccured(this.next);
136 * @param {string} windowId ID of the window.
137 * @param {string} oldName Old name of a file.
138 * @param {string} newName New name of a file.
139 * @return {Promise} Promise to be fulfilled on success.
141 function renameFile(windowId, oldName, newName) {
142 return remoteCall.callRemoteTestUtil('selectFile', windowId, [oldName]).
145 return remoteCall.fakeKeyDown(windowId, '#detail-table', 'Enter', true);
147 // Wait for rename text field.
148 return remoteCall.waitForElement(windowId, 'input.rename');
150 // Type new file name.
151 return remoteCall.callRemoteTestUtil(
152 'inputText', windowId, ['input.rename', newName]);
155 return remoteCall.fakeKeyDown(windowId, 'input.rename', 'Enter', false);
160 * Test for renaming a new directory.
161 * @param {string} path Initial path.
162 * @param {Array.<TestEntryInfo>} initialEntrySet Initial set of entries.
163 * @param {string} pathInBreadcrumb Initial path which is shown in breadcrumb.
164 * @return {Promise} Promise to be fulfilled on success.
166 function testRenameNewDirectory(path, initialEntrySet, pathInBreadcrumb) {
167 var expectedRows = TestEntryInfo.getExpectedRows(initialEntrySet);
169 return new Promise(function(resolve) {
170 setupAndWaitUntilReady(null, path, resolve);
171 }).then(function(windowId) {
172 return remoteCall.waitForFiles(windowId, expectedRows).then(function() {
173 return remoteCall.fakeKeyDown(windowId, '#list-container', 'U+0045',
176 // Wait for rename text field.
177 return remoteCall.waitForElement(windowId, 'input.rename');
179 // Type new file name.
180 return remoteCall.callRemoteTestUtil(
181 'inputText', windowId, ['input.rename', 'foo']);
184 return remoteCall.fakeKeyDown(windowId, 'input.rename', 'Enter', false);
186 // Press Enter again to try to get into the new directory.
187 return remoteCall.fakeKeyDown(windowId, '#list-container', 'Enter',
190 // Confirm that it doesn't move the directory since it's in renaming
192 return remoteCall.waitUntilCurrentDirectoryIsChanged(windowId,
195 // Wait until rename is completed.
196 return remoteCall.waitForElementLost(windowId, 'li[renaming]');
198 // Press Enter again.
199 return remoteCall.fakeKeyDown(windowId, '#list-container', 'Enter',
202 // Confirm that it moves to renamed directory.
203 return remoteCall.waitUntilCurrentDirectoryIsChanged(windowId,
204 pathInBreadcrumb + '/foo');
210 * Test for renaming a file.
211 * @param {string} path Initial path.
212 * @param {Array.<TestEntryInfo>} initialEntrySet Initial set of entries.
213 * @return {Promise} Promise to be fulfilled on success.
215 function testRenameFile(path, initialEntrySet) {
218 // Make expected rows.
219 var initialExpectedEntryRows = TestEntryInfo.getExpectedRows(initialEntrySet);
220 var expectedEntryRows = TestEntryInfo.getExpectedRows(initialEntrySet);
221 for (var i = 0; i < expectedEntryRows.length; i++) {
222 if (expectedEntryRows[i][0] === 'hello.txt') {
223 expectedEntryRows[i][0] = 'New File Name.txt';
227 chrome.test.assertTrue(
228 i != expectedEntryRows.length, 'hello.txt is not found.');
231 return new Promise(function(callback) {
232 setupAndWaitUntilReady(null, path, callback);
233 }).then(function(inWindowId) {
234 windowId = inWindowId;
235 return remoteCall.waitForFiles(windowId, initialExpectedEntryRows);
237 return renameFile(windowId, 'hello.txt', 'New File Name.txt');
239 // Wait until rename completes.
240 return remoteCall.waitForElementLost(windowId, '#detail-table [renaming]');
242 // Wait for the new file name.
243 return remoteCall.waitForFiles(windowId,
245 {ignoreLastModifiedTime: true});
247 return renameFile(windowId, 'New File Name.txt', '.hidden file');
249 // The error dialog is shown.
250 return waitAndAcceptDialog(windowId);
252 // The name did not change.
253 return remoteCall.waitForFiles(windowId,
255 {ignoreLastModifiedTime: true});
259 testcase.keyboardCopyDownloads = function() {
260 keyboardCopy(RootPath.DOWNLOADS);
263 testcase.keyboardDeleteDownloads = function() {
264 keyboardDelete(RootPath.DOWNLOADS);
267 testcase.keyboardCopyDrive = function() {
268 keyboardCopy(RootPath.DRIVE);
271 testcase.keyboardDeleteDrive = function() {
272 keyboardDelete(RootPath.DRIVE);
275 testcase.createNewFolderAndCheckFocus = function() {
278 testcase.renameFileDownloads = function() {
279 testPromise(testRenameFile(RootPath.DOWNLOADS, BASIC_LOCAL_ENTRY_SET));
282 testcase.renameFileDrive = function() {
283 testPromise(testRenameFile(RootPath.DRIVE, BASIC_DRIVE_ENTRY_SET));
286 testcase.renameNewDirectoryDownloads = function() {
287 testPromise(testRenameNewDirectory(RootPath.DOWNLOADS,
288 BASIC_LOCAL_ENTRY_SET, '/Downloads'));
291 testcase.renameNewDirectoryDrive = function() {
292 testPromise(testRenameNewDirectory(RootPath.DRIVE, BASIC_DRIVE_ENTRY_SET,