Bug 1829691 - Add Translations panel settings menuitem tests r=gregtatum

Adds tests for all of the menuitems in the Translations
panel settings menu, including always-translate-language,
never-translate-language, and never-translate-site.

Depends on D177859

Differential Revision: https://phabricator.services.mozilla.com/D177860
This commit is contained in:
Erik Nordin 2023-05-24 01:22:44 +00:00
Родитель 37a2f3806d
Коммит ca64f2b0e0
8 изменённых файлов: 546 добавлений и 7 удалений

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

@ -4,9 +4,12 @@ support-files =
!/toolkit/components/translations/tests/browser/shared-head.js
!/toolkit/components/translations/tests/browser/translations-test.mjs
[browser_manage_languages.js]
[browser_translations_panel_always_translate_language.js]
[browser_translations_panel_basics.js]
[browser_translations_panel_beta_langs.js]
[browser_translations_panel_button.js]
[browser_translations_panel_cancel.js]
[browser_translations_panel_gear.js]
[browser_translations_panel_never_translate_language.js]
[browser_translations_panel_never_translate_site.js]
[browser_translations_panel_retry.js]

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

@ -0,0 +1,82 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
/**
* Tests the effects that toggling the always-translate-languages menuitem
* has on subsequent page loads.
*/
add_task(async function test_page_loads_with_always_translate_language() {
const { cleanup, resolveDownloads, runInPage } = await loadTestPage({
page: SPANISH_PAGE_URL,
languagePairs: LANGUAGE_PAIRS,
prefs: [["browser.translations.alwaysTranslateLanguages", "pl,fr"]],
});
await assertTranslationsButton(
{ button: true },
"The translations button should be visible"
);
info(
'The document language "es" is not in the alwaysTranslateLanguages pref, ' +
"so the page should be untranslated, in its original form"
);
await runInPage(async TranslationsTest => {
const { getH1 } = TranslationsTest.getSelectors();
await TranslationsTest.assertTranslationResult(
"The page's H1 is in Spanish.",
getH1,
"Don Quijote de La Mancha"
);
});
info(
"Simulate clicking always-translate-language in the settings menu, " +
"adding the document language from the alwaysTranslateLanguages pref"
);
await openSettingsMenu();
await toggleAlwaysTranslateLanguage();
await navigate(SPANISH_PAGE_URL, "Reload the page");
resolveDownloads(1);
info(
"The page should now be automatically translated because the document language " +
"should be added to the always-translate pref"
);
await runInPage(async TranslationsTest => {
const { getH1 } = TranslationsTest.getSelectors();
await TranslationsTest.assertTranslationResult(
"The page's H1 is translated automatically",
getH1,
"DON QUIJOTE DE LA MANCHA [es to en, html]"
);
});
info(
"Simulate clicking always-translate-language in the settings menu " +
"removing the document language from the alwaysTranslateLanguages pref"
);
await openSettingsMenu();
await toggleAlwaysTranslateLanguage();
await navigate(SPANISH_PAGE_URL, "Reload the page");
info(
"The page should no longer automatically translated because the document language " +
"should be removed from the always-translate pref"
);
await runInPage(async TranslationsTest => {
const { getH1 } = TranslationsTest.getSelectors();
await TranslationsTest.assertTranslationResult(
"The page's H1 is in Spanish.",
getH1,
"Don Quijote de La Mancha"
);
});
await cleanup();
});

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

