OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 * pyautoAPI object provides a set of functions used by PyAuto tests | |
9 * to drive the file manager. | |
10 * | |
11 * Refer to chrome/test/functional/chromeos_file_browser.py for examples | |
12 * of how this API is used. | |
13 * | |
14 * TODO(olege): Fix style warnings. | |
15 */ | |
16 var pyautoAPI = { | |
17 /** | |
18 * Add the item with given name to the current selection. | |
19 * @param {string} name Name of the item to add to selection. | |
20 */ | |
21 addItemToSelection: function(name) { | |
22 var entryExists = false; | |
23 var dm = fileManager.directoryModel_.getFileList(); | |
24 for (var i = 0; i < dm.length; i++) { | |
25 if (dm.item(i).name == name) { | |
26 fileManager.currentList_.selectionModel.setIndexSelected(i, true); | |
27 fileManager.currentList_.scrollIndexIntoView(i); | |
28 fileManager.focusCurrentList_(); | |
29 entryExists = true; | |
30 break; | |
31 } | |
32 } | |
33 pyautoAPI.sendValue_(entryExists); | |
34 }, | |
35 | |
36 /** | |
37 * List all items in the current directory. | |
38 * We assume names do not contain '|' charecter. | |
39 */ | |
40 listDirectory: function() { | |
41 var list = []; | |
42 var dm = fileManager.directoryModel_.getFileList(); | |
43 for (var i = 0; i < dm.length; i++) { | |
44 list.push(dm.item(i).name); | |
45 } | |
46 pyautoAPI.sendJSONValue_(list); | |
47 }, | |
48 | |
49 /** | |
50 * Save the item using the given name. | |
51 * | |
52 * @param {string} name Name given to item to be saved. | |
53 */ | |
54 saveItemAs: function(name) { | |
55 if (fileManager.dialogType == DialogType.SELECT_SAVEAS_FILE) { | |
56 fileManager.filenameInput_.value = name; | |
57 fileManager.onOk_(); | |
58 } else { | |
59 throw new Error('Cannot save an item in this dialog type.'); | |
60 } | |
61 pyautoAPI.sendDone_(); | |
62 }, | |
63 | |
64 /** | |
65 * Open selected item. | |
66 */ | |
67 openItem: function() { | |
68 switch (fileManager.dialogType) { | |
69 case DialogType.SELECT_FOLDER: | |
70 case DialogType.SELECT_OPEN_FILE: | |
71 case DialogType.SELECT_OPEN_MULTI_FILE: | |
72 fileManager.onOk_(); | |
73 break; | |
74 default: | |
75 throw new Error('Cannot open an item in this dialog type.'); | |
76 } | |
77 pyautoAPI.sendDone_(); | |
78 }, | |
79 | |
80 /** | |
81 * Execute the default task for the selected item. | |
82 */ | |
83 executeDefaultTask: function() { | |
84 switch (fileManager.dialogType) { | |
85 case DialogType.FULL_PAGE: | |
86 if (fileManager.getSelection().tasks) | |
87 fileManager.getSelection().tasks.executeDefault(); | |
88 else | |
89 throw new Error('Cannot execute a task on an empty selection.'); | |
90 break; | |
91 default: | |
92 throw new Error('Cannot execute a task in this dialog type.'); | |
93 } | |
94 pyautoAPI.sendDone_(); | |
95 }, | |
96 | |
97 /** | |
98 * Executes the clipboard command. | |
99 * @param {string} command Command name. | |
100 */ | |
101 executeClipboardCommand_: function(command) { | |
102 // Input should not be focused, or the cut/cop/paste command | |
103 // will be treated as textual editing. | |
104 fileManager.filenameInput_.blur(); | |
105 fileManager.document_.execCommand(command); | |
106 }, | |
107 | |
108 /** | |
109 * Copy selected items to clipboard. | |
110 */ | |
111 copyItems: function() { | |
112 pyautoAPI.executeClipboardCommand_('copy'); | |
113 pyautoAPI.sendDone_(); | |
114 }, | |
115 | |
116 /** | |
117 * Cut selected items to clipboard. | |
118 */ | |
119 cutItems: function() { | |
120 pyautoAPI.executeClipboardCommand_('cut'); | |
121 pyautoAPI.sendDone_(); | |
122 }, | |
123 | |
124 /** | |
125 * Paste items from clipboard. | |
126 */ | |
127 pasteItems: function() { | |
128 var dm = fileManager.directoryModel_; | |
129 var onRescan = function() { | |
130 dm.removeEventListener('rescan-completed', onRescan); | |
131 pyautoAPI.sendDone_(); | |
132 }; | |
133 | |
134 dm.addEventListener('rescan-completed', onRescan); | |
135 pyautoAPI.executeClipboardCommand_('paste'); | |
136 }, | |
137 | |
138 /** | |
139 * Rename selected item. | |
140 * @param {string} name New name of the item. | |
141 */ | |
142 renameItem: function(name) { | |
143 var entry = fileManager.getSelection().entries[0]; | |
144 util.rename(entry, name, | |
145 function(newEntry) { | |
146 // Update directory model on success. | |
147 fileManager.directoryModel_.onRenameEntry( | |
148 entry, newEntry, pyautoAPI.sendDone_); | |
149 }, | |
150 pyautoAPI.sendDone_); | |
151 }, | |
152 | |
153 /** | |
154 * Delete selected entries. | |
155 */ | |
156 deleteItems: function() { | |
157 var dm = fileManager.directoryModel_; | |
158 var onRescan = function() { | |
159 dm.removeEventListener('rescan-completed', onRescan); | |
160 pyautoAPI.sendDone_(); | |
161 }; | |
162 | |
163 dm.addEventListener('rescan-completed', onRescan); | |
164 fileManager.deleteSelection(); | |
165 }, | |
166 | |
167 /** | |
168 * Create directory. | |
169 * @param {string} name Name of the directory. | |
170 */ | |
171 createDirectory: function(name) { | |
172 var dm = fileManager.directoryModel_; | |
173 var onRescan = function() { | |
174 dm.removeEventListener('rescan-completed', onRescan); | |
175 pyautoAPI.sendDone_(); | |
176 }; | |
177 | |
178 dm.addEventListener('rescan-completed', onRescan); | |
179 fileManager.directoryModel_.createDirectory(name, function() {}); | |
180 }, | |
181 | |
182 /** | |
183 * Change to a directory. | |
184 * A path starting with '/' * is absolute, otherwise it is relative to the | |
185 * current directory. | |
186 * @param {string} path Path to directory. | |
187 */ | |
188 changeDirectory: function(path) { | |
189 if (path.charAt(0) != '/') | |
190 path = fileManager.getCurrentDirectory() + '/' + path; | |
191 var dm = fileManager.directoryModel_; | |
192 | |
193 var onChanged = function() { | |
194 dm.removeEventListener('directory-changed', onChanged); | |
195 pyautoAPI.sendDone_(); | |
196 }; | |
197 | |
198 dm.addEventListener('directory-changed', onChanged); | |
199 dm.changeDirectory(path); | |
200 }, | |
201 | |
202 /** | |
203 * Get the absolute path of current directory. | |
204 */ | |
205 currentDirectory: function() { | |
206 pyautoAPI.sendValue_(fileManager.getCurrentDirectory()); | |
207 }, | |
208 | |
209 /** | |
210 * Get remaining and total size of selected directory. | |
211 */ | |
212 getSelectedDirectorySizeStats: function() { | |
213 var directoryURL = fileManager.getSelection().entries[0].toURL(); | |
214 chrome.fileBrowserPrivate.getSizeStats(directoryURL, function(stats) { | |
215 pyautoAPI.sendJSONValue_(stats); | |
216 }); | |
217 }, | |
218 | |
219 /** | |
220 * Returns whether the file manager is initialized. | |
221 * This function is polled by pyauto before calling any | |
222 * of the functions above. | |
223 */ | |
224 isInitialized: function() { | |
225 var initialized = fileManager && | |
226 fileManager.workerInitialized_ && | |
227 fileManager.getCurrentDirectory(); | |
228 pyautoAPI.sendValue_(!!initialized); | |
229 }, | |
230 | |
231 /** | |
232 * Callback function for returning primitiv types (int, string, boolean) | |
233 */ | |
234 sendValue_: function(value) { | |
235 window.domAutomationController.send(value); | |
236 }, | |
237 | |
238 /** | |
239 * Callback function for returning a JSON encoded value. | |
240 */ | |
241 sendJSONValue_: function(value) { | |
242 window.domAutomationController.send(JSON.stringify(value)); | |
243 }, | |
244 | |
245 /** | |
246 * Callback function signalling completion of operation. | |
247 */ | |
248 sendDone_: function() { | |
249 window.domAutomationController.send('done'); | |
250 } | |
251 }; | |
OLD | NEW |