mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 09:20:06 +02:00
Use waitForEvent
in the pasteFromClipboard
integration test helper function
This code contains the same bug that the previous commit fixed in `waitForEvent`, namely that we don't clear the timeout if the event is triggered. By using the now fixed `waitForEvent` function we not only deduplicate this code but we also fix this issue so that no incorrect timeout logs show up anymore.
This commit is contained in:
parent
4b95d689de
commit
f974b75d69
1 changed files with 29 additions and 36 deletions
|
@ -186,39 +186,54 @@ async function getSpanRectFromText(page, pageNumber, text) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function waitForEvent(page, eventName, timeout = 5000) {
|
async function waitForEvent(
|
||||||
|
page,
|
||||||
|
eventName,
|
||||||
|
selector = null,
|
||||||
|
validator = null,
|
||||||
|
timeout = 5000
|
||||||
|
) {
|
||||||
const handle = await page.evaluateHandle(
|
const handle = await page.evaluateHandle(
|
||||||
(name, timeOut) => {
|
(name, sel, validate, timeOut) => {
|
||||||
let callback = null,
|
let callback = null,
|
||||||
timeoutId = null;
|
timeoutId = null;
|
||||||
|
const element = sel ? document.querySelector(sel) : document;
|
||||||
return [
|
return [
|
||||||
Promise.race([
|
Promise.race([
|
||||||
new Promise(resolve => {
|
new Promise(resolve => {
|
||||||
// add event listener and wait for event to fire before returning
|
// The promise is resolved if the event fired in the context of the
|
||||||
callback = () => {
|
// selector and, if a validator is defined, the event data satisfies
|
||||||
|
// the conditions of the validator function.
|
||||||
|
callback = e => {
|
||||||
if (timeoutId) {
|
if (timeoutId) {
|
||||||
clearTimeout(timeoutId);
|
clearTimeout(timeoutId);
|
||||||
}
|
}
|
||||||
resolve(false);
|
// eslint-disable-next-line no-eval
|
||||||
|
resolve(validate ? eval(`(${validate})`)(e) : true);
|
||||||
};
|
};
|
||||||
document.addEventListener(name, callback, { once: true });
|
element.addEventListener(name, callback, { once: true });
|
||||||
}),
|
}),
|
||||||
new Promise(resolve => {
|
new Promise(resolve => {
|
||||||
timeoutId = setTimeout(() => {
|
timeoutId = setTimeout(() => {
|
||||||
document.removeEventListener(name, callback);
|
element.removeEventListener(name, callback);
|
||||||
resolve(true);
|
resolve(null);
|
||||||
}, timeOut);
|
}, timeOut);
|
||||||
}),
|
}),
|
||||||
]),
|
]),
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
eventName,
|
eventName,
|
||||||
|
selector,
|
||||||
|
validator ? validator.toString() : null,
|
||||||
timeout
|
timeout
|
||||||
);
|
);
|
||||||
const hasTimedout = await awaitPromise(handle);
|
const success = await awaitPromise(handle);
|
||||||
if (hasTimedout === true) {
|
if (success === null) {
|
||||||
console.log(`waitForEvent: timeout waiting for ${eventName}`);
|
console.log(`waitForEvent: ${eventName} didn't trigger within the timeout`);
|
||||||
|
} else if (!success) {
|
||||||
|
console.log(`waitForEvent: ${eventName} triggered, but validation failed`);
|
||||||
}
|
}
|
||||||
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function waitForStorageEntries(page, nEntries) {
|
async function waitForStorageEntries(page, nEntries) {
|
||||||
|
@ -291,35 +306,13 @@ async function pasteFromClipboard(page, data, selector, timeout = 100) {
|
||||||
await navigator.clipboard.write([new ClipboardItem(items)]);
|
await navigator.clipboard.write([new ClipboardItem(items)]);
|
||||||
}, data);
|
}, data);
|
||||||
|
|
||||||
|
const validator = e => e.clipboardData.items.length !== 0;
|
||||||
let hasPasteEvent = false;
|
let hasPasteEvent = false;
|
||||||
while (!hasPasteEvent) {
|
while (!hasPasteEvent) {
|
||||||
// We retry to paste if nothing has been pasted before the timeout.
|
// We retry to paste if nothing has been pasted before the timeout.
|
||||||
const handle = await page.evaluateHandle(
|
const promise = waitForEvent(page, "paste", selector, validator);
|
||||||
(sel, timeOut) => {
|
|
||||||
let callback = null;
|
|
||||||
const element = sel ? document.querySelector(sel) : document;
|
|
||||||
return [
|
|
||||||
Promise.race([
|
|
||||||
new Promise(resolve => {
|
|
||||||
callback = e => resolve(e.clipboardData.items.length !== 0);
|
|
||||||
element.addEventListener("paste", callback, {
|
|
||||||
once: true,
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
new Promise(resolve => {
|
|
||||||
setTimeout(() => {
|
|
||||||
element.removeEventListener("paste", callback);
|
|
||||||
resolve(false);
|
|
||||||
}, timeOut);
|
|
||||||
}),
|
|
||||||
]),
|
|
||||||
];
|
|
||||||
},
|
|
||||||
selector,
|
|
||||||
timeout
|
|
||||||
);
|
|
||||||
await kbPaste(page);
|
await kbPaste(page);
|
||||||
hasPasteEvent = await awaitPromise(handle);
|
hasPasteEvent = await promise;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue