summaryrefslogtreecommitdiffstats
path: root/browser/components/translations/tests/browser/browser_translations_select_context_menu_with_hyperlink.js
blob: cefd83f046a43c35dd1c6e6a22a6ad834c094d11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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();
  }
);