michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: Cu.import("resource://services-sync/stages/declined.js"); michael@0: Cu.import("resource://services-sync/stages/enginesync.js"); michael@0: Cu.import("resource://services-sync/engines.js"); michael@0: Cu.import("resource://services-sync/service.js"); michael@0: Cu.import("resource://services-common/observers.js"); michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: } michael@0: michael@0: function PetrolEngine() {} michael@0: PetrolEngine.prototype.name = "petrol"; michael@0: michael@0: function DieselEngine() {} michael@0: DieselEngine.prototype.name = "diesel"; michael@0: michael@0: function DummyEngine() {} michael@0: DummyEngine.prototype.name = "dummy"; michael@0: michael@0: function ActualEngine() {} michael@0: ActualEngine.prototype = {__proto__: Engine.prototype, michael@0: name: 'actual'}; michael@0: michael@0: function getEngineManager() { michael@0: let manager = new EngineManager(Service); michael@0: Service.engineManager = manager; michael@0: manager._engines = { michael@0: "petrol": new PetrolEngine(), michael@0: "diesel": new DieselEngine(), michael@0: "dummy": new DummyEngine(), michael@0: "actual": new ActualEngine(), michael@0: }; michael@0: return manager; michael@0: } michael@0: michael@0: /** michael@0: * 'Fetch' a meta/global record that doesn't mention declined. michael@0: * michael@0: * Push it into the EngineSynchronizer to set enabled; verify that those are michael@0: * correct. michael@0: * michael@0: * Then push it into DeclinedEngines to set declined; verify that none are michael@0: * declined, and a notification is sent for our locally disabled-but-not- michael@0: * declined engines. michael@0: */ michael@0: add_test(function testOldMeta() { michael@0: let meta = { michael@0: payload: { michael@0: engines: { michael@0: "petrol": 1, michael@0: "diesel": 2, michael@0: "nonlocal": 3, // Enabled but not supported. michael@0: }, michael@0: }, michael@0: }; michael@0: michael@0: _("Record: " + JSON.stringify(meta)); michael@0: michael@0: let manager = getEngineManager(); michael@0: michael@0: // Update enabled from meta/global. michael@0: let engineSync = new EngineSynchronizer(Service); michael@0: engineSync._updateEnabledFromMeta(meta, 3, manager); michael@0: michael@0: Assert.ok(manager._engines["petrol"].enabled, "'petrol' locally enabled."); michael@0: Assert.ok(manager._engines["diesel"].enabled, "'diesel' locally enabled."); michael@0: Assert.ok(!("nonlocal" in manager._engines), "We don't know anything about the 'nonlocal' engine."); michael@0: Assert.ok(!manager._engines["actual"].enabled, "'actual' not locally enabled."); michael@0: Assert.ok(!manager.isDeclined("actual"), "'actual' not declined, though."); michael@0: michael@0: let declinedEngines = new DeclinedEngines(Service); michael@0: michael@0: function onNotDeclined(subject, topic, data) { michael@0: Observers.remove("weave:engines:notdeclined", onNotDeclined); michael@0: Assert.ok(subject.undecided.has("actual"), "EngineManager observed that 'actual' was undecided."); michael@0: michael@0: let declined = manager.getDeclined(); michael@0: _("Declined: " + JSON.stringify(declined)); michael@0: michael@0: Assert.ok(!meta.changed, "No need to upload a new meta/global."); michael@0: run_next_test(); michael@0: } michael@0: michael@0: Observers.add("weave:engines:notdeclined", onNotDeclined); michael@0: michael@0: declinedEngines.updateDeclined(meta, manager); michael@0: }); michael@0: michael@0: /** michael@0: * 'Fetch' a meta/global that declines an engine we don't michael@0: * recognize. Ensure that we track that declined engine along michael@0: * with any we locally declined, and that the meta/global michael@0: * record is marked as changed and includes all declined michael@0: * engines. michael@0: */ michael@0: add_test(function testDeclinedMeta() { michael@0: let meta = { michael@0: payload: { michael@0: engines: { michael@0: "petrol": 1, michael@0: "diesel": 2, michael@0: "nonlocal": 3, // Enabled but not supported. michael@0: }, michael@0: declined: ["nonexistent"], // Declined and not supported. michael@0: }, michael@0: }; michael@0: michael@0: _("Record: " + JSON.stringify(meta)); michael@0: michael@0: let manager = getEngineManager(); michael@0: manager._engines["petrol"].enabled = true; michael@0: manager._engines["diesel"].enabled = true; michael@0: manager._engines["dummy"].enabled = true; michael@0: manager._engines["actual"].enabled = false; // Disabled but not declined. michael@0: michael@0: manager.decline(["localdecline"]); // Declined and not supported. michael@0: michael@0: let declinedEngines = new DeclinedEngines(Service); michael@0: michael@0: function onNotDeclined(subject, topic, data) { michael@0: Observers.remove("weave:engines:notdeclined", onNotDeclined); michael@0: Assert.ok(subject.undecided.has("actual"), "EngineManager observed that 'actual' was undecided."); michael@0: michael@0: let declined = manager.getDeclined(); michael@0: _("Declined: " + JSON.stringify(declined)); michael@0: michael@0: Assert.equal(declined.indexOf("actual"), -1, "'actual' is locally disabled, but not marked as declined."); michael@0: michael@0: Assert.equal(declined.indexOf("clients"), -1, "'clients' is enabled and not remotely declined."); michael@0: Assert.equal(declined.indexOf("petrol"), -1, "'petrol' is enabled and not remotely declined."); michael@0: Assert.equal(declined.indexOf("diesel"), -1, "'diesel' is enabled and not remotely declined."); michael@0: Assert.equal(declined.indexOf("dummy"), -1, "'dummy' is enabled and not remotely declined."); michael@0: michael@0: Assert.ok(0 <= declined.indexOf("nonexistent"), "'nonexistent' was declined on the server."); michael@0: michael@0: Assert.ok(0 <= declined.indexOf("localdecline"), "'localdecline' was declined locally."); michael@0: michael@0: // The meta/global is modified, too. michael@0: Assert.ok(0 <= meta.payload.declined.indexOf("nonexistent"), "meta/global's declined contains 'nonexistent'."); michael@0: Assert.ok(0 <= meta.payload.declined.indexOf("localdecline"), "meta/global's declined contains 'localdecline'."); michael@0: Assert.strictEqual(true, meta.changed, "meta/global was changed."); michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: Observers.add("weave:engines:notdeclined", onNotDeclined); michael@0: michael@0: declinedEngines.updateDeclined(meta, manager); michael@0: }); michael@0: