summaryrefslogtreecommitdiffstats
path: root/toolkit/components/translations/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:14:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:14:29 +0000
commitfbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8 (patch)
tree4c1ccaf5486d4f2009f9a338a98a83e886e29c97 /toolkit/components/translations/tests
parentReleasing progress-linux version 124.0.1-1~progress7.99u1. (diff)
downloadfirefox-fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8.tar.xz
firefox-fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8.zip
Merging upstream version 125.0.1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/translations/tests')
-rw-r--r--toolkit/components/translations/tests/browser/browser_translations_translation_document.js100
-rw-r--r--toolkit/components/translations/tests/browser/shared-head.js142
-rw-r--r--toolkit/components/translations/tests/browser/translations-test.mjs2
3 files changed, 174 insertions, 70 deletions
diff --git a/toolkit/components/translations/tests/browser/browser_translations_translation_document.js b/toolkit/components/translations/tests/browser/browser_translations_translation_document.js
index 9a00da9ccf..d3d56fd387 100644
--- a/toolkit/components/translations/tests/browser/browser_translations_translation_document.js
+++ b/toolkit/components/translations/tests/browser/browser_translations_translation_document.js
@@ -51,6 +51,7 @@ async function createDoc(html, options) {
/**
* Test utility to check that the document matches the expected markup
*
+ * @param {string} message
* @param {string} html
*/
async function htmlMatches(message, html) {
@@ -720,6 +721,105 @@ add_task(async function test_presumed_inlines3() {
cleanup();
});
+/**
+ * Test the display "none" properties properly subdivide in block elements.
+ */
+add_task(async function test_display_none() {
+ const { translate, htmlMatches, cleanup } = await createDoc(
+ /* html */ `
+ <p>
+ This is some text.
+ <span>It has inline elements</span>
+ <style></style>
+ </p>
+ `,
+ { mockedTranslatorPort: createBatchedMockedTranslatorPort() }
+ );
+
+ translate();
+
+ // Note: The bergamot translator does not translate style elements, while our fake
+ // translator does translate the inside of style elements. That is why in the assertion
+ // here the style element is blank rather than containing style.
+ await htmlMatches(
+ "Display none",
+ /* html */ `
+ <p>
+ aaaa aa aaaa aaaa.
+ <span data-moz-translations-id="0">
+ aa aaa aaaaaa aaaaaaaa
+ </span>
+ <style data-moz-translations-id="1">
+ </style>
+ </p>
+ `
+ );
+
+ cleanup();
+});
+
+/**
+ * Test the display "none" properties properly subdivide in block elements.
+ *
+ * TODO - See Bug 1885235
+ *
+ * This assertion is wrong, as our test suite doesn't properly compute the style for
+ * elements. The div with "display; none;" is still block, not "none".
+ */
+add_task(async function test_display_none_div() {
+ const { translate, htmlMatches, cleanup } = await createDoc(
+ /* html */ `
+ <div>
+ <span>
+ Start of inline text
+ </span>
+ <div style="display: none;">
+ hidden portion of
+ </div>
+ <span>
+ rest of inline text.
+ </span>
+ </div>
+ `,
+ { mockedTranslatorPort: createBatchedMockedTranslatorPort() }
+ );
+
+ translate();
+
+ // eslint-disable-next-line no-unused-vars
+ const _realExpectedResults = /* html */ `
+ <div>
+ <span>
+ aaaaa aa aaaaaa aaaa
+ </span>
+ <div style="display: none;">
+ aaaaaa aaaaaaa aa
+ </div>
+ <span>
+ aaaa aa aaaaaa aaaa.
+ </span>
+ </div>
+ `;
+
+ const currentResults = /* html */ `
+ <div>
+ <span>
+ aaaaa aa aaaaaa aaaa
+ </span>
+ <div style="display: none;">
+ bbbbbb bbbbbbb bb
+ </div>
+ <span>
+ cccc cc cccccc cccc.
+ </span>
+ </div>
+ `;
+
+ await htmlMatches("Display none", currentResults);
+
+ cleanup();
+});
+
add_task(async function test_chunking_large_text() {
const { translate, htmlMatches, cleanup } = await createDoc(
/* html */ `
diff --git a/toolkit/components/translations/tests/browser/shared-head.js b/toolkit/components/translations/tests/browser/shared-head.js
index bad8e48a1b..82b3e783a7 100644
--- a/toolkit/components/translations/tests/browser/shared-head.js
+++ b/toolkit/components/translations/tests/browser/shared-head.js
@@ -3,6 +3,13 @@
"use strict";
+/**
+ * @type {import("../../../ml/content/EngineProcess.sys.mjs")}
+ */
+const { EngineProcess } = ChromeUtils.importESModule(
+ "chrome://global/content/ml/EngineProcess.sys.mjs"
+);
+
// Avoid about:blank's non-standard behavior.
const BLANK_PAGE =
"data:text/html;charset=utf-8,<!DOCTYPE html><title>Blank</title>Blank page";
@@ -134,7 +141,7 @@ async function openAboutTranslations({
BrowserTestUtils.removeTab(tab);
await removeMocks();
- await TranslationsParent.destroyEngineProcess();
+ await EngineProcess.destroyTranslationsEngine();
await SpecialPowers.popPrefEnv();
}
@@ -142,6 +149,7 @@ async function openAboutTranslations({
/**
* Naively prettify's html based on the opening and closing tags. This is not robust
* for general usage, but should be adequate for these tests.
+ *
* @param {string} html
* @returns {string}
*/
@@ -340,11 +348,23 @@ function getTranslationsParent() {
}
/**
- * Closes the context menu if it is open.
+ * Closes all open panels and menu popups related to Translations.
*/
-function closeContextMenuIfOpen() {
- return waitForCondition(async () => {
- const contextMenu = document.getElementById("contentAreaContextMenu");
+async function closeAllOpenPanelsAndMenus() {
+ await closeSettingsMenuIfOpen();
+ await closeFullPageTranslationsPanelIfOpen();
+ await closeSelectTranslationsPanelIfOpen();
+ await closeContextMenuIfOpen();
+}
+
+/**
+ * Closes the popup element with the given Id if it is open.
+ *
+ * @param {string} popupElementId
+ */
+async function closePopupIfOpen(popupElementId) {
+ await waitForCondition(async () => {
+ const contextMenu = document.getElementById(popupElementId);
if (!contextMenu) {
return true;
}
@@ -362,50 +382,31 @@ function closeContextMenuIfOpen() {
}
/**
+ * Closes the context menu if it is open.
+ */
+async function closeContextMenuIfOpen() {
+ await closePopupIfOpen("contentAreaContextMenu");
+}
+
+/**
* Closes the translations panel settings menu if it is open.
*/
-function closeSettingsMenuIfOpen() {
- return waitForCondition(async () => {
- const settings = document.getElementById(
- "translations-panel-settings-menupopup"
- );
- if (!settings) {
- return true;
- }
- if (settings.state === "closed") {
- return true;
- }
- let popuphiddenPromise = BrowserTestUtils.waitForEvent(
- settings,
- "popuphidden"
- );
- PanelMultiView.hidePopup(settings);
- await popuphiddenPromise;
- return false;
- });
+async function closeSettingsMenuIfOpen() {
+ await closePopupIfOpen("full-page-translations-panel-settings-menupopup");
}
/**
* Closes the translations panel if it is open.
*/
-async function closeTranslationsPanelIfOpen() {
- await closeSettingsMenuIfOpen();
- return waitForCondition(async () => {
- const panel = document.getElementById("translations-panel");
- if (!panel) {
- return true;
- }
- if (panel.state === "closed") {
- return true;
- }
- let popuphiddenPromise = BrowserTestUtils.waitForEvent(
- panel,
- "popuphidden"
- );
- PanelMultiView.hidePopup(panel);
- await popuphiddenPromise;
- return false;
- });
+async function closeFullPageTranslationsPanelIfOpen() {
+ await closePopupIfOpen("full-page-translations-panel");
+}
+
+/**
+ * Closes the translations panel if it is open.
+ */
+async function closeSelectTranslationsPanelIfOpen() {
+ await closePopupIfOpen("select-translations-panel");
}
/**
@@ -442,10 +443,9 @@ async function setupActorTest({
actor,
remoteClients,
async cleanup() {
+ await closeAllOpenPanelsAndMenus();
await loadBlankPage();
- await TranslationsParent.destroyEngineProcess();
- await closeTranslationsPanelIfOpen();
- await closeContextMenuIfOpen();
+ await EngineProcess.destroyTranslationsEngine();
BrowserTestUtils.removeTab(tab);
await removeMocks();
TestTranslationsTelemetry.reset();
@@ -500,7 +500,7 @@ async function loadTestPage({
}) {
info(`Loading test page starting at url: ${page}`);
// Ensure no engine is being carried over from a previous test.
- await TranslationsParent.destroyEngineProcess();
+ await EngineProcess.destroyTranslationsEngine();
Services.fog.testResetFOG();
await SpecialPowers.pushPrefEnv({
set: [
@@ -552,7 +552,7 @@ async function loadTestPage({
if (autoOffer && TranslationsParent.shouldAlwaysOfferTranslations()) {
info("Waiting for the popup to be automatically shown.");
await waitForCondition(() => {
- const panel = document.getElementById("translations-panel");
+ const panel = document.getElementById("full-page-translations-panel");
return panel && panel.state === "open";
});
}
@@ -585,10 +585,9 @@ async function loadTestPage({
* @returns {Promise<void>}
*/
async cleanup() {
+ await closeAllOpenPanelsAndMenus();
await loadBlankPage();
- await TranslationsParent.destroyEngineProcess();
- await closeTranslationsPanelIfOpen();
- await closeContextMenuIfOpen();
+ await EngineProcess.destroyTranslationsEngine();
await removeMocks();
Services.fog.testResetFOG();
TranslationsParent.testAutomaticPopup = false;
@@ -658,7 +657,8 @@ async function captureTranslationsError(callback) {
/**
* Load a test page and run
- * @param {Object} options - The options for `loadTestPage` plus a `runInPage` function.
+ *
+ * @param {object} options - The options for `loadTestPage` plus a `runInPage` function.
*/
async function autoTranslatePage(options) {
const { prefs, languagePairs, ...otherOptions } = options;
@@ -676,6 +676,10 @@ async function autoTranslatePage(options) {
}
/**
+ * @typedef {ReturnType<createAttachmentMock>} AttachmentMock
+ */
+
+/**
* @param {RemoteSettingsClient} client
* @param {string} mockedCollectionName - The name of the mocked collection without
* the incrementing "id" part. This is provided so that attachments can be asserted
@@ -826,7 +830,7 @@ let _remoteSettingsMockId = 0;
* Creates a local RemoteSettingsClient for use within tests.
*
* @param {boolean} autoDownloadFromRemoteSettings
- * @param {Object[]} langPairs
+ * @param {object[]} langPairs
* @returns {RemoteSettingsClient}
*/
async function createTranslationModelsRemoteClient(
@@ -980,7 +984,7 @@ function hitEnterKey(button, message) {
*
* @see assertVisibility
*
- * @param {Object} options
+ * @param {object} options
* @param {string} options.message
* @param {Record<string, Element[]>} options.visible
* @param {Record<string, Element[]>} options.hidden
@@ -1011,7 +1015,7 @@ async function ensureVisibility({ message = null, visible = {}, hidden = {} }) {
/**
* Asserts that the provided elements are either visible or hidden.
*
- * @param {Object} options
+ * @param {object} options
* @param {string} options.message
* @param {Record<string, Element[]>} options.visible
* @param {Record<string, Element[]>} options.hidden
@@ -1066,10 +1070,9 @@ async function setupAboutPreferences(
const elements = await selectAboutPreferencesElements();
async function cleanup() {
+ await closeAllOpenPanelsAndMenus();
await loadBlankPage();
- await TranslationsParent.destroyEngineProcess();
- await closeTranslationsPanelIfOpen();
- await closeContextMenuIfOpen();
+ await EngineProcess.destroyTranslationsEngine();
BrowserTestUtils.removeTab(tab);
await removeMocks();
await SpecialPowers.popPrefEnv();
@@ -1137,8 +1140,8 @@ class TestTranslationsTelemetry {
* Asserts qualities about a counter telemetry metric.
*
* @param {string} name - The name of the metric.
- * @param {Object} counter - The Glean counter object.
- * @param {Object} expectedCount - The expected value of the counter.
+ * @param {object} counter - The Glean counter object.
+ * @param {object} expectedCount - The expected value of the counter.
*/
static async assertCounter(name, counter, expectedCount) {
// Ensures that glean metrics are collected from all child processes
@@ -1155,16 +1158,16 @@ class TestTranslationsTelemetry {
/**
* Asserts qualities about an event telemetry metric.
*
- * @param {string} name - The name of the metric.
- * @param {Object} event - The Glean event object.
- * @param {Object} expectations - The test expectations.
+ * @param {object} event - The Glean event object.
+ * @param {object} expectations - The test expectations.
* @param {number} expectations.expectedEventCount - The expected count of events.
* @param {boolean} expectations.expectNewFlowId
+ * @param {boolean} [expectations.expectFirstInteraction]
* - Expects the flowId to be different than the previous flowId if true,
* and expects it to be the same if false.
- * @param {Array<function>} [expectations.allValuePredicates=[]]
+ * @param {Array<Function>} [expectations.allValuePredicates=[]]
* - An array of function predicates to assert for all event values.
- * @param {Array<function>} [expectations.finalValuePredicates=[]]
+ * @param {Array<Function>} [expectations.finalValuePredicates=[]]
* - An array of function predicates to assert for only the final event value.
*/
static async assertEvent(
@@ -1264,8 +1267,8 @@ class TestTranslationsTelemetry {
* Asserts qualities about a rate telemetry metric.
*
* @param {string} name - The name of the metric.
- * @param {Object} rate - The Glean rate object.
- * @param {Object} expectations - The test expectations.
+ * @param {object} rate - The Glean rate object.
+ * @param {object} expectations - The test expectations.
* @param {number} expectations.expectedNumerator - The expected value of the numerator.
* @param {number} expectations.expectedDenominator - The expected value of the denominator.
*/
@@ -1295,7 +1298,7 @@ class TestTranslationsTelemetry {
* Provide longer defaults for the waitForCondition.
*
* @param {Function} callback
- * @param {string} messages
+ * @param {string} message
*/
function waitForCondition(callback, message) {
const interval = 100;
@@ -1346,9 +1349,10 @@ function getNeverTranslateSitesFromPerms() {
/**
* Opens a dialog window for about:preferences
+ *
* @param {string} dialogUrl - The URL of the dialog window
* @param {Function} callback - The function to open the dialog via UI
- * @returns {Object} The dialog window object
+ * @returns {object} The dialog window object
*/
async function waitForOpenDialogWindow(dialogUrl, callback) {
const dialogLoaded = promiseLoadSubDialog(dialogUrl);
@@ -1360,7 +1364,7 @@ async function waitForOpenDialogWindow(dialogUrl, callback) {
/**
* Closes an open dialog window and waits for it to close.
*
- * @param {Object} dialogWindow
+ * @param {object} dialogWindow
*/
async function waitForCloseDialogWindow(dialogWindow) {
const closePromise = BrowserTestUtils.waitForEvent(
diff --git a/toolkit/components/translations/tests/browser/translations-test.mjs b/toolkit/components/translations/tests/browser/translations-test.mjs
index 3e16be57e9..a740a2d1cc 100644
--- a/toolkit/components/translations/tests/browser/translations-test.mjs
+++ b/toolkit/components/translations/tests/browser/translations-test.mjs
@@ -60,7 +60,7 @@ export function getSelectors() {
* Provide longer defaults for the waitForCondition.
*
* @param {Function} callback
- * @param {string} messages
+ * @param {string} message
*/
function waitForCondition(callback, message) {
const interval = 100;