Merge pull request #17036 from calixteman/fix_test_freetext

[Editor] Remove almost all the waitForTimeout from the freetext integration tests
This commit is contained in:
calixteman 2023-09-29 11:41:53 +02:00 committed by GitHub
commit 46940a5a52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 594 additions and 334 deletions

File diff suppressed because it is too large Load Diff

View File

@ -21,6 +21,7 @@ const {
getComputedStyleSelector,
loadAndWait,
getFirstSerialized,
scrollIntoView,
} = require("./test_utils.js");
describe("Interaction", () => {
@ -538,12 +539,7 @@ describe("Interaction", () => {
text = await actAndWaitForInput(
page,
getSelector(refOpen),
async () => {
await page.evaluate(selector => {
const element = window.document.querySelector(selector);
element.scrollIntoView();
}, getSelector(refOpen));
},
() => scrollIntoView(page, getSelector(refOpen)),
false
);
expect(text)
@ -818,9 +814,7 @@ describe("Interaction", () => {
"window.PDFViewerApplication.scriptingReady === true"
);
await page.evaluate(selector => {
window.document.querySelector(selector).scrollIntoView();
}, getSelector("171R"));
await scrollIntoView(page, getSelector("171R"));
let sum = 0;
for (const [id, val] of [
@ -853,11 +847,7 @@ describe("Interaction", () => {
// Some unrendered annotations have been updated, so check
// that they've the correct value when rendered.
await page.evaluate(() => {
window.document
.querySelector('.page[data-page-number = "4"]')
.scrollIntoView();
});
await scrollIntoView(page, '.page[data-page-number = "4"]');
await page.waitForSelector(getSelector("299R"), {
timeout: 0,
});

View File

@ -97,16 +97,27 @@ function getSelectedEditors(page) {
}
exports.getSelectedEditors = getSelectedEditors;
async function waitForEvent(page, eventName, timeout = 30000) {
await Promise.race([
async function waitForEvent(page, eventName, timeout = 5000) {
const hasTimedout = await Promise.race([
// add event listener and wait for event to fire before returning
page.evaluate(name => {
return new Promise(resolve => {
document.addEventListener(name, resolve, { once: true });
});
}, eventName),
page.waitForTimeout(timeout),
page.evaluate(
name =>
new Promise(resolve => {
document.addEventListener(name, () => resolve(false), { once: true });
}),
eventName
),
page.evaluate(
timeOut =>
new Promise(resolve => {
setTimeout(() => resolve(true), timeOut);
}),
timeout
),
]);
if (hasTimedout === true) {
console.log(`waitForEvent: timeout waiting for ${eventName}`);
}
}
exports.waitForEvent = waitForEvent;
@ -119,15 +130,27 @@ const waitForStorageEntries = async (page, nEntries) => {
};
exports.waitForStorageEntries = waitForStorageEntries;
const waitForSelectedEditor = async (page, selector) => {
const waitForSerialized = async (page, nEntries) => {
await page.waitForFunction(
sel => document.querySelector(sel).classList.contains("selectedEditor"),
n =>
(window.PDFViewerApplication.pdfDocument.annotationStorage.serializable
.map?.size ?? 0) === n,
{},
selector
nEntries
);
};
exports.waitForSerialized = waitForSerialized;
const waitForSelectedEditor = async (page, selector) => {
await page.waitForSelector(`${selector}.selectedEditor`);
};
exports.waitForSelectedEditor = waitForSelectedEditor;
const waitForUnselectedEditor = async (page, selector) => {
await page.waitForSelector(`${selector}:not(.selectedEditor)`);
};
exports.waitForUnselectedEditor = waitForUnselectedEditor;
const mockClipboard = async pages => {
await Promise.all(
pages.map(async ([_, page]) => {
@ -203,6 +226,7 @@ async function dragAndDropAnnotation(page, startX, startY, tX, tY) {
await page.waitForTimeout(10);
await page.mouse.move(startX + tX, startY + tY);
await page.mouse.up();
await page.waitForSelector("#viewer:not(.noUserSelect)");
}
exports.dragAndDropAnnotation = dragAndDropAnnotation;
@ -217,3 +241,39 @@ async function waitForAnnotationEditorLayer(page) {
});
}
exports.waitForAnnotationEditorLayer = waitForAnnotationEditorLayer;
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 element = document.querySelector(sel);
const { top, bottom } = element.getBoundingClientRect();
return Math.abs(top) < 100 || Math.abs(bottom - window.innerHeight) < 100;
},
{},
selector
);
}
exports.scrollIntoView = scrollIntoView;