From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- .../translations/tests/browser/browser.toml | 110 ++ ...bout_preferences_manage_downloaded_languages.js | 225 ++++ ...ferences_settings_always_translate_languages.js | 97 ++ ...eferences_settings_never_translate_languages.js | 89 ++ ...t_preferences_settings_never_translate_sites.js | 124 ++ ...r_translations_about_preferences_settings_ui.js | 87 ++ .../browser_translations_panel_a11y_focus.js | 32 + ...ons_panel_always_translate_language_bad_data.js | 40 + ...ations_panel_always_translate_language_basic.js | 79 ++ ...tions_panel_always_translate_language_manual.js | 80 ++ ...ions_panel_always_translate_language_restore.js | 101 ++ ...ions_panel_app_menu_never_translate_language.js | 40 + ...slations_panel_app_menu_never_translate_site.js | 140 ++ ...translations_panel_auto_translate_error_view.js | 86 ++ ...anslations_panel_auto_translate_revisit_view.js | 86 ++ .../browser/browser_translations_panel_basics.js | 69 + .../browser/browser_translations_panel_button.js | 77 ++ .../browser/browser_translations_panel_cancel.js | 27 + ..._translate_language_with_translations_active.js | 147 ++ ...ranslate_language_with_translations_inactive.js | 93 ++ ...tions_panel_close_panel_never_translate_site.js | 159 +++ .../browser_translations_panel_engine_destroy.js | 55 + ...er_translations_panel_engine_destroy_pending.js | 72 + ...rowser_translations_panel_engine_unsupported.js | 59 + ...r_translations_panel_engine_unsupported_lang.js | 28 + .../browser/browser_translations_panel_firstrun.js | 33 + .../browser_translations_panel_firstrun_revisit.js | 57 + .../browser/browser_translations_panel_fuzzing.js | 240 ++++ .../browser/browser_translations_panel_gear.js | 32 + ..._translations_panel_never_translate_language.js | 186 +++ ...wser_translations_panel_never_translate_site.js | 247 ++++ ...translations_panel_never_translate_site_auto.js | 109 ++ ...ranslations_panel_never_translate_site_basic.js | 62 + ...anslations_panel_never_translate_site_manual.js | 84 ++ .../browser/browser_translations_panel_retry.js | 54 + ...translations_panel_settings_unsupported_lang.js | 74 + .../browser_translations_panel_switch_languages.js | 70 + .../browser/browser_translations_reader_mode.js | 115 ++ ...lations_select_context_menu_feature_disabled.js | 114 ++ ...text_menu_with_full_page_translations_active.js | 162 +++ ...nslations_select_context_menu_with_hyperlink.js | 109 ++ ...ns_select_context_menu_with_no_text_selected.js | 37 + ...tions_select_context_menu_with_text_selected.js | 76 ++ ...anslations_telemetry_firstrun_auto_translate.js | 117 ++ ...owser_translations_telemetry_firstrun_basics.js | 93 ++ ...tions_telemetry_firstrun_translation_failure.js | 149 ++ ...slations_telemetry_firstrun_unsupported_lang.js | 122 ++ .../browser_translations_telemetry_open_panel.js | 85 ++ ...wser_translations_telemetry_panel_auto_offer.js | 83 ++ ...slations_telemetry_panel_auto_offer_settings.js | 110 ++ ...wser_translations_telemetry_switch_languages.js | 156 +++ ...r_translations_telemetry_translation_failure.js | 212 +++ ...r_translations_telemetry_translation_request.js | 170 +++ .../components/translations/tests/browser/head.js | 1423 ++++++++++++++++++++ 54 files changed, 6783 insertions(+) create mode 100644 browser/components/translations/tests/browser/browser.toml create mode 100644 browser/components/translations/tests/browser/browser_translations_about_preferences_manage_downloaded_languages.js create mode 100644 browser/components/translations/tests/browser/browser_translations_about_preferences_settings_always_translate_languages.js create mode 100644 browser/components/translations/tests/browser/browser_translations_about_preferences_settings_never_translate_languages.js create mode 100644 browser/components/translations/tests/browser/browser_translations_about_preferences_settings_never_translate_sites.js create mode 100644 browser/components/translations/tests/browser/browser_translations_about_preferences_settings_ui.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_a11y_focus.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_always_translate_language_bad_data.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_always_translate_language_basic.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_always_translate_language_manual.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_always_translate_language_restore.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_app_menu_never_translate_language.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_app_menu_never_translate_site.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_auto_translate_error_view.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_auto_translate_revisit_view.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_basics.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_button.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_cancel.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_close_panel_never_translate_language_with_translations_active.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_close_panel_never_translate_language_with_translations_inactive.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_close_panel_never_translate_site.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_engine_destroy.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_engine_destroy_pending.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_engine_unsupported.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_engine_unsupported_lang.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_firstrun.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_firstrun_revisit.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_fuzzing.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_gear.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_never_translate_language.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_never_translate_site.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_never_translate_site_auto.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_never_translate_site_basic.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_never_translate_site_manual.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_retry.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_settings_unsupported_lang.js create mode 100644 browser/components/translations/tests/browser/browser_translations_panel_switch_languages.js create mode 100644 browser/components/translations/tests/browser/browser_translations_reader_mode.js create mode 100644 browser/components/translations/tests/browser/browser_translations_select_context_menu_feature_disabled.js create mode 100644 browser/components/translations/tests/browser/browser_translations_select_context_menu_with_full_page_translations_active.js create mode 100644 browser/components/translations/tests/browser/browser_translations_select_context_menu_with_hyperlink.js create mode 100644 browser/components/translations/tests/browser/browser_translations_select_context_menu_with_no_text_selected.js create mode 100644 browser/components/translations/tests/browser/browser_translations_select_context_menu_with_text_selected.js create mode 100644 browser/components/translations/tests/browser/browser_translations_telemetry_firstrun_auto_translate.js create mode 100644 browser/components/translations/tests/browser/browser_translations_telemetry_firstrun_basics.js create mode 100644 browser/components/translations/tests/browser/browser_translations_telemetry_firstrun_translation_failure.js create mode 100644 browser/components/translations/tests/browser/browser_translations_telemetry_firstrun_unsupported_lang.js create mode 100644 browser/components/translations/tests/browser/browser_translations_telemetry_open_panel.js create mode 100644 browser/components/translations/tests/browser/browser_translations_telemetry_panel_auto_offer.js create mode 100644 browser/components/translations/tests/browser/browser_translations_telemetry_panel_auto_offer_settings.js create mode 100644 browser/components/translations/tests/browser/browser_translations_telemetry_switch_languages.js create mode 100644 browser/components/translations/tests/browser/browser_translations_telemetry_translation_failure.js create mode 100644 browser/components/translations/tests/browser/browser_translations_telemetry_translation_request.js create mode 100644 browser/components/translations/tests/browser/head.js (limited to 'browser/components/translations/tests') diff --git a/browser/components/translations/tests/browser/browser.toml b/browser/components/translations/tests/browser/browser.toml new file mode 100644 index 0000000000..bc617bf2fd --- /dev/null +++ b/browser/components/translations/tests/browser/browser.toml @@ -0,0 +1,110 @@ +[DEFAULT] +support-files = [ + "head.js", + "!/toolkit/components/translations/tests/browser/shared-head.js", + "!/toolkit/components/translations/tests/browser/translations-test.mjs", +] + +["browser_translations_about_preferences_manage_downloaded_languages.js"] + +["browser_translations_about_preferences_settings_always_translate_languages.js"] + +["browser_translations_about_preferences_settings_never_translate_languages.js"] + +["browser_translations_about_preferences_settings_never_translate_sites.js"] + +["browser_translations_about_preferences_settings_ui.js"] + +["browser_translations_panel_a11y_focus.js"] + +["browser_translations_panel_always_translate_language_bad_data.js"] + +["browser_translations_panel_always_translate_language_basic.js"] + +["browser_translations_panel_always_translate_language_manual.js"] + +["browser_translations_panel_always_translate_language_restore.js"] + +["browser_translations_panel_app_menu_never_translate_language.js"] + +["browser_translations_panel_app_menu_never_translate_site.js"] + +["browser_translations_panel_auto_translate_error_view.js"] + +["browser_translations_panel_auto_translate_revisit_view.js"] + +["browser_translations_panel_basics.js"] + +["browser_translations_panel_button.js"] + +["browser_translations_panel_cancel.js"] + +["browser_translations_panel_close_panel_never_translate_language_with_translations_active.js"] + +["browser_translations_panel_close_panel_never_translate_language_with_translations_inactive.js"] + +["browser_translations_panel_close_panel_never_translate_site.js"] + +["browser_translations_panel_engine_destroy.js"] + +["browser_translations_panel_engine_destroy_pending.js"] + +["browser_translations_panel_engine_unsupported.js"] + +["browser_translations_panel_engine_unsupported_lang.js"] + +["browser_translations_panel_firstrun.js"] + +["browser_translations_panel_firstrun_revisit.js"] + +["browser_translations_panel_fuzzing.js"] +skip-if = ["true"] + +["browser_translations_panel_gear.js"] + +["browser_translations_panel_never_translate_language.js"] + +["browser_translations_panel_never_translate_site_auto.js"] + +["browser_translations_panel_never_translate_site_basic.js"] + +["browser_translations_panel_never_translate_site_manual.js"] + +["browser_translations_panel_retry.js"] +skip-if = ["os == 'linux' && !debug"] # Bug 1863227 + +["browser_translations_panel_settings_unsupported_lang.js"] + +["browser_translations_panel_switch_languages.js"] + +["browser_translations_reader_mode.js"] + +["browser_translations_select_context_menu_feature_disabled.js"] + +["browser_translations_select_context_menu_with_full_page_translations_active.js"] + +["browser_translations_select_context_menu_with_hyperlink.js"] + +["browser_translations_select_context_menu_with_no_text_selected.js"] + +["browser_translations_select_context_menu_with_text_selected.js"] + +["browser_translations_telemetry_firstrun_auto_translate.js"] + +["browser_translations_telemetry_firstrun_basics.js"] + +["browser_translations_telemetry_firstrun_translation_failure.js"] + +["browser_translations_telemetry_firstrun_unsupported_lang.js"] + +["browser_translations_telemetry_open_panel.js"] + +["browser_translations_telemetry_panel_auto_offer.js"] + +["browser_translations_telemetry_panel_auto_offer_settings.js"] + +["browser_translations_telemetry_switch_languages.js"] + +["browser_translations_telemetry_translation_failure.js"] + +["browser_translations_telemetry_translation_request.js"] diff --git a/browser/components/translations/tests/browser/browser_translations_about_preferences_manage_downloaded_languages.js b/browser/components/translations/tests/browser/browser_translations_about_preferences_manage_downloaded_languages.js new file mode 100644 index 0000000000..383f2094a7 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_about_preferences_manage_downloaded_languages.js @@ -0,0 +1,225 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const frenchModels = [ + "lex.50.50.enfr.s2t.bin", + "lex.50.50.fren.s2t.bin", + "model.enfr.intgemm.alphas.bin", + "model.fren.intgemm.alphas.bin", + "vocab.enfr.spm", + "vocab.fren.spm", +]; + +add_task(async function test_about_preferences_manage_languages() { + const { + cleanup, + remoteClients, + elements: { + downloadAllLabel, + downloadAll, + deleteAll, + frenchLabel, + frenchDownload, + frenchDelete, + spanishLabel, + spanishDownload, + spanishDelete, + ukrainianLabel, + ukrainianDownload, + ukrainianDelete, + }, + } = await setupAboutPreferences(LANGUAGE_PAIRS, { + prefs: [["browser.translations.newSettingsUI.enable", false]], + }); + + is( + downloadAllLabel.getAttribute("data-l10n-id"), + "translations-manage-install-description", + "The first row is all of the languages." + ); + is(frenchLabel.textContent, "French", "There is a French row."); + is(spanishLabel.textContent, "Spanish", "There is a Spanish row."); + is(ukrainianLabel.textContent, "Ukrainian", "There is a Ukrainian row."); + + await ensureVisibility({ + message: "Everything starts out as available to download", + visible: { + downloadAll, + frenchDownload, + spanishDownload, + ukrainianDownload, + }, + hidden: { deleteAll, frenchDelete, spanishDelete, ukrainianDelete }, + }); + + click(frenchDownload, "Downloading French"); + + Assert.deepEqual( + await remoteClients.translationModels.resolvePendingDownloads( + frenchModels.length + ), + frenchModels, + "French models were downloaded." + ); + + await ensureVisibility({ + message: "French can now be deleted, and delete all is available.", + visible: { + downloadAll, + deleteAll, + frenchDelete, + spanishDownload, + ukrainianDownload, + }, + hidden: { frenchDownload, spanishDelete, ukrainianDelete }, + }); + + click(frenchDelete, "Deleting French"); + + await ensureVisibility({ + message: "Everything can be downloaded.", + visible: { + downloadAll, + frenchDownload, + spanishDownload, + ukrainianDownload, + }, + hidden: { deleteAll, frenchDelete, spanishDelete, ukrainianDelete }, + }); + + click(downloadAll, "Downloading all languages."); + + const allModels = [ + "lex.50.50.enes.s2t.bin", + "lex.50.50.enfr.s2t.bin", + "lex.50.50.enuk.s2t.bin", + "lex.50.50.esen.s2t.bin", + "lex.50.50.fren.s2t.bin", + "lex.50.50.uken.s2t.bin", + "model.enes.intgemm.alphas.bin", + "model.enfr.intgemm.alphas.bin", + "model.enuk.intgemm.alphas.bin", + "model.esen.intgemm.alphas.bin", + "model.fren.intgemm.alphas.bin", + "model.uken.intgemm.alphas.bin", + "vocab.enes.spm", + "vocab.enfr.spm", + "vocab.enuk.spm", + "vocab.esen.spm", + "vocab.fren.spm", + "vocab.uken.spm", + ]; + Assert.deepEqual( + await remoteClients.translationModels.resolvePendingDownloads( + allModels.length + ), + allModels, + "All models were downloaded." + ); + Assert.deepEqual( + await remoteClients.translationsWasm.resolvePendingDownloads(1), + ["bergamot-translator"], + "Wasm was downloaded." + ); + + await ensureVisibility({ + message: "Everything can be deleted.", + visible: { deleteAll, frenchDelete, spanishDelete, ukrainianDelete }, + hidden: { downloadAll, frenchDownload, spanishDownload, ukrainianDownload }, + }); + + click(deleteAll, "Deleting all languages."); + + await ensureVisibility({ + message: "Everything can be downloaded again", + visible: { + downloadAll, + frenchDownload, + spanishDownload, + ukrainianDownload, + }, + hidden: { deleteAll, frenchDelete, spanishDelete, ukrainianDelete }, + }); + + click(frenchDownload, "Downloading French."); + click(spanishDownload, "Downloading Spanish."); + click(ukrainianDownload, "Downloading Ukrainian."); + + Assert.deepEqual( + await remoteClients.translationModels.resolvePendingDownloads( + allModels.length + ), + allModels, + "All models were downloaded again." + ); + + remoteClients.translationsWasm.assertNoNewDownloads(); + + await ensureVisibility({ + message: "Everything is downloaded again.", + visible: { deleteAll, frenchDelete, spanishDelete, ukrainianDelete }, + hidden: { downloadAll, frenchDownload, spanishDownload, ukrainianDownload }, + }); + + await cleanup(); +}); + +add_task(async function test_about_preferences_download_reject() { + const { + cleanup, + remoteClients, + elements: { document, frenchDownload }, + } = await setupAboutPreferences(LANGUAGE_PAIRS, { + prefs: [["browser.translations.newSettingsUI.enable", false]], + }); + + click(frenchDownload, "Downloading French"); + + is( + maybeGetByL10nId("translations-manage-error-install", document), + null, + "No error messages are present." + ); + + const failureErrors = await captureTranslationsError(() => + remoteClients.translationModels.rejectPendingDownloads(frenchModels.length) + ); + + ok( + !!failureErrors.length, + `The errors for download should have been reported, found ${failureErrors.length} errors` + ); + for (const { error } of failureErrors) { + is( + error?.message, + "Failed to download file.", + "The error reported was a download error." + ); + } + + await waitForCondition( + () => maybeGetByL10nId("translations-manage-error-install", document), + "The error message is now visible." + ); + + click(frenchDownload, "Attempting to download French again", document); + is( + maybeGetByL10nId("translations-manage-error-install", document), + null, + "The error message is hidden again." + ); + + const successErrors = await captureTranslationsError(() => + remoteClients.translationModels.resolvePendingDownloads(frenchModels.length) + ); + + is( + successErrors.length, + 0, + "Expected no errors downloading French the second time" + ); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_about_preferences_settings_always_translate_languages.js b/browser/components/translations/tests/browser/browser_translations_about_preferences_settings_always_translate_languages.js new file mode 100644 index 0000000000..9f40003bfc --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_about_preferences_settings_always_translate_languages.js @@ -0,0 +1,97 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +add_task( + async function test_about_preferences_always_translate_language_settings() { + const { + cleanup, + elements: { settingsButton }, + } = await setupAboutPreferences(LANGUAGE_PAIRS, { + prefs: [["browser.translations.newSettingsUI.enable", false]], + }); + + info("Ensuring the list of always-translate languages is empty"); + is( + getAlwaysTranslateLanguagesFromPref().length, + 0, + "The list of always-translate languages is empty" + ); + + info("Adding two languages to the alwaysTranslateLanguages pref"); + Services.prefs.setCharPref(ALWAYS_TRANSLATE_LANGS_PREF, "fr,de"); + + const dialogWindow = await waitForOpenDialogWindow( + "chrome://browser/content/preferences/dialogs/translations.xhtml", + () => { + click( + settingsButton, + "Opening the about:preferences Translations Settings" + ); + } + ); + let tree = dialogWindow.document.getElementById( + "alwaysTranslateLanguagesTree" + ); + let remove = dialogWindow.document.getElementById( + "removeAlwaysTranslateLanguage" + ); + let removeAll = dialogWindow.document.getElementById( + "removeAllAlwaysTranslateLanguages" + ); + + is( + tree.view.rowCount, + 2, + "The always-translate languages list has 2 items" + ); + ok(remove.disabled, "The 'Remove Language' button is disabled"); + ok(!removeAll.disabled, "The 'Remove All Languages' button is enabled"); + + info("Selecting the first always-translate language."); + tree.view.selection.select(0); + ok(!remove.disabled, "The 'Remove Language' button is enabled"); + + click(remove, "Clicking the remove-language button"); + is( + tree.view.rowCount, + 1, + "The always-translate languages list now contains 1 item" + ); + is( + getAlwaysTranslateLanguagesFromPref().length, + 1, + "One language tag in the pref" + ); + + info("Removing all languages from the alwaysTranslateLanguages pref"); + Services.prefs.setCharPref(ALWAYS_TRANSLATE_LANGS_PREF, ""); + is(tree.view.rowCount, 0, "The always-translate languages list is empty"); + ok(remove.disabled, "The 'Remove Language' button is disabled"); + ok(removeAll.disabled, "The 'Remove All Languages' button is disabled"); + + info("Adding more languages to the alwaysTranslateLanguages pref"); + Services.prefs.setCharPref(ALWAYS_TRANSLATE_LANGS_PREF, "fr,en,es"); + is( + tree.view.rowCount, + 3, + "The always-translate languages list has 3 items" + ); + ok(remove.disabled, "The 'Remove Language' button is disabled"); + ok(!removeAll.disabled, "The 'Remove All Languages' button is enabled"); + + click(removeAll, "Clicking the remove-all languages button"); + is(tree.view.rowCount, 0, "The always-translate languages list is empty"); + ok(remove.disabled, "The 'Remove Language' button is disabled"); + ok(removeAll.disabled, "The 'Remove All Languages' button is disabled"); + is( + getAlwaysTranslateLanguagesFromPref().length, + 0, + "There are no languages in the alwaysTranslateLanguages pref" + ); + + await waitForCloseDialogWindow(dialogWindow); + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_about_preferences_settings_never_translate_languages.js b/browser/components/translations/tests/browser/browser_translations_about_preferences_settings_never_translate_languages.js new file mode 100644 index 0000000000..758cfb4fba --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_about_preferences_settings_never_translate_languages.js @@ -0,0 +1,89 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +add_task( + async function test_about_preferences_never_translate_language_settings() { + const { + cleanup, + elements: { settingsButton }, + } = await setupAboutPreferences(LANGUAGE_PAIRS, { + prefs: [["browser.translations.newSettingsUI.enable", false]], + }); + + info("Ensuring the list of never-translate languages is empty"); + is( + getNeverTranslateLanguagesFromPref().length, + 0, + "The list of never-translate languages is empty" + ); + + info("Adding two languages to the neverTranslateLanguages pref"); + Services.prefs.setCharPref(NEVER_TRANSLATE_LANGS_PREF, "fr,de"); + + const dialogWindow = await waitForOpenDialogWindow( + "chrome://browser/content/preferences/dialogs/translations.xhtml", + () => { + click( + settingsButton, + "Opening the about:preferences Translations Settings" + ); + } + ); + let tree = dialogWindow.document.getElementById( + "neverTranslateLanguagesTree" + ); + let remove = dialogWindow.document.getElementById( + "removeNeverTranslateLanguage" + ); + let removeAll = dialogWindow.document.getElementById( + "removeAllNeverTranslateLanguages" + ); + + is(tree.view.rowCount, 2, "The never-translate languages list has 2 items"); + ok(remove.disabled, "The 'Remove Language' button is disabled"); + ok(!removeAll.disabled, "The 'Remove All Languages' button is enabled"); + + info("Selecting the first never-translate language."); + tree.view.selection.select(0); + ok(!remove.disabled, "The 'Remove Language' button is enabled"); + + click(remove, "Clicking the remove-language button"); + is( + tree.view.rowCount, + 1, + "The never-translate languages list now contains 1 item" + ); + is( + getNeverTranslateLanguagesFromPref().length, + 1, + "One language tag in the pref" + ); + + info("Removing all languages from the neverTranslateLanguages pref"); + Services.prefs.setCharPref(NEVER_TRANSLATE_LANGS_PREF, ""); + is(tree.view.rowCount, 0, "The never-translate languages list is empty"); + ok(remove.disabled, "The 'Remove Language' button is disabled"); + ok(removeAll.disabled, "The 'Remove All Languages' button is disabled"); + + info("Adding more languages to the neverTranslateLanguages pref"); + Services.prefs.setCharPref(NEVER_TRANSLATE_LANGS_PREF, "fr,en,es"); + is(tree.view.rowCount, 3, "The never-translate languages list has 3 items"); + ok(remove.disabled, "The 'Remove Language' button is disabled"); + ok(!removeAll.disabled, "The 'Remove All Languages' button is enabled"); + + click(removeAll, "Clicking the remove-all languages button"); + is(tree.view.rowCount, 0, "The never-translate languages list is empty"); + ok(remove.disabled, "The 'Remove Language' button is disabled"); + ok(removeAll.disabled, "The 'Remove All Languages' button is disabled"); + is( + getNeverTranslateLanguagesFromPref().length, + 0, + "There are no languages in the neverTranslateLanguages pref" + ); + + await waitForCloseDialogWindow(dialogWindow); + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_about_preferences_settings_never_translate_sites.js b/browser/components/translations/tests/browser/browser_translations_about_preferences_settings_never_translate_sites.js new file mode 100644 index 0000000000..dea7b1c473 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_about_preferences_settings_never_translate_sites.js @@ -0,0 +1,124 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const { PermissionTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/PermissionTestUtils.sys.mjs" +); + +add_task(async function test_about_preferences_never_translate_site_settings() { + const { + cleanup, + elements: { settingsButton }, + } = await setupAboutPreferences(LANGUAGE_PAIRS, { + prefs: [["browser.translations.newSettingsUI.enable", false]], + permissionsUrls: [ + "https://example.com", + "https://example.org", + "https://example.net", + ], + }); + + info("Ensuring the list of never-translate sites is empty"); + is( + getNeverTranslateSitesFromPerms().length, + 0, + "The list of never-translate sites is empty" + ); + + info("Adding two sites to the neverTranslateSites perms"); + PermissionTestUtils.add( + "https://example.com", + TRANSLATIONS_PERMISSION, + Services.perms.DENY_ACTION + ); + PermissionTestUtils.add( + "https://example.org", + TRANSLATIONS_PERMISSION, + Services.perms.DENY_ACTION + ); + PermissionTestUtils.add( + "https://example.net", + TRANSLATIONS_PERMISSION, + Services.perms.DENY_ACTION + ); + + const dialogWindow = await waitForOpenDialogWindow( + "chrome://browser/content/preferences/dialogs/translations.xhtml", + () => { + click( + settingsButton, + "Opening the about:preferences Translations Settings" + ); + } + ); + let tree = dialogWindow.document.getElementById("neverTranslateSitesTree"); + let remove = dialogWindow.document.getElementById("removeNeverTranslateSite"); + let removeAll = dialogWindow.document.getElementById( + "removeAllNeverTranslateSites" + ); + + is(tree.view.rowCount, 3, "The never-translate sites list has 2 items"); + ok(remove.disabled, "The 'Remove Site' button is disabled"); + ok(!removeAll.disabled, "The 'Remove All Sites' button is enabled"); + + info("Selecting the first never-translate site."); + tree.view.selection.select(0); + ok(!remove.disabled, "The 'Remove Site' button is enabled"); + + click(remove, "Clicking the remove-site button"); + is( + tree.view.rowCount, + 2, + "The never-translate sites list now contains 2 items" + ); + is( + getNeverTranslateSitesFromPerms().length, + 2, + "There are 2 sites with permissions" + ); + + info("Removing all sites from the neverTranslateSites perms"); + PermissionTestUtils.remove("https://example.com", TRANSLATIONS_PERMISSION); + PermissionTestUtils.remove("https://example.org", TRANSLATIONS_PERMISSION); + PermissionTestUtils.remove("https://example.net", TRANSLATIONS_PERMISSION); + + is(tree.view.rowCount, 0, "The never-translate sites list is empty"); + ok(remove.disabled, "The 'Remove Site' button is disabled"); + ok(removeAll.disabled, "The 'Remove All Sites' button is disabled"); + + info("Adding more sites to the neverTranslateSites perms"); + PermissionTestUtils.add( + "https://example.org", + TRANSLATIONS_PERMISSION, + Services.perms.DENY_ACTION + ); + PermissionTestUtils.add( + "https://example.com", + TRANSLATIONS_PERMISSION, + Services.perms.DENY_ACTION + ); + PermissionTestUtils.add( + "https://example.net", + TRANSLATIONS_PERMISSION, + Services.perms.DENY_ACTION + ); + + is(tree.view.rowCount, 3, "The never-translate sites list has 3 items"); + ok(remove.disabled, "The 'Remove Site' button is disabled"); + ok(!removeAll.disabled, "The 'Remove All Sites' button is enabled"); + + click(removeAll, "Clicking the remove-all sites button"); + is(tree.view.rowCount, 0, "The never-translate sites list is empty"); + ok(remove.disabled, "The 'Remove Site' button is disabled"); + ok(removeAll.disabled, "The 'Remove All Sites' button is disabled"); + is( + getNeverTranslateSitesFromPerms().length, + 0, + "There are no sites in the neverTranslateSites perms" + ); + + await waitForCloseDialogWindow(dialogWindow); + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_about_preferences_settings_ui.js b/browser/components/translations/tests/browser/browser_translations_about_preferences_settings_ui.js new file mode 100644 index 0000000000..ee81b84a36 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_about_preferences_settings_ui.js @@ -0,0 +1,87 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +add_task(async function test_translations_settings_pane_elements() { + const { + cleanup, + elements: { settingsButton }, + } = await setupAboutPreferences(LANGUAGE_PAIRS, { + prefs: [["browser.translations.newSettingsUI.enable", true]], + }); + + assertVisibility({ + message: "Expect paneGeneral elements to be visible.", + visible: { settingsButton }, + }); + + const { + backButton, + header, + translationsSettingsDescription, + translateAlwaysHeader, + translateNeverHeader, + translateAlwaysAddButton, + translateNeverAddButton, + translateNeverSiteHeader, + translateNeverSiteDesc, + translateDownloadLanguagesHeader, + translateDownloadLanguagesLearnMore, + } = + await TranslationsSettingsTestUtils.openAboutPreferencesTranslationsSettingsPane( + settingsButton + ); + + assertVisibility({ + message: "Expect paneTranslations elements to be visible.", + visible: { + backButton, + header, + translationsSettingsDescription, + translateAlwaysHeader, + translateNeverHeader, + translateAlwaysAddButton, + translateNeverAddButton, + translateNeverSiteHeader, + translateNeverSiteDesc, + translateDownloadLanguagesHeader, + translateDownloadLanguagesLearnMore, + }, + hidden: { + settingsButton, + }, + }); + + const promise = BrowserTestUtils.waitForEvent( + document, + "paneshown", + false, + event => event.detail.category === "paneGeneral" + ); + + click(backButton); + await promise; + + assertVisibility({ + message: "Expect paneGeneral elements to be visible.", + visible: { + settingsButton, + }, + hidden: { + backButton, + header, + translationsSettingsDescription, + translateAlwaysHeader, + translateNeverHeader, + translateAlwaysAddButton, + translateNeverAddButton, + translateNeverSiteHeader, + translateNeverSiteDesc, + translateDownloadLanguagesHeader, + translateDownloadLanguagesLearnMore, + }, + }); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_a11y_focus.js b/browser/components/translations/tests/browser/browser_translations_panel_a11y_focus.js new file mode 100644 index 0000000000..e2e8663f6d --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_a11y_focus.js @@ -0,0 +1,32 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests the a11y focus behavior. + */ +add_task(async function test_translations_panel_a11y_focus() { + const { cleanup } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openWithKeyboard: true, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + is( + document.activeElement.getAttribute("data-l10n-id"), + "translations-panel-settings-button", + "The settings button is focused." + ); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_always_translate_language_bad_data.js b/browser/components/translations/tests/browser/browser_translations_panel_always_translate_language_bad_data.js new file mode 100644 index 0000000000..ac026fb78f --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_always_translate_language_bad_data.js @@ -0,0 +1,40 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests that having an "always translate" set to your app locale doesn't break things. + */ +add_task(async function test_always_translate_with_bad_data() { + const { cleanup, runInPage } = await loadTestPage({ + page: ENGLISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.alwaysTranslateLanguages", "en,fr"]], + }); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + openFromAppMenu: true, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("en", { + checked: false, + disabled: true, + }); + await closeSettingsMenuIfOpen(); + await closeTranslationsPanelIfOpen(); + + info("Checking that the page is untranslated"); + await runInPage(async TranslationsTest => { + const { getH1 } = TranslationsTest.getSelectors(); + await TranslationsTest.assertTranslationResult( + "The page's H1 is untranslated and in the original English.", + getH1, + '"The Wonderful Wizard of Oz" by L. Frank Baum' + ); + }); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_always_translate_language_basic.js b/browser/components/translations/tests/browser/browser_translations_panel_always_translate_language_basic.js new file mode 100644 index 0000000000..2644d78f33 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_always_translate_language_basic.js @@ -0,0 +1,79 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests the effect of toggling the always-translate-language menuitem. + * Checking the box on an untranslated page should immediately translate the page. + * Unchecking the box on a translated page should immediately restore the page. + */ +add_task(async function test_toggle_always_translate_language_menuitem() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The translations button is visible." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.clickAlwaysTranslateLanguage({ + downloadHandler: resolveDownloads, + }); + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage, + "The page should be automatically translated." + ); + + await navigate("Navigate to a different Spanish page", { + url: SPANISH_PAGE_URL_DOT_ORG, + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage, + "The page should be automatically translated." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.clickAlwaysTranslateLanguage(); + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: false, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "Only the button appears" + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_always_translate_language_manual.js b/browser/components/translations/tests/browser/browser_translations_panel_always_translate_language_manual.js new file mode 100644 index 0000000000..8456b4cc08 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_always_translate_language_manual.js @@ -0,0 +1,80 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests the effect of toggling the always-translate-language menuitem after the page has + * been manually translated. This should not reload or retranslate the page, but just check + * the box. + */ +add_task( + async function test_activate_always_translate_language_after_manual_translation() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.clickTranslateButton({ + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.clickAlwaysTranslateLanguage(); + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.clickAlwaysTranslateLanguage(); + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: false, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "Only the button appears" + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_always_translate_language_restore.js b/browser/components/translations/tests/browser/browser_translations_panel_always_translate_language_restore.js new file mode 100644 index 0000000000..6cf89d2a03 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_always_translate_language_restore.js @@ -0,0 +1,101 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests the effect of unchecking the always-translate language menuitem after the page has + * been manually restored to its original form. + * This should have no effect on the page, and further page loads should no longer auto-translate. + */ +add_task( + async function test_deactivate_always_translate_language_after_restore() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The translations button is visible." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.clickAlwaysTranslateLanguage({ + downloadHandler: resolveDownloads, + }); + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage, + "The page should be automatically translated." + ); + + await navigate("Navigate to a different Spanish page", { + url: SPANISH_PAGE_URL_DOT_ORG, + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage, + "The page should be automatically translated." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + + await FullPageTranslationsTestUtils.clickRestoreButton(); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is reverted to have an icon." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.clickAlwaysTranslateLanguage(); + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: false, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button shows only the icon." + ); + + await navigate("Reload the page", { url: SPANISH_PAGE_URL_DOT_ORG }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button shows only the icon." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_app_menu_never_translate_language.js b/browser/components/translations/tests/browser/browser_translations_panel_app_menu_never_translate_language.js new file mode 100644 index 0000000000..ee2905ab99 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_app_menu_never_translate_language.js @@ -0,0 +1,40 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests the effect of unchecking the never-translate-language menuitem, removing + * the language from the never-translate languages list. + * The translations button should reappear. + */ +add_task(async function test_uncheck_never_translate_language_shows_button() { + const { cleanup, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.neverTranslateLanguages", "es"]], + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The translations button is available" + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsNeverTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.clickNeverTranslateLanguage(); + await FullPageTranslationsTestUtils.assertIsNeverTranslateLanguage("es", { + checked: false, + }); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_app_menu_never_translate_site.js b/browser/components/translations/tests/browser/browser_translations_panel_app_menu_never_translate_site.js new file mode 100644 index 0000000000..50fff4dff8 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_app_menu_never_translate_site.js @@ -0,0 +1,140 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests the effect of unchecking the never-translate-site menuitem, + * regranting translations permissions to this page. + * The translations button should reappear. + */ +add_task(async function test_uncheck_never_translate_site_shows_button() { + const { cleanup, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The translations button is visible." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: false } + ); + await FullPageTranslationsTestUtils.clickNeverTranslateSite(); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: true } + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: true } + ); + await FullPageTranslationsTestUtils.clickNeverTranslateSite(); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: false } + ); + + await cleanup(); +}); + +/** + * Tests the effect of unchecking the never-translate-site menuitem while + * the current language is in the always-translate languages list, regranting + * translations permissions to this page. + * The page should automatically translate. + */ +add_task( + async function test_uncheck_never_translate_site_with_always_translate_language() { + const { cleanup, runInPage, resolveDownloads } = await loadTestPage({ + page: BLANK_PAGE, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.alwaysTranslateLanguages", "es"]], + }); + + await navigate("Navigate to a Spanish page", { + url: SPANISH_PAGE_URL, + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: false } + ); + + await FullPageTranslationsTestUtils.clickNeverTranslateSite(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: true } + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: true } + ); + + await FullPageTranslationsTestUtils.clickNeverTranslateSite(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: false } + ); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_auto_translate_error_view.js b/browser/components/translations/tests/browser/browser_translations_panel_auto_translate_error_view.js new file mode 100644 index 0000000000..e71ee1392b --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_auto_translate_error_view.js @@ -0,0 +1,86 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const { PromiseTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/PromiseTestUtils.sys.mjs" +); + +/** + * This tests a specific defect where the error view was not showing properly + * when navigating to an auto-translated page after visiting a page in an unsupported + * language and viewing the panel. + * + * This test case tests the case where the auto translate fails and the panel + * automatically opens the panel to show the error view. + * + * See https://bugzilla.mozilla.org/show_bug.cgi?id=1845611 for more information. + */ +add_task( + async function test_revisit_view_updates_with_auto_translate_failure() { + const { cleanup, resolveDownloads, rejectDownloads, runInPage } = + await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: [ + // Do not include French. + { fromLang: "en", toLang: "es" }, + { fromLang: "es", toLang: "en" }, + ], + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The translations button is visible." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.clickAlwaysTranslateLanguage({ + downloadHandler: resolveDownloads, + }); + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await navigate("Navigate to a page in an unsupported language", { + url: FRENCH_PAGE_URL, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: false }, + "The translations button should be unavailable." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: + FullPageTranslationsTestUtils.assertPanelViewUnsupportedLanguage, + }); + + info("Destroy the engine process so that an error will happen."); + await TranslationsParent.destroyEngineProcess(); + + await navigate("Navigate back to a Spanish page.", { + url: SPANISH_PAGE_URL_DOT_ORG, + downloadHandler: rejectDownloads, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewError, + }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_auto_translate_revisit_view.js b/browser/components/translations/tests/browser/browser_translations_panel_auto_translate_revisit_view.js new file mode 100644 index 0000000000..dd4ffcecfd --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_auto_translate_revisit_view.js @@ -0,0 +1,86 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * This tests a specific defect where the revisit view was not showing properly + * when navigating to an auto-translated page after visiting a page in an unsupported + * language and viewing the panel. + * + * This test case tests the case where the auto translate succeeds and the user + * manually opens the panel to show the revisit view. + * + * See https://bugzilla.mozilla.org/show_bug.cgi?id=1845611 for more information. + */ +add_task( + async function test_revisit_view_updates_with_auto_translate_success() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: [ + // Do not include French. + { fromLang: "en", toLang: "es" }, + { fromLang: "es", toLang: "en" }, + ], + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The translations button is visible." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.clickAlwaysTranslateLanguage({ + downloadHandler: resolveDownloads, + }); + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await navigate("Navigate to a page in an unsupported language", { + url: FRENCH_PAGE_URL, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: false }, + "The translations button should be unavailable." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: + FullPageTranslationsTestUtils.assertPanelViewUnsupportedLanguage, + }); + + await navigate("Navigate back to the Spanish page.", { + url: SPANISH_PAGE_URL_DOT_ORG, + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_basics.js b/browser/components/translations/tests/browser/browser_translations_panel_basics.js new file mode 100644 index 0000000000..ef2e2c4708 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_basics.js @@ -0,0 +1,69 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests a basic panel open, translation, and restoration to the original language. + */ +add_task(async function test_translations_panel_basics() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + const { button } = + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + is(button.getAttribute("data-l10n-id"), "urlbar-translations-button2"); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + const panel = document.getElementById("translations-panel"); + const label = document.getElementById(panel.getAttribute("aria-labelledby")); + ok(label, "The a11y label for the panel can be found."); + assertVisibility({ visible: { label } }); + + await FullPageTranslationsTestUtils.clickTranslateButton(); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: true, locale: false, icon: true }, + "The icon presents the loading indicator." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewLoading, + }); + + await FullPageTranslationsTestUtils.clickCancelButton(); + + await resolveDownloads(1); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + + await FullPageTranslationsTestUtils.clickRestoreButton(); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is reverted to have an icon." + ); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_button.js b/browser/components/translations/tests/browser/browser_translations_panel_button.js new file mode 100644 index 0000000000..209cfc18a6 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_button.js @@ -0,0 +1,77 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Test that the translations button is correctly visible when navigating between pages. + */ +add_task(async function test_button_visible_navigation() { + const { cleanup } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The button should be visible since the page can be translated from Spanish." + ); + + await navigate("Navigate to an English page.", { url: ENGLISH_PAGE_URL }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: false }, + "The button should be invisible since the page is in English." + ); + + await navigate("Navigate back to a Spanish page.", { url: SPANISH_PAGE_URL }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The button should be visible again since the page is in Spanish." + ); + + await cleanup(); +}); + +/** + * Test that the translations button is correctly visible when opening and switch tabs. + */ +add_task(async function test_button_visible() { + const { cleanup, tab: spanishTab } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The button should be visible since the page can be translated from Spanish." + ); + + const { removeTab, tab: englishTab } = await addTab( + ENGLISH_PAGE_URL, + "Creating a new tab for a page in English." + ); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: false }, + "The button should be invisible since the tab is in English." + ); + + await switchTab(spanishTab, "spanish tab"); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The button should be visible again since the page is in Spanish." + ); + + await switchTab(englishTab, "english tab"); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: false }, + "Don't show for english pages" + ); + + await removeTab(); + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_cancel.js b/browser/components/translations/tests/browser/browser_translations_panel_cancel.js new file mode 100644 index 0000000000..17e680dd87 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_cancel.js @@ -0,0 +1,27 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests a panel open, and hitting the cancel button. + */ +add_task(async function test_translations_panel_cancel() { + const { cleanup } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.clickCancelButton(); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_close_panel_never_translate_language_with_translations_active.js b/browser/components/translations/tests/browser/browser_translations_panel_close_panel_never_translate_language_with_translations_active.js new file mode 100644 index 0000000000..0fbffd891a --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_close_panel_never_translate_language_with_translations_active.js @@ -0,0 +1,147 @@ +"use strict"; + +/** + * Tests the effect of checking the never-translate-language menuitem on a page where + * translations are active (always-translate-language is enabled). + * Checking the box on the page automatically closes/hides the translations panel. + */ +add_task( + async function test_panel_closes_on_toggle_never_translate_language_with_translations_active() { + const { cleanup, runInPage, resolveDownloads } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The translations button is available" + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.clickAlwaysTranslateLanguage({ + downloadHandler: resolveDownloads, + }); + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage, + "The page should be automatically translated." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.waitForTranslationsPopupEvent( + "popuphidden", + async () => { + await FullPageTranslationsTestUtils.clickNeverTranslateLanguage(); + } + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + await cleanup(); + } +); + +/** + * Tests the effect of checking the never-translate-language menuitem on + * a page where translations are active through always-translate-language + * and inactive on a site through never-translate-site. + * Checking the box on the page automatically closes/hides the translations panel. + */ +add_task( + async function test_panel_closes_on_toggle_never_translate_language_with_always_translate_language_and_never_translate_site_active() { + const { cleanup, runInPage, resolveDownloads } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The translations button is available" + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.clickAlwaysTranslateLanguage({ + downloadHandler: resolveDownloads, + }); + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage, + "The page should be automatically translated." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: false } + ); + await FullPageTranslationsTestUtils.clickNeverTranslateSite(); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: true } + ); + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: true } + ); + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.assertIsNeverTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.waitForTranslationsPopupEvent( + "popuphidden", + async () => { + await FullPageTranslationsTestUtils.clickNeverTranslateLanguage(); + } + ); + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_close_panel_never_translate_language_with_translations_inactive.js b/browser/components/translations/tests/browser/browser_translations_panel_close_panel_never_translate_language_with_translations_inactive.js new file mode 100644 index 0000000000..5df2468646 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_close_panel_never_translate_language_with_translations_inactive.js @@ -0,0 +1,93 @@ +"use strict"; + +/** + * Tests the effect of checking the never-translate-language menuitem on a page where + * translations and never translate site are inactive. + * Checking the box on the page automatically closes/hides the translations panel. + */ +add_task(async function test_panel_closes_on_toggle_never_translate_language() { + const { cleanup } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The translations button is available" + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsNeverTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.waitForTranslationsPopupEvent( + "popuphidden", + async () => { + await FullPageTranslationsTestUtils.clickNeverTranslateLanguage(); + } + ); + await cleanup(); +}); + +/** + * Tests the effect of checking the never-translate-language menuitem on a page where + * translations are inactive (never-translate-site is enabled). + * Checking the box on the page automatically closes/hides the translations panel. + */ +add_task( + async function test_panel_closes_on_toggle_never_translate_language_with_never_translate_site_enabled() { + const { cleanup } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The translations button is available" + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: false } + ); + await FullPageTranslationsTestUtils.clickNeverTranslateSite(); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: true } + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: true } + ); + + await FullPageTranslationsTestUtils.assertIsNeverTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.waitForTranslationsPopupEvent( + "popuphidden", + async () => { + await FullPageTranslationsTestUtils.clickNeverTranslateLanguage(); + } + ); + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_close_panel_never_translate_site.js b/browser/components/translations/tests/browser/browser_translations_panel_close_panel_never_translate_site.js new file mode 100644 index 0000000000..78679a046a --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_close_panel_never_translate_site.js @@ -0,0 +1,159 @@ +"use strict"; + +/** + * Tests the effect of checking the never-translate-site menuitem on a page where + * always-translate-language and never-translate-language are inactive. + * Checking the box on the page automatically closes/hides the translations panel. + */ +add_task(async function test_panel_closes_on_toggle_never_translate_site() { + const { cleanup } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The translations button is available" + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: false } + ); + await FullPageTranslationsTestUtils.waitForTranslationsPopupEvent( + "popuphidden", + async () => { + await FullPageTranslationsTestUtils.clickNeverTranslateSite(); + } + ); + + await cleanup(); +}); + +/** + * Tests the effect of checking the never-translate-site menuitem on a page where + * translations are active (always-translate-language is enabled). + * Checking the box on the page automatically restores the page and closes/hides the translations panel. + */ +add_task( + async function test_panel_closes_on_toggle_never_translate_site_with_translations_active() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The translations button is available" + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.clickAlwaysTranslateLanguage({ + downloadHandler: resolveDownloads, + }); + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage, + "The page should be automatically translated." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: false } + ); + + await FullPageTranslationsTestUtils.waitForTranslationsPopupEvent( + "popuphidden", + async () => { + await FullPageTranslationsTestUtils.clickNeverTranslateSite(); + } + ); + + await cleanup(); + } +); + +/** + * Tests the effect of checking the never-translate-site menuitem on a page where + * translations are inactive (never-translate-language is active). + * Checking the box on the page automatically closes/hides the translations panel. + */ +add_task( + async function test_panel_closes_on_toggle_never_translate_site_with_translations_inactive() { + const { cleanup } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The translations button is available" + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsNeverTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.clickNeverTranslateLanguage(); + await FullPageTranslationsTestUtils.assertIsNeverTranslateLanguage("es", { + checked: true, + }); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsNeverTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: false } + ); + await FullPageTranslationsTestUtils.waitForTranslationsPopupEvent( + "popuphidden", + async () => { + await FullPageTranslationsTestUtils.clickNeverTranslateSite(); + } + ); + + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_engine_destroy.js b/browser/components/translations/tests/browser/browser_translations_panel_engine_destroy.js new file mode 100644 index 0000000000..0a58dd7fa6 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_engine_destroy.js @@ -0,0 +1,55 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Manually destroy the engine, and test that the page is still translated afterwards. + */ +add_task(async function test_translations_engine_destroy() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The button is available." + ); + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.clickTranslateButton({ + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + info("Destroy the engine process"); + await TranslationsParent.destroyEngineProcess(); + + info("Mutate the page's content to re-trigger a translation."); + await runInPage(async TranslationsTest => { + const { getH1 } = TranslationsTest.getSelectors(); + getH1().innerText = "New text for the H1"; + }); + + info("The engine downloads should be requested again."); + resolveDownloads(1); + + await runInPage(async TranslationsTest => { + const { getH1 } = TranslationsTest.getSelectors(); + await TranslationsTest.assertTranslationResult( + "The mutated content should be translated.", + getH1, + "NEW TEXT FOR THE H1 [es to en]" + ); + }); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_engine_destroy_pending.js b/browser/components/translations/tests/browser/browser_translations_panel_engine_destroy_pending.js new file mode 100644 index 0000000000..ace1a845df --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_engine_destroy_pending.js @@ -0,0 +1,72 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Manually destroy the engine while a page is in the background, and test that the page + * is still translated after switching back to it. + */ +add_task(async function test_translations_engine_destroy_pending() { + const { + cleanup, + resolveDownloads, + runInPage, + tab: spanishTab, + } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The button is available." + ); + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.clickTranslateButton({ + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + const { removeTab: removeEnglishTab } = await addTab( + ENGLISH_PAGE_URL, + "Creating a new tab for a page in English." + ); + + info("Destroy the engine process"); + await TranslationsParent.destroyEngineProcess(); + + info("Mutate the page's content to re-trigger a translation."); + await runInPage(async TranslationsTest => { + const { getH1 } = TranslationsTest.getSelectors(); + getH1().innerText = "New text for the H1"; + }); + + info("Wait for a second to ensure the mutation takes."); + await TestUtils.waitForTick(); + + await switchTab(spanishTab, "spanish tab"); + + info("The engine downloads should be requested again."); + resolveDownloads(1); + + await runInPage(async TranslationsTest => { + const { getH1 } = TranslationsTest.getSelectors(); + await TranslationsTest.assertTranslationResult( + "The mutated content should be translated.", + getH1, + "NEW TEXT FOR THE H1 [es to en]" + ); + }); + + await removeEnglishTab(); + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_engine_unsupported.js b/browser/components/translations/tests/browser/browser_translations_panel_engine_unsupported.js new file mode 100644 index 0000000000..f0804f35aa --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_engine_unsupported.js @@ -0,0 +1,59 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests that the translations button does not appear when the translations + * engine is not supported. + */ +add_task(async function test_translations_button_hidden_when_cpu_unsupported() { + const { cleanup, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.simulateUnsupportedEngine", true]], + }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: false }, + "The button is not available." + ); + + await cleanup(); +}); + +/** + * Tests that the translate-page menuitem is not available in the app menu + * when the translations engine is not supported. + */ +add_task( + async function test_translate_page_app_menu_item_hidden_when_cpu_unsupported() { + const { cleanup, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.simulateUnsupportedEngine", true]], + }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + const appMenuButton = getById("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.hidden, + true, + "The app-menu translate button should be hidden because when the engine is not supported." + ); + + click(appMenuButton, "Closing the app menu"); + + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_engine_unsupported_lang.js b/browser/components/translations/tests/browser/browser_translations_panel_engine_unsupported_lang.js new file mode 100644 index 0000000000..79e5c6b119 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_engine_unsupported_lang.js @@ -0,0 +1,28 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests how the unsupported language flow works. + */ +add_task(async function test_unsupported_lang() { + const { cleanup } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: [ + // Do not include Spanish. + { fromLang: "fr", toLang: "en" }, + { fromLang: "en", toLang: "fr" }, + ], + }); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: + FullPageTranslationsTestUtils.assertPanelViewUnsupportedLanguage, + }); + + await FullPageTranslationsTestUtils.clickChangeSourceLanguageButton(); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_firstrun.js b/browser/components/translations/tests/browser/browser_translations_panel_firstrun.js new file mode 100644 index 0000000000..0c248a7837 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_firstrun.js @@ -0,0 +1,33 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests that the first run message is displayed. + */ +add_task(async function test_translations_panel_firstrun() { + const { cleanup } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.panelShown", false]], + }); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewFirstShow, + }); + + await FullPageTranslationsTestUtils.clickCancelButton(); + + await navigate("Load a different page on the same site", { + url: SPANISH_PAGE_URL_2, + }); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.clickCancelButton(); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_firstrun_revisit.js b/browser/components/translations/tests/browser/browser_translations_panel_firstrun_revisit.js new file mode 100644 index 0000000000..02c2d94db9 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_firstrun_revisit.js @@ -0,0 +1,57 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests that the first-show intro message message is displayed + * when viewing the panel subsequent times on the same URL, + * but is no longer displayed after navigating to new URL, + * or when returning to the first URL after navigating away. + */ +add_task(async function test_translations_panel_firstrun() { + const { cleanup } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.panelShown", false]], + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewFirstShow, + }); + + await FullPageTranslationsTestUtils.clickCancelButton(); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewFirstShow, + }); + + await FullPageTranslationsTestUtils.clickCancelButton(); + + await navigate("Navigate to a different website", { + url: SPANISH_PAGE_URL_DOT_ORG, + }); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.clickCancelButton(); + + await navigate("Navigate back to the first website", { + url: SPANISH_PAGE_URL, + }); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.clickCancelButton(); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_fuzzing.js b/browser/components/translations/tests/browser/browser_translations_panel_fuzzing.js new file mode 100644 index 0000000000..2f5285ac46 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_fuzzing.js @@ -0,0 +1,240 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Manually destroy the engine while a page is in the background, and test that the page + * is still translated after switching back to it. + */ +add_task(async function test_translations_panel_fuzzing() { + const { + cleanup, + runInPage: runInSpanishPage, + tab: spanishTab, + } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + autoDownloadFromRemoteSettings: true, + }); + + /** + * @typedef {object} Tab + */ + + /** @type {Tab?} */ + let englishTab; + /** @type {Function?} */ + let removeEnglishTab; + /** @type {boolean} */ + let isSpanishPageTranslated = false; + /** @type {"spanish" | "english"} */ + let activeTab = "spanish"; + /** @type {boolean} */ + let isEngineMaybeDestroyed = true; + /** @type {boolean} */ + let isTitleMutated = false; + /** @type {boolean} */ + let hasVerifiedMutation = true; + + function reportOperation(name) { + info( + `\n\nOperation: ${name} ` + + JSON.stringify({ + activeTab, + englishTab: !!englishTab, + isSpanishPageTranslated, + isEngineMaybeDestroyed, + isTitleMutated, + }) + ); + } + + /** + * A list of fuzzing operations. They return false when they are a noop given the + * conditions. + * + * @type {object} - Record Promise> + */ + const operations = { + async addEnglishTab() { + if (!englishTab) { + reportOperation("addEnglishTab"); + const { removeTab, tab } = await addTab( + ENGLISH_PAGE_URL, + "Creating a new tab for a page in English." + ); + + englishTab = tab; + removeEnglishTab = removeTab; + activeTab = "english"; + return true; + } + return false; + }, + + async removeEnglishTab() { + if (removeEnglishTab) { + reportOperation("removeEnglishTab"); + await removeEnglishTab(); + + englishTab = null; + removeEnglishTab = null; + activeTab = "spanish"; + return true; + } + return false; + }, + + async translateSpanishPage() { + if (!isSpanishPageTranslated) { + reportOperation("translateSpanishPage"); + if (activeTab === "english") { + await switchTab(spanishTab, "spanish tab"); + } + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The button is available." + ); + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.clickTranslateButton(); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: true, icon: true }, + "Translations button is fully loaded." + ); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInSpanishPage + ); + + isSpanishPageTranslated = true; + isEngineMaybeDestroyed = false; + activeTab = "spanish"; + return true; + } + return false; + }, + + async destroyEngineProcess() { + if ( + !isEngineMaybeDestroyed && + // Don't destroy the engine process until the mutation has been verified. + // There is an artifical race (e.g. only in tests) that happens from a new + // engine being requested, and forcefully destroyed before the it can be + // initialized. + hasVerifiedMutation + ) { + reportOperation("destroyEngineProcess"); + await TranslationsParent.destroyEngineProcess(); + isEngineMaybeDestroyed = true; + } + return true; + }, + + async mutateSpanishPage() { + if (isSpanishPageTranslated && !isTitleMutated) { + reportOperation("mutateSpanishPage"); + + info("Mutate the page's content to re-trigger a translation."); + await runInSpanishPage(async TranslationsTest => { + const { getH1 } = TranslationsTest.getSelectors(); + getH1().innerText = "New text for the H1"; + }); + + if (isEngineMaybeDestroyed) { + info("The engine may be recreated now."); + } + + isEngineMaybeDestroyed = false; + isTitleMutated = true; + hasVerifiedMutation = false; + return true; + } + return false; + }, + + async switchToSpanishTab() { + if (activeTab !== "spanish") { + reportOperation("switchToSpanishTab"); + await switchTab(spanishTab, "spanish tab"); + activeTab = "spanish"; + + if (isTitleMutated) { + await runInSpanishPage(async TranslationsTest => { + const { getH1 } = TranslationsTest.getSelectors(); + await TranslationsTest.assertTranslationResult( + "The mutated content should be translated.", + getH1, + "NEW TEXT FOR THE H1 [es to en]" + ); + }); + hasVerifiedMutation = true; + } + + return true; + } + return false; + }, + + async switchToEnglishTab() { + if (activeTab !== "english" && englishTab) { + reportOperation("switchToEnglishTab"); + await switchTab(englishTab, "english tab"); + activeTab = "english"; + return true; + } + return false; + }, + + async restoreSpanishPage() { + if (activeTab === "spanish" && isSpanishPageTranslated) { + reportOperation("restoreSpanishPage"); + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + + await FullPageTranslationsTestUtils.clickRestoreButton(); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated( + runInSpanishPage + ); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is reverted to have an icon." + ); + + isSpanishPageTranslated = false; + isTitleMutated = false; + return true; + } + return false; + }, + }; + + const fuzzSteps = 100; + info(`Starting the fuzzing with ${fuzzSteps} operations.`); + const opsArray = Object.values(operations); + + for (let i = 0; i < fuzzSteps; i++) { + // Pick a random operation and check if that it was not a noop, otherwise continue + // trying to find a valid operation. + while (true) { + const operation = opsArray[Math.floor(Math.random() * opsArray.length)]; + if (await operation()) { + break; + } + } + } + + if (removeEnglishTab) { + await removeEnglishTab(); + } + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_gear.js b/browser/components/translations/tests/browser/browser_translations_panel_gear.js new file mode 100644 index 0000000000..c24fc61e3d --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_gear.js @@ -0,0 +1,32 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Test managing the languages menu item. + */ +add_task(async function test_translations_panel_manage_languages() { + const { cleanup } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.clickManageLanguages(); + + await waitForCondition( + () => gBrowser.currentURI.spec === "about:preferences#general", + "Waiting for about:preferences to be opened." + ); + + info("Remove the about:preferences tab"); + gBrowser.removeCurrentTab(); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_never_translate_language.js b/browser/components/translations/tests/browser/browser_translations_panel_never_translate_language.js new file mode 100644 index 0000000000..6c1fea7754 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_never_translate_language.js @@ -0,0 +1,186 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests the effect of toggling the never-translate-language menuitem. + * Checking the box on an untranslated page should immediately hide the button. + * The button should not appear again for sites in the disabled language. + */ +add_task(async function test_toggle_never_translate_language_menuitem() { + const { cleanup, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The translations button is visible." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsNeverTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.clickNeverTranslateLanguage(); + await FullPageTranslationsTestUtils.assertIsNeverTranslateLanguage("es", { + checked: true, + }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate("Reload the page", { url: SPANISH_PAGE_URL }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate("Navigate to a different Spanish page", { + url: SPANISH_PAGE_URL_DOT_ORG, + }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await cleanup(); +}); + +/** + * Tests the effect of toggling the never-translate-language menuitem on a page where + * where translation is already active. + * Checking the box on a translated page should restore the page and hide the button. + * The button should not appear again for sites in the disabled language. + */ +add_task( + async function test_toggle_never_translate_language_menuitem_with_active_translations() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.clickTranslateButton({ + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsNeverTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.clickNeverTranslateLanguage(); + await FullPageTranslationsTestUtils.assertIsNeverTranslateLanguage("es", { + checked: true, + }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate("Reload the page", { url: SPANISH_PAGE_URL }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await cleanup(); + } +); + +/** + * Tests the effect of toggling the never-translate-language menuitem on a page where + * where translation is already active via always-translate. + * Checking the box on a translated page should restore the page and hide the button. + * The language should be moved from always-translate to never-translate. + * The button should not appear again for sites in the disabled language. + */ +add_task( + async function test_toggle_never_translate_language_menuitem_with_always_translate_active() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateLanguage("es", { + checked: false, + }); + + await FullPageTranslationsTestUtils.clickAlwaysTranslateLanguage({ + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateLanguage("es", { + checked: false, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateLanguage("es", { + checked: false, + }); + + await FullPageTranslationsTestUtils.clickNeverTranslateLanguage(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateLanguage("es", { + checked: true, + }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate("Reload the page", { url: SPANISH_PAGE_URL }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_never_translate_site.js b/browser/components/translations/tests/browser/browser_translations_panel_never_translate_site.js new file mode 100644 index 0000000000..858aa297df --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_never_translate_site.js @@ -0,0 +1,247 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests the effect of toggling the never-translate-site menuitem. + * Checking the box on an untranslated page should immediately hide the button. + * The button should not appear again for sites that share the same content principal + * of the disabled site. + */ +add_task(async function test_toggle_never_translate_site_menuitem() { + const { cleanup, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The translations button is visible." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: false } + ); + await FullPageTranslationsTestUtils.clickNeverTranslateSite(); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: true } + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate("Navigate to a Spanish page with the same content principal", { + url: SPANISH_PAGE_URL_2, + }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate( + "Navigate to a Spanish page with a different content principal", + { url: SPANISH_PAGE_URL_DOT_ORG } + ); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The translations button should be visible, because this content principal " + + "has not been denied translations permissions" + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await cleanup(); +}); + +/** + * Tests the effect of toggling the never-translate-site menuitem on a page where + * where translation is already active. + * Checking the box on a translated page should restore the page and hide the button. + * The button should not appear again for sites that share the same content principal + * of the disabled site. + */ +add_task( + async function test_toggle_never_translate_site_menuitem_with_active_translations() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The translations button is visible." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.clickTranslateButton({ + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: false } + ); + await FullPageTranslationsTestUtils.clickNeverTranslateSite(); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: true } + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate("Reload the page", { url: SPANISH_PAGE_URL }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate( + "Navigate to a Spanish page with the same content principal", + { url: SPANISH_PAGE_URL_2 } + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate( + "Navigate to a Spanish page with a different content principal", + { url: SPANISH_PAGE_URL_DOT_ORG } + ); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The translations button should be visible, because this content principal " + + "has not been denied translations permissions" + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await cleanup(); + } +); + +/** + * Tests the effect of toggling the never-translate-site menuitem on a page where + * where translation is already active via always-translate. + * Checking the box on a translated page should restore the page and hide the button. + * The button should not appear again for sites that share the same content principal + * of the disabled site, and no auto-translation should occur. + * Other sites should still auto-translate for this language. + */ +add_task( + async function test_toggle_never_translate_site_menuitem_with_always_translate_active() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: false } + ); + + await FullPageTranslationsTestUtils.clickAlwaysTranslateLanguage({ + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: false } + ); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: false } + ); + + await FullPageTranslationsTestUtils.clickNeverTranslateSite(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: true } + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate("Reload the page", { url: SPANISH_PAGE_URL }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate( + "Navigate to a Spanish page with the same content principal", + { url: SPANISH_PAGE_URL_2 } + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate( + "Navigate to a Spanish page with a different content principal", + { + url: SPANISH_PAGE_URL_DOT_ORG, + downloadHandler: resolveDownloads, + } + ); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_never_translate_site_auto.js b/browser/components/translations/tests/browser/browser_translations_panel_never_translate_site_auto.js new file mode 100644 index 0000000000..28f3b570c3 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_never_translate_site_auto.js @@ -0,0 +1,109 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests the effect of toggling the never-translate-site menuitem on a page where + * where translation is already active via always-translate. + * Checking the box on a translated page should restore the page and hide the button. + * The button should not appear again for sites that share the same content principal + * of the disabled site, and no auto-translation should occur. + * Other sites should still auto-translate for this language. + */ +add_task( + async function test_toggle_never_translate_site_menuitem_with_always_translate_active() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: false } + ); + + await FullPageTranslationsTestUtils.clickAlwaysTranslateLanguage({ + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: false } + ); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: false } + ); + + await FullPageTranslationsTestUtils.clickNeverTranslateSite(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: true } + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate("Reload the page", { url: SPANISH_PAGE_URL }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate( + "Navigate to a Spanish page with the same content principal", + { url: SPANISH_PAGE_URL_2 } + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate( + "Navigate to a Spanish page with a different content principal", + { + url: SPANISH_PAGE_URL_DOT_ORG, + downloadHandler: resolveDownloads, + } + ); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_never_translate_site_basic.js b/browser/components/translations/tests/browser/browser_translations_panel_never_translate_site_basic.js new file mode 100644 index 0000000000..098ff54d6e --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_never_translate_site_basic.js @@ -0,0 +1,62 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests the effect of toggling the never-translate-site menuitem. + * Checking the box on an untranslated page should immediately hide the button. + * The button should not appear again for sites that share the same content principal + * of the disabled site. + */ +add_task(async function test_toggle_never_translate_site_menuitem() { + const { cleanup, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The translations button is visible." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: false } + ); + await FullPageTranslationsTestUtils.clickNeverTranslateSite(); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: true } + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate("Navigate to a Spanish page with the same content principal", { + url: SPANISH_PAGE_URL_2, + }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate( + "Navigate to a Spanish page with a different content principal", + { url: SPANISH_PAGE_URL_DOT_ORG } + ); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The translations button should be visible, because this content principal " + + "has not been denied translations permissions" + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_never_translate_site_manual.js b/browser/components/translations/tests/browser/browser_translations_panel_never_translate_site_manual.js new file mode 100644 index 0000000000..d916342c33 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_never_translate_site_manual.js @@ -0,0 +1,84 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests the effect of toggling the never-translate-site menuitem on a page where + * where translation is already active. + * Checking the box on a translated page should restore the page and hide the button. + * The button should not appear again for sites that share the same content principal + * of the disabled site. + */ +add_task( + async function test_toggle_never_translate_site_menuitem_with_active_translations() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The translations button is visible." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.clickTranslateButton({ + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: false } + ); + await FullPageTranslationsTestUtils.clickNeverTranslateSite(); + await FullPageTranslationsTestUtils.assertIsNeverTranslateSite( + SPANISH_PAGE_URL, + { checked: true } + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate("Reload the page", { url: SPANISH_PAGE_URL }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate( + "Navigate to a Spanish page with the same content principal", + { url: SPANISH_PAGE_URL_2 } + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await navigate( + "Navigate to a Spanish page with a different content principal", + { url: SPANISH_PAGE_URL_DOT_ORG } + ); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The translations button should be visible, because this content principal " + + "has not been denied translations permissions" + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_retry.js b/browser/components/translations/tests/browser/browser_translations_panel_retry.js new file mode 100644 index 0000000000..76a6bd9429 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_retry.js @@ -0,0 +1,54 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests translating, and then immediately translating to a new language. + */ +add_task(async function test_translations_panel_retry() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.clickTranslateButton({ + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + + FullPageTranslationsTestUtils.switchSelectedToLanguage("fr"); + + await FullPageTranslationsTestUtils.clickTranslateButton({ + downloadHandler: resolveDownloads, + pivotTranslation: true, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "fr", + runInPage + ); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_settings_unsupported_lang.js b/browser/components/translations/tests/browser/browser_translations_panel_settings_unsupported_lang.js new file mode 100644 index 0000000000..18fcf484dd --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_settings_unsupported_lang.js @@ -0,0 +1,74 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * This tests a specific defect where the language checkbox states were not being + * updated correctly when visiting a web page in an unsupported language after + * previously enabling always-translate-language or never-translate-language + * on a site with a supported language. + * + * See https://bugzilla.mozilla.org/show_bug.cgi?id=1845611 for more information. + */ +add_task(async function test_unsupported_language_settings_menu_checkboxes() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: [ + // Do not include French. + { fromLang: "en", toLang: "es" }, + { fromLang: "es", toLang: "en" }, + ], + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The translations button is visible." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: false, + }); + await FullPageTranslationsTestUtils.clickAlwaysTranslateLanguage({ + downloadHandler: resolveDownloads, + }); + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("es", { + checked: true, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await navigate("Navigate to a page in an unsupported language.", { + url: FRENCH_PAGE_URL, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: false }, + "The translations button should be unavailable." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: + FullPageTranslationsTestUtils.assertPanelViewUnsupportedLanguage, + }); + await FullPageTranslationsTestUtils.assertIsAlwaysTranslateLanguage("fr", { + checked: false, + disabled: true, + }); + await FullPageTranslationsTestUtils.assertIsNeverTranslateLanguage("fr", { + checked: false, + disabled: true, + }); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_panel_switch_languages.js b/browser/components/translations/tests/browser/browser_translations_panel_switch_languages.js new file mode 100644 index 0000000000..3652c61d83 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_panel_switch_languages.js @@ -0,0 +1,70 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests switching the language. + */ +add_task(async function test_translations_panel_switch_language() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + const { translateButton } = TranslationsPanel.elements; + + ok(!translateButton.disabled, "The translate button starts as enabled"); + + FullPageTranslationsTestUtils.assertSelectedFromLanguage("es"); + FullPageTranslationsTestUtils.assertSelectedToLanguage("en"); + + FullPageTranslationsTestUtils.switchSelectedFromLanguage("en"); + + ok( + translateButton.disabled, + "The translate button is disabled when the languages are the same" + ); + + FullPageTranslationsTestUtils.switchSelectedFromLanguage("es"); + + ok( + !translateButton.disabled, + "When the languages are different it can be translated" + ); + + FullPageTranslationsTestUtils.switchSelectedFromLanguage(""); + + ok( + translateButton.disabled, + "The translate button is disabled nothing is selected." + ); + + FullPageTranslationsTestUtils.switchSelectedFromLanguage("en"); + FullPageTranslationsTestUtils.switchSelectedToLanguage("fr"); + + ok(!translateButton.disabled, "The translate button can now be used"); + + await FullPageTranslationsTestUtils.clickTranslateButton({ + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "en", + "fr", + runInPage + ); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_reader_mode.js b/browser/components/translations/tests/browser/browser_translations_reader_mode.js new file mode 100644 index 0000000000..d066257998 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_reader_mode.js @@ -0,0 +1,115 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests that the translations button becomes hidden when entering reader mode. + */ +add_task(async function test_translations_button_hidden_in_reader_mode() { + const { cleanup, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The translations button is visible." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await toggleReaderMode(); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: false }, + "The translations button is now hidden in reader mode." + ); + + await runInPage(async TranslationsTest => { + const { getH1 } = TranslationsTest.getSelectors(); + await TranslationsTest.assertTranslationResult( + "The page's H1 is now the reader-mode header", + getH1, + "Translations Test" + ); + }); + + await runInPage(async TranslationsTest => { + const { getLastParagraph } = TranslationsTest.getSelectors(); + await TranslationsTest.assertTranslationResult( + "The page's last paragraph is in Spanish.", + getLastParagraph, + "— Pues, aunque mováis más brazos que los del gigante Briareo, me lo habéis de pagar." + ); + }); + + await toggleReaderMode(); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The translations button is visible again outside of reader mode." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await cleanup(); +}); + +/** + * Tests that translations persist when entering reader mode after translating. + */ +add_task(async function test_translations_persist_in_reader_mode() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The translations button is visible." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.clickTranslateButton({ + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await toggleReaderMode(); + + await runInPage(async TranslationsTest => { + const { getH1 } = TranslationsTest.getSelectors(); + await TranslationsTest.assertTranslationResult( + "The page's H1 is now the translated reader-mode header", + getH1, + "TRANSLATIONS TEST [es to en, html]" + ); + }); + + await runInPage(async TranslationsTest => { + const { getLastParagraph } = TranslationsTest.getSelectors(); + await TranslationsTest.assertTranslationResult( + "The page's last paragraph is in Spanish.", + getLastParagraph, + "— PUES, AUNQUE MOVÁIS MÁS BRAZOS QUE LOS DEL GIGANTE BRIAREO, ME LO HABÉIS DE PAGAR. [es to en, html]" + ); + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: false }, + "The translations button is now hidden in reader mode." + ); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_select_context_menu_feature_disabled.js b/browser/components/translations/tests/browser/browser_translations_select_context_menu_feature_disabled.js new file mode 100644 index 0000000000..a6b3f71924 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_select_context_menu_feature_disabled.js @@ -0,0 +1,114 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +// This file ensures that the translate selection menu item is unavailable when the translation feature is disabled, +// This file will be removed when the feature is released, as the pref will no longer exist. +// +// https://bugzilla.mozilla.org/show_bug.cgi?id=1870366 +// +// However, for the time being, I like having these tests to ensure there is no regression when the pref +// is set to false. + +/** + * This test checks the availability of the translate-selection menu item in the context menu, + * ensuring it is not visible when the "browser.translations.select.enable" preference is set to false + * and no text is selected when the context menu is invoked. + */ +add_task( + async function test_translate_selection_menuitem_is_unavailable_with_feature_disabled_and_no_text_selected() { + const { cleanup, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.select.enable", false]], + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await SelectTranslationsTestUtils.assertContextMenuTranslateSelectionItem( + runInPage, + { + selectSpanishParagraph: false, + openAtSpanishParagraph: true, + expectMenuItemVisible: false, + }, + "The translate-selection context menu item should be unavailable when the feature is disabled." + ); + + await cleanup(); + } +); + +/** + * This test case verifies the functionality of the translate-selection context menu item + * when the selected text is not in the user's preferred language. The menu item should be + * localized to translate to the target language matching the user's top preferred language + * when the selected text is detected to be in a different language. + */ +add_task( + async function test_translate_selection_menuitem_is_unavailable_with_feature_disabled_and_text_selected() { + const { cleanup, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.select.enable", false]], + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await SelectTranslationsTestUtils.assertContextMenuTranslateSelectionItem( + runInPage, + { + selectSpanishParagraph: true, + openAtSpanishParagraph: true, + expectMenuItemVisible: false, + }, + "The translate-selection context menu item should be unavailable when the feature is disabled." + ); + + await cleanup(); + } +); + +/** + * This test checks the availability of the translate-selection menu item in the context menu, + * ensuring it is not visible when the "browser.translations.select.enable" preference is set to false + * and the context menu is invoked on a hyperlink. This would result in the menu item being available + * if the pref were set to true. + */ +add_task( + async function test_translate_selection_menuitem_is_unavailable_with_feature_disabled_and_clicking_a_hyperlink() { + const { cleanup, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.select.enable", false]], + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + await SelectTranslationsTestUtils.assertContextMenuTranslateSelectionItem( + runInPage, + { + selectSpanishParagraph: false, + openAtSpanishHyperlink: true, + expectMenuItemVisible: false, + }, + "The translate-selection context menu item should be unavailable when the feature is disabled." + ); + + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_select_context_menu_with_full_page_translations_active.js b/browser/components/translations/tests/browser/browser_translations_select_context_menu_with_full_page_translations_active.js new file mode 100644 index 0000000000..58cb655e38 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_select_context_menu_with_full_page_translations_active.js @@ -0,0 +1,162 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * This test case checks the behavior of the translate-selection menu item in the context menu + * when full-page translations is active or inactive. The menu item should be available under + * the correct selected-text conditions while full-page translations is inactive, and it should + * never be available while full-page translations is active. + */ +add_task( + async function test_translate_selection_menuitem_with_text_selected_and_full_page_translations_active() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.select.enable", true]], + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await SelectTranslationsTestUtils.assertContextMenuTranslateSelectionItem( + runInPage, + { + selectSpanishParagraph: true, + openAtSpanishParagraph: true, + expectMenuItemVisible: true, + expectedTargetLanguage: "en", + }, + "The translate-selection context menu item should be available while full-page translations is inactive." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.clickTranslateButton({ + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await SelectTranslationsTestUtils.assertContextMenuTranslateSelectionItem( + runInPage, + { + selectSpanishParagraph: true, + openAtSpanishParagraph: true, + expectMenuItemVisible: false, + }, + "The translate-selection context menu item should be unavailable while full-page translations is active." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + + await FullPageTranslationsTestUtils.clickRestoreButton(); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await SelectTranslationsTestUtils.assertContextMenuTranslateSelectionItem( + runInPage, + { + selectSpanishParagraph: true, + openAtSpanishParagraph: true, + expectMenuItemVisible: true, + expectedTargetLanguage: "en", + }, + "The translate-selection context menu item should be available while full-page translations is inactive." + ); + + await cleanup(); + } +); + +/** + * This test case checks the behavior of the translate-selection menu item in the context menu + * when full-page translations is active or inactive. The menu item should be available under + * the correct link-clicked conditions while full-page translations is inactive, and it should + * never be available while full-page translations is active. + */ +add_task( + async function test_translate_selection_menuitem_with_link_clicked_and_full_page_translations_active() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.select.enable", true]], + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await SelectTranslationsTestUtils.assertContextMenuTranslateSelectionItem( + runInPage, + { + selectSpanishParagraph: false, + openAtSpanishHyperlink: true, + expectMenuItemVisible: true, + expectedTargetLanguage: "en", + }, + "The translate-selection context menu item should be available while full-page translations is inactive." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.clickTranslateButton({ + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await SelectTranslationsTestUtils.assertContextMenuTranslateSelectionItem( + runInPage, + { + selectSpanishParagraph: false, + openAtSpanishHyperlink: true, + expectMenuItemVisible: false, + }, + "The translate-selection context menu item should be unavailable while full-page translations is active." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + + await FullPageTranslationsTestUtils.clickRestoreButton(); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await SelectTranslationsTestUtils.assertContextMenuTranslateSelectionItem( + runInPage, + { + selectSpanishParagraph: false, + openAtSpanishHyperlink: true, + expectMenuItemVisible: true, + expectedTargetLanguage: "en", + }, + "The translate-selection context menu item should be available while full-page translations is inactive." + ); + + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_select_context_menu_with_hyperlink.js b/browser/components/translations/tests/browser/browser_translations_select_context_menu_with_hyperlink.js new file mode 100644 index 0000000000..cefd83f046 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_select_context_menu_with_hyperlink.js @@ -0,0 +1,109 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * This test case verifies the functionality of the translate-selection context menu item + * when a hyperlink is right-clicked. The menu item should offer to translate the link text + * to a target language when the detected language of the link text does not match the preferred + * language. + */ +add_task( + async function test_translate_selection_menuitem_translate_link_text_to_target_language() { + const { cleanup, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.select.enable", true]], + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + await SelectTranslationsTestUtils.assertContextMenuTranslateSelectionItem( + runInPage, + { + selectSpanishParagraph: false, + openAtSpanishHyperlink: true, + expectMenuItemVisible: true, + expectedTargetLanguage: "en", + }, + "The translate-selection context menu item should be localized to translate the link text" + + "to the target language." + ); + + await cleanup(); + } +); + +/** + * This test case verifies the functionality of the translate-selection context menu item + * when a hyperlink is right-clicked, and the link text is in the top preferred language. + * The menu item should offer to translate the link text without specifying a target language, + * since it is already in the preferred language for the user. + */ +add_task( + async function test_translate_selection_menuitem_translate_link_text_in_preferred_language() { + const { cleanup, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.select.enable", true]], + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + await SelectTranslationsTestUtils.assertContextMenuTranslateSelectionItem( + runInPage, + { + selectSpanishParagraph: false, + openAtEnglishHyperlink: true, + expectMenuItemVisible: true, + expectedTargetLanguage: null, + }, + "The translate-selection context menu item should be localized to translate the link text" + + "without a target language." + ); + + await cleanup(); + } +); + +/** + * This test case ensures that the translate-selection context menu item functions correctly + * when text is actively selected but the context menu is invoked on an unselected hyperlink. + * The selected text content should take precedence over the link text, and the menu item should + * be localized to translate the selected text to the target language, rather than the hyperlink text. + */ +add_task( + async function test_translate_selection_menuitem_selected_text_takes_precedence_over_link_text() { + const { cleanup, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.select.enable", true]], + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + await SelectTranslationsTestUtils.assertContextMenuTranslateSelectionItem( + runInPage, + { + selectSpanishParagraph: true, + openAtEnglishHyperlink: true, + expectMenuItemVisible: true, + expectedTargetLanguage: "en", + }, + "The translate-selection context menu item should be localized to translate the selection" + + "even though the hyperlink is the element on which the context menu was invoked." + ); + + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_select_context_menu_with_no_text_selected.js b/browser/components/translations/tests/browser/browser_translations_select_context_menu_with_no_text_selected.js new file mode 100644 index 0000000000..82e5d3ba63 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_select_context_menu_with_no_text_selected.js @@ -0,0 +1,37 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * This test case verifies that the translate-selection context menu item is unavailable + * when no text is selected. + */ +add_task( + async function test_translate_selection_menuitem_is_unavailable_when_no_text_is_selected() { + const { cleanup, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.select.enable", true]], + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await SelectTranslationsTestUtils.assertContextMenuTranslateSelectionItem( + runInPage, + { + selectSpanishParagraph: false, + openAtSpanishParagraph: true, + expectMenuItemVisible: false, + }, + "The translate-selection context menu item should be unavailable when no text is selected." + ); + + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_select_context_menu_with_text_selected.js b/browser/components/translations/tests/browser/browser_translations_select_context_menu_with_text_selected.js new file mode 100644 index 0000000000..deb5911a37 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_select_context_menu_with_text_selected.js @@ -0,0 +1,76 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * This test case verifies the functionality of the translate-selection context menu item + * when the selected text is not in the user's preferred language. The menu item should be + * localized to translate to the target language matching the user's top preferred language + * when the selected text is detected to be in a different language. + */ +add_task( + async function test_translate_selection_menuitem_when_selected_text_is_not_preferred_language() { + const { cleanup, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.select.enable", true]], + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await SelectTranslationsTestUtils.assertContextMenuTranslateSelectionItem( + runInPage, + { + selectSpanishParagraph: true, + openAtSpanishParagraph: true, + expectMenuItemVisible: true, + expectedTargetLanguage: "en", + }, + "The translate-selection context menu item should display a target language " + + "when the selected text is not the preferred language." + ); + + await cleanup(); + } +); + +/** + * This test case verifies the functionality of the translate-selection context menu item + * when the selected text is detected to be in the user's preferred language. The menu item + * should not be localized to display a target language when the selected text matches the + * user's top preferred language. + */ +add_task( + async function test_translate_selection_menuitem_when_selected_text_is_preferred_language() { + const { cleanup, runInPage } = await loadTestPage({ + page: ENGLISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.select.enable", true]], + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: false }, + "The button is available." + ); + + await SelectTranslationsTestUtils.assertContextMenuTranslateSelectionItem( + runInPage, + { + selectFirstParagraph: true, + openAtFirstParagraph: true, + expectMenuItemVisible: true, + expectedTargetLanguage: null, + }, + "The translate-selection context menu item should not display a target language " + + "when the selected text is in the preferred language." + ); + + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_telemetry_firstrun_auto_translate.js b/browser/components/translations/tests/browser/browser_translations_telemetry_firstrun_auto_translate.js new file mode 100644 index 0000000000..abfc3dc32e --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_telemetry_firstrun_auto_translate.js @@ -0,0 +1,117 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests the entire flow of opening the translation settings menu and initiating + * an auto-translate request on the first panel interaction. + */ +add_task(async function test_translations_telemetry_firstrun_auto_translate() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.panelShown", false]], + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewFirstShow, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + await FullPageTranslationsTestUtils.clickAlwaysTranslateLanguage({ + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 1, + expectNewFlowId: true, + expectFirstInteraction: true, + finalValuePredicates: [ + value => value.extra.auto_show === "false", + value => value.extra.view_name === "defaultView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.alwaysTranslateLanguage, + { + expectedEventCount: 1, + expectNewFlowId: false, + expectFirstInteraction: true, + finalValuePredicates: [value => value.extra.language === "es"], + } + ); + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 1, + expectNewFlowId: false, + expectFirstInteraction: true, + }); + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 1, + expectNewFlowId: false, + expectFirstInteraction: true, + }); + await TestTranslationsTelemetry.assertEvent( + Glean.translations.translationRequest, + { + expectedEventCount: 1, + expectNewFlowId: false, + expectFirstInteraction: true, + } + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewRevisit, + }); + + await FullPageTranslationsTestUtils.clickRestoreButton(); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 2, + expectNewFlowId: true, + expectFirstInteraction: false, + finalValuePredicates: [ + value => value.extra.auto_show === "false", + value => value.extra.view_name === "revisitView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.restorePageButton, + { + expectedEventCount: 1, + expectFirstInteraction: false, + expectNewFlowId: false, + } + ); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 2, + expectFirstInteraction: false, + expectNewFlowId: false, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The button is available." + ); + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_telemetry_firstrun_basics.js b/browser/components/translations/tests/browser/browser_translations_telemetry_firstrun_basics.js new file mode 100644 index 0000000000..200e06b9ce --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_telemetry_firstrun_basics.js @@ -0,0 +1,93 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests that events in the first panel session are marked as first-interaction events + * and that events in the subsequent panel session are not marked as first-interaction events. + */ +add_task(async function test_translations_telemetry_firstrun_basics() { + const { cleanup } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.panelShown", false]], + }); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 0, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewFirstShow, + }); + + await FullPageTranslationsTestUtils.clickCancelButton(); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 1, + expectNewFlowId: true, + expectFirstInteraction: true, + finalValuePredicates: [ + value => value.extra.auto_show === "false", + value => value.extra.view_name === "defaultView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.cancelButton, + { + expectedEventCount: 1, + expectNewFlowId: false, + expectFirstInteraction: true, + } + ); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 1, + expectNewFlowId: false, + expectFirstInteraction: true, + }); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewFirstShow, + }); + + await FullPageTranslationsTestUtils.clickCancelButton(); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 2, + expectNewFlowId: true, + expectFirstInteraction: false, + allValuePredicates: [ + value => value.extra.auto_show === "false", + value => value.extra.view_name === "defaultView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.cancelButton, + { + expectedEventCount: 2, + expectNewFlowId: false, + expectFirstInteraction: false, + } + ); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 2, + expectNewFlowId: false, + expectFirstInteraction: false, + }); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_telemetry_firstrun_translation_failure.js b/browser/components/translations/tests/browser/browser_translations_telemetry_firstrun_translation_failure.js new file mode 100644 index 0000000000..5397175039 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_telemetry_firstrun_translation_failure.js @@ -0,0 +1,149 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const { PromiseTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/PromiseTestUtils.sys.mjs" +); + +/** + * Tests that the first-interaction event status is maintained across a subsequent panel + * open, if re-opening the panel is due to a translation failure. + */ +add_task(async function test_translations_telemetry_firstrun_failure() { + const { cleanup, rejectDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.panelShown", false]], + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewFirstShow, + }); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 1, + expectNewFlowId: true, + expectFirstInteraction: true, + finalValuePredicates: [ + value => value.extra.auto_show === "false", + value => value.extra.view_name === "defaultView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + + await FullPageTranslationsTestUtils.clickTranslateButton({ + downloadHandler: rejectDownloads, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewFirstShowError, + }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 2, + expectNewFlowId: false, + expectFirstInteraction: true, + finalValuePredicates: [ + value => value.extra.auto_show === "true", + value => value.extra.view_name === "errorView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.translateButton, + { + expectedEventCount: 1, + expectNewFlowId: false, + expectFirstInteraction: true, + } + ); + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 1, + expectNewFlowId: false, + expectFirstInteraction: true, + }); + await TestTranslationsTelemetry.assertEvent(Glean.translations.error, { + expectedEventCount: 1, + expectNewFlowId: false, + expectFirstInteraction: true, + finalValuePredicates: [ + value => + value.extra.reason === "Error: Intentionally rejecting downloads.", + ], + }); + await TestTranslationsTelemetry.assertEvent( + Glean.translations.translationRequest, + { + expectedEventCount: 1, + expectNewFlowId: false, + expectFirstInteraction: true, + finalValuePredicates: [ + value => value.extra.from_language === "es", + value => value.extra.to_language === "en", + value => value.extra.auto_translate === "false", + value => value.extra.document_language === "es", + value => value.extra.top_preferred_language === "en", + ], + } + ); + + await FullPageTranslationsTestUtils.clickCancelButton(); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.cancelButton, + { + expectedEventCount: 1, + expectNewFlowId: false, + expectFirstInteraction: true, + } + ); + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 2, + expectNewFlowId: false, + expectFirstInteraction: true, + }); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewFirstShow, + }); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 3, + expectNewFlowId: true, + expectFirstInteraction: false, + finalValuePredicates: [ + value => value.extra.auto_show === "false", + value => value.extra.view_name === "defaultView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + + await FullPageTranslationsTestUtils.clickCancelButton(); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.cancelButton, + { + expectedEventCount: 2, + expectNewFlowId: false, + expectFirstInteraction: false, + } + ); + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 3, + expectNewFlowId: false, + expectFirstInteraction: false, + }); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_telemetry_firstrun_unsupported_lang.js b/browser/components/translations/tests/browser/browser_translations_telemetry_firstrun_unsupported_lang.js new file mode 100644 index 0000000000..fa9c67ee37 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_telemetry_firstrun_unsupported_lang.js @@ -0,0 +1,122 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests that the first-interaction event status is maintained across a subsequent panel + * open, if re-opening the panel is due to requesting to change the source language. + */ +add_task( + async function test_translations_telemetry_firstrun_unsupported_lang() { + const { cleanup } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: [ + // Do not include Spanish. + { fromLang: "fr", toLang: "en" }, + { fromLang: "en", toLang: "fr" }, + ], + prefs: [["browser.translations.panelShown", false]], + }); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: + FullPageTranslationsTestUtils.assertPanelViewUnsupportedLanguage, + }); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 1, + expectNewFlowId: true, + expectFirstInteraction: true, + finalValuePredicates: [ + value => value.extra.auto_show === "false", + value => value.extra.view_name === "defaultView", + value => value.extra.opened_from === "appMenu", + value => value.extra.document_language === "es", + ], + }); + + await FullPageTranslationsTestUtils.clickChangeSourceLanguageButton({ + firstShow: true, + }); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.changeSourceLanguageButton, + { + expectedEventCount: 1, + expectNewFlowId: false, + expectFirstInteraction: true, + } + ); + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 1, + expectNewFlowId: false, + expectFirstInteraction: true, + }); + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 2, + expectNewFlowId: false, + expectFirstInteraction: true, + finalValuePredicates: [ + value => value.extra.auto_show === "false", + value => value.extra.view_name === "defaultView", + value => value.extra.opened_from === "appMenu", + value => value.extra.document_language === "es", + ], + }); + + await FullPageTranslationsTestUtils.clickCancelButton(); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.cancelButton, + { + expectedEventCount: 1, + expectNewFlowId: false, + expectFirstInteraction: true, + } + ); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 2, + expectNewFlowId: false, + expectFirstInteraction: true, + }); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + openFromAppMenu: true, + onOpenPanel: + FullPageTranslationsTestUtils.assertPanelViewUnsupportedLanguage, + }); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 3, + expectNewFlowId: true, + expectFirstInteraction: false, + finalValuePredicates: [ + value => value.extra.auto_show === "false", + value => value.extra.view_name === "defaultView", + value => value.extra.opened_from === "appMenu", + value => value.extra.document_language === "es", + ], + }); + + await FullPageTranslationsTestUtils.clickDismissErrorButton(); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.dismissErrorButton, + { + expectedEventCount: 1, + expectNewFlowId: false, + expectFirstInteraction: false, + } + ); + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 3, + expectNewFlowId: false, + expectFirstInteraction: false, + }); + + await cleanup(); + } +); diff --git a/browser/components/translations/tests/browser/browser_translations_telemetry_open_panel.js b/browser/components/translations/tests/browser/browser_translations_telemetry_open_panel.js new file mode 100644 index 0000000000..64a02287e8 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_telemetry_open_panel.js @@ -0,0 +1,85 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests the telemetry event for opening the translations panel. + */ +add_task(async function test_translations_telemetry_open_panel() { + const { cleanup } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 0, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.clickCancelButton(); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 1, + expectNewFlowId: true, + finalValuePredicates: [ + value => value.extra.auto_show === "false", + value => value.extra.view_name === "defaultView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.cancelButton, + { + expectedEventCount: 1, + expectNewFlowId: false, + } + ); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 1, + expectNewFlowId: false, + }); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.clickCancelButton(); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 2, + expectNewFlowId: true, + allValuePredicates: [ + value => value.extra.auto_show === "false", + value => value.extra.view_name === "defaultView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.cancelButton, + { + expectedEventCount: 2, + expectNewFlowId: false, + } + ); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 2, + expectNewFlowId: false, + }); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_telemetry_panel_auto_offer.js b/browser/components/translations/tests/browser/browser_translations_telemetry_panel_auto_offer.js new file mode 100644 index 0000000000..93ff473851 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_telemetry_panel_auto_offer.js @@ -0,0 +1,83 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests that the popup is automatically offered. + */ +add_task(async function test_translations_panel_auto_offer() { + const { cleanup } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + autoOffer: true, + }); + + await FullPageTranslationsTestUtils.clickCancelButton(); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 1, + expectNewFlowId: true, + allValuePredicates: [ + value => value.extra.auto_show === "true", + value => value.extra.view_name === "defaultView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.cancelButton, + { + expectedEventCount: 1, + expectNewFlowId: false, + } + ); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 1, + expectNewFlowId: false, + }); + + await navigate("Navigate to another page on the same domain.", { + url: SPANISH_PAGE_URL_2, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The button is still shown." + ); + + await navigate("Navigate to a page on a different domain.", { + url: SPANISH_PAGE_URL_DOT_ORG, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.clickCancelButton(); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 2, + expectNewFlowId: true, + allValuePredicates: [ + value => value.extra.auto_show === "true", + value => value.extra.view_name === "defaultView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.cancelButton, + { + expectedEventCount: 2, + expectNewFlowId: false, + } + ); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 2, + expectNewFlowId: false, + }); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_telemetry_panel_auto_offer_settings.js b/browser/components/translations/tests/browser/browser_translations_telemetry_panel_auto_offer_settings.js new file mode 100644 index 0000000000..9eae81904d --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_telemetry_panel_auto_offer_settings.js @@ -0,0 +1,110 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests that the automatic offering of the popup can be disabled. + */ +add_task(async function test_translations_panel_auto_offer_settings() { + const { cleanup } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + // Use the auto offer mechanics, but default the pref to the off position. + autoOffer: true, + prefs: [["browser.translations.automaticallyPopup", false]], + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The translations button is shown." + ); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 0, + }); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.openTranslationsSettingsMenu(); + await FullPageTranslationsTestUtils.assertIsAlwaysOfferTranslationsEnabled( + false + ); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 1, + expectNewFlowId: true, + allValuePredicates: [ + value => value.extra.auto_show === "false", + value => value.extra.view_name === "defaultView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + + await FullPageTranslationsTestUtils.clickAlwaysOfferTranslations(); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.alwaysOfferTranslations, + { + expectedEventCount: 1, + expectNewFlowId: false, + allValuePredicates: [value => value.extra.toggled_on === "true"], + } + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + await FullPageTranslationsTestUtils.assertIsAlwaysOfferTranslationsEnabled( + true + ); + + await FullPageTranslationsTestUtils.clickCancelButton(); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 2, + expectNewFlowId: true, + allValuePredicates: [ + value => value.extra.auto_show === "false", + value => value.extra.view_name === "defaultView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.cancelButton, + { + expectedEventCount: 1, + expectNewFlowId: false, + } + ); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 2, + expectNewFlowId: false, + }); + + await navigate( + "Wait for the popup to be shown when navigating to a different host.", + { + url: SPANISH_PAGE_URL_DOT_ORG, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + } + ); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 3, + expectNewFlowId: true, + finalValuePredicates: [ + value => value.extra.auto_show === "true", + value => value.extra.view_name === "defaultView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_telemetry_switch_languages.js b/browser/components/translations/tests/browser/browser_translations_telemetry_switch_languages.js new file mode 100644 index 0000000000..6c06abab95 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_telemetry_switch_languages.js @@ -0,0 +1,156 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests the telemetry events for switching the from-language. + */ +add_task(async function test_translations_telemetry_switch_from_language() { + const { cleanup, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + FullPageTranslationsTestUtils.assertSelectedFromLanguage("es"); + FullPageTranslationsTestUtils.switchSelectedFromLanguage("en"); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 1, + expectNewFlowId: true, + finalValuePredicates: [ + value => value.extra.auto_show === "false", + value => value.extra.view_name === "defaultView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.changeFromLanguage, + { + expectedEventCount: 1, + expectNewFlowId: false, + finalValuePredicates: [value => value.extra.language === "en"], + } + ); + + FullPageTranslationsTestUtils.switchSelectedFromLanguage("es"); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.changeFromLanguage, + { + expectedEventCount: 2, + expectNewFlowId: false, + finalValuePredicates: [value => value.extra.language === "es"], + } + ); + + FullPageTranslationsTestUtils.switchSelectedFromLanguage(""); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.changeFromLanguage, + { + expectedEventCount: 2, + } + ); + + FullPageTranslationsTestUtils.switchSelectedFromLanguage("en"); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.changeFromLanguage, + { + expectedEventCount: 3, + expectNewFlowId: false, + finalValuePredicates: [value => value.extra.language === "en"], + } + ); + + await cleanup(); +}); + +/** + * Tests the telemetry events for switching the to-language. + */ +add_task(async function test_translations_telemetry_switch_to_language() { + const { cleanup, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + FullPageTranslationsTestUtils.assertSelectedToLanguage("en"); + FullPageTranslationsTestUtils.switchSelectedToLanguage("fr"); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 1, + expectNewFlowId: true, + finalValuePredicates: [ + value => value.extra.auto_show === "false", + value => value.extra.view_name === "defaultView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.changeToLanguage, + { + expectedEventCount: 1, + expectNewFlowId: false, + finalValuePredicates: [value => value.extra.language === "fr"], + } + ); + + FullPageTranslationsTestUtils.switchSelectedToLanguage("en"); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.changeToLanguage, + { + expectedEventCount: 2, + expectNewFlowId: false, + finalValuePredicates: [value => value.extra.language === "en"], + } + ); + + FullPageTranslationsTestUtils.switchSelectedToLanguage(""); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.changeToLanguage, + { + expectedEventCount: 2, + } + ); + + FullPageTranslationsTestUtils.switchSelectedToLanguage("en"); + + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.changeToLanguage, + { + expectedEventCount: 3, + expectNewFlowId: false, + finalValuePredicates: [value => value.extra.language === "en"], + } + ); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_telemetry_translation_failure.js b/browser/components/translations/tests/browser/browser_translations_telemetry_translation_failure.js new file mode 100644 index 0000000000..e7d0cfb4f4 --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_telemetry_translation_failure.js @@ -0,0 +1,212 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const { PromiseTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/PromiseTestUtils.sys.mjs" +); + +/** + * Tests the telemetry event for a manual translation request failure. + */ +add_task( + async function test_translations_telemetry_manual_translation_failure() { + const { cleanup, rejectDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await TestTranslationsTelemetry.assertCounter( + "RequestCount", + Glean.translations.requestsCount, + 0 + ); + await TestTranslationsTelemetry.assertRate( + "ErrorRate", + Glean.translations.errorRate, + { + expectedNumerator: 0, + expectedDenominator: 0, + } + ); + await TestTranslationsTelemetry.assertEvent( + Glean.translations.translationRequest, + { + expectedEventCount: 0, + } + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 1, + expectNewFlowId: true, + finalValuePredicates: [ + value => value.extra.auto_show === "false", + value => value.extra.view_name === "defaultView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + + await FullPageTranslationsTestUtils.clickTranslateButton({ + downloadHandler: rejectDownloads, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewError, + }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 2, + expectNewFlowId: false, + finalValuePredicates: [ + value => value.extra.auto_show === "true", + value => value.extra.view_name === "errorView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.translateButton, + { + expectedEventCount: 1, + expectNewFlowId: false, + } + ); + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 1, + expectNewFlowId: false, + }); + await TestTranslationsTelemetry.assertCounter( + "RequestCount", + Glean.translations.requestsCount, + 1 + ); + await TestTranslationsTelemetry.assertRate( + "ErrorRate", + Glean.translations.errorRate, + { + expectedNumerator: 1, + expectedDenominator: 1, + } + ); + await TestTranslationsTelemetry.assertEvent(Glean.translations.error, { + expectedEventCount: 1, + expectNewFlowId: false, + finalValuePredicates: [ + value => + value.extra.reason === "Error: Intentionally rejecting downloads.", + ], + }); + await TestTranslationsTelemetry.assertEvent( + Glean.translations.translationRequest, + { + expectedEventCount: 1, + expectNewFlowId: false, + finalValuePredicates: [ + value => value.extra.from_language === "es", + value => value.extra.to_language === "en", + value => value.extra.auto_translate === "false", + value => value.extra.document_language === "es", + value => value.extra.top_preferred_language === "en", + ], + } + ); + + await cleanup(); + } +); + +/** + * Tests the telemetry event for an automatic translation request failure. + */ +add_task(async function test_translations_telemetry_auto_translation_failure() { + const { cleanup, rejectDownloads, runInPage } = await loadTestPage({ + page: BLANK_PAGE, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.alwaysTranslateLanguages", "es"]], + }); + + await navigate("Navigate to a Spanish page", { + url: SPANISH_PAGE_URL, + downloadHandler: rejectDownloads, + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewError, + }); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await TestTranslationsTelemetry.assertCounter( + "RequestCount", + Glean.translations.requestsCount, + 1 + ); + await TestTranslationsTelemetry.assertRate( + "ErrorRate", + Glean.translations.errorRate, + { + expectedNumerator: 1, + expectedDenominator: 1, + } + ); + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 1, + expectNewFlowId: true, + finalValuePredicates: [ + value => value.extra.auto_show === "true", + value => value.extra.view_name === "errorView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 0, + expectNewFlowId: false, + }); + await TestTranslationsTelemetry.assertEvent(Glean.translations.error, { + expectedEventCount: 1, + expectNewFlowId: false, + finalValuePredicates: [ + value => + value.extra.reason === "Error: Intentionally rejecting downloads.", + ], + }); + await TestTranslationsTelemetry.assertEvent( + Glean.translations.translationRequest, + { + expectedEventCount: 1, + expectNewFlowId: false, + finalValuePredicates: [ + value => value.extra.from_language === "es", + value => value.extra.to_language === "en", + value => value.extra.auto_translate === "true", + value => value.extra.document_language === "es", + value => value.extra.top_preferred_language === "en", + ], + } + ); + + await FullPageTranslationsTestUtils.clickCancelButton(); + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.cancelButton, + { + expectedEventCount: 1, + expectNewFlowId: false, + } + ); + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 1, + expectNewFlowId: false, + }); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/browser_translations_telemetry_translation_request.js b/browser/components/translations/tests/browser/browser_translations_telemetry_translation_request.js new file mode 100644 index 0000000000..90bd81a8ed --- /dev/null +++ b/browser/components/translations/tests/browser/browser_translations_telemetry_translation_request.js @@ -0,0 +1,170 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Tests the telemetry event for a manual translation request. + */ +add_task(async function test_translations_telemetry_manual_translation() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: SPANISH_PAGE_URL, + languagePairs: LANGUAGE_PAIRS, + }); + + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: false, icon: true }, + "The button is available." + ); + + await FullPageTranslationsTestUtils.assertPageIsUntranslated(runInPage); + + await TestTranslationsTelemetry.assertCounter( + "RequestCount", + Glean.translations.requestsCount, + 0 + ); + await TestTranslationsTelemetry.assertRate( + "ErrorRate", + Glean.translations.errorRate, + { + expectedNumerator: 0, + expectedDenominator: 0, + } + ); + await TestTranslationsTelemetry.assertEvent( + Glean.translations.translationRequest, + { + expectedEventCount: 0, + } + ); + + await FullPageTranslationsTestUtils.openTranslationsPanel({ + onOpenPanel: FullPageTranslationsTestUtils.assertPanelViewDefault, + }); + + await FullPageTranslationsTestUtils.clickTranslateButton({ + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await TestTranslationsTelemetry.assertCounter( + "RequestCount", + Glean.translations.requestsCount, + 1 + ); + await TestTranslationsTelemetry.assertRate( + "ErrorRate", + Glean.translations.errorRate, + { + expectedNumerator: 0, + expectedDenominator: 1, + } + ); + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 1, + expectNewFlowId: true, + finalValuePredicates: [ + value => value.extra.auto_show === "false", + value => value.extra.view_name === "defaultView", + value => value.extra.opened_from === "translationsButton", + value => value.extra.document_language === "es", + ], + }); + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.translateButton, + { + expectedEventCount: 1, + expectNewFlowId: false, + } + ); + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 1, + expectNewFlowId: false, + }); + await TestTranslationsTelemetry.assertEvent( + Glean.translations.translationRequest, + { + expectedEventCount: 1, + expectNewFlowId: false, + finalValuePredicates: [ + value => value.extra.from_language === "es", + value => value.extra.to_language === "en", + value => value.extra.auto_translate === "false", + value => value.extra.document_language === "es", + value => value.extra.top_preferred_language === "en", + ], + } + ); + + await cleanup(); +}); + +/** + * Tests the telemetry event for an automatic translation request. + */ +add_task(async function test_translations_telemetry_auto_translation() { + const { cleanup, resolveDownloads, runInPage } = await loadTestPage({ + page: BLANK_PAGE, + languagePairs: LANGUAGE_PAIRS, + prefs: [["browser.translations.alwaysTranslateLanguages", "es"]], + }); + + await navigate("Navigate to a Spanish page", { + url: SPANISH_PAGE_URL, + downloadHandler: resolveDownloads, + }); + + await FullPageTranslationsTestUtils.assertPageIsTranslated( + "es", + "en", + runInPage + ); + + await TestTranslationsTelemetry.assertCounter( + "RequestCount", + Glean.translations.requestsCount, + 1 + ); + await TestTranslationsTelemetry.assertRate( + "ErrorRate", + Glean.translations.errorRate, + { + expectedNumerator: 0, + expectedDenominator: 1, + } + ); + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.open, { + expectedEventCount: 0, + }); + await TestTranslationsTelemetry.assertEvent( + Glean.translationsPanel.translateButton, + { + expectedEventCount: 0, + } + ); + await TestTranslationsTelemetry.assertEvent(Glean.translationsPanel.close, { + expectedEventCount: 0, + }); + await TestTranslationsTelemetry.assertEvent( + Glean.translations.translationRequest, + { + expectedEventCount: 1, + expectNewFlowId: true, + finalValuePredicates: [ + value => value.extra.from_language === "es", + value => value.extra.to_language === "en", + value => value.extra.auto_translate === "true", + value => value.extra.document_language === "es", + value => value.extra.top_preferred_language === "en", + ], + } + ); + + await cleanup(); +}); diff --git a/browser/components/translations/tests/browser/head.js b/browser/components/translations/tests/browser/head.js new file mode 100644 index 0000000000..bc9968308c --- /dev/null +++ b/browser/components/translations/tests/browser/head.js @@ -0,0 +1,1423 @@ +/* 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 +); + +/** + * Opens a new tab in the foreground. + * + * @param {string} url + */ +async function addTab(url) { + logAction(url); + const tab = await BrowserTestUtils.openNewForegroundTab( + gBrowser, + url, + true // Wait for laod + ); + return { + tab, + removeTab() { + BrowserTestUtils.removeTab(tab); + }, + }; +} + +/** + * Simulates clicking an element with the mouse. + * + * @param {element} element - The element to click. + * @param {string} [message] - A message to log to info. + */ +function click(element, message) { + logAction(message); + return new Promise(resolve => { + element.addEventListener( + "click", + function () { + resolve(); + }, + { once: true } + ); + + EventUtils.synthesizeMouseAtCenter(element, { + type: "mousedown", + isSynthesized: false, + }); + EventUtils.synthesizeMouseAtCenter(element, { + type: "mouseup", + isSynthesized: false, + }); + }); +} + +/** + * Get all elements that match the l10n id. + * + * @param {string} l10nId + * @param {Document} doc + * @returns {Element} + */ +function getAllByL10nId(l10nId, doc = document) { + const elements = doc.querySelectorAll(`[data-l10n-id="${l10nId}"]`); + console.log(doc); + if (elements.length === 0) { + throw new Error("Could not find the element by l10n id: " + l10nId); + } + return elements; +} + +/** + * Retrieves an element by its Id. + * + * @param {string} id + * @param {Document} [doc] + * @returns {Element} + * @throws Throws if the element is not visible in the DOM. + */ +function getById(id, doc = document) { + const element = maybeGetById(id, /* ensureIsVisible */ true, doc); + if (!element) { + throw new Error("The element is not visible in the DOM: #" + id); + } + return element; +} + +/** + * Get an element by its l10n id, as this is a user-visible way to find an element. + * The `l10nId` represents the text that a user would actually see. + * + * @param {string} l10nId + * @param {Document} doc + * @returns {Element} + */ +function getByL10nId(l10nId, doc = document) { + const elements = doc.querySelectorAll(`[data-l10n-id="${l10nId}"]`); + if (elements.length === 0) { + throw new Error("Could not find the element by l10n id: " + l10nId); + } + for (const element of elements) { + if (BrowserTestUtils.isVisible(element)) { + return element; + } + } + throw new Error("The element is not visible in the DOM: " + l10nId); +} + +/** + * Returns the intl display name of a given language tag. + * + * @param {string} langTag - A BCP-47 language tag. + */ +const getIntlDisplayName = (() => { + let displayNames = null; + + return langTag => { + if (!displayNames) { + displayNames = new Services.intl.DisplayNames(undefined, { + type: "language", + fallback: "none", + }); + } + return displayNames.of(langTag); + }; +})(); + +/** + * Attempts to retrieve an element by its Id. + * + * @param {string} id - The Id of the element to retrieve. + * @param {boolean} [ensureIsVisible=true] - If set to true, the function will return null when the element is not visible. + * @param {Document} [doc=document] - The document from which to retrieve the element. + * @returns {Element | null} - The retrieved element. + * @throws Throws if no element was found by the given Id. + */ +function maybeGetById(id, ensureIsVisible = true, doc = document) { + const element = doc.getElementById(id); + if (!element) { + throw new Error("Could not find the element by id: #" + id); + } + + if (!ensureIsVisible) { + return element; + } + + if (BrowserTestUtils.isVisible(element)) { + return element; + } + + return null; +} + +/** + * A non-throwing version of `getByL10nId`. + * + * @param {string} l10nId + * @returns {Element | null} + */ +function maybeGetByL10nId(l10nId, doc = document) { + const selector = `[data-l10n-id="${l10nId}"]`; + const elements = doc.querySelectorAll(selector); + for (const element of elements) { + if (BrowserTestUtils.isVisible(element)) { + return element; + } + } + return null; +} + +/** + * Provide a uniform way to log actions. This abuses the Error stack to get the callers + * of the action. This should help in test debugging. + */ +function logAction(...params) { + const error = new Error(); + const stackLines = error.stack.split("\n"); + const actionName = stackLines[1]?.split("@")[0] ?? ""; + const taskFileLocation = stackLines[2]?.split("@")[1] ?? ""; + if (taskFileLocation.includes("head.js")) { + // Only log actions that were done at the test level. + return; + } + + info(`Action: ${actionName}(${params.join(", ")})`); + info( + `Source: ${taskFileLocation.replace( + "chrome://mochitests/content/browser/", + "" + )}` + ); +} + +/** + * Navigate to a URL and indicate a message as to why. + */ +async function navigate( + message, + { url, onOpenPanel = null, downloadHandler = null, pivotTranslation = false } +) { + logAction(); + // When the translations panel is open from the app menu, + // it doesn't close on navigate the way that it does when it's + // open from the translations button, so ensure that we always + // close it when we navigate to a new page. + await closeTranslationsPanelIfOpen(); + + info(message); + + // Load a blank page first to ensure that tests don't hang. + // I don't know why this is needed, but it appears to be necessary. + BrowserTestUtils.startLoadingURIString(gBrowser.selectedBrowser, BLANK_PAGE); + await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser); + + const loadTargetPage = async () => { + BrowserTestUtils.startLoadingURIString(gBrowser.selectedBrowser, url); + await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser); + + if (downloadHandler) { + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: true, locale: false, icon: true }, + "The icon presents the loading indicator." + ); + await downloadHandler(pivotTranslation ? 2 : 1); + } + }; + + info(`Loading url: "${url}"`); + if (onOpenPanel) { + await FullPageTranslationsTestUtils.waitForTranslationsPopupEvent( + "popupshown", + loadTargetPage, + onOpenPanel + ); + } else { + await loadTargetPage(); + } +} + +/** + * Switches to a given tab. + * + * @param {object} tab - The tab to switch to + * @param {string} name + */ +async function switchTab(tab, name) { + logAction("tab", name); + gBrowser.selectedTab = tab; + await new Promise(resolve => setTimeout(resolve, 0)); +} + +/** + * Click the reader-mode button if the reader-mode button is available. + * Fails if the reader-mode button is hidden. + */ +async function toggleReaderMode() { + logAction(); + const readerButton = document.getElementById("reader-mode-button"); + await waitForCondition(() => readerButton.hidden === false); + + readerButton.getAttribute("readeractive") + ? info("Exiting reader mode") + : info("Entering reader mode"); + + const readyPromise = readerButton.getAttribute("readeractive") + ? waitForCondition(() => !readerButton.getAttribute("readeractive")) + : BrowserTestUtils.waitForContentEvent( + gBrowser.selectedBrowser, + "AboutReaderContentReady" + ); + + click(readerButton, "Clicking the reader-mode button"); + await readyPromise; +} + +/** + * A class containing test utility functions specific to testing full-page translations. + */ +class FullPageTranslationsTestUtils { + /** + * A collection of element visibility expectations for the default panel view. + */ + static #defaultViewVisibilityExpectations = { + cancelButton: true, + fromMenuList: true, + fromLabel: true, + header: true, + langSelection: true, + toMenuList: true, + toLabel: true, + translateButton: true, + }; + + /** + * Asserts that the state of a checkbox with a given dataL10nId is + * checked or not, based on the value of expected being true or false. + * + * @param {string} dataL10nId - The data-l10n-id of the checkbox. + * @param {object} expectations + * @param {string} expectations.langTag - A BCP-47 language tag. + * @param {boolean} expectations.checked - Whether the checkbox is expected to be checked. + * @param {boolean} expectations.disabled - Whether the menuitem is expected to be disabled. + */ + static async #assertCheckboxState( + dataL10nId, + { langTag = null, checked = true, disabled = false } + ) { + const menuItems = getAllByL10nId(dataL10nId); + for (const menuItem of menuItems) { + if (langTag) { + const { + args: { language }, + } = document.l10n.getAttributes(menuItem); + is( + language, + getIntlDisplayName(langTag), + `Should match expected language display name for ${dataL10nId}` + ); + } + is( + menuItem.disabled, + disabled, + `Should match expected disabled state for ${dataL10nId}` + ); + await waitForCondition( + () => menuItem.getAttribute("checked") === (checked ? "true" : "false"), + "Waiting for checkbox state" + ); + is( + menuItem.getAttribute("checked"), + checked ? "true" : "false", + `Should match expected checkbox state for ${dataL10nId}` + ); + } + } + + /** + * Asserts that the always-offer-translations checkbox matches the expected checked state. + * + * @param {boolean} checked + */ + static async assertIsAlwaysOfferTranslationsEnabled(checked) { + info( + `Checking that always-offer-translations is ${ + checked ? "enabled" : "disabled" + }` + ); + await FullPageTranslationsTestUtils.#assertCheckboxState( + "translations-panel-settings-always-offer-translation", + { checked } + ); + } + + /** + * Asserts that the always-translate-language checkbox matches the expected checked state. + * + * @param {string} langTag - A BCP-47 language tag + * @param {object} expectations + * @param {boolean} expectations.checked - Whether the checkbox is expected to be checked. + * @param {boolean} expectations.disabled - Whether the menuitem is expected to be disabled. + */ + static async assertIsAlwaysTranslateLanguage( + langTag, + { checked = true, disabled = false } + ) { + info( + `Checking that always-translate is ${ + checked ? "enabled" : "disabled" + } for "${langTag}"` + ); + await FullPageTranslationsTestUtils.#assertCheckboxState( + "translations-panel-settings-always-translate-language", + { langTag, checked, disabled } + ); + } + + /** + * Asserts that the never-translate-language checkbox matches the expected checked state. + * + * @param {string} langTag - A BCP-47 language tag + * @param {object} expectations + * @param {boolean} expectations.checked - Whether the checkbox is expected to be checked. + * @param {boolean} expectations.disabled - Whether the menuitem is expected to be disabled. + */ + static async assertIsNeverTranslateLanguage( + langTag, + { checked = true, disabled = false } + ) { + info( + `Checking that never-translate is ${ + checked ? "enabled" : "disabled" + } for "${langTag}"` + ); + await FullPageTranslationsTestUtils.#assertCheckboxState( + "translations-panel-settings-never-translate-language", + { langTag, checked, disabled } + ); + } + + /** + * Asserts that the never-translate-site checkbox matches the expected checked state. + * + * @param {string} url - The url of a website + * @param {object} expectations + * @param {boolean} expectations.checked - Whether the checkbox is expected to be checked. + * @param {boolean} expectations.disabled - Whether the menuitem is expected to be disabled. + */ + static async assertIsNeverTranslateSite( + url, + { checked = true, disabled = false } + ) { + info( + `Checking that never-translate is ${ + checked ? "enabled" : "disabled" + } for "${url}"` + ); + await FullPageTranslationsTestUtils.#assertCheckboxState( + "translations-panel-settings-never-translate-site", + { checked, disabled } + ); + } + + /** + * Asserts that the proper language tags are shown on the translations button. + * + * @param {string} fromLanguage - The BCP-47 language tag being translated from. + * @param {string} toLanguage - The BCP-47 language tag being translated into. + */ + static async #assertLangTagIsShownOnTranslationsButton( + fromLanguage, + toLanguage + ) { + info( + `Ensuring that the translations button displays the language tag "${toLanguage}"` + ); + const { button, locale } = + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: false, locale: true, icon: true }, + "The icon presents the locale." + ); + is( + locale.innerText, + toLanguage, + `The expected language tag "${toLanguage}" is shown.` + ); + is( + button.getAttribute("data-l10n-id"), + "urlbar-translations-button-translated" + ); + const fromLangDisplay = getIntlDisplayName(fromLanguage); + const toLangDisplay = getIntlDisplayName(toLanguage); + is( + button.getAttribute("data-l10n-args"), + `{"fromLanguage":"${fromLangDisplay}","toLanguage":"${toLangDisplay}"}` + ); + } + /** + * Asserts that the Spanish test page has been translated by checking + * that the H1 element has been modified from its original form. + * + * @param {string} fromLanguage - The BCP-47 language tag being translated from. + * @param {string} toLanguage - The BCP-47 language tag being translated into. + * @param {Function} runInPage - Allows running a closure in the content page. + * @param {string} message - An optional message to log to info. + */ + static async assertPageIsTranslated( + fromLanguage, + toLanguage, + runInPage, + message = null + ) { + if (message) { + info(message); + } + info("Checking that the page is translated"); + const callback = async (TranslationsTest, { fromLang, toLang }) => { + const { getH1 } = TranslationsTest.getSelectors(); + await TranslationsTest.assertTranslationResult( + "The page's H1 is translated.", + getH1, + `DON QUIJOTE DE LA MANCHA [${fromLang} to ${toLang}, html]` + ); + }; + await runInPage(callback, { fromLang: fromLanguage, toLang: toLanguage }); + await FullPageTranslationsTestUtils.#assertLangTagIsShownOnTranslationsButton( + fromLanguage, + toLanguage + ); + } + + /** + * Asserts that the Spanish test page is untranslated by checking + * that the H1 element is still in its original Spanish form. + * + * @param {Function} runInPage - Allows running a closure in the content page. + * @param {string} message - An optional message to log to info. + */ + static async assertPageIsUntranslated(runInPage, message = null) { + if (message) { + info(message); + } + info("Checking that the page is untranslated"); + await runInPage(async TranslationsTest => { + const { getH1 } = TranslationsTest.getSelectors(); + await TranslationsTest.assertTranslationResult( + "The page's H1 is untranslated and in the original Spanish.", + getH1, + "Don Quijote de La Mancha" + ); + }); + } + + /** + * Asserts that for each provided expectation, the visible state of the corresponding + * element in TranslationsPanel.elements both exists and matches the visibility expectation. + * + * @param {object} expectations + * A list of expectations for the visibility of any subset of TranslationsPanel.elements + */ + static #assertPanelElementVisibility(expectations = {}) { + // Assume nothing is visible by default, and overwrite them + // with any specific expectations provided in the argument. + const finalExpectations = { + cancelButton: false, + changeSourceLanguageButton: false, + dismissErrorButton: false, + error: false, + fromMenuList: false, + fromLabel: false, + header: false, + intro: false, + introLearnMoreLink: false, + langSelection: false, + restoreButton: false, + toLabel: false, + toMenuList: false, + translateButton: false, + unsupportedHeader: false, + unsupportedHint: false, + unsupportedLearnMoreLink: false, + ...expectations, + }; + + const elements = TranslationsPanel.elements; + const hidden = {}; + const visible = {}; + + for (const propertyName in finalExpectations) { + ok( + elements.hasOwnProperty(propertyName), + `Expected translations panel elements to have property ${propertyName}` + ); + if (finalExpectations[propertyName]) { + visible[propertyName] = elements[propertyName]; + } else { + hidden[propertyName] = elements[propertyName]; + } + } + + assertVisibility({ hidden, visible }); + } + + /** + * Asserts that the TranslationsPanel header has the expected l10nId. + * + * @param {string} l10nId - The expected data-l10n-id of the header. + */ + static #assertPanelHeaderL10nId(l10nId) { + const { header } = TranslationsPanel.elements; + is( + header.getAttribute("data-l10n-id"), + l10nId, + "The translations panel header should match the expected data-l10n-id" + ); + } + + /** + * Asserts that the mainViewId of the panel matches the given string. + * + * @param {string} expectedId + */ + static #assertPanelMainViewId(expectedId) { + const mainViewId = + TranslationsPanel.elements.multiview.getAttribute("mainViewId"); + is( + mainViewId, + expectedId, + "The full-page Translations panel mainViewId should match its expected value" + ); + } + + /** + * Asserts that panel element visibility matches the default panel view. + */ + static assertPanelViewDefault() { + info("Checking that the panel shows the default view"); + FullPageTranslationsTestUtils.#assertPanelMainViewId( + "translations-panel-view-default" + ); + FullPageTranslationsTestUtils.#assertPanelElementVisibility({ + ...FullPageTranslationsTestUtils.#defaultViewVisibilityExpectations, + }); + FullPageTranslationsTestUtils.#assertPanelHeaderL10nId( + "translations-panel-header" + ); + } + + /** + * Asserts that panel element visibility matches the panel error view. + */ + static assertPanelViewError() { + info("Checking that the panel shows the error view"); + FullPageTranslationsTestUtils.#assertPanelMainViewId( + "translations-panel-view-default" + ); + FullPageTranslationsTestUtils.#assertPanelElementVisibility({ + error: true, + ...FullPageTranslationsTestUtils.#defaultViewVisibilityExpectations, + }); + FullPageTranslationsTestUtils.#assertPanelHeaderL10nId( + "translations-panel-header" + ); + } + + /** + * Asserts that the panel element visibility matches the panel loading view. + */ + static assertPanelViewLoading() { + info("Checking that the panel shows the loading view"); + FullPageTranslationsTestUtils.assertPanelViewDefault(); + const loadingButton = getByL10nId( + "translations-panel-translate-button-loading" + ); + ok(loadingButton, "The loading button is present"); + ok(loadingButton.disabled, "The loading button is disabled"); + } + + /** + * Asserts that panel element visibility matches the panel first-show view. + */ + static assertPanelViewFirstShow() { + info("Checking that the panel shows the first-show view"); + FullPageTranslationsTestUtils.#assertPanelMainViewId( + "translations-panel-view-default" + ); + FullPageTranslationsTestUtils.#assertPanelElementVisibility({ + intro: true, + introLearnMoreLink: true, + ...FullPageTranslationsTestUtils.#defaultViewVisibilityExpectations, + }); + FullPageTranslationsTestUtils.#assertPanelHeaderL10nId( + "translations-panel-intro-header" + ); + } + + /** + * Asserts that panel element visibility matches the panel first-show error view. + */ + static assertPanelViewFirstShowError() { + info("Checking that the panel shows the first-show error view"); + FullPageTranslationsTestUtils.#assertPanelMainViewId( + "translations-panel-view-default" + ); + FullPageTranslationsTestUtils.#assertPanelElementVisibility({ + error: true, + intro: true, + introLearnMoreLink: true, + ...FullPageTranslationsTestUtils.#defaultViewVisibilityExpectations, + }); + FullPageTranslationsTestUtils.#assertPanelHeaderL10nId( + "translations-panel-intro-header" + ); + } + + /** + * Asserts that panel element visibility matches the panel revisit view. + */ + static assertPanelViewRevisit() { + info("Checking that the panel shows the revisit view"); + FullPageTranslationsTestUtils.#assertPanelMainViewId( + "translations-panel-view-default" + ); + FullPageTranslationsTestUtils.#assertPanelElementVisibility({ + header: true, + langSelection: true, + restoreButton: true, + toLabel: true, + toMenuList: true, + translateButton: true, + }); + FullPageTranslationsTestUtils.#assertPanelHeaderL10nId( + "translations-panel-revisit-header" + ); + } + + /** + * Asserts that panel element visibility matches the panel unsupported language view. + */ + static assertPanelViewUnsupportedLanguage() { + info("Checking that the panel shows the unsupported-language view"); + FullPageTranslationsTestUtils.#assertPanelMainViewId( + "translations-panel-view-unsupported-language" + ); + FullPageTranslationsTestUtils.#assertPanelElementVisibility({ + changeSourceLanguageButton: true, + dismissErrorButton: true, + unsupportedHeader: true, + unsupportedHint: true, + unsupportedLearnMoreLink: true, + }); + } + + /** + * Asserts that the selected from-language matches the provided language tag. + * + * @param {string} langTag - A BCP-47 language tag. + */ + static assertSelectedFromLanguage(langTag) { + info(`Checking that the selected from-language matches ${langTag}`); + const { fromMenuList } = TranslationsPanel.elements; + is( + fromMenuList.value, + langTag, + "Expected selected from-language to match the given language tag" + ); + } + + /** + * Asserts that the selected to-language matches the provided language tag. + * + * @param {string} langTag - A BCP-47 language tag. + */ + static assertSelectedToLanguage(langTag) { + info(`Checking that the selected to-language matches ${langTag}`); + const { toMenuList } = TranslationsPanel.elements; + is( + toMenuList.value, + langTag, + "Expected selected to-language to match the given language tag" + ); + } + + /** + * Assert some property about the translations button. + * + * @param {Record} visibleAssertions + * @param {string} message The message for the assertion. + * @returns {HTMLElement} + */ + static async assertTranslationsButton(visibleAssertions, message) { + const elements = { + button: document.getElementById("translations-button"), + icon: document.getElementById("translations-button-icon"), + circleArrows: document.getElementById( + "translations-button-circle-arrows" + ), + locale: document.getElementById("translations-button-locale"), + }; + + for (const [name, element] of Object.entries(elements)) { + if (!element) { + throw new Error("Could not find the " + name); + } + } + + try { + // Test that the visibilities match. + await waitForCondition(() => { + for (const [name, visible] of Object.entries(visibleAssertions)) { + if (elements[name].hidden === visible) { + return false; + } + } + return true; + }, message); + } catch (error) { + // On a mismatch, report it. + for (const [name, expected] of Object.entries(visibleAssertions)) { + is(!elements[name].hidden, expected, `Visibility for "${name}"`); + } + } + + ok(true, message); + + return elements; + } + + /** + * Simulates the effect of clicking the always-offer-translations menuitem. + * Requires that the settings menu of the translations panel is open, + * otherwise the test will fail. + */ + static async clickAlwaysOfferTranslations() { + logAction(); + await FullPageTranslationsTestUtils.#clickSettingsMenuItemByL10nId( + "translations-panel-settings-always-offer-translation" + ); + } + + /** + * Simulates the effect of clicking the always-translate-language menuitem. + * Requires that the settings menu of the translations panel is open, + * otherwise the test will fail. + */ + static async clickAlwaysTranslateLanguage({ + downloadHandler = null, + pivotTranslation = false, + } = {}) { + logAction(); + await FullPageTranslationsTestUtils.#clickSettingsMenuItemByL10nId( + "translations-panel-settings-always-translate-language" + ); + if (downloadHandler) { + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: true, locale: false, icon: true }, + "The icon presents the loading indicator." + ); + await downloadHandler(pivotTranslation ? 2 : 1); + } + } + + /** + * Simulates clicking the cancel button. + */ + static async clickCancelButton() { + logAction(); + const { cancelButton } = TranslationsPanel.elements; + assertVisibility({ visible: { cancelButton } }); + await FullPageTranslationsTestUtils.waitForTranslationsPopupEvent( + "popuphidden", + () => { + click(cancelButton, "Clicking the cancel button"); + } + ); + } + + /** + * Simulates clicking the change-source-language button. + * + * @param {object} config + * @param {boolean} config.firstShow + * - True if the first-show view should be expected + * False if the default view should be expected + */ + static async clickChangeSourceLanguageButton({ firstShow = false } = {}) { + logAction(); + const { changeSourceLanguageButton } = TranslationsPanel.elements; + assertVisibility({ visible: { changeSourceLanguageButton } }); + await FullPageTranslationsTestUtils.waitForTranslationsPopupEvent( + "popupshown", + () => { + click( + changeSourceLanguageButton, + "Click the change-source-language button" + ); + }, + firstShow + ? FullPageTranslationsTestUtils.assertPanelViewFirstShow + : FullPageTranslationsTestUtils.assertPanelViewDefault + ); + } + + /** + * Simulates clicking the dismiss-error button. + */ + static async clickDismissErrorButton() { + logAction(); + const { dismissErrorButton } = TranslationsPanel.elements; + assertVisibility({ visible: { dismissErrorButton } }); + await FullPageTranslationsTestUtils.waitForTranslationsPopupEvent( + "popuphidden", + () => { + click(dismissErrorButton, "Click the dismiss-error button"); + } + ); + } + + /** + * Simulates the effect of clicking the manage-languages menuitem. + * Requires that the settings menu of the translations panel is open, + * otherwise the test will fail. + */ + static async clickManageLanguages() { + logAction(); + await FullPageTranslationsTestUtils.#clickSettingsMenuItemByL10nId( + "translations-panel-settings-manage-languages" + ); + } + + /** + * Simulates the effect of clicking the never-translate-language menuitem. + * Requires that the settings menu of the translations panel is open, + * otherwise the test will fail. + */ + static async clickNeverTranslateLanguage() { + logAction(); + await FullPageTranslationsTestUtils.#clickSettingsMenuItemByL10nId( + "translations-panel-settings-never-translate-language" + ); + } + + /** + * Simulates the effect of clicking the never-translate-site menuitem. + * Requires that the settings menu of the translations panel is open, + * otherwise the test will fail. + */ + static async clickNeverTranslateSite() { + logAction(); + await FullPageTranslationsTestUtils.#clickSettingsMenuItemByL10nId( + "translations-panel-settings-never-translate-site" + ); + } + + /** + * Simulates clicking the restore-page button. + */ + static async clickRestoreButton() { + logAction(); + const { restoreButton } = TranslationsPanel.elements; + assertVisibility({ visible: { restoreButton } }); + await FullPageTranslationsTestUtils.waitForTranslationsPopupEvent( + "popuphidden", + () => { + click(restoreButton, "Click the restore-page button"); + } + ); + } + + /* + * Simulates the effect of toggling a menu item in the translations panel + * settings menu. Requires that the settings menu is currently open, + * otherwise the test will fail. + */ + static async #clickSettingsMenuItemByL10nId(l10nId) { + info(`Toggling the "${l10nId}" settings menu item.`); + click(getByL10nId(l10nId), `Clicking the "${l10nId}" settings menu item.`); + await closeSettingsMenuIfOpen(); + } + + /** + * Simulates clicking the translate button. + * + * @param {object} config + * @param {Function} config.downloadHandler + * - The function handle expected downloads, resolveDownloads() or rejectDownloads() + * Leave as null to test more granularly, such as testing opening the loading view, + * or allowing for the automatic downloading of files. + * @param {boolean} config.pivotTranslation + * - True if the expected translation is a pivot translation, otherwise false. + * Affects the number of expected downloads. + */ + static async clickTranslateButton({ + downloadHandler = null, + pivotTranslation = false, + } = {}) { + logAction(); + const { translateButton } = TranslationsPanel.elements; + assertVisibility({ visible: { translateButton } }); + await FullPageTranslationsTestUtils.waitForTranslationsPopupEvent( + "popuphidden", + () => { + click(translateButton); + } + ); + + if (downloadHandler) { + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true, circleArrows: true, locale: false, icon: true }, + "The icon presents the loading indicator." + ); + await downloadHandler(pivotTranslation ? 2 : 1); + } + } + + /** + * Opens the translations panel. + * + * @param {object} config + * @param {Function} config.onOpenPanel + * - A function to run as soon as the panel opens. + * @param {boolean} config.openFromAppMenu + * - Open the panel from the app menu. If false, uses the translations button. + * @param {boolean} config.openWithKeyboard + * - Open the panel by synthesizing the keyboard. If false, synthesizes the mouse. + */ + static async openTranslationsPanel({ + onOpenPanel = null, + openFromAppMenu = false, + openWithKeyboard = false, + }) { + logAction(); + await closeTranslationsPanelIfOpen(); + if (openFromAppMenu) { + await FullPageTranslationsTestUtils.#openTranslationsPanelViaAppMenu({ + onOpenPanel, + openWithKeyboard, + }); + } else { + await FullPageTranslationsTestUtils.#openTranslationsPanelViaTranslationsButton( + { + onOpenPanel, + openWithKeyboard, + } + ); + } + } + + /** + * Opens the translations panel via the app menu. + * + * @param {object} config + * @param {Function} config.onOpenPanel + * - A function to run as soon as the panel opens. + * @param {boolean} config.openWithKeyboard + * - Open the panel by synthesizing the keyboard. If false, synthesizes the mouse. + */ + static async #openTranslationsPanelViaAppMenu({ + onOpenPanel = null, + openWithKeyboard = false, + }) { + logAction(); + const appMenuButton = getById("PanelUI-menu-button"); + if (openWithKeyboard) { + hitEnterKey(appMenuButton, "Opening the app-menu button with keyboard"); + } else { + click(appMenuButton, "Opening the app-menu button"); + } + await BrowserTestUtils.waitForEvent(window.PanelUI.mainView, "ViewShown"); + + const translateSiteButton = getById("appMenu-translate-button"); + + is( + translateSiteButton.disabled, + false, + "The app-menu translate button should be enabled" + ); + + await FullPageTranslationsTestUtils.waitForTranslationsPopupEvent( + "popupshown", + () => { + if (openWithKeyboard) { + hitEnterKey(translateSiteButton, "Opening the popup with keyboard"); + } else { + click(translateSiteButton, "Opening the popup"); + } + }, + onOpenPanel + ); + } + + /** + * Opens the translations panel via the translations button. + * + * @param {object} config + * @param {Function} config.onOpenPanel + * - A function to run as soon as the panel opens. + * @param {boolean} config.openWithKeyboard + * - Open the panel by synthesizing the keyboard. If false, synthesizes the mouse. + */ + static async #openTranslationsPanelViaTranslationsButton({ + onOpenPanel = null, + openWithKeyboard = false, + }) { + logAction(); + const { button } = + await FullPageTranslationsTestUtils.assertTranslationsButton( + { button: true }, + "The translations button is visible." + ); + await FullPageTranslationsTestUtils.waitForTranslationsPopupEvent( + "popupshown", + () => { + if (openWithKeyboard) { + hitEnterKey(button, "Opening the popup with keyboard"); + } else { + click(button, "Opening the popup"); + } + }, + onOpenPanel + ); + } + + /** + * Opens the translations panel settings menu. + * Requires that the translations panel is already open. + */ + static async openTranslationsSettingsMenu() { + logAction(); + const gearIcons = getAllByL10nId("translations-panel-settings-button"); + for (const gearIcon of gearIcons) { + if (gearIcon.hidden) { + continue; + } + click(gearIcon, "Open the settings menu"); + info("Waiting for settings menu to open."); + const manageLanguages = await waitForCondition(() => + maybeGetByL10nId("translations-panel-settings-manage-languages") + ); + ok( + manageLanguages, + "The manage languages item should be visible in the settings menu." + ); + return; + } + } + + /** + * Switches the selected from-language to the provided language tag. + * + * @param {string} langTag - A BCP-47 language tag. + */ + static switchSelectedFromLanguage(langTag) { + logAction(langTag); + const { fromMenuList } = TranslationsPanel.elements; + fromMenuList.value = langTag; + fromMenuList.dispatchEvent(new Event("command")); + } + + /** + * Switches the selected to-language to the provided language tag. + * + * @param {string} langTag - A BCP-47 language tag. + */ + static switchSelectedToLanguage(langTag) { + logAction(langTag); + const { toMenuList } = TranslationsPanel.elements; + toMenuList.value = langTag; + toMenuList.dispatchEvent(new Event("command")); + } + + /** + * XUL popups will fire the popupshown and popuphidden events. These will fire for + * any type of popup in the browser. This function waits for one of those events, and + * checks that the viewId of the popup is PanelUI-profiler + * + * @param {"popupshown" | "popuphidden"} eventName + * @param {Function} callback + * @param {Function} postEventAssertion + * An optional assertion to be made immediately after the event occurs. + * @returns {Promise} + */ + static async waitForTranslationsPopupEvent( + eventName, + callback, + postEventAssertion = null + ) { + // De-lazify the panel elements. + TranslationsPanel.elements; + const panel = document.getElementById("translations-panel"); + if (!panel) { + throw new Error("Unable to find the translations panel element."); + } + const promise = BrowserTestUtils.waitForEvent(panel, eventName); + await callback(); + info("Waiting for the translations panel popup to be shown"); + await promise; + if (postEventAssertion) { + postEventAssertion(); + } + // Wait a single tick on the event loop. + await new Promise(resolve => setTimeout(resolve, 0)); + } +} + +/** + * A class containing test utility functions specific to testing select translations. + */ +class SelectTranslationsTestUtils { + /** + * Opens the context menu then asserts properties of the translate-selection item in the context menu. + * + * @param {Function} runInPage - A content-exposed function to run within the context of the page. + * @param {object} options - Options for how to open the context menu and what properties to assert about the translate-selection item. + * @param {boolean} options.selectFirstParagraph - Selects the first paragraph before opening the context menu. + * @param {boolean} options.selectSpanishParagraph - Selects the Spanish paragraph before opening the context menu. + * This is only available in SPANISH_TEST_PAGE. + * @param {boolean} options.expectMenuItemIsVisible - Whether the translate-selection item is expected to be visible. + * Does not assert visibility if left undefined. + * @param {string} options.expectedTargetLanguage - The target language for translation. + * @param {boolean} options.openAtFirstParagraph - Opens the context menu at the first paragraph in the test page. + * @param {boolean} options.openAtSpanishParagraph - Opens the context menu at the Spanish paragraph in the test page. + * This is only available in SPANISH_TEST_PAGE. + * @param {boolean} options.openAtEnglishHyperlink - Opens the context menu at the English hyperlink in the test page. + * This is only available in SPANISH_TEST_PAGE. + * @param {boolean} options.openAtSpanishHyperlink - Opens the context menu at the Spanish hyperlink in the test page. + * This is only available in SPANISH_TEST_PAGE. + * @param {string} [message] - A message to log to info. + * @throws Throws an error if the properties of the translate-selection item do not match the expected options. + */ + static async assertContextMenuTranslateSelectionItem( + runInPage, + { + selectFirstParagraph, + selectSpanishParagraph, + expectMenuItemIsVisible, + expectedTargetLanguage, + openAtFirstParagraph, + openAtSpanishParagraph, + openAtEnglishHyperlink, + openAtSpanishHyperlink, + }, + message + ) { + logAction(); + + if (message) { + info(message); + } + + await closeTranslationsPanelIfOpen(); + await closeContextMenuIfOpen(); + + await SelectTranslationsTestUtils.openContextMenu(runInPage, { + selectFirstParagraph, + selectSpanishParagraph, + openAtFirstParagraph, + openAtSpanishParagraph, + openAtEnglishHyperlink, + openAtSpanishHyperlink, + }); + + const menuItem = maybeGetById( + "context-translate-selection", + /* ensureIsVisible */ false + ); + + if (expectMenuItemIsVisible !== undefined) { + const visibility = expectMenuItemIsVisible ? "visible" : "hidden"; + assertVisibility({ [visibility]: menuItem }); + } + + if (expectMenuItemIsVisible === true) { + if (expectedTargetLanguage) { + // Target language expected, check for the data-l10n-id with a `{$language}` argument. + const expectedL10nId = + selectFirstParagraph === true || selectSpanishParagraph === true + ? "main-context-menu-translate-selection-to-language" + : "main-context-menu-translate-link-text-to-language"; + await waitForCondition( + () => menuItem.getAttribute("data-l10n-id") === expectedL10nId, + `Waiting for translate-selection context menu item to localize with target language ${expectedTargetLanguage}` + ); + + is( + menuItem.getAttribute("data-l10n-id"), + expectedL10nId, + "Expected the translate-selection context menu item to be localized with a target language." + ); + + const l10nArgs = JSON.parse(menuItem.getAttribute("data-l10n-args")); + is( + l10nArgs.language, + getIntlDisplayName(expectedTargetLanguage), + `Expected the translate-selection context menu item to have the target language '${expectedTargetLanguage}'.` + ); + } else { + // No target language expected, check for the data-l10n-id that has no `{$language}` argument. + const expectedL10nId = + selectFirstParagraph === true || selectSpanishParagraph === true + ? "main-context-menu-translate-selection" + : "main-context-menu-translate-link-text"; + await waitForCondition( + () => menuItem.getAttribute("data-l10n-id") === expectedL10nId, + "Waiting for translate-selection context menu item to localize without target language." + ); + + is( + menuItem.getAttribute("data-l10n-id"), + expectedL10nId, + "Expected the translate-selection context menu item to be localized without a target language." + ); + } + } + + await closeContextMenuIfOpen(); + } + + /** + * Opens the context menu at a specified element on the page, based on the provided options. + * + * @param {Function} runInPage - A content-exposed function to run within the context of the page. + * @param {object} options - Options for opening the context menu. + * @param {boolean} options.selectFirstParagraph - Selects the first paragraph before opening the context menu. + * @param {boolean} options.selectSpanishParagraph - Selects the Spanish paragraph before opening the context menu. + * This is only available in SPANISH_TEST_PAGE. + * @param {boolean} options.openAtFirstParagraph - Opens the context menu at the first paragraph in the test page. + * @param {boolean} options.openAtSpanishParagraph - Opens the context menu at the Spanish paragraph in the test page. + * This is only available in SPANISH_TEST_PAGE. + * @param {boolean} options.openAtEnglishHyperlink - Opens the context menu at the English hyperlink in the test page. + * This is only available in SPANISH_TEST_PAGE. + * @param {boolean} options.openAtSpanishHyperlink - Opens the context menu at the Spanish hyperlink in the test page. + * This is only available in SPANISH_TEST_PAGE. + * @throws Throws an error if no valid option was provided for opening the menu. + */ + static async openContextMenu( + runInPage, + { + selectFirstParagraph, + selectSpanishParagraph, + openAtFirstParagraph, + openAtSpanishParagraph, + openAtEnglishHyperlink, + openAtSpanishHyperlink, + } + ) { + logAction(); + + if (selectFirstParagraph === true) { + await runInPage(async TranslationsTest => { + const { getFirstParagraph } = TranslationsTest.getSelectors(); + const paragraph = getFirstParagraph(); + TranslationsTest.selectContentElement(paragraph); + }); + } + + if (selectSpanishParagraph === true) { + await runInPage(async TranslationsTest => { + const { getSpanishParagraph } = TranslationsTest.getSelectors(); + const paragraph = getSpanishParagraph(); + TranslationsTest.selectContentElement(paragraph); + }); + } + + if (openAtFirstParagraph === true) { + await runInPage(async TranslationsTest => { + const { getFirstParagraph } = TranslationsTest.getSelectors(); + const paragraph = getFirstParagraph(); + await TranslationsTest.rightClickContentElement(paragraph); + }); + return; + } + + if (openAtSpanishParagraph === true) { + await runInPage(async TranslationsTest => { + const { getSpanishParagraph } = TranslationsTest.getSelectors(); + const paragraph = getSpanishParagraph(); + await TranslationsTest.rightClickContentElement(paragraph); + }); + return; + } + + if (openAtEnglishHyperlink === true) { + await runInPage(async TranslationsTest => { + const { getEnglishHyperlink } = TranslationsTest.getSelectors(); + const hyperlink = getEnglishHyperlink(); + await TranslationsTest.rightClickContentElement(hyperlink); + }); + return; + } + + if (openAtSpanishHyperlink === true) { + await runInPage(async TranslationsTest => { + const { getSpanishHyperlink } = TranslationsTest.getSelectors(); + const hyperlink = getSpanishHyperlink(); + await TranslationsTest.rightClickContentElement(hyperlink); + }); + return; + } + + throw new Error( + "openContextMenu() was not provided a declaration for which element to open the menu at." + ); + } +} + +class TranslationsSettingsTestUtils { + /** + * Opens the Translation Settings page by clicking the settings button sent in the argument. + * + * @param {HTMLElement} settingsButton + * @returns {Element} + */ + static async openAboutPreferencesTranslationsSettingsPane(settingsButton) { + const document = gBrowser.selectedBrowser.contentDocument; + + const promise = BrowserTestUtils.waitForEvent( + document, + "paneshown", + false, + event => event.detail.category === "paneTranslations" + ); + + click(settingsButton, "Click settings button"); + await promise; + + const elements = { + backButton: document.getElementById("translations-settings-back-button"), + header: document.getElementById("translations-settings-header"), + translationsSettingsDescription: document.getElementById( + "translations-settings-description" + ), + translateAlwaysHeader: document.getElementById( + "translations-settings-always-translate" + ), + translateNeverHeader: document.getElementById( + "translations-settings-never-translate" + ), + translateAlwaysAddButton: document.getElementById( + "translations-settings-always-translate-list" + ), + translateNeverAddButton: document.getElementById( + "translations-settings-never-translate-list" + ), + translateNeverSiteHeader: document.getElementById( + "translations-settings-never-sites-header" + ), + translateNeverSiteDesc: document.getElementById( + "translations-settings-never-sites" + ), + translateDownloadLanguagesHeader: document.getElementById( + "translations-settings-download-languages" + ), + translateDownloadLanguagesLearnMore: document.getElementById( + "download-languages-learn-more" + ), + }; + + return elements; + } +} -- cgit v1.2.3