browser/devtools/shared/test/head.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/shared/test/head.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +let {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
     1.9 +let TargetFactory = devtools.TargetFactory;
    1.10 +let {console} = Cu.import("resource://gre/modules/devtools/Console.jsm", {});
    1.11 +
    1.12 +gDevTools.testing = true;
    1.13 +SimpleTest.registerCleanupFunction(() => {
    1.14 +  gDevTools.testing = false;
    1.15 +});
    1.16 +
    1.17 +/**
    1.18 + * Open a new tab at a URL and call a callback on load
    1.19 + */
    1.20 +function addTab(aURL, aCallback)
    1.21 +{
    1.22 +  waitForExplicitFinish();
    1.23 +
    1.24 +  gBrowser.selectedTab = gBrowser.addTab();
    1.25 +  content.location = aURL;
    1.26 +
    1.27 +  let tab = gBrowser.selectedTab;
    1.28 +  let browser = gBrowser.getBrowserForTab(tab);
    1.29 +
    1.30 +  function onTabLoad() {
    1.31 +    browser.removeEventListener("load", onTabLoad, true);
    1.32 +    aCallback(browser, tab, browser.contentDocument);
    1.33 +  }
    1.34 +
    1.35 +  browser.addEventListener("load", onTabLoad, true);
    1.36 +}
    1.37 +
    1.38 +registerCleanupFunction(function tearDown() {
    1.39 +  while (gBrowser.tabs.length > 1) {
    1.40 +    gBrowser.removeCurrentTab();
    1.41 +  }
    1.42 +
    1.43 +  console = undefined;
    1.44 +});
    1.45 +
    1.46 +function catchFail(func) {
    1.47 +  return function() {
    1.48 +    try {
    1.49 +      return func.apply(null, arguments);
    1.50 +    }
    1.51 +    catch (ex) {
    1.52 +      ok(false, ex);
    1.53 +      console.error(ex);
    1.54 +      finish();
    1.55 +      throw ex;
    1.56 +    }
    1.57 +  };
    1.58 +}
    1.59 +
    1.60 +/**
    1.61 + * Polls a given function waiting for the given value.
    1.62 + *
    1.63 + * @param object aOptions
    1.64 + *        Options object with the following properties:
    1.65 + *        - validator
    1.66 + *        A validator function that should return the expected value. This is
    1.67 + *        called every few milliseconds to check if the result is the expected
    1.68 + *        one. When the returned result is the expected one, then the |success|
    1.69 + *        function is called and polling stops. If |validator| never returns
    1.70 + *        the expected value, then polling timeouts after several tries and
    1.71 + *        a failure is recorded - the given |failure| function is invoked.
    1.72 + *        - success
    1.73 + *        A function called when the validator function returns the expected
    1.74 + *        value.
    1.75 + *        - failure
    1.76 + *        A function called if the validator function timeouts - fails to return
    1.77 + *        the expected value in the given time.
    1.78 + *        - name
    1.79 + *        Name of test. This is used to generate the success and failure
    1.80 + *        messages.
    1.81 + *        - timeout
    1.82 + *        Timeout for validator function, in milliseconds. Default is 5000 ms.
    1.83 + *        - value
    1.84 + *        The expected value. If this option is omitted then the |validator|
    1.85 + *        function must return a trueish value.
    1.86 + *        Each of the provided callback functions will receive two arguments:
    1.87 + *        the |aOptions| object and the last value returned by |validator|.
    1.88 + */
    1.89 +function waitForValue(aOptions)
    1.90 +{
    1.91 +  let start = Date.now();
    1.92 +  let timeout = aOptions.timeout || 5000;
    1.93 +  let lastValue;
    1.94 +
    1.95 +  function wait(validatorFn, successFn, failureFn)
    1.96 +  {
    1.97 +    if ((Date.now() - start) > timeout) {
    1.98 +      // Log the failure.
    1.99 +      ok(false, "Timed out while waiting for: " + aOptions.name);
   1.100 +      let expected = "value" in aOptions ?
   1.101 +                     "'" + aOptions.value + "'" :
   1.102 +                     "a trueish value";
   1.103 +      info("timeout info :: got '" + lastValue + "', expected " + expected);
   1.104 +      failureFn(aOptions, lastValue);
   1.105 +      return;
   1.106 +    }
   1.107 +
   1.108 +    lastValue = validatorFn(aOptions, lastValue);
   1.109 +    let successful = "value" in aOptions ?
   1.110 +                      lastValue == aOptions.value :
   1.111 +                      lastValue;
   1.112 +    if (successful) {
   1.113 +      ok(true, aOptions.name);
   1.114 +      successFn(aOptions, lastValue);
   1.115 +    }
   1.116 +    else {
   1.117 +      setTimeout(function() wait(validatorFn, successFn, failureFn), 100);
   1.118 +    }
   1.119 +  }
   1.120 +
   1.121 +  wait(aOptions.validator, aOptions.success, aOptions.failure);
   1.122 +}
   1.123 +
   1.124 +function oneTimeObserve(name, callback) {
   1.125 +  var func = function() {
   1.126 +    Services.obs.removeObserver(func, name);
   1.127 +    callback();
   1.128 +  };
   1.129 +  Services.obs.addObserver(func, name, false);
   1.130 +}

mercurial