| OLD | NEW |
| 1 /* | 1 /* |
| 2 * THIS FILE INTENTIONALLY LEFT BLANK | 2 * THIS FILE INTENTIONALLY LEFT BLANK |
| 3 * | 3 * |
| 4 * More specifically, this file is intended for vendors to implement | 4 * More specifically, this file is intended for vendors to implement |
| 5 * code needed to integrate testharness.js tests with their own test systems. | 5 * code needed to integrate testharness.js tests with their own test systems. |
| 6 * | 6 * |
| 7 * Typically such integration will attach callbacks when each test is | 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 fi
le has | 8 * has run, using add_result_callback(callback(test)), or when the whole test fi
le has |
| 9 * completed, using add_completion_callback(callback(tests, harness_status)). | 9 * completed, using add_completion_callback(callback(tests, harness_status)). |
| 10 * | 10 * |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 return("NOTRUN"); | 31 return("NOTRUN"); |
| 32 } | 32 } |
| 33 | 33 |
| 34 /* Disable the default output of testharness.js. The default output formats | 34 /* Disable the default output of testharness.js. The default output formats |
| 35 * test results into an HTML table. When that table is dumped as text, no | 35 * test results into an HTML table. When that table is dumped as text, no |
| 36 * spacing between cells is preserved, and it is therefore not readable. By | 36 * spacing between cells is preserved, and it is therefore not readable. By |
| 37 * setting output to false, the HTML table will not be created | 37 * setting output to false, the HTML table will not be created |
| 38 */ | 38 */ |
| 39 setup({"output":false}); | 39 setup({"output":false}); |
| 40 | 40 |
| 41 /* If the test has a meta tag named flags and the content contains "dom", then i
t's a CSSWG test. |
| 42 */ |
| 43 function isCSSWGTest() { |
| 44 var flags = document.querySelector('meta[name=flags]'), |
| 45 content = flags ? flags.getAttribute('content') : null; |
| 46 |
| 47 return content && content.match(/\bdom\b/); |
| 48 } |
| 49 |
| 50 function isJSTest() { |
| 51 return !!document.querySelector('script[src*="/resources/testharness"]'); |
| 52 } |
| 53 |
| 41 /* Using a callback function, test results will be added to the page in a | 54 /* Using a callback function, test results will be added to the page in a |
| 42 * manner that allows dumpAsText to produce readable test results | 55 * manner that allows dumpAsText to produce readable test results |
| 43 */ | 56 */ |
| 44 add_completion_callback(function (tests, harness_status) { | 57 add_completion_callback(function (tests, harness_status) { |
| 45 | 58 |
| 46 // Create element to hold results | 59 // Create element to hold results |
| 47 var results = document.createElement("pre"); | 60 var results = document.createElement("pre"); |
| 48 | 61 |
| 49 // Declare result string | 62 // Declare result string |
| 50 var resultStr = "\n"; | 63 var resultStr = "This is a testharness.js-based test.\n"; |
| 51 | 64 |
| 52 // Sanitizes the given text for display in test results. | 65 // Sanitizes the given text for display in test results. |
| 53 function sanitize(text) { | 66 function sanitize(text) { |
| 54 if (!text) { | 67 if (!text) { |
| 55 return ""; | 68 return ""; |
| 56 } | 69 } |
| 57 // Escape null characters, otherwise diff will think the file is binary. | 70 // Escape null characters, otherwise diff will think the file is binary. |
| 58 text = text.replace(/\0/g, "\\0"); | 71 text = text.replace(/\0/g, "\\0"); |
| 59 // Escape carriage returns as they break rietveld's difftools. | 72 // Escape carriage returns as they break rietveld's difftools. |
| 60 return text.replace(/\r/g, "\\r"); | 73 return text.replace(/\r/g, "\\r"); |
| 61 } | 74 } |
| 62 | 75 |
| 63 // Check harness_status. If it is not 0, tests did not | 76 // Check harness_status. If it is not 0, tests did not |
| 64 // execute correctly, output the error code and message | 77 // execute correctly, output the error code and message |
| 65 if (harness_status.status != 0) { | 78 if (harness_status.status != 0) { |
| 66 resultStr += "Harness Error. harness_status.status = " + | 79 resultStr += "Harness Error. harness_status.status = " + |
| 67 harness_status.status + | 80 harness_status.status + |
| 68 " , harness_status.message = " + | 81 " , harness_status.message = " + |
| 69 harness_status.message + | 82 harness_status.message + |
| 70 "\n"; | 83 "\n"; |
| 71 } else { | 84 } else { |
| 72 // Iterate through tests array and build string that contains | 85 // Iterate through tests array and build string that contains |
| 73 // results for all tests | 86 // results for all tests |
| 74 for (var i = 0; i < tests.length; i++) { | 87 for (var i = 0; i < tests.length; i++) { |
| 75 resultStr += convertResult(tests[i].status) + " " + | 88 resultStr += convertResult(tests[i].status) + " " + |
| 76 sanitize(tests[i].name) + " " + | 89 sanitize(tests[i].name) + " " + |
| 77 sanitize(tests[i].message) + "\n"; | 90 sanitize(tests[i].message) + "\n"; |
| 78 } | 91 } |
| 79 } | 92 } |
| 93 resultStr += "Harness: the test ran to completion.\n"; |
| 80 | 94 |
| 81 // Set results element's innerHTML to the results string | 95 // Set results element's textContent to the results string |
| 82 results.innerHTML = resultStr; | 96 results.textContent = resultStr; |
| 83 | 97 |
| 84 // Add results element to document | 98 function done() { |
| 85 document.body.appendChild(results); | 99 if (self.testRunner) { |
| 100 var logDiv = document.getElementById('log'); |
| 101 if ((isCSSWGTest() || isJSTest()) && logDiv) { |
| 102 // Assume it's a CSSWG style test, and anything other than the l
og div isn't |
| 103 // material to the testrunner output, so should be hidden from t
he text dump |
| 104 for (var i = 0; i < document.body.children.length; i++) { |
| 105 if (document.body.children[i] === logDiv) continue; |
| 86 | 106 |
| 87 if (self.testRunner) | 107 document.body.children[i].style.visibility = "hidden"; |
| 88 testRunner.notifyDone(); | 108 } |
| 109 } |
| 110 } |
| 111 |
| 112 // Add results element to document |
| 113 document.body.appendChild(results); |
| 114 |
| 115 if (self.testRunner) |
| 116 testRunner.notifyDone(); |
| 117 } |
| 118 |
| 119 if (!document.body || document.readyState === 'loading') { |
| 120 window.addEventListener('load', done); |
| 121 } else { |
| 122 done(); |
| 123 } |
| 89 }); | 124 }); |
| OLD | NEW |