diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /testing/web-platform/tests/resources/test/nested-testharness.js | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/resources/test/nested-testharness.js')
-rw-r--r-- | testing/web-platform/tests/resources/test/nested-testharness.js | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/testing/web-platform/tests/resources/test/nested-testharness.js b/testing/web-platform/tests/resources/test/nested-testharness.js new file mode 100644 index 0000000000..d97c1568c7 --- /dev/null +++ b/testing/web-platform/tests/resources/test/nested-testharness.js @@ -0,0 +1,80 @@ +'use strict'; + +/** + * Execute testharness.js and one or more scripts in an iframe. Report the + * results of the execution. + * + * @param {...function|...string} bodies - a function body. If specified as a + * function object, it will be + * serialized to a string using the + * built-in + * `Function.prototype.toString` prior + * to inclusion in the generated + * iframe. + * + * @returns {Promise} eventual value describing the result of the test + * execution; the summary object has two properties: + * `harness` (a string describing the harness status) and + * `tests` (an object whose "own" property names are the + * titles of the defined sub-tests and whose associated + * values are the subtest statuses). + */ +function makeTest(...bodies) { + const closeScript = '<' + '/script>'; + let src = ` +<!DOCTYPE HTML> +<html> +<head> +<title>Document title</title> +<script src="/resources/testharness.js?${Math.random()}">${closeScript} +</head> + +<body> +<div id="log"></div>`; + bodies.forEach((body) => { + src += '<script>(' + body + ')();' + closeScript; + }); + + const iframe = document.createElement('iframe'); + + document.body.appendChild(iframe); + iframe.contentDocument.write(src); + + return new Promise((resolve) => { + window.addEventListener('message', function onMessage(e) { + if (e.source !== iframe.contentWindow) { + return; + } + if (!e.data || e.data.type !=='complete') { + return; + } + window.removeEventListener('message', onMessage); + resolve(e.data); + }); + + iframe.contentDocument.close(); + }).then(({ tests, status }) => { + const summary = { + harness: getEnumProp(status, status.status), + tests: {} + }; + + tests.forEach((test) => { + summary.tests[test.name] = getEnumProp(test, test.status); + }); + + return summary; + }); +} + +function getEnumProp(object, value) { + for (let property in object) { + if (!/^[A-Z]+$/.test(property)) { + continue; + } + + if (object[property] === value) { + return property; + } + } +} |