diff --git a/test/integration/test_utils.mjs b/test/integration/test_utils.mjs index 09a7dcd9d..55f5f82b3 100644 --- a/test/integration/test_utils.mjs +++ b/test/integration/test_utils.mjs @@ -543,6 +543,14 @@ async function dragAndDrop(page, selector, translations, steps = 1) { await page.waitForSelector("#viewer:not(.noUserSelect)"); } +function waitForPageChanging(page) { + return createPromise(page, resolve => { + window.PDFViewerApplication.eventBus.on("pagechanging", resolve, { + once: true, + }); + }); +} + function waitForAnnotationEditorLayer(page) { return createPromise(page, resolve => { window.PDFViewerApplication.eventBus.on( @@ -944,6 +952,7 @@ export { waitForEntryInStorage, waitForEvent, waitForNoElement, + waitForPageChanging, waitForPageRendered, waitForPointerUp, waitForSandboxTrip, diff --git a/test/integration/viewer_spec.mjs b/test/integration/viewer_spec.mjs index f2eed67ab..32c446c91 100644 --- a/test/integration/viewer_spec.mjs +++ b/test/integration/viewer_spec.mjs @@ -20,6 +20,7 @@ import { getSpanRectFromText, loadAndWait, scrollIntoView, + waitForPageChanging, waitForPageRendered, } from "./test_utils.mjs"; import { PNG } from "pngjs"; @@ -1273,4 +1274,48 @@ describe("PDF viewer", () => { ); }); }); + + describe("Keyboard scrolling on startup (bug 843653)", () => { + let pages; + + beforeEach(async () => { + pages = await loadAndWait("tracemonkey.pdf", ".textLayer .endOfContent"); + }); + + afterEach(async () => { + await closePages(pages); + }); + + it("must check that keyboard scrolling works without having to give the focus to the viewer", async () => { + await Promise.all( + pages.map(async ([browserName, page]) => { + const pdfViewer = await page.evaluateHandle( + () => window.PDFViewerApplication.pdfViewer + ); + + // The viewer should not have the focus. + const hasFocus = await pdfViewer.evaluate(viewer => + viewer.container.contains(document.activeElement) + ); + expect(hasFocus).withContext(`In ${browserName}`).toBeFalse(); + + let currentPageNumber = await pdfViewer.evaluate( + viewer => viewer.currentPageNumber + ); + expect(currentPageNumber).withContext(`In ${browserName}`).toBe(1); + + // Press the 'PageDown' key to check that it works. + const handle = await waitForPageChanging(page); + await page.keyboard.press("PageDown"); + await awaitPromise(handle); + + // The second page should be displayed. + currentPageNumber = await pdfViewer.evaluate( + viewer => viewer.currentPageNumber + ); + expect(currentPageNumber).withContext(`In ${browserName}`).toBe(2); + }) + ); + }); + }); }); diff --git a/web/app.js b/web/app.js index 084f981d0..8bc23b358 100644 --- a/web/app.js +++ b/web/app.js @@ -1455,11 +1455,6 @@ const PDFViewerApplication = { spreadMode, }); this.eventBus.dispatch("documentinit", { source: this }); - // Make all navigation keys work on document load, - // unless the viewer is embedded in a web page. - if (!this.isViewerEmbedded) { - pdfViewer.focus(); - } // For documents with different page sizes, once all pages are // resolved, ensure that the correct location becomes visible on load.