summaryrefslogtreecommitdiffstats
path: root/intl/l10n/test/test_l10nregistry.js
blob: cbbb1e7316478816a3a42526a853b8655f71cec8 (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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/* Any copyrighequal dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

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

const l10nReg = new L10nRegistry();

add_task(function test_methods_presence() {
  equal(typeof l10nReg.generateBundles, "function");
  equal(typeof l10nReg.generateBundlesSync, "function");
  equal(typeof l10nReg.getAvailableLocales, "function");
  equal(typeof l10nReg.registerSources, "function");
  equal(typeof l10nReg.removeSources, "function");
  equal(typeof l10nReg.updateSources, "function");
});

/**
 * Test that passing empty resourceIds list works.
 */
add_task(async function test_empty_resourceids() {
  const fs = [];

  const source = L10nFileSource.createMock("test", "", ["en-US"], "/localization/{locale}", fs);
  l10nReg.registerSources([source]);

  const bundles = l10nReg.generateBundles(["en-US"], []);

  const done = (await bundles.next()).done;

  equal(done, true);

  // cleanup
  l10nReg.clearSources();
});

/**
 * Test that passing empty sources list works.
 */
add_task(async function test_empty_sources() {
  const fs = [];
  const bundles = l10nReg.generateBundlesSync(["en-US"], fs);

  const done = (await bundles.next()).done;

  equal(done, true);

  // cleanup
  l10nReg.clearSources();
});

/**
 * This test tests generation of a proper context for a single
 * source scenario
 */
add_task(async function test_methods_calling() {
  const fs = [
    { path: "/localization/en-US/browser/menu.ftl", source: "key = Value" }
  ];
  const source = L10nFileSource.createMock("test", "", ["en-US"], "/localization/{locale}", fs);
  l10nReg.registerSources([source]);

  const bundles = l10nReg.generateBundles(["en-US"], ["/browser/menu.ftl"]);

  const bundle = (await bundles.next()).value;

  equal(bundle.hasMessage("key"), true);

  // cleanup
  l10nReg.clearSources();
});

/**
 * This test verifies that the public methods return expected values
 * for the single source scenario
 */
add_task(async function test_has_one_source() {
  const fs = [
    {path: "./app/data/locales/en-US/test.ftl", source: "key = value en-US"}
  ];
  let oneSource = L10nFileSource.createMock("app", "", ["en-US"], "./app/data/locales/{locale}/", fs);
  l10nReg.registerSources([oneSource]);


  // has one source

  equal(l10nReg.getSourceNames().length, 1);
  equal(l10nReg.hasSource("app"), true);


  // returns a single context

  let bundles = l10nReg.generateBundles(["en-US"], ["test.ftl"]);
  let bundle0 = (await bundles.next()).value;
  equal(bundle0.hasMessage("key"), true);

  equal((await bundles.next()).done, true);


  // returns no contexts for missing locale

  bundles = l10nReg.generateBundles(["pl"], ["test.ftl"]);

  equal((await bundles.next()).done, true);

  // cleanup
  l10nReg.clearSources();
});

/**
 * This test verifies that public methods return expected values
 * for the dual source scenario.
 */
add_task(async function test_has_two_sources() {
  const fs = [
    { path: "./platform/data/locales/en-US/test.ftl", source: "key = platform value" },
    { path: "./app/data/locales/pl/test.ftl", source: "key = app value" }
  ];
  let oneSource = L10nFileSource.createMock("platform", "", ["en-US"], "./platform/data/locales/{locale}/", fs);
  let secondSource = L10nFileSource.createMock("app", "", ["pl"], "./app/data/locales/{locale}/", fs);
  l10nReg.registerSources([oneSource, secondSource]);

  // has two sources

  equal(l10nReg.getSourceNames().length, 2);
  equal(l10nReg.hasSource("app"), true);
  equal(l10nReg.hasSource("platform"), true);


  // returns correct contexts for en-US

  let bundles = l10nReg.generateBundles(["en-US"], ["test.ftl"]);
  let bundle0 = (await bundles.next()).value;

  equal(bundle0.hasMessage("key"), true);
  let msg = bundle0.getMessage("key");
  equal(bundle0.formatPattern(msg.value), "platform value");

  equal((await bundles.next()).done, true);


  // returns correct contexts for [pl, en-US]

  bundles = l10nReg.generateBundles(["pl", "en-US"], ["test.ftl"]);
  bundle0 = (await bundles.next()).value;
  equal(bundle0.locales[0], "pl");
  equal(bundle0.hasMessage("key"), true);
  let msg0 = bundle0.getMessage("key");
  equal(bundle0.formatPattern(msg0.value), "app value");

  let bundle1 = (await bundles.next()).value;
  equal(bundle1.locales[0], "en-US");
  equal(bundle1.hasMessage("key"), true);
  let msg1 = bundle1.getMessage("key");
  equal(bundle1.formatPattern(msg1.value), "platform value");

  equal((await bundles.next()).done, true);

  // cleanup
  l10nReg.clearSources();
});

/**
 * This test verifies that behavior specific to the L10nFileSource
 * works correctly.
 *
 * In particular it tests that L10nFileSource correctly returns
 * missing files as `false` instead of `undefined`.
 */
add_task(function test_indexed() {
  let oneSource = new L10nFileSource("langpack-pl", "app", ["pl"], "/data/locales/{locale}/", {}, [
    "/data/locales/pl/test.ftl",
  ]);
  equal(oneSource.hasFile("pl", "test.ftl"), "present");
  equal(oneSource.hasFile("pl", "missing.ftl"), "missing");
});

/**
 * This test checks if the correct order of contexts is used for
 * scenarios where a new file source is added on top of the default one.
 */
add_task(async function test_override() {
  const fs = [
    { path: "/app/data/locales/pl/test.ftl", source: "key = value" },
    { path: "/data/locales/pl/test.ftl", source: "key = addon value"},
  ];
  let fileSource = L10nFileSource.createMock("app", "", ["pl"], "/app/data/locales/{locale}/", fs);
  let oneSource = L10nFileSource.createMock("langpack-pl", "", ["pl"], "/data/locales/{locale}/", fs);
  l10nReg.registerSources([fileSource, oneSource]);

  equal(l10nReg.getSourceNames().length, 2);
  equal(l10nReg.hasSource("langpack-pl"), true);

  let bundles = l10nReg.generateBundles(["pl"], ["test.ftl"]);
  let bundle0 = (await bundles.next()).value;
  equal(bundle0.locales[0], "pl");
  equal(bundle0.hasMessage("key"), true);
  let msg0 = bundle0.getMessage("key");
  equal(bundle0.formatPattern(msg0.value), "addon value");

  let bundle1 = (await bundles.next()).value;
  equal(bundle1.locales[0], "pl");
  equal(bundle1.hasMessage("key"), true);
  let msg1 = bundle1.getMessage("key");
  equal(bundle1.formatPattern(msg1.value), "value");

  equal((await bundles.next()).done, true);

  // cleanup
  l10nReg.clearSources();
});

/**
 * This test verifies that new contexts are returned
 * after source update.
 */
add_task(async function test_updating() {
  const fs = [
    { path: "/data/locales/pl/test.ftl", source: "key = value" }
  ];
  let oneSource = L10nFileSource.createMock("langpack-pl", "", ["pl"], "/data/locales/{locale}/", fs);
  l10nReg.registerSources([oneSource]);

  let bundles = l10nReg.generateBundles(["pl"], ["test.ftl"]);
  let bundle0 = (await bundles.next()).value;
  equal(bundle0.locales[0], "pl");
  equal(bundle0.hasMessage("key"), true);
  let msg0 = bundle0.getMessage("key");
  equal(bundle0.formatPattern(msg0.value), "value");


  const newSource = L10nFileSource.createMock("langpack-pl", "", ["pl"], "/data/locales/{locale}/", [
    { path: "/data/locales/pl/test.ftl", source: "key = new value" }
  ]);
  l10nReg.updateSources([newSource]);

  equal(l10nReg.getSourceNames().length, 1);
  bundles = l10nReg.generateBundles(["pl"], ["test.ftl"]);
  bundle0 = (await bundles.next()).value;
  msg0 = bundle0.getMessage("key");
  equal(bundle0.formatPattern(msg0.value), "new value");

  // cleanup
  l10nReg.clearSources();
});

/**
 * This test verifies that generated contexts return correct values
 * after sources are being removed.
 */
add_task(async function test_removing() {
  const fs = [
    { path: "/app/data/locales/pl/test.ftl", source: "key = value" },
    { path: "/data/locales/pl/test.ftl", source: "key = addon value" },
  ];

  let fileSource = L10nFileSource.createMock("app", "", ["pl"], "/app/data/locales/{locale}/", fs);
  let oneSource = L10nFileSource.createMock("langpack-pl", "", ["pl"], "/data/locales/{locale}/", fs);
  l10nReg.registerSources([fileSource, oneSource]);

  equal(l10nReg.getSourceNames().length, 2);
  equal(l10nReg.hasSource("langpack-pl"), true);

  let bundles = l10nReg.generateBundles(["pl"], ["test.ftl"]);
  let bundle0 = (await bundles.next()).value;
  equal(bundle0.locales[0], "pl");
  equal(bundle0.hasMessage("key"), true);
  let msg0 = bundle0.getMessage("key");
  equal(bundle0.formatPattern(msg0.value), "addon value");

  let bundle1 = (await bundles.next()).value;
  equal(bundle1.locales[0], "pl");
  equal(bundle1.hasMessage("key"), true);
  let msg1 = bundle1.getMessage("key");
  equal(bundle1.formatPattern(msg1.value), "value");

  equal((await bundles.next()).done, true);

  // Remove langpack

  l10nReg.removeSources(["langpack-pl"]);

  equal(l10nReg.getSourceNames().length, 1);
  equal(l10nReg.hasSource("langpack-pl"), false);

  bundles = l10nReg.generateBundles(["pl"], ["test.ftl"]);
  bundle0 = (await bundles.next()).value;
  equal(bundle0.locales[0], "pl");
  equal(bundle0.hasMessage("key"), true);
  msg0 = bundle0.getMessage("key");
  equal(bundle0.formatPattern(msg0.value), "value");

  equal((await bundles.next()).done, true);

  // Remove app source

  l10nReg.removeSources(["app"]);

  equal(l10nReg.getSourceNames().length, 0);

  bundles = l10nReg.generateBundles(["pl"], ["test.ftl"]);
  equal((await bundles.next()).done, true);

  // cleanup
  l10nReg.clearSources();
});

/**
 * This test verifies that the logic works correctly when there's a missing
 * file in the FileSource scenario.
 */
add_task(async function test_missing_file() {
  const fs = [
    { path: "./app/data/locales/en-US/test.ftl", source: "key = value en-US" },
    { path: "./platform/data/locales/en-US/test.ftl", source: "key = value en-US" },
    { path: "./platform/data/locales/en-US/test2.ftl", source: "key2 = value2 en-US" },
  ];
  let oneSource = L10nFileSource.createMock("app", "", ["en-US"], "./app/data/locales/{locale}/", fs);
  let twoSource = L10nFileSource.createMock("platform", "", ["en-US"], "./platform/data/locales/{locale}/", fs);
  l10nReg.registerSources([oneSource, twoSource]);

  // has two sources

  equal(l10nReg.getSourceNames().length, 2);
  equal(l10nReg.hasSource("app"), true);
  equal(l10nReg.hasSource("platform"), true);


  // returns a single context

  let bundles = l10nReg.generateBundles(["en-US"], ["test.ftl", "test2.ftl"]);

  // First permutation:
  //   [platform, platform] - both present
  let bundle1 = (await bundles.next());
  equal(bundle1.value.hasMessage("key"), true);

  // Second permutation skipped:
  //   [platform, app] - second missing
  // Third permutation:
  //   [app, platform] - both present
  let bundle2 = (await bundles.next());
  equal(bundle2.value.hasMessage("key"), true);

  // Fourth permutation skipped:
  //   [app, app] - second missing
  equal((await bundles.next()).done, true);

  // cleanup
  l10nReg.clearSources();
});

add_task(async function test_hasSource() {
  equal(l10nReg.hasSource("nonsense"), false, "Non-existing source doesn't exist");
  equal(l10nReg.hasSource("app"), false, "hasSource returns true before registering a source");
  let oneSource = new L10nFileSource("app", "app", ["en-US"], "/{locale}/");
  l10nReg.registerSources([oneSource]);
  equal(l10nReg.hasSource("app"), true, "hasSource returns true after registering a source");
  l10nReg.clearSources();
});

/**
 * This test verifies that we handle correctly a scenario where a source
 * is being removed while the iterator operates.
 */
add_task(async function test_remove_source_mid_iter_cycle() {
  const fs = [
    { path: "./platform/data/locales/en-US/test.ftl", source: "key = platform value" },
    { path: "./app/data/locales/pl/test.ftl", source: "key = app value" },
  ];
  let oneSource = L10nFileSource.createMock("platform", "", ["en-US"], "./platform/data/locales/{locale}/", fs);
  let secondSource = L10nFileSource.createMock("app", "", ["pl"], "./app/data/locales/{locale}/", fs);
  l10nReg.registerSources([oneSource, secondSource]);

  let bundles = l10nReg.generateBundles(["en-US", "pl"], ["test.ftl"]);

  let bundle0 = await bundles.next();

  // The registry has a copy of the file sources, so it will be unaffected.
  l10nReg.removeSources(["app"]);

  let bundle1 = await bundles.next();

  equal((await bundles.next()).done, true);

  // cleanup
  l10nReg.clearSources();
});

add_task(async function test_metasources() {
  let fs = [
    { path: "/localization/en-US/browser/menu1.ftl", source: "key1 = Value" },
    { path: "/localization/en-US/browser/menu2.ftl", source: "key2 = Value" },
    { path: "/localization/en-US/browser/menu3.ftl", source: "key3 = Value" },
    { path: "/localization/en-US/browser/menu4.ftl", source: "key4 = Value" },
    { path: "/localization/en-US/browser/menu5.ftl", source: "key5 = Value" },
    { path: "/localization/en-US/browser/menu6.ftl", source: "key6 = Value" },
    { path: "/localization/en-US/browser/menu7.ftl", source: "key7 = Value" },
    { path: "/localization/en-US/browser/menu8.ftl", source: "key8 = Value" },
  ];

  const browser = L10nFileSource.createMock("browser", "app", ["en-US"], "/localization/{locale}", fs);
  const toolkit = L10nFileSource.createMock("toolkit", "app", ["en-US"], "/localization/{locale}", fs);
  const browser2 = L10nFileSource.createMock("browser2", "langpack", ["en-US"], "/localization/{locale}", fs);
  const toolkit2 = L10nFileSource.createMock("toolkit2", "langpack", ["en-US"], "/localization/{locale}", fs);
  l10nReg.registerSources([toolkit, browser, toolkit2, browser2]);

  let res = [
    "/browser/menu1.ftl",
    "/browser/menu2.ftl",
    "/browser/menu3.ftl",
    "/browser/menu4.ftl",
    "/browser/menu5.ftl",
    "/browser/menu6.ftl",
    "/browser/menu7.ftl",
    {path: "/browser/menu8.ftl", optional: false},
  ];

  const bundles = l10nReg.generateBundles(["en-US"], res);

  let nbundles = 0;
  while (!(await bundles.next()).done) {
    nbundles += 1;
  }

  // If metasources are working properly, we'll generate 2^8 = 256 bundles for
  // each metasource giving 512 bundles in total. Otherwise, we generate
  // 4^8 = 65536 bundles.
  equal(nbundles, 512);

  // cleanup
  l10nReg.clearSources();
});

/**
 * This test verifies that when a required resource is missing for a locale,
 * we do not produce a bundle for that locale.
 */
add_task(async function test_missing_required_resource() {
  const fs = [
    { path: "./platform/data/locales/en-US/test.ftl", source: "test-key = en-US value" },
    { path: "./platform/data/locales/pl/missing-in-en-US.ftl", source: "missing-key = pl value" },
    { path: "./platform/data/locales/pl/test.ftl", source: "test-key = pl value" },
  ];
  let source = L10nFileSource.createMock("platform", "", ["en-US", "pl"], "./platform/data/locales/{locale}/", fs);
  l10nReg.registerSources([source]);

  equal(l10nReg.getSourceNames().length, 1);
  equal(l10nReg.hasSource("platform"), true);


  // returns correct contexts for [en-US, pl]

  let bundles = l10nReg.generateBundlesSync(["en-US", "pl"], ["test.ftl", "missing-in-en-US.ftl"]);
  let bundle0 = (await bundles.next()).value;

  equal(bundle0.locales[0], "pl");
  equal(bundle0.hasMessage("test-key"), true);
  equal(bundle0.hasMessage("missing-key"), true);

  let msg0 = bundle0.getMessage("test-key");
  equal(bundle0.formatPattern(msg0.value), "pl value");

  let msg1 = bundle0.getMessage("missing-key");
  equal(bundle0.formatPattern(msg1.value), "pl value");

  equal((await bundles.next()).done, true);


  // returns correct contexts for [pl, en-US]

  bundles = l10nReg.generateBundlesSync(["pl", "en-US"], ["test.ftl", {path: "missing-in-en-US.ftl", optional: false}]);
  bundle0 = (await bundles.next()).value;

  equal(bundle0.locales[0], "pl");
  equal(bundle0.hasMessage("test-key"), true);

  msg0 = bundle0.getMessage("test-key");
  equal(bundle0.formatPattern(msg0.value), "pl value");

  msg1 = bundle0.getMessage("missing-key");
  equal(bundle0.formatPattern(msg1.value), "pl value");

  equal((await bundles.next()).done, true);

  // cleanup
  l10nReg.clearSources();
});

/**
 * This test verifies that when an optional resource is missing, we continue
 * to produce a bundle for that locale. The bundle will have missing entries
 * with regard to the missing optional resource.
 */
add_task(async function test_missing_optional_resource() {
  const fs = [
    { path: "./platform/data/locales/en-US/test.ftl", source: "test-key = en-US value" },
    { path: "./platform/data/locales/pl/missing-in-en-US.ftl", source: "missing-key = pl value" },
    { path: "./platform/data/locales/pl/test.ftl", source: "test-key = pl value" },
  ];
  let source = L10nFileSource.createMock("platform", "", ["en-US", "pl"], "./platform/data/locales/{locale}/", fs);
  l10nReg.registerSources([source]);

  equal(l10nReg.getSourceNames().length, 1);
  equal(l10nReg.hasSource("platform"), true);


  // returns correct contexts for [en-US, pl]

  let bundles = l10nReg.generateBundlesSync(["en-US", "pl"], ["test.ftl", { path: "missing-in-en-US.ftl", optional: true }]);
  let bundle0 = (await bundles.next()).value;

  equal(bundle0.locales[0], "en-US");
  equal(bundle0.hasMessage("test-key"), true);
  equal(bundle0.hasMessage("missing-key"), false);

  let msg0 = bundle0.getMessage("test-key");
  equal(bundle0.formatPattern(msg0.value), "en-US value");

  equal(bundle0.getMessage("missing-key"), null);

  let bundle1 = (await bundles.next()).value;

  equal(bundle1.locales[0], "pl");
  equal(bundle1.hasMessage("test-key"), true);
  equal(bundle1.hasMessage("missing-key"), true);

  msg0 = bundle1.getMessage("test-key");
  equal(bundle1.formatPattern(msg0.value), "pl value");

  msg1 = bundle1.getMessage("missing-key");
  equal(bundle1.formatPattern(msg1.value), "pl value");

  equal((await bundles.next()).done, true);

  // returns correct contexts for [pl, en-US]

  bundles = l10nReg.generateBundlesSync(["pl", "en-US"], ["test.ftl", { path: "missing-in-en-US.ftl", optional: true }]);
  bundle0 = (await bundles.next()).value;

  equal(bundle0.locales[0], "pl");
  equal(bundle0.hasMessage("test-key"), true);
  equal(bundle0.hasMessage("missing-key"), true);

  msg0 = bundle0.getMessage("test-key");
  equal(bundle0.formatPattern(msg0.value), "pl value");

  msg1 = bundle0.getMessage("missing-key");
  equal(bundle0.formatPattern(msg1.value), "pl value");

  bundle1 = (await bundles.next()).value;

  equal(bundle1.locales[0], "en-US");
  equal(bundle1.hasMessage("test-key"), true);
  equal(bundle1.hasMessage("missing-key"), false);

  msg0 = bundle1.getMessage("test-key");
  equal(bundle1.formatPattern(msg0.value), "en-US value");

  equal(bundle1.getMessage("missing-key"), null);

  // cleanup
  l10nReg.clearSources();
});