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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const { getAddonAndLocalAPIsMocker } = ChromeUtils.importESModule(
"resource://testing-common/LangPackMatcherTestUtils.sys.mjs"
);
const { LangPackMatcher } = ChromeUtils.importESModule(
"resource://gre/modules/LangPackMatcher.sys.mjs"
);
const { sinon } = ChromeUtils.import("resource://testing-common/Sinon.jsm");
const sandbox = sinon.createSandbox();
const mockAddonAndLocaleAPIs = getAddonAndLocalAPIsMocker(this, sandbox);
add_task(function initSandbox() {
registerCleanupFunction(() => {
sandbox.restore();
});
});
add_task(function test_appLocaleLanguageMismatch() {
sandbox.restore();
mockAddonAndLocaleAPIs({
systemLocale: "es-ES",
appLocale: "en-US",
});
deepEqual(LangPackMatcher.getAppAndSystemLocaleInfo(), {
systemLocaleRaw: "es-ES",
systemLocale: { baseName: "es-ES", language: "es", region: "ES" },
appLocaleRaw: "en-US",
appLocale: { baseName: "en-US", language: "en", region: "US" },
matchType: "language-mismatch",
canLiveReload: true,
displayNames: {
systemLanguage: "Español (ES)",
appLanguage: "English (US)",
},
});
});
add_task(function test_appLocaleRegionMismatch() {
sandbox.restore();
mockAddonAndLocaleAPIs({
sandbox,
systemLocale: "en-CA",
appLocale: "en-US",
});
deepEqual(LangPackMatcher.getAppAndSystemLocaleInfo(), {
systemLocaleRaw: "en-CA",
systemLocale: { baseName: "en-CA", language: "en", region: "CA" },
appLocaleRaw: "en-US",
appLocale: { baseName: "en-US", language: "en", region: "US" },
matchType: "region-mismatch",
canLiveReload: true,
displayNames: {
systemLanguage: "English (CA)",
appLanguage: "English (US)",
},
});
});
add_task(function test_appLocaleScriptMismatch() {
sandbox.restore();
// Script mismatch:
mockAddonAndLocaleAPIs({
sandbox,
systemLocale: "zh-Hans-CN",
appLocale: "zh-CN",
});
deepEqual(LangPackMatcher.getAppAndSystemLocaleInfo(), {
systemLocaleRaw: "zh-Hans-CN",
systemLocale: { baseName: "zh-Hans-CN", language: "zh", region: "CN" },
appLocaleRaw: "zh-CN",
appLocale: { baseName: "zh-CN", language: "zh", region: "CN" },
matchType: "match",
canLiveReload: true,
displayNames: {
systemLanguage: "Chinese (Hans, China)",
appLanguage: "简体中文",
},
});
});
add_task(function test_appLocaleInvalidSystem() {
sandbox.restore();
// Script mismatch:
mockAddonAndLocaleAPIs({
sandbox,
systemLocale: "Not valid",
appLocale: "en-US",
});
deepEqual(LangPackMatcher.getAppAndSystemLocaleInfo(), {
systemLocaleRaw: "Not valid",
systemLocale: null,
appLocaleRaw: "en-US",
appLocale: { baseName: "en-US", language: "en", region: "US" },
matchType: "unknown",
canLiveReload: null,
displayNames: { systemLanguage: null, appLanguage: "English (US)" },
});
});
add_task(function test_bidiSwitchDisabled() {
Services.prefs.setBoolPref(
"intl.multilingual.liveReloadBidirectional",
false
);
sandbox.restore();
// Script mismatch:
mockAddonAndLocaleAPIs({
sandbox,
systemLocale: "ar-EG",
appLocale: "en-US",
});
deepEqual(LangPackMatcher.getAppAndSystemLocaleInfo(), {
systemLocaleRaw: "ar-EG",
systemLocale: { baseName: "ar-EG", language: "ar", region: "EG" },
appLocaleRaw: "en-US",
appLocale: { baseName: "en-US", language: "en", region: "US" },
matchType: "language-mismatch",
canLiveReload: false,
displayNames: {
systemLanguage: "Arabic (Egypt)",
appLanguage: "English (US)",
},
});
Services.prefs.clearUserPref("intl.multilingual.liveReloadBidirectional");
});
add_task(async function test_bidiSwitchEnabled() {
Services.prefs.setBoolPref("intl.multilingual.liveReloadBidirectional", true);
sandbox.restore();
// Script mismatch:
mockAddonAndLocaleAPIs({
sandbox,
systemLocale: "ar-EG",
appLocale: "en-US",
});
deepEqual(LangPackMatcher.getAppAndSystemLocaleInfo(), {
systemLocaleRaw: "ar-EG",
systemLocale: { baseName: "ar-EG", language: "ar", region: "EG" },
appLocaleRaw: "en-US",
appLocale: { baseName: "en-US", language: "en", region: "US" },
matchType: "language-mismatch",
canLiveReload: true,
displayNames: {
systemLanguage: "Arabic (Egypt)",
appLanguage: "English (US)",
},
});
Services.prefs.clearUserPref("intl.multilingual.liveReloadBidirectional");
});
function shuffle(array) {
return array
.map(value => ({ value, sort: Math.random() }))
.sort((a, b) => a.sort - b.sort)
.map(({ value }) => value);
}
add_task(async function test_negotiateLangPacks() {
const negotiations = [
{
// Exact match found.
systemLocale: "en-US",
availableLangPacks: ["en", "en-US", "zh", "zh-CN", "zh-Hans-CN"],
expectedLangPack: "en-US",
expectedDisplayName: "English (US)",
},
{
// Region-less match.
systemLocale: "en-CA",
availableLangPacks: ["en", "en-US", "zh", "zh-CN", "zh-Hans-CN"],
expectedLangPack: "en",
expectedDisplayName: "English",
},
{
// Fallback to a different region.
systemLocale: "en-CA",
availableLangPacks: ["en-US", "zh", "zh-CN", "zh-Hans-CN"],
expectedLangPack: "en-US",
expectedDisplayName: "English (US)",
},
{
// Match with a script. zh-Hans-CN is the locale used with simplified
// Chinese scripts, while zh-CN uses the Latin script.
systemLocale: "zh-Hans-CN",
availableLangPacks: ["en", "en-US", "zh", "zh-CN", "zh-Hans-CN"],
expectedLangPack: "zh-Hans-CN",
expectedDisplayName: "Chinese (Hans, China)",
},
{
// No reasonable match could be found.
systemLocale: "tlh", // Klingon
availableLangPacks: ["en", "en-US", "zh", "zh-CN", "zh-Hans-CN"],
expectedLangPack: null,
expectedDisplayName: null,
},
{
// Weird, but valid locale identifiers.
systemLocale: "en-US-u-hc-h23-ca-islamic-civil-ss-true",
availableLangPacks: ["en", "en-US", "zh", "zh-CN", "zh-Hans-CN"],
expectedLangPack: "en-US",
expectedDisplayName: "English (US)",
},
{
// Invalid system locale
systemLocale: "Not valid",
availableLangPacks: ["en", "en-US", "zh", "zh-CN", "zh-Hans-CN"],
expectedLangPack: null,
expectedDisplayName: null,
},
];
for (const {
systemLocale,
availableLangPacks,
expectedLangPack,
expectedDisplayName,
} of negotiations) {
sandbox.restore();
const { resolveLangPacks } = mockAddonAndLocaleAPIs({
sandbox,
systemLocale,
});
const promise = LangPackMatcher.negotiateLangPackForLanguageMismatch();
// Shuffle the order to ensure that this test doesn't require on ordering of the
// langpack responses.
resolveLangPacks(shuffle(availableLangPacks));
const { langPack, langPackDisplayName } = await promise;
equal(
langPack?.target_locale,
expectedLangPack,
`Resolve the systemLocale "${systemLocale}" with available langpacks: ${JSON.stringify(
availableLangPacks
)}`
);
equal(
langPackDisplayName,
expectedDisplayName,
"The display name matches."
);
}
});
add_task(async function test_ensureLangPackInstalled() {
sandbox.restore();
const { resolveLangPacks, resolveInstaller } = mockAddonAndLocaleAPIs({
sandbox,
systemLocale: "es-ES",
appLocale: "en-US",
});
const negotiatePromise = LangPackMatcher.negotiateLangPackForLanguageMismatch();
resolveLangPacks(["es-ES"]);
const { langPack } = await negotiatePromise;
const installPromise1 = LangPackMatcher.ensureLangPackInstalled(langPack);
const installPromise2 = LangPackMatcher.ensureLangPackInstalled(langPack);
resolveInstaller(["fake langpack"]);
info("Ensure both installers resolve when called twice in a row.");
await installPromise1;
await installPromise2;
ok(true, "Both were called.");
});
|