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