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

"use strict";

const ENGINE_ID = "enginetest@example.com";
let xpi;
let profile = do_get_profile().clone();

add_task(async function setup() {
  await SearchTestUtils.useTestEngines("data1");
  xpi = AddonTestUtils.createTempWebExtensionFile({
    manifest: {
      version: "1.0",
      browser_specific_settings: {
        gecko: { id: ENGINE_ID },
      },
      chrome_settings_overrides: {
        search_provider: {
          name: "Test Engine",
          search_url: `https://example.com/?q={searchTerms}`,
        },
      },
    },
  });
  await AddonTestUtils.manuallyInstall(xpi);
});

add_task(async function test_removeAddonOnStartup() {
  // First startup the add-on manager and ensure the engine is installed.
  await AddonTestUtils.promiseStartupManager();
  let promise = promiseAfterSettings();
  await Services.search.init();

  let engine = Services.search.getEngineByName("Test Engine");
  let allEngines = await Services.search.getEngines();

  Assert.ok(!!engine, "Should have installed the test engine");

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

  await AddonTestUtils.promiseShutdownManager();

  // Now remove it, reset the search service and start up the add-on manager.
  // Note: the saved settings will have the engine in. If this didn't work,
  // the engine would still be present.
  await IOUtils.remove(
    PathUtils.join(profile.path, "extensions", `${ENGINE_ID}.xpi`)
  );

  let removePromise = SearchTestUtils.promiseSearchNotification(
    SearchUtils.MODIFIED_TYPE.REMOVED,
    SearchUtils.TOPIC_ENGINE_MODIFIED
  );
  Services.search.wrappedJSObject.reset();
  await AddonTestUtils.promiseStartupManager();
  await Services.search.init();
  await removePromise;

  Assert.ok(
    !Services.search.getEngineByName("Test Engine"),
    "Should have removed the test engine"
  );

  let newEngines = await Services.search.getEngines();
  Assert.deepEqual(
    newEngines.map(e => e.name),
    allEngines.map(e => e.name).filter(n => n != "Test Engine"),
    "Should no longer have the test engine in the full list"
  );
  let newDefault = await Services.search.getDefault();
  Assert.equal(
    newDefault.name,
    "engine1",
    "Should have changed the default engine back to the configuration default"
  );

  await AddonTestUtils.promiseShutdownManager();
});