Files
pdf.js/test/integration/cursor_tools_spec.mjs

144 lines
4.2 KiB
JavaScript

/* Copyright 2026 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
closePages,
getRect,
loadAndWait,
waitForSelectionChange,
} from "./test_utils.mjs";
async function enableSelectTool(page) {
await page.click("#secondaryToolbarToggleButton");
await page.waitForSelector("#secondaryToolbar", { hidden: false });
await page.click("#cursorSelectTool");
await page.waitForFunction(
"window.PDFViewerApplication.pdfCursorTools.activeTool === 0"
);
}
async function enableHandTool(page) {
await page.click("#secondaryToolbarToggleButton");
await page.waitForSelector("#secondaryToolbar", { hidden: false });
await page.click("#cursorHandTool");
await page.waitForFunction(
"window.PDFViewerApplication.pdfCursorTools.activeTool === 1"
);
}
describe("Cursor tools", () => {
describe("Text selection", () => {
let pages;
beforeEach(async () => {
pages = await loadAndWait(
"tracemonkey.pdf",
".textLayer .endOfContent",
100
);
});
afterEach(async () => {
await closePages(pages);
});
it("check that text selection works", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await enableSelectTool(page);
const spanRect = await getRect(
page,
`.page[data-page-number="1"] > .textLayer > span`
);
const x = spanRect.x + 1,
y = spanRect.y + spanRect.height / 2;
await page.mouse.click(x, y, { count: 3 });
await waitForSelectionChange(
page,
"Trace-based Just-in-Time Type Specialization for Dynamic"
);
// Remove the selection.
await page.mouse.click(x, y);
await waitForSelectionChange(page, "");
})
);
});
});
describe("Hand tool", () => {
let pages;
beforeEach(async () => {
pages = await loadAndWait(
"tracemonkey.pdf",
".textLayer .endOfContent",
100
);
});
afterEach(async () => {
await closePages(pages);
});
it("check that hand tool scrolling works", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await enableHandTool(page);
const viewerRect = await getRect(page, "#viewerContainer");
const x = Math.floor(viewerRect.x + viewerRect.width / 2),
startY = Math.floor(viewerRect.y + (3 * viewerRect.height) / 4),
endY = Math.floor(viewerRect.y + viewerRect.height / 4),
steps = 10;
const initialScrollTop = await page.evaluate(
() => document.getElementById("viewerContainer").scrollTop
);
await page.mouse.move(x, startY);
await page.mouse.down();
// Puppeteer's `steps` option generates fractional intermediate
// positions, which Firefox doesn't support.
for (let i = 1; i <= steps; i++) {
await page.mouse.move(
x,
Math.round(startY + ((endY - startY) * i) / steps)
);
}
await page.mouse.up();
const scrollTop = await page.evaluate(
() => document.getElementById("viewerContainer").scrollTop
);
const scrollDelta = scrollTop - initialScrollTop,
expectedScrollDelta = startY - endY;
expect(Math.abs(scrollDelta - expectedScrollDelta))
.withContext(
`Expected a scroll delta of ${expectedScrollDelta}, got ${scrollDelta}, in ${browserName}`
)
.toBeLessThan(1);
// Finally, disable the hand tool.
await enableSelectTool(page);
})
);
});
});
});