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