@ -0,0 +1,64 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
/**
* Tests the effect that toggling the never-translate-languages menuitem
* has on subsequent page loads.
*/
add_task(async function test_page_loads_with_never_translate_language() {
const { cleanup, runInPage } = await loadTestPage({
page: SPANISH_PAGE_URL,
languagePairs: LANGUAGE_PAIRS,
prefs: [["browser.translations.neverTranslateLanguages", "pl,fr"]],
});
await assertTranslationsButton(
{ button: true },
"The translations button should be visible"
);
info(
'The document language "es" is not in the neverTranslateLanguages pref, ' +
"so the page should be untranslated, in its original form."
);
await runInPage(async TranslationsTest => {
const { getH1 } = TranslationsTest.getSelectors();
await TranslationsTest.assertTranslationResult(
"The page's H1 is in Spanish.",
getH1,
"Don Quijote de La Mancha"
);
});
info(
"Simulate clicking never-translate-language in the settings menu, " +
"adding the document language from the neverTranslateLanguages pref"
);
await openSettingsMenu();
await toggleNeverTranslateLanguage();
// Reload the page
await navigate(SPANISH_PAGE_URL);
await assertTranslationsButton(
{ button: false },
"The translations button should be invisible"
);
info(
"The page should still be in its original, untranslated form because " +
"the document language is in the neverTranslateLanguages pref"
);
await runInPage(async TranslationsTest => {
const { getH1 } = TranslationsTest.getSelectors();
await TranslationsTest.assertTranslationResult(
"The page's H1 is in Spanish.",
getH1,
"Don Quijote de La Mancha"
);
});
await cleanup();
});

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

@ -0,0 +1,209 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
/**
* Tests the effects that the never-translate-site menuitem has on
* subsequent page loads.
*/
add_task(async function test_page_loads_with_never_translate_site() {
const { cleanup, runInPage } = await loadTestPage({
page: SPANISH_PAGE_URL,
languagePairs: LANGUAGE_PAIRS,
permissionsUrls: [SPANISH_PAGE_URL],
});
await assertTranslationsButton(
{ button: true },
"The translations button should be visible"
);
info(
'The document language "es" is not in the alwaysTranslateLanguages pref, ' +
"so the page should be untranslated, in its original form"
);
await runInPage(async TranslationsTest => {
const { getH1 } = TranslationsTest.getSelectors();
await TranslationsTest.assertTranslationResult(
"The page's H1 is in Spanish.",
getH1,
"Don Quijote de La Mancha"
);
});
info("Disallow translations for this site");
await openSettingsMenu();
await toggleNeverTranslateSite();
info("Reload the page");
await navigate(SPANISH_PAGE_URL);
await assertTranslationsButton(
{ button: false },
"The translations button should be invisible"
);
info(
"The page should no longer automatically translated because the site " +
"no longer has permissions to be translated"
);
await runInPage(async TranslationsTest => {
const { getH1 } = TranslationsTest.getSelectors();
await TranslationsTest.assertTranslationResult(
"The page's H1 is in Spanish.",
getH1,
"Don Quijote de La Mancha"
);
});
info("Go to another page from the same site principal");
await navigate(SPANISH_PAGE_URL_2);
await assertTranslationsButton(
{ button: false },
"The translations button should be invisible"
);
info(
"This page should also be untranslated because the entire site " +
"no longer has permissions to be translated"
);
await runInPage(async TranslationsTest => {
const { getH1 } = TranslationsTest.getSelectors();
await TranslationsTest.assertTranslationResult(
"The page's H1 is in Spanish.",
getH1,
"Don Quijote de La Mancha"
);
});
info("Go to another page from another site principal");
await navigate(SPANISH_PAGE_URL_DOT_ORG);
await assertTranslationsButton(
{ button: true },
"The translations button should be visible"
);
info(
"This page should be untranslated because there are no auto-translate " +
"preferences set in this test"
);
await runInPage(async TranslationsTest => {
const { getH1 } = TranslationsTest.getSelectors();
await TranslationsTest.assertTranslationResult(
"The page's H1 is in Spanish.",
getH1,
"Don Quijote de La Mancha"
);
});
await cleanup();
});
/**
* Tests the effects that the never-translate-site menuitem has on
* subsequent page loads when always-translate-language is active.
*/
add_task(
async function test_page_loads_with_always_translate_language_and_never_translate_site() {
const { cleanup, resolveDownloads, runInPage } = await loadTestPage({
page: SPANISH_PAGE_URL,
languagePairs: LANGUAGE_PAIRS,
prefs: [["browser.translations.alwaysTranslateLanguages", "es"]],
permissionsUrls: [SPANISH_PAGE_URL],
});
await assertTranslationsButton(
{ button: true },
"The translations button should be visible"
);
resolveDownloads(1);
info(
"The page should be automatically translated because the document language " +
"should be in the alwaysTranslateLanguages"
);
await runInPage(async TranslationsTest => {
const { getH1 } = TranslationsTest.getSelectors();
await TranslationsTest.assertTranslationResult(
"The page's H1 is translated automatically",
getH1,
"DON QUIJOTE DE LA MANCHA [es to en, html]"
);
});
info("Disallow translations for this site");
await openSettingsMenu();
await toggleNeverTranslateSite();
info("Reload the page");
await navigate(SPANISH_PAGE_URL);
await assertTranslationsButton(
{ button: false },
"The translations button should be invisible"
);
info(
"The page should no longer automatically translated because the site " +
"no longer has permissions to be translated"
);
await runInPage(async TranslationsTest => {
const { getH1 } = TranslationsTest.getSelectors();
await TranslationsTest.assertTranslationResult(
"The page's H1 is in Spanish.",
getH1,
"Don Quijote de La Mancha"
);
});
info("Go to another page from the same site principal");
await navigate(SPANISH_PAGE_URL_2);
await assertTranslationsButton(
{ button: false },
"The translations button should be invisible"
);
info(
"This page should also be untranslated because the entire site " +
"no longer has permissions to be translated"
);
await runInPage(async TranslationsTest => {
const { getH1 } = TranslationsTest.getSelectors();
await TranslationsTest.assertTranslationResult(
"The page's H1 is in Spanish.",
getH1,
"Don Quijote de La Mancha"
);
});
info("Go to another page from a different site principal");
await navigate(SPANISH_PAGE_URL_DOT_ORG);
await assertTranslationsButton(
{ button: true },
"The translations button should be visible"
);
resolveDownloads(1);
info(
"The page should be automatically translated because the document language " +
"should be in the alwaysTranslateLanguages and this is a different site principal"
);
await runInPage(async TranslationsTest => {
const { getH1 } = TranslationsTest.getSelectors();
await TranslationsTest.assertTranslationResult(
"The page's H1 is translated automatically",
getH1,
"DON QUIJOTE DE LA MANCHA [es to en, html]"
);
});
await cleanup();
}
);

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

