From 6019d28a40bba9b2f55ba4a60c24fca1b5419baa Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Sun, 4 Oct 2026 14:47:32 +0200 Subject: [PATCH 1/3] Extract the search logic to a helper function in the find bar integration tests This refactoring makes the existing tests more compact/expressive, and makes it easier to add more find bar tests in a next commit without having to duplicate this logic more often. --- test/integration/find_spec.mjs | 44 ++++++++++++++-------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/test/integration/find_spec.mjs b/test/integration/find_spec.mjs index 5c4439a5b..27a2ed201 100644 --- a/test/integration/find_spec.mjs +++ b/test/integration/find_spec.mjs @@ -30,6 +30,14 @@ function fuzzyMatch(a, b, browserName, pixelFuzz = 3) { .toBeGreaterThan(b - pixelFuzz); } +async function search(page, query) { + await page.click("#viewFindButton"); + await page.waitForSelector("#findInput", { visible: true }); + await page.type("#findInput", query); + await page.waitForSelector("#findInput[data-status='']"); + await page.waitForSelector(".highlight"); +} + describe("find bar", () => { describe("highlight all", () => { let pages; @@ -45,12 +53,10 @@ describe("find bar", () => { it("must highlight text in the right position", async () => { await Promise.all( pages.map(async ([browserName, page]) => { + await search(page, "a"); + // Highlight all occurrences of the letter A (case insensitive). - await page.click("#viewFindButton"); - await page.waitForSelector("#findInput", { visible: true }); - await page.type("#findInput", "a"); await page.click("#findHighlightAll + label"); - await page.waitForSelector(".textLayer .highlight"); // The PDF file contains the text 'AB BA' in a monospace font on a // single line. Check if the two occurrences of A are highlighted. @@ -106,11 +112,8 @@ describe("find bar", () => { it("must search xfa correctly", async () => { await Promise.all( pages.map(async ([browserName, page]) => { - await page.click("#viewFindButton"); - await page.waitForSelector("#findInput", { visible: true }); - await page.type("#findInput", "preferences"); - await page.waitForSelector("#findInput[data-status='']"); - await page.waitForSelector(".xfaLayer .highlight"); + await search(page, "preferences"); + await waitForTextToBe( page, "#findResultsCount", @@ -136,13 +139,9 @@ describe("find bar", () => { it("must scroll to the search result text", async () => { await Promise.all( pages.map(async ([browserName, page]) => { - // Search for "40" - await page.click("#viewFindButton"); - await page.waitForSelector("#findInput", { visible: true }); - await page.type("#findInput", "40"); - - const highlight = await page.waitForSelector(".textLayer .highlight"); + await search(page, "40"); + const highlight = await page.$(".textLayer .highlight"); expect(await highlight.isIntersectingViewport()).toBeTrue(); }) ); @@ -165,12 +164,10 @@ describe("find bar", () => { pages.map(async ([browserName, page]) => { // Set a smaller viewport to simulate a mobile device await page.setViewport({ width: 350, height: 600 }); - await page.click("#viewFindButton"); - await page.waitForSelector("#findInput", { visible: true }); - await page.type("#findInput", "productivity"); - const highlight = await page.waitForSelector(".textLayer .highlight"); + await search(page, "productivity"); + const highlight = await page.$(".textLayer .highlight"); expect(await highlight.isIntersectingViewport()).toBeTrue(); }) ); @@ -195,10 +192,7 @@ describe("find bar", () => { it("must scroll each match into the viewport when navigating search results", async () => { await Promise.all( pages.map(async ([browserName, page]) => { - await page.click("#viewFindButton"); - await page.waitForSelector("#findInput", { visible: true }); - await page.type("#findInput", "hello"); - await page.waitForSelector("#findInput[data-status='']"); + await search(page, "hello"); for (let i = 0; i < 5; i++) { if (i > 0) { @@ -214,9 +208,7 @@ describe("find bar", () => { ); // The selected highlight must be visible in the viewport. - const selected = await page.waitForSelector( - ".textLayer .highlight.selected" - ); + const selected = await page.$(".textLayer .highlight.selected"); expect(await selected.isIntersectingViewport()) .withContext(`In ${browserName}, match ${i + 1}`) .toBeTrue(); From 45e5783c2159351afc32b6485fada07d5db8e7f5 Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Sun, 4 Oct 2026 15:37:45 +0200 Subject: [PATCH 2/3] Reword the names of the find bar integration tests This makes the test names more consistent to improve clarity on what the tests aim to check, and avoids using duplicate test names to make discovery of e.g. failed tests easier in the logs. --- test/integration/find_spec.mjs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/integration/find_spec.mjs b/test/integration/find_spec.mjs index 27a2ed201..6e6353434 100644 --- a/test/integration/find_spec.mjs +++ b/test/integration/find_spec.mjs @@ -50,7 +50,7 @@ describe("find bar", () => { await closePages(pages); }); - it("must highlight text in the right position", async () => { + it("must highlight search results in the right positions", async () => { await Promise.all( pages.map(async ([browserName, page]) => { await search(page, "a"); @@ -98,7 +98,7 @@ describe("find bar", () => { }); }); - describe("highlight all (XFA)", () => { + describe("search in the XFA layer", () => { let pages; beforeEach(async () => { @@ -109,7 +109,7 @@ describe("find bar", () => { await closePages(pages); }); - it("must search xfa correctly", async () => { + it("must find search results in the XFA layer", async () => { await Promise.all( pages.map(async ([browserName, page]) => { await search(page, "preferences"); @@ -125,7 +125,7 @@ describe("find bar", () => { }); }); - describe("issue19207.pdf", () => { + describe("scroll search results into view (CSS scaling)", () => { let pages; beforeEach(async () => { @@ -136,7 +136,7 @@ describe("find bar", () => { await closePages(pages); }); - it("must scroll to the search result text", async () => { + it("must scroll search results into view if CSS scaling is applied", async () => { await Promise.all( pages.map(async ([browserName, page]) => { await search(page, "40"); @@ -148,7 +148,7 @@ describe("find bar", () => { }); }); - describe("scrolls to the search result text for smaller viewports", () => { + describe("scroll search results into view (small viewport)", () => { let pages; beforeEach(async () => { @@ -159,7 +159,7 @@ describe("find bar", () => { await closePages(pages); }); - it("must scroll to the search result text", async () => { + it("must scroll search results into view if the viewport is small", async () => { await Promise.all( pages.map(async ([browserName, page]) => { // Set a smaller viewport to simulate a mobile device @@ -174,7 +174,7 @@ describe("find bar", () => { }); }); - describe("Check that the search results are correctly visible in rotated PDFs (bug 2021392)", () => { + describe("scroll search results into view (rotated pages, bug 2021392)", () => { let pages; beforeEach(async () => { @@ -189,7 +189,7 @@ describe("find bar", () => { await closePages(pages); }); - it("must scroll each match into the viewport when navigating search results", async () => { + it("must scroll search results into view if the pages are rotated", async () => { await Promise.all( pages.map(async ([browserName, page]) => { await search(page, "hello"); From 0e3a0002bd7b0e27bfcd67a0af801a11106c6dd0 Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Sun, 4 Oct 2026 15:59:32 +0200 Subject: [PATCH 3/3] Introduce more find bar integration tests This commit brings code coverage for the `web/pdf_find_bar.js` file to 100% by covering all missing cases, which include: - searching with no results; - searching with multiple results; - highlighting all search results using keyboard navigation; - closing the find bar via the toolbar button and the Escape key. --- test/integration/find_spec.mjs | 150 +++++++++++++++++++++++++++++++- test/integration/test_utils.mjs | 2 +- 2 files changed, 148 insertions(+), 4 deletions(-) diff --git a/test/integration/find_spec.mjs b/test/integration/find_spec.mjs index 6e6353434..036e82674 100644 --- a/test/integration/find_spec.mjs +++ b/test/integration/find_spec.mjs @@ -16,6 +16,7 @@ import { closePages, FSI, + kbFocusNext, loadAndWait, PDI, waitForTextToBe, @@ -30,12 +31,14 @@ function fuzzyMatch(a, b, browserName, pixelFuzz = 3) { .toBeGreaterThan(b - pixelFuzz); } -async function search(page, query) { +async function search(page, query, notFound = false) { await page.click("#viewFindButton"); await page.waitForSelector("#findInput", { visible: true }); await page.type("#findInput", query); - await page.waitForSelector("#findInput[data-status='']"); - await page.waitForSelector(".highlight"); + await page.waitForSelector("#findInput:not([data-status='pending'])"); + if (!notFound) { + await page.waitForSelector(".highlight"); + } } describe("find bar", () => { @@ -96,6 +99,99 @@ describe("find bar", () => { }) ); }); + + it("must highlight search results using keyboard navigation", async () => { + await Promise.all( + pages.map(async ([browserName, page]) => { + await search(page, "a"); + + await kbFocusNext(page, ".toggleButton:has(#findHighlightAll)"); + await page.keyboard.press("Enter"); + + const highlights = await page.$$(".textLayer .highlight"); + expect(highlights.length).withContext(`In ${browserName}`).toEqual(2); + }) + ); + }); + }); + + describe("search with no search results", () => { + let pages; + + beforeEach(async () => { + pages = await loadAndWait("find_all.pdf", ".textLayer", 100); + }); + + afterEach(async () => { + await closePages(pages); + }); + + it("must handle no search results being found", async () => { + await Promise.all( + pages.map(async ([browserName, page]) => { + await search(page, "nonexistent", /* notFound = */ true); + + await waitForTextToBe(page, "#findMsg", "Phrase not found"); + }) + ); + }); + }); + + describe("search with multiple search results", () => { + let pages; + + beforeEach(async () => { + pages = await loadAndWait("find_all.pdf", ".textLayer", 100); + }); + + afterEach(async () => { + await closePages(pages); + }); + + it("must handle multiple search results being found", async () => { + await Promise.all( + pages.map(async ([browserName, page]) => { + await search(page, "a"); + + // Moving forward should update the find count, and message if + // wrapping occurs, correctly. + for (let i = 0; i < 3; i++) { + if (i > 0) { + await page.click("#findNextButton"); + await page.waitForSelector("#findInput[data-status='']"); + } + + await waitForTextToBe( + page, + "#findResultsCount", + `${FSI}${(i % 2) + 1}${PDI} of ${FSI}2${PDI} matches` + ); + } + + await waitForTextToBe( + page, + "#findMsg", + "Reached end of document, continued from top" + ); + + // Moving backwards should also update the find count and message. + await page.click("#findPreviousButton"); + await page.waitForSelector("#findInput[data-status='']"); + + await waitForTextToBe( + page, + "#findResultsCount", + `${FSI}2${PDI} of ${FSI}2${PDI} matches` + ); + + await waitForTextToBe( + page, + "#findMsg", + "Reached top of document, continued from bottom" + ); + }) + ); + }); }); describe("search in the XFA layer", () => { @@ -217,4 +313,52 @@ describe("find bar", () => { ); }); }); + + describe("close the find bar", () => { + let pages; + + beforeEach(async () => { + pages = await loadAndWait("find_all.pdf", ".textLayer", 100); + }); + + afterEach(async () => { + await closePages(pages); + }); + + it("must close the find bar using the toolbar button", async () => { + await Promise.all( + pages.map(async ([browserName, page]) => { + await search(page, "a"); + + let highlights = await page.$$(".textLayer .highlight"); + expect(highlights.length).withContext(`In ${browserName}`).toEqual(1); + + await page.click("#viewFindButton"); + await page.waitForSelector("#findInput", { hidden: true }); + + // Closing the find bar should remove any highlights. + highlights = await page.$$(".textLayer .highlight"); + expect(highlights.length).withContext(`In ${browserName}`).toEqual(0); + }) + ); + }); + + it("must close the find bar using the Escape key", async () => { + await Promise.all( + pages.map(async ([browserName, page]) => { + await search(page, "a"); + + let highlights = await page.$$(".textLayer .highlight"); + expect(highlights.length).withContext(`In ${browserName}`).toEqual(1); + + await page.keyboard.press("Escape"); + await page.waitForSelector("#findInput", { hidden: true }); + + // Closing the find bar should remove any highlights. + highlights = await page.$$(".textLayer .highlight"); + expect(highlights.length).withContext(`In ${browserName}`).toEqual(0); + }) + ); + }); + }); }); diff --git a/test/integration/test_utils.mjs b/test/integration/test_utils.mjs index a44f040a9..a44fcbfe9 100644 --- a/test/integration/test_utils.mjs +++ b/test/integration/test_utils.mjs @@ -1019,7 +1019,7 @@ async function kbFocusNext(page, selector = null) { } } if (selector) { - await page.waitForSelector(`${selector}:focus`, { visible: true }); + await page.waitForSelector(`${selector}:focus-within`, { visible: true }); } }