summaryrefslogtreecommitdiffstats
path: root/browser/components/translation/test/browser_translation_yandex.js
blob: 10014fa899c3724446eb5a03ffb5fdcdc333fc81 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Test the Yandex Translator client against a mock Yandex service, yandex.sjs.

"use strict";

// The folllowing rejection is left unhandled in some cases. This bug should be
// fixed, but for the moment this file allows a class of rejections.
//
// NOTE: Allowing a whole class of rejections should be avoided. Normally you
//       should use "expectUncaughtRejection" to flag individual failures.
const { PromiseTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/PromiseTestUtils.sys.mjs"
);
PromiseTestUtils.allowMatchingRejectionsGlobally(/NS_ERROR_ILLEGAL_VALUE/);

const kEnginePref = "browser.translation.engine";
const kApiKeyPref = "browser.translation.yandex.apiKeyOverride";
const kDetectLanguagePref = "browser.translation.detectLanguage";
const kShowUIPref = "browser.translation.ui.show";

const { Translation } = ChromeUtils.import(
  "resource:///modules/translation/TranslationParent.jsm"
);

add_setup(async function() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [kEnginePref, "Yandex"],
      [kApiKeyPref, "yandexValidKey"],
      [kDetectLanguagePref, true],
      [kShowUIPref, true],
    ],
  });
});

/**
 * Ensure that the translation engine behaives as expected when translating
 * a sample page.
 */
add_task(async function test_yandex_translation() {
  // Loading the fixture page.
  let url = constructFixtureURL("bug1022725-fr.html");
  let tab = await promiseTestPageLoad(url);

  // Translating the contents of the loaded tab.
  gBrowser.selectedTab = tab;
  let browser = tab.linkedBrowser;

  await SpecialPowers.spawn(browser, [], async function() {
    const { TranslationDocument } = ChromeUtils.import(
      "resource:///modules/translation/TranslationDocument.jsm"
    );
    const { YandexTranslator } = ChromeUtils.import(
      "resource:///modules/translation/YandexTranslator.jsm"
    );

    let client = new YandexTranslator(
      new TranslationDocument(content.document),
      "fr",
      "en"
    );
    let result = await client.translate();

    Assert.ok(result, "There should be a result.");
  });

  gBrowser.removeTab(tab);
});

/**
 * Ensure that Yandex.Translate is propertly attributed.
 */
add_task(async function test_yandex_attribution() {
  // Loading the fixture page.
  let url = constructFixtureURL("bug1022725-fr.html");
  let tab = await promiseTestPageLoad(url);

  info("Show an info bar saying the current page is in French");
  let notif = showTranslationUI(tab, "fr");
  let attribution = notif._getAnonElt("translationEngine").selectedIndex;
  Assert.equal(attribution, 1, "Yandex attribution should be shown.");

  gBrowser.removeTab(tab);
});

add_task(async function test_preference_attribution() {
  let prefUrl = "about:preferences#general";
  let waitPrefLoaded = TestUtils.topicObserved("sync-pane-loaded", () => true);
  let tab = await promiseTestPageLoad(prefUrl);
  await waitPrefLoaded;
  let browser = gBrowser.getBrowserForTab(tab);
  let win = browser.contentWindow;
  let bingAttribution = win.document.getElementById("bingAttribution");
  ok(bingAttribution, "Bing attribution should exist.");
  ok(bingAttribution.hidden, "Bing attribution should be hidden.");

  gBrowser.removeTab(tab);
});

/**
 * A helper function for constructing a URL to a page stored in the
 * local fixture folder.
 *
 * @param filename  Name of a fixture file.
 */
function constructFixtureURL(filename) {
  // Deduce the Mochitest server address in use from a pref that was pre-processed.
  let server = Services.prefs
    .getCharPref("browser.translation.yandex.translateURLOverride")
    .replace("http://", "");
  server = server.substr(0, server.indexOf("/"));
  let url =
    "http://" +
    server +
    "/browser/browser/components/translation/test/fixtures/" +
    filename;
  return url;
}

/**
 * A helper function to open a new tab and wait for its content to load.
 *
 * @param String url  A URL to be loaded in the new tab.
 */
function promiseTestPageLoad(url) {
  return new Promise(resolve => {
    let tab = (gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser, url));
    let browser = gBrowser.selectedBrowser;
    BrowserTestUtils.browserLoaded(
      browser,
      false,
      loadurl => loadurl != "about:blank"
    ).then(() => {
      info("Page loaded: " + browser.currentURI.spec);
      resolve(tab);
    });
  });
}

function showTranslationUI(tab, aDetectedLanguage) {
  let browser = gBrowser.selectedBrowser;
  let actor = browser.browsingContext.currentWindowGlobal.getActor(
    "Translation"
  );
  actor.documentStateReceived({
    state: Translation.STATE_OFFER,
    originalShown: true,
    detectedLanguage: aDetectedLanguage,
  });

  return actor.notificationBox.getNotificationWithValue("translation");
}