diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:44:51 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:44:51 +0000 |
commit | 9e3c08db40b8916968b9f30096c7be3f00ce9647 (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /toolkit/components/translations/tests/browser | |
parent | Initial commit. (diff) | |
download | thunderbird-upstream.tar.xz thunderbird-upstream.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/translations/tests/browser')
19 files changed, 3711 insertions, 0 deletions
diff --git a/toolkit/components/translations/tests/browser/browser.ini b/toolkit/components/translations/tests/browser/browser.ini new file mode 100644 index 0000000000..3cbd829f4b --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser.ini @@ -0,0 +1,22 @@ +[DEFAULT] +support-files = + head.js + shared-head.js + 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 = + os == 'linux' # Bug 1821461 +[browser_about_translations_directions.js] +[browser_about_translations_dropdowns.js] +[browser_about_translations_enabling.js] +[browser_about_translations_translations.js] +[browser_full_page.js] +[browser_remote_settings.js] +[browser_translation_document.js] +[browser_translations_actor.js] +[browser_translations_actor_preferred_language.js] +[browser_translations_lang_tags.js] diff --git a/toolkit/components/translations/tests/browser/browser_about_translations_debounce.js b/toolkit/components/translations/tests/browser/browser_about_translations_debounce.js new file mode 100644 index 0000000000..f31a240ee8 --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_about_translations_debounce.js @@ -0,0 +1,84 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Test the debounce behavior. + */ +add_task(async function test_about_translations_debounce() { + await openAboutTranslations({ + languagePairs: [ + { fromLang: "en", toLang: "fr", isBeta: false }, + { fromLang: "fr", toLang: "en", isBeta: false }, + ], + runInPage: async ({ selectors }) => { + const { document, window } = content; + // Do not allow the debounce to come to completion. + Cu.waiveXrays(window).DEBOUNCE_DELAY = 5; + + await ContentTaskUtils.waitForCondition(() => { + return document.body.hasAttribute("ready"); + }, "Waiting for the document to be ready."); + + /** @type {HTMLSelectElement} */ + const fromSelect = document.querySelector(selectors.fromLanguageSelect); + /** @type {HTMLSelectElement} */ + const toSelect = document.querySelector(selectors.toLanguageSelect); + /** @type {HTMLTextAreaElement} */ + const translationTextarea = document.querySelector( + selectors.translationTextarea + ); + /** @type {HTMLDivElement} */ + const translationResult = document.querySelector( + selectors.translationResult + ); + + async function assertTranslationResult(translation) { + try { + await ContentTaskUtils.waitForCondition( + () => translation === translationResult.innerText, + `Waiting for: "${translation}"` + ); + } catch (error) { + // The result wasn't found, but the assertion below will report the error. + console.error(error); + } + + is( + translation, + translationResult.innerText, + "The text runs through the mocked translations engine." + ); + } + + function setInput(element, value) { + element.value = value; + element.dispatchEvent(new Event("input")); + } + setInput(fromSelect, "en"); + setInput(toSelect, "fr"); + setInput(translationTextarea, "T"); + + info("Get the translations into a stable translationed state"); + await assertTranslationResult("T [en to fr]"); + + info("Reset and pause the debounce state."); + Cu.waiveXrays(window).DEBOUNCE_DELAY = 1_000_000_000; + Cu.waiveXrays(window).DEBOUNCE_RUN_COUNT = 0; + + info("Input text which will be debounced."); + setInput(translationTextarea, "T"); + setInput(translationTextarea, "Te"); + setInput(translationTextarea, "Tex"); + is(Cu.waiveXrays(window).DEBOUNCE_RUN_COUNT, 0, "Debounce has not run."); + + info("Allow the debounce to actually come to completion."); + Cu.waiveXrays(window).DEBOUNCE_DELAY = 5; + setInput(translationTextarea, "Text"); + + await assertTranslationResult("TEXT [en to fr]"); + is(Cu.waiveXrays(window).DEBOUNCE_RUN_COUNT, 1, "Debounce ran once."); + }, + }); +}); diff --git a/toolkit/components/translations/tests/browser/browser_about_translations_directions.js b/toolkit/components/translations/tests/browser/browser_about_translations_directions.js new file mode 100644 index 0000000000..153ab1f1d9 --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_about_translations_directions.js @@ -0,0 +1,73 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +add_task(async function test_about_translations_language_directions() { + await openAboutTranslations({ + languagePairs: [ + // English (en) is LTR and Arabic (ar) is RTL. + { fromLang: "en", toLang: "ar", isBeta: true }, + { fromLang: "ar", toLang: "en", isBeta: true }, + ], + runInPage: async ({ selectors }) => { + const { document, window } = content; + Cu.waiveXrays(window).DEBOUNCE_DELAY = 5; // Make the timer run faster for tests. + + await ContentTaskUtils.waitForCondition(() => { + return document.body.hasAttribute("ready"); + }, "Waiting for the document to be ready."); + + /** @type {HTMLSelectElement} */ + const fromSelect = document.querySelector(selectors.fromLanguageSelect); + /** @type {HTMLSelectElement} */ + const toSelect = document.querySelector(selectors.toLanguageSelect); + /** @type {HTMLTextAreaElement} */ + const translationTextarea = document.querySelector( + selectors.translationTextarea + ); + /** @type {HTMLDivElement} */ + const translationResult = document.querySelector( + selectors.translationResult + ); + + fromSelect.value = "en"; + fromSelect.dispatchEvent(new Event("input")); + toSelect.value = "ar"; + toSelect.dispatchEvent(new Event("input")); + translationTextarea.value = "This text starts as LTR."; + translationTextarea.dispatchEvent(new Event("input")); + + is( + window.getComputedStyle(translationTextarea).direction, + "ltr", + "The English input is LTR" + ); + is( + window.getComputedStyle(translationResult).direction, + "rtl", + "The Arabic results are RTL" + ); + + toSelect.value = ""; + toSelect.dispatchEvent(new Event("input")); + fromSelect.value = "ar"; + fromSelect.dispatchEvent(new Event("input")); + toSelect.value = "en"; + toSelect.dispatchEvent(new Event("input")); + translationTextarea.value = "This text starts as RTL."; + translationTextarea.dispatchEvent(new Event("input")); + + is( + window.getComputedStyle(translationTextarea).direction, + "rtl", + "The Arabic input is RTL" + ); + is( + window.getComputedStyle(translationResult).direction, + "ltr", + "The English results are LTR" + ); + }, + }); +}); diff --git a/toolkit/components/translations/tests/browser/browser_about_translations_dropdowns.js b/toolkit/components/translations/tests/browser/browser_about_translations_dropdowns.js new file mode 100644 index 0000000000..67c5da1af9 --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_about_translations_dropdowns.js @@ -0,0 +1,131 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +add_task(async function test_about_translations_dropdowns() { + let languagePairs = [ + { fromLang: "en", toLang: "es", isBeta: false }, + { fromLang: "es", toLang: "en", isBeta: false }, + // This is not a bi-directional translation. + { fromLang: "is", toLang: "en", isBeta: true }, + ]; + await openAboutTranslations({ + languagePairs, + dataForContent: languagePairs, + runInPage: async ({ dataForContent: languagePairs, selectors }) => { + const { document } = content; + + await ContentTaskUtils.waitForCondition(() => { + return document.body.hasAttribute("ready"); + }, "Waiting for the document to be ready."); + + /** + * Some languages can be marked as hidden in the dropbdown. This function + * asserts the configuration of the options. + * + * @param {object} args + * @param {string} args.message + * @param {HTMLSelectElement} args.select + * @param {string[]} args.availableOptions + * @param {string} args.selectedValue + */ + function assertOptions({ + message, + select, + availableOptions, + selectedValue, + }) { + const options = [...select.options]; + const betaL10nId = "about-translations-displayname-beta"; + for (const option of options) { + for (const languagePair of languagePairs) { + if ( + languagePair.fromLang === option.value || + languagePair.toLang === option.value + ) { + if (option.getAttribute("data-l10n-id") === betaL10nId) { + is( + languagePair.isBeta, + true, + `Since data-l10n-id was ${betaL10nId} for ${option.value}, then it must be part of a beta language pair, but it was not.` + ); + } + if (!languagePair.isBeta) { + is( + option.getAttribute("data-l10n-id") === betaL10nId, + false, + `Since the languagePair is non-beta, the language option ${option.value} should not have a data-l10-id of ${betaL10nId}, but it does.` + ); + } + } + } + } + info(message); + Assert.deepEqual( + options.filter(option => !option.hidden).map(option => option.value), + availableOptions, + "The available options match." + ); + + is(selectedValue, select.value, "The selected value matches."); + } + + /** @type {HTMLSelectElement} */ + const fromSelect = document.querySelector(selectors.fromLanguageSelect); + /** @type {HTMLSelectElement} */ + const toSelect = document.querySelector(selectors.toLanguageSelect); + + assertOptions({ + message: 'From languages have "detect" already selected.', + select: fromSelect, + availableOptions: ["detect", "en", "is", "es"], + selectedValue: "detect", + }); + + assertOptions({ + message: + 'The "to" options do not have "detect" in the list, and nothing is selected.', + select: toSelect, + availableOptions: ["", "en", "es"], + selectedValue: "", + }); + + info('Switch the "to" language to "es".'); + toSelect.value = "es"; + toSelect.dispatchEvent(new Event("input")); + + assertOptions({ + message: 'The "from" languages no longer suggest "es".', + select: fromSelect, + availableOptions: ["detect", "en", "is"], + selectedValue: "detect", + }); + + assertOptions({ + message: 'The "to" options remain the same, but "es" is selected.', + select: toSelect, + availableOptions: ["", "en", "es"], + selectedValue: "es", + }); + + info('Switch the "from" language to English.'); + fromSelect.value = "en"; + fromSelect.dispatchEvent(new Event("input")); + + assertOptions({ + message: 'The "to" languages no longer suggest "en".', + select: toSelect, + availableOptions: ["", "es"], + selectedValue: "es", + }); + + assertOptions({ + message: 'The "from" options remain the same, but "en" is selected.', + select: fromSelect, + availableOptions: ["detect", "en", "is"], + selectedValue: "en", + }); + }, + }); +}); diff --git a/toolkit/components/translations/tests/browser/browser_about_translations_enabling.js b/toolkit/components/translations/tests/browser/browser_about_translations_enabling.js new file mode 100644 index 0000000000..662abc3995 --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_about_translations_enabling.js @@ -0,0 +1,124 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Checks that the page renders without issue, and that the expected elements + * are there. + */ +add_task(async function test_about_translations_enabled() { + await openAboutTranslations({ + runInPage: async ({ selectors }) => { + const { document, window } = content; + + await ContentTaskUtils.waitForCondition(() => { + const trElement = document.querySelector(selectors.translationResult); + const trBlankElement = document.querySelector( + selectors.translationResultBlank + ); + const { visibility: trVisibility } = window.getComputedStyle(trElement); + const { visibility: trBlankVisibility } = + window.getComputedStyle(trBlankElement); + return trVisibility === "hidden" && trBlankVisibility === "visible"; + }, `Waiting for placeholder text to be visible."`); + + function checkElementIsVisible(expectVisible, name) { + const expected = expectVisible ? "visible" : "hidden"; + const element = document.querySelector(selectors[name]); + ok(Boolean(element), `Element ${name} was found.`); + const { visibility } = window.getComputedStyle(element); + is( + visibility, + expected, + `Element ${name} was not ${expected} but should be.` + ); + } + + checkElementIsVisible(true, "pageHeader"); + checkElementIsVisible(true, "fromLanguageSelect"); + checkElementIsVisible(true, "toLanguageSelect"); + checkElementIsVisible(true, "translationTextarea"); + checkElementIsVisible(true, "translationResultBlank"); + + checkElementIsVisible(false, "translationResult"); + }, + }); +}); + +/** + * Checks that the page does not show the content when disabled. + */ +add_task(async function test_about_translations_disabled() { + await openAboutTranslations({ + disabled: true, + runInPage: async ({ selectors }) => { + const { document, window } = content; + + await ContentTaskUtils.waitForCondition(() => { + const element = document.querySelector(selectors.translationResult); + const { visibility } = window.getComputedStyle(element); + return visibility === "hidden"; + }, `Waiting for translated text to be hidden."`); + + function checkElementIsInvisible(name) { + const element = document.querySelector(selectors[name]); + ok(Boolean(element), `Element ${name} was found.`); + const { visibility } = window.getComputedStyle(element); + is(visibility, "hidden", `Element ${name} was invisible.`); + } + + checkElementIsInvisible("pageHeader"); + checkElementIsInvisible("fromLanguageSelect"); + checkElementIsInvisible("toLanguageSelect"); + checkElementIsInvisible("translationTextarea"); + checkElementIsInvisible("translationResult"); + checkElementIsInvisible("translationResultBlank"); + }, + }); +}); + +/** + * Test that the page is properly disabled when the engine isn't supported. + */ +add_task(async function test_about_translations_disabling() { + await openAboutTranslations({ + prefs: [["browser.translations.simulateUnsupportedEngine", true]], + runInPage: async ({ selectors }) => { + const { document, window } = content; + + info('Checking for the "no support" message.'); + await ContentTaskUtils.waitForCondition( + () => document.querySelector(selectors.noSupportMessage), + 'Waiting for the "no support" message.' + ); + + /** @type {HTMLSelectElement} */ + const fromSelect = document.querySelector(selectors.fromLanguageSelect); + /** @type {HTMLSelectElement} */ + const toSelect = document.querySelector(selectors.toLanguageSelect); + /** @type {HTMLTextAreaElement} */ + const translationTextarea = document.querySelector( + selectors.translationTextarea + ); + + ok(fromSelect.disabled, "The from select is disabled"); + ok(toSelect.disabled, "The to select is disabled"); + ok(translationTextarea.disabled, "The textarea is disabled"); + + function checkElementIsVisible(expectVisible, name) { + const expected = expectVisible ? "visible" : "hidden"; + const element = document.querySelector(selectors[name]); + ok(Boolean(element), `Element ${name} was found.`); + const { visibility } = window.getComputedStyle(element); + is( + visibility, + expected, + `Element ${name} was not ${expected} but should be.` + ); + } + + checkElementIsVisible(true, "translationInfo"); + }, + }); +}); diff --git a/toolkit/components/translations/tests/browser/browser_about_translations_translations.js b/toolkit/components/translations/tests/browser/browser_about_translations_translations.js new file mode 100644 index 0000000000..e519f2d0d2 --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_about_translations_translations.js @@ -0,0 +1,248 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Test that the UI actually translates text, but use a mocked translations engine. + * The results of the "translation" will be modifying the text to be full width latin + * characters, so that the results visually appear modified. + */ +add_task(async function test_about_translations_translations() { + await openAboutTranslations({ + languagePairs: [ + { fromLang: "en", toLang: "fr", isBeta: false }, + { fromLang: "fr", toLang: "en", isBeta: false }, + // This is not a bi-directional translation. + { fromLang: "is", toLang: "en", isBeta: true }, + ], + runInPage: async ({ selectors }) => { + const { document, window } = content; + Cu.waiveXrays(window).DEBOUNCE_DELAY = 5; // Make the timer run faster for tests. + + await ContentTaskUtils.waitForCondition(() => { + return document.body.hasAttribute("ready"); + }, "Waiting for the document to be ready."); + + /** @type {HTMLSelectElement} */ + const fromSelect = document.querySelector(selectors.fromLanguageSelect); + /** @type {HTMLSelectElement} */ + const toSelect = document.querySelector(selectors.toLanguageSelect); + /** @type {HTMLTextAreaElement} */ + const translationTextarea = document.querySelector( + selectors.translationTextarea + ); + /** @type {HTMLDivElement} */ + const translationResult = document.querySelector( + selectors.translationResult + ); + + async function assertTranslationResult(translation) { + try { + await ContentTaskUtils.waitForCondition( + () => translation === translationResult.innerText, + `Waiting for: "${translation}"` + ); + } catch (error) { + // The result wasn't found, but the assertion below will report the error. + console.error(error); + } + + is( + translation, + translationResult.innerText, + "The text runs through the mocked translations engine." + ); + } + + fromSelect.value = "en"; + fromSelect.dispatchEvent(new Event("input")); + + toSelect.value = "fr"; + toSelect.dispatchEvent(new Event("input")); + + translationTextarea.value = "Text to translate."; + translationTextarea.dispatchEvent(new Event("input")); + + // The mocked translations make the text uppercase and reports the models used. + await assertTranslationResult("TEXT TO TRANSLATE. [en to fr]"); + is( + translationResult.getAttribute("lang"), + "fr", + "The result is listed as in French." + ); + + // Blank out the "to" select so it doesn't try to translate between is to fr. + toSelect.value = ""; + toSelect.dispatchEvent(new Event("input")); + + fromSelect.value = "is"; + fromSelect.dispatchEvent(new Event("input")); + toSelect.value = "en"; + toSelect.dispatchEvent(new Event("input")); + translationTextarea.value = "This is the second translation."; + translationTextarea.dispatchEvent(new Event("input")); + + await assertTranslationResult( + "THIS IS THE SECOND TRANSLATION. [is to en]" + ); + is( + translationResult.getAttribute("lang"), + "en", + "The result is listed as in English." + ); + }, + }); +}); + +/** + * Test the useHTML pref. + */ +add_task(async function test_about_translations_html() { + await openAboutTranslations({ + languagePairs: [ + { fromLang: "en", toLang: "fr", isBeta: false }, + { fromLang: "fr", toLang: "en", isBeta: false }, + ], + prefs: [["browser.translations.useHTML", true]], + runInPage: async ({ selectors }) => { + const { document, window } = content; + Cu.waiveXrays(window).DEBOUNCE_DELAY = 5; // Make the timer run faster for tests. + + await ContentTaskUtils.waitForCondition(() => { + return document.body.hasAttribute("ready"); + }, "Waiting for the document to be ready."); + + /** @type {HTMLSelectElement} */ + const fromSelect = document.querySelector(selectors.fromLanguageSelect); + /** @type {HTMLSelectElement} */ + const toSelect = document.querySelector(selectors.toLanguageSelect); + /** @type {HTMLTextAreaElement} */ + const translationTextarea = document.querySelector( + selectors.translationTextarea + ); + /** @type {HTMLDivElement} */ + const translationResult = document.querySelector( + selectors.translationResult + ); + + async function assertTranslationResult(translation) { + try { + await ContentTaskUtils.waitForCondition( + () => translation === translationResult.innerText, + `Waiting for: "${translation}"` + ); + } catch (error) { + // The result wasn't found, but the assertion below will report the error. + console.error(error); + } + + is( + translation, + translationResult.innerText, + "The text runs through the mocked translations engine." + ); + } + + fromSelect.value = "en"; + fromSelect.dispatchEvent(new Event("input")); + toSelect.value = "fr"; + toSelect.dispatchEvent(new Event("input")); + translationTextarea.value = "Text to translate."; + translationTextarea.dispatchEvent(new Event("input")); + + // The mocked translations make the text uppercase and reports the models used. + await assertTranslationResult("TEXT TO TRANSLATE. [en to fr, html]"); + }, + }); +}); + +add_task(async function test_about_translations_language_identification() { + await openAboutTranslations({ + detectedLangTag: "en", + detectedLanguageConfidence: "0.98", + languagePairs: [ + { fromLang: "en", toLang: "fr", isBeta: false }, + { fromLang: "fr", toLang: "en", isBeta: false }, + ], + runInPage: async ({ selectors }) => { + const { document, window } = content; + Cu.waiveXrays(window).DEBOUNCE_DELAY = 5; // Make the timer run faster for tests. + + await ContentTaskUtils.waitForCondition(() => { + return document.body.hasAttribute("ready"); + }, "Waiting for the document to be ready."); + + /** @type {HTMLSelectElement} */ + const fromSelect = document.querySelector(selectors.fromLanguageSelect); + /** @type {HTMLSelectElement} */ + const toSelect = document.querySelector(selectors.toLanguageSelect); + /** @type {HTMLTextAreaElement} */ + const translationTextarea = document.querySelector( + selectors.translationTextarea + ); + /** @type {HTMLDivElement} */ + const translationResult = document.querySelector( + selectors.translationResult + ); + + async function assertTranslationResult(translation) { + try { + await ContentTaskUtils.waitForCondition( + () => translation === translationResult.innerText, + `Waiting for: "${translation}"` + ); + } catch (error) { + // The result wasn't found, but the assertion below will report the error. + console.error(error); + } + is( + translation, + translationResult.innerText, + "The language identification engine correctly informs the translation." + ); + } + + const fromSelectStartValue = fromSelect.value; + const detectStartText = fromSelect.options[0].textContent; + + is( + fromSelectStartValue, + "detect", + 'The fromSelect starting value is "detect"' + ); + + translationTextarea.value = "Text to translate."; + translationTextarea.dispatchEvent(new Event("input")); + + toSelect.value = "fr"; + toSelect.dispatchEvent(new Event("input")); + + await ContentTaskUtils.waitForCondition(() => { + const element = document.querySelector( + selectors.translationResultBlank + ); + const { visibility } = window.getComputedStyle(element); + return visibility === "hidden"; + }, `Waiting for placeholder text to be visible."`); + + const fromSelectFinalValue = fromSelect.value; + is( + fromSelectFinalValue, + fromSelectStartValue, + "The fromSelect value has not changed" + ); + + // The mocked translations make the text uppercase and reports the models used. + await assertTranslationResult("TEXT TO TRANSLATE. [en to fr]"); + + const detectFinalText = fromSelect.options[0].textContent; + is( + true, + detectFinalText.startsWith(detectStartText) && + detectFinalText.length > detectStartText.length, + `fromSelect starting display text (${detectStartText}) should be a substring of the final text (${detectFinalText})` + ); + }, + }); +}); diff --git a/toolkit/components/translations/tests/browser/browser_full_page.js b/toolkit/components/translations/tests/browser/browser_full_page.js new file mode 100644 index 0000000000..035b484d4f --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_full_page.js @@ -0,0 +1,138 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Check that the full page translation feature works. + */ +add_task(async function test_full_page_translation() { + await autoTranslatePage({ + page: TRANSLATIONS_TESTER_ES, + languagePairs: [ + { fromLang: "es", toLang: "en", isBeta: false }, + { fromLang: "en", toLang: "es", isBeta: false }, + ], + runInPage: async TranslationsTest => { + const selectors = TranslationsTest.getSelectors(); + + await TranslationsTest.assertTranslationResult( + "The main title gets translated.", + selectors.getH1, + "DON QUIJOTE DE LA MANCHA [es to en, html]" + ); + + await TranslationsTest.assertTranslationResult( + "The last paragraph gets translated. It is out of the viewport.", + selectors.getLastParagraph, + "— PUES, AUNQUE MOVÁIS MÁS BRAZOS QUE LOS DEL GIGANTE BRIAREO, ME LO HABÉIS DE PAGAR. [es to en, html]" + ); + + selectors.getH1().innerText = "Este es un titulo"; + + await TranslationsTest.assertTranslationResult( + "Mutations get tracked", + selectors.getH1, + "ESTE ES UN TITULO [es to en]" + ); + + await TranslationsTest.assertTranslationResult( + "Other languages do not get translated.", + selectors.getHeader, + "The following is an excerpt from Don Quijote de la Mancha, which is in the public domain" + ); + }, + }); +}); + +/** + * Check that the full page translation feature doesn't translate pages in the app's + * locale. + */ +add_task(async function test_about_translations_enabled() { + const { appLocaleAsBCP47 } = Services.locale; + if (!appLocaleAsBCP47.startsWith("en")) { + console.warn( + "This test assumes to be running in an 'en' app locale, however the app locale " + + `is set to ${appLocaleAsBCP47}. Skipping the test.` + ); + ok(true, "Skipping test."); + return; + } + + await autoTranslatePage({ + page: TRANSLATIONS_TESTER_EN, + languagePairs: [ + { fromLang: "es", toLang: "en", isBeta: false }, + { fromLang: "en", toLang: "es", isBeta: false }, + ], + runInPage: async () => { + const { document } = content; + + for (let i = 0; i < 5; i++) { + // There is no way to directly check the non-existence of a translation, as + // the translations engine works async, and you can't dispatch a CustomEvent + // to listen for translations, as this script runs after the initial translations + // check. So resort to a setTimeout and check a few times. This relies on timing, + // but _cannot fail_ if it's working correctly. It _will most likely fail_ if + // this page accidentally gets translated. + + const timeout = 10; + + info("Waiting for the timeout."); + // eslint-disable-next-line mozilla/no-arbitrary-setTimeout + await new Promise(resolve => setTimeout(resolve, timeout)); + is( + document.querySelector("h1").innerText, + `"The Wonderful Wizard of Oz" by L. Frank Baum`, + `The page remains untranslated after ${(i + 1) * timeout}ms.` + ); + } + }, + }); +}); + +/** + * Check that the full page translation feature works. + */ +add_task(async function test_language_identification_for_page_translation() { + await autoTranslatePage({ + page: TRANSLATIONS_TESTER_NO_TAG, + detectedLangTag: "es", + detectedLanguageConfidence: 0.95, + resolveLanguageIdDownloads: true, + languagePairs: [ + { fromLang: "es", toLang: "en", isBeta: false }, + { fromLang: "en", toLang: "es", isBeta: false }, + ], + runInPage: async TranslationsTest => { + const selectors = TranslationsTest.getSelectors(); + + await TranslationsTest.assertTranslationResult( + "The main title gets translated.", + selectors.getH1, + "DON QUIJOTE DE LA MANCHA [es to en, html]" + ); + + await TranslationsTest.assertTranslationResult( + "The last paragraph gets translated. It is out of the viewport.", + selectors.getLastParagraph, + "— PUES, AUNQUE MOVÁIS MÁS BRAZOS QUE LOS DEL GIGANTE BRIAREO, ME LO HABÉIS DE PAGAR. [es to en, html]" + ); + + selectors.getH1().innerText = "Este es un titulo"; + + await TranslationsTest.assertTranslationResult( + "Mutations get tracked", + selectors.getH1, + "ESTE ES UN TITULO [es to en]" + ); + + await TranslationsTest.assertTranslationResult( + "Other languages do not get translated.", + selectors.getHeader, + "The following is an excerpt from Don Quijote de la Mancha, which is in the public domain" + ); + }, + }); +}); diff --git a/toolkit/components/translations/tests/browser/browser_remote_settings.js b/toolkit/components/translations/tests/browser/browser_remote_settings.js new file mode 100644 index 0000000000..c77201e45d --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_remote_settings.js @@ -0,0 +1,303 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * @typedef {import("../translations").RemoteSettingsClient} RemoteSettingsClient + * @typedef {import("../../translations").TranslationModelRecord} TranslationModelRecord + */ + +const { RemoteSettings } = ChromeUtils.importESModule( + "resource://services-settings/remote-settings.sys.mjs" +); + +// The full Firefox version string. +const firefoxFullVersion = AppConstants.MOZ_APP_VERSION_DISPLAY; + +// The Firefox major version string (i.e. the first set of digits). +const firefoxMajorVersion = firefoxFullVersion.match(/\d+/); + +// The Firefox "AlphaZero" version string. +// This is a version that is less than even the latest Nightly +// which is of the form `${firefoxMajorVersion}.a1`. +const firefoxAlphaZeroVersion = `${firefoxMajorVersion}.a0`; + +/** + * Creates a local RemoteSettingsClient for use within tests. + * + * @param {string} mockedKey + * @returns {RemoteSettingsClient} + */ +async function createRemoteSettingsClient(mockedKey) { + const client = RemoteSettings(mockedKey); + await client.db.clear(); + await client.db.importChanges({}, Date.now()); + return client; +} + +// The following test ensures the capabilities of `filter_expression` in remote settings +// to successfully discriminate against the Firefox version when retrieving records. +// +// This is used when making major breaking changes that would require particular records +// to only show up in certain versions of Firefox, such as actual code changes that no +// longer allow compatibility with given records. +// +// Some examples might be: +// +// - Modifying a wasm.js file that is no longer compatible with a previous wasm binary record. +// In such a case, the old binary record would need to be shipped in versions with the old +// wasm.js file, and the new binary record would need to be shipped in version with the new +// wasm.js file. +// +// - Switching to a different library for translation or language detection. +// Using a different library would not only mean major changes to the code, but it would +// certainly mean that the records for the old libraries are no longer compatible. +// We will need to ship those records only in older versions of Firefox that utilize the old +// libraries, and ship new records in the new versions of Firefox. +add_task(async function test_filter_current_firefox_version() { + // Create a new RemoteSettingsClient just for this test. + let client = await createRemoteSettingsClient( + "test_filter_current_firefox_version" + ); + + // Create a list of records that are expected to pass the filter_expression and be + // successfully retrieved from the remote settings client. + const expectedPresentRecords = [ + { + name: `undefined filter expression`, + filter_expression: undefined, + }, + { + name: `null filter expression`, + filter_expression: null, + }, + { + name: `empty filter expression`, + filter_expression: ``, + }, + { + name: `env.version == ${firefoxFullVersion}`, + filter_expression: `env.version|versionCompare('${firefoxFullVersion}') == 0`, + }, + { + name: `env.version > ${firefoxAlphaZeroVersion}`, + filter_expression: `env.version|versionCompare('${firefoxAlphaZeroVersion}') > 0`, + }, + ]; + for (let record of expectedPresentRecords) { + client.db.create(record); + } + + // Create a list of records that are expected fo fail the filter_expression and be + // absent when we retrieve the records from the remote settings client. + const expectedAbsentRecords = [ + { + name: `env.version < 1`, + filter_expression: `env.version|versionCompare('1') < 0`, + }, + ]; + for (let record of expectedAbsentRecords) { + client.db.create(record); + } + + const retrievedRecords = await client.get(); + + // Ensure that each record that is expected to be present exists in the retrieved records. + for (let expectedPresentRecord of expectedPresentRecords) { + is( + retrievedRecords.some( + record => record.name == expectedPresentRecord.name + ), + true, + `The following record was expected to be present but was not found: ${expectedPresentRecord.name}\n` + ); + } + + // Ensure that each record that is expected to be absent does not exist in the retrieved records. + for (let expectedAbsentRecord of expectedAbsentRecords) { + is( + retrievedRecords.some(record => record.name == expectedAbsentRecord.name), + false, + `The following record was expected to be absent but was found: ${expectedAbsentRecord.name}\n` + ); + } + + // Ensure that the length of the retrieved records is exactly the length of the records expected to be present. + is( + retrievedRecords.length, + expectedPresentRecords.length, + `Expected ${expectedPresentRecords.length} items but got ${retrievedRecords.length} items\n` + ); +}); + +// The following test ensures that we are able to always retrieve the maximum +// compatible version of records. These are for version changes that do not +// require shipping different records based on a particular Firefox version, +// but rather for changes to model content or wasm runtimes that are fully +// compatible with the existing source code. +add_task(async function test_get_records_with_multiple_versions() { + // Create a new RemoteSettingsClient just for this test. + let client = await createRemoteSettingsClient( + "test_get_translation_model_records" + ); + + const lookupKey = record => + `${record.name}${TranslationsParent.languagePairKey( + record.fromLang, + record.toLang + )}`; + + // A mapping of each record name to its max version. + const maxVersionMap = {}; + + // Create a list of records that are all version 1.0 + /** @type {TranslationModelRecord[]} */ + const versionOneRecords = [ + { + id: crypto.randomUUID(), + name: "qualityModel.enes.bin", + fromLang: "en", + toLang: "es", + fileType: "qualityModel", + version: "1.0", + }, + { + id: crypto.randomUUID(), + name: "vocab.esen.spm", + fromLang: "en", + toLang: "es", + fileType: "vocab", + version: "1.0", + }, + { + id: crypto.randomUUID(), + name: "vocab.esen.spm", + fromLang: "es", + toLang: "en", + fileType: "vocab", + version: "1.0", + }, + { + id: crypto.randomUUID(), + name: "lex.50.50.enes.s2t.bin", + fromLang: "en", + toLang: "es", + fileType: "lex", + version: "1.0", + }, + { + id: crypto.randomUUID(), + name: "model.enes.intgemm.alphas.bin", + fromLang: "en", + toLang: "es", + fileType: "model", + version: "1.0", + }, + { + id: crypto.randomUUID(), + name: "vocab.deen.spm", + fromLang: "en", + toLang: "de", + fileType: "vocab", + version: "1.0", + }, + { + id: crypto.randomUUID(), + name: "lex.50.50.ende.s2t.bin", + fromLang: "en", + toLang: "de", + fileType: "lex", + version: "1.0", + }, + { + id: crypto.randomUUID(), + name: "model.ende.intgemm.alphas.bin", + fromLang: "en", + toLang: "de", + fileType: "model", + version: "1.0", + }, + ]; + versionOneRecords.reduce((map, record) => { + map[lookupKey(record)] = record.version; + return map; + }, maxVersionMap); + for (const record of versionOneRecords) { + client.db.create(record); + } + + // Create a list of records that are identical to some of the above, but with higher version numbers. + const higherVersionRecords = [ + { + id: crypto.randomUUID(), + name: "qualityModel.enes.bin", + fromLang: "en", + toLang: "es", + fileType: "qualityModel", + version: "1.1", + }, + { + id: crypto.randomUUID(), + name: "qualityModel.enes.bin", + fromLang: "en", + toLang: "es", + fileType: "qualityModel", + version: "1.2", + }, + { + id: crypto.randomUUID(), + name: "vocab.esen.spm", + fromLang: "en", + toLang: "es", + fileType: "vocab", + version: "1.1", + }, + ]; + higherVersionRecords.reduce((map, record) => { + const key = lookupKey(record); + if (record.version > map[key]) { + map[key] = record.version; + } + return map; + }, maxVersionMap); + for (const record of higherVersionRecords) { + client.db.create(record); + } + + TranslationsParent.mockTranslationsEngine( + client, + await createTranslationsWasmRemoteClient() + ); + + const retrievedRecords = await TranslationsParent.getMaxVersionRecords( + client, + { lookupKey } + ); + + for (const record of retrievedRecords) { + is( + lookupKey(record) in maxVersionMap, + true, + `Expected record ${record.name} to be contained in the nameToVersionMap, but found none\n` + ); + is( + record.version, + maxVersionMap[lookupKey(record)], + `Expected record ${record.name} to be version ${ + maxVersionMap[lookupKey(record)] + }, but found version ${record.version}\n` + ); + } + + const expectedSize = Object.keys(maxVersionMap).length; + is( + retrievedRecords.length, + expectedSize, + `Expected retrieved records to be the same size as the name-to-version map ( + ${expectedSize} + ), but found ${retrievedRecords.length}\n` + ); + + TranslationsParent.unmockTranslationsEngine(); +}); diff --git a/toolkit/components/translations/tests/browser/browser_translation_document.js b/toolkit/components/translations/tests/browser/browser_translation_document.js new file mode 100644 index 0000000000..ca309e6cc3 --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_translation_document.js @@ -0,0 +1,938 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * @type {typeof import("../../content/translations-document.sys.mjs")} + */ +const { TranslationsDocument } = ChromeUtils.importESModule( + "chrome://global/content/translations/translations-document.sys.mjs" +); + +/** + * @param {string} html + * @param {{ + * fakeTranslator?: (message: string) => Promise<string> + * }} [options] + */ +async function createDoc(html, options) { + await SpecialPowers.pushPrefEnv({ + set: [ + ["browser.translations.enable", true], + ["browser.translations.logLevel", "All"], + ], + }); + + const parser = new DOMParser(); + const document = parser.parseFromString(html, "text/html"); + + /** + * Fake translations by converting them to uppercase. + * @param {string} message + */ + async function fakeTranslator(message) { + /** + * @param {Node} node + */ + function upperCaseNode(node) { + if (typeof node.nodeValue === "string") { + node.nodeValue = node.nodeValue.toUpperCase(); + } + for (const childNode of node.childNodes) { + upperCaseNode(childNode); + } + } + const translatedDoc = parser.parseFromString(message, "text/html"); + upperCaseNode(translatedDoc.body); + return [translatedDoc.body.innerHTML]; + } + + const translationsDocument = new TranslationsDocument( + document, + "en", + 0, // This is a fake innerWindowID + options?.fakeTranslator ?? fakeTranslator, + options?.fakeTranslator ?? fakeTranslator + ); + + /** + * Test utility to check that the document matches the expected markup + * + * @param {string} html + */ + async function htmlMatches(message, html) { + const expected = naivelyPrettify(html); + try { + await BrowserTestUtils.waitForCondition( + () => naivelyPrettify(document.body.innerHTML) === expected, + "Waiting for HTML to match." + ); + ok(true, message); + } catch (error) { + console.error(error); + + // Provide a nice error message. + const actual = naivelyPrettify(document.body.innerHTML); + ok( + false, + `${message}\n\nExpected HTML:\n\n${expected}\n\nActual HTML:\n\n${actual}\n\n` + ); + } + } + + function translate() { + info("Running translation."); + translationsDocument.addRootElement(document.body); + } + + function cleanup() { + SpecialPowers.popPrefEnv(); + } + + return { translate, htmlMatches, cleanup, translationsDocument, document }; +} + +add_task(async function test_translated_div_element() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <div> + This is a simple translation. + </div> + `); + + await htmlMatches( + "The document starts out as expected.", + /* html */ ` + <div> + This is a simple translation. + </div> + ` + ); + + translate(); + + await htmlMatches( + "A single element with a single text node is translated into uppercase.", + /* html */ ` + <div> + THIS IS A SIMPLE TRANSLATION. + </div> + ` + ); + + cleanup(); +}); + +add_task(async function test_translated_textnode() { + const { translate, htmlMatches, cleanup } = await createDoc( + "This is a simple text translation." + ); + + translate(); + + await htmlMatches( + "A Text node at the root is translated into all caps", + "THIS IS A SIMPLE TEXT TRANSLATION." + ); + + cleanup(); +}); + +add_task(async function test_no_text_trees() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <div> + <div></div> + <span></span> + </div> + `); + + translate(); + + await htmlMatches( + "Trees with no text are not affected", + /* html */ ` + <div> + <div></div> + <span></span> + </div> + ` + ); + + cleanup(); +}); + +add_task(async function test_no_text_trees() { + const { translate, htmlMatches, cleanup } = await createDoc(""); + translate(); + await htmlMatches("No text is still no text", ""); + cleanup(); +}); + +add_task(async function test_translated_title() { + const { cleanup, document, translationsDocument } = + await createDoc(/* html */ ` + <!DOCTYPE html> + <html> + <head> + <meta charset="utf-8" /> + <title>This is an actual full page.</title> + </head> + <body> + + </body> + </html> + `); + + info("The title element is the only <head> element that is used as a root."); + translationsDocument.addRootElement( + document.getElementsByTagName("title")[0] + ); + + const translatedTitle = "THIS IS AN ACTUAL FULL PAGE."; + try { + await BrowserTestUtils.waitForCondition( + () => document.title === translatedTitle + ); + } catch (error) {} + is(document.title, translatedTitle, "The title was changed."); + + cleanup(); +}); + +add_task(async function test_translated_nested_elements() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <div class="menu-main-menu-container"> + <ul class="menu-list"> + <li class="menu-item menu-item-top-level"> + <a href="/">Latest Work</a> + </li> + <li class="menu-item menu-item-top-level"> + <a href="/category/interactive/">Creative Coding</a> + </li> + <li id="menu-id-categories" class="menu-item menu-item-top-level"> + <a href="#"><span class='category-arrow'>Categories</span></a> + </li> + </ul> + </div> + `); + + translate(); + + await htmlMatches( + "The nested elements are translated into all caps.", + /* html */ ` + <div class="menu-main-menu-container"> + <ul class="menu-list"> + <li class="menu-item menu-item-top-level" data-moz-translations-id="0"> + <a href="/" data-moz-translations-id="1">LATEST WORK</a> + </li> + <li class="menu-item menu-item-top-level" data-moz-translations-id="2"> + <a href="/category/interactive/" data-moz-translations-id="3">CREATIVE CODING</a> + </li> + <li id="menu-id-categories" class="menu-item menu-item-top-level" data-moz-translations-id="4"> + <a href="#" data-moz-translations-id="5"> + <span class="category-arrow" data-moz-translations-id="6">CATEGORIES</span> + </a> + </li> + </ul> + </div> + ` + ); + + cleanup(); +}); + +/** + * Only translate elements with a matching "from" language. + */ +add_task(async function test_translated_language() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <div> + <div> + No lang property + </div> + <div lang="en"> + Language matches + </div> + <div lang="fr"> + Language mismatch is ignored. + </div> + <div lang="en-US"> + Language match with region. + </div> + <div lang="fr"> + <div> + Language mismatch with + </div> + <div> + nested elements. + </div> + </div> + </div> + `); + + translate(); + + await htmlMatches( + "Language matching of elements behaves as expected.", + /* html */ ` + <div> + <div> + NO LANG PROPERTY + </div> + <div lang="en"> + LANGUAGE MATCHES + </div> + <div lang="fr"> + Language mismatch is ignored. + </div> + <div lang="en-US"> + LANGUAGE MATCH WITH REGION. + </div> + <div lang="fr"> + <div> + Language mismatch with + </div> + <div> + nested elements. + </div> + </div> + </div> + ` + ); + + cleanup(); +}); + +/** + * Test elements that have been marked as ignored. + */ +add_task(async function test_ignored_translations() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <div translate="yes"> + This is translated. + </div> + <div translate="no"> + This is not translated. + </div> + <div class="notranslate"> + This is not translated. + </div> + <div class="class-before notranslate class-after"> + This is not translated. + </div> + <div contenteditable> + This is not translated. + </div> + <div contenteditable="true"> + This is not translated. + </div> + <div contenteditable="false"> + This is translated. + </div> + `); + + translate(); + + await htmlMatches( + "Language matching of elements behaves as expected.", + /* html */ ` + <div translate="yes"> + THIS IS TRANSLATED. + </div> + <div translate="no"> + This is not translated. + </div> + <div class="notranslate"> + This is not translated. + </div> + <div class="class-before notranslate class-after"> + This is not translated. + </div> + <div contenteditable=""> + This is not translated. + </div> + <div contenteditable="true"> + This is not translated. + </div> + <div contenteditable="false"> + THIS IS TRANSLATED. + </div> + ` + ); + + cleanup(); +}); + +/** + * Test excluded tags. + */ +add_task(async function test_excluded_tags() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <div> + This is translated. + </div> + <code> + This is ignored + </code> + <script> + This is ignored + </script> + <textarea> + This is ignored + </textarea> + `); + + translate(); + + await htmlMatches( + "EXCLUDED_TAGS are not translated", + /* html */ ` + <div> + THIS IS TRANSLATED. + </div> + <code> + This is ignored + </code> + <script> + This is ignored + </script> + <textarea> + This is ignored + </textarea> + ` + ); + + cleanup(); +}); + +add_task(async function test_comments() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <!-- Comments don't make it to the DOM --> + <div> + <!-- These will be ignored in the translation. --> + This is translated. + </div> + `); + + translate(); + + await htmlMatches( + "Comments do not affect things.", + /* html */ ` + <div> + THIS IS TRANSLATED. + </div> + ` + ); + + cleanup(); +}); + +/** + * Test the batching behavior on what is sent in for a translation. + */ +add_task(async function test_translation_batching() { + const { translate, htmlMatches, cleanup } = await createDoc( + /* html */ ` + <div> + This is a simple section. + </div> + <div> + <span>This entire</span> section continues in a <b>batch</b>. + </div> + `, + { fakeTranslator: createBatchFakeTranslator() } + ); + + translate(); + + await htmlMatches( + "Batching", + /* html */ ` + <div> + aaaa aa a aaaaaa aaaaaaa. + </div> + <div> + <span data-moz-translations-id="0"> + bbbb bbbbbb + </span> + bbbbbbb bbbbbbbbb bb b + <b data-moz-translations-id="1"> + bbbbb + </b> + . + </div> + ` + ); + + cleanup(); +}); + +/** + * Test what happens when there are many inline elements. + */ +add_task(async function test_many_inlines() { + const { translate, htmlMatches, cleanup } = await createDoc( + /* html */ ` + <div> + <span> + This is a + </span> + <span> + much longer + </span> + <span> + section that includes + </span> + <span> + many span elements + </span> + <span> + to test what happens + </span> + <span> + in cases like this. + </span> + </div> + `, + { fakeTranslator: createBatchFakeTranslator() } + ); + + translate(); + + await htmlMatches( + "Batching", + /* html */ ` + <div> + <span data-moz-translations-id="0"> + aaaa aa a + </span> + <span data-moz-translations-id="1"> + aaaa aaaaaa + </span> + <span data-moz-translations-id="2"> + aaaaaaa aaaa aaaaaaaa + </span> + <span data-moz-translations-id="3"> + aaaa aaaa aaaaaaaa + </span> + <span data-moz-translations-id="4"> + aa aaaa aaaa aaaaaaa + </span> + <span data-moz-translations-id="5"> + aa aaaaa aaaa aaaa. + </span> + </div> + ` + ); + + cleanup(); +}); + +/** + * Test what happens when there are many inline elements. + */ +add_task(async function test_many_inlines() { + const { translate, htmlMatches, cleanup } = await createDoc( + /* html */ ` + <div> + <div> + This is a + </div> + <div> + much longer + </div> + <div> + section that includes + </div> + <div> + many div elements + </div> + <div> + to test what happens + </div> + <div> + in cases like this. + </div> + </div> + `, + { fakeTranslator: createBatchFakeTranslator() } + ); + + translate(); + + await htmlMatches( + "Batching", + /* html */ ` + <div> + <div> + aaaa aa a + </div> + <div> + bbbb bbbbbb + </div> + <div> + ccccccc cccc cccccccc + </div> + <div> + dddd ddd dddddddd + </div> + <div> + ee eeee eeee eeeeeee + </div> + <div> + ff fffff ffff ffff. + </div> + </div> + ` + ); + + cleanup(); +}); + +/** + * Test the "presumed" inline content behavior. + */ +add_task(async function test_presumed_inlines1() { + const { translate, htmlMatches, cleanup } = await createDoc( + /* html */ ` + <div> + Text node + <div>Block element</div> + </div> + `, + { fakeTranslator: createBatchFakeTranslator() } + ); + + translate(); + + await htmlMatches( + "Mixing a text node with otherwise block elements will send it all in as one batch.", + /* html */ ` + <div> + aaaa aaaa + <div data-moz-translations-id="0"> + aaaaa aaaaaaa + </div> + </div> + ` + ); + + cleanup(); +}); + +/** + * Test what happens when there are many inline elements. + */ +add_task(async function test_presumed_inlines2() { + const { translate, htmlMatches, cleanup } = await createDoc( + /* html */ ` + <div> + Text node + <span>Inline</span> + <div>Block Element</div> + </div> + `, + { fakeTranslator: createBatchFakeTranslator() } + ); + + translate(); + + await htmlMatches( + "Conflicting inline and block elements will be sent in together if there are more inlines", + /* html */ ` + <div> + aaaa aaaa + <span data-moz-translations-id="0"> + aaaaaa + </span> + <div data-moz-translations-id="1"> + aaaaa aaaaaaa + </div> + </div> + ` + ); + + cleanup(); +}); + +add_task(async function test_presumed_inlines3() { + const { translate, htmlMatches, cleanup } = await createDoc( + /* html */ ` + <div> + Text node + <span>Inline</span> + <div>Block Element</div> + <div>Block Element</div> + <div>Block Element</div> + </span> + `, + { fakeTranslator: createBatchFakeTranslator() } + ); + + translate(); + + // TODO - There is a bug in the implementation, see the untranslated "Text node" below. + // this was in the original implementation. + await htmlMatches( + "Conflicting inlines will be sent in as separate blocks if there are more block elements", + /* html */ ` + <div> + Text node + <span> + aaaaaa + </span> + <div> + bbbbb bbbbbbb + </div> + <div> + ccccc ccccccc + </div> + <div> + ddddd ddddddd + </div> + </div> + ` + ); + + cleanup(); +}); + +add_task(async function test_chunking_large_text() { + const { translate, htmlMatches, cleanup } = await createDoc( + /* html */ ` + <pre> + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque fermentum est ante, ut porttitor enim molestie et. Nam mattis ullamcorper justo a ultrices. Ut ac sodales lorem. Sed feugiat ultricies lacus. Proin dapibus sit amet nunc a ullamcorper. Donec leo purus, convallis quis urna non, semper pulvinar augue. Nulla placerat turpis arcu, sit amet imperdiet sapien tincidunt ut. Donec sit amet luctus lorem, sed consectetur lectus. Pellentesque est nisi, feugiat et ipsum quis, vestibulum blandit nulla. + + Proin accumsan sapien ut nibh mattis tincidunt. Donec facilisis nibh sodales, mattis risus et, malesuada lorem. Nam suscipit lacinia venenatis. Praesent ac consectetur ante. Vestibulum pulvinar ut massa in viverra. Nunc tincidunt tortor nunc. Vivamus sit amet hendrerit mi. Aliquam posuere velit non ante facilisis euismod. In ullamcorper, lacus vel hendrerit tincidunt, dui justo iaculis nulla, sit amet tincidunt nisl magna et urna. Sed varius tincidunt ligula. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nam sed gravida ligula. Donec tincidunt arcu eros, ac maximus magna auctor eu. Vivamus suscipit neque velit, in ullamcorper elit pulvinar et. Morbi auctor tempor risus, imperdiet placerat velit gravida vel. Duis ultricies accumsan libero quis molestie. + + Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Etiam nec arcu dapibus enim vulputate vulputate aliquet a libero. Nam hendrerit pulvinar libero, eget posuere quam porta eu. Pellentesque dignissim justo eu leo accumsan, sit amet suscipit ante gravida. Vivamus eu faucibus orci. Quisque sagittis tortor eget orci venenatis porttitor. Quisque mollis ipsum a dignissim dignissim. + + Aenean sagittis nisi lectus, non lacinia orci dapibus viverra. Donec diam lorem, tincidunt sed massa vel, vulputate tincidunt metus. In quam felis, egestas et faucibus faucibus, vestibulum quis tortor. Morbi odio mi, suscipit vitae leo in, consequat interdum augue. Quisque purus velit, dictum ac ante eget, volutpat dapibus ante. Suspendisse quis augue vitae velit elementum dictum nec aliquet nisl. Maecenas vestibulum quam augue, eu maximus urna blandit eu. Donec nunc risus, elementum id ligula nec, ultrices venenatis libero. Suspendisse ullamcorper ex ante, malesuada pulvinar sem placerat vel. + + In hac habitasse platea dictumst. Duis vulputate tellus arcu, at posuere ligula viverra luctus. Fusce ultrices malesuada neque vitae vehicula. Aliquam blandit nisi sed nibh facilisis, non varius turpis venenatis. Vestibulum ut velit laoreet, sagittis leo ac, pharetra ex. Aenean mollis risus sed nibh auctor, et feugiat neque iaculis. Fusce fermentum libero metus, at consectetur massa euismod sed. Mauris ut metus sit amet leo porttitor mollis. Vivamus tincidunt lorem non purus suscipit sollicitudin. Maecenas ut tristique elit. Ut eu volutpat turpis. Suspendisse nec tristique augue. Nullam faucibus egestas volutpat. Sed tempor eros et mi ultrices, nec feugiat eros egestas. + </pre> + `, + { fakeTranslator: createBatchFakeTranslator() } + ); + + translate(); + + await htmlMatches( + "Large chunks of text can be still sent in for translation in one big pass, " + + "this could be slow bad behavior for the user.", + /* html */ ` + <pre> + aaaaa aaaaa aaaaa aaa aaaa, aaaaaaaaaaa aaaaaaaaaa aaaa. aaaaaaa aaaaaaaaa aaa aaaa, aa aaaaaaaaa aaaa aaaaaaaa aa. aaa aaaaaa aaaaaaaaaaa aaaaa a aaaaaaaa. aa aa aaaaaaa aaaaa. aaa aaaaaaa aaaaaaaaa aaaaa. aaaaa aaaaaaa aaa aaaa aaaa a aaaaaaaaaaa. aaaaa aaa aaaaa, aaaaaaaaa aaaa aaaa aaa, aaaaaa aaaaaaaa aaaaa. aaaaa aaaaaaaa aaaaaa aaaa, aaa aaaa aaaaaaaaa aaaaaa aaaaaaaaa aa. aaaaa aaa aaaa aaaaaa aaaaa, aaa aaaaaaaaaaa aaaaaa. aaaaaaaaaaaa aaa aaaa, aaaaaaa aa aaaaa aaaa, aaaaaaaaaa aaaaaaa aaaaa. + + aaaaa aaaaaaaa aaaaaa aa aaaa aaaaaa aaaaaaaaa. aaaaa aaaaaaaaa aaaa aaaaaaa, aaaaaa aaaaa aa, aaaaaaaaa aaaaa. aaa aaaaaaaa aaaaaaa aaaaaaaaa. aaaaaaaa aa aaaaaaaaaaa aaaa. aaaaaaaaaa aaaaaaaa aa aaaaa aa aaaaaaa. aaaa aaaaaaaaa aaaaaa aaaa. aaaaaaa aaa aaaa aaaaaaaaa aa. aaaaaaa aaaaaaa aaaaa aaa aaaa aaaaaaaaa aaaaaaa. aa aaaaaaaaaaa, aaaaa aaa aaaaaaaaa aaaaaaaaa, aaa aaaaa aaaaaaa aaaaa, aaa aaaa aaaaaaaaa aaaa aaaaa aa aaaa. aaa aaaaaa aaaaaaaaa aaaaaa. aaaaaaaa aa aaaaaaaaa aaaaa aa aaaa aaaaa aaaaaa aa aaaaaaaa. aaa aaa aaaaaaa aaaaaa. aaaaa aaaaaaaaa aaaa aaaa, aa aaaaaaa aaaaa aaaaaa aa. aaaaaaa aaaaaaaa aaaaa aaaaa, aa aaaaaaaaaaa aaaa aaaaaaaa aa. aaaaa aaaaaa aaaaaa aaaaa, aaaaaaaaa aaaaaaaa aaaaa aaaaaaa aaa. aaaa aaaaaaaaa aaaaaaaa aaaaaa aaaa aaaaaaaa. + + aaaaaaaaaa aaaa aaaaa aaaaaa aa aaaaaaaa aaaa aaaaaa aa aaaaaaaa aaaaaaa aaaaaaa aaaaa; aaaaa aaa aaaa aaaaaaa aaaa aaaaaaaaa aaaaaaaaa aaaaaaa a aaaaaa. aaa aaaaaaaaa aaaaaaaa aaaaaa, aaaa aaaaaaa aaaa aaaaa aa. aaaaaaaaaaaa aaaaaaaaa aaaaa aa aaa aaaaaaaa, aaa aaaa aaaaaaaa aaaa aaaaaaa. aaaaaaa aa aaaaaaaa aaaa. aaaaaaa aaaaaaaa aaaaaa aaaa aaaa aaaaaaaaa aaaaaaaaa. aaaaaaa aaaaaa aaaaa a aaaaaaaaa aaaaaaaaa. + + aaaaaa aaaaaaaa aaaa aaaaaa, aaa aaaaaaa aaaa aaaaaaa aaaaaaa. aaaaa aaaa aaaaa, aaaaaaaaa aaa aaaaa aaa, aaaaaaaaa aaaaaaaaa aaaaa. aa aaaa aaaaa, aaaaaaa aa aaaaaaaa aaaaaaaa, aaaaaaaaaa aaaa aaaaaa. aaaaa aaaa aa, aaaaaaaa aaaaa aaa aa, aaaaaaaaa aaaaaaaa aaaaa. aaaaaaa aaaaa aaaaa, aaaaaa aa aaaa aaaa, aaaaaaaa aaaaaaa aaaa. aaaaaaaaaaa aaaa aaaaa aaaaa aaaaa aaaaaaaaa aaaaaa aaa aaaaaaa aaaa. aaaaaaaa aaaaaaaaaa aaaa aaaaa, aa aaaaaaa aaaa aaaaaaa aa. aaaaa aaaa aaaaa, aaaaaaaaa aa aaaaaa aaa, aaaaaaaa aaaaaaaaa aaaaaa. aaaaaaaaaaa aaaaaaaaaaa aa aaaa, aaaaaaaaa aaaaaaaa aaa aaaaaaaa aaa. + + aa aaa aaaaaaaaa aaaaaa aaaaaaaa. aaaa aaaaaaaaa aaaaaa aaaa, aa aaaaaaa aaaaaa aaaaaaa aaaaaa. aaaaa aaaaaaaa aaaaaaaaa aaaaa aaaaa aaaaaaaa. aaaaaaa aaaaaaa aaaa aaa aaaa aaaaaaaaa, aaa aaaaaa aaaaaa aaaaaaaaa. aaaaaaaaaa aa aaaaa aaaaaaa, aaaaaaaa aaa aa, aaaaaaaa aa. aaaaaa aaaaaa aaaaa aaa aaaa aaaaaa, aa aaaaaaa aaaaa aaaaaaa. aaaaa aaaaaaaaa aaaaaa aaaaa, aa aaaaaaaaaaa aaaaa aaaaaaa aaa. aaaaaa aa aaaaa aaa aaaa aaa aaaaaaaaa aaaaaa. aaaaaaa aaaaaaaaa aaaaa aaa aaaaa aaaaaaaa aaaaaaaaaaaa. aaaaaaaa aa aaaaaaaaa aaaa. aa aa aaaaaaaa aaaaaa. aaaaaaaaaaa aaa aaaaaaaaa aaaaa. aaaaaa aaaaaaaa aaaaaaa aaaaaaaa. aaa aaaaaa aaaa aa aa aaaaaaaa, aaa aaaaaaa aaaa aaaaaaa. + </pre> + ` + ); + + cleanup(); +}); + +add_task(async function test_reordering() { + const { translate, htmlMatches, cleanup } = await createDoc( + /* html */ ` + <span> + B - This was first. + </span> + <span> + A - This was second. + </span> + <span> + C - This was third. + </span> + `, + { fakeTranslator: reorderingTranslator } + ); + + translate(); + + await htmlMatches( + "Nodes can be re-ordered by the translator", + /* html */ ` + <span data-moz-translations-id="1"> + A - THIS WAS SECOND. + </span> + <span data-moz-translations-id="0"> + B - THIS WAS FIRST. + </span> + <span data-moz-translations-id="2"> + C - THIS WAS THIRD. + </span> + ` + ); + + cleanup(); +}); + +add_task(async function test_reordering2() { + const { translate, htmlMatches, cleanup } = await createDoc( + /* html */ ` + B - This was first. + <span> + A - This was second. + </span> + C - This was third. + `, + { fakeTranslator: reorderingTranslator } + ); + + translate(); + + // Note: ${" "} is used below to ensure that the whitespace is not stripped from + // the test. + await htmlMatches( + "Text nodes can be re-ordered.", + /* html */ ` + <span data-moz-translations-id="0"> + A - THIS WAS SECOND. + </span> + B - THIS WAS FIRST. +${" "} + C - THIS WAS THIRD. + ` + ); + + cleanup(); +}); + +add_task(async function test_mutations() { + const { translate, htmlMatches, cleanup, document } = + await createDoc(/* html */ ` + <div> + This is a simple translation. + </div> + `); + + translate(); + + await htmlMatches( + "It translates.", + /* html */ ` + <div> + THIS IS A SIMPLE TRANSLATION. + </div> + ` + ); + + info('Trigger the "childList" mutation.'); + const div = document.createElement("div"); + div.innerText = "This is an added node."; + document.body.appendChild(div); + + await htmlMatches( + "The added node gets translated.", + /* html */ ` + <div> + THIS IS A SIMPLE TRANSLATION. + </div> + <div> + THIS IS AN ADDED NODE. + </div> + ` + ); + + info('Trigger the "characterData" mutation.'); + document.querySelector("div").firstChild.nodeValue = + "This is a changed node."; + + await htmlMatches( + "The changed node gets translated", + /* html */ ` + <div> + THIS IS A CHANGED NODE. + </div> + <div> + THIS IS AN ADDED NODE. + </div> + ` + ); + cleanup(); +}); + +add_task(async function test_tables() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <table> + <tr> + <th>Table header 1</th> + <th>Table header 2</th> + </tr> + <tr> + <td>Table data 1</td> + <td>Table data 2</td> + </tr> + </table> + `); + + translate(); + + await htmlMatches( + "Tables are correctly translated.", + /* html */ ` + <table> + <tbody> + <tr> + <th> + TABLE HEADER 1 + </th> + <th> + TABLE HEADER 2 + </th> + </tr> + <tr> + <td> + TABLE DATA 1 + </td> + <td> + TABLE DATA 2 + </td> + </tr> + </tbody> + </table> + ` + ); + + cleanup(); +}); + +// TODO(Bug 1819205) - Attribute support needs to be added.ç +add_task(async function test_attributes() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <label title="Titles are user visible">Enter information:</label> + <input type="text" placeholder="This is a placeholder"> + `); + + translate(); + + // This is what this test should assert: + // eslint-disable-next-line no-unused-vars + const actualExpected = /* html */ ` + <label title="TITLES ARE USER VISIBLE"> + ENTER INFORMATION: + </label> + <input type="text" placeholder="THIS IS A PLACEHOLDER" > + `; + + await htmlMatches( + "Placeholders support needs to be added", + /* html */ ` + <label title="Titles are user visible"> + ENTER INFORMATION: + </label> + <input type="text" placeholder="This is a placeholder"> + ` + ); + + cleanup(); +}); diff --git a/toolkit/components/translations/tests/browser/browser_translations_actor.js b/toolkit/components/translations/tests/browser/browser_translations_actor.js new file mode 100644 index 0000000000..03ee1e2948 --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_translations_actor.js @@ -0,0 +1,217 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * This file contains unit tests for the translations actor. Generally it's preferable + * to test behavior in a full integration test, but occasionally it's useful to test + * specific implementation behavior. + */ + +/** + * Enforce the pivot language behavior in ensureLanguagePairsHavePivots. + */ +add_task(async function test_pivot_language_behavior() { + info( + "Expect 4 console.error messages notifying of the lack of a pivot language." + ); + + const { actor, cleanup } = await setupActorTest({ + languagePairs: [ + { fromLang: "en", toLang: "es", isBeta: false }, + { fromLang: "es", toLang: "en", isBeta: false }, + { fromLang: "en", toLang: "yue", isBeta: true }, + { fromLang: "yue", toLang: "en", isBeta: true }, + // This is not a bi-directional translation. + { fromLang: "is", toLang: "en", isBeta: false }, + // These are non-pivot languages. + { fromLang: "zh", toLang: "ja", isBeta: true }, + { fromLang: "ja", toLang: "zh", isBeta: true }, + ], + }); + + const { languagePairs } = await actor.getSupportedLanguages(); + + // The pairs aren't guaranteed to be sorted. + languagePairs.sort((a, b) => + TranslationsParent.languagePairKey(a.fromLang, a.toLang).localeCompare( + TranslationsParent.languagePairKey(b.fromLang, b.toLang) + ) + ); + + Assert.deepEqual( + languagePairs, + [ + { fromLang: "en", toLang: "es", isBeta: false }, + { fromLang: "en", toLang: "yue", isBeta: true }, + { fromLang: "es", toLang: "en", isBeta: false }, + { fromLang: "is", toLang: "en", isBeta: false }, + { fromLang: "yue", toLang: "en", isBeta: true }, + ], + "Non-pivot languages were removed." + ); + + return cleanup(); +}); + +async function usingAppLocale(locale, callback) { + info(`Mocking the locale "${locale}", expect missing resource errors.`); + const { availableLocales, requestedLocales } = Services.locale; + Services.locale.availableLocales = [locale]; + Services.locale.requestedLocales = [locale]; + + if (Services.locale.appLocaleAsBCP47 !== locale) { + throw new Error("Unable to change the app locale."); + } + await callback(); + + // Reset back to the originals. + Services.locale.availableLocales = availableLocales; + Services.locale.requestedLocales = requestedLocales; +} + +add_task(async function test_translating_to_and_from_app_language() { + const PIVOT_LANGUAGE = "en"; + + const { actor, cleanup } = await setupActorTest({ + languagePairs: [ + { fromLang: PIVOT_LANGUAGE, toLang: "es" }, + { fromLang: "es", toLang: PIVOT_LANGUAGE }, + { fromLang: PIVOT_LANGUAGE, toLang: "fr" }, + { fromLang: "fr", toLang: PIVOT_LANGUAGE }, + { fromLang: PIVOT_LANGUAGE, toLang: "pl" }, + { fromLang: "pl", toLang: PIVOT_LANGUAGE }, + ], + }); + + /** + * Each language pair has multiple models. De-duplicate the language pairs and + * return a sorted list. + */ + function getUniqueLanguagePairs(records) { + const langPairs = new Set(); + for (const { fromLang, toLang } of records) { + langPairs.add(TranslationsParent.languagePairKey(fromLang, toLang)); + } + return Array.from(langPairs) + .sort() + .map(langPair => { + const [fromLang, toLang] = langPair.split(","); + return { + fromLang, + toLang, + }; + }); + } + + function assertLanguagePairs({ + app, + requested, + message, + languagePairs, + isForDeletion, + }) { + return usingAppLocale(app, async () => { + Assert.deepEqual( + getUniqueLanguagePairs( + await actor.getRecordsForTranslatingToAndFromAppLanguage( + requested, + isForDeletion + ) + ), + languagePairs, + message + ); + }); + } + + await assertLanguagePairs({ + message: + "When the app locale is the pivot language, download another language.", + app: PIVOT_LANGUAGE, + requested: "fr", + languagePairs: [ + { fromLang: PIVOT_LANGUAGE, toLang: "fr" }, + { fromLang: "fr", toLang: PIVOT_LANGUAGE }, + ], + }); + + await assertLanguagePairs({ + message: "When a pivot language is required, they are both downloaded.", + app: "fr", + requested: "pl", + languagePairs: [ + { fromLang: PIVOT_LANGUAGE, toLang: "fr" }, + { fromLang: PIVOT_LANGUAGE, toLang: "pl" }, + { fromLang: "fr", toLang: PIVOT_LANGUAGE }, + { fromLang: "pl", toLang: PIVOT_LANGUAGE }, + ], + }); + + await assertLanguagePairs({ + message: + "When downloading the pivot language, only download the one for the app's locale.", + app: "es", + requested: PIVOT_LANGUAGE, + languagePairs: [ + { fromLang: PIVOT_LANGUAGE, toLang: "es" }, + { fromLang: "es", toLang: PIVOT_LANGUAGE }, + ], + }); + + await assertLanguagePairs({ + message: + "Delete just the requested language when the app locale is the pivot language", + app: PIVOT_LANGUAGE, + requested: "fr", + isForDeletion: true, + languagePairs: [ + { fromLang: PIVOT_LANGUAGE, toLang: "fr" }, + { fromLang: "fr", toLang: PIVOT_LANGUAGE }, + ], + }); + + await assertLanguagePairs({ + message: "Delete just the requested language, and not the pivot.", + app: "fr", + requested: "pl", + isForDeletion: true, + languagePairs: [ + { fromLang: PIVOT_LANGUAGE, toLang: "pl" }, + { fromLang: "pl", toLang: PIVOT_LANGUAGE }, + ], + }); + + await assertLanguagePairs({ + message: "Delete just the requested language, and not the pivot.", + app: "fr", + requested: "pl", + isForDeletion: true, + languagePairs: [ + { fromLang: PIVOT_LANGUAGE, toLang: "pl" }, + { fromLang: "pl", toLang: PIVOT_LANGUAGE }, + ], + }); + + await assertLanguagePairs({ + message: "Delete just the pivot → app and app → pivot.", + app: "es", + requested: PIVOT_LANGUAGE, + isForDeletion: true, + languagePairs: [ + { fromLang: PIVOT_LANGUAGE, toLang: "es" }, + { fromLang: "es", toLang: PIVOT_LANGUAGE }, + ], + }); + + await assertLanguagePairs({ + message: + "If the app and request language are the same, nothing is returned.", + app: "fr", + requested: "fr", + languagePairs: [], + }); + + return cleanup(); +}); diff --git a/toolkit/components/translations/tests/browser/browser_translations_actor_preferred_language.js b/toolkit/components/translations/tests/browser/browser_translations_actor_preferred_language.js new file mode 100644 index 0000000000..50b548b6f1 --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_translations_actor_preferred_language.js @@ -0,0 +1,81 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +function waitForAppLocaleChanged() { + new Promise(resolve => { + function onChange() { + Services.obs.removeObserver(onChange, "intl:app-locales-changed"); + resolve(); + } + Services.obs.addObserver(onChange, "intl:app-locales-changed"); + }); +} + +async function testWithLocales({ + systemLocales, + appLocales, + webLanguages, + test, +}) { + const cleanup = await mockLocales({ + systemLocales, + appLocales, + webLanguages, + }); + test(); + return cleanup(); +} + +add_task(async function test_preferred_language() { + await testWithLocales({ + systemLocales: ["en-US"], + appLocales: ["en-US"], + webLanguages: ["en-US"], + test() { + Assert.deepEqual( + TranslationsParent.getPreferredLanguages(), + ["en"], + "When all locales are English, only English is preferred." + ); + }, + }); + + await testWithLocales({ + systemLocales: ["es-ES"], + appLocales: ["en-US"], + webLanguages: ["en-US"], + test() { + Assert.deepEqual( + TranslationsParent.getPreferredLanguages(), + ["en", "es"], + "When the operating system differs, it is added tot he end of the preferred languages." + ); + }, + }); + + await testWithLocales({ + systemLocales: ["zh-TW", "zh-CN", "de"], + appLocales: ["pt-BR", "pl"], + webLanguages: ["cs", "hu"], + test() { + Assert.deepEqual( + TranslationsParent.getPreferredLanguages(), + [ + // appLocales, notice that "en" is the last fallback. + "pt", + "pl", + "en", + // webLanguages + "cs", + "hu", + // systemLocales + "zh", + "de", + ], + "Demonstrate an unrealistic but complicated locale situation." + ); + }, + }); +}); diff --git a/toolkit/components/translations/tests/browser/browser_translations_lang_tags.js b/toolkit/components/translations/tests/browser/browser_translations_lang_tags.js new file mode 100644 index 0000000000..8320d1ac29 --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_translations_lang_tags.js @@ -0,0 +1,125 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const PIVOT_LANGUAGE = "en"; + +const LANGUAGE_PAIRS = [ + { fromLang: PIVOT_LANGUAGE, toLang: "es" }, + { fromLang: "es", toLang: PIVOT_LANGUAGE }, + { fromLang: PIVOT_LANGUAGE, toLang: "fr" }, + { fromLang: "fr", toLang: PIVOT_LANGUAGE }, + { fromLang: PIVOT_LANGUAGE, toLang: "pl" }, + { fromLang: "pl", toLang: PIVOT_LANGUAGE }, +]; + +async function runLangTagsTest( + { + systemLocales, + appLocales, + webLanguages, + page, + languagePairs = LANGUAGE_PAIRS, + }, + langTags +) { + const cleanupLocales = await mockLocales({ + systemLocales, + appLocales, + webLanguages, + }); + + const { cleanup: cleanupTestPage } = await loadTestPage({ + page, + languagePairs, + }); + const actor = getTranslationsParent(); + + await TestUtils.waitForCondition( + async () => (await actor.getLangTagsForTranslation())?.docLangTag, + "Waiting for a document language tag to be found." + ); + + Assert.deepEqual(await actor.getLangTagsForTranslation(), langTags); + + await cleanupLocales(); + await cleanupTestPage(); +} + +add_task(async function test_lang_tags_direct_translations() { + info( + "Test the detected languages for translations when a translation pair is available" + ); + await runLangTagsTest( + { + systemLocales: ["en"], + appLocales: ["en"], + webLanguages: ["en"], + page: TRANSLATIONS_TESTER_ES, + }, + { + docLangTag: "es", + userLangTag: "en", + isDocLangTagSupported: true, + } + ); +}); + +add_task(async function test_lang_tags_with_pivots() { + info("Test the detected languages for translations when a pivot is needed."); + await runLangTagsTest( + { + systemLocales: ["fr"], + appLocales: ["fr", "en"], + webLanguages: ["fr", "en"], + page: TRANSLATIONS_TESTER_ES, + }, + { + docLangTag: "es", + userLangTag: "fr", + isDocLangTagSupported: true, + } + ); +}); + +add_task(async function test_lang_tags_with_pivots_second_preferred() { + info( + "Test using a pivot language when the first preferred lang tag doesn't match" + ); + await runLangTagsTest( + { + systemLocales: ["it"], + appLocales: ["it", "en"], + webLanguages: ["it", "en"], + page: TRANSLATIONS_TESTER_ES, + }, + { + docLangTag: "es", + userLangTag: "en", + isDocLangTagSupported: true, + } + ); +}); + +add_task(async function test_lang_tags_with_non_supported_doc_language() { + info("Test using a pivot language when the doc language isn't supported"); + await runLangTagsTest( + { + systemLocales: ["fr"], + appLocales: ["fr", "en"], + webLanguages: ["fr", "en"], + page: TRANSLATIONS_TESTER_ES, + languagePairs: [ + { fromLang: PIVOT_LANGUAGE, toLang: "fr" }, + { fromLang: "fr", toLang: PIVOT_LANGUAGE }, + // No Spanish support. + ], + }, + { + docLangTag: "es", + userLangTag: "fr", + isDocLangTagSupported: false, + } + ); +}); diff --git a/toolkit/components/translations/tests/browser/head.js b/toolkit/components/translations/tests/browser/head.js new file mode 100644 index 0000000000..08c5793a46 --- /dev/null +++ b/toolkit/components/translations/tests/browser/head.js @@ -0,0 +1,9 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +Services.scriptloader.loadSubScript( + "chrome://mochitests/content/browser/toolkit/components/translations/tests/browser/shared-head.js", + this +); diff --git a/toolkit/components/translations/tests/browser/shared-head.js b/toolkit/components/translations/tests/browser/shared-head.js new file mode 100644 index 0000000000..7ccc461b83 --- /dev/null +++ b/toolkit/components/translations/tests/browser/shared-head.js @@ -0,0 +1,1011 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +// Avoid about:blank's non-standard behavior. +const BLANK_PAGE = + "data:text/html;charset=utf-8,<!DOCTYPE html><title>Blank</title>Blank page"; + +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_COM_PREFIX + DIR_PATH + "translations-tester-en.html"; +const TRANSLATIONS_TESTER_ES = + 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_COM_PREFIX + DIR_PATH + "translations-tester-no-tag.html"; + +/** + * The mochitest runs in the parent process. This function opens up a new tab, + * opens up about:translations, and passes the test requirements into the content process. + * + * @template T + * + * @param {object} options + * + * @param {T} options.dataForContent + * The data must support structural cloning and will be passed into the + * content process. + * + * @param {(args: { dataForContent: T, selectors: Record<string, string> }) => Promise<void>} options.runInPage + * This function must not capture any values, as it will be cloned in the content process. + * Any required data should be passed in using the "dataForContent" parameter. The + * "selectors" property contains any useful selectors for the content. + * + * @param {boolean} [options.disabled] + * Disable the panel through a pref. + * + * @param {number} detectedLanguageConfidence + * This is the value for the MockedLanguageIdEngine to give as a confidence score for + * the mocked detected language. + * + * @param {string} detectedLangTag + * This is the BCP 47 language tag for the MockedLanguageIdEngine to return as + * the mocked detected language. + * + * @param {Array<{ fromLang: string, toLang: string, isBeta: boolean }>} options.languagePairs + * The translation languages pairs to mock for the test. + * + * @param {Array<[string, string]>} options.prefs + * Prefs to push on for the test. + */ +async function openAboutTranslations({ + dataForContent, + disabled, + runInPage, + detectedLanguageConfidence, + detectedLangTag, + languagePairs = DEFAULT_LANGUAGE_PAIRS, + prefs, +}) { + await SpecialPowers.pushPrefEnv({ + set: [ + // Enabled by default. + ["browser.translations.enable", !disabled], + ["browser.translations.logLevel", "All"], + ...(prefs ?? []), + ], + }); + + /** + * Collect any relevant selectors for the page here. + */ + const selectors = { + pageHeader: '[data-l10n-id="about-translations-header"]', + fromLanguageSelect: "select#language-from", + toLanguageSelect: "select#language-to", + translationTextarea: "textarea#translation-from", + translationResult: "#translation-to", + translationResultBlank: "#translation-to-blank", + translationInfo: "#translation-info", + noSupportMessage: "[data-l10n-id='about-translations-no-support']", + }; + + // Start the tab at a blank page. + let tab = await BrowserTestUtils.openNewForegroundTab( + gBrowser, + BLANK_PAGE, + true // waitForLoad + ); + + const { removeMocks, remoteClients } = await createAndMockRemoteSettings({ + languagePairs, + // TODO(Bug 1814168) - Do not test download behavior as this is not robustly + // handled for about:translations yet. + autoDownloadFromRemoteSettings: true, + detectedLangTag, + detectedLanguageConfidence, + }); + + // Now load the about:translations page, since the actor could be mocked. + BrowserTestUtils.loadURIString(tab.linkedBrowser, "about:translations"); + await BrowserTestUtils.browserLoaded(tab.linkedBrowser); + + // Resolve the files. + await remoteClients.languageIdModels.resolvePendingDownloads(1); + // The language id and translation engine each have a wasm file, so expect 2 downloads. + await remoteClients.translationsWasm.resolvePendingDownloads(2); + await remoteClients.translationModels.resolvePendingDownloads( + languagePairs.length * FILES_PER_LANGUAGE_PAIR + ); + + await ContentTask.spawn( + tab.linkedBrowser, + { dataForContent, selectors }, + runInPage + ); + + removeMocks(); + + BrowserTestUtils.removeTab(tab); + await SpecialPowers.popPrefEnv(); +} + +/** + * Naively prettify's html based on the opening and closing tags. This is not robust + * for general usage, but should be adequate for these tests. + * @param {string} html + * @returns {string} + */ +function naivelyPrettify(html) { + let result = ""; + let indent = 0; + + function addText(actualEndIndex) { + const text = html.slice(startIndex, actualEndIndex).trim(); + if (text) { + for (let i = 0; i < indent; i++) { + result += " "; + } + result += text + "\n"; + } + startIndex = actualEndIndex; + } + + let startIndex = 0; + let endIndex = 0; + for (; endIndex < html.length; endIndex++) { + if ( + html[endIndex] === " " || + html[endIndex] === "\t" || + html[endIndex] === "n" + ) { + // Skip whitespace. + // " <div>foobar</div>" + // ^^^ + startIndex = endIndex; + continue; + } + + // Find all of the text. + // "<div>foobar</div>" + // ^^^^^^ + while (endIndex < html.length && html[endIndex] !== "<") { + endIndex++; + } + + addText(endIndex); + + if (html[endIndex] === "<") { + if (html[endIndex + 1] === "/") { + // "<div>foobar</div>" + // ^ + while (endIndex < html.length && html[endIndex] !== ">") { + endIndex++; + } + indent--; + addText(endIndex + 1); + } else { + // "<div>foobar</div>" + // ^ + while (endIndex < html.length && html[endIndex] !== ">") { + endIndex++; + } + // "<div>foobar</div>" + // ^ + addText(endIndex + 1); + indent++; + } + } + } + + return result.trim(); +} + +/** + * This fake translator reports on the batching of calls by replacing the text + * with a letter. Each call of the function moves the letter forward alphabetically. + * + * So consecutive calls would transform things like: + * "First translation" -> "aaaa aaaaaaaaa" + * "Second translation" -> "bbbbb bbbbbbbbb" + * "Third translation" -> "cccc ccccccccc" + * + * This can visually show what the translation batching behavior looks like. + */ +function createBatchFakeTranslator() { + let letter = "a"; + /** + * @param {string} message + */ + return async function fakeTranslator(message) { + /** + * @param {Node} node + */ + function transformNode(node) { + if (typeof node.nodeValue === "string") { + node.nodeValue = node.nodeValue.replace(/\w/g, letter); + } + for (const childNode of node.childNodes) { + transformNode(childNode); + } + } + + const parser = new DOMParser(); + const translatedDoc = parser.parseFromString(message, "text/html"); + transformNode(translatedDoc.body); + + // "Increment" the letter. + letter = String.fromCodePoint(letter.codePointAt(0) + 1); + + return [translatedDoc.body.innerHTML]; + }; +} + +/** + * This fake translator reorders Nodes to be in alphabetical order, and then + * uppercases the text. This allows for testing the reordering behavior of the + * translation engine. + * + * @param {string} message + */ +async function reorderingTranslator(message) { + /** + * @param {Node} node + */ + function transformNode(node) { + if (typeof node.nodeValue === "string") { + node.nodeValue = node.nodeValue.toUpperCase(); + } + const nodes = [...node.childNodes]; + nodes.sort((a, b) => + (a.textContent?.trim() ?? "").localeCompare(b.textContent?.trim() ?? "") + ); + for (const childNode of nodes) { + childNode.remove(); + } + for (const childNode of nodes) { + // Re-append in sorted order. + node.appendChild(childNode); + transformNode(childNode); + } + } + + const parser = new DOMParser(); + const translatedDoc = parser.parseFromString(message, "text/html"); + transformNode(translatedDoc.body); + + return [translatedDoc.body.innerHTML]; +} + +/** + * @returns {import("../../actors/TranslationsParent.sys.mjs").TranslationsParent} + */ +function getTranslationsParent() { + return gBrowser.selectedBrowser.browsingContext.currentWindowGlobal.getActor( + "Translations" + ); +} + +/** + * This is for tests that don't need a browser page to run. + */ +async function setupActorTest({ + languagePairs, + prefs, + detectedLanguageConfidence, + detectedLangTag, +}) { + await SpecialPowers.pushPrefEnv({ + set: [ + // Enabled by default. + ["browser.translations.enable", true], + ["browser.translations.logLevel", "All"], + ...(prefs ?? []), + ], + }); + + const { remoteClients, removeMocks } = await createAndMockRemoteSettings({ + languagePairs, + detectedLangTag, + detectedLanguageConfidence, + }); + + // Create a new tab so each test gets a new actor, and doesn't re-use the old one. + const tab = await BrowserTestUtils.openNewForegroundTab( + gBrowser, + BLANK_PAGE, + true // waitForLoad + ); + + return { + actor: getTranslationsParent(), + remoteClients, + cleanup() { + BrowserTestUtils.removeTab(tab); + removeMocks(); + return SpecialPowers.popPrefEnv(); + }, + }; +} + +/** + * Provide some default language pairs when none are provided. + */ +const DEFAULT_LANGUAGE_PAIRS = [ + { fromLang: "en", toLang: "es", isBeta: false }, + { fromLang: "es", toLang: "en", isBeta: false }, +]; + +async function createAndMockRemoteSettings({ + languagePairs = DEFAULT_LANGUAGE_PAIRS, + detectedLanguageConfidence = 0.5, + detectedLangTag = "en", + autoDownloadFromRemoteSettings = false, +}) { + const remoteClients = { + translationModels: await createTranslationModelsRemoteClient( + autoDownloadFromRemoteSettings, + languagePairs + ), + translationsWasm: await createTranslationsWasmRemoteClient( + autoDownloadFromRemoteSettings + ), + languageIdModels: await createLanguageIdModelsRemoteClient( + autoDownloadFromRemoteSettings + ), + }; + + TranslationsParent.mockTranslationsEngine( + remoteClients.translationModels.client, + remoteClients.translationsWasm.client + ); + + TranslationsParent.mockLanguageIdentification( + detectedLangTag, + detectedLanguageConfidence, + remoteClients.languageIdModels.client + ); + return { + removeMocks() { + TranslationsParent.unmockTranslationsEngine(); + TranslationsParent.unmockLanguageIdentification(); + }, + remoteClients, + }; +} + +async function loadTestPage({ + languagePairs, + autoDownloadFromRemoteSettings = false, + detectedLanguageConfidence, + detectedLangTag, + page, + prefs, + permissionsUrls = [], +}) { + Services.fog.testResetFOG(); + await SpecialPowers.pushPrefEnv({ + set: [ + // Enabled by default. + ["browser.translations.enable", true], + ["browser.translations.logLevel", "All"], + ...(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( + gBrowser, + BLANK_PAGE, + true // waitForLoad + ); + + const { remoteClients, removeMocks } = await createAndMockRemoteSettings({ + languagePairs, + detectedLanguageConfidence, + detectedLangTag, + autoDownloadFromRemoteSettings, + }); + + BrowserTestUtils.loadURIString(tab.linkedBrowser, page); + await BrowserTestUtils.browserLoaded(tab.linkedBrowser); + + return { + tab, + remoteClients, + + /** + * @param {number} count - Count of the language pairs expected. + */ + async resolveDownloads(count) { + await remoteClients.translationsWasm.resolvePendingDownloads(1); + await remoteClients.translationModels.resolvePendingDownloads( + FILES_PER_LANGUAGE_PAIR * count + ); + }, + + /** + * @param {number} count - Count of the language pairs expected. + */ + async rejectDownloads(count) { + await remoteClients.translationsWasm.rejectPendingDownloads(1); + await remoteClients.translationModels.rejectPendingDownloads( + FILES_PER_LANGUAGE_PAIR * count + ); + }, + + async resolveLanguageIdDownloads() { + await remoteClients.translationsWasm.resolvePendingDownloads(1); + await remoteClients.languageIdModels.resolvePendingDownloads(1); + }, + + /** + * @returns {Promise<void>} + */ + cleanup() { + removeMocks(); + Services.fog.testResetFOG(); + BrowserTestUtils.removeTab(tab); + return Promise.all([ + SpecialPowers.popPrefEnv(), + SpecialPowers.popPermissions(), + ]); + }, + + /** + * Runs a callback in the content page. The function's contents are serialized as + * a string, and run in the page. The `translations-test.mjs` module is made + * available to the page. + * + * @param {(TranslationsTest: import("./translations-test.mjs")) => any} callback + * @returns {Promise<void>} + */ + runInPage(callback) { + // ContentTask.spawn runs the `Function.prototype.toString` on this function in + // order to send it into the content process. The following function is doing its + // own string manipulation in order to load in the TranslationsTest module. + const fn = new Function(/* js */ ` + const TranslationsTest = ChromeUtils.importESModule( + "chrome://mochitests/content/browser/toolkit/components/translations/tests/browser/translations-test.mjs" + ); + + // Pass in the values that get injected by the task runner. + TranslationsTest.setup({Assert, ContentTaskUtils, content}); + + return (${callback.toString()})(TranslationsTest); + `); + + return ContentTask.spawn( + tab.linkedBrowser, + {}, // Data to inject. + fn + ); + }, + }; +} + +/** + * Captures any reported errors in the TranslationsParent. + * + * @param {Function} callback + * @returns {Array<{ error: Error, args: any[] }>} + */ +async function captureTranslationsError(callback) { + const { reportError } = TranslationsParent; + + let errors = []; + TranslationsParent.reportError = (error, ...args) => { + errors.push({ error, args }); + }; + + await callback(); + + // Restore the original function. + TranslationsParent.reportError = reportError; + return errors; +} + +/** + * Load a test page and run + * @param {Object} options - The options for `loadTestPage` plus a `runInPage` function. + */ +async function autoTranslatePage(options) { + const { cleanup, runInPage } = await loadTestPage({ + autoDownloadFromRemoteSettings: true, + prefs: [ + ["browser.translations.autoTranslate", true], + ...(options.prefs ?? []), + ], + ...options, + }); + await runInPage(options.runInPage); + await cleanup(); +} + +/** + * @param {RemoteSettingsClient} client + * @param {boolean} autoDownloadFromRemoteSettings - Skip the manual download process, + * and automatically download the files. Normally it's preferrable to manually trigger + * the downloads to trigger the download behavior, but this flag lets you bypass this + * and automatically download the files. + */ +function createAttachmentMock(client, autoDownloadFromRemoteSettings) { + const pendingDownloads = []; + client.attachments.download = record => + new Promise((resolve, reject) => { + console.log("Download requested:", client.collectionName, record.name); + if (autoDownloadFromRemoteSettings) { + resolve({ buffer: new ArrayBuffer() }); + } else { + pendingDownloads.push({ record, resolve, reject }); + } + }); + + function resolvePendingDownloads(expectedDownloadCount) { + info( + `Resolving ${expectedDownloadCount} mocked downloads for "${client.collectionName}"` + ); + return downloadHandler(expectedDownloadCount, download => + download.resolve({ buffer: new ArrayBuffer() }) + ); + } + + async function rejectPendingDownloads(expectedDownloadCount) { + info( + `Intentionally rejecting ${expectedDownloadCount} mocked downloads for "${client.collectionName}"` + ); + + // Add 1 to account for the original attempt. + const attempts = TranslationsParent.MAX_DOWNLOAD_RETRIES + 1; + return downloadHandler(expectedDownloadCount * attempts, download => + download.reject(new Error("Intentionally rejecting downloads.")) + ); + } + + async function downloadHandler(expectedDownloadCount, action) { + const names = []; + let maxTries = 100; + while (names.length < expectedDownloadCount && maxTries-- > 0) { + await new Promise(resolve => setTimeout(resolve, 0)); + let download = pendingDownloads.shift(); + if (!download) { + // Uncomment the following to debug download issues: + // console.log(`No pending download:`, client.collectionName, names.length); + continue; + } + console.log(`Handling download:`, client.collectionName); + action(download); + names.push(download.record.name); + } + + // This next check is not guaranteed to catch an unexpected download, but wait + // at least one event loop tick to see if any more downloads were added. + await new Promise(resolve => setTimeout(resolve, 0)); + + if (pendingDownloads.length) { + throw new Error( + `An unexpected download was found, only expected ${expectedDownloadCount} downloads` + ); + } + + return names.sort((a, b) => a.localeCompare(b)); + } + + async function assertNoNewDownloads() { + await new Promise(resolve => setTimeout(resolve, 0)); + is( + pendingDownloads.length, + 0, + `No downloads happened for "${client.collectionName}"` + ); + } + + return { + client, + pendingDownloads, + resolvePendingDownloads, + rejectPendingDownloads, + assertNoNewDownloads, + }; +} + +/** + * The amount of files that are generated per mocked language pair. + */ +const FILES_PER_LANGUAGE_PAIR = 3; + +/** + * Creates a local RemoteSettingsClient for use within tests. + * + * @param {boolean} autoDownloadFromRemoteSettings + * @param {Object[]} langPairs + * @returns {RemoteSettingsClient} + */ +async function createTranslationModelsRemoteClient( + autoDownloadFromRemoteSettings, + langPairs +) { + const records = []; + for (const { fromLang, toLang, isBeta } of langPairs) { + const lang = fromLang + toLang; + const models = [ + { fileType: "model", name: `model.${lang}.intgemm.alphas.bin` }, + { fileType: "lex", name: `lex.50.50.${lang}.s2t.bin` }, + { fileType: "vocab", name: `vocab.${lang}.spm` }, + ]; + + if (models.length !== FILES_PER_LANGUAGE_PAIR) { + throw new Error("Files per language pair was wrong."); + } + + for (const { fileType, name } of models) { + records.push({ + id: crypto.randomUUID(), + name, + fromLang, + toLang, + fileType, + version: isBeta ? "0.1" : "1.0", + last_modified: Date.now(), + schema: Date.now(), + }); + } + } + + const { RemoteSettings } = ChromeUtils.importESModule( + "resource://services-settings/remote-settings.sys.mjs" + ); + const client = RemoteSettings("test-translation-models"); + const metadata = {}; + await client.db.clear(); + await client.db.importChanges(metadata, Date.now(), records); + + return createAttachmentMock(client, autoDownloadFromRemoteSettings); +} + +/** + * Creates a local RemoteSettingsClient for use within tests. + * + * @param {boolean} autoDownloadFromRemoteSettings + * @returns {RemoteSettingsClient} + */ +async function createTranslationsWasmRemoteClient( + autoDownloadFromRemoteSettings +) { + const records = ["bergamot-translator", "fasttext-wasm"].map(name => ({ + id: crypto.randomUUID(), + name, + version: "1.0", + last_modified: Date.now(), + schema: Date.now(), + })); + + const { RemoteSettings } = ChromeUtils.importESModule( + "resource://services-settings/remote-settings.sys.mjs" + ); + const client = RemoteSettings("test-translations-wasm"); + const metadata = {}; + await client.db.clear(); + await client.db.importChanges(metadata, Date.now(), records); + + return createAttachmentMock(client, autoDownloadFromRemoteSettings); +} + +/** + * Creates a local RemoteSettingsClient for use within tests. + * + * @param {boolean} autoDownloadFromRemoteSettings + * @returns {RemoteSettingsClient} + */ +async function createLanguageIdModelsRemoteClient( + autoDownloadFromRemoteSettings +) { + const records = [ + { + id: crypto.randomUUID(), + name: "lid.176.ftz", + version: "1.0", + last_modified: Date.now(), + schema: Date.now(), + }, + ]; + + const { RemoteSettings } = ChromeUtils.importESModule( + "resource://services-settings/remote-settings.sys.mjs" + ); + const client = RemoteSettings("test-language-id-models"); + const metadata = {}; + await client.db.clear(); + await client.db.importChanges(metadata, Date.now(), records); + + return createAttachmentMock(client, autoDownloadFromRemoteSettings); +} + +async function selectAboutPreferencesElements() { + const document = gBrowser.selectedBrowser.contentDocument; + + const rows = await TestUtils.waitForCondition(() => { + const elements = document.querySelectorAll(".translations-manage-language"); + if (elements.length !== 3) { + return false; + } + return elements; + }, "Waiting for manage language rows."); + + const [downloadAllRow, frenchRow, spanishRow] = rows; + + const downloadAllLabel = downloadAllRow.querySelector("label"); + const downloadAll = downloadAllRow.querySelector( + "#translations-manage-install-all" + ); + const deleteAll = downloadAllRow.querySelector( + "#translations-manage-delete-all" + ); + const frenchLabel = frenchRow.querySelector("label"); + const frenchDownload = frenchRow.querySelector( + `[data-l10n-id="translations-manage-download-button"]` + ); + const frenchDelete = frenchRow.querySelector( + `[data-l10n-id="translations-manage-delete-button"]` + ); + const spanishLabel = spanishRow.querySelector("label"); + const spanishDownload = spanishRow.querySelector( + `[data-l10n-id="translations-manage-download-button"]` + ); + const spanishDelete = spanishRow.querySelector( + `[data-l10n-id="translations-manage-delete-button"]` + ); + + return { + document, + downloadAllLabel, + downloadAll, + deleteAll, + frenchLabel, + frenchDownload, + frenchDelete, + spanishLabel, + spanishDownload, + spanishDelete, + }; +} + +function click(button, message) { + info(message); + if (button.hidden) { + throw new Error("The button was hidden when trying to click it."); + } + button.click(); +} + +/** + * @param {Object} options + * @param {string} options.message + * @param {Record<string, Element[]>} options.visible + * @param {Record<string, Element[]>} options.hidden + */ +async function assertVisibility({ message, visible, hidden }) { + info(message); + try { + // First wait for the condition to be met. + await TestUtils.waitForCondition(() => { + for (const element of Object.values(visible)) { + if (element.hidden) { + return false; + } + } + for (const element of Object.values(hidden)) { + if (!element.hidden) { + return false; + } + } + return true; + }); + } catch (error) { + // Ignore, this will get caught below. + } + // Now report the conditions. + for (const [name, element] of Object.entries(visible)) { + ok(!element.hidden, `${name} is visible.`); + } + for (const [name, element] of Object.entries(hidden)) { + ok(element.hidden, `${name} is hidden.`); + } +} + +async function setupAboutPreferences(languagePairs) { + await SpecialPowers.pushPrefEnv({ + set: [ + // Enabled by default. + ["browser.translations.enable", true], + ["browser.translations.logLevel", "All"], + ], + }); + const tab = await BrowserTestUtils.openNewForegroundTab( + gBrowser, + BLANK_PAGE, + true // waitForLoad + ); + + const { remoteClients, removeMocks } = await createAndMockRemoteSettings({ + languagePairs, + }); + + BrowserTestUtils.loadURIString(tab.linkedBrowser, "about:preferences"); + await BrowserTestUtils.browserLoaded(tab.linkedBrowser); + + const elements = await selectAboutPreferencesElements(); + + async function cleanup() { + gBrowser.removeCurrentTab(); + removeMocks(); + await SpecialPowers.popPrefEnv(); + } + + return { + cleanup, + remoteClients, + elements, + }; +} + +function waitForAppLocaleChanged() { + new Promise(resolve => { + function onChange() { + Services.obs.removeObserver(onChange, "intl:app-locales-changed"); + resolve(); + } + Services.obs.addObserver(onChange, "intl:app-locales-changed"); + }); +} + +async function mockLocales({ systemLocales, appLocales, webLanguages }) { + const appLocaleChanged1 = waitForAppLocaleChanged(); + + TranslationsParent.mockedSystemLocales = systemLocales; + const { availableLocales, requestedLocales } = Services.locale; + + info("Mocking locales, so expect potential .ftl resource errors."); + Services.locale.availableLocales = appLocales; + Services.locale.requestedLocales = appLocales; + + await appLocaleChanged1; + + await SpecialPowers.pushPrefEnv({ + set: [["intl.accept_languages", webLanguages.join(",")]], + }); + + return async () => { + const appLocaleChanged2 = waitForAppLocaleChanged(); + + // Reset back to the originals. + TranslationsParent.mockedSystemLocales = null; + Services.locale.availableLocales = availableLocales; + Services.locale.requestedLocales = requestedLocales; + + await appLocaleChanged2; + + await SpecialPowers.popPrefEnv(); + }; +} + +/** + * Helpful test functions for translations telemetry + */ +class TestTranslationsTelemetry { + /** + * Asserts qualities about a counter telemetry metric. + * + * @param {string} name - The name of the metric. + * @param {Object} counter - The Glean counter object. + * @param {Object} expectedCount - The expected value of the counter. + */ + static async assertCounter(name, counter, expectedCount) { + // Ensures that glean metrics are collected from all child processes + // so that calls to testGetValue() are up to date. + await Services.fog.testFlushAllChildren(); + const count = counter.testGetValue() ?? 0; + is( + count, + expectedCount, + `Telemetry counter ${name} should have expected count` + ); + } + + /** + * Asserts qualities about an event telemetry metric. + * + * @param {string} name - The name of the metric. + * @param {Object} event - The Glean event object. + * @param {Object} expectations - The test expectations. + * @param {number} expectations.expectedLength - The expected length of the event. + * @param {Array<function>} [expectations.allValuePredicates=[]] + * - An array of function predicates to assert for all event values. + * @param {Array<function>} [expectations.finalValuePredicates=[]] + * - An array of function predicates to assert for only the final event value. + */ + static async assertEvent( + name, + event, + { expectedLength, allValuePredicates = [], finalValuePredicates = [] } + ) { + // Ensures that glean metrics are collected from all child processes + // so that calls to testGetValue() are up to date. + await Services.fog.testFlushAllChildren(); + const values = event.testGetValue() ?? []; + const length = values.length; + + is( + length, + expectedLength, + `Telemetry event ${name} should have length ${expectedLength}` + ); + + if (allValuePredicates.length !== 0) { + is( + length > 0, + true, + `Telemetry event ${name} should contain values if allPredicates are specified` + ); + for (const value of values) { + for (const predicate of allValuePredicates) { + is( + predicate(value), + true, + `Telemetry event ${name} allPredicate { ${predicate.toString()} } should pass for each value` + ); + } + } + } + + if (finalValuePredicates.length !== 0) { + is( + length > 0, + true, + `Telemetry event ${name} should contain values if finalPredicates are specified` + ); + for (const predicate of finalValuePredicates) { + is( + predicate(values[length - 1]), + true, + `Telemetry event ${name} finalPredicate { ${predicate.toString()} } should pass for final value` + ); + } + } + } + + /** + * Asserts qualities about a rate telemetry metric. + * + * @param {string} name - The name of the metric. + * @param {Object} rate - The Glean rate object. + * @param {Object} expectations - The test expectations. + * @param {number} expectations.expectedNumerator - The expected value of the numerator. + * @param {number} expectations.expectedDenominator - The expected value of the denominator. + */ + static async assertRate( + name, + rate, + { expectedNumerator, expectedDenominator } + ) { + // Ensures that glean metrics are collected from all child processes + // so that calls to testGetValue() are up to date. + await Services.fog.testFlushAllChildren(); + const { numerator = 0, denominator = 0 } = rate.testGetValue() ?? {}; + is( + numerator, + expectedNumerator, + `Telemetry rate ${name} should have expected numerator` + ); + is( + denominator, + expectedDenominator, + `Telemetry rate ${name} should have expected denominator` + ); + } +} diff --git a/toolkit/components/translations/tests/browser/translations-test.mjs b/toolkit/components/translations/tests/browser/translations-test.mjs new file mode 100644 index 0000000000..d15a141837 --- /dev/null +++ b/toolkit/components/translations/tests/browser/translations-test.mjs @@ -0,0 +1,66 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +// eslint-disable-next-line no-unused-vars +let ok; +let is; +// eslint-disable-next-line no-unused-vars +let isnot; +let ContentTaskUtils; + +/** @type {{ document: Document, window: Window }} */ +let content; + +/** + * Inject the global variables from the test scope into the ES module scope. + */ +export function setup(config) { + // When a function is provided to `ContentTask.spawn`, that function is provided the + // Assert library through variable capture. In this case, this code is an ESM module, + // and does not have access to that scope. To work around this issue, pass any any + // relevant variables to that can be bound to the module scope. + // + // See: https://searchfox.org/mozilla-central/rev/cdddec7fd690700efa4d6b48532cf70155e0386b/testing/mochitest/BrowserTestUtils/content/content-task.js#78 + const { Assert } = config; + ok = Assert.ok.bind(Assert); + is = Assert.equal.bind(Assert); + isnot = Assert.notEqual.bind(Assert); + + ContentTaskUtils = config.ContentTaskUtils; + content = config.content; +} + +export function getSelectors() { + return { + getH1() { + return content.document.querySelector("h1"); + }, + getLastParagraph() { + return content.document.querySelector("p:last-child"); + }, + getHeader() { + return content.document.querySelector("header"); + }, + }; +} + +/** + * Asserts that a page was translated with a specific result. + * + * @param {string} message The assertion message. + * @param {Function} getNode A function to get the node. + * @param {string} translation The translated message. + */ +export async function assertTranslationResult(message, getNode, translation) { + try { + await ContentTaskUtils.waitForCondition( + () => translation === getNode()?.innerText, + `Waiting for: "${translation}"` + ); + } catch (error) { + // The result wasn't found, but the assertion below will report the error. + console.error(error); + } + + is(translation, getNode()?.innerText, message); +} diff --git a/toolkit/components/translations/tests/browser/translations-tester-en.html b/toolkit/components/translations/tests/browser/translations-tester-en.html new file mode 100644 index 0000000000..f32a7486ed --- /dev/null +++ b/toolkit/components/translations/tests/browser/translations-tester-en.html @@ -0,0 +1,34 @@ +<!DOCTYPE html> +<html lang="en"> +<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> + <!-- The following is an excerpt from The Wondeful Wizard of Oz, which is in the public domain --> + <h1>"The Wonderful Wizard of Oz" by L. Frank Baum</h1> + <p>The little girl, seeing she had lost one of her pretty shoes, grew angry, and said to the Witch, “Give me back my shoe!”</p> + <p>“I will not,” retorted the Witch, “for it is now my shoe, and not yours.”</p> + <p>“You are a wicked creature!” cried Dorothy. “You have no right to take my shoe from me.”</p> + <p>“I shall keep it, just the same,” said the Witch, laughing at her, “and someday I shall get the other one from you, too.”</p> + <p>This made Dorothy so very angry that she picked up the bucket of water that stood near and dashed it over the Witch, wetting her from head to foot.</p> + <p>Instantly the wicked woman gave a loud cry of fear, and then, as Dorothy looked at her in wonder, the Witch began to shrink and fall away.</p> + <p>“See what you have done!” she screamed. “In a minute I shall melt away.”</p> + <p>“I’m very sorry, indeed,” said Dorothy, who was truly frightened to see the Witch actually melting away like brown sugar before her very eyes.</p> + <p>“Didn’t you know water would be the end of me?” asked the Witch, in a wailing, despairing voice.</p> + <p>“Of course not,” answered Dorothy. “How should I?”</p> + </div> +</body> +</html> diff --git a/toolkit/components/translations/tests/browser/translations-tester-es-2.html b/toolkit/components/translations/tests/browser/translations-tester-es-2.html new file mode 100644 index 0000000000..abf2d42c62 --- /dev/null +++ b/toolkit/components/translations/tests/browser/translations-tester-es-2.html @@ -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> diff --git a/toolkit/components/translations/tests/browser/translations-tester-es.html b/toolkit/components/translations/tests/browser/translations-tester-es.html new file mode 100644 index 0000000000..abf2d42c62 --- /dev/null +++ b/toolkit/components/translations/tests/browser/translations-tester-es.html @@ -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> diff --git a/toolkit/components/translations/tests/browser/translations-tester-no-tag.html b/toolkit/components/translations/tests/browser/translations-tester-no-tag.html new file mode 100644 index 0000000000..7ef29cb70b --- /dev/null +++ b/toolkit/components/translations/tests/browser/translations-tester-no-tag.html @@ -0,0 +1,35 @@ +<!DOCTYPE html> +<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> |