2012-08-29 14:43:41 -07:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
|
|
|
http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
|
2010-06-16 14:30:08 -07:00
|
|
|
Cu.import("resource://services-sync/engines.js");
|
2012-08-29 14:43:41 -07:00
|
|
|
Cu.import("resource://services-sync/service.js");
|
2010-06-01 15:06:16 -07:00
|
|
|
|
|
|
|
function run_test() {
|
2014-03-13 16:37:25 -07:00
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
|
|
|
function PetrolEngine() {}
|
|
|
|
PetrolEngine.prototype.name = "petrol";
|
|
|
|
|
|
|
|
function DieselEngine() {}
|
|
|
|
DieselEngine.prototype.name = "diesel";
|
|
|
|
|
|
|
|
function DummyEngine() {}
|
|
|
|
DummyEngine.prototype.name = "dummy";
|
|
|
|
|
|
|
|
function ActualEngine() {}
|
|
|
|
ActualEngine.prototype = {__proto__: Engine.prototype,
|
|
|
|
name: 'actual'};
|
|
|
|
|
|
|
|
add_test(function test_basics() {
|
2010-06-01 15:06:16 -07:00
|
|
|
_("We start out with a clean slate");
|
2012-08-29 14:43:41 -07:00
|
|
|
|
|
|
|
let manager = new EngineManager(Service);
|
|
|
|
|
|
|
|
let engines = manager.getAll();
|
2010-06-01 15:06:16 -07:00
|
|
|
do_check_eq(engines.length, 0);
|
2012-08-29 14:43:41 -07:00
|
|
|
do_check_eq(manager.get('dummy'), undefined);
|
2010-06-01 15:06:16 -07:00
|
|
|
|
|
|
|
_("Register an engine");
|
2012-08-29 14:43:41 -07:00
|
|
|
manager.register(DummyEngine);
|
|
|
|
let dummy = manager.get('dummy');
|
2010-06-01 15:06:16 -07:00
|
|
|
do_check_true(dummy instanceof DummyEngine);
|
|
|
|
|
2012-08-29 14:43:41 -07:00
|
|
|
engines = manager.getAll();
|
2010-06-01 15:06:16 -07:00
|
|
|
do_check_eq(engines.length, 1);
|
|
|
|
do_check_eq(engines[0], dummy);
|
|
|
|
|
|
|
|
_("Register an already registered engine is ignored");
|
2012-08-29 14:43:41 -07:00
|
|
|
manager.register(DummyEngine);
|
|
|
|
do_check_eq(manager.get('dummy'), dummy);
|
2010-06-01 15:06:16 -07:00
|
|
|
|
|
|
|
_("Register multiple engines in one go");
|
2012-08-29 14:43:41 -07:00
|
|
|
manager.register([PetrolEngine, DieselEngine]);
|
|
|
|
let petrol = manager.get('petrol');
|
|
|
|
let diesel = manager.get('diesel');
|
2010-06-01 15:06:16 -07:00
|
|
|
do_check_true(petrol instanceof PetrolEngine);
|
|
|
|
do_check_true(diesel instanceof DieselEngine);
|
|
|
|
|
2012-08-29 14:43:41 -07:00
|
|
|
engines = manager.getAll();
|
2010-06-01 15:06:16 -07:00
|
|
|
do_check_eq(engines.length, 3);
|
|
|
|
do_check_neq(engines.indexOf(petrol), -1);
|
|
|
|
do_check_neq(engines.indexOf(diesel), -1);
|
|
|
|
|
|
|
|
_("Retrieve multiple engines in one go");
|
2012-08-29 14:43:41 -07:00
|
|
|
engines = manager.get(["dummy", "diesel"]);
|
2010-06-01 15:06:16 -07:00
|
|
|
do_check_eq(engines.length, 2);
|
|
|
|
do_check_neq(engines.indexOf(dummy), -1);
|
|
|
|
do_check_neq(engines.indexOf(diesel), -1);
|
|
|
|
|
|
|
|
_("getEnabled() only returns enabled engines");
|
2012-08-29 14:43:41 -07:00
|
|
|
engines = manager.getEnabled();
|
2010-06-01 15:06:16 -07:00
|
|
|
do_check_eq(engines.length, 0);
|
|
|
|
|
|
|
|
petrol.enabled = true;
|
2012-08-29 14:43:41 -07:00
|
|
|
engines = manager.getEnabled();
|
2010-06-01 15:06:16 -07:00
|
|
|
do_check_eq(engines.length, 1);
|
|
|
|
do_check_eq(engines[0], petrol);
|
|
|
|
|
|
|
|
dummy.enabled = true;
|
|
|
|
diesel.enabled = true;
|
2012-08-29 14:43:41 -07:00
|
|
|
engines = manager.getEnabled();
|
2010-06-01 15:06:16 -07:00
|
|
|
do_check_eq(engines.length, 3);
|
|
|
|
|
|
|
|
_("Unregister an engine by name");
|
2012-08-29 14:43:41 -07:00
|
|
|
manager.unregister('dummy');
|
|
|
|
do_check_eq(manager.get('dummy'), undefined);
|
|
|
|
engines = manager.getAll();
|
2010-06-01 15:06:16 -07:00
|
|
|
do_check_eq(engines.length, 2);
|
|
|
|
do_check_eq(engines.indexOf(dummy), -1);
|
|
|
|
|
|
|
|
_("Unregister an engine by value");
|
2012-08-29 14:43:41 -07:00
|
|
|
// manager.unregister() checks for instanceof Engine, so let's make one:
|
|
|
|
manager.register(ActualEngine);
|
|
|
|
let actual = manager.get('actual');
|
2010-06-01 15:06:16 -07:00
|
|
|
do_check_true(actual instanceof ActualEngine);
|
|
|
|
do_check_true(actual instanceof Engine);
|
|
|
|
|
2012-08-29 14:43:41 -07:00
|
|
|
manager.unregister(actual);
|
|
|
|
do_check_eq(manager.get('actual'), undefined);
|
2014-03-13 16:37:25 -07:00
|
|
|
|
|
|
|
run_next_test();
|
|
|
|
});
|
|
|
|
|