Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_manager_browsertest / file_manager / folder_shortcuts.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  * Constants for selectors.
9  */
10 var TREEITEM_DRIVE = '#directory-tree > div:nth-child(1) > .tree-children ' +
11     '> div:nth-child(1) ';
12 var TREEITEM_A = TREEITEM_DRIVE + '> .tree-children > div:nth-child(1) ';
13 var TREEITEM_B = TREEITEM_A + '> .tree-children > div:nth-child(1) ';
14 var TREEITEM_C = TREEITEM_B + '> .tree-children > div:nth-child(1) ';
15 var TREEITEM_D = TREEITEM_DRIVE + '> .tree-children > div:nth-child(2) ';
16 var TREEITEM_E = TREEITEM_D + '> .tree-children > div:nth-child(1) ';
17 var EXPAND_ICON = '> .tree-row > .expand-icon';
18 var VOLUME_ICON = '> .tree-row > .volume-icon';
19 var EXPANDED_SUBTREE = '> .tree-children[expanded]';
20
21 /**
22  * Entry set which is used for this test.
23  * @type {Array.<TestEntryInfo>}
24  * @const
25  */
26 var ENTRY_SET = [
27   ENTRIES.directoryA,
28   ENTRIES.directoryB,
29   ENTRIES.directoryC,
30   ENTRIES.directoryD,
31   ENTRIES.directoryE,
32   ENTRIES.directoryF
33 ];
34
35 /**
36  * Constants for each folders.
37  * @type {Array.<Object>}
38  * @const
39  */
40 var DIRECTORY = {
41   Drive: {
42     contents: [ENTRIES.directoryA.getExpectedRow(),
43                ENTRIES.directoryD.getExpectedRow()],
44     name: 'Drive',
45     navItem: '#tree-item-autogen-id-2',
46     treeItem: TREEITEM_DRIVE
47   },
48   A: {
49     contents: [ENTRIES.directoryB.getExpectedRow()],
50     name: 'A',
51     navItem: '#tree-item-autogen-id-13',
52     treeItem: TREEITEM_A
53   },
54   B: {
55     contents: [ENTRIES.directoryC.getExpectedRow()],
56     name: 'B',
57     treeItem: TREEITEM_B
58   },
59   C: {
60     contents: [],
61     name: 'C',
62     navItem: '#tree-item-autogen-id-13',
63     treeItem: TREEITEM_C
64   },
65   D: {
66     contents: [ENTRIES.directoryE.getExpectedRow()],
67     name: 'D',
68     navItem: '#tree-item-autogen-id-12',
69     treeItem: TREEITEM_D
70   },
71   E: {
72     contents: [ENTRIES.directoryF.getExpectedRow()],
73     name: 'E',
74     treeItem: TREEITEM_E
75   }
76 };
77
78 /**
79  * Opens two file manager windows.
80  * @return {Promise} Promise fulfilled with an array containing two window IDs.
81  */
82 function openWindows() {
83   return Promise.all([
84     openNewWindow(null, RootPath.DRIVE),
85     openNewWindow(null, RootPath.DRIVE)
86   ]).then(function(windowIds) {
87     return Promise.all([
88       remoteCall.waitForElement(windowIds[0], '#detail-table'),
89       remoteCall.waitForElement(windowIds[1], '#detail-table')
90     ]).then(function() {
91       return windowIds;
92     });
93   });
94 }
95
96 /**
97  * Expands tree item on the directory tree by clicking expand icon.
98  * @param {string} windowId ID of target window.
99  * @param {Object} directory Directory whose tree item should be expanded.
100  * @return {Promise} Promise fulfilled on success.
101  */
102 function expandTreeItem(windowId, directory) {
103   return remoteCall.waitForElement(
104       windowId, directory.treeItem + EXPAND_ICON).then(function() {
105     return remoteCall.callRemoteTestUtil(
106         'fakeMouseClick', windowId, [directory.treeItem + EXPAND_ICON]);
107   }).then(function(result) {
108     chrome.test.assertTrue(result);
109     return remoteCall.waitForElement(windowId,
110                                      directory.treeItem + EXPANDED_SUBTREE);
111   });
112 }
113
114 /**
115  * Expands whole directory tree.
116  * @param {string} windowId ID of target window.
117  * @return {Promise} Promise fulfilled on success.
118  */
119 function expandDirectoryTree(windowId) {
120   return expandTreeItem(windowId, DIRECTORY.Drive).then(function() {
121     return expandTreeItem(windowId, DIRECTORY.A);
122   }).then(function() {
123     return expandTreeItem(windowId, DIRECTORY.B);
124   }).then(function() {
125     return expandTreeItem(windowId, DIRECTORY.D);
126   });
127 }
128
129 /**
130  * Makes |directory| the current directory.
131  * @param {string} windowId ID of target window.
132  * @param {Object} directory Directory which should be a current directory.
133  * @return {Promise} Promise fulfilled on success.
134  */
135 function navigateToDirectory(windowId, directory) {
136   return remoteCall.waitForElement(
137       windowId, directory.treeItem + VOLUME_ICON).then(function() {
138     return remoteCall.callRemoteTestUtil(
139         'fakeMouseClick', windowId, [directory.treeItem + VOLUME_ICON]);
140   }).then(function(result) {
141     chrome.test.assertTrue(result);
142     return remoteCall.waitForFiles(windowId, directory.contents);
143   });
144 }
145
146 /**
147  * Creates folder shortcut to |directory|.
148  * The current directory must be a parent of the |directory|.
149  * @param {string} windowId ID of target window.
150  * @param {Object} directory Directory of shortcut to be created.
151  * @return {Promise} Promise fulfilled on success.
152  */
153 function createShortcut(windowId, directory) {
154   return remoteCall.callRemoteTestUtil(
155       'selectFile', windowId, [directory.name]).then(function(result) {
156     chrome.test.assertTrue(result);
157     return remoteCall.waitForElement(windowId, ['.table-row[selected]']);
158   }).then(function() {
159     return remoteCall.callRemoteTestUtil(
160         'fakeMouseRightClick', windowId, ['.table-row[selected]']);
161   }).then(function(result) {
162     chrome.test.assertTrue(result);
163     return remoteCall.waitForElement(
164         windowId, '#file-context-menu:not([hidden])');
165   }).then(function() {
166     return remoteCall.waitForElement(
167         windowId, '[command="#create-folder-shortcut"]');
168   }).then(function() {
169     return remoteCall.callRemoteTestUtil(
170         'fakeMouseClick', windowId, ['[command="#create-folder-shortcut"]']);
171   }).then(function(result) {
172     chrome.test.assertTrue(result);
173     return remoteCall.waitForElement(windowId, directory.navItem);
174   });
175 }
176
177 /**
178  * Removes folder shortcut to |directory|.
179  * The current directory must be a parent of the |directory|.
180  * @param {string} windowId ID of target window.
181  * @param {Object} directory Directory of shortcut ot be removed.
182  * @return {Promise} Promise fullfilled on success.
183  */
184 function removeShortcut(windowId, directory) {
185   return remoteCall.callRemoteTestUtil(
186       'fakeMouseRightClick',
187       windowId,
188       [directory.navItem]).then(function(result) {
189     chrome.test.assertTrue(result);
190     return remoteCall.waitForElement(
191         windowId, '#roots-context-menu:not([hidden])');
192   }).then(function() {
193     return remoteCall.waitForElement(
194         windowId, '[command="#remove-folder-shortcut"]');
195   }).then(function() {
196     return remoteCall.callRemoteTestUtil(
197         'fakeMouseClick', windowId, ['[command="#remove-folder-shortcut"]']);
198   }).then(function(result) {
199     chrome.test.assertTrue(result);
200     return remoteCall.waitForElementLost(windowId, directory.navItem);
201   });
202 }
203
204 /**
205  * Waits until the current directory become |currentDir| and folder shortcut to
206  * |shortcutDir| is selected.
207  * @param {string} windowId ID of target window.
208  * @param {Object} currentDir Directory which should be a current directory.
209  * @param {Object} shortcutDir Directory whose shortcut should be selected.
210  * @return {Promise} Promise fullfilled on success.
211  */
212 function expectSelection(windowId, currentDir, shortcutDir) {
213   return remoteCall.waitForFiles(windowId, currentDir.contents).
214       then(function() {
215         return remoteCall.waitForElement(
216             windowId, shortcutDir.navItem + '[selected]');
217       });
218 }
219
220 /**
221  * Clicks folder shortcut to |directory|.
222  * @param {string} windowId ID of target window.
223  * @param {Object} directory Directory whose shortcut will be clicked.
224  * @return {Promise} Promise fullfilled with result of fakeMouseClick.
225  */
226 function clickShortcut(windowId, directory) {
227   return remoteCall.waitForElement(windowId, directory.navItem).
228     then(function() {
229       return remoteCall.callRemoteTestUtil(
230           'fakeMouseClick', windowId, [directory.navItem]);
231     });
232 }
233
234 /**
235  * Creates some shortcuts and traverse them and some other directories.
236  */
237 testcase.traverseFolderShortcuts = function() {
238   var windowId;
239   StepsRunner.run([
240     // Set up each window.
241     function() {
242       addEntries(['drive'], ENTRY_SET, this.next);
243     },
244     function(result) {
245       chrome.test.assertTrue(result);
246       openNewWindow(null, RootPath.DRIVE).then(this.next);
247     },
248     function(inWindowId) {
249       windowId = inWindowId;
250       remoteCall.waitForElement(windowId, '#detail-table').then(this.next);
251     },
252     function() {
253       expandDirectoryTree(windowId).then(this.next);
254     },
255     function() {
256       remoteCall.waitForFiles(windowId, DIRECTORY.Drive.contents).
257           then(this.next);
258     },
259
260     // Create shortcut to D
261     function() {
262       createShortcut(windowId, DIRECTORY.D).then(this.next);
263     },
264
265     // Create sortcut to C
266     function() {
267       navigateToDirectory(windowId, DIRECTORY.B).then(this.next);
268     },
269     function() {
270       createShortcut(windowId, DIRECTORY.C).then(this.next);
271     },
272
273     // Click shortcut to drive.
274     // Current directory should be Drive root.
275     // Shortcut to Drive root should be selected.
276     function() {
277       clickShortcut(windowId, DIRECTORY.Drive).then(this.next);
278     },
279     function() {
280       expectSelection(
281           windowId, DIRECTORY.Drive, DIRECTORY.Drive).then(this.next);
282     },
283
284     // Press Ctrl+4 to select 4th shortcut.
285     // Current directory should be D.
286     // Shortcut to C should be selected.
287     function() {
288       remoteCall.callRemoteTestUtil('fakeKeyDown', windowId,
289           ['#file-list', 'U+0034', true], this.next);
290     },
291     function(result) {
292       chrome.test.assertTrue(result);
293       expectSelection(windowId, DIRECTORY.D, DIRECTORY.D).then(this.next);
294     },
295
296     // Press UP to select 3rd shortcut.
297     // Current directory should be C.
298     // Shortcut to C should be selected.
299     function() {
300       remoteCall.callRemoteTestUtil('fakeKeyDown', windowId,
301           ['#directory-tree', 'Up', false], this.next);
302     },
303     function(result) {
304       chrome.test.assertTrue(result);
305       expectSelection(windowId, DIRECTORY.C, DIRECTORY.C).then(this.next);
306     },
307
308     function() {
309       checkIfNoErrorsOccured(this.next);
310     }
311   ]);
312 };
313
314 /**
315  * Adds and removes shortcuts from other window and check if the active
316  * directories and selected navigation items are correct.
317  */
318 testcase.addRemoveFolderShortcuts = function() {
319   var windowId1;
320   var windowId2;
321   StepsRunner.run([
322     // Set up each window.
323     function() {
324       addEntries(['drive'], ENTRY_SET, this.next);
325     },
326     function(result) {
327       chrome.test.assertTrue(result);
328       openWindows().then(this.next);
329     },
330     function(windowIds) {
331       windowId1 = windowIds[0];
332       windowId2 = windowIds[1];
333       expandDirectoryTree(windowId1).then(this.next);
334     },
335     function() {
336       expandDirectoryTree(windowId2).then(this.next);
337     },
338     function() {
339       remoteCall.waitForFiles(windowId1, DIRECTORY.Drive.contents).
340           then(this.next);
341     },
342     function() {
343       remoteCall.waitForFiles(windowId2, DIRECTORY.Drive.contents).
344           then(this.next);
345     },
346
347     // Create shortcut to D
348     function() {
349       createShortcut(windowId1, DIRECTORY.D).then(this.next);
350     },
351
352     // Click D.
353     // Current directory should be D.
354     // Shortcut to D should be selected.
355     function() {
356       clickShortcut(windowId1, DIRECTORY.D).then(this.next);
357     },
358     function() {
359       expectSelection(windowId1, DIRECTORY.D, DIRECTORY.D).then(this.next);
360     },
361
362     // Create shortcut to A in another window.
363     function() {
364       createShortcut(windowId2, DIRECTORY.A).then(this.next);
365     },
366
367     // The index of shortcut to D is changed.
368     // Current directory should remain D.
369     // Shortcut to D should keep selected.
370     function() {
371       expectSelection(windowId1, DIRECTORY.D, DIRECTORY.D).then(this.next);
372     },
373
374     // Remove shortcut to D in another window.
375     function() {
376       removeShortcut(windowId2, DIRECTORY.D).then(this.next);
377     },
378
379     // Directory D in the directory tree should be selected.
380     function() {
381       remoteCall.waitForElement(windowId1, TREEITEM_D + '[selected]').
382           then(this.next);
383     },
384
385     function() {
386       checkIfNoErrorsOccured(this.next);
387     }
388   ]);
389 };
390