Bug 602871 - XPCShell test harness should ignore system idle time while running tests, r=ted a=tests-only

This commit is contained in:
Marco Bonardo 2010-10-19 11:18:40 +02:00
Родитель 31388ae51b
Коммит 08adb4f504
2 изменённых файлов: 126 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,23 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
function run_test() {
print("Init the fake idle service and check its identity.");
let fakeIdleService = Components.classes["@mozilla.org/widget/idleservice;1"].
getService(Components.interfaces.nsIIdleService);
try {
fakeIdleService.QueryInterface(Components.interfaces.nsIFactory);
} catch (ex) {
do_throw("The fake idle service implements nsIFactory.");
}
// We need at least one PASS, thus sanity check the idle time.
do_check_eq(fakeIdleService.idleTime, 0);
print("Init the real idle service and check its identity.");
let realIdleService = do_get_idle();
try {
realIdleService.QueryInterface(Components.interfaces.nsIFactory);
do_throw("The real idle service does not implement nsIFactory.");
} catch (ex) {}
}

Просмотреть файл

@ -182,6 +182,102 @@ function _dump_exception_stack(stack) {
});
}
/**
* Overrides idleService with a mock. Idle is commonly used for maintenance
* tasks, thus if a test uses a service that requires the idle service, it will
* start handling them.
* This behaviour would cause random failures and slowdown tests execution,
* for example by running database vacuum or cleanups for each test.
*
* @note Idle service is overridden by default. If a test requires it, it will
* have to call do_get_idle() function at least once before use.
*/
_fakeIdleService = {
get registrar() {
delete this.registrar;
return this.registrar =
Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar);
},
contractID: "@mozilla.org/widget/idleservice;1",
get CID() this.registrar.contractIDToCID(this.contractID),
activate: function FIS_activate()
{
if (!this.originalFactory) {
// Save original factory.
this.originalFactory =
Components.manager.getClassObject(Components.classes[this.contractID],
Components.interfaces.nsIFactory);
// Unregister original factory.
this.registrar.unregisterFactory(this.CID, this.originalFactory);
// Replace with the mock.
this.registrar.registerFactory(this.CID, "Fake Idle Service",
this.contractID, this.factory
);
}
},
deactivate: function FIS_deactivate()
{
if (this.originalFactory) {
// Unregister the mock.
this.registrar.unregisterFactory(this.CID, this.factory);
// Restore original factory.
this.registrar.registerFactory(this.CID, "Idle Service",
this.contractID, this.originalFactory);
delete this.originalFactory;
}
},
factory: {
// nsIFactory
createInstance: function (aOuter, aIID)
{
if (aOuter) {
throw Components.results.NS_ERROR_NO_AGGREGATION;
}
return _fakeIdleService.QueryInterface(aIID);
},
lockFactory: function (aLock) {
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
},
QueryInterface: function(aIID) {
if (aIID.equals(Components.interfaces.nsIFactory) ||
aIID.equals(Components.interfaces.nsISupports)) {
return this;
}
throw Components.results.NS_ERROR_NO_INTERFACE;
}
},
// nsIIdleService
get idleTime() 0,
addIdleObserver: function () {},
removeIdleObserver: function () {},
QueryInterface: function(aIID) {
// Useful for testing purposes, see test_get_idle.js.
if (aIID.equals(Components.interfaces.nsIFactory)) {
return this.factory;
}
if (aIID.equals(Components.interfaces.nsIIdleService) ||
aIID.equals(Components.interfaces.nsISupports)) {
return this;
}
throw Components.results.NS_ERROR_NO_INTERFACE;
}
}
/**
* Restores the idle service factory if needed and returns the service's handle.
* @return A handle to the idle service.
*/
function do_get_idle() {
_fakeIdleService.deactivate();
return Components.classes[_fakeIdleService.contractID]
.getService(Components.interfaces.nsIIdleService);
}
function _execute_test() {
// Map resource://test/ to the current working directory.
let (ios = Components.classes["@mozilla.org/network/io-service;1"]
@ -193,6 +289,10 @@ function _execute_test() {
protocolHandler.setSubstitution("test", curDirURI);
}
// Override idle service by default.
// Call do_get_idle() to restore the factory and get the service.
_fakeIdleService.activate();
// _HEAD_FILES is dynamically defined by <runxpcshelltests.py>.
_load_files(_HEAD_FILES);
// _TEST_FILE is dynamically defined by <runxpcshelltests.py>.
@ -230,6 +330,9 @@ function _execute_test() {
while ((func = _cleanupFunctions.pop()))
func();
// Restore idle service to avoid leaks.
_fakeIdleService.deactivate();
if (!_passed)
return;