Bug 1613705 - [localization] part5: Refactor tests to use new API model. r=platform-i18n-reviewers,dminor

Depends on D113247

Differential Revision: https://phabricator.services.mozilla.com/D113248
This commit is contained in:
Zibi Braniecki 2021-08-03 05:52:01 +00:00
Родитель 67ae94fecc
Коммит 7336dd1851
7 изменённых файлов: 51 добавлений и 97 удалений

Просмотреть файл

@ -11,6 +11,7 @@
#include "nsIObserver.h"
#include "nsWeakReference.h"
#include "nsWrapperCache.h"
#include "nsWeakReference.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/BindingDeclarations.h"

Просмотреть файл

@ -7,20 +7,24 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US", {
useIsolating: false,
});
bundle.addResource(new FluentResource(`
const mockSource = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}/", [
{
path: "/localization/en-US/mock.ftl",
source: `
key1 = Value
.title = Title 1
.accesskey = K
key2 =
.label = This is a label for { $user }
`));
yield bundle;
}
`
}
]);
let registry = new L10nRegistry({
bundleOptions: {
useIsolating: false
}
});
registry.registerSources([mockSource]);
function getAttributeByName(attributes, name) {
return attributes.find(attr => attr.name === name);
@ -32,7 +36,7 @@ key2 =
const loc = new Localization(
['mock.ftl'],
false,
{ generateBundles },
registry,
);
{
@ -68,8 +72,8 @@ key2 =
]);
ok(false, "Missing argument didn't cause an exception.");
} catch (e) {
is(e,
"[fluent][resolver] errors in en-US/key2: Resolver error: Unknown variable: $user.",
is(e.message,
"[fluent][resolver] errors in en-US/key2: Resolver error: Unknown variable: $user",
"Missing key causes an exception.");
}
}
@ -83,8 +87,8 @@ key2 =
]);
ok(false, "Missing key didn't cause an exception.");
} catch (e) {
is(e,
"[fluent] Missing translations in en-US: key4.",
is(e.message,
"[fluent] Missing message in locale en-US: key4",
"Missing key causes an exception.");
}
}

Просмотреть файл

@ -71,7 +71,7 @@ key3 = Value { $count }
ok(false, "Missing key didn't cause an exception.");
} catch (e) {
is(e.message,
"[fluent] Missing message: key4",
"[fluent] Missing message in locale en-US: key4",
"Missing key causes an exception.");
}
}

Просмотреть файл

