summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_declined.js
blob: aecd33ee6e7a0d50116c96b1b07096714d530d9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

const { DeclinedEngines } = ChromeUtils.importESModule(
  "resource://services-sync/stages/declined.sys.mjs"
);
const { EngineSynchronizer } = ChromeUtils.importESModule(
  "resource://services-sync/stages/enginesync.sys.mjs"
);
const { Service } = ChromeUtils.importESModule(
  "resource://services-sync/service.sys.mjs"
);
const { Observers } = ChromeUtils.importESModule(
  "resource://services-common/observers.sys.mjs"
);

function PetrolEngine() {}
PetrolEngine.prototype.name = "petrol";

function DieselEngine() {}
DieselEngine.prototype.name = "diesel";

function DummyEngine() {}
DummyEngine.prototype.name = "dummy";

function ActualEngine() {}
ActualEngine.prototype.name = "actual";
Object.setPrototypeOf(ActualEngine.prototype, SyncEngine.prototype);

function getEngineManager() {
  let manager = new EngineManager(Service);
  Service.engineManager = manager;
  manager._engines = {
    petrol: new PetrolEngine(),
    diesel: new DieselEngine(),
    dummy: new DummyEngine(),
    actual: new ActualEngine(),
  };
  return manager;
}

/**
 * 'Fetch' a meta/global record that doesn't mention declined.
 *
 * Push it into the EngineSynchronizer to set enabled; verify that those are
 * correct.
 *
 * Then push it into DeclinedEngines to set declined; verify that none are
 * declined, and a notification is sent for our locally disabled-but-not-
 * declined engines.
 */
add_task(async function testOldMeta() {
  let meta = {
    payload: {
      engines: {
        petrol: 1,
        diesel: 2,
        nonlocal: 3, // Enabled but not supported.
      },
    },
  };

  _("Record: " + JSON.stringify(meta));

  let manager = getEngineManager();

  // Update enabled from meta/global.
  let engineSync = new EngineSynchronizer(Service);
  await engineSync._updateEnabledFromMeta(meta, 3, manager);

  Assert.ok(manager._engines.petrol.enabled, "'petrol' locally enabled.");
  Assert.ok(manager._engines.diesel.enabled, "'diesel' locally enabled.");
  Assert.ok(
    !("nonlocal" in manager._engines),
    "We don't know anything about the 'nonlocal' engine."
  );
  Assert.ok(!manager._engines.actual.enabled, "'actual' not locally enabled.");
  Assert.ok(!manager.isDeclined("actual"), "'actual' not declined, though.");

  let declinedEngines = new DeclinedEngines(Service);

  function onNotDeclined(subject) {
    Observers.remove("weave:engines:notdeclined", onNotDeclined);
    Assert.ok(
      subject.undecided.has("actual"),
      "EngineManager observed that 'actual' was undecided."
    );

    let declined = manager.getDeclined();
    _("Declined: " + JSON.stringify(declined));

    Assert.ok(!meta.changed, "No need to upload a new meta/global.");
    run_next_test();
  }

  Observers.add("weave:engines:notdeclined", onNotDeclined);

  declinedEngines.updateDeclined(meta, manager);
});

/**
 * 'Fetch' a meta/global that declines an engine we don't
 * recognize. Ensure that we track that declined engine along
 * with any we locally declined, and that the meta/global
 * record is marked as changed and includes all declined
 * engines.
 */
add_task(async function testDeclinedMeta() {
  let meta = {
    payload: {
      engines: {
        petrol: 1,
        diesel: 2,
        nonlocal: 3, // Enabled but not supported.
      },
      declined: ["nonexistent"], // Declined and not supported.
    },
  };

  _("Record: " + JSON.stringify(meta));

  let manager = getEngineManager();
  manager._engines.petrol.enabled = true;
  manager._engines.diesel.enabled = true;
  manager._engines.dummy.enabled = true;
  manager._engines.actual.enabled = false; // Disabled but not declined.

  manager.decline(["localdecline"]); // Declined and not supported.

  let declinedEngines = new DeclinedEngines(Service);

  function onNotDeclined(subject) {
    Observers.remove("weave:engines:notdeclined", onNotDeclined);
    Assert.ok(
      subject.undecided.has("actual"),
      "EngineManager observed that 'actual' was undecided."
    );

    let declined = manager.getDeclined();
    _("Declined: " + JSON.stringify(declined));

    Assert.equal(
      declined.indexOf("actual"),
      -1,
      "'actual' is locally disabled, but not marked as declined."
    );

    Assert.equal(
      declined.indexOf("clients"),
      -1,
      "'clients' is enabled and not remotely declined."
    );
    Assert.equal(
      declined.indexOf("petrol"),
      -1,
      "'petrol' is enabled and not remotely declined."
    );
    Assert.equal(
      declined.indexOf("diesel"),
      -1,
      "'diesel' is enabled and not remotely declined."
    );
    Assert.equal(
      declined.indexOf("dummy"),
      -1,
      "'dummy' is enabled and not remotely declined."
    );

    Assert.ok(
      0 <= declined.indexOf("nonexistent"),
      "'nonexistent' was declined on the server."
    );

    Assert.ok(
      0 <= declined.indexOf("localdecline"),
      "'localdecline' was declined locally."
    );

    // The meta/global is modified, too.
    Assert.ok(
      0 <= meta.payload.declined.indexOf("nonexistent"),
      "meta/global's declined contains 'nonexistent'."
    );
    Assert.ok(
      0 <= meta.payload.declined.indexOf("localdecline"),
      "meta/global's declined contains 'localdecline'."
    );
    Assert.strictEqual(true, meta.changed, "meta/global was changed.");
  }

  Observers.add("weave:engines:notdeclined", onNotDeclined);

  declinedEngines.updateDeclined(meta, manager);
});