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