services/sync/tps/extensions/mozmill/resource/modules/driver.js

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, you can obtain one at http://mozilla.org/MPL/2.0/. */
     5 /**
     6  * @namespace Defines the Mozmill driver for global actions
     7  */
     8 var driver = exports;
    10 Cu.import("resource://gre/modules/Services.jsm");
    12 // Temporarily include utils module to re-use sleep
    13 var assertions = {}; Cu.import('resource://mozmill/modules/assertions.js', assertions);
    14 var mozmill = {}; Cu.import("resource://mozmill/driver/mozmill.js", mozmill);
    15 var utils = {}; Cu.import('resource://mozmill/stdlib/utils.js', utils);
    17 /**
    18  * Gets the topmost browser window. If there are none at that time, optionally
    19  * opens one. Otherwise will raise an exception if none are found.
    20  *
    21  * @memberOf driver
    22  * @param {Boolean] [aOpenIfNone=true] Open a new browser window if none are found.
    23  * @returns {DOMWindow}
    24  */
    25 function getBrowserWindow(aOpenIfNone) {
    26   // Set default
    27   if (typeof aOpenIfNone === 'undefined') {
    28     aOpenIfNone = true;
    29   }
    31   // If implicit open is off, turn on strict checking, and vice versa.
    32   let win = getTopmostWindowByType("navigator:browser", !aOpenIfNone);
    34   // Can just assume automatic open here. If we didn't want it and nothing found,
    35   // we already raised above when getTopmostWindow was called.
    36   if (!win)
    37     win = openBrowserWindow();
    39   return win;
    40 }
    43 /**
    44  * Retrieves the hidden window on OS X
    45  *
    46  * @memberOf driver
    47  * @returns {DOMWindow} The hidden window
    48  */
    49 function getHiddenWindow() {
    50   return Services.appShell.hiddenDOMWindow;
    51 }
    54 /**
    55  * Opens a new browser window
    56  *
    57  * @memberOf driver
    58  * @returns {DOMWindow}
    59  */
    60 function openBrowserWindow() {
    61   // On OS X we have to be able to create a new browser window even with no other
    62   // window open. Therefore we have to use the hidden window. On other platforms
    63   // at least one remaining browser window has to exist.
    64   var win = mozmill.isMac ? getHiddenWindow() :
    65                             getTopmostWindowByType("navigator:browser", true);
    66   return win.OpenBrowserWindow();
    67 }
    70 /**
    71  * Pause the test execution for the given amount of time
    72  *
    73  * @type utils.sleep
    74  * @memberOf driver
    75  */
    76 var sleep = utils.sleep;
    78 /**
    79  * Wait until the given condition via the callback returns true.
    80  *
    81  * @type utils.waitFor
    82  * @memberOf driver
    83  */
    84 var waitFor = assertions.Assert.waitFor;
    86 //
    87 // INTERNAL WINDOW ENUMERATIONS
    88 //
    90 /**
    91  * Internal function to build a list of DOM windows using a given enumerator
    92  * and filter.
    93  *
    94  * @private
    95  * @memberOf driver
    96  * @param {nsISimpleEnumerator} aEnumerator Window enumerator to use.
    97  * @param {Function} [aFilterCallback] Function which is used to filter windows.
    98  * @param {Boolean} [aStrict=true] Throw an error if no windows found
    99  *
   100  * @returns {DOMWindow[]} The windows found, in the same order as the enumerator.
   101  */
   102 function _getWindows(aEnumerator, aFilterCallback, aStrict) {
   103   // Set default
   104   if (typeof aStrict === 'undefined')
   105     aStrict = true;
   107   let windows = [];
   109   while (aEnumerator.hasMoreElements()) {
   110     let window = aEnumerator.getNext();
   112     if (!aFilterCallback || aFilterCallback(window)) {
   113       windows.push(window);
   114     }
   115   }
   117   // If this list is empty and we're strict, throw an error
   118   if (windows.length === 0 && aStrict) {
   119     var message = 'No windows were found';
   121     // We'll throw a more detailed error if a filter was used.
   122     if (aFilterCallback && aFilterCallback.name)
   123       message += ' using filter "' + aFilterCallback.name + '"';
   125     throw new Error(message);
   126   }
   128   return windows;
   129 }
   131 //
   132 // FILTER CALLBACKS
   133 //
   135 /**
   136  * Generator of a closure to filter a window based by a method
   137  *
   138  * @memberOf driver
   139  * @param {String} aName Name of the method in the window object.
   140  * @returns {Boolean} True if the condition is met.
   141  */
   142 function windowFilterByMethod(aName) {
   143   return function byMethod(aWindow) { return (aName in aWindow); }
   144 }
   147 /**
   148  * Generator of a closure to filter a window based by the its title
   149  *
   150  * @param {String} aTitle Title of the window.
   151  * @returns {Boolean} True if the condition is met.
   152  */
   153 function windowFilterByTitle(aTitle) {
   154   return function byTitle(aWindow) { return (aWindow.document.title === aTitle); }
   155 }
   158 /**
   159  * Generator of a closure to filter a window based by the its type
   160  *
   161  * @memberOf driver
   162  * @param {String} aType Type of the window.
   163  * @returns {Boolean} True if the condition is met.
   164  */
   165 function windowFilterByType(aType) {
   166   return function byType(aWindow) {
   167            var type = aWindow.document.documentElement.getAttribute("windowtype");
   168            return (type === aType);
   169          }
   170 }
   172 //
   173 // WINDOW LIST RETRIEVAL FUNCTIONS
   174 //
   176 /**
   177  * Retrieves a sorted list of open windows based on their age (newest to oldest),
   178  * optionally matching filter criteria.
   179  *
   180  * @memberOf driver
   181  * @param {Function} [aFilterCallback] Function which is used to filter windows.
   182  * @param {Boolean} [aStrict=true] Throw an error if no windows found
   183  *
   184  * @returns {DOMWindow[]} List of windows.
   185  */
   186 function getWindowsByAge(aFilterCallback, aStrict) {
   187   var windows = _getWindows(Services.wm.getEnumerator(""),
   188                             aFilterCallback, aStrict);
   190   // Reverse the list, since naturally comes back old->new
   191   return windows.reverse();
   192 }
   195 /**
   196  * Retrieves a sorted list of open windows based on their z order (topmost first),
   197  * optionally matching filter criteria.
   198  *
   199  * @memberOf driver
   200  * @param {Function} [aFilterCallback] Function which is used to filter windows.
   201  * @param {Boolean} [aStrict=true] Throw an error if no windows found
   202  *
   203  * @returns {DOMWindow[]} List of windows.
   204  */
   205 function getWindowsByZOrder(aFilterCallback, aStrict) {
   206   return _getWindows(Services.wm.getZOrderDOMWindowEnumerator("", true),
   207                      aFilterCallback, aStrict);
   208 }
   210 //
   211 // SINGLE WINDOW RETRIEVAL FUNCTIONS
   212 //
   214 /**
   215  * Retrieves the last opened window, optionally matching filter criteria.
   216  *
   217  * @memberOf driver
   218  * @param {Function} [aFilterCallback] Function which is used to filter windows.
   219  * @param {Boolean} [aStrict=true] If true, throws error if no window found.
   220  *
   221  * @returns {DOMWindow} The window, or null if none found and aStrict == false
   222  */
   223 function getNewestWindow(aFilterCallback, aStrict) {
   224   var windows = getWindowsByAge(aFilterCallback, aStrict);
   225   return windows.length ? windows[0] : null;
   226 }
   228 /**
   229  * Retrieves the topmost window, optionally matching filter criteria.
   230  *
   231  * @memberOf driver
   232  * @param {Function} [aFilterCallback] Function which is used to filter windows.
   233  * @param {Boolean} [aStrict=true] If true, throws error if no window found.
   234  *
   235  * @returns {DOMWindow} The window, or null if none found and aStrict == false
   236  */
   237 function getTopmostWindow(aFilterCallback, aStrict) {
   238   var windows = getWindowsByZOrder(aFilterCallback, aStrict);
   239   return windows.length ? windows[0] : null;
   240 }
   243 /**
   244  * Retrieves the topmost window given by the window type
   245  *
   246  * XXX: Bug 462222
   247  *      This function has to be used instead of getTopmostWindow until the
   248  *      underlying platform bug has been fixed.
   249  *
   250  * @memberOf driver
   251  * @param {String} [aWindowType=null] Window type to query for
   252  * @param {Boolean} [aStrict=true] Throw an error if no windows found
   253  *
   254  * @returns {DOMWindow} The window, or null if none found and aStrict == false
   255  */
   256 function getTopmostWindowByType(aWindowType, aStrict) {
   257   if (typeof aStrict === 'undefined')
   258     aStrict = true;
   260   var win = Services.wm.getMostRecentWindow(aWindowType);
   262   if (win === null && aStrict) {
   263     var message = 'No windows of type "' + aWindowType + '" were found';
   264     throw new errors.UnexpectedError(message);
   265   }
   267   return win;
   268 }
   271 // Export of functions
   272 driver.getBrowserWindow = getBrowserWindow;
   273 driver.getHiddenWindow = getHiddenWindow;
   274 driver.openBrowserWindow = openBrowserWindow;
   275 driver.sleep = sleep;
   276 driver.waitFor = waitFor;
   278 driver.windowFilterByMethod = windowFilterByMethod;
   279 driver.windowFilterByTitle = windowFilterByTitle;
   280 driver.windowFilterByType = windowFilterByType;
   282 driver.getWindowsByAge = getWindowsByAge;
   283 driver.getNewestWindow = getNewestWindow;
   284 driver.getTopmostWindowByType = getTopmostWindowByType;
   287 // XXX Bug: 462222
   288 //     Currently those functions cannot be used. So they shouldn't be exported.
   289 //driver.getWindowsByZOrder = getWindowsByZOrder;
   290 //driver.getTopmostWindow = getTopmostWindow;

mercurial