services/sync/tests/unit/test_declined.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 Cu.import("resource://services-sync/stages/declined.js");
michael@0 5 Cu.import("resource://services-sync/stages/enginesync.js");
michael@0 6 Cu.import("resource://services-sync/engines.js");
michael@0 7 Cu.import("resource://services-sync/service.js");
michael@0 8 Cu.import("resource://services-common/observers.js");
michael@0 9
michael@0 10 function run_test() {
michael@0 11 run_next_test();
michael@0 12 }
michael@0 13
michael@0 14 function PetrolEngine() {}
michael@0 15 PetrolEngine.prototype.name = "petrol";
michael@0 16
michael@0 17 function DieselEngine() {}
michael@0 18 DieselEngine.prototype.name = "diesel";
michael@0 19
michael@0 20 function DummyEngine() {}
michael@0 21 DummyEngine.prototype.name = "dummy";
michael@0 22
michael@0 23 function ActualEngine() {}
michael@0 24 ActualEngine.prototype = {__proto__: Engine.prototype,
michael@0 25 name: 'actual'};
michael@0 26
michael@0 27 function getEngineManager() {
michael@0 28 let manager = new EngineManager(Service);
michael@0 29 Service.engineManager = manager;
michael@0 30 manager._engines = {
michael@0 31 "petrol": new PetrolEngine(),
michael@0 32 "diesel": new DieselEngine(),
michael@0 33 "dummy": new DummyEngine(),
michael@0 34 "actual": new ActualEngine(),
michael@0 35 };
michael@0 36 return manager;
michael@0 37 }
michael@0 38
michael@0 39 /**
michael@0 40 * 'Fetch' a meta/global record that doesn't mention declined.
michael@0 41 *
michael@0 42 * Push it into the EngineSynchronizer to set enabled; verify that those are
michael@0 43 * correct.
michael@0 44 *
michael@0 45 * Then push it into DeclinedEngines to set declined; verify that none are
michael@0 46 * declined, and a notification is sent for our locally disabled-but-not-
michael@0 47 * declined engines.
michael@0 48 */
michael@0 49 add_test(function testOldMeta() {
michael@0 50 let meta = {
michael@0 51 payload: {
michael@0 52 engines: {
michael@0 53 "petrol": 1,
michael@0 54 "diesel": 2,
michael@0 55 "nonlocal": 3, // Enabled but not supported.
michael@0 56 },
michael@0 57 },
michael@0 58 };
michael@0 59
michael@0 60 _("Record: " + JSON.stringify(meta));
michael@0 61
michael@0 62 let manager = getEngineManager();
michael@0 63
michael@0 64 // Update enabled from meta/global.
michael@0 65 let engineSync = new EngineSynchronizer(Service);
michael@0 66 engineSync._updateEnabledFromMeta(meta, 3, manager);
michael@0 67
michael@0 68 Assert.ok(manager._engines["petrol"].enabled, "'petrol' locally enabled.");
michael@0 69 Assert.ok(manager._engines["diesel"].enabled, "'diesel' locally enabled.");
michael@0 70 Assert.ok(!("nonlocal" in manager._engines), "We don't know anything about the 'nonlocal' engine.");
michael@0 71 Assert.ok(!manager._engines["actual"].enabled, "'actual' not locally enabled.");
michael@0 72 Assert.ok(!manager.isDeclined("actual"), "'actual' not declined, though.");
michael@0 73
michael@0 74 let declinedEngines = new DeclinedEngines(Service);
michael@0 75
michael@0 76 function onNotDeclined(subject, topic, data) {
michael@0 77 Observers.remove("weave:engines:notdeclined", onNotDeclined);
michael@0 78 Assert.ok(subject.undecided.has("actual"), "EngineManager observed that 'actual' was undecided.");
michael@0 79
michael@0 80 let declined = manager.getDeclined();
michael@0 81 _("Declined: " + JSON.stringify(declined));
michael@0 82
michael@0 83 Assert.ok(!meta.changed, "No need to upload a new meta/global.");
michael@0 84 run_next_test();
michael@0 85 }
michael@0 86
michael@0 87 Observers.add("weave:engines:notdeclined", onNotDeclined);
michael@0 88
michael@0 89 declinedEngines.updateDeclined(meta, manager);
michael@0 90 });
michael@0 91
michael@0 92 /**
michael@0 93 * 'Fetch' a meta/global that declines an engine we don't
michael@0 94 * recognize. Ensure that we track that declined engine along
michael@0 95 * with any we locally declined, and that the meta/global
michael@0 96 * record is marked as changed and includes all declined
michael@0 97 * engines.
michael@0 98 */
michael@0 99 add_test(function testDeclinedMeta() {
michael@0 100 let meta = {
michael@0 101 payload: {
michael@0 102 engines: {
michael@0 103 "petrol": 1,
michael@0 104 "diesel": 2,
michael@0 105 "nonlocal": 3, // Enabled but not supported.
michael@0 106 },
michael@0 107 declined: ["nonexistent"], // Declined and not supported.
michael@0 108 },
michael@0 109 };
michael@0 110
michael@0 111 _("Record: " + JSON.stringify(meta));
michael@0 112
michael@0 113 let manager = getEngineManager();
michael@0 114 manager._engines["petrol"].enabled = true;
michael@0 115 manager._engines["diesel"].enabled = true;
michael@0 116 manager._engines["dummy"].enabled = true;
michael@0 117 manager._engines["actual"].enabled = false; // Disabled but not declined.
michael@0 118
michael@0 119 manager.decline(["localdecline"]); // Declined and not supported.
michael@0 120
michael@0 121 let declinedEngines = new DeclinedEngines(Service);
michael@0 122
michael@0 123 function onNotDeclined(subject, topic, data) {
michael@0 124 Observers.remove("weave:engines:notdeclined", onNotDeclined);
michael@0 125 Assert.ok(subject.undecided.has("actual"), "EngineManager observed that 'actual' was undecided.");
michael@0 126
michael@0 127 let declined = manager.getDeclined();
michael@0 128 _("Declined: " + JSON.stringify(declined));
michael@0 129
michael@0 130 Assert.equal(declined.indexOf("actual"), -1, "'actual' is locally disabled, but not marked as declined.");
michael@0 131
michael@0 132 Assert.equal(declined.indexOf("clients"), -1, "'clients' is enabled and not remotely declined.");
michael@0 133 Assert.equal(declined.indexOf("petrol"), -1, "'petrol' is enabled and not remotely declined.");
michael@0 134 Assert.equal(declined.indexOf("diesel"), -1, "'diesel' is enabled and not remotely declined.");
michael@0 135 Assert.equal(declined.indexOf("dummy"), -1, "'dummy' is enabled and not remotely declined.");
michael@0 136
michael@0 137 Assert.ok(0 <= declined.indexOf("nonexistent"), "'nonexistent' was declined on the server.");
michael@0 138
michael@0 139 Assert.ok(0 <= declined.indexOf("localdecline"), "'localdecline' was declined locally.");
michael@0 140
michael@0 141 // The meta/global is modified, too.
michael@0 142 Assert.ok(0 <= meta.payload.declined.indexOf("nonexistent"), "meta/global's declined contains 'nonexistent'.");
michael@0 143 Assert.ok(0 <= meta.payload.declined.indexOf("localdecline"), "meta/global's declined contains 'localdecline'.");
michael@0 144 Assert.strictEqual(true, meta.changed, "meta/global was changed.");
michael@0 145
michael@0 146 run_next_test();
michael@0 147 }
michael@0 148
michael@0 149 Observers.add("weave:engines:notdeclined", onNotDeclined);
michael@0 150
michael@0 151 declinedEngines.updateDeclined(meta, manager);
michael@0 152 });
michael@0 153

mercurial