summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_locale_data.js
blob: 31468e91d34711e88354f3a0cc01b005348e0f29 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"use strict";

AddonTestUtils.init(this);
AddonTestUtils.overrideCertDB();
AddonTestUtils.createAppInfo(
  "xpcshell@tests.mozilla.org",
  "XPCShell",
  "1",
  "42"
);

const { ExtensionData } = ChromeUtils.importESModule(
  "resource://gre/modules/Extension.sys.mjs"
);

async function generateAddon(data) {
  let xpi = AddonTestUtils.createTempWebExtensionFile(data);

  let fileURI = Services.io.newFileURI(xpi);
  let jarURI = NetUtil.newURI(`jar:${fileURI.spec}!/`);

  let extension = new ExtensionData(jarURI, false);
  await extension.loadManifest();

  return extension;
}

add_task(async function testMissingDefaultLocale() {
  let extension = await generateAddon({
    files: {
      "_locales/en_US/messages.json": {},
    },
  });

  equal(extension.errors.length, 0, "No errors reported");

  await extension.initAllLocales();

  equal(extension.errors.length, 1, "One error reported");

  info(`Got error: ${extension.errors[0]}`);

  ok(
    extension.errors[0].includes('"default_locale" property is required'),
    "Got missing default_locale error"
  );
});

add_task(async function testInvalidDefaultLocale() {
  let extension = await generateAddon({
    manifest: {
      default_locale: "en",
    },

    files: {
      "_locales/en_US/messages.json": {},
    },
  });

  equal(extension.errors.length, 1, "One error reported");

  info(`Got error: ${extension.errors[0]}`);

  ok(
    extension.errors[0].includes(
      "Loading locale file _locales/en/messages.json"
    ),
    "Got invalid default_locale error"
  );

  await extension.initAllLocales();

  equal(extension.errors.length, 2, "Two errors reported");

  info(`Got error: ${extension.errors[1]}`);

  ok(
    extension.errors[1].includes('"default_locale" property must correspond'),
    "Got invalid default_locale error"
  );
});

add_task(async function testUnexpectedDefaultLocale() {
  let extension = await generateAddon({
    manifest: {
      default_locale: "en_US",
    },
  });

  equal(extension.errors.length, 1, "One error reported");

  info(`Got error: ${extension.errors[0]}`);

  ok(
    extension.errors[0].includes(
      "Loading locale file _locales/en-US/messages.json"
    ),
    "Got invalid default_locale error"
  );

  await extension.initAllLocales();

  equal(extension.errors.length, 2, "One error reported");

  info(`Got error: ${extension.errors[1]}`);

  ok(
    extension.errors[1].includes('"default_locale" property must correspond'),
    "Got unexpected default_locale error"
  );
});

add_task(async function testInvalidSyntax() {
  let extension = await generateAddon({
    manifest: {
      default_locale: "en_US",
    },

    files: {
      "_locales/en_US/messages.json":
        '{foo: {message: "bar", description: "baz"}}',
    },
  });

  equal(extension.errors.length, 1, "No errors reported");

  info(`Got error: ${extension.errors[0]}`);

  ok(
    extension.errors[0].includes(
      "Loading locale file _locales/en_US/messages.json: SyntaxError"
    ),
    "Got syntax error"
  );

  await extension.initAllLocales();

  equal(extension.errors.length, 2, "One error reported");

  info(`Got error: ${extension.errors[1]}`);

  ok(
    extension.errors[1].includes(
      "Loading locale file _locales/en_US/messages.json: SyntaxError"
    ),
    "Got syntax error"
  );
});

add_task(async function testExtractLocalizedManifest() {
  let extension = await generateAddon({
    manifest: {
      name: "__MSG_extensionName__",
      default_locale: "en_US",
      icons: {
        16: "__MSG_extensionIcon__",
      },
    },

    files: {
      "_locales/en_US/messages.json": `{
        "extensionName": {"message": "foo"},
        "extensionIcon": {"message": "icon-en.png"}
      }`,
      "_locales/de_DE/messages.json": `{
        "extensionName": {"message": "bar"},
        "extensionIcon": {"message": "icon-de.png"}
      }`,
    },
  });

  await extension.loadManifest();
  equal(extension.manifest.name, "foo", "name localized");
  equal(extension.manifest.icons["16"], "icon-en.png", "icons localized");

  let manifest = await extension.getLocalizedManifest("de-DE");
  ok(extension.localeData.has("de-DE"), "has de_DE locale");
  equal(manifest.name, "bar", "name localized");
  equal(manifest.icons["16"], "icon-de.png", "icons localized");

  await Assert.rejects(
    extension.getLocalizedManifest("xx-XX"),
    /does not contain the locale xx-XX/,
    "xx-XX does not exist"
  );
});

add_task(async function testRestartThenExtractLocalizedManifest() {
  await AddonTestUtils.promiseStartupManager();

  let wrapper = ExtensionTestUtils.loadExtension({
    manifest: {
      name: "__MSG_extensionName__",
      default_locale: "en_US",
    },
    useAddonManager: "permanent",
    files: {
      "_locales/en_US/messages.json": '{"extensionName": {"message": "foo"}}',
      "_locales/de_DE/messages.json": '{"extensionName": {"message": "bar"}}',
    },
  });

  await wrapper.startup();

  await AddonTestUtils.promiseRestartManager();
  await wrapper.startupPromise;

  let { extension } = wrapper;
  let manifest = await extension.getLocalizedManifest("de-DE");
  ok(extension.localeData.has("de-DE"), "has de_DE locale");
  equal(manifest.name, "bar", "name localized");

  await Assert.rejects(
    extension.getLocalizedManifest("xx-XX"),
    /does not contain the locale xx-XX/,
    "xx-XX does not exist"
  );

  await wrapper.unload();
  await AddonTestUtils.promiseShutdownManager();
});