Bug 1621674 - Update tests to use Localization WebIDL. r=jfkthame,Mardak

Differential Revision: https://phabricator.services.mozilla.com/D66549

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Zibi Braniecki 2020-04-10 23:35:58 +00:00
parent 244ca91df9
commit a7712e985a
25 changed files with 95 additions and 73 deletions

View File

@ -55,30 +55,36 @@ describe("RemoteL10n", () => {
assert.calledOnce(domL10nStub);
const { args } = domL10nStub.firstCall;
// The first arg is the resource array, and the second one is the bundle generator.
assert.equal(args.length, 2);
// The first arg is the resource array,
// the second one is false (use async),
// and the third one is the bundle generator.
assert.equal(args.length, 3);
assert.deepEqual(args[0], [
"browser/newtab/asrouter.ftl",
"browser/branding/brandings.ftl",
"browser/branding/sync-brand.ftl",
"branding/brand.ftl",
]);
assert.isFunction(args[1]);
assert.isFalse(args[1]);
assert.isFunction(args[2].generateBundles);
});
it("should load the local Fluent file if USE_REMOTE_L10N_PREF is false", () => {
sandbox.stub(global.Services.prefs, "getBoolPref").returns(false);
RemoteL10n._createDOML10n();
const { args } = domL10nStub.firstCall;
// The first arg is the resource array, and the second one should be null.
assert.equal(args.length, 2);
// The first arg is the resource array,
// the second one is false (use async),
// and the third one is null.
assert.equal(args.length, 3);
assert.deepEqual(args[0], [
"browser/newtab/asrouter.ftl",
"browser/branding/brandings.ftl",
"browser/branding/sync-brand.ftl",
"branding/brand.ftl",
]);
assert.isUndefined(args[1]);
assert.isFalse(args[1]);
assert.isEmpty(args[2]);
});
});
});

View File