@ -166,7 +166,7 @@ add_task(async function test_has_two_sources() {
* missing files as `false` instead of `undefined`.
*/
add_task(function test_indexed() {
let oneSource = new L10nFileSource("langpack-pl", ["pl"], "/data/locales/{locale}/", [
let oneSource = new L10nFileSource("langpack-pl", ["pl"], "/data/locales/{locale}/", {}, [
"/data/locales/pl/test.ftl",
]);
equal(oneSource.hasFile("pl", "test.ftl"), "present");

Просмотреть файл

@ -28,13 +28,9 @@ key-attr =
const source = L10nFileSource.createMock("test", ["de", "en-US"], "/localization/{locale}", fs);
l10nReg.registerSources([source]);
async function* generateBundles(resIds) {
yield * l10nReg.generateBundles(["de", "en-US"], resIds);
}
const l10n = new Localization([
"/browser/menu.ftl",
], false, { generateBundles });
], false, l10nReg, ["de", "en-US"]);
{
let values = await l10n.formatValues([
@ -84,9 +80,6 @@ key-attr =
strictEqual(messages[2].value, "[en] Value3");
strictEqual(messages[3].value, null);
}
l10nReg.clearSources();
Services.locale.requestedLocales = originalRequested;
});
add_task(async function test_builtins() {
@ -110,20 +103,14 @@ key = { PLATFORM() ->
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
l10nReg.registerSources([source]);
async function* generateBundles(resIds) {
yield * await l10nReg.generateBundles(["en-US"], resIds);
}
const l10n = new Localization([
"/test.ftl",
], false, { generateBundles });
], false, l10nReg, ["en-US"]);
let values = await l10n.formatValues([{id: "key"}]);
ok(values[0].includes(
`${ known_platforms[AppConstants.platform].toUpperCase() } Value`));
l10nReg.clearSources();
});
add_task(async function test_add_remove_resourceIds() {
@ -133,16 +120,11 @@ add_task(async function test_add_remove_resourceIds() {
{ path: "/localization/en-US/toolkit/menu.ftl", source: "key2 = Value2" },
];
const originalRequested = Services.locale.requestedLocales;
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
l10nReg.registerSources([source]);
async function* generateBundles(resIds) {
yield * await l10nReg.generateBundles(["en-US"], resIds);
}
const l10n = new Localization(["/browser/menu.ftl"], false, { generateBundles });
const l10n = new Localization(["/browser/menu.ftl"], false, l10nReg, ["en-US"]);
let values = await l10n.formatValues([{id: "key1"}, {id: "key2"}]);
@ -172,31 +154,20 @@ add_task(async function test_add_remove_resourceIds() {
strictEqual(values[0], null);
strictEqual(values[1], "Value2");
l10nReg.clearSources();
Services.locale.requestedLocales = originalRequested;
});
add_task(async function test_switch_to_async() {
const l10nReg = new L10nRegistry();
const fs = [
{ path: "/localization/en-US/browser/menu.ftl", source: "key1 = Value1" },
{ path: "/localization/en-US/toolkit/menu.ftl", source: "key2 = Value2" },
];
const originalRequested = Services.locale.requestedLocales;
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
l10nReg.registerSources([source]);
async function* generateBundles(resIds) {
yield * await l10nReg.generateBundles(["en-US"], resIds);
}
function* generateBundlesSync(resIds) {
yield * l10nReg.generateBundlesSync(["en-US"], resIds);
}
const l10n = new Localization(["/browser/menu.ftl"], false, { generateBundles, generateBundlesSync });
const l10n = new Localization(["/browser/menu.ftl"], false, l10nReg, ["en-US"]);
let values = await l10n.formatValues([{id: "key1"}, {id: "key2"}]);
@ -220,7 +191,4 @@ add_task(async function test_switch_to_async() {
strictEqual(values[0], null);
strictEqual(values[1], "Value2");
l10nReg.clearSources();
Services.locale.requestedLocales = originalRequested;
});

Просмотреть файл

@ -18,18 +18,13 @@ key-attr =
.label = [en] Label 3
` },
];
const originalRequested = Services.locale.requestedLocales;
const source = L10nFileSource.createMock("test", ["de", "en-US"], "/localization/{locale}", fs);
l10nReg.registerSources([source]);
function* generateBundlesSync(resIds) {
yield * l10nReg.generateBundlesSync(["de", "en-US"], resIds);
}
const l10n = new Localization([
"/browser/menu.ftl",
], true, { generateBundlesSync });
], true, l10nReg, ["de", "en-US"]);
{
@ -80,8 +75,6 @@ key-attr =
strictEqual(messages[2].value, "[en] Value3");
strictEqual(messages[3].value, null);
}
Services.locale.requestedLocales = originalRequested;
});
add_task(function test_builtins() {
@ -105,19 +98,14 @@ key = { PLATFORM() ->
const l10nReg = new L10nRegistry();
l10nReg.registerSources([source]);
function* generateBundlesSync(resIds) {
yield * l10nReg.generateBundlesSync(["en-US"], resIds);
}
const l10n = new Localization([
"/test.ftl",
], true, { generateBundlesSync });
], true, l10nReg, ["en-US"]);
let values = l10n.formatValuesSync([{id: "key"}]);
ok(values[0].includes(
`${ known_platforms[AppConstants.platform].toUpperCase() } Value`));
});
add_task(function test_add_remove_resourceIds() {
@ -131,11 +119,7 @@ add_task(function test_add_remove_resourceIds() {
const l10nReg = new L10nRegistry();
l10nReg.registerSources([source]);
function* generateBundlesSync(resIds) {
yield * l10nReg.generateBundlesSync(["en-US"], resIds);
}
const l10n = new Localization(["/browser/menu.ftl"], true, { generateBundlesSync });
const l10n = new Localization(["/browser/menu.ftl"], true, l10nReg, ["en-US"]);
let values = l10n.formatValuesSync([{id: "key1"}, {id: "key2"}]);
@ -165,8 +149,6 @@ add_task(function test_add_remove_resourceIds() {
strictEqual(values[0], null);
strictEqual(values[1], "Value2");
Services.locale.requestedLocales = originalRequested;
});
add_task(function test_calling_sync_methods_in_async_mode_fails() {

Просмотреть файл

@ -3,25 +3,26 @@
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const originalValues = {};
const originalValues = {
requested: Services.locale.requestedLocales,
};
const l10nReg = new L10nRegistry();
function addMockFileSource() {
const fs = [
{ path: "/localization/de/browser/menu.ftl", source: `
function getMockRegistry() {
const mockSource = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}/", [
{
path: "/localization/en-US/mock.ftl",
source: `
key = This is a single message
.tooltip = This is a tooltip
.accesskey = f` },
];
originalValues.requested = Services.locale.requestedLocales;
const source = L10nFileSource.createMock("test", ["de"], "/localization/{locale}", fs);
l10nReg.registerSources([source]);
return async function* generateMessages(resIds) {
yield * await l10nReg.generateBundles(["de"], resIds);
};
.tooltip = This is a tooltip
.accesskey = f
`
}
]);
let registry = new L10nRegistry();
registry.registerSources([mockSource]);
return registry;
}
function getAttributeByName(attributes, name) {
@ -40,11 +41,11 @@ function getAttributeByName(attributes, name) {
add_task(async function test_pseudo_works() {
Services.prefs.setStringPref("intl.l10n.pseudo", "");
let generateBundles = addMockFileSource();
let mockRegistry = getMockRegistry();
const l10n = new Localization([
"/browser/menu.ftl",
], false, { generateBundles });
"mock.ftl",
], false, mockRegistry);
{
// 1. Start with no pseudo
@ -97,7 +98,6 @@ add_task(async function test_pseudo_works() {
equal(attr1.value, "f");
}
l10nReg.clearSources();
Services.locale.requestedLocales = originalValues.requested;
});
@ -108,11 +108,11 @@ add_task(async function test_pseudo_works() {
add_task(async function test_unavailable_strategy_works() {
Services.prefs.setStringPref("intl.l10n.pseudo", "");
let generateBundles = addMockFileSource();
let mockRegistry = getMockRegistry();
const l10n = new Localization([
"/browser/menu.ftl",
], false, { generateBundles });
"mock.ftl",
], false, mockRegistry);
{
// 1. Set unavailable pseudo strategy
@ -128,6 +128,5 @@ add_task(async function test_unavailable_strategy_works() {
}
Services.prefs.setStringPref("intl.l10n.pseudo", "");
l10nReg.clearSources();
Services.locale.requestedLocales = originalValues.requested;
});