1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/translation/test/browser_translation_infobar.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,200 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +// tests the translation infobar, using a fake 'Translation' implementation. 1.9 + 1.10 +Components.utils.import("resource:///modules/translation/Translation.jsm"); 1.11 + 1.12 +function waitForCondition(condition, nextTest, errorMsg) { 1.13 + var tries = 0; 1.14 + var interval = setInterval(function() { 1.15 + if (tries >= 30) { 1.16 + ok(false, errorMsg); 1.17 + moveOn(); 1.18 + } 1.19 + var conditionPassed; 1.20 + try { 1.21 + conditionPassed = condition(); 1.22 + } catch (e) { 1.23 + ok(false, e + "\n" + e.stack); 1.24 + conditionPassed = false; 1.25 + } 1.26 + if (conditionPassed) { 1.27 + moveOn(); 1.28 + } 1.29 + tries++; 1.30 + }, 100); 1.31 + var moveOn = function() { clearInterval(interval); nextTest(); }; 1.32 +} 1.33 + 1.34 +var TranslationStub = { 1.35 + translate: function(aFrom, aTo) { 1.36 + this.state = this.STATE_TRANSLATING; 1.37 + this.translatedFrom = aFrom; 1.38 + this.translatedTo = aTo; 1.39 + }, 1.40 + 1.41 + _reset: function() { 1.42 + this.translatedFrom = ""; 1.43 + this.translatedTo = ""; 1.44 + }, 1.45 + 1.46 + failTranslation: function() { 1.47 + this.state = this.STATE_ERROR; 1.48 + this._reset(); 1.49 + }, 1.50 + 1.51 + finishTranslation: function() { 1.52 + this.showTranslatedContent(); 1.53 + this.state = this.STATE_TRANSLATED; 1.54 + this._reset(); 1.55 + } 1.56 +}; 1.57 + 1.58 +function showTranslationUI(aDetectedLanguage) { 1.59 + let browser = gBrowser.selectedBrowser; 1.60 + Translation.languageDetected(browser, aDetectedLanguage); 1.61 + let ui = browser.translationUI; 1.62 + for (let name of ["translate", "_reset", "failTranslation", "finishTranslation"]) 1.63 + ui[name] = TranslationStub[name]; 1.64 + return ui.notificationBox.getNotificationWithValue("translation"); 1.65 +} 1.66 + 1.67 +function test() { 1.68 + waitForExplicitFinish(); 1.69 + 1.70 + let tab = gBrowser.addTab(); 1.71 + gBrowser.selectedTab = tab; 1.72 + tab.linkedBrowser.addEventListener("load", function onload() { 1.73 + tab.linkedBrowser.removeEventListener("load", onload, true); 1.74 + TranslationStub.browser = gBrowser.selectedBrowser; 1.75 + registerCleanupFunction(function () { 1.76 + gBrowser.removeTab(tab); 1.77 + }); 1.78 + run_tests(() => { 1.79 + finish(); 1.80 + }); 1.81 + }, true); 1.82 + 1.83 + content.location = "data:text/plain,test page"; 1.84 +} 1.85 + 1.86 +function checkURLBarIcon(aExpectTranslated = false) { 1.87 + is(!PopupNotifications.getNotification("translate"), aExpectTranslated, 1.88 + "translate icon " + (aExpectTranslated ? "not " : "") + "shown"); 1.89 + is(!!PopupNotifications.getNotification("translated"), aExpectTranslated, 1.90 + "translated icon " + (aExpectTranslated ? "" : "not ") + "shown"); 1.91 +} 1.92 + 1.93 +function run_tests(aFinishCallback) { 1.94 + info("Show an info bar saying the current page is in French"); 1.95 + let notif = showTranslationUI("fr"); 1.96 + is(notif.state, notif.translation.STATE_OFFER, "the infobar is offering translation"); 1.97 + is(notif._getAnonElt("detectedLanguage").value, "fr", "The detected language is displayed"); 1.98 + checkURLBarIcon(); 1.99 + 1.100 + info("Click the 'Translate' button"); 1.101 + notif._getAnonElt("translate").click(); 1.102 + is(notif.state, notif.translation.STATE_TRANSLATING, "the infobar is in the translating state"); 1.103 + ok(!!notif.translation.translatedFrom, "Translation.translate has been called"); 1.104 + is(notif.translation.translatedFrom, "fr", "from language correct"); 1.105 + is(notif.translation.translatedTo, Translation.defaultTargetLanguage, "from language correct"); 1.106 + checkURLBarIcon(); 1.107 + 1.108 + info("Make the translation fail and check we are in the error state."); 1.109 + notif.translation.failTranslation(); 1.110 + is(notif.state, notif.translation.STATE_ERROR, "infobar in the error state"); 1.111 + checkURLBarIcon(); 1.112 + 1.113 + info("Click the try again button"); 1.114 + notif._getAnonElt("tryAgain").click(); 1.115 + is(notif.state, notif.translation.STATE_TRANSLATING, "infobar in the translating state"); 1.116 + ok(!!notif.translation.translatedFrom, "Translation.translate has been called"); 1.117 + is(notif.translation.translatedFrom, "fr", "from language correct"); 1.118 + is(notif.translation.translatedTo, Translation.defaultTargetLanguage, "from language correct"); 1.119 + checkURLBarIcon(); 1.120 + 1.121 + info("Make the translation succeed and check we are in the 'translated' state."); 1.122 + notif.translation.finishTranslation(); 1.123 + is(notif.state, notif.translation.STATE_TRANSLATED, "infobar in the translated state"); 1.124 + checkURLBarIcon(true); 1.125 + 1.126 + info("Test 'Show original' / 'Show Translation' buttons."); 1.127 + // First check 'Show Original' is visible and 'Show Translation' is hidden. 1.128 + ok(!notif._getAnonElt("showOriginal").hidden, "'Show Original' button visible"); 1.129 + ok(notif._getAnonElt("showTranslation").hidden, "'Show Translation' button hidden"); 1.130 + // Click the button. 1.131 + notif._getAnonElt("showOriginal").click(); 1.132 + // Check that the url bar icon shows the original content is displayed. 1.133 + checkURLBarIcon(); 1.134 + // And the 'Show Translation' button is now visible. 1.135 + ok(notif._getAnonElt("showOriginal").hidden, "'Show Original' button hidden"); 1.136 + ok(!notif._getAnonElt("showTranslation").hidden, "'Show Translation' button visible"); 1.137 + // Click the 'Show Translation' button 1.138 + notif._getAnonElt("showTranslation").click(); 1.139 + // Check that the url bar icon shows the page is translated. 1.140 + checkURLBarIcon(true); 1.141 + // Check that the 'Show Original' button is visible again. 1.142 + ok(!notif._getAnonElt("showOriginal").hidden, "'Show Original' button visible"); 1.143 + ok(notif._getAnonElt("showTranslation").hidden, "'Show Translation' button hidden"); 1.144 + 1.145 + info("Check that changing the source language causes a re-translation"); 1.146 + let from = notif._getAnonElt("fromLanguage"); 1.147 + from.value = "es"; 1.148 + from.doCommand(); 1.149 + is(notif.state, notif.translation.STATE_TRANSLATING, "infobar in the translating state"); 1.150 + ok(!!notif.translation.translatedFrom, "Translation.translate has been called"); 1.151 + is(notif.translation.translatedFrom, "es", "from language correct"); 1.152 + is(notif.translation.translatedTo, Translation.defaultTargetLanguage, "to language correct"); 1.153 + // We want to show the 'translated' icon while re-translating, 1.154 + // because we are still displaying the previous translation. 1.155 + checkURLBarIcon(true); 1.156 + notif.translation.finishTranslation(); 1.157 + checkURLBarIcon(true); 1.158 + 1.159 + info("Check that changing the target language causes a re-translation"); 1.160 + let to = notif._getAnonElt("toLanguage"); 1.161 + to.value = "pl"; 1.162 + to.doCommand(); 1.163 + is(notif.state, notif.translation.STATE_TRANSLATING, "infobar in the translating state"); 1.164 + ok(!!notif.translation.translatedFrom, "Translation.translate has been called"); 1.165 + is(notif.translation.translatedFrom, "es", "from language correct"); 1.166 + is(notif.translation.translatedTo, "pl", "to language correct"); 1.167 + checkURLBarIcon(true); 1.168 + notif.translation.finishTranslation(); 1.169 + checkURLBarIcon(true); 1.170 + 1.171 + // Cleanup. 1.172 + notif.close(); 1.173 + 1.174 + info("Reopen the info bar to check that it's possible to override the detected language."); 1.175 + notif = showTranslationUI("fr"); 1.176 + is(notif.state, notif.translation.STATE_OFFER, "the infobar is offering translation"); 1.177 + is(notif._getAnonElt("detectedLanguage").value, "fr", "The detected language is displayed"); 1.178 + // Change the language and click 'Translate' 1.179 + notif._getAnonElt("detectedLanguage").value = "ja"; 1.180 + notif._getAnonElt("translate").click(); 1.181 + is(notif.state, notif.translation.STATE_TRANSLATING, "the infobar is in the translating state"); 1.182 + ok(!!notif.translation.translatedFrom, "Translation.translate has been called"); 1.183 + is(notif.translation.translatedFrom, "ja", "from language correct"); 1.184 + notif.close(); 1.185 + 1.186 + info("Reopen to check the 'Not Now' button closes the notification."); 1.187 + notif = showTranslationUI("fr"); 1.188 + let notificationBox = gBrowser.getNotificationBox(); 1.189 + ok(!!notificationBox.getNotificationWithValue("translation"), "there's a 'translate' notification"); 1.190 + notif._getAnonElt("notNow").click(); 1.191 + ok(!notificationBox.getNotificationWithValue("translation"), "no 'translate' notification after clicking 'not now'"); 1.192 + 1.193 + info("Check that clicking the url bar icon reopens the info bar"); 1.194 + checkURLBarIcon(); 1.195 + // Clicking the anchor element causes a 'showing' event to be sent 1.196 + // asynchronously to our callback that will then show the infobar. 1.197 + PopupNotifications.getNotification("translate").anchorElement.click(); 1.198 + waitForCondition(() => !!notificationBox.getNotificationWithValue("translation"), () => { 1.199 + ok(!!notificationBox.getNotificationWithValue("translation"), 1.200 + "there's a 'translate' notification"); 1.201 + aFinishCallback(); 1.202 + }, "timeout waiting for the info bar to reappear"); 1.203 +}