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

"use strict";

const { promiseShutdownManager, promiseStartupManager } = AddonTestUtils;

add_task(async function setup() {
  Services.locale.availableLocales = [
    ...Services.locale.availableLocales,
    "en",
    "de",
    "fr",
  ];
  Services.locale.requestedLocales = ["en"];

  await SearchTestUtils.useTestEngines("data1");
  await promiseStartupManager();
  await Services.search.init();
  await promiseAfterSettings();

  registerCleanupFunction(promiseShutdownManager);
});

add_task(async function test_language_switch_changes_name() {
  await SearchTestUtils.installSearchExtension(
    {
      name: "__MSG_engineName__",
      id: "engine@tests.mozilla.org",
      search_url_get_params: `q={searchTerms}&version=1.0`,
      default_locale: "en",
      version: "1.0",
    },
    { skipUnload: false },
    {
      "_locales/en/messages.json": {
        engineName: {
          message: "English Name",
          description: "The Name",
        },
      },
      "_locales/fr/messages.json": {
        engineName: {
          message: "French Name",
          description: "The Name",
        },
      },
    }
  );

  let engine = Services.search.getEngineById("engine@tests.mozilla.orgdefault");
  Assert.ok(!!engine, "Should have loaded the engine");
  Assert.equal(
    engine.name,
    "English Name",
    "Should have loaded the English version of the name"
  );

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

  let promiseChanged = TestUtils.topicObserved(
    "browser-search-engine-modified",
    (eng, verb) => verb == "engine-changed"
  );

  await promiseSetLocale("fr");

  await promiseChanged;

  engine = Services.search.getEngineById("engine@tests.mozilla.orgdefault");
  Assert.ok(!!engine, "Should still be available");
  Assert.equal(
    engine.name,
    "French Name",
    "Should have updated to the French version of the name"
  );

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

  promiseChanged = TestUtils.topicObserved(
    "browser-search-engine-modified",
    (eng, verb) => verb == "engine-changed"
  );

  // Check for changing to a locale the add-on doesn't have.
  await promiseSetLocale("de");

  await promiseChanged;

  engine = Services.search.getEngineById("engine@tests.mozilla.orgdefault");
  Assert.ok(!!engine, "Should still be available");
  Assert.equal(
    engine.name,
    "English Name",
    "Should have fallen back to the default locale (English) version of the name"
  );

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