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.

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

mercurial