browser/devtools/shared/test/head.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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 let {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
     6 let TargetFactory = devtools.TargetFactory;
     7 let {console} = Cu.import("resource://gre/modules/devtools/Console.jsm", {});
     9 gDevTools.testing = true;
    10 SimpleTest.registerCleanupFunction(() => {
    11   gDevTools.testing = false;
    12 });
    14 /**
    15  * Open a new tab at a URL and call a callback on load
    16  */
    17 function addTab(aURL, aCallback)
    18 {
    19   waitForExplicitFinish();
    21   gBrowser.selectedTab = gBrowser.addTab();
    22   content.location = aURL;
    24   let tab = gBrowser.selectedTab;
    25   let browser = gBrowser.getBrowserForTab(tab);
    27   function onTabLoad() {
    28     browser.removeEventListener("load", onTabLoad, true);
    29     aCallback(browser, tab, browser.contentDocument);
    30   }
    32   browser.addEventListener("load", onTabLoad, true);
    33 }
    35 registerCleanupFunction(function tearDown() {
    36   while (gBrowser.tabs.length > 1) {
    37     gBrowser.removeCurrentTab();
    38   }
    40   console = undefined;
    41 });
    43 function catchFail(func) {
    44   return function() {
    45     try {
    46       return func.apply(null, arguments);
    47     }
    48     catch (ex) {
    49       ok(false, ex);
    50       console.error(ex);
    51       finish();
    52       throw ex;
    53     }
    54   };
    55 }
    57 /**
    58  * Polls a given function waiting for the given value.
    59  *
    60  * @param object aOptions
    61  *        Options object with the following properties:
    62  *        - validator
    63  *        A validator function that should return the expected value. This is
    64  *        called every few milliseconds to check if the result is the expected
    65  *        one. When the returned result is the expected one, then the |success|
    66  *        function is called and polling stops. If |validator| never returns
    67  *        the expected value, then polling timeouts after several tries and
    68  *        a failure is recorded - the given |failure| function is invoked.
    69  *        - success
    70  *        A function called when the validator function returns the expected
    71  *        value.
    72  *        - failure
    73  *        A function called if the validator function timeouts - fails to return
    74  *        the expected value in the given time.
    75  *        - name
    76  *        Name of test. This is used to generate the success and failure
    77  *        messages.
    78  *        - timeout
    79  *        Timeout for validator function, in milliseconds. Default is 5000 ms.
    80  *        - value
    81  *        The expected value. If this option is omitted then the |validator|
    82  *        function must return a trueish value.
    83  *        Each of the provided callback functions will receive two arguments:
    84  *        the |aOptions| object and the last value returned by |validator|.
    85  */
    86 function waitForValue(aOptions)
    87 {
    88   let start = Date.now();
    89   let timeout = aOptions.timeout || 5000;
    90   let lastValue;
    92   function wait(validatorFn, successFn, failureFn)
    93   {
    94     if ((Date.now() - start) > timeout) {
    95       // Log the failure.
    96       ok(false, "Timed out while waiting for: " + aOptions.name);
    97       let expected = "value" in aOptions ?
    98                      "'" + aOptions.value + "'" :
    99                      "a trueish value";
   100       info("timeout info :: got '" + lastValue + "', expected " + expected);
   101       failureFn(aOptions, lastValue);
   102       return;
   103     }
   105     lastValue = validatorFn(aOptions, lastValue);
   106     let successful = "value" in aOptions ?
   107                       lastValue == aOptions.value :
   108                       lastValue;
   109     if (successful) {
   110       ok(true, aOptions.name);
   111       successFn(aOptions, lastValue);
   112     }
   113     else {
   114       setTimeout(function() wait(validatorFn, successFn, failureFn), 100);
   115     }
   116   }
   118   wait(aOptions.validator, aOptions.success, aOptions.failure);
   119 }
   121 function oneTimeObserve(name, callback) {
   122   var func = function() {
   123     Services.obs.removeObserver(func, name);
   124     callback();
   125   };
   126   Services.obs.addObserver(func, name, false);
   127 }

mercurial