[Editor] Corrrectly get the words from the alt-text when reporting the telemetry (bug 1929311)

This commit is contained in:
Calixte Denizet 2024-12-03 16:11:41 +01:00
parent f8d11a3a3a
commit e161826a44
3 changed files with 93 additions and 4 deletions

View File

@ -897,6 +897,9 @@ describe("Stamp Editor", () => {
eventBus.on("annotationeditoruimanager", ({ uiManager }) => { eventBus.on("annotationeditoruimanager", ({ uiManager }) => {
window.uiManager = uiManager; window.uiManager = uiManager;
}); });
eventBus.on("reporttelemetry", ({ details }) => {
(window.telemetry ||= []).push(structuredClone(details));
});
}, },
}, },
{ {
@ -917,6 +920,7 @@ describe("Stamp Editor", () => {
} }
await page.evaluate(() => { await page.evaluate(() => {
window.uiManager.reset(); window.uiManager.reset();
window.telemetry = [];
}); });
// Disable editing mode. // Disable editing mode.
await switchToStamp(page, /* disable */ true); await switchToStamp(page, /* disable */ true);
@ -953,7 +957,7 @@ describe("Stamp Editor", () => {
// Check that AI guessed the correct alt text. // Check that AI guessed the correct alt text.
await page.waitForFunction( await page.waitForFunction(
`document.getElementById("newAltTextDescriptionTextarea").value === `document.getElementById("newAltTextDescriptionTextarea").value ===
"Fake alt text"` "Fake alt text."`
); );
// Check that the dialog has the correct title: "Edit..." // Check that the dialog has the correct title: "Edit..."
@ -1182,6 +1186,82 @@ describe("Stamp Editor", () => {
await page.waitForSelector("#newAltTextDisclaimer[hidden]"); await page.waitForSelector("#newAltTextDisclaimer[hidden]");
} }
}); });
it("must check that the data in telemetry are correct", async () => {
// Run sequentially to avoid clipboard issues.
for (const [browserName, page] of pages) {
await page.evaluate(() => {
window.PDFViewerApplication.mlManager.enableAltTextModelDownload = true;
});
await switchToStamp(page);
// Add an image.
await copyImage(page, "../images/firefox_logo.png", 0);
const editorSelector = getEditorSelector(0);
await page.waitForSelector(editorSelector);
await waitForSerialized(page, 1);
// Wait for the dialog to be visible.
await page.waitForSelector("#newAltTextDialog", { visible: true });
// Check that AI guessed the correct alt text.
await page.waitForFunction(
`document.getElementById("newAltTextDescriptionTextarea").value ===
"Fake alt text."`
);
// Clear the input and check that the title changes to "Add..."
await clearInput(
page,
"#newAltTextDescriptionTextarea",
/* waitForInputEvent = */ true
);
// Save the empty text.
await page.click("#newAltTextSave");
await page.waitForSelector("#newAltTextDialog", { visible: false });
// Get the telemetry data and clean.
let telemetry = await page.evaluate(() => {
const tel = window.telemetry;
window.telemetry = [];
return tel;
});
let saveTelemetry = telemetry.find(
details => details.data.action === "pdfjs.image.alt_text.user_edit"
);
expect(saveTelemetry.data.data)
.withContext(`In ${browserName}`)
.toEqual({
total_words: 3,
words_removed: 3,
words_added: 0,
});
// Click on the Review button.
const buttonSelector = `${editorSelector} button.altText.new`;
await page.waitForSelector(buttonSelector, { visible: true });
await page.click(buttonSelector);
await page.waitForSelector("#newAltTextDialog", { visible: true });
// Add a new alt text and check that the title changes to "Edit..."
await page.type("#newAltTextDescriptionTextarea", "Fake text alt foo.");
// Save the empty text.
await page.click("#newAltTextSave");
await page.waitForSelector("#newAltTextDialog", { visible: false });
telemetry = await page.evaluate(() => window.telemetry);
saveTelemetry = telemetry.find(
details => details.data.action === "pdfjs.image.alt_text.user_edit"
);
expect(saveTelemetry.data.data)
.withContext(`In ${browserName}`)
.toEqual({
total_words: 3,
words_removed: 0,
words_added: 1,
});
}
});
}); });
describe("New alt-text flow (bug 1920515)", () => { describe("New alt-text flow (bug 1920515)", () => {

View File

@ -132,7 +132,7 @@ class FakeMLManager {
guess({ request: { data } }) { guess({ request: { data } }) {
return new Promise(resolve => { return new Promise(resolve => {
setTimeout(() => { setTimeout(() => {
resolve(data ? { output: "Fake alt text" } : { error: true }); resolve(data ? { output: "Fake alt text." } : { error: true });
}, 3000); }, 3000);
}); });
} }

View File

@ -460,6 +460,15 @@ class NewAltTextManager {
this.#uiManager = null; this.#uiManager = null;
} }
#extractWords(text) {
return new Set(
text
.toLowerCase()
.split(/[^\p{L}\p{N}]+/gu)
.filter(x => !!x)
);
}
#save() { #save() {
const altText = this.#textarea.value.trim(); const altText = this.#textarea.value.trim();
this.#currentEditor.altTextData = { this.#currentEditor.altTextData = {
@ -469,8 +478,8 @@ class NewAltTextManager {
this.#currentEditor.altTextData.guessedAltText = this.#guessedAltText; this.#currentEditor.altTextData.guessedAltText = this.#guessedAltText;
if (this.#guessedAltText && this.#guessedAltText !== altText) { if (this.#guessedAltText && this.#guessedAltText !== altText) {
const guessedWords = new Set(this.#guessedAltText.split(/\s+/)); const guessedWords = this.#extractWords(this.#guessedAltText);
const words = new Set(altText.split(/\s+/)); const words = this.#extractWords(altText);
this.#currentEditor._reportTelemetry({ this.#currentEditor._reportTelemetry({
action: "pdfjs.image.alt_text.user_edit", action: "pdfjs.image.alt_text.user_edit",
data: { data: {