Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_manager_browsertest / file_manager / keyboard_operations.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 'use strict';
6
7 /**
8  * Waits until a dialog with an OK button is shown and accepts it.
9  *
10  * @param {string} windowId Target window ID.
11  * @return {Promise} Promise to be fulfilled after clicking the OK button in the
12  *     dialog.
13  */
14 function waitAndAcceptDialog(windowId) {
15   return remoteCall.waitForElement(windowId, '.cr-dialog-ok').
16       then(remoteCall.callRemoteTestUtil.bind(remoteCall,
17                                               'fakeMouseClick',
18                                               windowId,
19                                               ['.cr-dialog-ok'],
20                                               null)).
21       then(function(result) {
22         chrome.test.assertTrue(result);
23         return remoteCall.waitForElementLost(windowId, '.cr-dialog-container');
24       });
25 }
26
27 /**
28  * Tests copying a file to the same directory and waits until the file lists
29  * changes.
30  *
31  * @param {string} path Directory path to be tested.
32  */
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']]);
40
41   var appId, fileListBefore;
42   StepsRunner.run([
43     // Set up File Manager.
44     function() {
45       setupAndWaitUntilReady(null, path, this.next);
46     },
47     // Copy the file.
48     function(inAppId, inFileListBefore) {
49       appId = inAppId;
50       fileListBefore = inFileListBefore;
51       chrome.test.assertEq(expectedFilesBefore, inFileListBefore);
52       remoteCall.callRemoteTestUtil('copyFile', appId, [filename], this.next);
53     },
54     // Wait for a file list change.
55     function(result) {
56       chrome.test.assertTrue(result);
57       remoteCall.waitForFiles(
58           appId, expectedFilesAfter, {ignoreLastModifiedTime: true}).
59           then(this.next);
60     },
61     // Verify the result.
62     function(fileList) {
63       checkIfNoErrorsOccured(this.next);
64     }
65   ]);
66 };
67
68 /**
69  * Tests deleting a file and and waits until the file lists changes.
70  * @param {string} path Directory path to be tested.
71  */
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)
77         return true;
78     }
79     return false;
80   };
81
82   var filename = 'world.ogv';
83   var directoryName = 'photos';
84   var appId, fileListBefore;
85   StepsRunner.run([
86     // Set up File Manager.
87     function() {
88       setupAndWaitUntilReady(null, path, this.next);
89     },
90     // Delete the file.
91     function(inAppId, inFileListBefore) {
92       appId = inAppId;
93       fileListBefore = inFileListBefore;
94       chrome.test.assertTrue(isFilePresent(filename, fileListBefore));
95       remoteCall.callRemoteTestUtil(
96           'deleteFile', appId, [filename], this.next);
97     },
98     // Reply to a dialog.
99     function(result) {
100       chrome.test.assertTrue(result);
101       waitAndAcceptDialog(appId).then(this.next);
102     },
103     // Wait for a file list change.
104     function() {
105       remoteCall.waitForFileListChange(appId, fileListBefore.length).
106         then(this.next);
107     },
108     // Delete the directory.
109     function(fileList) {
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);
115     },
116     // Reply to a dialog.
117     function(result) {
118       chrome.test.assertTrue(result);
119       waitAndAcceptDialog(appId).then(this.next);
120     },
121     // Wait for a file list change.
122     function() {
123       remoteCall.waitForFileListChange(
124           appId, fileListBefore.length).then(this.next);
125     },
126     // Verify the result.
127     function(fileList) {
128       chrome.test.assertFalse(isFilePresent(directoryName, fileList));
129       checkIfNoErrorsOccured(this.next);
130     }
131   ]);
132 }
133
134 /**
135  * Renames a file.
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.
140  */
141 function renameFile(windowId, oldName, newName) {
142   return remoteCall.callRemoteTestUtil('selectFile', windowId, [oldName]).
143     then(function() {
144       // Push Ctrl+Enter.
145       return remoteCall.fakeKeyDown(windowId, '#detail-table', 'Enter', true);
146     }).then(function() {
147       // Wait for rename text field.
148       return remoteCall.waitForElement(windowId, 'input.rename');
149     }).then(function() {
150       // Type new file name.
151       return remoteCall.callRemoteTestUtil(
152           'inputText', windowId, ['input.rename', newName]);
153     }).then(function() {
154       // Push Enter.
155       return remoteCall.fakeKeyDown(windowId, 'input.rename', 'Enter', false);
156     });
157 }
158
159 /**
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.
165  */
166 function testRenameNewDirectory(path, initialEntrySet, pathInBreadcrumb) {
167   var expectedRows = TestEntryInfo.getExpectedRows(initialEntrySet);
168
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',
174           true);
175     }).then(function() {
176       // Wait for rename text field.
177       return remoteCall.waitForElement(windowId, 'input.rename');
178     }).then(function() {
179       // Type new file name.
180       return remoteCall.callRemoteTestUtil(
181           'inputText', windowId, ['input.rename', 'foo']);
182     }).then(function() {
183       // Press Enter.
184       return remoteCall.fakeKeyDown(windowId, 'input.rename', 'Enter', false);
185     }).then(function() {
186       // Press Enter again to try to get into the new directory.
187       return remoteCall.fakeKeyDown(windowId, '#list-container', 'Enter',
188           false);
189     }).then(function() {
190       // Confirm that it doesn't move the directory since it's in renaming
191       // process.
192       return remoteCall.waitUntilCurrentDirectoryIsChanged(windowId,
193           pathInBreadcrumb);
194     }).then(function() {
195       // Wait until rename is completed.
196       return remoteCall.waitForElementLost(windowId, 'li[renaming]');
197     }).then(function() {
198       // Press Enter again.
199       return remoteCall.fakeKeyDown(windowId, '#list-container', 'Enter',
200           false);
201     }).then(function() {
202       // Confirm that it moves to renamed directory.
203       return remoteCall.waitUntilCurrentDirectoryIsChanged(windowId,
204           pathInBreadcrumb + '/foo');
205     });
206   });
207 }
208
209 /**
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.
214  */
215 function testRenameFile(path, initialEntrySet) {
216   var windowId;
217
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';
224       break;
225     }
226   }
227   chrome.test.assertTrue(
228       i != expectedEntryRows.length, 'hello.txt is not found.');
229
230   // Open a window.
231   return new Promise(function(callback) {
232     setupAndWaitUntilReady(null, path, callback);
233   }).then(function(inWindowId) {
234     windowId = inWindowId;
235     return remoteCall.waitForFiles(windowId, initialExpectedEntryRows);
236   }).then(function(){
237     return renameFile(windowId, 'hello.txt', 'New File Name.txt');
238   }).then(function() {
239     // Wait until rename completes.
240     return remoteCall.waitForElementLost(windowId, '#detail-table [renaming]');
241   }).then(function() {
242     // Wait for the new file name.
243     return remoteCall.waitForFiles(windowId,
244                         expectedEntryRows,
245                         {ignoreLastModifiedTime: true});
246   }).then(function() {
247     return renameFile(windowId, 'New File Name.txt', '.hidden file');
248   }).then(function() {
249     // The error dialog is shown.
250     return waitAndAcceptDialog(windowId);
251   }).then(function() {
252     // The name did not change.
253     return remoteCall.waitForFiles(windowId,
254                         expectedEntryRows,
255                         {ignoreLastModifiedTime: true});
256   });
257 };
258
259 testcase.keyboardCopyDownloads = function() {
260   keyboardCopy(RootPath.DOWNLOADS);
261 };
262
263 testcase.keyboardDeleteDownloads = function() {
264   keyboardDelete(RootPath.DOWNLOADS);
265 };
266
267 testcase.keyboardCopyDrive = function() {
268   keyboardCopy(RootPath.DRIVE);
269 };
270
271 testcase.keyboardDeleteDrive = function() {
272   keyboardDelete(RootPath.DRIVE);
273 };
274
275 testcase.createNewFolderAndCheckFocus = function() {
276 };
277
278 testcase.renameFileDownloads = function() {
279   testPromise(testRenameFile(RootPath.DOWNLOADS, BASIC_LOCAL_ENTRY_SET));
280 };
281
282 testcase.renameFileDrive = function() {
283   testPromise(testRenameFile(RootPath.DRIVE, BASIC_DRIVE_ENTRY_SET));
284 };
285
286 testcase.renameNewDirectoryDownloads = function() {
287   testPromise(testRenameNewDirectory(RootPath.DOWNLOADS,
288       BASIC_LOCAL_ENTRY_SET, '/Downloads'));
289 };
290
291 testcase.renameNewDirectoryDrive = function() {
292   testPromise(testRenameNewDirectory(RootPath.DRIVE, BASIC_DRIVE_ENTRY_SET,
293       '/My Drive'));
294 };