Files
ladybird/Tests/LibWeb/Text/input/include.js
Andrew Kaster acd604c5e1 LibWeb: Add a test-only API to spoof the current URL and origin
This is not that easy to use for test developers, as forgetting to set
the url back to its original state after testing your specific API will
cause future navigations to fail in inexplicable ways.
2024-09-20 22:41:24 +01:00

87 lines
2.1 KiB
JavaScript

var __outputElement = null;
let __alreadyCalledTest = false;
let __originalURL = null;
function __preventMultipleTestFunctions() {
if (__alreadyCalledTest) {
throw new Error("You must only call test() or asyncTest() once per page");
}
__alreadyCalledTest = true;
}
if (globalThis.internals === undefined) {
internals = {
signalTextTestIsDone: function () {},
spoofCurrentURL: function (url) {},
};
}
function __finishTest() {
if (__originalURL) {
internals.spoofCurrentURL(__originalURL);
}
internals.signalTextTestIsDone();
}
function spoofCurrentURL(url) {
if (__originalURL === null) {
__originalURL = document.location.href;
}
internals.spoofCurrentURL(url);
}
function println(s) {
__outputElement.appendChild(document.createTextNode(s + "\n"));
}
function printElement(e) {
let element_string = `<${e.nodeName} `;
if (e.id) element_string += `id="${e.id}" `;
element_string += ">";
println(element_string);
}
function animationFrame() {
const { promise, resolve } = Promise.withResolvers();
requestAnimationFrame(resolve);
return promise;
}
function timeout(ms) {
const { promise, resolve } = Promise.withResolvers();
setTimeout(resolve, ms);
return promise;
}
document.addEventListener("DOMContentLoaded", function () {
__outputElement = document.createElement("pre");
__outputElement.setAttribute("id", "out");
document.body.appendChild(__outputElement);
});
function test(f) {
__preventMultipleTestFunctions();
document.addEventListener("DOMContentLoaded", f);
window.addEventListener("load", () => {
__finishTest();
});
}
function asyncTest(f) {
const done = () => {
__preventMultipleTestFunctions();
__finishTest();
};
document.addEventListener("DOMContentLoaded", () => {
f(done);
});
}
function promiseTest(f) {
document.addEventListener("DOMContentLoaded", () => {
f().then(() => {
__preventMultipleTestFunctions();
__finishTest();
});
});
}