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.
5 import simplejson
as json
# found in third_party
8 class FileBrowser(object):
9 """This class provides an API for automating the ChromeOS File Browser.
12 # Create and change into 'hello world' folder.
13 executor = pyauto.PyUITest.JavascriptExecutorInTab(self)
14 file_browser = chromeos.file_browser.FileBrowser(self, executor)
15 if file_browser.WaitUntilInitialized():
16 file_browser.CreateDirectory('hello world')
17 file_browser.ChangeDirectory('hello world')
19 For complete examples refer to chromeos_file_browser.py.
22 def __init__(self
, ui_test
, executor
):
23 """Initialize FileBrowser.
26 ui_test: derived from pyauto.PyUITest - base class for UI test cases.
27 executor: derived from pyauto.PyUITest.JavascriptExecutor.
29 self
._ui
_test
= ui_test
30 self
.executor
= executor
32 def Select(self
, name
):
33 """Add entry with given name to the current selection.
36 name: Name of the entry to add to selection
42 pyautoAPI.addItemToSelection('%s');
44 return self
.executor
.Execute(script
)
46 def DirectoryContents(self
):
47 """Return a set containing all entries in the current directory.
53 pyautoAPI.listDirectory();
55 list = json
.loads(self
.executor
.Execute(script
))
59 """Save the entry using the given name.
62 name: Name given to entry to be saved.
65 pyautoAPI.saveItemAs('%s');
67 self
.executor
.Execute(script
)
70 """Open selected entries."""
74 self
.executor
.Execute(script
)
76 def ExecuteDefaultTask(self
):
77 """Open selected entries."""
79 pyautoAPI.executeDefaultTask()
81 self
.executor
.Execute(script
)
84 """Copy selected entries to clipboard."""
86 pyautoAPI.copyItems();
88 self
.executor
.Execute(script
)
91 """Cut selected entries to clipboard. """
95 self
.executor
.Execute(script
)
98 """Paste entries from clipboard."""
100 pyautoAPI.pasteItems();
102 self
.executor
.Execute(script
)
104 def Rename(self
, name
):
105 """Rename selected entry.
108 name: New name of the entry.
111 pyautoAPI.renameItem('%s');
113 self
.executor
.Execute(script
)
116 """Delete selected entries."""
118 pyautoAPI.deleteItems();
120 self
.executor
.Execute(script
)
122 def CreateDirectory(self
, name
):
126 name: Name of the directory.
129 pyautoAPI.createDirectory('%s');
131 self
.executor
.Execute(script
)
133 def ChangeDirectory(self
, path
):
134 """Change to a directory.
136 A path starting with '/' is absolute, otherwise it is relative to the
140 name: Path to directory.
143 pyautoAPI.changeDirectory('%s');
145 self
.executor
.Execute(script
)
147 def CurrentDirectory(self
):
148 """Get the absolute path of current directory.
151 Path to the current directory.
154 pyautoAPI.currentDirectory();
156 return self
.executor
.Execute(script
)
158 def GetSelectedDirectorySizeStats(self
):
159 """Get remaining and total size of selected directory.
162 A tuple: (remaining size in KB, total size in KB)
165 pyautoAPI.getSelectedDirectorySizeStats();
167 stats
= json
.loads(self
.executor
.Execute(script
))
168 return stats
['remainingSizeKB'], stats
['totalSizeKB']
170 def WaitUntilInitialized(self
):
171 """Returns whether the file manager is initialized.
173 This should be called before calling any of the functions above.
176 Whether file manager is initialied.
178 def _IsInitialized():
180 pyautoAPI.isInitialized();
182 return self
.executor
.Execute(script
)
183 return self
._ui
_test
.WaitUntil(lambda: _IsInitialized())