@ -7,7 +7,7 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource(`
key1 = Value for Key 1
@ -21,7 +21,8 @@ key2 = Value for <a>Key 2<a/>.
addLoadEvent(async () => {
const domLoc = new DOMLocalization(
[],
mockGenerateMessages
false,
{ generateBundles },
);
await domLoc.translateFragment(document.body);

View File

@ -7,7 +7,7 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource(`
key1 = Value for Key 1
@ -22,7 +22,8 @@ key1 = Value for Key 1
const domLoc = new DOMLocalization(
[],
mockGenerateMessages
false,
{ generateBundles },
);
await domLoc.translateRoots();

View File

@ -44,7 +44,7 @@
</script>
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource(`
key1 = Value for Key 1
@ -54,7 +54,8 @@ key1 = Value for Key 1
document.domLoc = new DOMLocalization(
[],
mockGenerateMessages
false,
{ generateBundles }
);
</script>
</head>

View File

@ -7,7 +7,7 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource(`
key1 = Value for Key 1
@ -23,7 +23,8 @@ key2 = Value for Key 2
const domLoc = new DOMLocalization(
[],
mockGenerateMessages
false,
{ generateBundles },
);
await domLoc.translateRoots();

View File

@ -10,7 +10,7 @@
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js" />
<script type="application/javascript">
<![CDATA[
async function * generateMessages(resourceIds) {
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource(`
file-menu =
@ -29,7 +29,8 @@ container = Some text with an <image data-l10n-name="foo"> inside it.
const domLoc = new DOMLocalization(
[],
generateMessages
false,
{ generateBundles },
);
async function foo() {

View File

@ -8,14 +8,15 @@
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {}
async function* generateBundles(resourceIds) {}
window.onload = function() {
SimpleTest.waitForExplicitFinish();
const domLoc = new DOMLocalization(
[],
mockGenerateMessages
false,
{ generateBundles },
);
const p1 = document.querySelectorAll("p")[0];

View File

@ -7,7 +7,7 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource("title = Hello World"));
bundle.addResource(new FluentResource("title2 = Hello Another World"));
@ -19,7 +19,8 @@
const domLoc = new DOMLocalization(
[],
mockGenerateMessages
false,
{ generateBundles },
);
const h1 = document.querySelectorAll("h1")[0];

View File

@ -7,7 +7,7 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource("title = <strong>Hello</strong> World"));
bundle.addResource(new FluentResource(`title2 = This is <a data-l10n-name="link">a link</a>!`));
@ -19,7 +19,8 @@
const domLoc = new DOMLocalization(
[],
mockGenerateMessages
false,
{ generateBundles },
);
const p1 = document.querySelectorAll("p")[0];

View File

@ -7,7 +7,7 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
// No translations!
yield bundle;
@ -17,7 +17,8 @@
addLoadEvent(async () => {
const domLoc = new DOMLocalization(
[],
mockGenerateMessages
false,
{ generateBundles },
);
await domLoc.translateFragment(document.body).then(() => {

View File

@ -7,7 +7,7 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource(`title = Visit <a data-l10n-name="mozilla-link">Mozilla</a> or <a data-l10n-name="firefox-link">Firefox</a> website!`));
yield bundle;
@ -18,7 +18,8 @@
const domLoc = new DOMLocalization(
[],
mockGenerateMessages
false,
{ generateBundles },
);
await domLoc.translateFragment(document.body);

View File

@ -7,7 +7,7 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource(`title = Visit <a data-l10n-name="mozilla-link">Mozilla</a> or <a data-l10n-name="firefox-link">Firefox</a> website!`));
yield bundle;
@ -18,7 +18,8 @@
const domLoc = new DOMLocalization(
[],
mockGenerateMessages
false,
{ generateBundles },
);
await domLoc.translateFragment(document.body);

View File

@ -7,7 +7,7 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource(`
key1 =
@ -22,7 +22,8 @@ key2 =
async function test() {
const domLoc = new DOMLocalization(
[],
mockGenerateMessages
false,
{ generateBundles },
);
await domLoc.translateFragment(document.body);

View File

@ -7,7 +7,7 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource(`
key1 = Translation For Key 1
@ -21,7 +21,8 @@ key2 = Visit <a data-l10n-name="link">this link<a/>.
addLoadEvent(async () => {
const domLoc = new DOMLocalization(
[],
mockGenerateMessages
false,
{ generateBundles },
);
await domLoc.translateFragment(document.body);

View File

@ -8,14 +8,15 @@
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {}
async function* generateBundles(resourceIds) {}
window.onload = function() {
SimpleTest.waitForExplicitFinish();
const domLoc = new DOMLocalization(
[],
mockGenerateMessages
false,
{ generateBundles },
);
const p1 = document.querySelectorAll("p")[0];

View File

@ -7,7 +7,7 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource("title = Hello World"));
bundle.addResource(new FluentResource("link =\n .title = Click me"));
@ -19,7 +19,8 @@
const domLoc = new DOMLocalization(
[],
mockGenerateMessages
false,
{ generateBundles },
);
const p1 = document.querySelectorAll("p")[0];

View File

@ -7,7 +7,7 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource("title = Hello World"));
bundle.addResource(new FluentResource("subtitle = Welcome to FluentBundle"));
@ -19,7 +19,8 @@
const domLoc = new DOMLocalization(
[],
mockGenerateMessages
false,
{ generateBundles },
);
const frag = document.querySelectorAll("div")[0];

View File

@ -7,7 +7,7 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource("title = Hello World"));
bundle.addResource(new FluentResource("title2 = Hello Another World"));
@ -19,7 +19,8 @@
const domLoc = new DOMLocalization(
[],
mockGenerateMessages
false,
{ generateBundles },
);
const frag1 = document.querySelectorAll("div")[0];

View File

@ -7,7 +7,7 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US");
bundle.addResource(new FluentResource(`
key1 = Key 1
@ -20,7 +20,8 @@ key4 = Key 4
document.domLoc = new DOMLocalization(
[],
mockGenerateMessages
false,
{ generateBundles }
);
document.domLoc.connectRoot(document.documentElement);
</script>

View File

@ -8,7 +8,7 @@
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US", {
useIsolating: false,
});
@ -31,7 +31,8 @@ key2 =
const loc = new Localization(
['mock.ftl'],
mockGenerateMessages,
false,
{ generateBundles },
);
{

View File

@ -7,7 +7,7 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US", {
useIsolating: false,
});
@ -24,7 +24,8 @@ key3 = Value { $count }
const loc = new Localization(
['mock.ftl'],
mockGenerateMessages,
false,
{ generateBundles },
);
{

View File

@ -7,7 +7,7 @@
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script type="application/javascript">
"use strict";
async function* mockGenerateMessages(resourceIds) {
async function* generateBundles(resourceIds) {
const bundle = new FluentBundle("en-US",
{
useIsolating: false,
@ -25,7 +25,8 @@ key3 = Value { $count }
const loc = new Localization(
['mock.ftl'],
mockGenerateMessages,
false,
{ generateBundles },
);
{

View File

@ -2,7 +2,6 @@
http://creativecommons.org/publicdomain/zero/1.0/ */
const { AppConstants } = ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
const { Localization } = ChromeUtils.import("resource://gre/modules/Localization.jsm");
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
add_task(function test_methods_presence() {
@ -29,13 +28,13 @@ add_task(async function test_methods_calling() {
const source = new FileSource("test", ["de", "en-US"], "/localization/{locale}");
L10nRegistry.registerSource(source);
async function* generateMessages(resIds) {
async function* generateBundles(resIds) {
yield * await L10nRegistry.generateBundles(["de", "en-US"], resIds);
}
const l10n = new Localization([
"/browser/menu.ftl",
], false, generateMessages);
], false, { generateBundles });
let values = await l10n.formatValues([{id: "key"}, {id: "key2"}]);
@ -75,13 +74,13 @@ key = { PLATFORM() ->
const source = new FileSource("test", ["en-US"], "/localization/{locale}");
L10nRegistry.registerSource(source);
async function* generateMessages(resIds) {
async function* generateBundles(resIds) {
yield * await L10nRegistry.generateBundles(["en-US"], resIds);
}
const l10n = new Localization([
"/test.ftl",
], false, generateMessages);
], false, { generateBundles });
let values = await l10n.formatValues([{id: "key"}]);
@ -110,11 +109,11 @@ add_task(async function test_add_remove_resourceIds() {
const source = new FileSource("test", ["en-US"], "/localization/{locale}");
L10nRegistry.registerSource(source);
async function* generateMessages(resIds) {
async function* generateBundles(resIds) {
yield * await L10nRegistry.generateBundles(["en-US"], resIds);
}
const l10n = new Localization(["/browser/menu.ftl"], false, generateMessages);
const l10n = new Localization(["/browser/menu.ftl"], false, { generateBundles });
let values = await l10n.formatValues([{id: "key1"}, {id: "key2"}]);
@ -168,15 +167,15 @@ add_task(async function test_switch_to_async() {
const source = new FileSource("test", ["en-US"], "/localization/{locale}");
L10nRegistry.registerSource(source);
async function* generateMessages(resIds) {
async function* generateBundles(resIds) {
yield * await L10nRegistry.generateBundles(["en-US"], resIds);
}
function* generateMessagesSync(resIds) {
function* generateBundlesSync(resIds) {
yield * L10nRegistry.generateBundlesSync(["en-US"], resIds);
}
const l10n = new Localization(["/browser/menu.ftl"], false, generateMessages, generateMessagesSync);
const l10n = new Localization(["/browser/menu.ftl"], false, { generateBundles, generateBundlesSync });
let values = await l10n.formatValues([{id: "key1"}, {id: "key2"}]);

View File

@ -2,7 +2,6 @@
http://creativecommons.org/publicdomain/zero/1.0/ */
const { AppConstants } = ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
const { Localization } = ChromeUtils.import("resource://gre/modules/Localization.jsm");
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
add_task(function test_methods_calling() {
@ -23,13 +22,13 @@ add_task(function test_methods_calling() {
const source = new FileSource("test", ["de", "en-US"], "/localization/{locale}");
L10nRegistry.registerSource(source);
function* generateMessagesSync(resIds) {
function* generateBundlesSync(resIds) {
yield * L10nRegistry.generateBundlesSync(["de", "en-US"], resIds);
}
const l10n = new Localization([
"/browser/menu.ftl",
], true, null, generateMessagesSync);
], true, { generateBundlesSync });
let values = l10n.formatValuesSync([{id: "key"}, {id: "key2"}]);
@ -69,13 +68,13 @@ key = { PLATFORM() ->
const source = new FileSource("test", ["en-US"], "/localization/{locale}");
L10nRegistry.registerSource(source);
function* generateMessagesSync(resIds) {
function* generateBundlesSync(resIds) {
yield * L10nRegistry.generateBundlesSync(["en-US"], resIds);
}
const l10n = new Localization([
"/test.ftl",
], true, null, generateMessagesSync);
], true, { generateBundlesSync });
let values = l10n.formatValuesSync([{id: "key"}]);
@ -104,11 +103,11 @@ add_task(function test_add_remove_resourceIds() {
const source = new FileSource("test", ["en-US"], "/localization/{locale}");
L10nRegistry.registerSource(source);
function* generateMessagesSync(resIds) {
function* generateBundlesSync(resIds) {
yield * L10nRegistry.generateBundlesSync(["en-US"], resIds);
}
const l10n = new Localization(["/browser/menu.ftl"], true, null, generateMessagesSync);
const l10n = new Localization(["/browser/menu.ftl"], true, { generateBundlesSync });
let values = l10n.formatValuesSync([{id: "key1"}, {id: "key2"}]);
@ -148,4 +147,4 @@ add_task(function test_calling_sync_methods_in_async_mode_fails() {
Assert.throws(() => {
l10n.formatMessagesSync([{ id: "key1"}]);
}, /Can't use sync formatWithFallback when state is async./);
});
});

View File

@ -1,7 +1,6 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
const { Localization } = ChromeUtils.import("resource://gre/modules/Localization.jsm");
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { L10nRegistry, FileSource } =
ChromeUtils.import("resource://gre/modules/L10nRegistry.jsm");
@ -46,12 +45,11 @@ function getAttributeByName(attributes, name) {
add_task(async function test_accented_works() {
Services.prefs.setStringPref("intl.l10n.pseudo", "");
let generateMessages = addMockFileSource();
let generateBundles = addMockFileSource();
const l10n = new Localization([
"/browser/menu.ftl",
], false, generateMessages);
l10n.registerObservers();
], false, { generateBundles });
{
// 1. Start with no pseudo
@ -116,12 +114,11 @@ add_task(async function test_accented_works() {
add_task(async function test_unavailable_strategy_works() {
Services.prefs.setStringPref("intl.l10n.pseudo", "");
let generateMessages = addMockFileSource();
let generateBundles = addMockFileSource();
const l10n = new Localization([
"/browser/menu.ftl",
], false, generateMessages);
l10n.registerObservers();
], false, { generateBundles });
{
// 1. Set unavailable pseudo strategy