diff options
Diffstat (limited to 'toolkit/components/translations/tests/browser')
32 files changed, 5685 insertions, 0 deletions
diff --git a/toolkit/components/translations/tests/browser/browser.toml b/toolkit/components/translations/tests/browser/browser.toml new file mode 100644 index 0000000000..50a1be7150 --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser.toml @@ -0,0 +1,53 @@ +[DEFAULT] +support-files = [ + "head.js", + "shared-head.js", + "translations-test.mjs", + "translations-tester-empty-pdf-file.pdf", + "translations-tester-en.html", + "translations-tester-es.html", + "translations-tester-es-2.html", + "translations-tester-fr.html", + "translations-tester-no-tag.html", + "translations-tester-shadow-dom-es.html", + "translations-tester-shadow-dom-mutation-es.html", + "translations-tester-shadow-dom-mutation-es-2.html", + "translations-tester-shadow-dom-slot-es.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_translations_actor.js"] + +["browser_translations_actor_detected_langs.js"] + +["browser_translations_actor_empty_langs.js"] + +["browser_translations_actor_preferred_language.js"] + +["browser_translations_actor_sync_models.js"] + +["browser_translations_actor_versioning.js"] + +["browser_translations_full_page.js"] + +["browser_translations_lang_tags.js"] + +["browser_translations_pdf_is_disabled.js"] + +["browser_translations_remote_settings.js"] + +["browser_translations_shadow_dom.js"] + +["browser_translations_shadow_dom_mutation.js"] + +["browser_translations_translation_document.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..c1a5a3ae2c --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_about_translations_debounce.js @@ -0,0 +1,91 @@ +/* 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" }, + { fromLang: "fr", toLang: "en" }, + ], + 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.", + 100, + 200 + ); + + /** @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}"`, + 100, + 200 + ); + } 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..1bd02256b1 --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_about_translations_directions.js @@ -0,0 +1,78 @@ +/* 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" }, + { fromLang: "ar", toLang: "en" }, + ], + 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.", + 100, + 200 + ); + + /** @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..6aed512952 --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_about_translations_dropdowns.js @@ -0,0 +1,112 @@ +/* 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" }, + { fromLang: "es", toLang: "en" }, + // This is not a bi-directional translation. + { fromLang: "is", toLang: "en" }, + ]; + 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.", + 100, + 200 + ); + + /** + * 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]; + 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..cde5fd03b4 --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_about_translations_enabling.js @@ -0,0 +1,137 @@ +/* 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."`, + 100, + 200 + ); + + 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."`, + 100, + 200 + ); + + 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.', + 100, + 200 + ); + + /** @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..033adf5ad8 --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_about_translations_translations.js @@ -0,0 +1,272 @@ +/* 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" }, + { fromLang: "fr", toLang: "en" }, + // This is not a bi-directional translation. + { fromLang: "is", toLang: "en" }, + ], + 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.", + 100, + 200 + ); + + /** @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}"`, + 100, + 200 + ); + } 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" }, + { fromLang: "fr", toLang: "en" }, + ], + 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.", + 100, + 200 + ); + + /** @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}"`, + 100, + 200 + ); + } 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({ + languagePairs: [ + { fromLang: "en", toLang: "fr" }, + { fromLang: "fr", toLang: "en" }, + ], + 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.", + 100, + 200 + ); + + /** @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}"`, + 100, + 200 + ); + } 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 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."`, + 100, + 200 + ); + + 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_translations_actor.js b/toolkit/components/translations/tests/browser/browser_translations_actor.js new file mode 100644 index 0000000000..457d032ea9 --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_translations_actor.js @@ -0,0 +1,234 @@ +/* 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 fromLanguagePairs = [ + { fromLang: "en", toLang: "es" }, + { fromLang: "es", toLang: "en" }, + { fromLang: "en", toLang: "yue" }, + { fromLang: "yue", toLang: "en" }, + // This is not a bi-directional translation. + { fromLang: "is", toLang: "en" }, + // These are non-pivot languages. + { fromLang: "zh", toLang: "ja" }, + { fromLang: "ja", toLang: "zh" }, + ]; + + // Sort the language pairs, as the order is not guaranteed. + function sort(list) { + return list.sort((a, b) => + `${a.fromLang}-${a.toLang}`.localeCompare(`${b.fromLang}-${b.toLang}`) + ); + } + + const { cleanup } = await setupActorTest({ + languagePairs: fromLanguagePairs, + }); + + const { languagePairs } = await TranslationsParent.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) + ) + ); + + if (SpecialPowers.isDebugBuild) { + Assert.deepEqual( + sort(languagePairs), + sort([ + { fromLang: "en", toLang: "es" }, + { fromLang: "en", toLang: "yue" }, + { fromLang: "es", toLang: "en" }, + { fromLang: "is", toLang: "en" }, + { fromLang: "yue", toLang: "en" }, + ]), + "Non-pivot languages were removed on debug builds." + ); + } else { + Assert.deepEqual( + sort(languagePairs), + sort(fromLanguagePairs), + "Non-pivot languages are retained on non-debug builds." + ); + } + + 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 { 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 TranslationsParent.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_detected_langs.js b/toolkit/components/translations/tests/browser/browser_translations_actor_detected_langs.js new file mode 100644 index 0000000000..65479a968e --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_translations_actor_detected_langs.js @@ -0,0 +1,86 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +add_task(async function test_detected_language() { + const { cleanup, tab } = await loadTestPage({ + // This page will get its language changed by the test. + page: ENGLISH_PAGE_URL, + autoDownloadFromRemoteSettings: true, + languagePairs: [ + // Spanish + { fromLang: "es", toLang: "en" }, + { fromLang: "en", toLang: "es" }, + + // Norwegian Bokmål + { fromLang: "nb", toLang: "en" }, + { fromLang: "en", toLang: "nb" }, + ], + }); + + async function getDetectedLanguagesFor(docLangTag) { + await ContentTask.spawn( + tab.linkedBrowser, + { docLangTag }, + function changeLanguage({ docLangTag }) { + content.document.body.parentNode.setAttribute("lang", docLangTag); + } + ); + // Clear out the cached values. + getTranslationsParent().languageState.detectedLanguages = null; + return getTranslationsParent().getDetectedLanguages(docLangTag); + } + + Assert.deepEqual( + await getDetectedLanguagesFor("es"), + { + docLangTag: "es", + userLangTag: "en", + isDocLangTagSupported: true, + }, + "Spanish is detected as a supported language." + ); + + Assert.deepEqual( + await getDetectedLanguagesFor("chr"), + { + docLangTag: "chr", + userLangTag: "en", + isDocLangTagSupported: false, + }, + "Cherokee is detected, but is not a supported language." + ); + + Assert.deepEqual( + await getDetectedLanguagesFor("no"), + { + docLangTag: "nb", + userLangTag: "en", + isDocLangTagSupported: true, + }, + "The Norwegian macro language is detected, but it defaults to Norwegian Bokmål." + ); + + Assert.deepEqual( + await getDetectedLanguagesFor("spa"), + { + docLangTag: "es", + userLangTag: "en", + isDocLangTagSupported: true, + }, + 'The three letter "spa" locale is canonicalized to "es".' + ); + + Assert.deepEqual( + await getDetectedLanguagesFor("gibberish"), + { + docLangTag: "en", + userLangTag: null, + isDocLangTagSupported: true, + }, + "A gibberish locale is discarded, and the language is detected." + ); + + return cleanup(); +}); diff --git a/toolkit/components/translations/tests/browser/browser_translations_actor_empty_langs.js b/toolkit/components/translations/tests/browser/browser_translations_actor_empty_langs.js new file mode 100644 index 0000000000..132bb9a1f2 --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_translations_actor_empty_langs.js @@ -0,0 +1,90 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Test some corner cases from Bug 1849815 where empty web languages were causing + * issues. + */ +add_task(async function test_detected_language() { + const { cleanup, tab } = await loadTestPage({ + // This page will get its language changed by the test. + page: ENGLISH_PAGE_URL, + autoDownloadFromRemoteSettings: true, + // Empty out the accept languages. + languagePairs: [ + // Spanish + { fromLang: "es", toLang: "en" }, + { fromLang: "en", toLang: "es" }, + // French + { fromLang: "fr", toLang: "en" }, + { fromLang: "en", toLang: "fr" }, + ], + }); + + async function getDetectedLanguagesFor(docLangTag) { + await ContentTask.spawn( + tab.linkedBrowser, + { docLangTag }, + function changeLanguage({ docLangTag }) { + content.document.body.parentNode.setAttribute("lang", docLangTag); + } + ); + // Clear out the cached values. + getTranslationsParent().languageState.detectedLanguages = null; + return getTranslationsParent().getDetectedLanguages(docLangTag); + } + + { + const cleanupLocales = await mockLocales({ + systemLocales: ["en"], + appLocales: ["en"], + webLanguages: [""], + }); + + Assert.deepEqual( + await getDetectedLanguagesFor("en"), + { + docLangTag: "en", + userLangTag: null, + isDocLangTagSupported: true, + }, + "If the web languages are empty, do not offer a language matching the app locale." + ); + + await cleanupLocales(); + } + + { + const cleanupLocales = await mockLocales({ + systemLocales: ["en", "es"], + appLocales: ["en"], + webLanguages: [""], + }); + + Assert.deepEqual( + await getDetectedLanguagesFor("en"), + { + docLangTag: "en", + userLangTag: null, + isDocLangTagSupported: true, + }, + "When there are multiple system locales, the app locale is used." + ); + + Assert.deepEqual( + await getDetectedLanguagesFor("es"), + { + docLangTag: "es", + userLangTag: "en", + isDocLangTagSupported: true, + }, + "When there are multiple system locales, the app locale is used." + ); + + await cleanupLocales(); + } + + 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..0960e05b6e --- /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 to the 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(), + [ + // webLanguages + "cs", + "hu", + // appLocales, notice that "en" is the last fallback. + "pt", + "pl", + "en", + // systemLocales + "zh", + "de", + ], + "Demonstrate an unrealistic but complicated locale situation." + ); + }, + }); +}); diff --git a/toolkit/components/translations/tests/browser/browser_translations_actor_sync_models.js b/toolkit/components/translations/tests/browser/browser_translations_actor_sync_models.js new file mode 100644 index 0000000000..1d1877fc38 --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_translations_actor_sync_models.js @@ -0,0 +1,235 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * An actor unit test for testing RemoteSettings update behavior. This uses the + * recommendations from: + * + * https://firefox-source-docs.mozilla.org/services/settings/index.html#unit-tests + */ +add_task(async function test_translations_actor_sync_update() { + const { remoteClients, cleanup } = await setupActorTest({ + autoDownloadFromRemoteSettings: true, + languagePairs: [ + { fromLang: "en", toLang: "es" }, + { fromLang: "es", toLang: "en" }, + ], + }); + + const decoder = new TextDecoder(); + const modelsPromise = TranslationsParent.getLanguageTranslationModelFiles( + "en", + "es" + ); + + await remoteClients.translationModels.resolvePendingDownloads( + FILES_PER_LANGUAGE_PAIR + ); + + const oldModels = await modelsPromise; + + is( + decoder.decode(oldModels.model.buffer), + "Mocked download: test-translation-models model.enes.intgemm.alphas.bin 1.0", + "The version 1.0 model is downloaded." + ); + + const newModelRecords = createRecordsForLanguagePair("en", "es"); + for (const newModelRecord of newModelRecords) { + newModelRecord.version = "1.1"; + } + + info('Emitting a remote client "sync" event with an updated record.'); + await remoteClients.translationModels.client.emit("sync", { + data: { + created: [], + updated: newModelRecords.map(newRecord => ({ + old: oldModels[newRecord.fileType].record, + new: newRecord, + })), + deleted: [], + }, + }); + + const updatedModelsPromise = + TranslationsParent.getLanguageTranslationModelFiles("en", "es"); + + await remoteClients.translationModels.resolvePendingDownloads( + FILES_PER_LANGUAGE_PAIR + ); + + const { model: updatedModel } = await updatedModelsPromise; + + is( + decoder.decode(updatedModel.buffer), + "Mocked download: test-translation-models model.enes.intgemm.alphas.bin 1.1", + "The version 1.1 model is downloaded." + ); + + return cleanup(); +}); + +/** + * An actor unit test for testing RemoteSettings delete behavior. + */ +add_task(async function test_translations_actor_sync_delete() { + const { remoteClients, cleanup } = await setupActorTest({ + autoDownloadFromRemoteSettings: true, + languagePairs: [ + { fromLang: "en", toLang: "es" }, + { fromLang: "es", toLang: "en" }, + ], + }); + + const decoder = new TextDecoder(); + const modelsPromise = TranslationsParent.getLanguageTranslationModelFiles( + "en", + "es" + ); + + await remoteClients.translationModels.resolvePendingDownloads( + FILES_PER_LANGUAGE_PAIR + ); + + const { model } = await modelsPromise; + + is( + decoder.decode(model.buffer), + "Mocked download: test-translation-models model.enes.intgemm.alphas.bin 1.0", + "The version 1.0 model is downloaded." + ); + + info('Emitting a remote client "sync" event with a deleted record.'); + await remoteClients.translationModels.client.emit("sync", { + data: { + created: [], + updated: [], + deleted: [model.record], + }, + }); + + let errorMessage; + await TranslationsParent.getLanguageTranslationModelFiles("en", "es").catch( + error => { + errorMessage = error?.message; + } + ); + + is( + errorMessage, + 'No model file was found for "en" to "es."', + "The model was successfully removed." + ); + + return cleanup(); +}); + +/** + * An actor unit test for testing RemoteSettings creation behavior. + */ +add_task(async function test_translations_actor_sync_create() { + const { remoteClients, cleanup } = await setupActorTest({ + autoDownloadFromRemoteSettings: true, + languagePairs: [ + { fromLang: "en", toLang: "es" }, + { fromLang: "es", toLang: "en" }, + ], + }); + + const decoder = new TextDecoder(); + const modelsPromise = TranslationsParent.getLanguageTranslationModelFiles( + "en", + "es" + ); + + await remoteClients.translationModels.resolvePendingDownloads( + FILES_PER_LANGUAGE_PAIR + ); + + is( + decoder.decode((await modelsPromise).model.buffer), + "Mocked download: test-translation-models model.enes.intgemm.alphas.bin 1.0", + "The version 1.0 model is downloaded." + ); + + info('Emitting a remote client "sync" event with new records.'); + await remoteClients.translationModels.client.emit("sync", { + data: { + created: createRecordsForLanguagePair("en", "fr"), + updated: [], + deleted: [], + }, + }); + + const updatedModelsPromise = + TranslationsParent.getLanguageTranslationModelFiles("en", "fr"); + + await remoteClients.translationModels.resolvePendingDownloads( + FILES_PER_LANGUAGE_PAIR + ); + + const { vocab, lex, model } = await updatedModelsPromise; + + is( + decoder.decode(vocab.buffer), + "Mocked download: test-translation-models vocab.enfr.spm 1.0", + "The en to fr vocab is downloaded." + ); + is( + decoder.decode(lex.buffer), + "Mocked download: test-translation-models lex.50.50.enfr.s2t.bin 1.0", + "The en to fr lex is downloaded." + ); + is( + decoder.decode(model.buffer), + "Mocked download: test-translation-models model.enfr.intgemm.alphas.bin 1.0", + "The en to fr model is downloaded." + ); + + return cleanup(); +}); + +add_task(async function test_translations_parent_download_size() { + const { cleanup } = await setupActorTest({ + languagePairs: [ + { fromLang: "en", toLang: "es" }, + { fromLang: "es", toLang: "en" }, + { fromLang: "en", toLang: "de" }, + { fromLang: "de", toLang: "en" }, + ], + }); + + const directSize = + await TranslationsParent.getExpectedTranslationDownloadSize("en", "es"); + // Includes model, lex, and vocab files (x3), each mocked at 123 bytes. + is( + directSize, + 3 * 123, + "Returned the expected download size for a direct translation." + ); + + const pivotSize = await TranslationsParent.getExpectedTranslationDownloadSize( + "es", + "de" + ); + // Includes a pivot (x2), model, lex, and vocab files (x3), each mocked at 123 bytes. + is( + pivotSize, + 2 * 3 * 123, + "Returned the expected download size for a pivot." + ); + + const notApplicableSize = + await TranslationsParent.getExpectedTranslationDownloadSize( + "unknown", + "unknown" + ); + is( + notApplicableSize, + 0, + "Returned the expected download size for an unknown or not applicable model." + ); + return cleanup(); +}); diff --git a/toolkit/components/translations/tests/browser/browser_translations_actor_versioning.js b/toolkit/components/translations/tests/browser/browser_translations_actor_versioning.js new file mode 100644 index 0000000000..7e96f62fbf --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_translations_actor_versioning.js @@ -0,0 +1,81 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +add_task(async function test_remote_settings_versioning() { + const tests = [ + { + majorVersion: 1, + existingVersion: "1.0", + nextVersion: "1.1", + expectation: true, + }, + { + majorVersion: 1, + existingVersion: null, + nextVersion: "1.1", + expectation: true, + }, + { + majorVersion: 1, + existingVersion: null, + nextVersion: "1.0beta", + expectation: true, + }, + { + majorVersion: 1, + existingVersion: null, + nextVersion: "1.0a", + expectation: true, + }, + { + majorVersion: 2, + existingVersion: null, + nextVersion: "1.0a", + expectation: false, + }, + { + majorVersion: 2, + existingVersion: "2.0", + nextVersion: "1.0a", + expectation: false, + }, + { + majorVersion: 2, + existingVersion: "2.1", + nextVersion: "3.2", + expectation: false, + }, + { + majorVersion: 2, + existingVersion: null, + nextVersion: "3.2", + expectation: false, + }, + { + majorVersion: 1, + nextVersion: "1.0", + existingVersion: undefined, + expectation: true, + }, + ]; + for (const { + majorVersion, + existingVersion, + nextVersion, + expectation, + } of tests) { + is( + TranslationsParent.isBetterRecordVersion( + majorVersion, + nextVersion, + existingVersion + ), + expectation, + `Given a major version of ${majorVersion}, an existing version ${existingVersion} ` + + `and a next version of ${nextVersion}, is the next version is ` + + `${expectation ? "" : "not "}best.` + ); + } +}); diff --git a/toolkit/components/translations/tests/browser/browser_translations_full_page.js b/toolkit/components/translations/tests/browser/browser_translations_full_page.js new file mode 100644 index 0000000000..7ee15902f8 --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_translations_full_page.js @@ -0,0 +1,135 @@ +/* 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: SPANISH_PAGE_URL, + languagePairs: [ + { fromLang: "es", toLang: "en" }, + { fromLang: "en", toLang: "es" }, + ], + 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: ENGLISH_PAGE_URL, + languagePairs: [ + { fromLang: "es", toLang: "en" }, + { fromLang: "en", toLang: "es" }, + ], + 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: NO_LANGUAGE_URL, + languagePairs: [ + { fromLang: "es", toLang: "en" }, + { fromLang: "en", toLang: "es" }, + ], + 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_translations_lang_tags.js b/toolkit/components/translations/tests/browser/browser_translations_lang_tags.js new file mode 100644 index 0000000000..3540df0beb --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_translations_lang_tags.js @@ -0,0 +1,114 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +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 waitForCondition( + async () => actor.languageState.detectedLanguages?.docLangTag, + "Waiting for a document language tag to be found." + ); + + Assert.deepEqual(actor.languageState.detectedLanguages, 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: SPANISH_PAGE_URL, + }, + { + 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: SPANISH_PAGE_URL, + }, + { + 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: SPANISH_PAGE_URL, + }, + { + 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: SPANISH_PAGE_URL, + 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/browser_translations_pdf_is_disabled.js b/toolkit/components/translations/tests/browser/browser_translations_pdf_is_disabled.js new file mode 100644 index 0000000000..b3ac09169f --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_translations_pdf_is_disabled.js @@ -0,0 +1,31 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests that the translations button becomes disabled when entering pdf. + */ +add_task(async function test_translations_button_disabled_in_pdf() { + const { cleanup } = await loadTestPage({ + page: EMPTY_PDF_URL, + }); + + const appMenuButton = document.getElementById("PanelUI-menu-button"); + + click(appMenuButton, "Opening the app menu"); + await BrowserTestUtils.waitForEvent(window.PanelUI.mainView, "ViewShown"); + + const translateSiteButton = document.getElementById( + "appMenu-translate-button" + ); + is( + translateSiteButton.disabled, + true, + "The app-menu translate button should be disabled because PDFs are restricted" + ); + + click(appMenuButton, "Closing the app menu"); + + await cleanup(); +}); diff --git a/toolkit/components/translations/tests/browser/browser_translations_remote_settings.js b/toolkit/components/translations/tests/browser/browser_translations_remote_settings.js new file mode 100644 index 0000000000..50987babdf --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_translations_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, majorVersion: 1 } + ); + + 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_translations_shadow_dom.js b/toolkit/components/translations/tests/browser/browser_translations_shadow_dom.js new file mode 100644 index 0000000000..86c8d8b33f --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_translations_shadow_dom.js @@ -0,0 +1,82 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const URL = + "https://example.com/browser/toolkit/components/translations/tests/browser/translations-tester-shadow-dom-es.html"; + +const URL_SLOT = + "https://example.com/browser/toolkit/components/translations/tests/browser/translations-tester-shadow-dom-slot-es.html"; + +/** + * Check that the translation feature works with ShadowDOM. + */ +add_task(async function test_shadow_dom_translation() { + await autoTranslatePage({ + page: URL, + languagePairs: [ + { fromLang: "es", toLang: "en" }, + { fromLang: "en", toLang: "es" }, + ], + runInPage: async TranslationsTest => { + await TranslationsTest.assertTranslationResult( + "Text outside of the Shadow DOM is translated", + function () { + return content.document.querySelector("h1"); + }, + "ESTO SE CONTENTA EN LUZ DOM [es to en, html]" + ); + + await TranslationsTest.assertTranslationResult( + "The content in the Shadow DOM is translated.", + function () { + const root = content.document.getElementById("host").shadowRoot; + return root.querySelector("p"); + }, + "ESTO SE CONTENTO EN SHADOW DOM [es to en, html]" + ); + + await TranslationsTest.assertTranslationResult( + "Content in the interior root of a Shadow DOM is translated.", + function () { + const outerRoot = content.document.getElementById("host").shadowRoot; + const innerRoot = outerRoot.querySelector("div").shadowRoot; + return innerRoot.querySelector("p"); + }, + "ESTO SE CONTENTA EN RAÍZ INTERIOR [es to en, html]" + ); + + await TranslationsTest.assertTranslationResult( + "Content in the Shaodw DOM where the host element is inside an empty textContent element is translated.", + function () { + const root = content.document.getElementById("host2").shadowRoot; + return root.querySelector("p"); + }, + "ESTO SE CONTENTO EN SHADOW DOM 2 [es to en, html]" + ); + }, + }); +}); + +/** + * Check that the translation feature works with ShadowDOM with slotted text node. + */ +add_task(async function test_shadow_dom_translation_slotted() { + await autoTranslatePage({ + page: URL_SLOT, + languagePairs: [ + { fromLang: "es", toLang: "en" }, + { fromLang: "en", toLang: "es" }, + ], + runInPage: async TranslationsTest => { + await TranslationsTest.assertTranslationResult( + "Slotted text node is translated", + function () { + return content.document.getElementById("host"); + }, + "ESTO SE CONTENTA EN LUZ DOM [es to en]" + ); + }, + }); +}); diff --git a/toolkit/components/translations/tests/browser/browser_translations_shadow_dom_mutation.js b/toolkit/components/translations/tests/browser/browser_translations_shadow_dom_mutation.js new file mode 100644 index 0000000000..c5a7891ee2 --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_translations_shadow_dom_mutation.js @@ -0,0 +1,164 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const URL = + "https://example.com/browser/toolkit/components/translations/tests/browser/translations-tester-shadow-dom-mutation-es.html"; + +const URL_2 = + "https://example.com/browser/toolkit/components/translations/tests/browser/translations-tester-shadow-dom-mutation-es-2.html"; + +/** + * Check that the translation feature works with mutations around ShadowDOM + */ +add_task(async function test_shadow_dom_mutation() { + await autoTranslatePage({ + page: URL, + languagePairs: [ + { fromLang: "es", toLang: "en" }, + { fromLang: "en", toLang: "es" }, + ], + runInPage: async TranslationsTest => { + await TranslationsTest.assertTranslationResult( + "Basic translation works", + function () { + return content.document.querySelector("h1"); + }, + "THIS IS CONTENT IN LIGHT DOM [es to en, html]" + ); + + info("Test 1: Mutation on existing shadow tree"); + const root1 = content.document.getElementById("host1").shadowRoot; + root1.innerHTML = "<p>This is mutated content in the Shadow DOM</p>"; + await TranslationsTest.assertTranslationResult( + "The content is translated when shadow tree is modified", + function () { + const root1 = content.document.getElementById("host1").shadowRoot; + return root1.querySelector("p"); + }, + "THIS IS MUTATED CONTENT IN THE SHADOW DOM [es to en, html]" + ); + + info("Test 2: Shadow host added later"); + const host2 = content.document.createElement("div"); + host2.id = "host2"; + const root2 = host2.attachShadow({ mode: "open" }); + root2.innerHTML = "<p>This is content in a shadow DOM</p>"; + content.document.body.appendChild(host2); + await TranslationsTest.assertTranslationResult( + "The content is translated when the host element is added later", + function () { + const root2 = content.document.getElementById("host2").shadowRoot; + return root2.querySelector("p"); + }, + "THIS IS CONTENT IN A SHADOW DOM [es to en, html]" + ); + + info("Test 3: Mutation Works on newly added shadow tree"); + const newNode = content.document.createElement("p"); + newNode.innerHTML = + "<p>This is mutated content in newly added shadow DOM</p>"; + newNode.id = "newNode"; + root2.appendChild(newNode); + await TranslationsTest.assertTranslationResult( + "The content is translated when a new node is added to the newly added shadow tree", + function () { + const root2 = content.document.getElementById("host2").shadowRoot; + return root2.getElementById("newNode"); + }, + "THIS IS MUTATED CONTENT IN NEWLY ADDED SHADOW DOM [es to en, html]" + ); + }, + }); +}); + +add_task(async function test_shadow_dom_mutation_nested_1() { + await autoTranslatePage({ + page: URL, + languagePairs: [ + { fromLang: "es", toLang: "en" }, + { fromLang: "en", toLang: "es" }, + ], + runInPage: async TranslationsTest => { + await TranslationsTest.assertTranslationResult( + "Basic translation works", + function () { + return content.document.querySelector("h1"); + }, + "THIS IS CONTENT IN LIGHT DOM [es to en, html]" + ); + + info("Test 1: Nested shadow host added later"); + const root1 = content.document.getElementById("host1").shadowRoot; + root1.innerHTML = "<div id='innerHost'></div>"; + + const innerHost = root1.getElementById("innerHost"); + innerHost.id = "innerHost"; + const innerRoot = innerHost.attachShadow({ mode: "open" }); + innerRoot.innerHTML = "<p>This is content in nested shadow DOM</p>"; + + await TranslationsTest.assertTranslationResult( + "The content is translated when a inner host element is added later", + function () { + const root1 = content.document.getElementById("host1").shadowRoot; + const innerRoot = root1.getElementById("innerHost").shadowRoot; + return innerRoot.querySelector("p"); + }, + "THIS IS CONTENT IN NESTED SHADOW DOM [es to en, html]" + ); + + info("Test 2: Mutation on inner shadow tree works"); + const newInnerNode = content.document.createElement("p"); + newInnerNode.innerHTML = + "<p>This is mutated content in nested shadow DOM</p>"; + newInnerNode.id = "newInnerNode"; + innerRoot.appendChild(newInnerNode); + await TranslationsTest.assertTranslationResult( + "The content is translated when inner shadow tree is mutated", + function () { + const root = content.document.getElementById("host1").shadowRoot; + const innerRoot = root.getElementById("innerHost").shadowRoot; + return innerRoot.getElementById("newInnerNode"); + }, + "THIS IS MUTATED CONTENT IN NESTED SHADOW DOM [es to en, html]" + ); + }, + }); +}); + +// Test to ensure mutations on a nested shadow tree that is +// added before pageload works. +add_task(async function test_shadow_dom_mutation_nested_2() { + await autoTranslatePage({ + page: URL_2, + languagePairs: [ + { fromLang: "es", toLang: "en" }, + { fromLang: "en", toLang: "es" }, + ], + runInPage: async TranslationsTest => { + await TranslationsTest.assertTranslationResult( + "Basic translation works", + function () { + return content.document.querySelector("h1"); + }, + "THIS IS CONTENT IN LIGHT DOM [es to en, html]" + ); + + const root1 = content.document.getElementById("host1").shadowRoot; + const innerRoot = root1.getElementById("innerHost").shadowRoot; + innerRoot.innerHTML = + "<p>This is mutated content in inner shadow DOM</p>"; + + await TranslationsTest.assertTranslationResult( + "The content is translated when the inner shadow tree is modified", + function () { + const root1 = content.document.getElementById("host1").shadowRoot; + const innerRoot = root1.getElementById("innerHost").shadowRoot; + return innerRoot.querySelector("p"); + }, + "THIS IS MUTATED CONTENT IN INNER SHADOW DOM [es to en, html]" + ); + }, + }); +}); diff --git a/toolkit/components/translations/tests/browser/browser_translations_translation_document.js b/toolkit/components/translations/tests/browser/browser_translations_translation_document.js new file mode 100644 index 0000000000..9a00da9ccf --- /dev/null +++ b/toolkit/components/translations/tests/browser/browser_translations_translation_document.js @@ -0,0 +1,1446 @@ +/* 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, LRUCache } = ChromeUtils.importESModule( + "chrome://global/content/translations/translations-document.sys.mjs" +); +/** + * @param {string} html + * @param {{ + * mockedTranslatorPort?: (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"); + + // For some reason, the document <body> here from the DOMParser is "display: flex" by + // default. Ensure that it is "display: block" instead, otherwise the children of the + // <body> will not be "display: inline". + document.body.style.display = "block"; + + const translate = () => { + info("Creating the TranslationsDocument."); + return new TranslationsDocument( + document, + "en", + "EN", + 0, // This is a fake innerWindowID + options?.mockedTranslatorPort ?? createMockedTranslatorPort(), + () => { + throw new Error("Cannot request a new port"); + }, + performance.now(), + () => performance.now(), + new LRUCache() + ); + }; + + /** + * 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 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 cleanup() { + SpecialPowers.popPrefEnv(); + } + + return { htmlMatches, cleanup, translate, document }; +} + +add_task(async function test_translated_div_element() { + const { translate, htmlMatches, cleanup } = await createDoc(/* 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, translate } = await createDoc(/* html */ ` + <!DOCTYPE html> + <html> + <head> + <meta charset="utf-8" /> + <title>This is an actual full page.</title> + </head> + <body> + + </body> + </html> + `); + + translate(); + + const translatedTitle = "THIS IS AN ACTUAL FULL PAGE."; + try { + await 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"> + <a href="/" data-moz-translations-id="0"> + LATEST WORK + </a> + </li> + <li class="menu-item menu-item-top-level"> + <a href="/category/interactive/" data-moz-translations-id="0"> + CREATIVE CODING + </a> + </li> + <li id="menu-id-categories" class="menu-item menu-item-top-level"> + <a href="#" data-moz-translations-id="0"> + <span class="category-arrow" data-moz-translations-id="1"> + 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> + <!-- These will be ignored in the translation. --> + 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> + `, + { mockedTranslatorPort: createBatchedMockedTranslatorPort() } + ); + + 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 the inline/block behavior on what is sent in for a translation. + */ +add_task(async function test_translation_inline_styling() { + const { document, translate, htmlMatches, cleanup } = await createDoc( + /* html */ ` + Bare text is sent in a batch. + <span> + Inline text is sent in a <b>batch</b>. + </span> + <span id="spanAsBlock"> + Display "block" overrides the inline designation. + </span> + `, + { mockedTranslatorPort: createBatchedMockedTranslatorPort() } + ); + + info("Setting a span as display: block."); + const span = document.getElementById("spanAsBlock"); + span.style.display = "block"; + is(span.ownerGlobal.getComputedStyle(span).display, "block"); + + translate(); + + await htmlMatches( + "Span as a display: block", + /* html */ ` + aaaa aaaa aa aaaa aa a aaaaa. + <span> + bbbbbb bbbb bb bbbb bb b + <b data-moz-translations-id="0"> + bbbbb + </b> + . + </span> + <span id="spanAsBlock" style="display: block;"> + ccccccc "ccccc" ccccccccc ccc cccccc ccccccccccc. + </span> + ` + ); + + 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> + `, + { mockedTranslatorPort: createBatchedMockedTranslatorPort() } + ); + + 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> + `, + { mockedTranslatorPort: createBatchedMockedTranslatorPort() } + ); + + 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 a mix of inline text and block elements. + */ +add_task(async function test_presumed_inlines1() { + const { translate, htmlMatches, cleanup } = await createDoc( + /* html */ ` + <div> + Text node + <div>Block element</div> + </div> + `, + { mockedTranslatorPort: createBatchedMockedTranslatorPort() } + ); + + translate(); + + await htmlMatches( + "Mixing a text node with block elements will send in two batches.", + /* html */ ` + <div> + aaaa aaaa + <div> + bbbbb bbbbbbb + </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> + `, + { mockedTranslatorPort: createBatchedMockedTranslatorPort() } + ); + + translate(); + + await htmlMatches( + "A mix of inline and blocks will be sent in separately.", + /* html */ ` + <div> + aaaa aaaa + <span> + bbbbbb + </span> + <div> + ccccc ccccccc + </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> + `, + { mockedTranslatorPort: createBatchedMockedTranslatorPort() } + ); + + translate(); + + await htmlMatches( + "Conflicting inlines will be sent in as separate blocks if there are more block elements", + /* html */ ` + <div> + aaaa aaaa + <span> + bbbbbb + </span> + <div> + ccccc ccccccc + </div> + <div> + ddddd ddddddd + </div> + <div> + eeeee eeeeeee + </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> + `, + { mockedTranslatorPort: createBatchedMockedTranslatorPort() } + ); + + 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> + `, + { mockedTranslatorPort: createdReorderingMockedTranslatorPort() } + ); + + 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. + `, + { mockedTranslatorPort: createdReorderingMockedTranslatorPort() } + ); + + 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_svgs() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <div> + <div>Text before is translated</div> + <svg width="200" height="200" xmlns="http://www.w3.org/2000/svg"> + <style>.myText { font-family: sans-serif; }</style> + <rect x="10" y="10" width="80" height="60" class="myRect" /> + <circle cx="150" cy="50" r="30" class="myCircle" /> + <text x="50%" y="50%" text-anchor="middle" alignment-baseline="middle" class="myText"> + Text inside of the SVG is untranslated. + </text> + </svg> + <div>Text after is translated</div> + </div> + `); + + translate(); + + await htmlMatches( + "SVG text gets translated, and style elements are left alone.", + /* html */ ` + <div> + <div> + TEXT BEFORE IS TRANSLATED + </div> + <svg width="200" height="200" xmlns="http://www.w3.org/2000/svg"> + <style> + .myText { font-family: sans-serif; } + </style> + <rect x="10" y="10" width="80" height="60" class="myRect"> + </rect> + <circle cx="150" cy="50" r="30" class="myCircle"> + </circle> + <text x="50%" y="50%" text-anchor="middle" alignment-baseline="middle" class="myText"> + TEXT INSIDE OF THE SVG IS UNTRANSLATED. + </text> + </svg> + <div> + TEXT AFTER IS TRANSLATED + </div> + </div> + ` + ); + + await cleanup(); +}); + +add_task(async function test_svgs_more() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"> + <foreignObject x="20" y="20" width="160" height="160"> + <div xmlns="http://www.w3.org/1999/xhtml"> + This is a div inside of an SVG. + </div> + </foreignObject> + </svg> + `); + + translate(); + + await htmlMatches( + "Foreign objects get translated", + /* html */ ` + <svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"> + <foreignObject x="20" y="20" width="160" height="160"> + <div xmlns="http://www.w3.org/1999/xhtml"> + THIS IS A DIV INSIDE OF AN SVG. + </div> + </foreignObject> + </svg> + ` + ); + + await 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(); +}); + +// Attribute translation for title and placeholder +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 added", + /* html */ ` + <label title="TITLES ARE USER VISIBLE"> + ENTER INFORMATION: + </label> + <input type="text" placeholder="THIS IS A PLACEHOLDER"> + ` + ); + + cleanup(); +}); + +add_task(async function test_html_attributes() { + const { translate, document, cleanup } = await createDoc(/* html */ ` + <!DOCTYPE html> + <html lang="en" > + <head> + <meta charset="utf-8" /> + </head> + <body> + </body> + </html> + `); + + translate(); + + try { + await waitForCondition(() => document.documentElement.lang === "EN"); + } catch (error) {} + is(document.documentElement.lang, "EN", "The lang attribute was changed"); + + cleanup(); +}); + +// Attribute translation for title +add_task(async function test_attributes() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <div title="Titles are user visible"> + </div> + `); + + translate(); + + await htmlMatches( + "Attribute translation for title", + /* html */ ` + <div title="TITLES ARE USER VISIBLE"> + </div> + ` + ); + + cleanup(); +}); + +// Attribute translation for title with innerHTML +add_task(async function test_attributes() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <div title="Titles are user visible"> + Simple translation. + </div> + `); + + translate(); + + await htmlMatches( + "translation for title with innerHTML", + /* html */ ` + <div title="TITLES ARE USER VISIBLE"> + SIMPLE TRANSLATION. + </div> + ` + ); + + cleanup(); +}); + +// Attribute translation for title and placeholder in same element +add_task(async function test_attributes() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <input type="text" placeholder="This is a placeholder" title="Titles are user visible"> + `); + + translate(); + + await htmlMatches( + "title and placeholder together", + /* html */ ` + <input type="text" placeholder="THIS IS A PLACEHOLDER" title="TITLES ARE USER VISIBLE"> + ` + ); + cleanup(); +}); + +// Attribute translation for placeholder +add_task(async function test_attributes() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <input type="text" placeholder="This is a placeholder"> + `); + + translate(); + + await htmlMatches( + "Attribute translation for placeholder", + /* html */ ` + <input type="text" placeholder="THIS IS A PLACEHOLDER"> + ` + ); + cleanup(); +}); + +add_task(async function test_translated_title() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <div title="The title is translated" class="do-not-translate-this"> + Inner text is translated. + </div> + `); + + translate(); + + await htmlMatches( + "Language matching of elements behaves as expected.", + /* html */ ` + <div title="THE TITLE IS TRANSLATED" class="do-not-translate-this"> + INNER TEXT IS TRANSLATED. + </div> + ` + ); + + cleanup(); +}); + +add_task(async function test_title_attribute_subnodes() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <div> + <span>Span text 1</span> + <span>Span text 2</span> + <span>Span text 3</span> + <span>Span text 4</span> + <span>Span text 5</span> + This is text. + </div> + `); + + translate(); + + await htmlMatches( + "Titles are translated", + /* html */ ` + <div> + <span data-moz-translations-id="0">SPAN TEXT 1</span> + <span data-moz-translations-id="1">SPAN TEXT 2</span> + <span data-moz-translations-id="2">SPAN TEXT 3</span> + <span data-moz-translations-id="3">SPAN TEXT 4</span> + <span data-moz-translations-id="4">SPAN TEXT 5</span> + THIS IS TEXT. + </div> + ` + ); + + cleanup(); +}); + +add_task(async function test_title_attribute_subnodes() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <div title="Title in div"> + <span title="Title 1">Span text 1</span> + <span title="Title 2">Span text 2</span> + <span title="Title 3">Span text 3</span> + <span title="Title 4">Span text 4</span> + <span title="Title 5">Span text 5</span> + This is text. + </div> + `); + + translate(); + + await htmlMatches( + "Titles are translated", + /* html */ ` + <div title="TITLE IN DIV"> + <span title="TITLE 1" data-moz-translations-id="0">SPAN TEXT 1</span> + <span title="TITLE 2" data-moz-translations-id="1">SPAN TEXT 2</span> + <span title="TITLE 3" data-moz-translations-id="2">SPAN TEXT 3</span> + <span title="TITLE 4" data-moz-translations-id="3">SPAN TEXT 4</span> + <span title="TITLE 5" data-moz-translations-id="4">SPAN TEXT 5</span> + THIS IS TEXT. + </div> + ` + ); + + cleanup(); +}); + +// Attribute translation for nested text +add_task(async function test_attributes() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <div> + This is the outer div + <label> + Enter information: + <input type="text"> + </label> + </div> + `); + + translate(); + + await htmlMatches( + "translation for Nested with text", + /* html */ ` + <div> + THIS IS THE OUTER DIV + <label data-moz-translations-id="0"> + ENTER INFORMATION: + <input type="text" data-moz-translations-id="1"> + </label> + </div> + ` + ); + + cleanup(); +}); + +// Attribute translation Nested Attributes +add_task(async function test_attributes() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <div title="Titles are user visible"> + This is the outer div + <label> + Enter information: + <input type="text" placeholder="This is a placeholder"> + </label> + </div> + `); + + translate(); + + await htmlMatches( + "Translations: Nested Attributes", + /* html */ ` + <div title="TITLES ARE USER VISIBLE"> + THIS IS THE OUTER DIV + <label data-moz-translations-id="0"> + ENTER INFORMATION: + <input type="text" placeholder="THIS IS A PLACEHOLDER" data-moz-translations-id="1"> + </label> + </div> + ` + ); + + cleanup(); +}); + +add_task(async function test_attributes() { + const { translate, htmlMatches, cleanup } = await createDoc(/* html */ ` + <div> + This is the outer div + <label> + Enter information 1: + <label> + Enter information 2: + </label> + </label> + </div> + `); + + translate(); + + await htmlMatches( + "Translations: Nested elements", + /* html */ ` + <div> + THIS IS THE OUTER DIV + <label data-moz-translations-id="0"> + ENTER INFORMATION 1: + <label data-moz-translations-id="1"> + ENTER INFORMATION 2: + </label> + </label> + </div> + ` + ); + + cleanup(); +}); + +add_task(async function test_mutations_with_attributes() { + 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."; + div.setAttribute("title", "title is added"); + document.body.appendChild(div); + + await htmlMatches( + "The added node gets translated.", + /* html */ ` + <div> + THIS IS A SIMPLE TRANSLATION. + </div> + <div title="TITLE IS ADDED"> + 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 title="TITLE IS ADDED"> + THIS IS AN ADDED NODE. + </div> + ` + ); + + info('Trigger the "childList" mutation.'); + const inp = document.createElement("input"); + inp.setAttribute("placeholder", "input placeholder is added"); + document.body.appendChild(inp); + + await htmlMatches( + "The placeholder in input node gets translated.", + /* html */ ` + <div> + THIS IS A CHANGED NODE. + </div> + <div title="TITLE IS ADDED"> + THIS IS AN ADDED NODE. + </div> + <input placeholder="INPUT PLACEHOLDER IS ADDED"> + ` + ); + + info("Trigger attribute mutation."); + // adding attribute to first div + document.querySelector("div").setAttribute("title", "New attribute"); + document.querySelector("input").setAttribute("title", "New attribute input"); + + await htmlMatches( + "The new attribute gets translated.", + /* html */ ` + <div title="NEW ATTRIBUTE"> + THIS IS A CHANGED NODE. + </div> + <div title="TITLE IS ADDED"> + THIS IS AN ADDED NODE. + </div> + <input placeholder="INPUT PLACEHOLDER IS ADDED" title="NEW ATTRIBUTE INPUT"> + ` + ); + + cleanup(); +}); + +add_task(async function test_mutations_subtree_attributes() { + 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.innerHTML = /* html */ ` + <div title="This is an outer node"> + This is some inner text. + <input placeholder="This is a placeholder" /> + </div> + `; + document.body.appendChild(div.children[0]); + + await htmlMatches( + "The added node gets translated.", + /* html */ ` + <div> + THIS IS A SIMPLE TRANSLATION. + </div> + <div title="THIS IS AN OUTER NODE"> + THIS IS SOME INNER TEXT. + <input placeholder="THIS IS A PLACEHOLDER"> + </div> + ` + ); + + cleanup(); +}); 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..bad8e48a1b --- /dev/null +++ b/toolkit/components/translations/tests/browser/shared-head.js @@ -0,0 +1,1442 @@ +/* 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 ENGLISH_PAGE_URL = + URL_COM_PREFIX + DIR_PATH + "translations-tester-en.html"; +const SPANISH_PAGE_URL = + URL_COM_PREFIX + DIR_PATH + "translations-tester-es.html"; +const FRENCH_PAGE_URL = + URL_COM_PREFIX + DIR_PATH + "translations-tester-fr.html"; +const SPANISH_PAGE_URL_2 = + URL_COM_PREFIX + DIR_PATH + "translations-tester-es-2.html"; +const SPANISH_PAGE_URL_DOT_ORG = + URL_ORG_PREFIX + DIR_PATH + "translations-tester-es.html"; +const NO_LANGUAGE_URL = + URL_COM_PREFIX + DIR_PATH + "translations-tester-no-tag.html"; +const EMPTY_PDF_URL = + URL_COM_PREFIX + DIR_PATH + "translations-tester-empty-pdf-file.pdf"; + +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: "uk" }, + { fromLang: "uk", toLang: PIVOT_LANGUAGE }, +]; + +const TRANSLATIONS_PERMISSION = "translations"; +const ALWAYS_TRANSLATE_LANGS_PREF = + "browser.translations.alwaysTranslateLanguages"; +const NEVER_TRANSLATE_LANGS_PREF = + "browser.translations.neverTranslateLanguages"; + +/** + * 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 {Array<{ fromLang: string, toLang: string }>} 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, + languagePairs = 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, + }); + + // Now load the about:translations page, since the actor could be mocked. + BrowserTestUtils.startLoadingURIString( + tab.linkedBrowser, + "about:translations" + ); + await BrowserTestUtils.browserLoaded(tab.linkedBrowser); + + await remoteClients.translationsWasm.resolvePendingDownloads(1); + await remoteClients.translationModels.resolvePendingDownloads( + languagePairs.length * FILES_PER_LANGUAGE_PAIR + ); + + await ContentTask.spawn( + tab.linkedBrowser, + { dataForContent, selectors }, + runInPage + ); + + await loadBlankPage(); + BrowserTestUtils.removeTab(tab); + + await removeMocks(); + await TranslationsParent.destroyEngineProcess(); + + 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(); +} + +/** + * Recursively transforms all child nodes to have uppercased text. + * + * @param {Node} node + */ +function upperCaseNode(node) { + if (typeof node.nodeValue === "string") { + node.nodeValue = node.nodeValue.toUpperCase(); + } + for (const childNode of node.childNodes) { + upperCaseNode(childNode); + } +} + +/** + * Creates a mocked message port for translations. + * + * @returns {MessagePort} This is mocked + */ +function createMockedTranslatorPort(transformNode = upperCaseNode) { + const parser = new DOMParser(); + const mockedPort = { + async postMessage(message) { + // Make this response async. + await TestUtils.waitForTick(); + + switch (message.type) { + case "TranslationsPort:GetEngineStatusRequest": + mockedPort.onmessage({ + data: { + type: "TranslationsPort:GetEngineStatusResponse", + status: "ready", + }, + }); + break; + case "TranslationsPort:TranslationRequest": { + const { messageId, sourceText } = message; + + const translatedDoc = parser.parseFromString(sourceText, "text/html"); + transformNode(translatedDoc.body); + mockedPort.onmessage({ + data: { + type: "TranslationsPort:TranslationResponse", + targetText: translatedDoc.body.innerHTML, + messageId, + }, + }); + } + } + }, + }; + return mockedPort; +} + +/** + * This mocked 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. + * + * @returns {MessagePort} A mocked port. + */ +function createBatchedMockedTranslatorPort() { + let letter = "a"; + + /** + * @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); + } + } + + return createMockedTranslatorPort(node => { + transformNode(node); + letter = String.fromCodePoint(letter.codePointAt(0) + 1); + }); +} + +/** + * This mocked translator reorders Nodes to be in alphabetical order, and then + * uppercases the text. This allows for testing the reordering behavior of the + * translation engine. + * + * @returns {MessagePort} A mocked port. + */ +function createdReorderingMockedTranslatorPort() { + /** + * @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); + } + } + + return createMockedTranslatorPort(transformNode); +} + +/** + * @returns {import("../../actors/TranslationsParent.sys.mjs").TranslationsParent} + */ +function getTranslationsParent() { + return gBrowser.selectedBrowser.browsingContext.currentWindowGlobal.getActor( + "Translations" + ); +} + +/** + * Closes the context menu if it is open. + */ +function closeContextMenuIfOpen() { + return waitForCondition(async () => { + const contextMenu = document.getElementById("contentAreaContextMenu"); + if (!contextMenu) { + return true; + } + if (contextMenu.state === "closed") { + return true; + } + let popuphiddenPromise = BrowserTestUtils.waitForEvent( + contextMenu, + "popuphidden" + ); + PanelMultiView.hidePopup(contextMenu); + await popuphiddenPromise; + return false; + }); +} + +/** + * Closes the translations panel settings menu if it is open. + */ +function closeSettingsMenuIfOpen() { + return waitForCondition(async () => { + const settings = document.getElementById( + "translations-panel-settings-menupopup" + ); + if (!settings) { + return true; + } + if (settings.state === "closed") { + return true; + } + let popuphiddenPromise = BrowserTestUtils.waitForEvent( + settings, + "popuphidden" + ); + PanelMultiView.hidePopup(settings); + await popuphiddenPromise; + return false; + }); +} + +/** + * Closes the translations panel if it is open. + */ +async function closeTranslationsPanelIfOpen() { + await closeSettingsMenuIfOpen(); + return waitForCondition(async () => { + const panel = document.getElementById("translations-panel"); + if (!panel) { + return true; + } + if (panel.state === "closed") { + return true; + } + let popuphiddenPromise = BrowserTestUtils.waitForEvent( + panel, + "popuphidden" + ); + PanelMultiView.hidePopup(panel); + await popuphiddenPromise; + return false; + }); +} + +/** + * This is for tests that don't need a browser page to run. + */ +async function setupActorTest({ + languagePairs, + prefs, + autoDownloadFromRemoteSettings = false, +}) { + await SpecialPowers.pushPrefEnv({ + set: [ + // Enabled by default. + ["browser.translations.enable", true], + ["browser.translations.logLevel", "All"], + ...(prefs ?? []), + ], + }); + + const { remoteClients, removeMocks } = await createAndMockRemoteSettings({ + languagePairs, + autoDownloadFromRemoteSettings, + }); + + // 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, + ENGLISH_PAGE_URL, + true // waitForLoad + ); + + const actor = getTranslationsParent(); + return { + actor, + remoteClients, + async cleanup() { + await loadBlankPage(); + await TranslationsParent.destroyEngineProcess(); + await closeTranslationsPanelIfOpen(); + await closeContextMenuIfOpen(); + BrowserTestUtils.removeTab(tab); + await removeMocks(); + TestTranslationsTelemetry.reset(); + return SpecialPowers.popPrefEnv(); + }, + }; +} + +async function createAndMockRemoteSettings({ + languagePairs = LANGUAGE_PAIRS, + autoDownloadFromRemoteSettings = false, +}) { + const remoteClients = { + translationModels: await createTranslationModelsRemoteClient( + autoDownloadFromRemoteSettings, + languagePairs + ), + translationsWasm: await createTranslationsWasmRemoteClient( + autoDownloadFromRemoteSettings + ), + }; + + // The TranslationsParent will pull the language pair values from the JSON dump + // of Remote Settings. Clear these before mocking the translations engine. + TranslationsParent.clearCache(); + + TranslationsParent.mockTranslationsEngine( + remoteClients.translationModels.client, + remoteClients.translationsWasm.client + ); + + return { + async removeMocks() { + await remoteClients.translationModels.client.attachments.deleteAll(); + await remoteClients.translationModels.client.db.clear(); + await remoteClients.translationsWasm.client.db.clear(); + + TranslationsParent.unmockTranslationsEngine(); + TranslationsParent.clearCache(); + }, + remoteClients, + }; +} + +async function loadTestPage({ + languagePairs, + autoDownloadFromRemoteSettings = false, + page, + prefs, + autoOffer, + permissionsUrls, +}) { + info(`Loading test page starting at url: ${page}`); + // Ensure no engine is being carried over from a previous test. + await TranslationsParent.destroyEngineProcess(); + Services.fog.testResetFOG(); + await SpecialPowers.pushPrefEnv({ + set: [ + // Enabled by default. + ["browser.translations.enable", true], + ["browser.translations.logLevel", "All"], + ["browser.translations.panelShown", true], + ["browser.translations.automaticallyPopup", true], + ["browser.translations.alwaysTranslateLanguages", ""], + ["browser.translations.neverTranslateLanguages", ""], + ...(prefs ?? []), + ], + }); + await SpecialPowers.pushPermissions( + [ + ENGLISH_PAGE_URL, + FRENCH_PAGE_URL, + NO_LANGUAGE_URL, + SPANISH_PAGE_URL, + SPANISH_PAGE_URL_2, + SPANISH_PAGE_URL_DOT_ORG, + ...(permissionsUrls || []), + ].map(url => ({ + type: TRANSLATIONS_PERMISSION, + allow: true, + context: url, + })) + ); + + if (autoOffer) { + TranslationsParent.testAutomaticPopup = true; + } + + // Start the tab at a blank page. + const tab = await BrowserTestUtils.openNewForegroundTab( + gBrowser, + BLANK_PAGE, + true // waitForLoad + ); + + const { remoteClients, removeMocks } = await createAndMockRemoteSettings({ + languagePairs, + autoDownloadFromRemoteSettings, + }); + + BrowserTestUtils.startLoadingURIString(tab.linkedBrowser, page); + await BrowserTestUtils.browserLoaded(tab.linkedBrowser); + + if (autoOffer && TranslationsParent.shouldAlwaysOfferTranslations()) { + info("Waiting for the popup to be automatically shown."); + await waitForCondition(() => { + const panel = document.getElementById("translations-panel"); + return panel && panel.state === "open"; + }); + } + + 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 + ); + }, + + /** + * @returns {Promise<void>} + */ + async cleanup() { + await loadBlankPage(); + await TranslationsParent.destroyEngineProcess(); + await closeTranslationsPanelIfOpen(); + await closeContextMenuIfOpen(); + await removeMocks(); + Services.fog.testResetFOG(); + TranslationsParent.testAutomaticPopup = false; + TranslationsParent.resetHostsOffered(); + BrowserTestUtils.removeTab(tab); + TestTranslationsTelemetry.reset(); + 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, data = {}) { + // 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}); + + const data = ${JSON.stringify(data)}; + + return (${callback.toString()})(TranslationsTest, data); + `); + + 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 { prefs, languagePairs, ...otherOptions } = options; + const fromLangs = languagePairs.map(language => language.fromLang).join(","); + const { cleanup, runInPage } = await loadTestPage({ + autoDownloadFromRemoteSettings: true, + prefs: [ + ["browser.translations.alwaysTranslateLanguages", fromLangs], + ...(prefs ?? []), + ], + ...otherOptions, + }); + await runInPage(options.runInPage); + await cleanup(); +} + +/** + * @param {RemoteSettingsClient} client + * @param {string} mockedCollectionName - The name of the mocked collection without + * the incrementing "id" part. This is provided so that attachments can be asserted + * as being of a certain version. + * @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, + mockedCollectionName, + autoDownloadFromRemoteSettings +) { + const pendingDownloads = []; + client.attachments.download = record => + new Promise((resolve, reject) => { + console.log("Download requested:", client.collectionName, record.name); + if (autoDownloadFromRemoteSettings) { + const encoder = new TextEncoder(); + const { buffer } = encoder.encode( + `Mocked download: ${mockedCollectionName} ${record.name} ${record.version}` + ); + + resolve({ buffer }); + } 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; + +function createRecordsForLanguagePair(fromLang, toLang) { + const records = []; + 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` }, + ]; + + const attachment = { + hash: `${crypto.randomUUID()}`, + size: `123`, + filename: `model.${lang}.intgemm.alphas.bin`, + location: `main-workspace/translations-models/${crypto.randomUUID()}.bin`, + mimetype: "application/octet-stream", + }; + + 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: TranslationsParent.LANGUAGE_MODEL_MAJOR_VERSION + ".0", + last_modified: Date.now(), + schema: Date.now(), + attachment, + }); + } + return records; +} + +/** + * Increments each time a remote settings client is created to ensure a unique client + * name for each test run. + */ +let _remoteSettingsMockId = 0; + +/** + * 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 } of langPairs) { + records.push(...createRecordsForLanguagePair(fromLang, toLang)); + } + + const { RemoteSettings } = ChromeUtils.importESModule( + "resource://services-settings/remote-settings.sys.mjs" + ); + const mockedCollectionName = "test-translation-models"; + const client = RemoteSettings( + `${mockedCollectionName}-${_remoteSettingsMockId++}` + ); + const metadata = {}; + await client.db.clear(); + await client.db.importChanges(metadata, Date.now(), records); + + return createAttachmentMock( + client, + mockedCollectionName, + autoDownloadFromRemoteSettings + ); +} + +/** + * Creates a local RemoteSettingsClient for use within tests. + * + * @param {boolean} autoDownloadFromRemoteSettings + * @returns {RemoteSettingsClient} + */ +async function createTranslationsWasmRemoteClient( + autoDownloadFromRemoteSettings +) { + const records = ["bergamot-translator"].map(name => ({ + id: crypto.randomUUID(), + name, + version: TranslationsParent.BERGAMOT_MAJOR_VERSION + ".0", + last_modified: Date.now(), + schema: Date.now(), + })); + + const { RemoteSettings } = ChromeUtils.importESModule( + "resource://services-settings/remote-settings.sys.mjs" + ); + const mockedCollectionName = "test-translation-wasm"; + const client = RemoteSettings( + `${mockedCollectionName}-${_remoteSettingsMockId++}` + ); + const metadata = {}; + await client.db.clear(); + await client.db.importChanges(metadata, Date.now(), records); + + return createAttachmentMock( + client, + mockedCollectionName, + autoDownloadFromRemoteSettings + ); +} + +async function selectAboutPreferencesElements() { + const document = gBrowser.selectedBrowser.contentDocument; + + const settingsButton = document.getElementById( + "translations-manage-settings-button" + ); + + const rows = await waitForCondition(() => { + const elements = document.querySelectorAll(".translations-manage-language"); + if (elements.length !== 4) { + return false; + } + return elements; + }, "Waiting for manage language rows."); + + const [downloadAllRow, frenchRow, spanishRow, ukrainianRow] = 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-language-install-button"]` + ); + const frenchDelete = frenchRow.querySelector( + `[data-l10n-id="translations-manage-language-remove-button"]` + ); + const spanishLabel = spanishRow.querySelector("label"); + const spanishDownload = spanishRow.querySelector( + `[data-l10n-id="translations-manage-language-install-button"]` + ); + const spanishDelete = spanishRow.querySelector( + `[data-l10n-id="translations-manage-language-remove-button"]` + ); + const ukrainianLabel = ukrainianRow.querySelector("label"); + const ukrainianDownload = ukrainianRow.querySelector( + `[data-l10n-id="translations-manage-language-install-button"]` + ); + const ukrainianDelete = ukrainianRow.querySelector( + `[data-l10n-id="translations-manage-language-remove-button"]` + ); + + return { + document, + downloadAllLabel, + downloadAll, + deleteAll, + frenchLabel, + frenchDownload, + frenchDelete, + ukrainianLabel, + ukrainianDownload, + ukrainianDelete, + settingsButton, + 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(); +} + +function hitEnterKey(button, message) { + info(message); + button.dispatchEvent( + new KeyboardEvent("keypress", { + key: "Enter", + keyCode: KeyboardEvent.DOM_VK_RETURN, + }) + ); +} + +/** + * Similar to assertVisibility, but is asynchronous and attempts + * to wait for the elements to match the expected states if they + * do not already. + * + * @see assertVisibility + * + * @param {Object} options + * @param {string} options.message + * @param {Record<string, Element[]>} options.visible + * @param {Record<string, Element[]>} options.hidden + */ +async function ensureVisibility({ message = null, visible = {}, hidden = {} }) { + try { + // First wait for the condition to be met. + await waitForCondition(() => { + for (const element of Object.values(visible)) { + if (BrowserTestUtils.isHidden(element)) { + return false; + } + } + for (const element of Object.values(hidden)) { + if (BrowserTestUtils.isVisible(element)) { + return false; + } + } + return true; + }); + } catch (error) { + // Ignore, this will get caught below. + } + // Now report the conditions. + assertVisibility({ message, visible, hidden }); +} + +/** + * Asserts that the provided elements are either visible or hidden. + * + * @param {Object} options + * @param {string} options.message + * @param {Record<string, Element[]>} options.visible + * @param {Record<string, Element[]>} options.hidden + */ +function assertVisibility({ message = null, visible = {}, hidden = {} }) { + if (message) { + info(message); + } + for (const [name, element] of Object.entries(visible)) { + ok(BrowserTestUtils.isVisible(element), `${name} is visible.`); + } + for (const [name, element] of Object.entries(hidden)) { + ok(BrowserTestUtils.isHidden(element), `${name} is hidden.`); + } +} + +async function setupAboutPreferences( + languagePairs, + { prefs = [], permissionsUrls = [] } = {} +) { + await SpecialPowers.pushPrefEnv({ + set: [ + // Enabled by default. + ["browser.translations.enable", true], + ["browser.translations.logLevel", "All"], + ...prefs, + ], + }); + await SpecialPowers.pushPermissions( + permissionsUrls.map(url => ({ + type: TRANSLATIONS_PERMISSION, + allow: true, + context: url, + })) + ); + const tab = await BrowserTestUtils.openNewForegroundTab( + gBrowser, + BLANK_PAGE, + true // waitForLoad + ); + + const { remoteClients, removeMocks } = await createAndMockRemoteSettings({ + languagePairs, + }); + + BrowserTestUtils.startLoadingURIString( + tab.linkedBrowser, + "about:preferences" + ); + await BrowserTestUtils.browserLoaded(tab.linkedBrowser); + + const elements = await selectAboutPreferencesElements(); + + async function cleanup() { + await loadBlankPage(); + await TranslationsParent.destroyEngineProcess(); + await closeTranslationsPanelIfOpen(); + await closeContextMenuIfOpen(); + BrowserTestUtils.removeTab(tab); + await removeMocks(); + await SpecialPowers.popPrefEnv(); + TestTranslationsTelemetry.reset(); + } + + 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 { + static #previousFlowId = null; + + static reset() { + TestTranslationsTelemetry.#previousFlowId = null; + } + + /** + * 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.expectedEventCount - The expected count of events. + * @param {boolean} expectations.expectNewFlowId + * - Expects the flowId to be different than the previous flowId if true, + * and expects it to be the same if false. + * @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( + event, + { + expectedEventCount, + expectNewFlowId = null, + expectFirstInteraction = null, + 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 events = event.testGetValue() ?? []; + const eventCount = events.length; + const name = + eventCount > 0 ? `${events[0].category}.${events[0].name}` : null; + + if (eventCount > 0 && expectFirstInteraction !== null) { + is( + events[eventCount - 1].extra.first_interaction, + expectFirstInteraction ? "true" : "false", + "The newest event should be match the given first-interaction expectation" + ); + } + + if (eventCount > 0 && expectNewFlowId !== null) { + const flowId = events[eventCount - 1].extra.flow_id; + if (expectNewFlowId) { + is( + events[eventCount - 1].extra.flow_id !== + TestTranslationsTelemetry.#previousFlowId, + true, + `The newest flowId ${flowId} should be different than the previous flowId ${ + TestTranslationsTelemetry.#previousFlowId + }` + ); + } else { + is( + events[eventCount - 1].extra.flow_id === + TestTranslationsTelemetry.#previousFlowId, + true, + `The newest flowId ${flowId} should be equal to the previous flowId ${ + TestTranslationsTelemetry.#previousFlowId + }` + ); + } + TestTranslationsTelemetry.#previousFlowId = flowId; + } + + if (eventCount !== expectedEventCount) { + console.error("Actual events:", events); + } + + is( + eventCount, + expectedEventCount, + `There should be ${expectedEventCount} telemetry events of type ${name}` + ); + + if (allValuePredicates.length !== 0) { + is( + eventCount > 0, + true, + `Telemetry event ${name} should contain values if allPredicates are specified` + ); + for (const value of events) { + 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( + eventCount > 0, + true, + `Telemetry event ${name} should contain values if finalPredicates are specified` + ); + for (const predicate of finalValuePredicates) { + is( + predicate(events[eventCount - 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` + ); + } +} + +/** + * Provide longer defaults for the waitForCondition. + * + * @param {Function} callback + * @param {string} messages + */ +function waitForCondition(callback, message) { + const interval = 100; + // Use 4 times the defaults to guard against intermittents. Many of the tests rely on + // communication between the parent and child process, which is inherently async. + const maxTries = 50 * 4; + return TestUtils.waitForCondition(callback, message, interval, maxTries); +} + +/** + * Retrieves the always-translate language list as an array. + * + * @returns {Array<string>} + */ +function getAlwaysTranslateLanguagesFromPref() { + let langs = Services.prefs.getCharPref(ALWAYS_TRANSLATE_LANGS_PREF); + return langs ? langs.split(",") : []; +} + +/** + * Retrieves the never-translate language list as an array. + * + * @returns {Array<string>} + */ +function getNeverTranslateLanguagesFromPref() { + let langs = Services.prefs.getCharPref(NEVER_TRANSLATE_LANGS_PREF); + return langs ? langs.split(",") : []; +} + +/** + * Retrieves the never-translate site list as an array. + * + * @returns {Array<string>} + */ +function getNeverTranslateSitesFromPerms() { + let results = []; + for (let perm of Services.perms.all) { + if ( + perm.type == TRANSLATIONS_PERMISSION && + perm.capability == Services.perms.DENY_ACTION + ) { + results.push(perm.principal); + } + } + + return results; +} + +/** + * Opens a dialog window for about:preferences + * @param {string} dialogUrl - The URL of the dialog window + * @param {Function} callback - The function to open the dialog via UI + * @returns {Object} The dialog window object + */ +async function waitForOpenDialogWindow(dialogUrl, callback) { + const dialogLoaded = promiseLoadSubDialog(dialogUrl); + await callback(); + const dialogWindow = await dialogLoaded; + return dialogWindow; +} + +/** + * Closes an open dialog window and waits for it to close. + * + * @param {Object} dialogWindow + */ +async function waitForCloseDialogWindow(dialogWindow) { + const closePromise = BrowserTestUtils.waitForEvent( + content.gSubDialog._dialogStack, + "dialogclose" + ); + dialogWindow.close(); + await closePromise; +} + +// Extracted from https://searchfox.org/mozilla-central/rev/40ef22080910c2e2c27d9e2120642376b1d8b8b2/browser/components/preferences/in-content/tests/head.js#41 +function promiseLoadSubDialog(aURL) { + return new Promise((resolve, reject) => { + content.gSubDialog._dialogStack.addEventListener( + "dialogopen", + function dialogopen(aEvent) { + if ( + aEvent.detail.dialog._frame.contentWindow.location == "about:blank" + ) { + return; + } + content.gSubDialog._dialogStack.removeEventListener( + "dialogopen", + dialogopen + ); + + Assert.equal( + aEvent.detail.dialog._frame.contentWindow.location.toString(), + aURL, + "Check the proper URL is loaded" + ); + + // Check visibility + isnot( + aEvent.detail.dialog._overlay, + null, + "Element should not be null, when checking visibility" + ); + Assert.ok( + !BrowserTestUtils.isHidden(aEvent.detail.dialog._overlay), + "The element is visible" + ); + + // Check that stylesheets were injected + let expectedStyleSheetURLs = + aEvent.detail.dialog._injectedStyleSheets.slice(0); + for (let styleSheet of aEvent.detail.dialog._frame.contentDocument + .styleSheets) { + let i = expectedStyleSheetURLs.indexOf(styleSheet.href); + if (i >= 0) { + info("found " + styleSheet.href); + expectedStyleSheetURLs.splice(i, 1); + } + } + Assert.equal( + expectedStyleSheetURLs.length, + 0, + "All expectedStyleSheetURLs should have been found" + ); + + // Wait for the next event tick to make sure the remaining part of the + // testcase runs after the dialog gets ready for input. + executeSoon(() => resolve(aEvent.detail.dialog._frame.contentWindow)); + } + ); + }); +} + +/** + * Loads the blank-page URL. + * + * This is useful for resetting the state during cleanup, and also + * before starting a test, to further help ensure that there is no + * unintentional state left over from test case. + */ +async function loadBlankPage() { + BrowserTestUtils.startLoadingURIString(gBrowser.selectedBrowser, BLANK_PAGE); + await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser); +} 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..3e16be57e9 --- /dev/null +++ b/toolkit/components/translations/tests/browser/translations-test.mjs @@ -0,0 +1,129 @@ +/* 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"); + }, + getHeader() { + return content.document.querySelector("header"); + }, + getFirstParagraph() { + return content.document.querySelector("p:first-of-type"); + }, + getLastParagraph() { + return content.document.querySelector("p:last-of-type"); + }, + getSpanishParagraph() { + return content.document.getElementById("spanish-paragraph"); + }, + getSpanishHyperlink() { + return content.document.getElementById("spanish-hyperlink"); + }, + getEnglishHyperlink() { + return content.document.getElementById("english-hyperlink"); + }, + }; +} + +/** + * Provide longer defaults for the waitForCondition. + * + * @param {Function} callback + * @param {string} messages + */ +function waitForCondition(callback, message) { + const interval = 100; + // Use 4 times the defaults to guard against intermittents. Many of the tests rely on + // communication between the parent and child process, which is inherently async. + const maxTries = 50 * 4; + return ContentTaskUtils.waitForCondition( + callback, + message, + interval, + maxTries + ); +} + +/** + * 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 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); +} + +/** + * Simulates right-clicking an element with the mouse. + * + * @param {element} element - The element to right-click. + */ +export function rightClickContentElement(element) { + return new Promise(resolve => { + element.addEventListener( + "contextmenu", + function () { + resolve(); + }, + { once: true } + ); + + const EventUtils = ContentTaskUtils.getEventUtils(content); + EventUtils.sendMouseEvent({ type: "contextmenu" }, element, content.window); + }); +} + +/** + * Selects all the content within a specified element. + * + * @param {Element} element - The element containing the content to be selected. + * @returns {string} - The text content of the selection. + */ +export function selectContentElement(element) { + content.focus(); + content.getSelection().selectAllChildren(element); + return element.textContent; +} diff --git a/toolkit/components/translations/tests/browser/translations-tester-empty-pdf-file.pdf b/toolkit/components/translations/tests/browser/translations-tester-empty-pdf-file.pdf new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/toolkit/components/translations/tests/browser/translations-tester-empty-pdf-file.pdf 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..f589df0649 --- /dev/null +++ b/toolkit/components/translations/tests/browser/translations-tester-es.html @@ -0,0 +1,44 @@ +<!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 id="spanish-paragraph">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> + <div> + <header lang="en">The following is a link to another test page in Spanish.</header> + <p><a id="spanish-hyperlink" href="https://example.org/browser/translations-tester-es.html">Otra pagina en español.</a></p> + </div> + <div> + <header lang="en">The following is a link to another test page in English.</header> + <p lang="en"><a id="english-hyperlink" href="https://example.org/browser/translations-tester-en.html">Another page in English.</a></p> + </div> +</body> +</html> diff --git a/toolkit/components/translations/tests/browser/translations-tester-fr.html b/toolkit/components/translations/tests/browser/translations-tester-fr.html new file mode 100644 index 0000000000..de8a158d25 --- /dev/null +++ b/toolkit/components/translations/tests/browser/translations-tester-fr.html @@ -0,0 +1,9 @@ +<!DOCTYPE html> +<html lang="fr"> +<head> + <meta charset="utf-8" /> + <title>Translations Test (fr)</title> +</head> +<body> +</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> diff --git a/toolkit/components/translations/tests/browser/translations-tester-shadow-dom-es.html b/toolkit/components/translations/tests/browser/translations-tester-shadow-dom-es.html new file mode 100644 index 0000000000..ed4b1ceec2 --- /dev/null +++ b/toolkit/components/translations/tests/browser/translations-tester-shadow-dom-es.html @@ -0,0 +1,38 @@ +<!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> + <h1>Esto se contenta en Luz DOM</h1> + <div id="host"></div> + <div><div><div id="host2"></div></div></div> + <script> + const host = document.getElementById("host"); + const root = host.attachShadow({mode: "open"}); + root.innerHTML = "<p>Esto se contento en Shadow DOM</p><div id='innerHost'></div>"; + + // Nested shadow tree + const innerHost = root.querySelector("div"); + const innerRoot = innerHost.attachShadow({mode: "open"}); + innerRoot.innerHTML = "<p>Esto se contenta en raíz interior </p>"; + + // Host within an empty textContent element + const host2 = document.getElementById("host2"); + const root2 = host2.attachShadow({mode: "open"}); + root2.innerHTML = "<p>Esto se contento en Shadow DOM 2</p>"; + </script> +</body> +</html> diff --git a/toolkit/components/translations/tests/browser/translations-tester-shadow-dom-mutation-es-2.html b/toolkit/components/translations/tests/browser/translations-tester-shadow-dom-mutation-es-2.html new file mode 100644 index 0000000000..b63d05cee8 --- /dev/null +++ b/toolkit/components/translations/tests/browser/translations-tester-shadow-dom-mutation-es-2.html @@ -0,0 +1,30 @@ +<!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> + <h1>This is content in light DOM</h1> + <div id="host1"></div> + <script> + const host1 = document.getElementById("host1"); + const root1 = host1.attachShadow({mode: "open"}); + root1.innerHTML = "<div id='innerHost'></div>"; + + const innerHost = root1.querySelector("div"); + const innerRoot = innerHost.attachShadow({mode: "open"}); + </script> +</body> +</html> diff --git a/toolkit/components/translations/tests/browser/translations-tester-shadow-dom-mutation-es.html b/toolkit/components/translations/tests/browser/translations-tester-shadow-dom-mutation-es.html new file mode 100644 index 0000000000..45add59a18 --- /dev/null +++ b/toolkit/components/translations/tests/browser/translations-tester-shadow-dom-mutation-es.html @@ -0,0 +1,26 @@ +<!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> + <h1>This is content in light DOM</h1> + <div id="host1"></div> + <script> + const host1 = document.getElementById("host1"); + host1.attachShadow({mode: "open"}); + </script> +</body> +</html> diff --git a/toolkit/components/translations/tests/browser/translations-tester-shadow-dom-slot-es.html b/toolkit/components/translations/tests/browser/translations-tester-shadow-dom-slot-es.html new file mode 100644 index 0000000000..a46e76b14c --- /dev/null +++ b/toolkit/components/translations/tests/browser/translations-tester-shadow-dom-slot-es.html @@ -0,0 +1,28 @@ +<!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 id="host"> + Esto se contenta en luz dom + <div> + <script> + const host = document.getElementById("host"); + const root = host.attachShadow({mode: "open"}); + root.innerHTML = "<slot></slot>"; + </script> +</body> +</html> |