Home | History | Annotate | Download | only in chromeos
      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 import simplejson as json  # found in third_party
      6 
      7 
      8 class FileBrowser(object):
      9   """This class provides an API for automating the ChromeOS File Browser.
     10 
     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')
     18 
     19   For complete examples refer to chromeos_file_browser.py.
     20   """
     21 
     22   def __init__(self, ui_test, executor):
     23     """Initialize FileBrowser.
     24 
     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
     31 
     32   def Select(self, name):
     33     """Add entry with given name to the current selection.
     34 
     35     Args:
     36       name: Name of the entry to add to selection
     37 
     38     Returns:
     39        Whether entry exists.
     40     """
     41     script = """
     42       pyautoAPI.addItemToSelection('%s');
     43     """ % name
     44     return self.executor.Execute(script)
     45 
     46   def DirectoryContents(self):
     47     """Return a set containing all entries in the current directory.
     48 
     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)
     57 
     58   def Save(self, name):
     59     """Save the entry using the given name.
     60 
     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)
     68 
     69   def Open(self):
     70     """Open selected entries."""
     71     script = """
     72       pyautoAPI.openItem();
     73     """
     74     self.executor.Execute(script)
     75 
     76   def ExecuteDefaultTask(self):
     77     """Open selected entries."""
     78     script = """
     79       pyautoAPI.executeDefaultTask()
     80     """
     81     self.executor.Execute(script)
     82 
     83   def Copy(self):
     84     """Copy selected entries to clipboard."""
     85     script = """
     86       pyautoAPI.copyItems();
     87     """
     88     self.executor.Execute(script)
     89 
     90   def Cut(self):
     91     """Cut selected entries to clipboard. """
     92     script = """
     93       pyautoAPI.cutItems();
     94     """
     95     self.executor.Execute(script)
     96 
     97   def Paste(self):
     98     """Paste entries from clipboard."""
     99     script = """
    100       pyautoAPI.pasteItems();
    101     """
    102     self.executor.Execute(script)
    103 
    104   def Rename(self, name):
    105     """Rename selected entry.
    106 
    107     Args:
    108       name: New name of the entry.
    109     """
    110     script = """
    111       pyautoAPI.renameItem('%s');
    112     """ % name
    113     self.executor.Execute(script)
    114 
    115   def Delete(self):
    116     """Delete selected entries."""
    117     script = """
    118       pyautoAPI.deleteItems();
    119     """
    120     self.executor.Execute(script)
    121 
    122   def CreateDirectory(self, name):
    123     """Create directory.
    124 
    125     Args:
    126       name: Name of the directory.
    127     """
    128     script = """
    129       pyautoAPI.createDirectory('%s');
    130     """ % name
    131     self.executor.Execute(script)
    132 
    133   def ChangeDirectory(self, path):
    134     """Change to a directory.
    135 
    136     A path starting with '/' is absolute, otherwise it is relative to the
    137     current directory.
    138 
    139     Args:
    140       name: Path to directory.
    141     """
    142     script = """
    143       pyautoAPI.changeDirectory('%s');
    144     """ % path
    145     self.executor.Execute(script)
    146 
    147   def CurrentDirectory(self):
    148     """Get the absolute path of current directory.
    149 
    150     Returns:
    151       Path to the current directory.
    152     """
    153     script = """
    154       pyautoAPI.currentDirectory();
    155     """
    156     return self.executor.Execute(script)
    157 
    158   def GetSelectedDirectorySizeStats(self):
    159     """Get remaining and total size of selected directory.
    160 
    161     Returns:
    162       A tuple: (remaining size in KB, total size in KB)
    163     """
    164     script = """
    165       pyautoAPI.getSelectedDirectorySizeStats();
    166     """
    167     stats = json.loads(self.executor.Execute(script))
    168     return stats['remainingSizeKB'], stats['totalSizeKB']
    169 
    170   def WaitUntilInitialized(self):
    171     """Returns whether the file manager is initialized.
    172 
    173     This should be called before calling any of the functions above.
    174 
    175     Returns:
    176       Whether file manager is initialied.
    177     """
    178     def _IsInitialized():
    179       script = """
    180         pyautoAPI.isInitialized();
    181       """
    182       return self.executor.Execute(script)
    183     return self._ui_test.WaitUntil(lambda: _IsInitialized())
    184