1 // Copyright (c) 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 waitForElement(windowId, '.cr-dialog-ok').
16 then(callRemoteTestUtil.bind(null,
21 then(function(result) {
22 chrome.test.assertTrue(result);
23 return 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 callRemoteTestUtil('copyFile', appId, [filename], this.next);
54 // Wait for a file list change.
56 chrome.test.assertTrue(result);
57 waitForFiles(appId, expectedFilesAfter, {ignoreLastModifiedTime: true}).
62 checkIfNoErrorsOccured(this.next);
68 * Tests deleting a file and and waits until the file lists changes.
69 * @param {string} path Directory path to be tested.
71 function keyboardDelete(path) {
72 // Returns true if |fileList| contains |filename|.
73 var isFilePresent = function(filename, fileList) {
74 for (var i = 0; i < fileList.length; i++) {
75 if (getFileName(fileList[i]) == filename)
81 var filename = 'world.ogv';
82 var directoryName = 'photos';
83 var appId, fileListBefore;
85 // Set up File Manager.
87 setupAndWaitUntilReady(null, path, this.next);
90 function(inAppId, inFileListBefore) {
92 fileListBefore = inFileListBefore;
93 chrome.test.assertTrue(isFilePresent(filename, fileListBefore));
95 'deleteFile', appId, [filename], this.next);
99 chrome.test.assertTrue(result);
100 waitAndAcceptDialog(appId).then(this.next);
102 // Wait for a file list change.
104 waitForFileListChange(appId, fileListBefore.length).then(this.next);
106 // Delete the directory.
108 fileListBefore = fileList;
109 chrome.test.assertFalse(isFilePresent(filename, fileList));
110 chrome.test.assertTrue(isFilePresent(directoryName, fileList));
111 callRemoteTestUtil('deleteFile', appId, [directoryName], this.next);
113 // Reply to a dialog.
115 chrome.test.assertTrue(result);
116 waitAndAcceptDialog(appId).then(this.next);
118 // Wait for a file list change.
120 waitForFileListChange(appId, fileListBefore.length).then(this.next);
122 // Verify the result.
124 chrome.test.assertFalse(isFilePresent(directoryName, fileList));
125 checkIfNoErrorsOccured(this.next);
132 * @param {string} windowId ID of the window.
133 * @param {string} oldName Old name of a file.
134 * @param {string} newName New name of a file.
135 * @return {Promise} Promise to be fulfilled on success.
137 function renameFile(windowId, oldName, newName) {
138 return callRemoteTestUtil('selectFile', windowId, [oldName]).then(function() {
140 return fakeKeyDown(windowId, '#detail-table', 'Enter', true);
142 // Wait for rename text field.
143 return waitForElement(windowId, 'input.rename');
145 // Type new file name.
146 return callRemoteTestUtil('inputText', windowId, ['input.rename', newName]);
149 return fakeKeyDown(windowId, 'input.rename', 'Enter', false);
154 * Test for renaming a file.
155 * @param {string} path Initial path.
156 * @param {Array.<TestEntryInfo>} initialEntrySet Initial set of entries.
157 * @return {Promise} Promise to be fulfilled on success.
159 function testRenameFile(path, initialEntrySet) {
162 // Make expected rows.
163 var initialExpectedEntryRows = TestEntryInfo.getExpectedRows(initialEntrySet);
164 var expectedEntryRows = TestEntryInfo.getExpectedRows(initialEntrySet);
165 for (var i = 0; i < expectedEntryRows.length; i++) {
166 if (expectedEntryRows[i][0] === 'hello.txt') {
167 expectedEntryRows[i][0] = 'New File Name.txt';
171 chrome.test.assertTrue(
172 i != expectedEntryRows.length, 'hello.txt is not found.');
175 return new Promise(function(callback) {
176 setupAndWaitUntilReady(null, path, callback);
177 }).then(function(inWindowId) {
178 windowId = inWindowId;
179 return waitForFiles(windowId, initialExpectedEntryRows);
181 return renameFile(windowId, 'hello.txt', 'New File Name.txt');
183 // Wait until rename completes.
184 return waitForElementLost(windowId, '#detail-table [renaming]');
186 // Wait for the new file name.
187 return waitForFiles(windowId,
189 {ignoreLastModifiedTime: true});
191 return renameFile(windowId, 'New File Name.txt', '.hidden file');
193 // The error dialog is shown.
194 return waitAndAcceptDialog(windowId);
196 // The name did not change.
197 return waitForFiles(windowId,
199 {ignoreLastModifiedTime: true});
203 testcase.keyboardCopyDownloads = function() {
204 keyboardCopy(RootPath.DOWNLOADS);
207 testcase.keyboardDeleteDownloads = function() {
208 keyboardDelete(RootPath.DOWNLOADS);
211 testcase.keyboardCopyDrive = function() {
212 keyboardCopy(RootPath.DRIVE);
215 testcase.keyboardDeleteDrive = function() {
216 keyboardDelete(RootPath.DRIVE);
219 testcase.createNewFolderAndCheckFocus = function() {
222 testcase.renameFileDownloads = function() {
223 testPromise(testRenameFile(RootPath.DOWNLOADS, BASIC_LOCAL_ENTRY_SET));
226 testcase.renameFileDrive = function() {
227 testPromise(testRenameFile(RootPath.DRIVE, BASIC_DRIVE_ENTRY_SET));