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