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

/**
 * Tests reloading engines when a user has an engine installed that is the
 * same name as an application provided engine being added to the user's set
 * of engines.
 *
 * Ensures that settings are not automatically taken across.
 */

"use strict";

const CONFIG = [
  {
    webExtension: {
      id: "engine@search.mozilla.org",
      name: "Test search engine",
      search_url: "https://www.google.com/search",
      params: [
        {
          name: "q",
          value: "{searchTerms}",
        },
      ],
    },
    appliesTo: [
      {
        included: { everywhere: true },
        default: "yes",
      },
    ],
  },
  {
    webExtension: {
      id: "engine-pref@search.mozilla.org",
      name: "engine-pref",
      search_url: "https://www.google.com/search",
      params: [
        {
          name: "q",
          value: "{searchTerms}",
        },
      ],
    },
    appliesTo: [
      {
        included: { regions: ["FR"] },
      },
    ],
  },
];

const CONFIG_V2 = [
  {
    recordType: "engine",
    identifier: "engine",
    base: {
      name: "Test search engine",
      urls: {
        search: {
          base: "https://www.google.com/search",
          params: [],
          searchTermParamName: "q",
        },
      },
    },
    variants: [
      {
        environment: { allRegionsAndLocales: true },
      },
    ],
  },
  {
    recordType: "engine",
    identifier: "engine-pref",
    base: {
      name: "engine-pref",
      urls: {
        search: {
          base: "https://www.google.com/search",
          params: [],
          searchTermParamName: "q",
        },
      },
    },
    variants: [
      {
        environment: { regions: ["FR"] },
      },
    ],
  },
  {
    recordType: "defaultEngines",
    globalDefault: "engine",
    specificDefaults: [],
  },
  {
    recordType: "engineOrders",
    orders: [],
  },
];

add_setup(async () => {
  let server = useHttpServer();
  server.registerContentType("sjs", "sjs");

  // We use a region that doesn't install `engine-pref` by default so that we can
  // manually install it first (like when a user installs a browser add-on), and
  // then test what happens when we switch regions to one which would install
  // `engine-pref`.
  Region._setHomeRegion("US", false);

  await SearchTestUtils.useTestEngines(
    "data",
    null,
    SearchUtils.newSearchConfigEnabled ? CONFIG_V2 : CONFIG
  );
  await AddonTestUtils.promiseStartupManager();
  await Services.search.init();
});

add_task(async function test_reload_engines_with_duplicate() {
  let engines = await Services.search.getEngines();

  Assert.deepEqual(
    engines.map(e => e.name),
    ["Test search engine"],
    "Should have the expected default engines"
  );
  // Simulate a user installing a search engine that shares the same name as an
  // application provided search engine not currently installed in their browser.
  let engine = await SearchTestUtils.promiseNewSearchEngine({
    url: `${gDataUrl}engineMaker.sjs?${JSON.stringify({
      baseURL: gDataUrl,
      name: "engine-pref",
      method: "GET",
    })}`,
  });

  engine.alias = "testEngine";

  let engineId = engine.id;

  Region._setHomeRegion("FR", false);

  await Services.search.wrappedJSObject._maybeReloadEngines();

  Assert.ok(
    !(await Services.search.getEngineById(engineId)),
    "Should not have added the duplicate engine"
  );

  engines = await Services.search.getEngines();

  Assert.deepEqual(
    engines.map(e => e.name),
    ["Test search engine", "engine-pref"],
    "Should have the expected default engines"
  );

  let enginePref = await Services.search.getEngineByName("engine-pref");

  Assert.equal(
    enginePref.alias,
    "",
    "Should not have copied the alias from the duplicate engine"
  );
});