From d72fd9ce4f1c4727cc07652fa3dc6c1208cd7d53 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Thu, 26 Oct 2023 22:44:35 +0200 Subject: [PATCH] Fix intermittents failure on mac --- test/integration/find_spec.mjs | 7 +- test/integration/freetext_editor_spec.mjs | 96 +++++++++++++++++------ test/integration/test_utils.mjs | 28 ++----- 3 files changed, 85 insertions(+), 46 deletions(-) diff --git a/test/integration/find_spec.mjs b/test/integration/find_spec.mjs index 05ed27d1723a0..f460163cad31a 100644 --- a/test/integration/find_spec.mjs +++ b/test/integration/find_spec.mjs @@ -29,7 +29,7 @@ describe("find bar", () => { let pages; beforeAll(async () => { - pages = await loadAndWait("find_all.pdf#zoom=100", ".textLayer"); + pages = await loadAndWait("find_all.pdf", ".textLayer", 100); }); afterAll(async () => { @@ -76,7 +76,7 @@ describe("find bar", () => { let pages; beforeAll(async () => { - pages = await loadAndWait("xfa_imm5257e.pdf#zoom=100", ".xfaLayer"); + pages = await loadAndWait("xfa_imm5257e.pdf", ".xfaLayer"); }); afterAll(async () => { @@ -91,6 +91,9 @@ describe("find bar", () => { await page.type("#findInput", "preferences"); await page.waitForSelector("#findInput[data-status='']"); await page.waitForSelector(".xfaLayer .highlight"); + await page.waitForFunction( + () => !!document.querySelector("#findResultsCount")?.textContent + ); const resultElement = await page.waitForSelector("#findResultsCount"); const resultText = await resultElement.evaluate(el => el.textContent); /** Unicode bidi isolation characters. */ diff --git a/test/integration/freetext_editor_spec.mjs b/test/integration/freetext_editor_spec.mjs index 6defc20818ba5..412167f518a0b 100644 --- a/test/integration/freetext_editor_spec.mjs +++ b/test/integration/freetext_editor_spec.mjs @@ -93,6 +93,20 @@ const waitForPositionChange = (page, selector, xy) => xy ); +const cancelFocusIn = async (page, selector) => { + page.evaluate(sel => { + const el = document.querySelector(sel); + el.addEventListener( + "focusin", + evt => { + evt.preventDefault(); + evt.stopPropagation(); + }, + { capture: true, once: true } + ); + }, selector); +}; + describe("FreeText Editor", () => { describe("FreeText", () => { let pages; @@ -300,22 +314,23 @@ describe("FreeText Editor", () => { it("must check that aria-owns is correct", async () => { await Promise.all( pages.map(async ([browserName, page]) => { + await page.$eval(".textLayer", el => { + for (const span of el.querySelectorAll( + `span[role="presentation"]` + )) { + if (span.innerText.includes("Stacks are simple to create")) { + span.setAttribute("pdfjs", true); + } + } + }); + + await scrollIntoView(page, `span[pdfjs="true"]`); + const [stacksRect, oldAriaOwns] = await page.$eval( - ".textLayer", + `span[pdfjs="true"]`, el => { - for (const span of el.querySelectorAll( - `span[role="presentation"]` - )) { - if (span.innerText.includes("Stacks are simple to create")) { - span.setAttribute("pdfjs", true); - const { x, y, width, height } = span.getBoundingClientRect(); - return [ - { x, y, width, height }, - span.getAttribute("aria-owns"), - ]; - } - } - return null; + const { x, y, width, height } = el.getBoundingClientRect(); + return [{ x, y, width, height }, el.getAttribute("aria-owns")]; } ); @@ -345,6 +360,7 @@ describe("FreeText Editor", () => { expect(ariaOwns.endsWith("_7-editor")) .withContext(`In ${browserName}`) .toEqual(true); + await scrollIntoView(page, ".annotationEditorLayer"); }) ); }); @@ -2236,6 +2252,7 @@ describe("FreeText Editor", () => { }); await page.type(`${getEditorSelector(0)} .internal`, data); // Commit. + await cancelFocusIn(page, getEditorSelector(0)); await page.keyboard.press("Escape"); await page.waitForSelector( `${getEditorSelector(0)} .overlay.enabled` @@ -2488,6 +2505,7 @@ describe("FreeText Editor", () => { await page.type(`${getEditorSelector(0)} .internal`, data); // Commit. + await cancelFocusIn(page, getEditorSelector(0)); await page.keyboard.press("Escape"); await page.waitForSelector( `${getEditorSelector(0)} .overlay.enabled` @@ -2541,20 +2559,35 @@ describe("FreeText Editor", () => { const { x, y } = el.getBoundingClientRect(); return { x, y }; }); + const selectorEditor = getEditorSelector(0); await page.mouse.click(rect.x + 10, rect.y + 10); - await page.waitForSelector(getEditorSelector(0), { + await page.waitForSelector(selectorEditor, { visible: true, }); - await page.type(`${getEditorSelector(0)} .internal`, "Hello"); + await page.type(`${selectorEditor} .internal`, "Hello"); // Commit. + await cancelFocusIn(page, selectorEditor); await page.keyboard.press("Escape"); - await page.waitForSelector( - `${getEditorSelector(0)} .overlay.enabled` + await page.waitForSelector(`${selectorEditor} .overlay.enabled`); + + // Unselect. + await page.keyboard.press("Escape"); + await waitForUnselectedEditor(page, selectorEditor); + + const editorRect = await page.$eval(selectorEditor, el => { + const { x, y, width, height } = el.getBoundingClientRect(); + return { x, y, width, height }; + }); + + // Select the editor created previously. + await page.mouse.click( + editorRect.x + editorRect.width / 2, + editorRect.y + editorRect.height / 2 ); // Go to the last page. - await page.keyboard.press("End"); + await scrollIntoView(page, `.page[data-page-number = "14"]`); const page14Selector = `.page[data-page-number = "14"] > .annotationEditorLayer.freetextEditing`; await page.waitForSelector(page14Selector, { @@ -2638,20 +2671,35 @@ describe("FreeText Editor", () => { const { x, y } = el.getBoundingClientRect(); return { x, y }; }); + const selectorEditor = getEditorSelector(0); await page.mouse.click(rect.x + 10, rect.y + 10); - await page.waitForSelector(getEditorSelector(0), { + await page.waitForSelector(selectorEditor, { visible: true, }); - await page.type(`${getEditorSelector(0)} .internal`, "Hello"); + await page.type(`${selectorEditor} .internal`, "Hello"); // Commit. + await cancelFocusIn(page, selectorEditor); await page.keyboard.press("Escape"); - await page.waitForSelector( - `${getEditorSelector(0)} .overlay.enabled` + await page.waitForSelector(`${selectorEditor} .overlay.enabled`); + + // Unselect. + await page.keyboard.press("Escape"); + await waitForUnselectedEditor(page, selectorEditor); + + const editorRect = await page.$eval(selectorEditor, el => { + const { x, y, width, height } = el.getBoundingClientRect(); + return { x, y, width, height }; + }); + + // Select the editor created previously. + await page.mouse.click( + editorRect.x + editorRect.width / 2, + editorRect.y + editorRect.height / 2 ); // Go to the last page. - await kbGoToEnd(page); + await scrollIntoView(page, `.page[data-page-number = "14"]`); await page.waitForSelector( `.page[data-page-number = "14"] > .annotationEditorLayer.freetextEditing`, { diff --git a/test/integration/test_utils.mjs b/test/integration/test_utils.mjs index be1928de25636..d5114b20ae199 100644 --- a/test/integration/test_utils.mjs +++ b/test/integration/test_utils.mjs @@ -252,34 +252,22 @@ async function waitForTextLayer(page) { } async function scrollIntoView(page, selector) { - const promise = page.evaluate( - sel => - new Promise(resolve => { - const el = document.querySelector(sel); - const observer = new IntersectionObserver( - () => { - observer.disconnect(); - resolve(); - }, - { - root: document.querySelector("#viewerContainer"), - threshold: 0.1, - } - ); - observer.observe(el); - }), - selector - ); await page.evaluate(sel => { const element = document.querySelector(sel); element.scrollIntoView({ behavior: "instant", block: "start" }); }, selector); - await promise; await page.waitForFunction( sel => { + const toolbarHeight = document + .querySelector("#toolbarContainer") + .getBoundingClientRect().height; const element = document.querySelector(sel); const { top, bottom } = element.getBoundingClientRect(); - return Math.abs(top) < 100 || Math.abs(bottom - window.innerHeight) < 100; + return ( + Math.abs(top) < toolbarHeight + 100 || + Math.abs(bottom - window.innerHeight) < 100 || + (top > toolbarHeight && bottom < window.innerHeight) + ); }, {}, selector