Bug 1631253, use a capturing listener for the load event to check for possible translations, also add a test for the infobar opening, r=florian

Differential Revision: https://phabricator.services.mozilla.com/D71749
This commit is contained in:
Neil Deakin 2020-04-24 16:43:34 +00:00
Родитель 87cd1d1834
Коммит 1f5f8a3219
2 изменённых файлов: 59 добавлений и 55 удалений

Просмотреть файл

@ -422,7 +422,7 @@ let ACTORS = {
moduleURI: "resource:///modules/translation/TranslationChild.jsm", moduleURI: "resource:///modules/translation/TranslationChild.jsm",
events: { events: {
pageshow: {}, pageshow: {},
load: { mozSystemGroup: true }, load: { mozSystemGroup: true, capture: true },
}, },
}, },
enablePreference: "browser.translation.detectLanguage", enablePreference: "browser.translation.detectLanguage",

Просмотреть файл

@ -11,32 +11,15 @@ ChromeUtils.import(
); );
var { Translation, TranslationParent } = tmp; var { Translation, TranslationParent } = tmp;
const kDetectLanguagePref = "browser.translation.detectLanguage";
const kShowUIPref = "browser.translation.ui.show"; const kShowUIPref = "browser.translation.ui.show";
function waitForCondition(condition, nextTest, errorMsg) { const text =
var tries = 0; "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.";
var interval = setInterval(function() { const EXAMPLE_URL =
if (tries >= 30) { "http://example.com/document-builder.sjs?html=<html><body>" +
ok(false, errorMsg); text +
moveOn(); "</body></html>";
}
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();
};
}
// Create a subclass that overrides the translation functions. This can be // Create a subclass that overrides the translation functions. This can be
// instantiated separately from the normal actor creation process. This will // instantiated separately from the normal actor creation process. This will
@ -95,29 +78,6 @@ function hasTranslationInfoBar() {
.getNotificationWithValue("translation"); .getNotificationWithValue("translation");
} }
function test() {
waitForExplicitFinish();
Services.prefs.setBoolPref(kShowUIPref, true);
let tab = BrowserTestUtils.addTab(gBrowser);
gBrowser.selectedTab = tab;
BrowserTestUtils.browserLoaded(tab.linkedBrowser).then(() => {
TranslationStub.browser = gBrowser.selectedBrowser;
registerCleanupFunction(function() {
gBrowser.removeTab(tab);
Services.prefs.clearUserPref(kShowUIPref);
});
run_tests(() => {
finish();
});
});
BrowserTestUtils.loadURI(
gBrowser.selectedBrowser,
"data:text/plain,test page"
);
}
function checkURLBarIcon(aExpectTranslated = false) { function checkURLBarIcon(aExpectTranslated = false) {
is( is(
!PopupNotifications.getNotification("translate"), !PopupNotifications.getNotification("translate"),
@ -131,7 +91,18 @@ function checkURLBarIcon(aExpectTranslated = false) {
); );
} }
function run_tests(aFinishCallback) { 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"); info("Show an info bar saying the current page is in French");
let notif = showTranslationUI("fr"); let notif = showTranslationUI("fr");
is( is(
@ -335,12 +306,45 @@ function run_tests(aFinishCallback) {
// Clicking the anchor element causes a 'showing' event to be sent // Clicking the anchor element causes a 'showing' event to be sent
// asynchronously to our callback that will then show the infobar. // asynchronously to our callback that will then show the infobar.
PopupNotifications.getNotification("translate").anchorElement.click(); PopupNotifications.getNotification("translate").anchorElement.click();
waitForCondition(
await BrowserTestUtils.waitForCondition(
hasTranslationInfoBar, hasTranslationInfoBar,
() => {
ok(hasTranslationInfoBar(), "there's a 'translate' notification");
aFinishCallback();
},
"timeout waiting for the info bar to reappear" "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);
});