summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_tab_quickwrite.js
blob: c363992d66b2adba6a829e1e821aa4fa9e0b2758 (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
195
196
197
198
199
200
201
202
203
204
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

ChromeUtils.importESModule("resource://services-sync/engines/tabs.sys.mjs");
const { Service } = ChromeUtils.importESModule(
  "resource://services-sync/service.sys.mjs"
);

const { TabProvider } = ChromeUtils.importESModule(
  "resource://services-sync/engines/tabs.sys.mjs"
);

const FAR_FUTURE = 4102405200000; // 2100/01/01

add_task(async function setup() {
  // Since these are xpcshell tests, we'll need to mock ui features
  TabProvider.shouldSkipWindow = mockShouldSkipWindow;
  TabProvider.getWindowEnumerator = mockGetWindowEnumerator.bind(this, [
    "http://foo.com",
  ]);
});

async function prepareServer() {
  _("Setting up Sync server");
  Service.serverConfiguration = {
    max_post_records: 100,
  };

  let server = new SyncServer();
  server.start();
  await SyncTestingInfrastructure(server, "username");
  server.registerUser("username");

  let collection = server.createCollection("username", "tabs");
  await generateNewKeys(Service.collectionKeys);

  let engine = Service.engineManager.get("tabs");
  await engine.initialize();

  return { server, collection, engine };
}

async function withPatchedValue(object, name, patchedVal, fn) {
  _(`patching ${name}=${patchedVal}`);
  let old = object[name];
  object[name] = patchedVal;
  try {
    await fn();
  } finally {
    object[name] = old;
  }
}

add_task(async function test_tab_quickwrite_works() {
  _("Ensure a simple quickWrite works.");
  let { server, collection, engine } = await prepareServer();
  Assert.equal(collection.count(), 0, "starting with 0 tab records");
  Assert.ok(await engine.quickWrite());
  // Validate we didn't bork lastSync
  let lastSync = await engine.getLastSync();
  Assert.ok(lastSync < FAR_FUTURE);
  Assert.equal(collection.count(), 1, "tab record was written");

  await promiseStopServer(server);
});

add_task(async function test_tab_bad_status() {
  _("Ensure quickWrite silently aborts when we aren't setup correctly.");
  let { server, engine } = await prepareServer();
  // Store the original lock to reset it back after this test
  let lock = engine.lock;
  // Arrange for this test to fail if it tries to take the lock.
  engine.lock = function () {
    throw new Error("this test should abort syncing before locking");
  };
  let quickWrite = engine.quickWrite.bind(engine); // lol javascript.

  await withPatchedValue(engine, "enabled", false, quickWrite);
  await withPatchedValue(Service, "serverConfiguration", null, quickWrite);

  Services.prefs.clearUserPref("services.sync.username");
  await quickWrite();
  // Validate we didn't bork lastSync
  let lastSync = await engine.getLastSync();
  Assert.ok(lastSync < FAR_FUTURE);
  Service.status.resetSync();
  engine.lock = lock;
  await promiseStopServer(server);
});

add_task(async function test_tab_quickwrite_lock() {
  _("Ensure we fail to quickWrite if the engine is locked.");
  let { server, collection, engine } = await prepareServer();

  Assert.equal(collection.count(), 0, "starting with 0 tab records");
  engine.lock();
  Assert.ok(!(await engine.quickWrite()));
  Assert.equal(collection.count(), 0, "didn't sync due to being locked");
  engine.unlock();

  await promiseStopServer(server);
});

add_task(async function test_tab_quickwrite_keeps_old_tabs() {
  _("Ensure we don't delete other tabs on quickWrite (bug 1801295).");
  let { server, engine } = await prepareServer();

  // need a first sync to ensure everything is setup correctly.
  await Service.sync({ engines: ["tabs"] });

  const id = "fake-guid-99";
  let remoteRecord = encryptPayload({
    id,
    clientName: "not local",
    tabs: [
      {
        title: "title2",
        urlHistory: ["http://bar.com/"],
        icon: "",
        lastUsed: 3000,
      },
    ],
  });

  let collection = server.getCollection("username", "tabs");
  collection.insert(id, remoteRecord);

  await Service.sync({ engines: ["tabs"] });

  // collection should now have 2 records - ours and the pretend remote one we inserted.
  Assert.equal(collection.count(), 2, "starting with 2 tab records");

  // So fxAccounts.device.recentDeviceList is not null.
  engine.service.clientsEngine.fxAccounts.device._deviceListCache = {
    devices: [],
  };
  // trick the clients engine into thinking it has a remote client with the same guid.
  engine.service.clientsEngine._store._remoteClients = {};
  engine.service.clientsEngine._store._remoteClients[id] = {
    id,
    fxaDeviceId: id,
  };

  let clients = await engine.getAllClients();
  Assert.equal(clients.length, 1);

  _("Doing a quick-write");
  Assert.ok(await engine.quickWrite());

  // Should still have our client after a quickWrite.
  _("Grabbing clients after the quick-write");
  clients = await engine.getAllClients();
  Assert.equal(clients.length, 1);

  engine.service.clientsEngine._store._remoteClients = {};

  await promiseStopServer(server);
});

add_task(async function test_tab_lastSync() {
  _("Ensure we restore the lastSync timestamp after a quick-write.");
  let { server, collection, engine } = await prepareServer();

  await engine.initialize();
  await engine.service.clientsEngine.initialize();

  let origLastSync = engine.lastSync;
  Assert.ok(await engine.quickWrite());
  Assert.equal(engine.lastSync, origLastSync);
  Assert.equal(collection.count(), 1, "successful sync");
  engine.unlock();

  await promiseStopServer(server);
});

add_task(async function test_tab_quickWrite_telemetry() {
  _("Ensure we record the telemetry we expect.");
  // hook into telemetry
  let telem = get_sync_test_telemetry();
  telem.payloads = [];
  let oldSubmit = telem.submit;
  let submitPromise = new Promise(resolve => {
    telem.submit = function (ping) {
      telem.submit = oldSubmit;
      resolve(ping);
    };
  });

  let { server, collection, engine } = await prepareServer();

  Assert.equal(collection.count(), 0, "starting with 0 tab records");
  Assert.ok(await engine.quickWrite());
  Assert.equal(collection.count(), 1, "tab record was written");

  let ping = await submitPromise;
  let syncs = ping.syncs;
  Assert.equal(syncs.length, 1);
  let sync = syncs[0];
  Assert.equal(sync.why, "quick-write");
  Assert.equal(sync.engines.length, 1);
  Assert.equal(sync.engines[0].name, "tabs");

  await promiseStopServer(server);
});