summaryrefslogtreecommitdiffstats
path: root/toolkit/components/search/tests/xpcshell/test_appProvided_icons_updates.js
blob: 4159b9f0fbb9098da0e824b48f505f7122ed1b61 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* Any copyright is dedicated to the Public Domain.
https://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Tests to ensure that icons for application provided engines are correctly
 * updated from remote settings.
 */

"use strict";

// A skeleton configuration that gets filled in from TESTS during `add_setup`.
let CONFIG = [
  {
    identifier: "engine_no_initial_icon",
    recordType: "engine",
    base: {
      name: "engine_no_initial_icon name",
      urls: {
        search: {
          base: "https://example.com/1",
          searchTermParamName: "q",
        },
      },
    },
    variants: [{ environment: { allRegionsAndLocales: true } }],
  },
  {
    identifier: "engine_icon_updates",
    recordType: "engine",
    base: {
      name: "engine_icon_updates name",
      urls: {
        search: {
          base: "https://example.com/2",
          searchTermParamName: "q",
        },
      },
    },
    variants: [{ environment: { allRegionsAndLocales: true } }],
  },
  {
    identifier: "engine_icon_not_local",
    recordType: "engine",
    base: {
      name: "engine_icon_not_local name",
      urls: {
        search: {
          base: "https://example.com/3",
          searchTermParamName: "q",
        },
      },
    },
    variants: [{ environment: { allRegionsAndLocales: true } }],
  },
  {
    identifier: "engine_icon_out_of_date",
    recordType: "engine",
    base: {
      name: "engine_icon_out_of_date name",
      urls: {
        search: {
          base: "https://example.com/4",
          searchTermParamName: "q",
        },
      },
    },
    variants: [{ environment: { allRegionsAndLocales: true } }],
  },
  {
    recordType: "defaultEngines",
    globalDefault: "engine_no_initial_icon",
    specificDefaults: [],
  },
  {
    recordType: "engineOrders",
    orders: [],
  },
];

async function assertIconMatches(actualIconData, expectedIcon) {
  let expectedBuffer = new Uint8Array(await getFileDataBuffer(expectedIcon));

  Assert.equal(
    actualIconData.length,
    expectedBuffer.length,
    "Should have received matching buffer lengths for the expected icon"
  );
  Assert.ok(
    actualIconData.every((value, index) => value === expectedBuffer[index]),
    "Should have received matching data for the expected icon"
  );
}

async function assertEngineIcon(engineName, expectedIcon) {
  let engine = Services.search.getEngineByName(engineName);
  let engineIconURL = await engine.getIconURL(16);

  if (expectedIcon) {
    Assert.notEqual(
      engineIconURL,
      null,
      "Should have an icon URL for the engine."
    );

    let response = await fetch(engineIconURL);
    let buffer = new Uint8Array(await response.arrayBuffer());

    await assertIconMatches(buffer, expectedIcon);
  } else {
    Assert.equal(
      engineIconURL,
      null,
      "Should not have an icon URL for the engine."
    );
  }
}

let originalIconId = Services.uuid.generateUUID().toString();
let client;

add_setup(async function setup() {
  useHttpServer();
  SearchTestUtils.useMockIdleService();

  client = RemoteSettings("search-config-icons");
  await client.db.clear();

  sinon.stub(client.attachments, "_baseAttachmentsURL").returns(gDataUrl);

  // Add some initial records and attachments into the remote settings collection.
  await insertRecordIntoCollection(client, {
    id: originalIconId,
    filename: "remoteIcon.ico",
    // This uses a wildcard match to test the icon is still applied correctly.
    engineIdentifiers: ["engine_icon_upd*"],
    imageSize: 16,
  });
  // This attachment is not cached, so we don't have it locally.
  await insertRecordIntoCollection(
    client,
    {
      id: Services.uuid.generateUUID().toString(),
      filename: "bigIcon.ico",
      engineIdentifiers: [
        // This also tests multiple engine idenifiers works.
        "enterprise",
        "next_generation",
        "engine_icon_not_local",
      ],
      imageSize: 16,
    },
    false
  );

  // Add a record that is out of date, and update it with a newer one, but don't
  // cache the attachment for the new one.
  let outOfDateRecordId = Services.uuid.generateUUID().toString();
  await insertRecordIntoCollection(
    client,
    {
      id: outOfDateRecordId,
      filename: "remoteIcon.ico",
      engineIdentifiers: ["engine_icon_out_of_date"],
      imageSize: 16,
      // 10 minutes ago.
      lastModified: Date.now() - 600000,
    },
    true
  );
  let { record } = await mockRecordWithAttachment({
    id: outOfDateRecordId,
    filename: "bigIcon.ico",
    engineIdentifiers: ["engine_icon_out_of_date"],
    imageSize: 16,
  });
  await client.db.update(record);
  await client.db.importChanges({}, record.lastModified);

  await SearchTestUtils.useTestEngines("simple-engines", null, CONFIG);
  await Services.search.init();

  // Testing that an icon is not local generates a `Could not find {id}...`
  // message.
  consoleAllowList.push("Could not find");
});

add_task(async function test_icon_added_unknown_engine() {
  // If the engine is unknown, and this is a new icon, we should still download
  // the icon, in case the engine is added to the configuration later.
  let newIconId = Services.uuid.generateUUID().toString();

  let mock = await mockRecordWithAttachment({
    id: newIconId,
    filename: "bigIcon.ico",
    engineIdentifiers: ["engine_unknown"],
    imageSize: 16,
  });
  await client.db.update(mock.record, Date.now());

  await client.emit("sync", {
    data: {
      current: [mock.record],
      created: [mock.record],
      updated: [],
      deleted: [],
    },
  });

  SearchTestUtils.idleService._fireObservers("idle");

  let icon;
  await TestUtils.waitForCondition(async () => {
    try {
      icon = await client.attachments.get(mock.record);
    } catch (ex) {
      // Do nothing.
    }
    return !!icon;
  }, "Should have loaded the icon into the attachments store.");

  await assertIconMatches(new Uint8Array(icon.buffer), "bigIcon.ico");
});

add_task(async function test_icon_added_existing_engine() {
  // If the engine is unknown, and this is a new icon, we should still download
  // it, in case the engine is added to the configuration later.
  let newIconId = Services.uuid.generateUUID().toString();

  let mock = await mockRecordWithAttachment({
    id: newIconId,
    filename: "bigIcon.ico",
    engineIdentifiers: ["engine_no_initial_icon"],
    imageSize: 16,
  });
  await client.db.update(mock.record, Date.now());

  let promiseEngineUpdated = SearchTestUtils.promiseSearchNotification(
    SearchUtils.MODIFIED_TYPE.CHANGED,
    SearchUtils.TOPIC_ENGINE_MODIFIED
  );

  await client.emit("sync", {
    data: {
      current: [mock.record],
      created: [mock.record],
      updated: [],
      deleted: [],
    },
  });

  SearchTestUtils.idleService._fireObservers("idle");

  await promiseEngineUpdated;
  await assertEngineIcon("engine_no_initial_icon name", "bigIcon.ico");
});

add_task(async function test_icon_updated() {
  // Test that when an update for an engine icon is received, the engine is
  // correctly updated.

  // Check the engine has the expected icon to start with.
  await assertEngineIcon("engine_icon_updates name", "remoteIcon.ico");

  // Update the icon for the engine.
  let mock = await mockRecordWithAttachment({
    id: originalIconId,
    filename: "bigIcon.ico",
    engineIdentifiers: ["engine_icon_upd*"],
    imageSize: 16,
  });
  await client.db.update(mock.record, Date.now());

  let promiseEngineUpdated = SearchTestUtils.promiseSearchNotification(
    SearchUtils.MODIFIED_TYPE.CHANGED,
    SearchUtils.TOPIC_ENGINE_MODIFIED
  );

  await client.emit("sync", {
    data: {
      current: [mock.record],
      created: [],
      updated: [{ new: mock.record }],
      deleted: [],
    },
  });
  SearchTestUtils.idleService._fireObservers("idle");

  await promiseEngineUpdated;
  await assertEngineIcon("engine_icon_updates name", "bigIcon.ico");
});

add_task(async function test_icon_not_local() {
  // Tests that a download is queued and triggered when the icon for an engine
  // is not in either the local dump nor the cache.

  await assertEngineIcon("engine_icon_not_local name", null);

  // A download should have been queued, so fire idle to trigger it.
  let promiseEngineUpdated = SearchTestUtils.promiseSearchNotification(
    SearchUtils.MODIFIED_TYPE.CHANGED,
    SearchUtils.TOPIC_ENGINE_MODIFIED
  );
  SearchTestUtils.idleService._fireObservers("idle");
  await promiseEngineUpdated;

  await assertEngineIcon("engine_icon_not_local name", "bigIcon.ico");
});

add_task(async function test_icon_out_of_date() {
  // Tests that a download is queued and triggered when the icon for an engine
  // is not in either the local dump nor the cache.

  await assertEngineIcon("engine_icon_out_of_date name", "remoteIcon.ico");

  // A download should have been queued, so fire idle to trigger it.
  let promiseEngineUpdated = SearchTestUtils.promiseSearchNotification(
    SearchUtils.MODIFIED_TYPE.CHANGED,
    SearchUtils.TOPIC_ENGINE_MODIFIED
  );
  SearchTestUtils.idleService._fireObservers("idle");
  await promiseEngineUpdated;

  await assertEngineIcon("engine_icon_out_of_date name", "bigIcon.ico");
});