2005-03-25 06:41:33 +03:00
|
|
|
/* vim:set ts=2 sw=2 sts=2 et: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Google Inc.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2005
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Darin Fisher <darin@meer.net>
|
2006-01-27 23:56:16 +03:00
|
|
|
* Boris Zbarsky <bzbarsky@mit.edu>
|
2007-02-03 04:46:09 +03:00
|
|
|
* Jeff Walden <jwalden+code@mit.edu>
|
2009-06-05 02:05:22 +04:00
|
|
|
* Serge Gautherie <sgautherie.bz@free.fr>
|
2005-03-25 06:41:33 +03:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2009-03-17 17:22:55 +03:00
|
|
|
/*
|
|
|
|
* This file contains common code that is loaded before each test file(s).
|
2006-09-12 10:19:26 +04:00
|
|
|
* See http://developer.mozilla.org/en/docs/Writing_xpcshell-based_unit_tests
|
2009-03-17 17:22:55 +03:00
|
|
|
* for more information.
|
2006-09-12 10:19:26 +04:00
|
|
|
*/
|
2005-03-25 06:41:33 +03:00
|
|
|
|
|
|
|
var _quit = false;
|
2009-06-05 02:05:22 +04:00
|
|
|
var _passed = true;
|
2005-03-25 06:41:33 +03:00
|
|
|
var _tests_pending = 0;
|
2009-06-05 02:05:22 +04:00
|
|
|
var _passedChecks = 0, _falsePassedChecks = 0;
|
2009-07-11 03:42:59 +04:00
|
|
|
var _cleanupFunctions = [];
|
2010-08-14 00:28:14 +04:00
|
|
|
var _pendingTimers = [];
|
2005-03-25 06:41:33 +03:00
|
|
|
|
2009-11-12 10:01:04 +03:00
|
|
|
function _dump(str) {
|
|
|
|
if (typeof _XPCSHELL_PROCESS == "undefined") {
|
|
|
|
dump(str);
|
|
|
|
} else {
|
|
|
|
dump(_XPCSHELL_PROCESS + ": " + str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-13 08:52:00 +03:00
|
|
|
// Disable automatic network detection, so tests work correctly when
|
|
|
|
// not connected to a network.
|
|
|
|
let (ios = Components.classes["@mozilla.org/network/io-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIIOService2)) {
|
|
|
|
ios.manageOfflineStatus = false;
|
|
|
|
ios.offline = false;
|
|
|
|
}
|
|
|
|
|
2009-05-11 23:54:39 +04:00
|
|
|
// Enable crash reporting, if possible
|
|
|
|
// We rely on the Python harness to set MOZ_CRASHREPORTER_NO_REPORT
|
|
|
|
// and handle checking for minidumps.
|
2010-08-12 12:55:28 +04:00
|
|
|
// Note that if we're in a child process, we don't want to init the
|
|
|
|
// crashreporter component.
|
|
|
|
try { // nsIXULRuntime is not available in some configurations.
|
|
|
|
let processType = Components.classes["@mozilla.org/xre/runtime;1"].
|
|
|
|
getService(Components.interfaces.nsIXULRuntime).processType;
|
|
|
|
if (processType == Components.interfaces.nsIXULRuntime.PROCESS_TYPE_DEFAULT &&
|
|
|
|
"@mozilla.org/toolkit/crash-reporter;1" in Components.classes) {
|
|
|
|
// Remember to update </toolkit/crashreporter/test/unit/test_crashreporter.js>
|
|
|
|
// too if you change this initial setting.
|
|
|
|
let (crashReporter =
|
|
|
|
Components.classes["@mozilla.org/toolkit/crash-reporter;1"]
|
|
|
|
.getService(Components.interfaces.nsICrashReporter)) {
|
|
|
|
crashReporter.enabled = true;
|
|
|
|
crashReporter.minidumpPath = do_get_cwd();
|
2010-07-02 00:03:42 +04:00
|
|
|
}
|
2009-05-11 23:54:39 +04:00
|
|
|
}
|
|
|
|
}
|
2010-08-12 12:55:28 +04:00
|
|
|
catch (e) { }
|
2009-05-11 23:54:39 +04:00
|
|
|
|
2010-08-14 00:28:14 +04:00
|
|
|
/**
|
|
|
|
* Date.now() is not necessarily monotonically increasing (insert sob story
|
|
|
|
* about times not being the right tool to use for measuring intervals of time,
|
|
|
|
* robarnold can tell all), so be wary of error by erring by at least
|
|
|
|
* _timerFuzz ms.
|
|
|
|
*/
|
|
|
|
const _timerFuzz = 15;
|
|
|
|
|
|
|
|
function _Timer(func, delay) {
|
|
|
|
delay = Number(delay);
|
|
|
|
if (delay < 0)
|
|
|
|
do_throw("do_timeout() delay must be nonnegative");
|
2009-06-13 17:11:00 +04:00
|
|
|
|
2009-12-28 20:31:00 +03:00
|
|
|
if (typeof func !== "function")
|
2010-08-14 00:28:14 +04:00
|
|
|
do_throw("string callbacks no longer accepted; use a function!");
|
2009-12-28 20:31:00 +03:00
|
|
|
|
|
|
|
this._func = func;
|
2010-08-14 00:28:14 +04:00
|
|
|
this._start = Date.now();
|
|
|
|
this._delay = delay;
|
|
|
|
|
|
|
|
var timer = Components.classes["@mozilla.org/timer;1"]
|
|
|
|
.createInstance(Components.interfaces.nsITimer);
|
|
|
|
timer.initWithCallback(this, delay + _timerFuzz, timer.TYPE_ONE_SHOT);
|
|
|
|
|
2009-11-03 21:39:40 +03:00
|
|
|
// Keep timer alive until it fires
|
2010-08-14 00:28:14 +04:00
|
|
|
_pendingTimers.push(timer);
|
2005-03-25 06:41:33 +03:00
|
|
|
}
|
2010-08-14 00:28:14 +04:00
|
|
|
_Timer.prototype = {
|
2005-03-25 06:41:33 +03:00
|
|
|
QueryInterface: function(iid) {
|
|
|
|
if (iid.Equals(Components.interfaces.nsITimerCallback) ||
|
|
|
|
iid.Equals(Components.interfaces.nsISupports))
|
|
|
|
return this;
|
2009-06-13 17:11:00 +04:00
|
|
|
|
2005-03-25 06:41:33 +03:00
|
|
|
throw Components.results.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
2009-06-13 17:11:00 +04:00
|
|
|
|
2005-03-25 06:41:33 +03:00
|
|
|
notify: function(timer) {
|
2010-08-14 00:28:14 +04:00
|
|
|
_pendingTimers.splice(_pendingTimers.indexOf(timer), 1);
|
|
|
|
|
|
|
|
// The current nsITimer implementation can undershoot, but even if it
|
|
|
|
// couldn't, paranoia is probably a virtue here given the potential for
|
|
|
|
// random orange on tinderboxen.
|
|
|
|
var end = Date.now();
|
|
|
|
var elapsed = end - this._start;
|
|
|
|
if (elapsed >= this._delay) {
|
|
|
|
try {
|
|
|
|
this._func.call(null);
|
|
|
|
} catch (e) {
|
2010-08-14 10:03:36 +04:00
|
|
|
do_throw("exception thrown from do_timeout callback: " + e);
|
2010-08-14 00:28:14 +04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timer undershot, retry with a little overshoot to try to avoid more
|
|
|
|
// undershoots.
|
|
|
|
var newDelay = this._delay - elapsed;
|
|
|
|
do_timeout(newDelay, this._func);
|
2005-03-25 06:41:33 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-09-12 10:19:26 +04:00
|
|
|
function _do_main() {
|
2005-03-25 06:41:33 +03:00
|
|
|
if (_quit)
|
|
|
|
return;
|
|
|
|
|
2009-11-12 10:01:04 +03:00
|
|
|
_dump("TEST-INFO | (xpcshell/head.js) | running event loop\n");
|
2009-06-05 02:05:22 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
var thr = Components.classes["@mozilla.org/thread-manager;1"]
|
|
|
|
.getService().currentThread;
|
2005-03-25 06:41:33 +03:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
while (!_quit)
|
2006-05-11 01:32:48 +04:00
|
|
|
thr.processNextEvent(true);
|
2005-03-25 06:41:33 +03:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
while (thr.hasPendingEvents())
|
2006-05-11 01:32:48 +04:00
|
|
|
thr.processNextEvent(true);
|
2005-03-25 06:41:33 +03:00
|
|
|
}
|
|
|
|
|
2006-09-12 10:19:26 +04:00
|
|
|
function _do_quit() {
|
2009-11-12 10:01:04 +03:00
|
|
|
_dump("TEST-INFO | (xpcshell/head.js) | exiting test\n");
|
2005-03-25 06:41:33 +03:00
|
|
|
|
|
|
|
_quit = true;
|
|
|
|
}
|
|
|
|
|
2010-03-08 21:26:54 +03:00
|
|
|
function _dump_exception_stack(stack) {
|
|
|
|
stack.split("\n").forEach(function(frame) {
|
|
|
|
if (!frame)
|
|
|
|
return;
|
|
|
|
// frame is of the form "fname(args)@file:line"
|
|
|
|
let frame_regexp = new RegExp("(.*)\\(.*\\)@(.*):(\\d*)", "g");
|
|
|
|
let parts = frame_regexp.exec(frame);
|
|
|
|
dump("JS frame :: " + parts[2] + " :: " + (parts[1] ? parts[1] : "anonymous") + " :: line " + parts[3] + "\n");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2009-03-17 17:22:55 +03:00
|
|
|
function _execute_test() {
|
2010-02-23 16:11:37 +03:00
|
|
|
// Map resource://test/ to the current working directory.
|
|
|
|
let (ios = Components.classes["@mozilla.org/network/io-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIIOService)) {
|
|
|
|
let protocolHandler =
|
|
|
|
ios.getProtocolHandler("resource")
|
|
|
|
.QueryInterface(Components.interfaces.nsIResProtocolHandler);
|
|
|
|
let curDirURI = ios.newFileURI(do_get_cwd());
|
|
|
|
protocolHandler.setSubstitution("test", curDirURI);
|
|
|
|
}
|
|
|
|
|
2009-06-13 17:11:00 +04:00
|
|
|
// _HEAD_FILES is dynamically defined by <runxpcshelltests.py>.
|
|
|
|
_load_files(_HEAD_FILES);
|
|
|
|
// _TEST_FILE is dynamically defined by <runxpcshelltests.py>.
|
|
|
|
_load_files(_TEST_FILE);
|
|
|
|
|
2009-03-17 17:22:55 +03:00
|
|
|
try {
|
|
|
|
do_test_pending();
|
|
|
|
run_test();
|
|
|
|
do_test_finished();
|
|
|
|
_do_main();
|
|
|
|
} catch (e) {
|
2009-06-05 02:05:22 +04:00
|
|
|
_passed = false;
|
2010-03-08 21:26:54 +03:00
|
|
|
// do_check failures are already logged and set _quit to true and throw
|
|
|
|
// NS_ERROR_ABORT. If both of those are true it is likely this exception
|
|
|
|
// has already been logged so there is no need to log it again. It's
|
|
|
|
// possible that this will mask an NS_ERROR_ABORT that happens after a
|
|
|
|
// do_check failure though.
|
|
|
|
if (!_quit || e != Components.results.NS_ERROR_ABORT) {
|
2010-09-10 21:20:38 +04:00
|
|
|
msg = "TEST-UNEXPECTED-FAIL | (xpcshell/head.js) | " + e;
|
2010-03-08 21:26:54 +03:00
|
|
|
if (e.stack) {
|
2010-09-10 21:20:38 +04:00
|
|
|
_dump(msg + " - See following stack:\n");
|
2010-03-08 21:26:54 +03:00
|
|
|
_dump_exception_stack(e.stack);
|
|
|
|
}
|
|
|
|
else {
|
2010-09-10 21:20:38 +04:00
|
|
|
_dump(msg + "\n");
|
2010-03-08 21:26:54 +03:00
|
|
|
}
|
|
|
|
}
|
2009-03-17 17:22:55 +03:00
|
|
|
}
|
|
|
|
|
2009-06-13 17:11:00 +04:00
|
|
|
// _TAIL_FILES is dynamically defined by <runxpcshelltests.py>.
|
|
|
|
_load_files(_TAIL_FILES);
|
|
|
|
|
2009-07-11 03:42:59 +04:00
|
|
|
// Execute all of our cleanup functions.
|
|
|
|
var func;
|
|
|
|
while ((func = _cleanupFunctions.pop()))
|
|
|
|
func();
|
|
|
|
|
2009-06-05 02:05:22 +04:00
|
|
|
if (!_passed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var truePassedChecks = _passedChecks - _falsePassedChecks;
|
2009-11-12 10:01:04 +03:00
|
|
|
if (truePassedChecks > 0) {
|
|
|
|
_dump("TEST-PASS | (xpcshell/head.js) | " + truePassedChecks + " (+ " +
|
2009-06-05 02:05:22 +04:00
|
|
|
_falsePassedChecks + ") check(s) passed\n");
|
2009-11-12 10:01:04 +03:00
|
|
|
} else {
|
2009-06-05 02:05:22 +04:00
|
|
|
// ToDo: switch to TEST-UNEXPECTED-FAIL when all tests have been updated. (Bug 496443)
|
2009-11-12 10:01:04 +03:00
|
|
|
_dump("TEST-INFO | (xpcshell/head.js) | No (+ " + _falsePassedChecks + ") checks actually run\n");
|
|
|
|
}
|
2009-03-17 17:22:55 +03:00
|
|
|
}
|
|
|
|
|
2009-06-13 17:11:00 +04:00
|
|
|
/**
|
|
|
|
* Loads files.
|
|
|
|
*
|
|
|
|
* @param aFiles Array of files to load.
|
|
|
|
*/
|
|
|
|
function _load_files(aFiles) {
|
|
|
|
function loadTailFile(element, index, array) {
|
|
|
|
load(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
aFiles.forEach(loadTailFile);
|
|
|
|
}
|
|
|
|
|
2009-03-17 17:22:55 +03:00
|
|
|
|
2006-09-12 10:19:26 +04:00
|
|
|
/************** Functions to be used from the tests **************/
|
|
|
|
|
2010-08-14 00:28:14 +04:00
|
|
|
/**
|
|
|
|
* Calls the given function at least the specified number of milliseconds later.
|
|
|
|
* The callback will not undershoot the given time, but it might overshoot --
|
|
|
|
* don't expect precision!
|
|
|
|
*
|
|
|
|
* @param delay : uint
|
|
|
|
* the number of milliseconds to delay
|
|
|
|
* @param callback : function() : void
|
|
|
|
* the function to call
|
|
|
|
*/
|
2009-12-28 20:31:00 +03:00
|
|
|
function do_timeout(delay, func) {
|
2010-08-14 00:28:14 +04:00
|
|
|
new _Timer(func, Number(delay));
|
2006-09-12 10:19:26 +04:00
|
|
|
}
|
|
|
|
|
2009-08-06 18:25:16 +04:00
|
|
|
function do_execute_soon(callback) {
|
2010-03-08 21:26:54 +03:00
|
|
|
do_test_pending();
|
2009-08-06 18:25:16 +04:00
|
|
|
var tm = Components.classes["@mozilla.org/thread-manager;1"]
|
|
|
|
.getService(Components.interfaces.nsIThreadManager);
|
|
|
|
|
|
|
|
tm.mainThread.dispatch({
|
|
|
|
run: function() {
|
2010-03-08 21:26:54 +03:00
|
|
|
try {
|
|
|
|
callback();
|
|
|
|
} catch (e) {
|
|
|
|
// do_check failures are already logged and set _quit to true and throw
|
|
|
|
// NS_ERROR_ABORT. If both of those are true it is likely this exception
|
|
|
|
// has already been logged so there is no need to log it again. It's
|
|
|
|
// possible that this will mask an NS_ERROR_ABORT that happens after a
|
|
|
|
// do_check failure though.
|
|
|
|
if (!_quit || e != Components.results.NS_ERROR_ABORT) {
|
|
|
|
dump("TEST-UNEXPECTED-FAIL | (xpcshell/head.js) | " + e);
|
|
|
|
if (e.stack) {
|
|
|
|
dump(" - See following stack:\n");
|
|
|
|
_dump_exception_stack(e.stack);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dump("\n");
|
|
|
|
}
|
|
|
|
_do_quit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
do_test_finished();
|
|
|
|
}
|
2009-08-06 18:25:16 +04:00
|
|
|
}
|
|
|
|
}, Components.interfaces.nsIThread.DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
2008-07-16 09:45:42 +04:00
|
|
|
function do_throw(text, stack) {
|
|
|
|
if (!stack)
|
|
|
|
stack = Components.stack.caller;
|
2009-06-05 02:05:22 +04:00
|
|
|
|
|
|
|
_passed = false;
|
2009-11-12 10:01:04 +03:00
|
|
|
_dump("TEST-UNEXPECTED-FAIL | " + stack.filename + " | " + text +
|
2009-06-05 02:05:22 +04:00
|
|
|
" - See following stack:\n");
|
2005-03-25 06:41:33 +03:00
|
|
|
var frame = Components.stack;
|
|
|
|
while (frame != null) {
|
2009-11-12 10:01:04 +03:00
|
|
|
_dump(frame + "\n");
|
2005-03-25 06:41:33 +03:00
|
|
|
frame = frame.caller;
|
|
|
|
}
|
2009-06-05 02:05:22 +04:00
|
|
|
|
|
|
|
_do_quit();
|
2005-03-25 06:41:33 +03:00
|
|
|
throw Components.results.NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2008-07-16 09:45:42 +04:00
|
|
|
function do_check_neq(left, right, stack) {
|
|
|
|
if (!stack)
|
|
|
|
stack = Components.stack.caller;
|
2009-06-05 02:05:22 +04:00
|
|
|
|
|
|
|
var text = left + " != " + right;
|
2009-11-12 10:01:04 +03:00
|
|
|
if (left == right) {
|
2009-06-05 02:05:22 +04:00
|
|
|
do_throw(text, stack);
|
2009-11-12 10:01:04 +03:00
|
|
|
} else {
|
2009-06-05 02:05:22 +04:00
|
|
|
++_passedChecks;
|
2009-11-12 10:01:04 +03:00
|
|
|
_dump("TEST-PASS | " + stack.filename + " | [" + stack.name + " : " +
|
2009-06-05 02:05:22 +04:00
|
|
|
stack.lineNumber + "] " + text + "\n");
|
|
|
|
}
|
2005-03-25 06:41:33 +03:00
|
|
|
}
|
|
|
|
|
2008-07-16 09:45:42 +04:00
|
|
|
function do_check_eq(left, right, stack) {
|
|
|
|
if (!stack)
|
|
|
|
stack = Components.stack.caller;
|
2009-06-05 02:05:22 +04:00
|
|
|
|
|
|
|
var text = left + " == " + right;
|
2009-11-12 10:01:04 +03:00
|
|
|
if (left != right) {
|
2009-06-05 02:05:22 +04:00
|
|
|
do_throw(text, stack);
|
2009-11-12 10:01:04 +03:00
|
|
|
} else {
|
2009-06-05 02:05:22 +04:00
|
|
|
++_passedChecks;
|
2009-11-12 10:01:04 +03:00
|
|
|
_dump("TEST-PASS | " + stack.filename + " | [" + stack.name + " : " +
|
2009-06-05 02:05:22 +04:00
|
|
|
stack.lineNumber + "] " + text + "\n");
|
|
|
|
}
|
2005-03-25 06:41:33 +03:00
|
|
|
}
|
|
|
|
|
2008-07-16 09:45:42 +04:00
|
|
|
function do_check_true(condition, stack) {
|
|
|
|
if (!stack)
|
|
|
|
stack = Components.stack.caller;
|
2009-06-05 02:05:22 +04:00
|
|
|
|
2008-07-16 09:45:42 +04:00
|
|
|
do_check_eq(condition, true, stack);
|
2006-07-17 00:47:13 +04:00
|
|
|
}
|
|
|
|
|
2008-07-16 09:45:42 +04:00
|
|
|
function do_check_false(condition, stack) {
|
|
|
|
if (!stack)
|
|
|
|
stack = Components.stack.caller;
|
2009-06-05 02:05:22 +04:00
|
|
|
|
2008-07-16 09:45:42 +04:00
|
|
|
do_check_eq(condition, false, stack);
|
2006-12-15 05:13:54 +03:00
|
|
|
}
|
|
|
|
|
2005-03-25 06:41:33 +03:00
|
|
|
function do_test_pending() {
|
2009-06-05 02:05:22 +04:00
|
|
|
++_tests_pending;
|
|
|
|
|
2009-11-12 10:01:04 +03:00
|
|
|
_dump("TEST-INFO | (xpcshell/head.js) | test " + _tests_pending +
|
2009-06-05 02:05:22 +04:00
|
|
|
" pending\n");
|
2005-03-25 06:41:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function do_test_finished() {
|
2009-11-12 10:01:04 +03:00
|
|
|
_dump("TEST-INFO | (xpcshell/head.js) | test " + _tests_pending +
|
2009-06-05 02:05:22 +04:00
|
|
|
" finished\n");
|
|
|
|
|
2005-03-25 06:41:33 +03:00
|
|
|
if (--_tests_pending == 0)
|
2006-09-12 10:19:26 +04:00
|
|
|
_do_quit();
|
2005-03-25 06:41:33 +03:00
|
|
|
}
|
2006-12-15 05:13:54 +03:00
|
|
|
|
2009-03-21 18:20:00 +03:00
|
|
|
function do_get_file(path, allowNonexistent) {
|
2007-01-31 10:22:12 +03:00
|
|
|
try {
|
2009-03-21 18:20:00 +03:00
|
|
|
let lf = Components.classes["@mozilla.org/file/directory_service;1"]
|
|
|
|
.getService(Components.interfaces.nsIProperties)
|
|
|
|
.get("CurWorkD", Components.interfaces.nsILocalFile);
|
|
|
|
|
|
|
|
let bits = path.split("/");
|
|
|
|
for (let i = 0; i < bits.length; i++) {
|
|
|
|
if (bits[i]) {
|
|
|
|
if (bits[i] == "..")
|
|
|
|
lf = lf.parent;
|
|
|
|
else
|
|
|
|
lf.append(bits[i]);
|
|
|
|
}
|
2008-07-24 19:33:41 +04:00
|
|
|
}
|
2007-02-03 04:46:09 +03:00
|
|
|
|
2009-06-05 02:05:22 +04:00
|
|
|
if (!allowNonexistent && !lf.exists()) {
|
|
|
|
// Not using do_throw(): caller will continue.
|
|
|
|
_passed = false;
|
|
|
|
var stack = Components.stack.caller;
|
2009-11-12 10:01:04 +03:00
|
|
|
_dump("TEST-UNEXPECTED-FAIL | " + stack.filename + " | [" +
|
2009-06-05 02:05:22 +04:00
|
|
|
stack.name + " : " + stack.lineNumber + "] " + lf.path +
|
|
|
|
" does not exist\n");
|
2007-06-08 02:34:06 +04:00
|
|
|
}
|
2009-03-21 18:20:00 +03:00
|
|
|
|
|
|
|
return lf;
|
2007-06-08 02:34:06 +04:00
|
|
|
}
|
2009-06-05 02:05:22 +04:00
|
|
|
catch (ex) {
|
2009-03-21 18:20:00 +03:00
|
|
|
do_throw(ex.toString(), Components.stack.caller);
|
|
|
|
}
|
2009-06-05 02:05:22 +04:00
|
|
|
|
2009-03-21 18:20:00 +03:00
|
|
|
return null;
|
|
|
|
}
|
2007-02-03 04:46:09 +03:00
|
|
|
|
2009-03-21 18:20:00 +03:00
|
|
|
// do_get_cwd() isn't exactly self-explanatory, so provide a helper
|
|
|
|
function do_get_cwd() {
|
|
|
|
return do_get_file("");
|
2007-01-31 06:25:46 +03:00
|
|
|
}
|
2007-05-09 14:48:21 +04:00
|
|
|
|
2009-06-13 17:11:00 +04:00
|
|
|
/**
|
|
|
|
* Loads _HTTPD_JS_PATH file, which is dynamically defined by
|
|
|
|
* <runxpcshelltests.py>.
|
|
|
|
*/
|
|
|
|
function do_load_httpd_js() {
|
|
|
|
load(_HTTPD_JS_PATH);
|
|
|
|
}
|
|
|
|
|
2010-06-25 18:43:25 +04:00
|
|
|
function do_load_manifest(path) {
|
2007-05-09 14:48:21 +04:00
|
|
|
var lf = do_get_file(path);
|
|
|
|
const nsIComponentRegistrar = Components.interfaces.nsIComponentRegistrar;
|
|
|
|
do_check_true(Components.manager instanceof nsIComponentRegistrar);
|
2009-06-05 02:05:22 +04:00
|
|
|
// Previous do_check_true() is not a test check.
|
|
|
|
++_falsePassedChecks;
|
2007-05-09 14:48:21 +04:00
|
|
|
Components.manager.autoRegister(lf);
|
|
|
|
}
|
2007-08-04 03:00:39 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a DOM document.
|
|
|
|
*
|
|
|
|
* @param aPath File path to the document.
|
|
|
|
* @param aType Content type to use in DOMParser.
|
|
|
|
*
|
|
|
|
* @return nsIDOMDocument from the file.
|
|
|
|
*/
|
|
|
|
function do_parse_document(aPath, aType) {
|
|
|
|
switch (aType) {
|
|
|
|
case "application/xhtml+xml":
|
|
|
|
case "application/xml":
|
|
|
|
case "text/xml":
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2009-06-05 02:05:22 +04:00
|
|
|
do_throw("type: expected application/xhtml+xml, application/xml or text/xml," +
|
|
|
|
" got '" + aType + "'",
|
|
|
|
Components.stack.caller);
|
2007-08-04 03:00:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
var lf = do_get_file(aPath);
|
|
|
|
const C_i = Components.interfaces;
|
|
|
|
const parserClass = "@mozilla.org/xmlextras/domparser;1";
|
|
|
|
const streamClass = "@mozilla.org/network/file-input-stream;1";
|
|
|
|
var stream = Components.classes[streamClass]
|
|
|
|
.createInstance(C_i.nsIFileInputStream);
|
|
|
|
stream.init(lf, -1, -1, C_i.nsIFileInputStream.CLOSE_ON_EOF);
|
|
|
|
var parser = Components.classes[parserClass]
|
|
|
|
.createInstance(C_i.nsIDOMParser);
|
|
|
|
var doc = parser.parseFromStream(stream, null, lf.fileSize, aType);
|
|
|
|
parser = null;
|
|
|
|
stream = null;
|
|
|
|
lf = null;
|
|
|
|
return doc;
|
|
|
|
}
|
2009-07-11 03:42:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a function that will run when the test harness is done running all
|
|
|
|
* tests.
|
|
|
|
*
|
|
|
|
* @param aFunction
|
|
|
|
* The function to be called when the test harness has finished running.
|
|
|
|
*/
|
|
|
|
function do_register_cleanup(aFunction)
|
|
|
|
{
|
|
|
|
_cleanupFunctions.push(aFunction);
|
|
|
|
}
|
2009-05-14 15:37:40 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a directory with the profile service,
|
|
|
|
* and return the directory as an nsILocalFile.
|
|
|
|
*
|
|
|
|
* @return nsILocalFile of the profile directory.
|
|
|
|
*/
|
|
|
|
function do_get_profile() {
|
2010-04-02 02:42:22 +04:00
|
|
|
// Since we have a profile, we will notify profile shutdown topics at
|
|
|
|
// the end of the current test, to ensure correct cleanup on shutdown.
|
|
|
|
do_register_cleanup(function() {
|
|
|
|
let obsSvc = Components.classes["@mozilla.org/observer-service;1"].
|
|
|
|
getService(Components.interfaces.nsIObserverService);
|
|
|
|
obsSvc.notifyObservers(null, "profile-change-net-teardown", null);
|
|
|
|
obsSvc.notifyObservers(null, "profile-change-teardown", null);
|
|
|
|
obsSvc.notifyObservers(null, "profile-before-change", null);
|
|
|
|
});
|
|
|
|
|
2009-05-14 15:37:40 +04:00
|
|
|
let env = Components.classes["@mozilla.org/process/environment;1"]
|
|
|
|
.getService(Components.interfaces.nsIEnvironment);
|
|
|
|
// the python harness sets this in the environment for us
|
|
|
|
let profd = env.get("XPCSHELL_TEST_PROFILE_DIR");
|
|
|
|
let file = Components.classes["@mozilla.org/file/local;1"]
|
|
|
|
.createInstance(Components.interfaces.nsILocalFile);
|
|
|
|
file.initWithPath(profd);
|
|
|
|
|
|
|
|
let dirSvc = Components.classes["@mozilla.org/file/directory_service;1"]
|
|
|
|
.getService(Components.interfaces.nsIProperties);
|
|
|
|
let provider = {
|
|
|
|
getFile: function(prop, persistent) {
|
|
|
|
persistent.value = true;
|
2010-09-16 02:55:08 +04:00
|
|
|
if (prop == "ProfD" || prop == "ProfLD" || prop == "ProfDS" ||
|
|
|
|
prop == "ProfLDS" || prop == "TmpD") {
|
2009-05-14 15:37:40 +04:00
|
|
|
return file.clone();
|
|
|
|
}
|
|
|
|
throw Components.results.NS_ERROR_FAILURE;
|
|
|
|
},
|
|
|
|
QueryInterface: function(iid) {
|
2010-04-01 15:29:13 +04:00
|
|
|
if (iid.equals(Components.interfaces.nsIDirectoryServiceProvider) ||
|
2009-05-14 15:37:40 +04:00
|
|
|
iid.equals(Components.interfaces.nsISupports)) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
throw Components.results.NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
dirSvc.QueryInterface(Components.interfaces.nsIDirectoryService)
|
|
|
|
.registerProvider(provider);
|
|
|
|
return file.clone();
|
|
|
|
}
|
2009-10-17 11:05:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function loads head.js (this file) in the child process, so that all
|
|
|
|
* functions defined in this file (do_throw, etc) are available to subsequent
|
|
|
|
* sendCommand calls. It also sets various constants used by these functions.
|
|
|
|
*
|
|
|
|
* (Note that you may use sendCommand without calling this function first; you
|
|
|
|
* simply won't have any of the functions in this file available.)
|
|
|
|
*/
|
|
|
|
function do_load_child_test_harness()
|
|
|
|
{
|
|
|
|
// Make sure this isn't called from child process
|
|
|
|
var runtime = Components.classes["@mozilla.org/xre/app-info;1"]
|
|
|
|
.getService(Components.interfaces.nsIXULRuntime);
|
|
|
|
if (runtime.processType !=
|
|
|
|
Components.interfaces.nsIXULRuntime.PROCESS_TYPE_DEFAULT)
|
|
|
|
{
|
|
|
|
do_throw("run_test_in_child cannot be called from child!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow to be called multiple times, but only run once
|
|
|
|
if (typeof do_load_child_test_harness.alreadyRun != "undefined")
|
|
|
|
return;
|
|
|
|
do_load_child_test_harness.alreadyRun = 1;
|
|
|
|
|
|
|
|
function addQuotes (str) {
|
|
|
|
return '"' + str + '"';
|
|
|
|
}
|
|
|
|
var quoted_head_files = _HEAD_FILES.map(addQuotes);
|
|
|
|
var quoted_tail_files = _TAIL_FILES.map(addQuotes);
|
|
|
|
|
2009-11-12 10:01:04 +03:00
|
|
|
_XPCSHELL_PROCESS = "parent";
|
|
|
|
|
2009-10-17 11:05:27 +04:00
|
|
|
sendCommand(
|
|
|
|
"const _HEAD_JS_PATH='" + _HEAD_JS_PATH + "'; "
|
|
|
|
+ "const _HTTPD_JS_PATH='" + _HTTPD_JS_PATH + "'; "
|
|
|
|
+ "const _HEAD_FILES=[" + quoted_head_files.join() + "];"
|
|
|
|
+ "const _TAIL_FILES=[" + quoted_tail_files.join() + "];"
|
2009-11-12 10:01:04 +03:00
|
|
|
+ "const _XPCSHELL_PROCESS='child';"
|
2009-10-17 11:05:27 +04:00
|
|
|
+ "load(_HEAD_JS_PATH);");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs an entire xpcshell unit test in a child process (rather than in chrome,
|
|
|
|
* which is the default).
|
|
|
|
*
|
|
|
|
* This function returns immediately, before the test has completed.
|
|
|
|
*
|
|
|
|
* @param testFile
|
|
|
|
* The name of the script to run. Path format same as load().
|
|
|
|
* @param optionalCallback.
|
|
|
|
* Optional function to be called (in parent) when test on child is
|
|
|
|
* complete. If provided, the function must call do_test_finished();
|
|
|
|
*/
|
|
|
|
function run_test_in_child(testFile, optionalCallback)
|
|
|
|
{
|
|
|
|
var callback = (typeof optionalCallback == 'undefined') ?
|
|
|
|
do_test_finished : optionalCallback;
|
|
|
|
|
|
|
|
do_load_child_test_harness();
|
|
|
|
|
|
|
|
var testPath = do_get_file(testFile).path.replace(/\\/g, "/");
|
|
|
|
do_test_pending();
|
|
|
|
sendCommand("const _TEST_FILE=['" + testPath + "']; _execute_test();",
|
|
|
|
callback);
|
|
|
|
}
|
|
|
|
|