Commit b193fb7d authored by Quinten Yearsley's avatar Quinten Yearsley Committed by Commit Bot

Clean up style in testharnessreport.js

This CL changes testharnessreport.js to adhere closer to
the style guide:
https://chromium.googlesource.com/chromium/src/+/master/styleguide/web/web.md#JavaScript

In this CL:
 - Wrap long lines
 - Make quotes consistent
 - Replaces var with let/const
 - Update some comments

This should not change behavior.

Change-Id: I1df1b42fd74333bba392eb8f3a91a15b4eb4d4fd
Reviewed-on: https://chromium-review.googlesource.com/600769
Commit-Queue: Quinten Yearsley <qyearsley@chromium.org>
Reviewed-by: default avatarKent Tamura <tkent@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491878}
parent 3ae357a3
/* /*
* THIS FILE INTENTIONALLY LEFT BLANK * This file is intended for vendors to implement code needed to integrate
* * testharness.js tests with their own test systems.
* More specifically, this file is intended for vendors to implement
* code needed to integrate testharness.js tests with their own test systems.
* *
* Typically such integration will attach callbacks when each test is * Typically such integration will attach callbacks when each test is
* has run, using add_result_callback(callback(test)), or when the whole test file has * has run, using add_result_callback(callback(test)), or when the whole test
* completed, using add_completion_callback(callback(tests, harness_status)). * file has completed, using add_completion_callback(callback(tests,
* harness_status)).
* *
* For more documentation about the callback functions and the * For more documentation about the callback functions and the
* parameters they are called with see testharness.js * parameters they are called with, see testharness.js.
*/ */
(function() { (function() {
var output_document = document; let output_document = document;
// Setup for WebKit JavaScript tests // Setup for Blink JavaScript tests. self.testRunner is expected to be
// present when tests are run
if (self.testRunner) { if (self.testRunner) {
testRunner.dumpAsText(); testRunner.dumpAsText();
testRunner.waitUntilDone(); testRunner.waitUntilDone();
...@@ -24,11 +24,11 @@ ...@@ -24,11 +24,11 @@
testRunner.setCloseRemainingWindowsWhenComplete(true); testRunner.setCloseRemainingWindowsWhenComplete(true);
testRunner.setDumpJavaScriptDialogs(false); testRunner.setDumpJavaScriptDialogs(false);
// fetch-event-referrer-policy.https.html intentiontially loads mixed // fetch-event-referrer-policy.https.html intentionally loads mixed
// content in order to test the referrer policy, so // content in order to test the referrer policy, so
// WebKitAllowRunningInsecureContent must be set to true or else the // WebKitAllowRunningInsecureContent must be set to true or else the
// load would be blocked. // load would be blocked.
if (document.URL.indexOf("service-workers/service-worker/fetch-event-referrer-policy.https.html") >= 0) { if (document.URL.indexOf('service-workers/service-worker/fetch-event-referrer-policy.https.html') >= 0) {
testRunner.overridePreference('WebKitAllowRunningInsecureContent', true); testRunner.overridePreference('WebKitAllowRunningInsecureContent', true);
} }
} }
...@@ -39,158 +39,168 @@ ...@@ -39,158 +39,168 @@
// setting output to false, the HTML table will not be created. // setting output to false, the HTML table will not be created.
// Also, disable timeout (except for explicit timeout), since the Blink // Also, disable timeout (except for explicit timeout), since the Blink
// layout test runner has its own timeout mechanism. // layout test runner has its own timeout mechanism.
// See: https://github.com/w3c/testharness.js/blob/master/docs/api.md#setup // See: http://web-platform-tests.org/writing-tests/testharness-api.html
setup({ setup({'output': false, 'explicit_timeout': true});
"output": false,
"explicit_timeout": true
});
// Function used to convert the test status code into the corresponding /** Converts the testharness test status into the corresponding string. */
// string
function convertResult(resultStatus) { function convertResult(resultStatus) {
switch (resultStatus) { switch (resultStatus) {
case 0: case 0:
return "PASS"; return 'PASS';
case 1: case 1:
return "FAIL"; return 'FAIL';
case 2: case 2:
return "TIMEOUT"; return 'TIMEOUT';
default: default:
return "NOTRUN"; return 'NOTRUN';
} }
} }
var localPathRegExp; let localPathRegExp;
if (document.URL.startsWith("file:///")) { if (document.URL.startsWith('file:///')) {
var index = document.URL.indexOf("/external/wpt"); const index = document.URL.indexOf('/external/wpt');
if (index >= 0) { if (index >= 0) {
var localPath = document.URL.substring("file:///".length, index + "/external/wpt".length); const localPath = document.URL.substring(
localPathRegExp = new RegExp(localPath.replace(/(\W)/g, "\\$1"), "g"); 'file:///'.length, index + '/external/wpt'.length);
localPathRegExp = new RegExp(localPath.replace(/(\W)/g, '\\$1'), 'g');
} }
} }
// Sanitizes the given text for display in test results. /** Sanitizes the given text for display in test results. */
function sanitize(text) { function sanitize(text) {
if (!text) { if (!text) {
return ""; return '';
} }
// Escape null characters, otherwise diff will think the file is binary. // Escape null characters, otherwise diff will think the file is binary.
text = text.replace(/\0/g, "\\0"); text = text.replace(/\0/g, '\\0');
// Escape carriage returns as they break rietveld's difftools. // Escape carriage returns as they break rietveld's difftools.
text = text.replace(/\r/g, "\\r"); text = text.replace(/\r/g, '\\r');
// Replace machine-dependent path with "...". // Replace machine-dependent path with "...".
if (localPathRegExp) if (localPathRegExp)
text = text.replace(localPathRegExp, "..."); text = text.replace(localPathRegExp, '...');
return text; return text;
} }
/** Checks whether the current path is a manual test in WPT. */
function isWPTManualTest() { function isWPTManualTest() {
var path = location.pathname; // Here we assume that if wptserve is running, then the hostname
if (location.hostname == 'web-platform.test' // is web-platform.test.
&& /.*-manual(\.https)?\.html$/.test(path)) { const path = location.pathname;
if (location.hostname == 'web-platform.test' &&
/.*-manual(\.https)?\.html$/.test(path)) {
return true; return true;
} }
// If the file is loaded locally via file://, it must include
// the wpt directory in the path.
return /\/external\/wpt\/.*-manual(\.https)?\.html$/.test(path); return /\/external\/wpt\/.*-manual(\.https)?\.html$/.test(path);
} }
// Returns a directory part relative to WPT root and a basename part of the /**
// current test. e.g. * Returns a directory part relative to WPT root and a basename part of the
// Current test: file:///.../LayoutTests/external/wpt/pointerevents/foobar.html * current test. e.g.
// Output: "/pointerevents/foobar" * Current test: file:///.../LayoutTests/external/wpt/pointerevents/foobar.html
* Output: "/pointerevents/foobar"
*/
function pathAndBaseNameInWPT() { function pathAndBaseNameInWPT() {
var path = location.pathname; const path = location.pathname;
let matches;
if (location.hostname == 'web-platform.test') { if (location.hostname == 'web-platform.test') {
var matches = path.match(/^(\/.*)\.html$/); matches = path.match(/^(\/.*)\.html$/);
return matches ? matches[1] : null; return matches ? matches[1] : null;
} }
var matches = path.match(/external\/wpt(\/.*)\.html$/); matches = path.match(/external\/wpt(\/.*)\.html$/);
return matches ? matches[1] : null; return matches ? matches[1] : null;
} }
/** Loads the WPT automation script for the current test, if applicable. */
function loadAutomationScript() { function loadAutomationScript() {
var pathAndBase = pathAndBaseNameInWPT(); const pathAndBase = pathAndBaseNameInWPT();
if (!pathAndBase) if (!pathAndBase) {
return; return;
var automationPath = location.pathname.replace(/\/external\/wpt\/.*$/, '/external/wpt_automation'); }
if (location.hostname == 'web-platform.test') let automationPath = location.pathname.replace(
/\/external\/wpt\/.*$/, '/external/wpt_automation');
if (location.hostname == 'web-platform.test') {
automationPath = '/wpt_automation'; automationPath = '/wpt_automation';
}
// Export importAutomationScript for use by the automation scripts. // Export importAutomationScript for use by the automation scripts.
window.importAutomationScript = function(relativePath) { window.importAutomationScript = function(relativePath) {
var script = document.createElement('script'); const script = document.createElement('script');
script.src = automationPath + relativePath; script.src = automationPath + relativePath;
document.head.appendChild(script); document.head.appendChild(script);
} };
var src; let src;
if (pathAndBase.startsWith('/fullscreen/') if (pathAndBase.startsWith('/fullscreen/') ||
|| pathAndBase.startsWith('/webusb/')) { pathAndBase.startsWith('/webusb/')) {
// Fullscreen tests all use the same automation script and WebUSB // Fullscreen tests all use the same automation script and WebUSB
// tests borrow it. // tests borrow it.
src = automationPath + '/fullscreen/auto-click.js'; src = automationPath + '/fullscreen/auto-click.js';
} else if (pathAndBase.startsWith('/pointerevents/') } else if (
|| pathAndBase.startsWith('/uievents/') pathAndBase.startsWith('/pointerevents/') ||
|| pathAndBase.startsWith('/pointerlock/') pathAndBase.startsWith('/uievents/') ||
|| pathAndBase.startsWith('/html/') pathAndBase.startsWith('/pointerlock/') ||
|| pathAndBase.startsWith('/input-events/')) { pathAndBase.startsWith('/html/') ||
pathAndBase.startsWith('/input-events/')) {
// Per-test automation scripts. // Per-test automation scripts.
src = automationPath + pathAndBase + '-automation.js'; src = automationPath + pathAndBase + '-automation.js';
} else { } else {
return; return;
} }
var script = document.createElement('script'); const script = document.createElement('script');
script.src = src; script.src = src;
document.head.appendChild(script); document.head.appendChild(script);
} }
var didDispatchLoadEvent = false; // Listen for the load event, and load an automation script if necessary.
let didDispatchLoadEvent = false;
window.addEventListener('load', function() { window.addEventListener('load', function() {
didDispatchLoadEvent = true; didDispatchLoadEvent = true;
if (isWPTManualTest()) { if (isWPTManualTest()) {
setTimeout(loadAutomationScript, 0); setTimeout(loadAutomationScript, 0);
} }
}, { once: true }); }, {once: true});
add_start_callback(function(properties) { add_start_callback(function(properties) {
if (properties.output_document) if (properties.output_document)
output_document = properties.output_document; output_document = properties.output_document;
}); });
// Using a callback function, test results will be added to the page in a // Using a callback function, test results will be added to the page in a
// manner that allows dumpAsText to produce readable test results. // manner that allows dumpAsText to produce readable test results.
add_completion_callback(function (tests, harness_status) { add_completion_callback(function(tests, harness_status) {
// Create element to hold results. // Create element to hold results.
var results = output_document.createElement("pre"); const resultsElement = output_document.createElement('pre');
// Declare result string. // Declare result string.
var resultStr = "This is a testharness.js-based test.\n"; let resultStr = 'This is a testharness.js-based test.\n';
// Check harness_status. If it is not 0, tests did not execute // Check harness_status. If it is not 0, tests did not execute
// correctly, output the error code and message. // correctly, output the error code and message.
if (harness_status.status != 0) { if (harness_status.status != 0) {
resultStr += "Harness Error. harness_status.status = " + resultStr +=
harness_status.status + 'Harness Error. harness_status.status = ' + harness_status.status +
" , harness_status.message = " + ' , harness_status.message = ' + harness_status.message + '\n';
harness_status.message +
"\n";
} }
// Iterate through tests array and build string that contains // Iterate through the `tests` array and build a string that contains
// results for all tests. // results for all the subtests in this test.
let testResults = ""; let testResults = '';
let resultCounter = [0, 0, 0, 0]; let resultCounter = [0, 0, 0, 0];
// reflection tests contain huge number of tests, and Chromium code // The reflection tests contain huge number of tests, and Chromium code
// review tool has the 1MB diff size limit. We merge PASS lines. // review tool has the 1MB diff size limit. We merge PASS lines.
if (output_document.URL.indexOf("/html/dom/reflection") >= 0) { if (output_document.URL.indexOf('/html/dom/reflection') >= 0) {
for (var i = 0; i < tests.length; ++i) { for (let i = 0; i < tests.length; ++i) {
if (tests[i].status == 0) { if (tests[i].status == 0) {
var colon = tests[i].name.indexOf(':'); const colon = tests[i].name.indexOf(':');
if (colon > 0) { if (colon > 0) {
var prefix = tests[i].name.substring(0, colon + 1); const prefix = tests[i].name.substring(0, colon + 1);
var j = i + 1; let j = i + 1;
for (; j < tests.length; ++j) { for (; j < tests.length; ++j) {
if (!tests[j].name.startsWith(prefix) || tests[j].status != 0) if (!tests[j].name.startsWith(prefix) ||
tests[j].status != 0)
break; break;
} }
const numPasses = j - i; const numPasses = j - i;
...@@ -204,20 +214,21 @@ ...@@ -204,20 +214,21 @@
} }
} }
resultCounter[tests[i].status]++; resultCounter[tests[i].status]++;
testResults += convertResult(tests[i].status) + " " + testResults += convertResult(tests[i].status) + ' ' +
sanitize(tests[i].name) + " " + sanitize(tests[i].name) + ' ' + sanitize(tests[i].message) +
sanitize(tests[i].message) + "\n"; '\n';
} }
} else { } else {
for (var i = 0; i < tests.length; ++i) { for (let i = 0; i < tests.length; ++i) {
resultCounter[tests[i].status]++; resultCounter[tests[i].status]++;
testResults += convertResult(tests[i].status) + " " + testResults += convertResult(tests[i].status) + ' ' +
sanitize(tests[i].name) + " " + sanitize(tests[i].name) + ' ' + sanitize(tests[i].message) +
sanitize(tests[i].message) + "\n"; '\n';
} }
} }
if (output_document.URL.indexOf("http://web-platform.test") >= 0 && if (output_document.URL.indexOf('http://web-platform.test') >= 0 &&
tests.length >= 50 && (resultCounter[1] || resultCounter[2] || resultCounter[3])) { tests.length >= 50 &&
(resultCounter[1] || resultCounter[2] || resultCounter[3])) {
// Output failure metrics if there are many. // Output failure metrics if there are many.
resultStr += `Found ${tests.length} tests;` + resultStr += `Found ${tests.length} tests;` +
` ${resultCounter[0]} PASS,` + ` ${resultCounter[0]} PASS,` +
...@@ -227,16 +238,17 @@ ...@@ -227,16 +238,17 @@
} }
resultStr += testResults; resultStr += testResults;
resultStr += "Harness: the test ran to completion.\n"; resultStr += 'Harness: the test ran to completion.\n';
// Set results element's textContent to the results string. resultsElement.textContent = resultStr;
results.textContent = resultStr;
function done() { function done() {
let xhtmlNS = 'http://www.w3.org/1999/xhtml'; const xhtmlNS = 'http://www.w3.org/1999/xhtml';
var body = null; let body = null;
if (output_document.body && output_document.body.tagName == 'BODY' && output_document.body.namespaceURI == xhtmlNS) if (output_document.body && output_document.body.tagName == 'BODY' &&
output_document.body.namespaceURI == xhtmlNS) {
body = output_document.body; body = output_document.body;
}
// A temporary workaround since |window.self| property lookup starts // A temporary workaround since |window.self| property lookup starts
// failing if the frame is detached. |output_document| may be an // failing if the frame is detached. |output_document| may be an
// ancestor of |self| so clearing |textContent| may detach |self|. // ancestor of |self| so clearing |textContent| may detach |self|.
...@@ -244,7 +256,7 @@ ...@@ -244,7 +256,7 @@
// value. // value.
// TODO(dcheng): Remove this hack after fixing window/self/frames // TODO(dcheng): Remove this hack after fixing window/self/frames
// lookup in https://crbug.com/618672 // lookup in https://crbug.com/618672
var cachedSelf = window.self; const cachedSelf = window.self;
if (cachedSelf.testRunner) { if (cachedSelf.testRunner) {
// The following DOM operations may show console messages. We // The following DOM operations may show console messages. We
// suppress them because they are not related to the running // suppress them because they are not related to the running
...@@ -253,8 +265,9 @@ ...@@ -253,8 +265,9 @@
// Anything isn't material to the testrunner output, so should // Anything isn't material to the testrunner output, so should
// be hidden from the text dump. // be hidden from the text dump.
if (body) if (body) {
body.textContent = ''; body.textContent = '';
}
} }
// Add results element to output_document. // Add results element to output_document.
...@@ -268,16 +281,17 @@ ...@@ -268,16 +281,17 @@
body.setAttribute('style', 'white-space:pre;'); body.setAttribute('style', 'white-space:pre;');
html.appendChild(body); html.appendChild(body);
} }
output_document.body.appendChild(results); output_document.body.appendChild(resultsElement);
if (cachedSelf.testRunner) if (cachedSelf.testRunner) {
testRunner.notifyDone(); testRunner.notifyDone();
}
} }
if (didDispatchLoadEvent || output_document.readyState != 'loading') { if (didDispatchLoadEvent || output_document.readyState != 'loading') {
// This function might not be the last 'completion callback', and // This function might not be the last "completion callback", and
// another completion callback might generate more results. So, we // another completion callback might generate more results.
// don't dump the results immediately. // So, we don't dump the results immediately.
setTimeout(done, 0); setTimeout(done, 0);
} else { } else {
// Parsing the test HTML isn't finished yet. // Parsing the test HTML isn't finished yet.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment