summaryrefslogtreecommitdiffstats
path: root/toolkit/components/search/tests/xpcshell/test_webextensions_migrate_to.js
blob: b9e78203caad5735aaf3699ec82e72ae937bac67 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/*
 * Test migrating legacy add-on engines in background.
 */

"use strict";

add_task(async function setup() {
  useHttpServer("opensearch");
  await AddonTestUtils.promiseStartupManager();
  await SearchTestUtils.useTestEngines("data1");

  let data = await readJSONFile(do_get_file("data/search-migration.json"));

  await promiseSaveSettingsData(data);

  await Services.search.init();

  // We need the extension installed for this test, but we do not want to
  // trigger the functions that happen on installation, so stub that out.
  // The manifest already has details of this engine.
  let oldFunc = Services.search.wrappedJSObject.addEnginesFromExtension;
  Services.search.wrappedJSObject.addEnginesFromExtension = () => {};

  // Add the add-on so add-on manager has a valid item.
  await SearchTestUtils.installSearchExtension({
    id: "simple",
    name: "simple search",
    search_url: "https://example.com/",
  });

  Services.search.wrappedJSObject.addEnginesFromExtension = oldFunc;
});

add_task(async function test_migrateLegacyEngineDifferentName() {
  await Services.search.init();

  let engine = Services.search.getEngineByName("simple");
  Assert.ok(engine, "Should have the legacy add-on engine.");

  // Set this engine as default, the new engine should become the default
  // after migration.
  await Services.search.setDefault(
    engine,
    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
  );

  engine = Services.search.getEngineByName("simple search");
  Assert.ok(engine, "Should have the WebExtension engine.");

  await Services.search.runBackgroundChecks();

  engine = Services.search.getEngineByName("simple");
  Assert.ok(!engine, "Should have removed the legacy add-on engine");

  engine = Services.search.getEngineByName("simple search");
  Assert.ok(engine, "Should have kept the WebExtension engine.");

  Assert.equal(
    (await Services.search.getDefault()).name,
    engine.name,
    "Should have switched to the WebExtension engine as default."
  );
});