2010-03-01 13:10:31 -08:00
|
|
|
/*=============================================================================
|
|
|
|
Common Helpers functions
|
|
|
|
=============================================================================*/
|
2010-09-28 14:15:58 -07:00
|
|
|
|
2011-04-26 15:23:18 -07:00
|
|
|
const kDefaultWait = 2000;
|
2010-09-28 14:15:58 -07:00
|
|
|
// Wait for a condition and call a supplied callback if condition is met within
|
|
|
|
// alloted time. If condition is not met, cause a hard failure, stopping the test.
|
2010-03-01 13:10:31 -08:00
|
|
|
function waitFor(callback, test, timeout) {
|
|
|
|
if (test()) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout = timeout || Date.now();
|
2011-04-26 15:23:18 -07:00
|
|
|
if (Date.now() - timeout > kDefaultWait)
|
2010-03-01 13:10:31 -08:00
|
|
|
throw "waitFor timeout";
|
|
|
|
setTimeout(waitFor, 50, callback, test, timeout);
|
|
|
|
};
|
|
|
|
|
2010-09-28 14:15:58 -07:00
|
|
|
// Wait for a condition and call a supplied callback if condition is met within
|
|
|
|
// alloted time. If condition is not met, continue anyway. Use this helper if the
|
|
|
|
// callback will test for the outcome, but not stop the entire test.
|
|
|
|
function waitForAndContinue(callback, test, timeout) {
|
|
|
|
if (test()) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout = timeout || Date.now();
|
2011-04-26 15:23:18 -07:00
|
|
|
if (Date.now() - timeout > kDefaultWait) {
|
2010-09-28 14:15:58 -07:00
|
|
|
callback();
|
2010-10-05 17:42:55 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTimeout(waitForAndContinue, 50, callback, test, timeout);
|
2010-09-28 14:15:58 -07:00
|
|
|
};
|
|
|
|
|
2011-05-03 10:12:02 -07:00
|
|
|
// Listen for the specified message once, then remove the listener.
|
|
|
|
function onMessageOnce(aMessageManager, aName, aCallback) {
|
|
|
|
aMessageManager.addMessageListener(aName, function onMessage(aMessage) {
|
|
|
|
aMessageManager.removeMessageListener(aName, onMessage);
|
2011-05-25 04:49:12 -07:00
|
|
|
setTimeout(function() {
|
|
|
|
aCallback(aMessage);
|
|
|
|
}, 0);
|
2011-05-03 10:12:02 -07:00
|
|
|
});
|
2011-05-25 04:49:12 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// This function is useful for debugging one test where you need to wait for
|
|
|
|
// application to be ready
|
|
|
|
function waitForFirstPaint(aCallback) {
|
|
|
|
function hasFirstPaint() {
|
|
|
|
let startupInfo = Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup).getStartupInfo();
|
|
|
|
return ("firstPaint" in startupInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasFirstPaint()) {
|
|
|
|
waitFor(aCallback, hasFirstPaint, Date.now() + 3000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aCallback();
|
|
|
|
};
|
2011-05-03 10:12:02 -07:00
|
|
|
|
2010-03-01 13:10:31 -08:00
|
|
|
function makeURI(spec) {
|
2010-07-13 07:36:09 -07:00
|
|
|
return Services.io.newURI(spec, null, null);
|
2010-03-01 13:10:31 -08:00
|
|
|
};
|
|
|
|
|
2010-05-03 10:52:39 -07:00
|
|
|
EventUtils.synthesizeString = function synthesizeString(aString, aWindow) {
|
|
|
|
for (let i = 0; i < aString.length; i++) {
|
|
|
|
EventUtils.synthesizeKey(aString.charAt(i), {}, aWindow);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-03-01 13:10:31 -08:00
|
|
|
EventUtils.synthesizeMouseForContent = function synthesizeMouseForContent(aElement, aOffsetX, aOffsetY, aEvent, aWindow) {
|
2010-09-16 12:29:03 -07:00
|
|
|
let container = document.getElementById("browsers");
|
2010-03-01 13:10:31 -08:00
|
|
|
let rect = container.getBoundingClientRect();
|
|
|
|
|
|
|
|
EventUtils.synthesizeMouse(aElement, rect.left + aOffsetX, rect.top + aOffsetY, aEvent, aWindow);
|
|
|
|
};
|
2010-07-27 15:51:41 -07:00
|
|
|
|
|
|
|
let AsyncTests = {
|
|
|
|
_tests: {},
|
|
|
|
waitFor: function(aMessage, aData, aCallback) {
|
|
|
|
messageManager.addMessageListener(aMessage, this);
|
|
|
|
if (!this._tests[aMessage])
|
|
|
|
this._tests[aMessage] = [];
|
|
|
|
|
|
|
|
this._tests[aMessage].push(aCallback || function() {});
|
2011-01-04 06:56:29 -08:00
|
|
|
setTimeout(function() {
|
|
|
|
Browser.selectedBrowser.messageManager.sendAsyncMessage(aMessage, aData || { });
|
|
|
|
}, 0);
|
2010-07-27 15:51:41 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(aMessage) {
|
|
|
|
let test = this._tests[aMessage.name];
|
|
|
|
let callback = test.shift();
|
|
|
|
if (callback)
|
|
|
|
callback(aMessage.json);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-05-06 14:03:10 -07:00
|
|
|
let gCurrentTest = null;
|
|
|
|
let gTests = [];
|
|
|
|
|
|
|
|
// Iterating tests by shifting test out one by one as runNextTest is called.
|
|
|
|
function runNextTest() {
|
|
|
|
// Run the next test until all tests completed
|
|
|
|
if (gTests.length > 0) {
|
|
|
|
gCurrentTest = gTests.shift();
|
|
|
|
info(gCurrentTest.desc);
|
|
|
|
gCurrentTest.run();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let serverRoot = "http://example.com/browser/mobile/chrome/tests/";
|
2011-05-04 13:25:24 -07:00
|
|
|
let baseURI = "http://mochi.test:8888/browser/mobile/chrome/tests/";
|
2011-05-25 04:49:12 -07:00
|
|
|
|
|
|
|
let chromeRoot = getRootDirectory(gTestPath);
|
|
|
|
messageManager.loadFrameScript(chromeRoot + "remote_head.js", true);
|
2010-07-27 15:51:41 -07:00
|
|
|
messageManager.loadFrameScript("chrome://mochikit/content/tests/SimpleTest/EventUtils.js", true);
|