Bug 1580599 Add a Fluent string helper to shared-head.js - r=nchevobbe

Differential Revision: https://phabricator.services.mozilla.com/D135358
This commit is contained in:
Greg Tatum 2022-01-10 17:09:17 +00:00
Родитель 302855c2a4
Коммит 81b009eef2
3 изменённых файлов: 78 добавлений и 15 удалений

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

@ -64,6 +64,12 @@ loader.lazyRequireGetter(
"devtools/client/responsive/manager"
);
loader.lazyRequireGetter(
this,
"FluentReact",
"devtools/client/shared/vendor/fluent-react"
);
const TEST_DIR = gTestPath.substr(0, gTestPath.lastIndexOf("/"));
const CHROME_URL_ROOT = TEST_DIR + "/";
const URL_ROOT = CHROME_URL_ROOT.replace(
@ -1899,3 +1905,45 @@ const waitForPresShell = function(context) {
}, "Waiting for a valid presShell");
});
};
/**
* In tests using Fluent localization, it is preferable to match DOM elements using
* a message ID rather than the raw string as:
*
* 1. It allows testing infrastructure to be multilingual if needed.
* 2. It isolates the tests from localization changes.
*
* @param {Array<string>} resourceIds A list of .ftl files to load.
* @returns {(id: string, args?: Record<string, FluentVariable>) => string}
*/
async function getFluentStringHelper(resourceIds) {
const locales = Services.locale.appLocalesAsBCP47;
const generator = L10nRegistry.getInstance().generateBundles(
locales,
resourceIds
);
const bundles = [];
for await (const bundle of generator) {
bundles.push(bundle);
}
const reactLocalization = new FluentReact.ReactLocalization(bundles);
/**
* Get the string from a message id. It throws when the message is not found.
*
* @param {string} id
* @param {Record<string, FluentVariable>} [args] optional
* @returns {string}
*/
return (id, args) => {
const string = reactLocalization.getString(id, args);
if (!string) {
throw new Error(
`Could not find a string for "${id}". Was the correct resource bundle loaded?`
);
}
return string;
};
}

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

@ -11,6 +11,13 @@ requestLongerTimeout(4);
add_task(async function() {
let toolbox;
const getFluentString = await getFluentStringHelper([
"devtools/client/toolbox.ftl",
]);
const hideSplitConsoleLabel = getFluentString(
"toolbox-meatball-menu-hideconsole-label"
);
await addTab(TEST_URI);
await testConsoleLoadOnDifferentPanel();
await testKeyboardShortcuts();
@ -118,7 +125,7 @@ add_task(async function() {
let label;
if (menuItem && menuItem.querySelector(".label")) {
label =
menuItem.querySelector(".label").textContent === "Hide Split Console"
menuItem.querySelector(".label").textContent === hideSplitConsoleLabel
? "hide"
: "split";
}

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

@ -9,17 +9,28 @@ const TEST_URI =
"data:text/html;charset=utf-8,<!DOCTYPE html><p>Web Console test for splitting</p>";
add_task(async function() {
const getFluentString = await getFluentStringHelper([
"devtools/client/toolbox.ftl",
]);
const hideLabel = getFluentString("toolbox-meatball-menu-hideconsole-label");
const showLabel = getFluentString("toolbox-meatball-menu-splitconsole-label");
info("Opening a tab while there is no user setting on split console pref");
let toolbox = await openNewTabAndToolbox(TEST_URI, "inspector");
ok(!toolbox.splitConsole, "Split console is hidden by default");
ok(
!(await doesMenuSayHide(toolbox)),
is(
await getSplitConsoleMenuLabel(toolbox),
showLabel,
"Split console menu item says split by default"
);
await toggleSplitConsoleWithEscape(toolbox);
ok(toolbox.splitConsole, "Split console is now visible.");
ok(await doesMenuSayHide(toolbox), "Split console menu item now says hide");
is(
await getSplitConsoleMenuLabel(toolbox),
hideLabel,
"Split console menu item now says hide"
);
ok(getVisiblePrefValue(), "Visibility pref is true");
is(
@ -40,8 +51,9 @@ add_task(async function() {
isInputFocused(toolbox.getPanel("webconsole").hud),
"Split console input is focused by default"
);
ok(
await doesMenuSayHide(toolbox),
is(
await getSplitConsoleMenuLabel(toolbox),
hideLabel,
"Split console menu item initially says hide"
);
is(
@ -64,8 +76,9 @@ add_task(async function() {
await toggleSplitConsoleWithEscape(toolbox);
ok(!toolbox.splitConsole, "Split console is now hidden.");
ok(
!(await doesMenuSayHide(toolbox)),
is(
await getSplitConsoleMenuLabel(toolbox),
showLabel,
"Split console menu item now says split"
);
ok(!getVisiblePrefValue(), "Visibility pref is false");
@ -98,7 +111,7 @@ function getHeightPrefValue() {
return Services.prefs.getIntPref("devtools.toolbox.splitconsoleHeight");
}
async function doesMenuSayHide(toolbox) {
async function getSplitConsoleMenuLabel(toolbox) {
const button = toolbox.doc.getElementById("toolbox-meatball-menu-button");
await waitUntil(
() => toolbox.win.getComputedStyle(button).pointerEvents === "auto"
@ -113,15 +126,10 @@ async function doesMenuSayHide(toolbox) {
"toolbox-meatball-menu-splitconsole"
);
const result =
menuItem &&
menuItem.querySelector(".label") &&
menuItem.querySelector(".label").textContent === "Hide Split Console";
toolbox.doc.addEventListener(
"popuphidden",
() => {
resolve(result);
resolve(menuItem?.querySelector(".label")?.textContent);
},
{ once: true }
);