Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_manager_browsertest / keyboard_operations.js
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.
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 waitForElement(windowId, '.cr-dialog-ok').
16       then(callRemoteTestUtil.bind(null,
17                                    'fakeMouseClick',
18                                    windowId,
19                                    ['.cr-dialog-ok'],
20                                    null)).
21       then(function(result) {
22         chrome.test.assertTrue(result);
23         return 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       callRemoteTestUtil('copyFile', appId, [filename], this.next);
53     },
54     // Wait for a file list change.
55     function(result) {
56       chrome.test.assertTrue(result);
57       waitForFiles(appId, expectedFilesAfter, {ignoreLastModifiedTime: true}).
58           then(this.next);
59     },
60     // Verify the result.
61     function(fileList) {
62       checkIfNoErrorsOccured(this.next);
63     }
64   ]);
65 };
66
67 /**
68  * Tests deleting a file and and waits until the file lists changes.
69  * @param {string} path Directory path to be tested.
70  */
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)
76         return true;
77     }
78     return false;
79   };
80
81   var filename = 'world.ogv';
82   var directoryName = 'photos';
83   var appId, fileListBefore;
84   StepsRunner.run([
85     // Set up File Manager.
86     function() {
87       setupAndWaitUntilReady(null, path, this.next);
88     },
89     // Delete the file.
90     function(inAppId, inFileListBefore) {
91       appId = inAppId;
92       fileListBefore = inFileListBefore;
93       chrome.test.assertTrue(isFilePresent(filename, fileListBefore));
94       callRemoteTestUtil(
95           'deleteFile', appId, [filename], this.next);
96     },
97     // Reply to a dialog.
98     function(result) {
99       chrome.test.assertTrue(result);
100       waitAndAcceptDialog(appId).then(this.next);
101     },
102     // Wait for a file list change.
103     function() {
104       waitForFileListChange(appId, fileListBefore.length).then(this.next);
105     },
106     // Delete the directory.
107     function(fileList) {
108       fileListBefore = fileList;
109       chrome.test.assertFalse(isFilePresent(filename, fileList));
110       chrome.test.assertTrue(isFilePresent(directoryName, fileList));
111       callRemoteTestUtil('deleteFile', appId, [directoryName], this.next);
112     },
113     // Reply to a dialog.
114     function(result) {
115       chrome.test.assertTrue(result);
116       waitAndAcceptDialog(appId).then(this.next);
117     },
118     // Wait for a file list change.
119     function() {
120       waitForFileListChange(appId, fileListBefore.length).then(this.next);
121     },
122     // Verify the result.
123     function(fileList) {
124       chrome.test.assertFalse(isFilePresent(directoryName, fileList));
125       checkIfNoErrorsOccured(this.next);
126     }
127   ]);
128 }
129
130 /**
131  * Renames a file.
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.
136  */
137 function renameFile(windowId, oldName, newName) {
138   return callRemoteTestUtil('selectFile', windowId, [oldName]).then(function() {
139     // Push Ctrl+Enter.
140     return fakeKeyDown(windowId, '#detail-table', 'Enter', true);
141   }).then(function() {
142     // Wait for rename text field.
143     return waitForElement(windowId, 'input.rename');
144   }).then(function() {
145     // Type new file name.
146     return callRemoteTestUtil('inputText', windowId, ['input.rename', newName]);
147   }).then(function() {
148     // Push Enter.
149     return fakeKeyDown(windowId, 'input.rename', 'Enter', false);
150   });
151 }
152
153 /**
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.
158  */
159 function testRenameFile(path, initialEntrySet) {
160   var windowId;
161
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';
168       break;
169     }
170   }
171   chrome.test.assertTrue(
172       i != expectedEntryRows.length, 'hello.txt is not found.');
173
174   // Open a window.
175   return new Promise(function(callback) {
176     setupAndWaitUntilReady(null, path, callback);
177   }).then(function(inWindowId) {
178     windowId = inWindowId;
179     return waitForFiles(windowId, initialExpectedEntryRows);
180   }).then(function(){
181     return renameFile(windowId, 'hello.txt', 'New File Name.txt');
182   }).then(function() {
183     // Wait until rename completes.
184     return waitForElementLost(windowId, '#detail-table [renaming]');
185   }).then(function() {
186     // Wait for the new file name.
187     return waitForFiles(windowId,
188                         expectedEntryRows,
189                         {ignoreLastModifiedTime: true});
190   }).then(function() {
191     return renameFile(windowId, 'New File Name.txt', '.hidden file');
192   }).then(function() {
193     // The error dialog is shown.
194     return waitAndAcceptDialog(windowId);
195   }).then(function() {
196     // The name did not change.
197     return waitForFiles(windowId,
198                         expectedEntryRows,
199                         {ignoreLastModifiedTime: true});
200   });
201 };
202
203 testcase.keyboardCopyDownloads = function() {
204   keyboardCopy(RootPath.DOWNLOADS);
205 };
206
207 testcase.keyboardDeleteDownloads = function() {
208   keyboardDelete(RootPath.DOWNLOADS);
209 };
210
211 testcase.keyboardCopyDrive = function() {
212   keyboardCopy(RootPath.DRIVE);
213 };
214
215 testcase.keyboardDeleteDrive = function() {
216   keyboardDelete(RootPath.DRIVE);
217 };
218
219 testcase.createNewFolderAndCheckFocus = function() {
220 };
221
222 testcase.renameFileDownloads = function() {
223   testPromise(testRenameFile(RootPath.DOWNLOADS, BASIC_LOCAL_ENTRY_SET));
224 };
225
226 testcase.renameFileDrive = function() {
227   testPromise(testRenameFile(RootPath.DRIVE, BASIC_DRIVE_ENTRY_SET));
228 };