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