Bug 973292 - Record the number of characters that are translated with FHR r=felipe

This commit is contained in:
Tim Taubert 2014-06-11 15:57:52 +02:00
parent 331a3afbe5
commit 836f8943e6
5 changed files with 127 additions and 3 deletions

View File

@ -46,6 +46,7 @@ this.BingTranslation = function(translationDocument, sourceLanguage, targetLangu
this.targetLanguage = targetLanguage;
this._pendingRequests = 0;
this._partialSuccess = false;
this._translatedCharacterCount = 0;
};
this.BingTranslation.prototype = {
@ -105,6 +106,8 @@ this.BingTranslation.prototype = {
this._parseChunkResult(bingRequest)) {
// error on request
this._partialSuccess = true;
// Count the number of characters successfully translated.
this._translatedCharacterCount += bingRequest.characterCount;
}
// Check if all pending requests have been
@ -115,7 +118,9 @@ this.BingTranslation.prototype = {
// the "Error" state will appear.
if (this._pendingRequests == 0) {
if (this._partialSuccess) {
this._onFinishedDeferred.resolve("success");
this._onFinishedDeferred.resolve({
characterCount: this._translatedCharacterCount
});
} else {
this._onFinishedDeferred.reject("failure");
}
@ -237,6 +242,7 @@ function BingRequest(translationData, sourceLanguage, targetLanguage) {
this.translationData = translationData;
this.sourceLanguage = sourceLanguage;
this.targetLanguage = targetLanguage;
this.characterCount = 0;
}
BingRequest.prototype = {
@ -263,6 +269,7 @@ BingRequest.prototype = {
for (let [, text] of this.translationData) {
requestString += '<s:string>' + text + '</s:string>';
this.characterCount += text.length;
}
requestString += '</Texts>' +

View File

@ -206,6 +206,10 @@ TranslationUI.prototype = {
this.originalShown = false;
this.state = Translation.STATE_TRANSLATED;
this.showURLBarIcon();
// Record the number of characters translated.
TranslationHealthReport.recordTranslation(msg.data.from, msg.data.to,
msg.data.characterCount);
} else {
this.state = Translation.STATE_ERROR;
}

View File

@ -131,8 +131,13 @@ TranslationContentHandler.prototype = {
translationDocument.translationError = false;
bingTranslation.translate().then(
success => {
this.global.sendAsyncMessage("Translation:Finished", {success: true});
result => {
this.global.sendAsyncMessage("Translation:Finished", {
characterCount: result.characterCount,
from: msg.data.from,
to: msg.data.to,
success: true
});
translationDocument.showTranslation();
},
error => {

View File

@ -1,4 +1,6 @@
[DEFAULT]
[browser_translation_fhr.js]
skip-if = true # Needs to wait until bug 1022725.
[browser_translation_infobar.js]
[browser_translation_exceptions.js]

View File

@ -0,0 +1,106 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
let tmp = {};
Cu.import("resource:///modules/translation/Translation.jsm", tmp);
let {Translation} = tmp;
add_task(function* setup() {
Services.prefs.setBoolPref("toolkit.telemetry.enabled", true);
Services.prefs.setBoolPref("browser.translation.detectLanguage", true);
Services.prefs.setBoolPref("browser.translation.ui.show", true);
registerCleanupFunction(() => {
Services.prefs.clearUserPref("toolkit.telemetry.enabled");
Services.prefs.clearUserPref("browser.translation.detectLanguage");
Services.prefs.clearUserPref("browser.translation.ui.show");
});
});
add_task(function* test_fhr() {
let start = new Date();
// Translate a page.
yield translate("<h1>Hallo Welt!</h1>", "de", "en");
let [pageCount, charCount] = yield retrieveTranslationCounts();
// Translate another page.
yield translate("<h1>Hallo Welt!</h1><h1>Bratwurst!</h1>", "de", "en");
let [pageCount2, charCount2] = yield retrieveTranslationCounts();
// Check that it's still the same day of the month as when we started. This
// prevents intermittent failures when the test starts before and ends after
// midnight.
if (start.getDate() == new Date().getDate()) {
Assert.equal(pageCount2, pageCount + 1);
Assert.equal(charCount2, charCount + 21);
}
});
function retrieveTranslationCounts() {
return Task.spawn(function* task_retrieve_counts() {
let svc = Cc["@mozilla.org/datareporting/service;1"].getService();
let reporter = svc.wrappedJSObject.healthReporter;
yield reporter.onInit();
// Get the provider.
let provider = reporter.getProvider("org.mozilla.translation");
let measurement = provider.getMeasurement("translation", 1);
let values = yield measurement.getValues();
let day = values.days.getDay(new Date());
if (!day) {
// This should never happen except when the test runs at midnight.
return [0, 0];
}
return [day.get("pageTranslatedCount"), day.get("charactersTranslatedCount")];
});
}
function translate(text, from, to) {
return Task.spawn(function* task_translate() {
// Create some content to translate.
let tab = gBrowser.selectedTab =
gBrowser.addTab("data:text/html;charset=utf-8," + text);
// Wait until that's loaded.
let browser = tab.linkedBrowser;
yield promiseBrowserLoaded(browser);
// Send a translation offer.
Translation.documentStateReceived(browser, {state: Translation.STATE_OFFER,
originalShown: true,
detectedLanguage: from});
// Translate the page.
browser.translationUI.translate(from, to);
yield waitForMessage(browser, "Translation:Finished");
// Cleanup.
gBrowser.removeTab(tab);
});
}
function waitForMessage({messageManager}, name) {
return new Promise(resolve => {
messageManager.addMessageListener(name, function onMessage() {
messageManager.removeMessageListener(name, onMessage);
resolve();
});
});
}
function promiseBrowserLoaded(browser) {
return new Promise(resolve => {
browser.addEventListener("load", function onLoad(event) {
if (event.target == browser.contentDocument) {
browser.removeEventListener("load", onLoad, true);
resolve();
}
}, true);
});
}