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
*
* More specifically, this file is intended for vendors to implement
* code needed to integrate testharness.js tests with their own test systems.
* 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
* has run, using add_result_callback(callback(test)), or when the whole test file has
* completed, using add_completion_callback(callback(tests, harness_status)).
* has run, using add_result_callback(callback(test)), or when the whole test
* file has completed, using add_completion_callback(callback(tests,
* harness_status)).
*
* 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() {
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) {
testRunner.dumpAsText();
testRunner.waitUntilDone();
......@@ -24,11 +24,11 @@
testRunner.setCloseRemainingWindowsWhenComplete(true);
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
// WebKitAllowRunningInsecureContent must be set to true or else the
// 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);
}
}
......@@ -39,158 +39,168 @@
// setting output to false, the HTML table will not be created.
// Also, disable timeout (except for explicit timeout), since the Blink
// layout test runner has its own timeout mechanism.
// See: https://github.com/w3c/testharness.js/blob/master/docs/api.md#setup
setup({
"output": false,
"explicit_timeout": true
});
// See: http://web-platform-tests.org/writing-tests/testharness-api.html
setup({'output': false, 'explicit_timeout': true});
// Function used to convert the test status code into the corresponding
// string
/** Converts the testharness test status into the corresponding string. */
function convertResult(resultStatus) {
switch (resultStatus) {
case 0:
return "PASS";
case 1:
return "FAIL";
case 2:
return "TIMEOUT";
default:
return "NOTRUN";
case 0:
return 'PASS';
case 1:
return 'FAIL';
case 2:
return 'TIMEOUT';
default:
return 'NOTRUN';
}
}
var localPathRegExp;
if (document.URL.startsWith("file:///")) {
var index = document.URL.indexOf("/external/wpt");
let localPathRegExp;
if (document.URL.startsWith('file:///')) {
const index = document.URL.indexOf('/external/wpt');
if (index >= 0) {
var localPath = document.URL.substring("file:///".length, index + "/external/wpt".length);
localPathRegExp = new RegExp(localPath.replace(/(\W)/g, "\\$1"), "g");
const localPath = document.URL.substring(
'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) {
if (!text) {
return "";
return '';
}
// 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.
text = text.replace(/\r/g, "\\r");
text = text.replace(/\r/g, '\\r');
// Replace machine-dependent path with "...".
if (localPathRegExp)
text = text.replace(localPathRegExp, "...");
text = text.replace(localPathRegExp, '...');
return text;
}
/** Checks whether the current path is a manual test in WPT. */
function isWPTManualTest() {
var path = location.pathname;
if (location.hostname == 'web-platform.test'
&& /.*-manual(\.https)?\.html$/.test(path)) {
// Here we assume that if wptserve is running, then the hostname
// is web-platform.test.
const path = location.pathname;
if (location.hostname == 'web-platform.test' &&
/.*-manual(\.https)?\.html$/.test(path)) {
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);
}
// Returns a directory part relative to WPT root and a basename part of the
// current test. e.g.
// Current test: file:///.../LayoutTests/external/wpt/pointerevents/foobar.html
// Output: "/pointerevents/foobar"
/**
* Returns a directory part relative to WPT root and a basename part of the
* current test. e.g.
* Current test: file:///.../LayoutTests/external/wpt/pointerevents/foobar.html
* Output: "/pointerevents/foobar"
*/
function pathAndBaseNameInWPT() {
var path = location.pathname;
const path = location.pathname;
let matches;
if (location.hostname == 'web-platform.test') {
var matches = path.match(/^(\/.*)\.html$/);
matches = path.match(/^(\/.*)\.html$/);
return matches ? matches[1] : null;
}
var matches = path.match(/external\/wpt(\/.*)\.html$/);
matches = path.match(/external\/wpt(\/.*)\.html$/);
return matches ? matches[1] : null;
}
/** Loads the WPT automation script for the current test, if applicable. */
function loadAutomationScript() {
var pathAndBase = pathAndBaseNameInWPT();
if (!pathAndBase)
const pathAndBase = pathAndBaseNameInWPT();
if (!pathAndBase) {
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';
}
// Export importAutomationScript for use by the automation scripts.
window.importAutomationScript = function(relativePath) {
var script = document.createElement('script');
const script = document.createElement('script');
script.src = automationPath + relativePath;
document.head.appendChild(script);
}
};
var src;
if (pathAndBase.startsWith('/fullscreen/')
|| pathAndBase.startsWith('/webusb/')) {
let src;
if (pathAndBase.startsWith('/fullscreen/') ||
pathAndBase.startsWith('/webusb/')) {
// Fullscreen tests all use the same automation script and WebUSB
// tests borrow it.
src = automationPath + '/fullscreen/auto-click.js';
} else if (pathAndBase.startsWith('/pointerevents/')
|| pathAndBase.startsWith('/uievents/')
|| pathAndBase.startsWith('/pointerlock/')
|| pathAndBase.startsWith('/html/')
|| pathAndBase.startsWith('/input-events/')) {
} else if (
pathAndBase.startsWith('/pointerevents/') ||
pathAndBase.startsWith('/uievents/') ||
pathAndBase.startsWith('/pointerlock/') ||
pathAndBase.startsWith('/html/') ||
pathAndBase.startsWith('/input-events/')) {
// Per-test automation scripts.
src = automationPath + pathAndBase + '-automation.js';
} else {
return;
}
var script = document.createElement('script');
const script = document.createElement('script');
script.src = src;
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() {
didDispatchLoadEvent = true;
if (isWPTManualTest()) {
setTimeout(loadAutomationScript, 0);
}
}, { once: true });
}, {once: true});
add_start_callback(function(properties) {
if (properties.output_document)
output_document = properties.output_document;
if (properties.output_document)
output_document = properties.output_document;
});
// Using a callback function, test results will be added to the page in a
// 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.
var results = output_document.createElement("pre");
const resultsElement = output_document.createElement('pre');
// 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
// correctly, output the error code and message.
if (harness_status.status != 0) {
resultStr += "Harness Error. harness_status.status = " +
harness_status.status +
" , harness_status.message = " +
harness_status.message +
"\n";
resultStr +=
'Harness Error. harness_status.status = ' + harness_status.status +
' , harness_status.message = ' + harness_status.message + '\n';
}
// Iterate through tests array and build string that contains
// results for all tests.
let testResults = "";
// Iterate through the `tests` array and build a string that contains
// results for all the subtests in this test.
let testResults = '';
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.
if (output_document.URL.indexOf("/html/dom/reflection") >= 0) {
for (var i = 0; i < tests.length; ++i) {
if (output_document.URL.indexOf('/html/dom/reflection') >= 0) {
for (let i = 0; i < tests.length; ++i) {
if (tests[i].status == 0) {
var colon = tests[i].name.indexOf(':');
const colon = tests[i].name.indexOf(':');
if (colon > 0) {
var prefix = tests[i].name.substring(0, colon + 1);
var j = i + 1;
const prefix = tests[i].name.substring(0, colon + 1);
let j = i + 1;
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;
}
const numPasses = j - i;
......@@ -204,20 +214,21 @@
}
}
resultCounter[tests[i].status]++;
testResults += convertResult(tests[i].status) + " " +
sanitize(tests[i].name) + " " +
sanitize(tests[i].message) + "\n";
testResults += convertResult(tests[i].status) + ' ' +
sanitize(tests[i].name) + ' ' + sanitize(tests[i].message) +
'\n';
}
} else {
for (var i = 0; i < tests.length; ++i) {
for (let i = 0; i < tests.length; ++i) {
resultCounter[tests[i].status]++;
testResults += convertResult(tests[i].status) + " " +
sanitize(tests[i].name) + " " +
sanitize(tests[i].message) + "\n";
testResults += convertResult(tests[i].status) + ' ' +
sanitize(tests[i].name) + ' ' + sanitize(tests[i].message) +
'\n';
}
}
if (output_document.URL.indexOf("http://web-platform.test") >= 0 &&
tests.length >= 50 && (resultCounter[1] || resultCounter[2] || resultCounter[3])) {
if (output_document.URL.indexOf('http://web-platform.test') >= 0 &&
tests.length >= 50 &&
(resultCounter[1] || resultCounter[2] || resultCounter[3])) {
// Output failure metrics if there are many.
resultStr += `Found ${tests.length} tests;` +
` ${resultCounter[0]} PASS,` +
......@@ -227,16 +238,17 @@
}
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.
results.textContent = resultStr;
resultsElement.textContent = resultStr;
function done() {
let xhtmlNS = 'http://www.w3.org/1999/xhtml';
var body = null;
if (output_document.body && output_document.body.tagName == 'BODY' && output_document.body.namespaceURI == xhtmlNS)
const xhtmlNS = 'http://www.w3.org/1999/xhtml';
let body = null;
if (output_document.body && output_document.body.tagName == 'BODY' &&
output_document.body.namespaceURI == xhtmlNS) {
body = output_document.body;
}
// A temporary workaround since |window.self| property lookup starts
// failing if the frame is detached. |output_document| may be an
// ancestor of |self| so clearing |textContent| may detach |self|.
......@@ -244,7 +256,7 @@
// value.
// TODO(dcheng): Remove this hack after fixing window/self/frames
// lookup in https://crbug.com/618672
var cachedSelf = window.self;
const cachedSelf = window.self;
if (cachedSelf.testRunner) {
// The following DOM operations may show console messages. We
// suppress them because they are not related to the running
......@@ -253,8 +265,9 @@
// Anything isn't material to the testrunner output, so should
// be hidden from the text dump.
if (body)
if (body) {
body.textContent = '';
}
}
// Add results element to output_document.
......@@ -268,16 +281,17 @@
body.setAttribute('style', 'white-space:pre;');
html.appendChild(body);
}
output_document.body.appendChild(results);
output_document.body.appendChild(resultsElement);
if (cachedSelf.testRunner)
if (cachedSelf.testRunner) {
testRunner.notifyDone();
}
}
if (didDispatchLoadEvent || output_document.readyState != 'loading') {
// This function might not be the last 'completion callback', and
// another completion callback might generate more results. So, we
// don't dump the results immediately.
// This function might not be the last "completion callback", and
// another completion callback might generate more results.
// So, we don't dump the results immediately.
setTimeout(done, 0);
} else {
// 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