Debian Codegraph @

testing/web-platform/tests/resources/test/nested-testharness.js

raw
1 'use strict';
2
3 /**
4 * Execute testharness.js and one or more scripts in an iframe. Report the
5 * results of the execution.
6 *
7 * @param {...function|...string} bodies - a function body. If specified as a
8 * function object, it will be
9 * serialized to a string using the
10 * built-in
11 * `Function.prototype.toString` prior
12 * to inclusion in the generated
13 * iframe.
14 *
15 * @returns {Promise} eventual value describing the result of the test
16 * execution; the summary object has two properties:
17 * `harness` (a string describing the harness status) and
18 * `tests` (an object whose "own" property names are the
19 * titles of the defined sub-tests and whose associated
20 * values are the subtest statuses).
21 */
22 function makeTest(...bodies) {
23 const closeScript = '<' + '/script>';
24 let src = `
25 <!DOCTYPE HTML>
26 <html>
27 <head>
28 <title>Document title</title>
29 <script src="/resources/testharness.js?${Math.random()}">${closeScript}
30 </head>
31
32 <body>
33 <div id="log"></div>`;
34 bodies.forEach((body) => {
35 src += '<script>(' + body + ')();' + closeScript;
36 });
37
38 const iframe = document.createElement('iframe');
39
40 document.body.appendChild(iframe);
41 iframe.contentDocument.write(src);
42
43 return new Promise((resolve) => {
44 window.addEventListener('message', function onMessage(e) {
45 if (e.source !== iframe.contentWindow) {
46 return;
47 }
48 if (!e.data || e.data.type !=='complete') {
49 return;
50 }
51 window.removeEventListener('message', onMessage);
52 resolve(e.data);
53 });
54
55 iframe.contentDocument.close();
56 }).then(({ tests, status }) => {
57 const summary = {
58 harness: getEnumProp(status, status.status),
59 tests: {}
60 };
61
62 tests.forEach((test) => {
63 summary.tests[test.name] = getEnumProp(test, test.status);
64 });
65
66 return summary;
67 });
68 }
69
70 function getEnumProp(object, value) {
71 for (let property in object) {
72 if (!/^[A-Z]+$/.test(property)) {
73 continue;
74 }
75
76 if (object[property] === value) {
77 return property;
78 }
79 }
80 }