2014-03-21 11:07:38 -07:00
|
|
|
/* 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.
|
|
|
|
|
2014-06-03 09:08:35 -07:00
|
|
|
let tmp = {};
|
|
|
|
Cu.import("resource:///modules/translation/Translation.jsm", tmp);
|
|
|
|
let {Translation} = tmp;
|
2014-04-07 10:39:07 -07:00
|
|
|
|
2014-05-27 15:45:45 -07:00
|
|
|
const kShowUIPref = "browser.translation.ui.show";
|
|
|
|
|
2014-04-07 10:39:07 -07:00
|
|
|
function waitForCondition(condition, nextTest, errorMsg) {
|
|
|
|
var tries = 0;
|
|
|
|
var interval = setInterval(function() {
|
|
|
|
if (tries >= 30) {
|
|
|
|
ok(false, errorMsg);
|
|
|
|
moveOn();
|
|
|
|
}
|
|
|
|
var conditionPassed;
|
|
|
|
try {
|
|
|
|
conditionPassed = condition();
|
|
|
|
} catch (e) {
|
|
|
|
ok(false, e + "\n" + e.stack);
|
|
|
|
conditionPassed = false;
|
|
|
|
}
|
|
|
|
if (conditionPassed) {
|
|
|
|
moveOn();
|
|
|
|
}
|
|
|
|
tries++;
|
|
|
|
}, 100);
|
|
|
|
var moveOn = function() { clearInterval(interval); nextTest(); };
|
|
|
|
}
|
|
|
|
|
|
|
|
var TranslationStub = {
|
2014-03-21 11:07:38 -07:00
|
|
|
translate: function(aFrom, aTo) {
|
2014-06-09 07:50:58 -07:00
|
|
|
this.state = Translation.STATE_TRANSLATING;
|
2014-04-07 10:39:07 -07:00
|
|
|
this.translatedFrom = aFrom;
|
|
|
|
this.translatedTo = aTo;
|
2014-03-21 11:07:38 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
_reset: function() {
|
2014-04-07 10:39:07 -07:00
|
|
|
this.translatedFrom = "";
|
|
|
|
this.translatedTo = "";
|
2014-03-21 11:07:38 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
failTranslation: function() {
|
2014-06-09 07:50:58 -07:00
|
|
|
this.state = Translation.STATE_ERROR;
|
2014-03-21 11:07:38 -07:00
|
|
|
this._reset();
|
|
|
|
},
|
|
|
|
|
|
|
|
finishTranslation: function() {
|
2014-04-07 10:39:07 -07:00
|
|
|
this.showTranslatedContent();
|
2014-06-09 07:50:58 -07:00
|
|
|
this.state = Translation.STATE_TRANSLATED;
|
2014-03-21 11:07:38 -07:00
|
|
|
this._reset();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-16 15:02:33 -07:00
|
|
|
function showTranslationUI(aDetectedLanguage) {
|
|
|
|
let browser = gBrowser.selectedBrowser;
|
2014-06-09 07:50:58 -07:00
|
|
|
Translation.documentStateReceived(browser, {state: Translation.STATE_OFFER,
|
|
|
|
originalShown: true,
|
|
|
|
detectedLanguage: aDetectedLanguage});
|
2014-04-16 15:02:33 -07:00
|
|
|
let ui = browser.translationUI;
|
|
|
|
for (let name of ["translate", "_reset", "failTranslation", "finishTranslation"])
|
|
|
|
ui[name] = TranslationStub[name];
|
|
|
|
return ui.notificationBox.getNotificationWithValue("translation");
|
|
|
|
}
|
2014-04-07 10:39:07 -07:00
|
|
|
|
2014-05-16 08:17:06 -07:00
|
|
|
function hasTranslationInfoBar() {
|
|
|
|
return !!gBrowser.getNotificationBox().getNotificationWithValue("translation");
|
|
|
|
}
|
|
|
|
|
2014-03-21 11:07:38 -07:00
|
|
|
function test() {
|
|
|
|
waitForExplicitFinish();
|
|
|
|
|
2014-05-27 15:45:45 -07:00
|
|
|
Services.prefs.setBoolPref(kShowUIPref, true);
|
2014-04-07 10:39:07 -07:00
|
|
|
let tab = gBrowser.addTab();
|
|
|
|
gBrowser.selectedTab = tab;
|
|
|
|
tab.linkedBrowser.addEventListener("load", function onload() {
|
|
|
|
tab.linkedBrowser.removeEventListener("load", onload, true);
|
|
|
|
TranslationStub.browser = gBrowser.selectedBrowser;
|
|
|
|
registerCleanupFunction(function () {
|
|
|
|
gBrowser.removeTab(tab);
|
2014-05-27 15:45:45 -07:00
|
|
|
Services.prefs.clearUserPref(kShowUIPref);
|
2014-04-07 10:39:07 -07:00
|
|
|
});
|
|
|
|
run_tests(() => {
|
|
|
|
finish();
|
|
|
|
});
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
content.location = "data:text/plain,test page";
|
|
|
|
}
|
2014-03-21 11:07:38 -07:00
|
|
|
|
2014-04-07 10:39:07 -07:00
|
|
|
function checkURLBarIcon(aExpectTranslated = false) {
|
|
|
|
is(!PopupNotifications.getNotification("translate"), aExpectTranslated,
|
|
|
|
"translate icon " + (aExpectTranslated ? "not " : "") + "shown");
|
|
|
|
is(!!PopupNotifications.getNotification("translated"), aExpectTranslated,
|
|
|
|
"translated icon " + (aExpectTranslated ? "" : "not ") + "shown");
|
|
|
|
}
|
2014-03-21 11:07:38 -07:00
|
|
|
|
2014-04-07 10:39:07 -07:00
|
|
|
function run_tests(aFinishCallback) {
|
|
|
|
info("Show an info bar saying the current page is in French");
|
2014-04-16 15:02:33 -07:00
|
|
|
let notif = showTranslationUI("fr");
|
2014-06-09 07:50:58 -07:00
|
|
|
is(notif.state, Translation.STATE_OFFER, "the infobar is offering translation");
|
2014-04-07 10:39:07 -07:00
|
|
|
is(notif._getAnonElt("detectedLanguage").value, "fr", "The detected language is displayed");
|
|
|
|
checkURLBarIcon();
|
2014-03-21 11:07:38 -07:00
|
|
|
|
2014-04-07 10:39:07 -07:00
|
|
|
info("Click the 'Translate' button");
|
|
|
|
notif._getAnonElt("translate").click();
|
2014-06-09 07:50:58 -07:00
|
|
|
is(notif.state, Translation.STATE_TRANSLATING, "the infobar is in the translating state");
|
2014-04-16 15:02:33 -07:00
|
|
|
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");
|
2014-04-07 10:39:07 -07:00
|
|
|
checkURLBarIcon();
|
|
|
|
|
|
|
|
info("Make the translation fail and check we are in the error state.");
|
2014-04-16 15:02:33 -07:00
|
|
|
notif.translation.failTranslation();
|
2014-06-09 07:50:58 -07:00
|
|
|
is(notif.state, Translation.STATE_ERROR, "infobar in the error state");
|
2014-04-07 10:39:07 -07:00
|
|
|
checkURLBarIcon();
|
|
|
|
|
|
|
|
info("Click the try again button");
|
2014-03-21 11:07:38 -07:00
|
|
|
notif._getAnonElt("tryAgain").click();
|
2014-06-09 07:50:58 -07:00
|
|
|
is(notif.state, Translation.STATE_TRANSLATING, "infobar in the translating state");
|
2014-04-16 15:02:33 -07:00
|
|
|
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");
|
2014-04-07 10:39:07 -07:00
|
|
|
checkURLBarIcon();
|
|
|
|
|
|
|
|
info("Make the translation succeed and check we are in the 'translated' state.");
|
2014-04-16 15:02:33 -07:00
|
|
|
notif.translation.finishTranslation();
|
2014-06-09 07:50:58 -07:00
|
|
|
is(notif.state, Translation.STATE_TRANSLATED, "infobar in the translated state");
|
2014-04-07 10:39:07 -07:00
|
|
|
checkURLBarIcon(true);
|
|
|
|
|
|
|
|
info("Test 'Show original' / 'Show Translation' buttons.");
|
2014-03-21 11:07:38 -07:00
|
|
|
// 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();
|
2014-04-07 10:39:07 -07:00
|
|
|
// Check that the url bar icon shows the original content is displayed.
|
|
|
|
checkURLBarIcon();
|
2014-03-21 11:07:38 -07:00
|
|
|
// 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();
|
2014-04-07 10:39:07 -07:00
|
|
|
// Check that the url bar icon shows the page is translated.
|
|
|
|
checkURLBarIcon(true);
|
2014-03-21 11:07:38 -07:00
|
|
|
// 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");
|
|
|
|
|
2014-04-07 10:39:07 -07:00
|
|
|
info("Check that changing the source language causes a re-translation");
|
2014-03-21 11:07:38 -07:00
|
|
|
let from = notif._getAnonElt("fromLanguage");
|
|
|
|
from.value = "es";
|
|
|
|
from.doCommand();
|
2014-06-09 07:50:58 -07:00
|
|
|
is(notif.state, Translation.STATE_TRANSLATING, "infobar in the translating state");
|
2014-04-16 15:02:33 -07:00
|
|
|
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");
|
2014-04-07 10:39:07 -07:00
|
|
|
// We want to show the 'translated' icon while re-translating,
|
|
|
|
// because we are still displaying the previous translation.
|
|
|
|
checkURLBarIcon(true);
|
2014-04-16 15:02:33 -07:00
|
|
|
notif.translation.finishTranslation();
|
2014-04-07 10:39:07 -07:00
|
|
|
checkURLBarIcon(true);
|
|
|
|
|
|
|
|
info("Check that changing the target language causes a re-translation");
|
2014-03-21 11:07:38 -07:00
|
|
|
let to = notif._getAnonElt("toLanguage");
|
|
|
|
to.value = "pl";
|
|
|
|
to.doCommand();
|
2014-06-09 07:50:58 -07:00
|
|
|
is(notif.state, Translation.STATE_TRANSLATING, "infobar in the translating state");
|
2014-04-16 15:02:33 -07:00
|
|
|
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");
|
2014-04-07 10:39:07 -07:00
|
|
|
checkURLBarIcon(true);
|
2014-04-16 15:02:33 -07:00
|
|
|
notif.translation.finishTranslation();
|
2014-04-07 10:39:07 -07:00
|
|
|
checkURLBarIcon(true);
|
2014-03-21 11:07:38 -07:00
|
|
|
|
|
|
|
// Cleanup.
|
|
|
|
notif.close();
|
|
|
|
|
2014-04-07 10:39:07 -07:00
|
|
|
info("Reopen the info bar to check that it's possible to override the detected language.");
|
2014-04-16 15:02:33 -07:00
|
|
|
notif = showTranslationUI("fr");
|
2014-06-09 07:50:58 -07:00
|
|
|
is(notif.state, Translation.STATE_OFFER, "the infobar is offering translation");
|
2014-03-21 11:07:38 -07:00
|
|
|
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();
|
2014-06-09 07:50:58 -07:00
|
|
|
is(notif.state, Translation.STATE_TRANSLATING, "the infobar is in the translating state");
|
2014-04-16 15:02:33 -07:00
|
|
|
ok(!!notif.translation.translatedFrom, "Translation.translate has been called");
|
|
|
|
is(notif.translation.translatedFrom, "ja", "from language correct");
|
2014-03-21 11:07:38 -07:00
|
|
|
notif.close();
|
|
|
|
|
2014-04-07 10:39:07 -07:00
|
|
|
info("Reopen to check the 'Not Now' button closes the notification.");
|
2014-04-16 15:02:33 -07:00
|
|
|
notif = showTranslationUI("fr");
|
2014-03-21 11:07:38 -07:00
|
|
|
let notificationBox = gBrowser.getNotificationBox();
|
2014-05-16 08:17:06 -07:00
|
|
|
is(hasTranslationInfoBar(), true, "there's a 'translate' notification");
|
2014-03-21 11:07:38 -07:00
|
|
|
notif._getAnonElt("notNow").click();
|
2014-05-16 08:17:06 -07:00
|
|
|
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");
|
2014-03-21 11:07:38 -07:00
|
|
|
|
2014-04-07 10:39:07 -07:00
|
|
|
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();
|
2014-05-16 08:17:06 -07:00
|
|
|
waitForCondition(hasTranslationInfoBar, () => {
|
|
|
|
ok(hasTranslationInfoBar(), "there's a 'translate' notification");
|
2014-04-07 10:39:07 -07:00
|
|
|
aFinishCallback();
|
|
|
|
}, "timeout waiting for the info bar to reappear");
|
2014-03-21 11:07:38 -07:00
|
|
|
}
|