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

"use strict";

const kExtensionID = "simple@tests.mozilla.org";

add_setup(async function () {
  useHttpServer("opensearch");
  await AddonTestUtils.promiseStartupManager();
  await SearchTestUtils.useTestEngines("data1");
  await Services.search.init();
});

add_task(async function test_migrateLegacyEngine() {
  let engine = await SearchTestUtils.promiseNewSearchEngine({
    url: gDataUrl + "simple.xml",
  });

  // Modify the loadpath so it looks like a legacy plugin loadpath
  engine.wrappedJSObject._loadPath = `jar:[profile]/extensions/${kExtensionID}.xpi!/simple.xml`;
  engine.wrappedJSObject._extensionID = null;

  await Services.search.setDefault(
    engine,
    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
  );

  // This should replace the existing engine
  let extension = await SearchTestUtils.installSearchExtension(
    {
      id: "simple",
      name: "simple",
      search_url: "https://example.com/",
    },
    { skipUnload: true }
  );

  engine = Services.search.getEngineByName("simple");
  Assert.equal(engine.wrappedJSObject._loadPath, "[addon]" + kExtensionID);
  Assert.equal(engine.wrappedJSObject._extensionID, kExtensionID);

  Assert.equal(
    (await Services.search.getDefault()).name,
    "simple",
    "Should have kept the default engine the same"
  );

  await extension.unload();
});

add_task(async function test_migrateLegacyEngineDifferentName() {
  let engine = await SearchTestUtils.promiseNewSearchEngine({
    url: gDataUrl + "simple.xml",
  });

  // Modify the loadpath so it looks like an legacy plugin loadpath
  engine.wrappedJSObject._loadPath = `jar:[profile]/extensions/${kExtensionID}.xpi!/simple.xml`;
  engine.wrappedJSObject._extensionID = null;

  await Services.search.setDefault(
    engine,
    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
  );

  // This should replace the existing engine - it has the same id, but a different name.
  let extension = await SearchTestUtils.installSearchExtension(
    {
      id: "simple",
      name: "simple search",
      search_url: "https://example.com/",
    },
    { skipUnload: true }
  );

  engine = Services.search.getEngineByName("simple");
  Assert.equal(engine, null, "Should have removed the old engine");

  // The engine should have changed its name.
  engine = Services.search.getEngineByName("simple search");
  Assert.equal(engine.wrappedJSObject._loadPath, "[addon]" + kExtensionID);
  Assert.equal(engine.wrappedJSObject._extensionID, kExtensionID);

  Assert.equal(
    (await Services.search.getDefault()).name,
    "simple search",
    "Should have made the new engine default"
  );

  await extension.unload();
});