2 * THIS FILE INTENTIONALLY LEFT BLANK
4 * More specifically, this file is intended for vendors to implement
5 * code needed to integrate testharness.js tests with their own test systems.
7 * Typically such integration will attach callbacks when each test is
8 * has run, using add_result_callback(callback(test)), or when the whole test file has
9 * completed, using add_completion_callback(callback(tests, harness_status)).
11 * For more documentation about the callback functions and the
12 * parameters they are called with see testharness.js
17 // Setup for WebKit JavaScript tests
18 if (self.testRunner) {
19 testRunner.dumpAsText();
20 testRunner.waitUntilDone();
21 testRunner.setCanOpenWindows();
22 testRunner.setCloseRemainingWindowsWhenComplete(true);
25 // Disable the default output of testharness.js. The default output formats
26 // test results into an HTML table. When that table is dumped as text, no
27 // spacing between cells is preserved, and it is therefore not readable. By
28 // setting output to false, the HTML table will not be created.
29 setup({"output":false});
31 // Function used to convert the test status code into the corresponding
33 function convertResult(resultStatus) {
34 switch (resultStatus) {
46 // Sanitizes the given text for display in test results.
47 function sanitize(text) {
51 // Escape null characters, otherwise diff will think the file is binary.
52 text = text.replace(/\0/g, "\\0");
53 // Escape carriage returns as they break rietveld's difftools.
54 return text.replace(/\r/g, "\\r");
57 // If the test has a meta tag named flags and the content contains "dom",
58 // then it's a CSSWG test.
59 function isCSSWGTest() {
60 var flags = document.querySelector('meta[name=flags]'),
61 content = flags ? flags.getAttribute('content') : null;
62 return content && content.match(/\bdom\b/);
66 return !!document.querySelector('script[src*="/resources/testharness"]');
69 var didDispatchLoadEvent = false;
70 var handleLoad = function() {
71 didDispatchLoadEvent = true;
72 window.removeEventListener('load', handleLoad);
74 window.addEventListener('load', handleLoad, false);
76 // Using a callback function, test results will be added to the page in a
77 // manner that allows dumpAsText to produce readable test results.
78 add_completion_callback(function (tests, harness_status) {
80 // Create element to hold results.
81 var results = document.createElement("pre");
83 // Declare result string.
84 var resultStr = "This is a testharness.js-based test.\n";
86 // Check harness_status. If it is not 0, tests did not execute
87 // correctly, output the error code and message.
88 if (harness_status.status != 0) {
89 resultStr += "Harness Error. harness_status.status = " +
90 harness_status.status +
91 " , harness_status.message = " +
92 harness_status.message +
95 // Iterate through tests array and build string that contains
96 // results for all tests.
97 for (var i = 0; i < tests.length; ++i) {
98 resultStr += convertResult(tests[i].status) + " " +
99 sanitize(tests[i].name) + " " +
100 sanitize(tests[i].message) + "\n";
103 resultStr += "Harness: the test ran to completion.\n";
105 // Set results element's textContent to the results string.
106 results.textContent = resultStr;
109 if (self.testRunner) {
110 var logDiv = document.getElementById('log');
111 if ((isCSSWGTest() || isJSTest()) && logDiv) {
112 // Assume it's a CSSWG style test, and anything other than
113 // the log div isn't material to the testrunner output, so
114 // should be hidden from the text dump.
115 document.body.textContent = '';
119 // Add results element to document.
121 document.documentElement.appendChild(document.createElement("body"));
122 document.body.appendChild(results);
125 testRunner.notifyDone();
128 if (didDispatchLoadEvent || document.readyState != 'loading') {
129 // This function might not be the last 'completion callback', and
130 // another completion callback might generate more results. So, we
131 // don't dump the results immediately.
134 // Parsing the test HTML isn't finished yet.
135 window.addEventListener('load', done);