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

/*
 * Test migration load path for user, enterprise policy and add-on
 * engines.
 */

"use strict";

const { EnterprisePolicyTesting } = ChromeUtils.importESModule(
  "resource://testing-common/EnterprisePolicyTesting.sys.mjs"
);

const enterprisePolicy = {
  policies: {
    SearchEngines: {
      Add: [
        {
          Name: "Policy",
          Encoding: "windows-1252",
          URLTemplate: "http://example.com/?q={searchTerms}",
        },
      ],
    },
  },
};

add_setup(async function () {
  // This initializes the policy engine for xpcshell tests
  let policies = Cc["@mozilla.org/enterprisepolicies;1"].getService(
    Ci.nsIObserver
  );
  policies.observe(null, "policies-startup", null);

  await SearchTestUtils.useTestEngines("data1");
  await AddonTestUtils.promiseStartupManager();
  await EnterprisePolicyTesting.setupPolicyEngineWithJson(enterprisePolicy);
  // Setting the enterprise policy starts the search service initialising,
  // so we wait for that to complete before starting the test, we can
  // then also add an extra add-on engine.
  await Services.search.init();
  let settingsFileWritten = promiseAfterSettings();
  await SearchTestUtils.installSearchExtension();
  await settingsFileWritten;
});

/**
 * Loads the settings file and ensures it has not already been migrated.
 */
add_task(async function test_load_and_check_settings() {
  let settingsTemplate = await readJSONFile(
    do_get_file("data/search-legacy-old-loadPaths.json")
  );

  Assert.less(
    settingsTemplate.version,
    8,
    "Should be a version older than when indexing engines by id was introduced"
  );
  let engine = settingsTemplate.engines.find(e => e.id == "policy-Policy");
  Assert.equal(
    engine._loadPath,
    "[other]addEngineWithDetails:set-via-policy",
    "Should have a old style load path for the policy engine"
  );
  engine = settingsTemplate.engines.find(
    e => e.id == "bbc163e7-7b1a-47aa-a32c-c59062de2754"
  );
  Assert.equal(
    engine._loadPath,
    "[other]addEngineWithDetails:set-via-user",
    "Should have a old style load path for the user engine"
  );
  engine = settingsTemplate.engines.find(
    e => e.id == "example@tests.mozilla.orgdefault"
  );
  Assert.equal(
    engine._loadPath,
    "[other]addEngineWithDetails:example@tests.mozilla.org",
    "Should have a old style load path for the add-on engine"
  );

  await promiseSaveSettingsData(settingsTemplate);
});

/**
 * Tests that an installed engine matches the expected data.
 *
 * @param {object} expectedData The expected data for the engine
 */
async function assertInstalledEngineMatches(expectedData) {
  let engine = await Services.search.getEngineByName(expectedData.name);

  Assert.ok(engine, `Should have found the ${expectedData.type} engine`);
  Assert.equal(
    engine.wrappedJSObject._loadPath,
    expectedData.loadPath,
    "Should have migrated the loadPath"
  );
}

add_task(async function test_migration_from_pre_ids() {
  const settingsFileWritten = promiseAfterSettings();

  await Services.search.wrappedJSObject.reset();
  await Services.search.init();

  await settingsFileWritten;

  await assertInstalledEngineMatches({
    type: "Policy",
    name: "Policy",
    loadPath: "[policy]",
  });
  await assertInstalledEngineMatches({
    type: "User",
    name: "User",
    loadPath: "[user]",
  });
  await assertInstalledEngineMatches({
    type: "Add-on",
    name: "Example",
    loadPath: "[addon]example@tests.mozilla.org",
  });
});