summaryrefslogtreecommitdiffstats
path: root/browser/components/translation/test/browser_translation_infobar.js
blob: feef5c78c61a2e1fffd6171830351890272f13f9 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* 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/. */

// tests the translation infobar, using a fake 'Translation' implementation.

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

const kDetectLanguagePref = "browser.translation.detectLanguage";
const kShowUIPref = "browser.translation.ui.show";

const text =
  "Il y a aujourd'hui trois cent quarante-huit ans six mois et dix-neuf jours que les Parisiens s'éveillèrent au bruit de toutes les cloches sonnant à grande volée dans la triple enceinte de la Cité, de l'Université et de la Ville.";
const EXAMPLE_URL =
  "http://example.com/document-builder.sjs?html=<html><body>" +
  text +
  "</body></html>";

// Create a subclass that overrides the translation functions. This can be
// instantiated separately from the normal actor creation process. This will
// allow testing translations even when the browser.translation.detectLanguage
// preference is disabled.
class TranslationStub extends TranslationParent {
  constructor(browser) {
    super();
    this._browser = browser;
  }

  get browser() {
    return this._browser;
  }

  sendAsyncMessage(name, data) {}

  translate(aFrom, aTo) {
    this.state = Translation.STATE_TRANSLATING;
    this.translatedFrom = aFrom;
    this.translatedTo = aTo;
  }

  _reset() {
    this.translatedFrom = "";
    this.translatedTo = "";
  }

  failTranslation() {
    this.state = Translation.STATE_ERROR;
    this._reset();
  }

  finishTranslation() {
    this.showTranslatedContent();
    this.state = Translation.STATE_TRANSLATED;
    this._reset();
  }
}

function showTranslationUI(aDetectedLanguage) {
  let browser = gBrowser.selectedBrowser;
  let translation = new TranslationStub(browser);
  translation.documentStateReceived({
    state: Translation.STATE_OFFER,
    originalShown: true,
    detectedLanguage: aDetectedLanguage,
  });
  return translation.notificationBox.getNotificationWithValue("translation");
}

function hasTranslationInfoBar() {
  return !!gBrowser
    .getNotificationBox()
    .getNotificationWithValue("translation");
}

function checkURLBarIcon(aExpectTranslated = false) {
  is(
    !PopupNotifications.getNotification("translate"),
    aExpectTranslated,
    "translate icon " + (aExpectTranslated ? "not " : "") + "shown"
  );
  is(
    !!PopupNotifications.getNotification("translated"),
    aExpectTranslated,
    "translated icon " + (aExpectTranslated ? "" : "not ") + "shown"
  );
}

