Skip to content

Commit

Permalink
Fix new intermittent failures with ink and stamp tests
Browse files Browse the repository at this point in the history
It happens only on windows with chrome.
For any reason, click event isn't correctly triggered and it seems work correctly
with pointerup.
And it seems that when drawing a svg on an OffscreenCanvas we need to wait
a little in order to be able to transfer it: it's why this patch adds
a check on the canvas content.
  • Loading branch information
calixteman committed Oct 2, 2023
1 parent 3ca63c6 commit 077d239
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
10 changes: 5 additions & 5 deletions test/integration/ink_editor_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ const {
waitForStorageEntries,
} = require("./test_utils.js");

const waitForClick = async page =>
const waitForPointerUp = page =>
page.evaluate(
() =>
new Promise(resolve => {
window.addEventListener("click", resolve, { once: true });
window.addEventListener("pointerup", resolve, { once: true });
})
);

Expand Down Expand Up @@ -77,7 +77,7 @@ describe("Ink Editor", () => {
for (let i = 0; i < 3; i++) {
const x = rect.x + 100 + i * 100;
const y = rect.y + 100 + i * 100;
const promise = waitForClick(page);
const promise = waitForPointerUp(page);
await page.mouse.move(x, y);
await page.mouse.down();
await page.mouse.move(x + 50, y + 50);
Expand Down Expand Up @@ -115,7 +115,7 @@ describe("Ink Editor", () => {

const xStart = rect.x + 300;
const yStart = rect.y + 300;
const clickPromise = waitForClick(page);
const clickPromise = waitForPointerUp(page);
await page.mouse.move(xStart, yStart);
await page.mouse.down();
await page.mouse.move(xStart + 50, yStart + 50);
Expand Down Expand Up @@ -185,7 +185,7 @@ describe("Ink Editor", () => {

const x = rect.x + 20;
const y = rect.y + 20;
const clickPromise = waitForClick(page);
const clickPromise = waitForPointerUp(page);
await page.mouse.move(x, y);
await page.mouse.down();
await page.mouse.move(x + 50, y + 50);
Expand Down
25 changes: 22 additions & 3 deletions test/integration/stamp_editor_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ const clearAll = async page => {
await waitForStorageEntries(page, 0);
};

const waitForImage = async (page, selector) => {
await page.waitForSelector(`${selector} canvas`);
await page.waitForFunction(
sel => {
const canvas = document.querySelector(sel);
const data = canvas
.getContext("2d")
.getImageData(0, 0, canvas.width, canvas.height);
return data.data.some(x => x !== 0);
},
{},
`${selector} canvas`
);
await page.waitForSelector(`${selector} .altText`);
};

describe("Stamp Editor", () => {
describe("Basic operations", () => {
let pages;
Expand Down Expand Up @@ -70,7 +86,7 @@ describe("Stamp Editor", () => {
await input.uploadFile(
`${path.join(__dirname, "../images/firefox_logo.png")}`
);
await page.waitForSelector(`${getEditorSelector(0)} .altText`);
await waitForImage(page, getEditorSelector(0));

const { width } = await getEditorDimensions(page, 0);

Expand Down Expand Up @@ -100,7 +116,7 @@ describe("Stamp Editor", () => {
await input.uploadFile(
`${path.join(__dirname, "../images/firefox_logo.svg")}`
);
await page.waitForSelector(`${getEditorSelector(1)} .altText`);
await waitForImage(page, getEditorSelector(1));

const { width } = await getEditorDimensions(page, 1);

Expand Down Expand Up @@ -153,6 +169,7 @@ describe("Stamp Editor", () => {
await input.uploadFile(
`${path.join(__dirname, "../images/firefox_logo.png")}`
);
await waitForImage(page, getEditorSelector(i));
await page.waitForSelector(`${getEditorSelector(i)} .altText`);

for (let j = 0; j < 4; j++) {
Expand Down Expand Up @@ -236,8 +253,10 @@ describe("Stamp Editor", () => {
await page.keyboard.press("v");
await page.keyboard.up("Control");

await waitForImage(page, getEditorSelector(0));

// Wait for the alt-text button to be visible.
const buttonSelector = "#pdfjs_internal_editor_0 button.altText";
const buttonSelector = `${getEditorSelector(0)} button.altText`;
await page.waitForSelector(buttonSelector);

// Click on the alt-text button.
Expand Down
13 changes: 12 additions & 1 deletion test/integration/test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,18 @@ function getEditorDimensions(page, id) {
}
exports.getEditorDimensions = getEditorDimensions;

function serializeBitmapDimensions(page) {
async function serializeBitmapDimensions(page) {
await page.waitForFunction(() => {
try {
const map =
window.PDFViewerApplication.pdfDocument.annotationStorage.serializable
.map;
return !!map;
} catch {
return false;
}
});

return page.evaluate(() => {
const { map } =
window.PDFViewerApplication.pdfDocument.annotationStorage.serializable;
Expand Down

0 comments on commit 077d239

Please sign in to comment.