@ -12,7 +12,7 @@ Services.scriptloader.loadSubScript(
* Assert some property about the translations button.
*
* @param {Record<string, boolean>} visibleAssertions
* @param {string} message The messag for the assertion.
* @param {string} message The message for the assertion.
* @returns {HTMLElement}
*/
async function assertTranslationsButton(visibleAssertions, message) {
@ -51,12 +51,138 @@ async function assertTranslationsButton(visibleAssertions, message) {
return elements;
}
/**
* A convenience function to open the settings menu of the
* translations panel. Fails the test if the menu cannot be opened.
*/
async function openSettingsMenu() {
const { button } = await assertTranslationsButton(
{ button: true },
"The button is available."
);
await waitForTranslationsPopupEvent("popupshown", () => {
click(button, "Opening the popup");
});
const gearIcon = getByL10nId("translations-panel-settings-button");
click(gearIcon, "Open the settings menu");
}
/**
* Simulates the effect of clicking the always-translate-language menuitem.
* Requires that the settings menu of the translations panel is open,
* otherwise the test will fail.
*/
async function toggleAlwaysTranslateLanguage() {
const alwaysTranslateLanguage = getByL10nId(
"translations-panel-settings-always-translate-language"
);
info("Toggle the always-translate-language menuitem");
await alwaysTranslateLanguage.doCommand();
}
/**
* Simulates the effect of clicking the never-translate-language menuitem.
* Requires that the settings menu of the translations panel is open,
* otherwise the test will fail.
*/
async function toggleNeverTranslateLanguage() {
const neverTranslateLanguage = getByL10nId(
"translations-panel-settings-never-translate-language"
);
info("Toggle the never-translate-language menuitem");
await neverTranslateLanguage.doCommand();
}
/**
* Simulates the effect of clicking the never-translate-site menuitem.
* Requires that the settings menu of the translations panel is open,
* otherwise the test will fail.
*/
async function toggleNeverTranslateSite() {
const neverTranslateSite = getByL10nId(
"translations-panel-settings-never-translate-site"
);
info("Toggle the never-translate-site menuitem");
await neverTranslateSite.doCommand();
}
/**
* Asserts that the always-translate-language checkbox matches the expected checked state.
*
* @param {string} langTag - A BCP-47 language tag
* @param {boolean} expectChecked - Whether the checkbox should be checked
*/
async function assertIsAlwaysTranslateLanguage(langTag, expectChecked) {
await assertCheckboxState(
"translations-panel-settings-always-translate-language",
expectChecked
);
}
/**
* Asserts that the never-translate-language checkbox matches the expected checked state.
*
* @param {string} langTag - A BCP-47 language tag
* @param {boolean} expectChecked - Whether the checkbox should be checked
*/
async function assertIsNeverTranslateLanguage(langTag, expectChecked) {
await assertCheckboxState(
"translations-panel-settings-never-translate-language",
expectChecked
);
}
/**
* Asserts that the never-translate-site checkbox matches the expected checked state.
*
* @param {string} url - The url of a website
* @param {boolean} expectChecked - Whether the checkbox should be checked
*/
async function assertIsNeverTranslateSite(url, expectChecked) {
await assertCheckboxState(
"translations-panel-settings-never-translate-site",
expectChecked
);
}
/**
* Asserts that the state of a checkbox with a given dataL10nId is
* checked or not, based on the value of expected being true or false.
*
* @param {string} dataL10nId - The data-l10n-id of the checkbox.
* @param {boolean} expectChecked - Whether the checkbox should be checked.
*/
async function assertCheckboxState(dataL10nId, expectChecked) {
const menuItems = getAllByL10nId(dataL10nId);
for (const menuItem of menuItems) {
await TestUtils.waitForCondition(
() =>
menuItem.getAttribute("checked") === (expectChecked ? "true" : "false"),
"Waiting for checkbox state"
);
is(
menuItem.getAttribute("checked"),
expectChecked ? "true" : "false",
`Should match expected checkbox state for ${dataL10nId}`
);
}
}
/**
* Navigate to a URL and indicate a message as to why.
*/
function navigate(url, message) {
async function navigate(url, message) {
info(message);
// Load a blank page first to ensure that tests don't hang.
// I don't know why this is needed, but it appears to be necessary.
BrowserTestUtils.loadURIString(gBrowser.selectedBrowser, BLANK_PAGE);
await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
BrowserTestUtils.loadURIString(gBrowser.selectedBrowser, url);
await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
}
/**
@ -194,6 +320,8 @@ async function waitForViewShown(callback) {
const ENGLISH_PAGE_URL = TRANSLATIONS_TESTER_EN;
const SPANISH_PAGE_URL = TRANSLATIONS_TESTER_ES;
const SPANISH_PAGE_URL_2 = TRANSLATIONS_TESTER_ES_2;
const SPANISH_PAGE_URL_DOT_ORG = TRANSLATIONS_TESTER_ES_DOT_ORG;
const LANGUAGE_PAIRS = [
{ fromLang: "es", toLang: "en", isBeta: false },
{ fromLang: "en", toLang: "es", isBeta: false },

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

@ -5,6 +5,7 @@ support-files =
translations-test.mjs
translations-tester-en.html
translations-tester-es.html
translations-tester-es-2.html
translations-tester-no-tag.html
[browser_about_translations_debounce.js]
skip-if =

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

@ -7,15 +7,20 @@
const BLANK_PAGE =
"data:text/html;charset=utf-8,<!DOCTYPE html><title>Blank</title>Blank page";
const URL_PREFIX = "https://example.com/browser/";
const URL_COM_PREFIX = "https://example.com/browser/";
const URL_ORG_PREFIX = "https://example.org/browser/";
const CHROME_URL_PREFIX = "chrome://mochitests/content/browser/";
const DIR_PATH = "toolkit/components/translations/tests/browser/";
const TRANSLATIONS_TESTER_EN =
URL_PREFIX + DIR_PATH + "translations-tester-en.html";
URL_COM_PREFIX + DIR_PATH + "translations-tester-en.html";
const TRANSLATIONS_TESTER_ES =
URL_PREFIX + DIR_PATH + "translations-tester-es.html";
URL_COM_PREFIX + DIR_PATH + "translations-tester-es.html";
const TRANSLATIONS_TESTER_ES_2 =
URL_COM_PREFIX + DIR_PATH + "translations-tester-es-2.html";
const TRANSLATIONS_TESTER_ES_DOT_ORG =
URL_ORG_PREFIX + DIR_PATH + "translations-tester-es.html";
const TRANSLATIONS_TESTER_NO_TAG =
URL_PREFIX + DIR_PATH + "translations-tester-no-tag.html";
URL_COM_PREFIX + DIR_PATH + "translations-tester-no-tag.html";
/**
* The mochitest runs in the parent process. This function opens up a new tab,
@ -363,6 +368,7 @@ async function loadTestPage({
detectedLangTag,
page,
prefs,
permissionsUrls = [],
}) {
await SpecialPowers.pushPrefEnv({
set: [
@ -372,6 +378,13 @@ async function loadTestPage({
...(prefs ?? []),
],
});
await SpecialPowers.pushPermissions(
permissionsUrls.map(url => ({
type: "translations",
allow: true,
context: url,
}))
);
// Start the tab at a blank page.
const tab = await BrowserTestUtils.openNewForegroundTab(
@ -415,7 +428,10 @@ async function loadTestPage({
cleanup() {
removeMocks();
BrowserTestUtils.removeTab(tab);
return SpecialPowers.popPrefEnv();
return Promise.all([
SpecialPowers.popPrefEnv(),
SpecialPowers.popPermissions(),
]);
},
/**

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

@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8" />
<title>Translations Test</title>
<style>
div {
margin: 10px auto;
width: 300px
}
p {
margin: 47px 0;
font-size: 21px;
line-height: 2;
}
</style>
</head>
<body>
<div>
<header lang="en">The following is an excerpt from Don Quijote de la Mancha, which is in the public domain</header>
<h1>Don Quijote de La Mancha</h1>
<h2>Capítulo VIII.</h2>
<p>Del buen suceso que el valeroso don Quijote tuvo en la espantable y jamás imaginada aventura de los molinos de viento, con otros sucesos dignos de felice recordación</p>
<p>En esto, descubrieron treinta o cuarenta molinos de viento que hay en aquel campo; y, así como don Quijote los vio, dijo a su escudero:</p>
<p>— La ventura va guiando nuestras cosas mejor de lo que acertáramos a desear, porque ves allí, amigo Sancho Panza, donde se descubren treinta, o pocos más, desaforados gigantes, con quien pienso hacer batalla y quitarles a todos las vidas, con cuyos despojos comenzaremos a enriquecer; que ésta es buena guerra, y es gran servicio de Dios quitar tan mala simiente de sobre la faz de la tierra.</p>
<p>— ¿Qué gigantes? —dijo Sancho Panza.</p>
<p>— Aquellos que allí ves —respondió su amo— de los brazos largos, que los suelen tener algunos de casi dos leguas.</p>
<p>— Mire vuestra merced —respondió Sancho— que aquellos que allí se parecen no son gigantes, sino molinos de viento, y lo que en ellos parecen brazos son las aspas, que, volteadas del viento, hacen andar la piedra del molino.</p>
<p>— Bien parece —respondió don Quijote— que no estás cursado en esto de las aventuras: ellos son gigantes; y si tienes miedo, quítate de ahí, y ponte en oración en el espacio que yo voy a entrar con ellos en fiera y desigual batalla.</p>
<p>Y, diciendo esto, dio de espuelas a su caballo Rocinante, sin atender a las voces que su escudero Sancho le daba, advirtiéndole que, sin duda alguna, eran molinos de viento, y no gigantes, aquellos que iba a acometer. Pero él iba tan puesto en que eran gigantes, que ni oía las voces de su escudero Sancho ni echaba de ver, aunque estaba ya bien cerca, lo que eran; antes, iba diciendo en voces altas:</p>
<p>— Non fuyades, cobardes y viles criaturas, que un solo caballero es el que os acomete.</p>
<p>Levantóse en esto un poco de viento y las grandes aspas comenzaron a moverse, lo cual visto por don Quijote, dijo:</p>
<p>— Pues, aunque mováis más brazos que los del gigante Briareo, me lo habéis de pagar.</p>
</div>
</body>
</html>