add_task(async function test_infobar() {
  await SpecialPowers.pushPrefEnv({
    set: [[kShowUIPref, true]],
  });

  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "data:text/plain,test page"
  );

  TranslationStub.browser = gBrowser.selectedBrowser;

  info("Show an info bar saying the current page is in French");
  let notif = showTranslationUI("fr");
  is(
    notif.state,
    Translation.STATE_OFFER,
    "the infobar is offering translation"
  );
  is(
    notif._getAnonElt("detectedLanguage").value,
    "fr",
    "The detected language is displayed"
  );
  checkURLBarIcon();

  info("Click the 'Translate' button");
  notif._getAnonElt("translate").click();
  is(
    notif.state,
    Translation.STATE_TRANSLATING,
    "the infobar is in the translating state"
  );
  ok(
    !!notif.translation.translatedFrom,
    "Translation.translate has been called"
  );
  is(notif.translation.translatedFrom, "fr", "from language correct");
  is(
    notif.translation.translatedTo,
    Translation.defaultTargetLanguage,
    "from language correct"
  );
  checkURLBarIcon();

  info("Make the translation fail and check we are in the error state.");
  notif.translation.failTranslation();
  is(notif.state, Translation.STATE_ERROR, "infobar in the error state");
  checkURLBarIcon();

  info("Click the try again button");
  notif._getAnonElt("tryAgain").click();
  is(
    notif.state,
    Translation.STATE_TRANSLATING,
    "infobar in the translating state"
  );
  ok(
    !!notif.translation.translatedFrom,
    "Translation.translate has been called"
  );
  is(notif.translation.translatedFrom, "fr", "from language correct");
  is(
    notif.translation.translatedTo,
    Translation.defaultTargetLanguage,
    "from language correct"
  );
  checkURLBarIcon();

  info(
    "Make the translation succeed and check we are in the 'translated' state."
  );
  notif.translation.finishTranslation();
  is(
    notif.state,
    Translation.STATE_TRANSLATED,
    "infobar in the translated state"
  );
  checkURLBarIcon(true);

  info("Test 'Show original' / 'Show Translation' buttons.");
  // First check 'Show Original' is visible and 'Show Translation' is hidden.
  ok(
    !notif._getAnonElt("showOriginal").hidden,
    "'Show Original' button visible"
  );
  ok(
    notif._getAnonElt("showTranslation").hidden,
    "'Show Translation' button hidden"
  );
  // Click the button.
  notif._getAnonElt("showOriginal").click();
  // Check that the url bar icon shows the original content is displayed.
  checkURLBarIcon();
  // And the 'Show Translation' button is now visible.
  ok(notif._getAnonElt("showOriginal").hidden, "'Show Original' button hidden");
  ok(
    !notif._getAnonElt("showTranslation").hidden,
    "'Show Translation' button visible"
  );
  // Click the 'Show Translation' button
  notif._getAnonElt("showTranslation").click();
  // Check that the url bar icon shows the page is translated.
  checkURLBarIcon(true);
  // Check that the 'Show Original' button is visible again.
  ok(
    !notif._getAnonElt("showOriginal").hidden,
    "'Show Original' button visible"
  );
  ok(
    notif._getAnonElt("showTranslation").hidden,
    "'Show Translation' button hidden"
  );

  info("Check that changing the source language causes a re-translation");
  let from = notif._getAnonElt("fromLanguage");
  from.value = "es";
  from.doCommand();
  is(
    notif.state,
    Translation.STATE_TRANSLATING,
    "infobar in the translating state"
  );
  ok(
    !!notif.translation.translatedFrom,
    "Translation.translate has been called"
  );
  is(notif.translation.translatedFrom, "es", "from language correct");
  is(
    notif.translation.translatedTo,
    Translation.defaultTargetLanguage,
    "to language correct"
  );
  // We want to show the 'translated' icon while re-translating,
  // because we are still displaying the previous translation.
  checkURLBarIcon(true);
  notif.translation.finishTranslation();
  checkURLBarIcon(true);

  info("Check that changing the target language causes a re-translation");
  let to = notif._getAnonElt("toLanguage");
  to.value = "pl";
  to.doCommand();
  is(
    notif.state,
    Translation.STATE_TRANSLATING,
    "infobar in the translating state"
  );
  ok(
    !!notif.translation.translatedFrom,
    "Translation.translate has been called"
  );
  is(notif.translation.translatedFrom, "es", "from language correct");
  is(notif.translation.translatedTo, "pl", "to language correct");
  checkURLBarIcon(true);
  notif.translation.finishTranslation();
  checkURLBarIcon(true);

  // Cleanup.
  notif.close();

  info(
    "Reopen the info bar to check that it's possible to override the detected language."
  );
  notif = showTranslationUI("fr");
  is(
    notif.state,
    Translation.STATE_OFFER,
    "the infobar is offering translation"
  );
  is(
    notif._getAnonElt("detectedLanguage").value,
    "fr",
    "The detected language is displayed"
  );
  // Change the language and click 'Translate'
  notif._getAnonElt("detectedLanguage").value = "ja";
  notif._getAnonElt("translate").click();
  is(
    notif.state,
    Translation.STATE_TRANSLATING,
    "the infobar is in the translating state"
  );
  ok(
    !!notif.translation.translatedFrom,
    "Translation.translate has been called"
  );
  is(notif.translation.translatedFrom, "ja", "from language correct");
  notif.close();

  info("Reopen to check the 'Not Now' button closes the notification.");
  notif = showTranslationUI("fr");
  is(hasTranslationInfoBar(), true, "there's a 'translate' notification");
  notif._getAnonElt("notNow").click();
  is(
    hasTranslationInfoBar(),
    false,
    "no 'translate' notification after clicking 'not now'"
  );

  info("Reopen to check the url bar icon closes the notification.");
  notif = showTranslationUI("fr");
  is(hasTranslationInfoBar(), true, "there's a 'translate' notification");
  PopupNotifications.getNotification("translate").anchorElement.click();
  is(
    hasTranslationInfoBar(),
    false,
    "no 'translate' notification after clicking the url bar icon"
  );

  info("Check that clicking the url bar icon reopens the info bar");
  checkURLBarIcon();
  // Clicking the anchor element causes a 'showing' event to be sent
  // asynchronously to our callback that will then show the infobar.
  PopupNotifications.getNotification("translate").anchorElement.click();

  await BrowserTestUtils.waitForCondition(
    hasTranslationInfoBar,
    "timeout waiting for the info bar to reappear"
  );

  ok(hasTranslationInfoBar(), "there's a 'translate' notification");

  await BrowserTestUtils.removeTab(tab);
});

add_task(async function test_infobar_using_page() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [kDetectLanguagePref, true],
      [kShowUIPref, true],
    ],
  });

  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, EXAMPLE_URL);

  await BrowserTestUtils.waitForCondition(
    hasTranslationInfoBar,
    "timeout waiting for the info bar to reappear"
  );

  let notificationBox = gBrowser.getNotificationBox(tab.linkedBrowser);
  let notif = notificationBox.getNotificationWithValue("translation");
  is(
    notif.state,
    Translation.STATE_OFFER,
    "the infobar is offering translation"
  );
  is(
    notif._getAnonElt("detectedLanguage").value,
    "fr",
    "The detected language is displayed"
  );
  checkURLBarIcon();

  await BrowserTestUtils.removeTab(tab);
});