Allow overlapping sync and async startup requests
[chromium-blink-merge.git] / chrome / test / pyautolib / chromeos / file_browser.py
blobcad92b178f467a4910f1ebcc9ca6488032f8884b
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.
11 Example:
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.
20 """
22 def __init__(self, ui_test, executor):
23 """Initialize FileBrowser.
25 Args:
26 ui_test: derived from pyauto.PyUITest - base class for UI test cases.
27 executor: derived from pyauto.PyUITest.JavascriptExecutor.
28 """
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.
35 Args:
36 name: Name of the entry to add to selection
38 Returns:
39 Whether entry exists.
40 """
41 script = """
42 pyautoAPI.addItemToSelection('%s');
43 """ % name
44 return self.executor.Execute(script)
46 def DirectoryContents(self):
47 """Return a set containing all entries in the current directory.
49 Returns:
50 A set of entries.
51 """
52 script = """
53 pyautoAPI.listDirectory();
54 """
55 list = json.loads(self.executor.Execute(script))
56 return set(list)
58 def Save(self, name):
59 """Save the entry using the given name.
61 Args:
62 name: Name given to entry to be saved.
63 """
64 script = """
65 pyautoAPI.saveItemAs('%s');
66 """ % name
67 self.executor.Execute(script)
69 def Open(self):
70 """Open selected entries."""
71 script = """
72 pyautoAPI.openItem();
73 """
74 self.executor.Execute(script)
76 def ExecuteDefaultTask(self):
77 """Open selected entries."""
78 script = """
79 pyautoAPI.executeDefaultTask()
80 """
81 self.executor.Execute(script)
83 def Copy(self):
84 """Copy selected entries to clipboard."""
85 script = """
86 pyautoAPI.copyItems();
87 """
88 self.executor.Execute(script)
90 def Cut(self):
91 """Cut selected entries to clipboard. """
92 script = """
93 pyautoAPI.cutItems();
94 """
95 self.executor.Execute(script)
97 def Paste(self):
98 """Paste entries from clipboard."""
99 script = """
100 pyautoAPI.pasteItems();
102 self.executor.Execute(script)
104 def Rename(self, name):
105 """Rename selected entry.
107 Args:
108 name: New name of the entry.
110 script = """
111 pyautoAPI.renameItem('%s');
112 """ % name
113 self.executor.Execute(script)
115 def Delete(self):
116 """Delete selected entries."""
117 script = """
118 pyautoAPI.deleteItems();
120 self.executor.Execute(script)
122 def CreateDirectory(self, name):
123 """Create directory.
125 Args:
126 name: Name of the directory.
128 script = """
129 pyautoAPI.createDirectory('%s');
130 """ % name
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
137 current directory.
139 Args:
140 name: Path to directory.
142 script = """
143 pyautoAPI.changeDirectory('%s');
144 """ % path
145 self.executor.Execute(script)
147 def CurrentDirectory(self):
148 """Get the absolute path of current directory.
150 Returns:
151 Path to the current directory.
153 script = """
154 pyautoAPI.currentDirectory();
156 return self.executor.Execute(script)
158 def GetSelectedDirectorySizeStats(self):
159 """Get remaining and total size of selected directory.
161 Returns:
162 A tuple: (remaining size in KB, total size in KB)
164 script = """
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.
175 Returns:
176 Whether file manager is initialied.
178 def _IsInitialized():
179 script = """
180 pyautoAPI.isInitialized();
182 return self.executor.Execute(script)
183 return self._ui_test.WaitUntil(lambda: _IsInitialized())