Convert the integration test-files to JavaScript modules

This commit is contained in:
Jonas Jenwald 2023-10-12 13:16:58 +02:00
parent 33456d3704
commit 9878d058fe
9 changed files with 83 additions and 68 deletions

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
const { closePages, loadAndWait } = require("./test_utils.js");
import { closePages, loadAndWait } from "./test_utils.mjs";
describe("accessibility", () => {
describe("structure tree", () => {

View File

@ -13,12 +13,12 @@
* limitations under the License.
*/
const {
import {
closePages,
getSelector,
getQuerySelector,
getSelector,
loadAndWait,
} = require("./test_utils.js");
} from "./test_utils.mjs";
describe("Annotation highlight", () => {
describe("annotation-highlight.pdf", () => {

View File

@ -13,13 +13,13 @@
* limitations under the License.
*/
const {
import {
closePages,
loadAndWait,
mockClipboard,
waitForEvent,
waitForTextLayer,
} = require("./test_utils.js");
} from "./test_utils.mjs";
const selectAll = async page => {
const promise = waitForEvent(page, "selectionchange");

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
const { closePages, loadAndWait } = require("./test_utils.js");
import { closePages, loadAndWait } from "./test_utils.mjs";
function fuzzyMatch(a, b, browserName, pixelFuzz = 3) {
expect(a)

View File

@ -13,24 +13,23 @@
* limitations under the License.
*/
const {
import {
closePages,
dragAndDropAnnotation,
getEditors,
getEditorSelector,
getSelectedEditors,
getFirstSerialized,
getSelectedEditors,
getSerialized,
loadAndWait,
scrollIntoView,
waitForEvent,
waitForSelectedEditor,
waitForUnselectedEditor,
waitForSerialized,
waitForStorageEntries,
} = require("./test_utils.js");
const PNG = require("pngjs").PNG;
waitForUnselectedEditor,
} from "./test_utils.mjs";
import { PNG } from "pngjs";
const copyPaste = async page => {
let promise = waitForEvent(page, "copy");

View File

@ -13,12 +13,12 @@
* limitations under the License.
*/
const {
import {
closePages,
getSelectedEditors,
loadAndWait,
waitForStorageEntries,
} = require("./test_utils.js");
} from "./test_utils.mjs";
const waitForPointerUp = page =>
page.evaluate(

View File

@ -13,16 +13,16 @@
* limitations under the License.
*/
const {
import {
clearInput,
closePages,
getSelector,
getQuerySelector,
getComputedStyleSelector,
loadAndWait,
getFirstSerialized,
getQuerySelector,
getSelector,
loadAndWait,
scrollIntoView,
} = require("./test_utils.js");
} from "./test_utils.mjs";
describe("Interaction", () => {
async function actAndWaitForInput(page, selector, action, clear = true) {

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
const {
import {
closePages,
getEditorDimensions,
getEditorSelector,
@ -21,11 +21,14 @@ const {
loadAndWait,
serializeBitmapDimensions,
waitForAnnotationEditorLayer,
waitForStorageEntries,
waitForSelectedEditor,
} = require("./test_utils.js");
const path = require("path");
const fs = require("fs");
waitForStorageEntries,
} from "./test_utils.mjs";
import { fileURLToPath } from "url";
import fs from "fs";
import path from "path";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const selectAll = async page => {
await page.keyboard.down("Control");

View File

@ -13,8 +13,8 @@
* limitations under the License.
*/
exports.loadAndWait = (filename, selector, zoom, pageSetup) =>
Promise.all(
function loadAndWait(filename, selector, zoom, pageSetup) {
return Promise.all(
global.integrationSessions.map(async session => {
const page = await session.browser.newPage();
@ -49,40 +49,42 @@ exports.loadAndWait = (filename, selector, zoom, pageSetup) =>
return [session.name, page];
})
);
}
exports.closePages = pages =>
Promise.all(
function closePages(pages) {
return Promise.all(
pages.map(async ([_, page]) => {
// Avoid to keep something from a previous test.
await page.evaluate(() => window.localStorage.clear());
await page.close();
})
);
}
exports.clearInput = async (page, selector) => {
async function clearInput(page, selector) {
await page.click(selector);
await page.keyboard.down("Control");
await page.keyboard.press("A");
await page.keyboard.up("Control");
await page.keyboard.press("Backspace");
await page.waitForTimeout(10);
};
}
function getSelector(id) {
return `[data-element-id="${id}"]`;
}
exports.getSelector = getSelector;
function getQuerySelector(id) {
return `document.querySelector('${getSelector(id)}')`;
}
exports.getQuerySelector = getQuerySelector;
function getComputedStyleSelector(id) {
return `getComputedStyle(${getQuerySelector(id)})`;
}
exports.getComputedStyleSelector = getComputedStyleSelector;
exports.getEditorSelector = n => `#pdfjs_internal_editor_${n}`;
function getEditorSelector(n) {
return `#pdfjs_internal_editor_${n}`;
}
function getSelectedEditors(page) {
return page.evaluate(() => {
@ -95,7 +97,6 @@ function getSelectedEditors(page) {
return results;
});
}
exports.getSelectedEditors = getSelectedEditors;
async function waitForEvent(page, eventName, timeout = 5000) {
const hasTimedout = await Promise.race([
@ -119,40 +120,35 @@ async function waitForEvent(page, eventName, timeout = 5000) {
console.log(`waitForEvent: timeout waiting for ${eventName}`);
}
}
exports.waitForEvent = waitForEvent;
const waitForStorageEntries = async (page, nEntries) => {
await page.waitForFunction(
async function waitForStorageEntries(page, nEntries) {
return page.waitForFunction(
n => window.PDFViewerApplication.pdfDocument.annotationStorage.size === n,
{},
nEntries
);
};
exports.waitForStorageEntries = waitForStorageEntries;
}
const waitForSerialized = async (page, nEntries) => {
await page.waitForFunction(
async function waitForSerialized(page, nEntries) {
return page.waitForFunction(
n =>
(window.PDFViewerApplication.pdfDocument.annotationStorage.serializable
.map?.size ?? 0) === n,
{},
nEntries
);
};
exports.waitForSerialized = waitForSerialized;
}
const waitForSelectedEditor = async (page, selector) => {
await page.waitForSelector(`${selector}.selectedEditor`);
};
exports.waitForSelectedEditor = waitForSelectedEditor;
async function waitForSelectedEditor(page, selector) {
return page.waitForSelector(`${selector}.selectedEditor`);
}
const waitForUnselectedEditor = async (page, selector) => {
await page.waitForSelector(`${selector}:not(.selectedEditor)`);
};
exports.waitForUnselectedEditor = waitForUnselectedEditor;
async function waitForUnselectedEditor(page, selector) {
return page.waitForSelector(`${selector}:not(.selectedEditor)`);
}
const mockClipboard = async pages => {
await Promise.all(
async function mockClipboard(pages) {
return Promise.all(
pages.map(async ([_, page]) => {
await page.evaluate(() => {
let data = null;
@ -164,8 +160,7 @@ const mockClipboard = async pages => {
});
})
);
};
exports.mockClipboard = mockClipboard;
}
async function getSerialized(page, filter = undefined) {
const values = await page.evaluate(() => {
@ -175,11 +170,10 @@ async function getSerialized(page, filter = undefined) {
});
return filter ? values.map(filter) : values;
}
exports.getSerialized = getSerialized;
const getFirstSerialized = async (page, filter = undefined) =>
(await getSerialized(page, filter))[0];
exports.getFirstSerialized = getFirstSerialized;
async function getFirstSerialized(page, filter = undefined) {
return (await getSerialized(page, filter))[0];
}
function getEditors(page, kind) {
return page.evaluate(aKind => {
@ -191,7 +185,6 @@ function getEditors(page, kind) {
return results;
}, kind);
}
exports.getEditors = getEditors;
function getEditorDimensions(page, id) {
return page.evaluate(n => {
@ -205,7 +198,6 @@ function getEditorDimensions(page, id) {
};
}, id);
}
exports.getEditorDimensions = getEditorDimensions;
async function serializeBitmapDimensions(page) {
await page.waitForFunction(() => {
@ -229,7 +221,6 @@ async function serializeBitmapDimensions(page) {
: [];
});
}
exports.serializeBitmapDimensions = serializeBitmapDimensions;
async function dragAndDropAnnotation(page, startX, startY, tX, tY) {
await page.mouse.move(startX, startY);
@ -239,7 +230,6 @@ async function dragAndDropAnnotation(page, startX, startY, tX, tY) {
await page.mouse.up();
await page.waitForSelector("#viewer:not(.noUserSelect)");
}
exports.dragAndDropAnnotation = dragAndDropAnnotation;
async function waitForAnnotationEditorLayer(page) {
return page.evaluate(() => {
@ -251,7 +241,6 @@ async function waitForAnnotationEditorLayer(page) {
});
});
}
exports.waitForAnnotationEditorLayer = waitForAnnotationEditorLayer;
async function waitForTextLayer(page) {
return page.evaluate(() => {
@ -260,7 +249,6 @@ async function waitForTextLayer(page) {
});
});
}
exports.waitForTextLayer = waitForTextLayer;
async function scrollIntoView(page, selector) {
const promise = page.evaluate(
@ -296,4 +284,29 @@ async function scrollIntoView(page, selector) {
selector
);
}
exports.scrollIntoView = scrollIntoView;
export {
clearInput,
closePages,
dragAndDropAnnotation,
getComputedStyleSelector,
getEditorDimensions,
getEditors,
getEditorSelector,
getFirstSerialized,
getQuerySelector,
getSelectedEditors,
getSelector,
getSerialized,
loadAndWait,
mockClipboard,
scrollIntoView,
serializeBitmapDimensions,
waitForAnnotationEditorLayer,
waitForEvent,
waitForSelectedEditor,
waitForSerialized,
waitForStorageEntries,
waitForTextLayer,
waitForUnselectedEditor,
};