Skip to content

Commit

Permalink
Refactor the copy/paste logic in the integration tests
Browse files Browse the repository at this point in the history
The integration tests are currently not consistent in how they do
copy/pasting: some tests use the `kbCopy`/`kbPaste` functions with
waiting for the event inline, some have their own helper function to
combine those actions and some even call `kbCopy`/`kbPaste` without
waiting for the event at all (which can cause intermittent failures).

This commit fixes the issues by providing a set of four helper functions
that all tests use and that abstract e.g. waiting for the event away
from the caller. This makes the invididual tests simpler and consistent,
reduces code duplication and fixes possible intermittent failures
due to not waiting for events to trigger.
  • Loading branch information
timvandermeij committed Jun 25, 2024
1 parent 5885874 commit 0b61902
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 88 deletions.
12 changes: 3 additions & 9 deletions test/integration/copy_paste_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import {
closePages,
kbCopy,
copy,
kbSelectAll,
loadAndWait,
mockClipboard,
Expand Down Expand Up @@ -55,10 +55,7 @@ describe("Copy and paste", () => {
);
await selectAll(page);

const promise = waitForEvent(page, "copy");
await kbCopy(page);
await promise;

await copy(page);
await page.waitForFunction(
`document.querySelector('#viewerContainer').style.cursor !== "wait"`
);
Expand Down Expand Up @@ -159,10 +156,7 @@ describe("Copy and paste", () => {
);
await selectAll(page);

const promise = waitForEvent(page, "copy");
await kbCopy(page);
await promise;

await copy(page);
await page.waitForFunction(
`document.querySelector('#viewerContainer').style.cursor !== "wait"`
);
Expand Down
100 changes: 38 additions & 62 deletions test/integration/freetext_editor_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import {
awaitPromise,
closePages,
copy,
copyData,
createPromise,
dragAndDropAnnotation,
firstPageOnTop,
Expand All @@ -30,21 +32,19 @@ import {
kbBigMoveLeft,
kbBigMoveRight,
kbBigMoveUp,
kbCopy,
kbGoToBegin,
kbGoToEnd,
kbModifierDown,
kbModifierUp,
kbPaste,
kbRedo,
kbSelectAll,
kbUndo,
loadAndWait,
pasteFromClipboard,
paste,
pasteData,
scrollIntoView,
switchToEditor,
waitForAnnotationEditorLayer,
waitForEvent,
waitForSelectedEditor,
waitForSerialized,
waitForStorageEntries,
Expand All @@ -53,16 +53,6 @@ import {
} from "./test_utils.mjs";
import { PNG } from "pngjs";

const copyPaste = async page => {
let promise = waitForEvent(page, "copy");
await kbCopy(page);
await promise;

promise = waitForEvent(page, "paste");
await kbPaste(page);
await promise;
};

const selectAll = async page => {
await kbSelectAll(page);
await page.waitForFunction(
Expand Down Expand Up @@ -187,7 +177,8 @@ describe("FreeText Editor", () => {
);

await waitForSelectedEditor(page, getEditorSelector(0));
await copyPaste(page);
await copy(page);
await paste(page);
await page.waitForSelector(getEditorSelector(1), {
visible: true,
});
Expand All @@ -203,7 +194,8 @@ describe("FreeText Editor", () => {

expect(pastedContent).withContext(`In ${browserName}`).toEqual(content);

await copyPaste(page);
await copy(page);
await paste(page);
await page.waitForSelector(getEditorSelector(2), {
visible: true,
});
Expand Down Expand Up @@ -263,7 +255,8 @@ describe("FreeText Editor", () => {
);

await waitForSelectedEditor(page, getEditorSelector(3));
await copyPaste(page);
await copy(page);
await paste(page);
await page.waitForSelector(getEditorSelector(4), {
visible: true,
});
Expand All @@ -276,9 +269,7 @@ describe("FreeText Editor", () => {
);

for (let i = 0; i < 2; i++) {
const promise = waitForEvent(page, "paste");
await kbPaste(page);
await promise;
await paste(page);
await page.waitForSelector(getEditorSelector(5 + i));
}

Expand Down Expand Up @@ -597,7 +588,8 @@ describe("FreeText Editor", () => {
.withContext(`In ${browserName}`)
.toEqual([0, 1, 3]);

await copyPaste(page);
await copy(page);
await paste(page);
await page.waitForSelector(getEditorSelector(6), {
visible: true,
});
Expand Down Expand Up @@ -1275,7 +1267,8 @@ describe("FreeText Editor", () => {
);
await waitForSelectedEditor(page, getEditorSelector(1));

await copyPaste(page);
await copy(page);
await paste(page);
await page.waitForSelector(getEditorSelector(6), {
visible: true,
});
Expand Down Expand Up @@ -3425,14 +3418,11 @@ describe("FreeText Editor", () => {
);

await select(0);
await pasteFromClipboard(
page,
{
"text/html": "<b>Bold Foo</b>",
"text/plain": "Foo",
},
`${editorSelector} .internal`
);
await copyData(page, {
"text/html": "<b>Bold Foo</b>",
"text/plain": "Foo",
});
await pasteData(page, `${editorSelector} .internal`);

let lastText = data;

Expand All @@ -3442,28 +3432,20 @@ describe("FreeText Editor", () => {
expect(text).withContext(`In ${browserName}`).toEqual(lastText);

await select(3);
await pasteFromClipboard(
page,
{
"text/html": "<b>Bold Bar</b><br><b>Oof</b>",
"text/plain": "Bar\nOof",
},
`${editorSelector} .internal`
);
await copyData(page, {
"text/html": "<b>Bold Bar</b><br><b>Oof</b>",
"text/plain": "Bar\nOof",
});
await pasteData(page, `${editorSelector} .internal`);

await waitForTextChange(lastText, editorSelector);
text = await getText(editorSelector);
lastText = `FooBar\nOof${data}`;
expect(text).withContext(`In ${browserName}`).toEqual(lastText);

await select(0);
await pasteFromClipboard(
page,
{
"text/html": "<b>basic html</b>",
},
`${editorSelector} .internal`
);
await copyData(page, { "text/html": "<b>basic html</b>" });
await pasteData(page, `${editorSelector} .internal`);

// Nothing should change, so it's hard to wait on something.
// eslint-disable-next-line no-restricted-syntax
Expand All @@ -3477,15 +3459,12 @@ describe("FreeText Editor", () => {
const prevHTML = await getHTML();

// Try to paste an image.
await pasteFromClipboard(
page,
{
"image/png":
// 1x1 transparent png.
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
},
`${editorSelector} .internal`
);
await copyData(page, {
"image/png":
// 1x1 transparent png.
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
});
await pasteData(page, `${editorSelector} .internal`);

// Nothing should change, so it's hard to wait on something.
// eslint-disable-next-line no-restricted-syntax
Expand All @@ -3505,14 +3484,11 @@ describe("FreeText Editor", () => {
});

const fooBar = "Foo\nBar\nOof";
await pasteFromClipboard(
page,
{
"text/html": "<b>html</b>",
"text/plain": fooBar,
},
`${editorSelector} .internal`
);
await copyData(page, {
"text/html": "<b>html</b>",
"text/plain": fooBar,
});
await pasteData(page, `${editorSelector} .internal`);

await waitForTextChange("", editorSelector);
text = await getText(editorSelector);
Expand Down
25 changes: 12 additions & 13 deletions test/integration/stamp_editor_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ import {
applyFunctionToEditor,
awaitPromise,
closePages,
copy,
copyData,
getEditorDimensions,
getEditorSelector,
getFirstSerialized,
getRect,
getSerialized,
kbBigMoveDown,
kbBigMoveRight,
kbCopy,
kbPaste,
kbSelectAll,
kbUndo,
loadAndWait,
pasteFromClipboard,
paste,
pasteData,
scrollIntoView,
serializeBitmapDimensions,
switchToEditor,
Expand Down Expand Up @@ -78,12 +79,10 @@ const copyImage = async (page, imagePath, number) => {
const data = fs
.readFileSync(path.join(__dirname, imagePath))
.toString("base64");
await pasteFromClipboard(
page,
{ "image/png": `data:image/png;base64,${data}` },
"",
500
);

await copyData(page, { "image/png": `data:image/png;base64,${data}` });
await pasteData(page);

await waitForImage(page, getEditorSelector(number));
};

Expand Down Expand Up @@ -574,13 +573,13 @@ describe("Stamp Editor", () => {
await page1.click("#editorStamp");

await copyImage(page1, "../images/firefox_logo.png", 0);
await kbCopy(page1);
await copy(page1);

const [, page2] = pages2[i];
await page2.bringToFront();
await page2.click("#editorStamp");

await kbPaste(page2);
await paste(page2);

await waitForImage(page2, getEditorSelector(0));
}
Expand Down Expand Up @@ -835,8 +834,8 @@ describe("Stamp Editor", () => {
);
await page.waitForSelector(`${getEditorSelector(0)} .altText.done`);

await kbCopy(page);
await kbPaste(page);
await copy(page);
await paste(page);
await page.waitForSelector(`${getEditorSelector(1)} .altText.done`);
await waitForSerialized(page, 2);

Expand Down
23 changes: 19 additions & 4 deletions test/integration/test_utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,13 @@ async function mockClipboard(pages) {
);
}

async function pasteFromClipboard(page, data, selector, timeout = 100) {
async function copy(page) {
const promise = waitForEvent(page, "copy");
await kbCopy(page);
await promise;
}

async function copyData(page, data) {
await page.evaluate(async dat => {
const items = Object.create(null);
for (const [type, value] of Object.entries(dat)) {
Expand All @@ -305,7 +311,15 @@ async function pasteFromClipboard(page, data, selector, timeout = 100) {
}
await navigator.clipboard.write([new ClipboardItem(items)]);
}, data);
}

async function paste(page) {
const promise = waitForEvent(page, "paste");
await kbPaste(page);
await promise;
}

async function pasteData(page, selector = null) {
const validator = e => e.clipboardData.items.length !== 0;
let hasPasteEvent = false;
while (!hasPasteEvent) {
Expand Down Expand Up @@ -634,6 +648,8 @@ export {
clearInput,
closePages,
closeSinglePage,
copy,
copyData,
createPromise,
dragAndDropAnnotation,
firstPageOnTop,
Expand All @@ -654,21 +670,20 @@ export {
kbBigMoveLeft,
kbBigMoveRight,
kbBigMoveUp,
kbCopy,
kbDeleteLastWord,
kbFocusNext,
kbFocusPrevious,
kbGoToBegin,
kbGoToEnd,
kbModifierDown,
kbModifierUp,
kbPaste,
kbRedo,
kbSelectAll,
kbUndo,
loadAndWait,
mockClipboard,
pasteFromClipboard,
paste,
pasteData,
scrollIntoView,
serializeBitmapDimensions,
switchToEditor,
Expand Down

0 comments on commit 0b61902

Please sign in to comment.