2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-10-12 10:06:04 -07:00
|
|
|
|
2013-05-01 09:27:24 -07:00
|
|
|
let {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
|
2013-09-27 09:48:37 -07:00
|
|
|
let {console} = Cu.import("resource://gre/modules/devtools/Console.jsm", {});
|
2014-05-29 06:54:00 -07:00
|
|
|
let TargetFactory = devtools.TargetFactory;
|
2012-08-14 07:51:48 -07:00
|
|
|
|
2014-03-13 14:27:10 -07:00
|
|
|
gDevTools.testing = true;
|
|
|
|
SimpleTest.registerCleanupFunction(() => {
|
|
|
|
gDevTools.testing = false;
|
|
|
|
});
|
|
|
|
|
2014-06-13 07:27:10 -07:00
|
|
|
const TEST_URI_ROOT = "http://example.com/browser/browser/devtools/shared/test/";
|
|
|
|
|
2012-05-10 10:33:54 -07:00
|
|
|
/**
|
|
|
|
* Open a new tab at a URL and call a callback on load
|
|
|
|
*/
|
2011-10-12 10:06:04 -07:00
|
|
|
function addTab(aURL, aCallback)
|
|
|
|
{
|
|
|
|
waitForExplicitFinish();
|
|
|
|
|
2012-05-02 13:20:02 -07:00
|
|
|
gBrowser.selectedTab = gBrowser.addTab();
|
|
|
|
content.location = aURL;
|
|
|
|
|
2012-05-10 10:33:54 -07:00
|
|
|
let tab = gBrowser.selectedTab;
|
|
|
|
let browser = gBrowser.getBrowserForTab(tab);
|
|
|
|
|
|
|
|
function onTabLoad() {
|
|
|
|
browser.removeEventListener("load", onTabLoad, true);
|
|
|
|
aCallback(browser, tab, browser.contentDocument);
|
|
|
|
}
|
2012-05-02 13:20:02 -07:00
|
|
|
|
2011-10-12 10:06:04 -07:00
|
|
|
browser.addEventListener("load", onTabLoad, true);
|
|
|
|
}
|
|
|
|
|
2014-05-29 06:54:00 -07:00
|
|
|
function promiseTab(aURL) {
|
|
|
|
let deferred = Promise.defer();
|
|
|
|
addTab(aURL, deferred.resolve);
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
2011-10-12 10:06:04 -07:00
|
|
|
registerCleanupFunction(function tearDown() {
|
|
|
|
while (gBrowser.tabs.length > 1) {
|
|
|
|
gBrowser.removeCurrentTab();
|
|
|
|
}
|
|
|
|
|
2012-05-10 10:33:54 -07:00
|
|
|
console = undefined;
|
2011-10-12 10:06:04 -07:00
|
|
|
});
|
2012-05-10 10:33:54 -07:00
|
|
|
|
2012-05-15 03:27:19 -07:00
|
|
|
function catchFail(func) {
|
|
|
|
return function() {
|
|
|
|
try {
|
|
|
|
return func.apply(null, arguments);
|
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
ok(false, ex);
|
|
|
|
console.error(ex);
|
|
|
|
finish();
|
2012-05-17 11:04:33 -07:00
|
|
|
throw ex;
|
2012-05-15 03:27:19 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2012-06-14 04:36:48 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Polls a given function waiting for the given value.
|
|
|
|
*
|
|
|
|
* @param object aOptions
|
|
|
|
* Options object with the following properties:
|
|
|
|
* - validator
|
|
|
|
* A validator function that should return the expected value. This is
|
|
|
|
* called every few milliseconds to check if the result is the expected
|
|
|
|
* one. When the returned result is the expected one, then the |success|
|
|
|
|
* function is called and polling stops. If |validator| never returns
|
|
|
|
* the expected value, then polling timeouts after several tries and
|
|
|
|
* a failure is recorded - the given |failure| function is invoked.
|
|
|
|
* - success
|
|
|
|
* A function called when the validator function returns the expected
|
|
|
|
* value.
|
|
|
|
* - failure
|
|
|
|
* A function called if the validator function timeouts - fails to return
|
|
|
|
* the expected value in the given time.
|
|
|
|
* - name
|
|
|
|
* Name of test. This is used to generate the success and failure
|
|
|
|
* messages.
|
|
|
|
* - timeout
|
|
|
|
* Timeout for validator function, in milliseconds. Default is 5000 ms.
|
|
|
|
* - value
|
|
|
|
* The expected value. If this option is omitted then the |validator|
|
|
|
|
* function must return a trueish value.
|
|
|
|
* Each of the provided callback functions will receive two arguments:
|
|
|
|
* the |aOptions| object and the last value returned by |validator|.
|
|
|
|
*/
|
|
|
|
function waitForValue(aOptions)
|
|
|
|
{
|
|
|
|
let start = Date.now();
|
|
|
|
let timeout = aOptions.timeout || 5000;
|
|
|
|
let lastValue;
|
|
|
|
|
|
|
|
function wait(validatorFn, successFn, failureFn)
|
|
|
|
{
|
|
|
|
if ((Date.now() - start) > timeout) {
|
|
|
|
// Log the failure.
|
|
|
|
ok(false, "Timed out while waiting for: " + aOptions.name);
|
|
|
|
let expected = "value" in aOptions ?
|
|
|
|
"'" + aOptions.value + "'" :
|
|
|
|
"a trueish value";
|
|
|
|
info("timeout info :: got '" + lastValue + "', expected " + expected);
|
|
|
|
failureFn(aOptions, lastValue);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastValue = validatorFn(aOptions, lastValue);
|
|
|
|
let successful = "value" in aOptions ?
|
|
|
|
lastValue == aOptions.value :
|
|
|
|
lastValue;
|
|
|
|
if (successful) {
|
|
|
|
ok(true, aOptions.name);
|
|
|
|
successFn(aOptions, lastValue);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setTimeout(function() wait(validatorFn, successFn, failureFn), 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wait(aOptions.validator, aOptions.success, aOptions.failure);
|
|
|
|
}
|
2013-03-12 21:51:30 -07:00
|
|
|
|
|
|
|
function oneTimeObserve(name, callback) {
|
|
|
|
var func = function() {
|
|
|
|
Services.obs.removeObserver(func, name);
|
|
|
|
callback();
|
|
|
|
};
|
|
|
|
Services.obs.addObserver(func, name, false);
|
|
|
|
}
|
2014-05-29 06:54:00 -07:00
|
|
|
|
|
|
|
function* createHost(type = "bottom", src = "data:text/html;charset=utf-8,") {
|
|
|
|
let host = new Hosts[type](gBrowser.selectedTab);
|
|
|
|
let iframe = yield host.create();
|
|
|
|
|
|
|
|
let loaded = Promise.defer();
|
|
|
|
let domHelper = new DOMHelpers(iframe.contentWindow);
|
|
|
|
iframe.setAttribute("src", src);
|
|
|
|
domHelper.onceDOMReady(loaded.resolve);
|
|
|
|
yield loaded.promise;
|
|
|
|
|
|
|
|
return [host, iframe.contentWindow, iframe.contentDocument];
|
|
|
|
}
|