browser/components/translation/test/browser_translation_infobar.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 // tests the translation infobar, using a fake 'Translation' implementation.
     7 Components.utils.import("resource:///modules/translation/Translation.jsm");
     9 function waitForCondition(condition, nextTest, errorMsg) {
    10   var tries = 0;
    11   var interval = setInterval(function() {
    12     if (tries >= 30) {
    13       ok(false, errorMsg);
    14       moveOn();
    15     }
    16     var conditionPassed;
    17     try {
    18       conditionPassed = condition();
    19     } catch (e) {
    20       ok(false, e + "\n" + e.stack);
    21       conditionPassed = false;
    22     }
    23     if (conditionPassed) {
    24       moveOn();
    25     }
    26     tries++;
    27   }, 100);
    28   var moveOn = function() { clearInterval(interval); nextTest(); };
    29 }
    31 var TranslationStub = {
    32   translate: function(aFrom, aTo) {
    33     this.state = this.STATE_TRANSLATING;
    34     this.translatedFrom = aFrom;
    35     this.translatedTo = aTo;
    36   },
    38   _reset: function() {
    39     this.translatedFrom = "";
    40     this.translatedTo = "";
    41   },
    43   failTranslation: function() {
    44     this.state = this.STATE_ERROR;
    45     this._reset();
    46   },
    48   finishTranslation: function() {
    49     this.showTranslatedContent();
    50     this.state = this.STATE_TRANSLATED;
    51     this._reset();
    52   }
    53 };
    55 function showTranslationUI(aDetectedLanguage) {
    56   let browser = gBrowser.selectedBrowser;
    57   Translation.languageDetected(browser, aDetectedLanguage);
    58   let ui = browser.translationUI;
    59   for (let name of ["translate", "_reset", "failTranslation", "finishTranslation"])
    60     ui[name] = TranslationStub[name];
    61   return ui.notificationBox.getNotificationWithValue("translation");
    62 }
    64 function test() {
    65   waitForExplicitFinish();
    67   let tab = gBrowser.addTab();
    68   gBrowser.selectedTab = tab;
    69   tab.linkedBrowser.addEventListener("load", function onload() {
    70     tab.linkedBrowser.removeEventListener("load", onload, true);
    71     TranslationStub.browser = gBrowser.selectedBrowser;
    72     registerCleanupFunction(function () {
    73       gBrowser.removeTab(tab);
    74     });
    75     run_tests(() => {
    76       finish();
    77     });
    78   }, true);
    80   content.location = "data:text/plain,test page";
    81 }
    83 function checkURLBarIcon(aExpectTranslated = false) {
    84   is(!PopupNotifications.getNotification("translate"), aExpectTranslated,
    85      "translate icon " + (aExpectTranslated ? "not " : "") + "shown");
    86   is(!!PopupNotifications.getNotification("translated"), aExpectTranslated,
    87      "translated icon " + (aExpectTranslated ? "" : "not ") + "shown");
    88 }
    90 function run_tests(aFinishCallback) {
    91   info("Show an info bar saying the current page is in French");
    92   let notif = showTranslationUI("fr");
    93   is(notif.state, notif.translation.STATE_OFFER, "the infobar is offering translation");
    94   is(notif._getAnonElt("detectedLanguage").value, "fr", "The detected language is displayed");
    95   checkURLBarIcon();
    97   info("Click the 'Translate' button");
    98   notif._getAnonElt("translate").click();
    99   is(notif.state, notif.translation.STATE_TRANSLATING, "the infobar is in the translating state");
   100   ok(!!notif.translation.translatedFrom, "Translation.translate has been called");
   101   is(notif.translation.translatedFrom, "fr", "from language correct");
   102   is(notif.translation.translatedTo, Translation.defaultTargetLanguage, "from language correct");
   103   checkURLBarIcon();
   105   info("Make the translation fail and check we are in the error state.");
   106   notif.translation.failTranslation();
   107   is(notif.state, notif.translation.STATE_ERROR, "infobar in the error state");
   108   checkURLBarIcon();
   110   info("Click the try again button");
   111   notif._getAnonElt("tryAgain").click();
   112   is(notif.state, notif.translation.STATE_TRANSLATING, "infobar in the translating state");
   113   ok(!!notif.translation.translatedFrom, "Translation.translate has been called");
   114   is(notif.translation.translatedFrom, "fr", "from language correct");
   115   is(notif.translation.translatedTo, Translation.defaultTargetLanguage, "from language correct");
   116   checkURLBarIcon();
   118   info("Make the translation succeed and check we are in the 'translated' state.");
   119   notif.translation.finishTranslation();
   120   is(notif.state, notif.translation.STATE_TRANSLATED, "infobar in the translated state");
   121   checkURLBarIcon(true);
   123   info("Test 'Show original' / 'Show Translation' buttons.");
   124   // First check 'Show Original' is visible and 'Show Translation' is hidden.
   125   ok(!notif._getAnonElt("showOriginal").hidden, "'Show Original' button visible");
   126   ok(notif._getAnonElt("showTranslation").hidden, "'Show Translation' button hidden");
   127   // Click the button.
   128   notif._getAnonElt("showOriginal").click();
   129   // Check that the url bar icon shows the original content is displayed.
   130   checkURLBarIcon();
   131   // And the 'Show Translation' button is now visible.
   132   ok(notif._getAnonElt("showOriginal").hidden, "'Show Original' button hidden");
   133   ok(!notif._getAnonElt("showTranslation").hidden, "'Show Translation' button visible");
   134   // Click the 'Show Translation' button
   135   notif._getAnonElt("showTranslation").click();
   136   // Check that the url bar icon shows the page is translated.
   137   checkURLBarIcon(true);
   138   // Check that the 'Show Original' button is visible again.
   139   ok(!notif._getAnonElt("showOriginal").hidden, "'Show Original' button visible");
   140   ok(notif._getAnonElt("showTranslation").hidden, "'Show Translation' button hidden");
   142   info("Check that changing the source language causes a re-translation");
   143   let from = notif._getAnonElt("fromLanguage");
   144   from.value = "es";
   145   from.doCommand();
   146   is(notif.state, notif.translation.STATE_TRANSLATING, "infobar in the translating state");
   147   ok(!!notif.translation.translatedFrom, "Translation.translate has been called");
   148   is(notif.translation.translatedFrom, "es", "from language correct");
   149   is(notif.translation.translatedTo, Translation.defaultTargetLanguage, "to language correct");
   150   // We want to show the 'translated' icon while re-translating,
   151   // because we are still displaying the previous translation.
   152   checkURLBarIcon(true);
   153   notif.translation.finishTranslation();
   154   checkURLBarIcon(true);
   156   info("Check that changing the target language causes a re-translation");
   157   let to = notif._getAnonElt("toLanguage");
   158   to.value = "pl";
   159   to.doCommand();
   160   is(notif.state, notif.translation.STATE_TRANSLATING, "infobar in the translating state");
   161   ok(!!notif.translation.translatedFrom, "Translation.translate has been called");
   162   is(notif.translation.translatedFrom, "es", "from language correct");
   163   is(notif.translation.translatedTo, "pl", "to language correct");
   164   checkURLBarIcon(true);
   165   notif.translation.finishTranslation();
   166   checkURLBarIcon(true);
   168   // Cleanup.
   169   notif.close();
   171   info("Reopen the info bar to check that it's possible to override the detected language.");
   172   notif = showTranslationUI("fr");
   173   is(notif.state, notif.translation.STATE_OFFER, "the infobar is offering translation");
   174   is(notif._getAnonElt("detectedLanguage").value, "fr", "The detected language is displayed");
   175   // Change the language and click 'Translate'
   176   notif._getAnonElt("detectedLanguage").value = "ja";
   177   notif._getAnonElt("translate").click();
   178   is(notif.state, notif.translation.STATE_TRANSLATING, "the infobar is in the translating state");
   179   ok(!!notif.translation.translatedFrom, "Translation.translate has been called");
   180   is(notif.translation.translatedFrom, "ja", "from language correct");
   181   notif.close();
   183   info("Reopen to check the 'Not Now' button closes the notification.");
   184   notif = showTranslationUI("fr");
   185   let notificationBox = gBrowser.getNotificationBox();
   186   ok(!!notificationBox.getNotificationWithValue("translation"), "there's a 'translate' notification");
   187   notif._getAnonElt("notNow").click();
   188   ok(!notificationBox.getNotificationWithValue("translation"), "no 'translate' notification after clicking 'not now'");
   190   info("Check that clicking the url bar icon reopens the info bar");
   191   checkURLBarIcon();
   192   // Clicking the anchor element causes a 'showing' event to be sent
   193   // asynchronously to our callback that will then show the infobar.
   194   PopupNotifications.getNotification("translate").anchorElement.click();
   195   waitForCondition(() => !!notificationBox.getNotificationWithValue("translation"), () => {
   196     ok(!!notificationBox.getNotificationWithValue("translation"),
   197        "there's a 'translate' notification");
   198     aFinishCallback();
   199   }, "timeout waiting for the info bar to reappear");
   200 }

mercurial