Skip to content

Commit

Permalink
test: add hr-time Web platform tests
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed May 7, 2020
1 parent e454e9b commit 880aab0
Show file tree
Hide file tree
Showing 19 changed files with 538 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/fixtures/wpt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Last update:
- interfaces: https://github.com/web-platform-tests/wpt/tree/712c9f275e/interfaces
- html/webappapis/microtask-queuing: https://github.com/web-platform-tests/wpt/tree/0c3bed38df/html/webappapis/microtask-queuing
- html/webappapis/timers: https://github.com/web-platform-tests/wpt/tree/ddfe9c089b/html/webappapis/timers
- hr-time: https://github.com/web-platform-tests/wpt/tree/a5d1774ecf/hr-time

[Web Platform Tests]: https://github.com/web-platform-tests/wpt
[`git node wpt`]: https://github.com/nodejs/node-core-utils/blob/master/docs/git-node.md#git-node-wpt
4 changes: 4 additions & 0 deletions test/fixtures/wpt/hr-time/META.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spec: https://w3c.github.io/hr-time/
suggested_reviewers:
- plehegar
- igrigorik
28 changes: 28 additions & 0 deletions test/fixtures/wpt/hr-time/basic.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
test(function() {
assert_true((self.performance !== undefined), "self.performance exists");
assert_equals(typeof self.performance, "object", "self.performance is an object");
assert_equals((typeof self.performance.now), "function", "self.performance.now() is a function");
assert_equals(typeof self.performance.now(), "number", "self.performance.now() returns a number");
}, "self.performance.now() is a function that returns a number");

test(function() {
assert_true(self.performance.now() > 0);
}, "self.performance.now() returns a positive number");

test(function() {
var now1 = self.performance.now();
var now2 = self.performance.now();
assert_true((now2-now1) >= 0);
}, "self.performance.now() difference is not negative");

async_test(function() {
// Check whether the performance.now() method is close to Date() within 30ms (due to inaccuracies)
var initial_hrt = self.performance.now();
var initial_date = Date.now();
this.step_timeout(function() {
var final_hrt = self.performance.now();
var final_date = Date.now();
assert_approx_equals(final_hrt - initial_hrt, final_date - initial_date, 30, 'High resolution time value increased by approximately the same amount as time from date object');
this.done();
}, 2000);
}, 'High resolution time has approximately the right relative magnitude');
23 changes: 23 additions & 0 deletions test/fixtures/wpt/hr-time/idlharness.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// META: global=window,worker
// META: script=/resources/WebIDLParser.js
// META: script=/resources/idlharness.js
// META: timeout=long

'use strict';

// https://w3c.github.io/hr-time/

idl_test(
['hr-time'],
['html', 'dom'],
async idl_array => {
if (self.GLOBAL.isWorker()) {
idl_array.add_objects({ WorkerGlobalScope: ['self'] });
} else {
idl_array.add_objects({ Window: ['self'] });
}
idl_array.add_objects({
Performance: ['performance'],
});
}
);
13 changes: 13 additions & 0 deletions test/fixtures/wpt/hr-time/monotonic-clock.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// The time values returned when calling the now method MUST be monotonically increasing and not subject to system clock adjustments or system clock skew.
test(function() {
assert_true(self.performance.now() > 0, "self.performance.now() returns positive numbers");
}, "self.performance.now() returns a positive number");

// The difference between any two chronologically recorded time values returned from the now method MUST never be negative.
test(function() {
var now1 = self.performance.now();
var now2 = self.performance.now();
assert_true((now2-now1) >= 0, "self.performance.now() difference is not negative");
},
"self.performance.now() difference is not negative"
);
76 changes: 76 additions & 0 deletions test/fixtures/wpt/hr-time/performance-tojson.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<!doctype html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>

test(() => {
// Check Performance attributes.
assert_equals(typeof(performance.toJSON), 'function');
const json = performance.toJSON();
assert_equals(typeof(json), 'object');
assert_equals(json.timeOrigin, performance.timeOrigin,
'performance.toJSON().timeOrigin should match performance.timeOrigin');

// Check PerformanceTiming toJSON.
const jsonTiming = json.timing;
const timing = performance.timing;
assert_equals(typeof(timing.toJSON), 'function');
const timingJSON = timing.toJSON();
assert_equals(typeof(timingJSON), 'object');
// Check PerformanceTiming attributes, from both:
// 1) |jsonTiming| from Performance.
// 2) |timingJSON| from PerformanceTiming.
const performanceTimingKeys = [
'navigationStart',
'unloadEventStart',
'unloadEventEnd',
'redirectStart',
'redirectEnd',
'fetchStart',
'domainLookupStart',
'domainLookupEnd',
'connectStart',
'connectEnd',
'secureConnectionStart',
'requestStart',
'responseStart',
'responseEnd',
'domLoading',
'domInteractive',
'domContentLoadedEventStart',
'domContentLoadedEventEnd',
'domComplete',
'loadEventStart',
'loadEventEnd'
];
for (const key of performanceTimingKeys) {
assert_equals(jsonTiming[key], timing[key],
`performance.toJSON().timing.${key} should match performance.timing.${key}`);
assert_equals(timingJSON[key], timing[key],
`performance.timing.toJSON().${key} should match performance.timing.${key}`);
}

// Check PerformanceNavigation toJSON.
const jsonNavigation = json.navigation;
const navigation = performance.navigation;
assert_equals(typeof(navigation.toJSON), 'function');
const navigationJSON = navigation.toJSON();
assert_equals(typeof(navigationJSON), 'object');
// Check PerformanceNavigation attributes, from both:
// 1) |jsonNavigation| from Performance.
// 2) |navigationJSON| from PerformanceNavigation.
let performanceNavigationKeys = ['type', 'redirectCount'];
for (const key of performanceNavigationKeys) {
assert_equals(jsonNavigation[key], navigation[key],
`performance.toJSON().navigation.${key} should match performance.navigation.${key}`);
assert_equals(navigationJSON[key], navigation[key],
`performance.navigation.toJSON().${key} should match performance.navigation.${key}`);
}
}, 'Test performance.toJSON()');
</script>
</body>
</html>
9 changes: 9 additions & 0 deletions test/fixtures/wpt/hr-time/resources/now_frame.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE HTML>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>window.performance.now frame</title>
<link rel="author" title="Google" href="http://www.google.com/" />
</head>
<body></body>
</html>
13 changes: 13 additions & 0 deletions test/fixtures/wpt/hr-time/resources/unload-a.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>Helper page for ../unload-manual.html</title>
</head>
<body>
<script src="./unload.js"></script>
<script>
setupListeners("a", "./unload-b.html");
</script>
<button id="proceed">Click me!</button>
</body>
</html>
13 changes: 13 additions & 0 deletions test/fixtures/wpt/hr-time/resources/unload-b.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>Helper page for ../unload-manual.html</title>
</head>
<body>
<script src="./unload.js"></script>
<script>
setupListeners("b", "./unload-c.html");
</script>
<button id="proceed">Click me again!</button>
</body>
</html>
13 changes: 13 additions & 0 deletions test/fixtures/wpt/hr-time/resources/unload-c.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>Helper page for ../unload-manual.html</title>
</head>
<body>
<script src="./unload.js"></script>
<script>
setupListeners("c", null);
</script>
<button id="proceed">Click me, one last time!</button>
</body>
</html>
51 changes: 51 additions & 0 deletions test/fixtures/wpt/hr-time/resources/unload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const syncDelay = ms => {
const start = performance.now();
let elapsedTime;
do {
elapsedTime = performance.now() - start;
} while (elapsedTime < ms);
};

const markTime = (docName, lifecycleEventName) => {
// Calculating these values before the below `mark` invocation ensures that delays in
// reaching across to the other window object doesn't interfere with the correctness
// of the test.
const dateNow = Date.now();
const performanceNow = performance.now();

window.opener.mark({
docName,
lifecycleEventName,
performanceNow: performanceNow,
dateNow: dateNow
});
};

const setupUnloadPrompt = (docName, msg) => {
window.addEventListener("beforeunload", ev => {
markTime(docName, "beforeunload");
return ev.returnValue = msg || "Click OK to continue test."
});
};

const setupListeners = (docName, nextDocument) => {
window.addEventListener("load", () => {
markTime(docName, "load");
document.getElementById("proceed").addEventListener("click", ev => {
ev.preventDefault();
if (nextDocument) {
document.location = nextDocument;
} else {
window.close();
}
})
});

setupUnloadPrompt(docName);

window.addEventListener("unload", () => {
markTime(docName, "unload");
if (docName !== "c") { syncDelay(1000); }
});
};

59 changes: 59 additions & 0 deletions test/fixtures/wpt/hr-time/test_cross_frame_start.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" >
<title>window.performance.now across frames</title>
<link rel="author" title="Google" href="http://www.google.com/">
<link rel="help" href="http://www.w3.org/TR/hr-time/#sec-extenstions-performance-interface">

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<script type="text/javascript">
setup({explicit_done: true});

var setup_frame = async_test("Setup the frame");

function start_test() {
setup_frame.step_timeout(function () {
var iframe = document.createElement('iframe');
iframe.id = 'frameContext';
iframe.onload = finish_test;
iframe.src = "resources/now_frame.html";
document.body.appendChild(iframe);
setup_frame.done();
}, 1000);
}

function finish_test() {
var childWindow = document.getElementById('frameContext').contentWindow;

// Verify a positive number is returned for both the frame and parent.
test(function() { assert_true(window.performance.now() > 0); }, 'parent performance.now() > 0');
test(function() { assert_true(childWindow.performance.now() > 0); }, 'child performance.now() > 0');

// Verify that the test properly created the child at least a second after the parent.
test(function () { assert_true(childWindow.performance.timing.navigationStart > (window.performance.timing.navigationStart + 1000)); },
'Child created at least 1 second after parent');

test(function () {
var parentNow = window.performance.now();
var childNow = childWindow.performance.now();
var childParentSkew = Math.abs(childNow - parentNow);
assert_true(childParentSkew > 1000, 'Child and parent\'s now()s have different bases (skewed more than 1 second)');

var childLoadTime = childWindow.performance.timing.loadEventStart - childWindow.performance.timing.navigationStart;
assert_true(1000 > (childNow - childLoadTime), 'Child\'s now() is based on its document\'s navigationStart');
}, 'Child and parent time bases are correct');

done();
}
</script>

</head>
<body onload="start_test()">
<h1>Description</h1>
<p>This test validates the values of the window.performance.now() are based on the current document's navigationStart.</p>
<div id="log"></div>
</body>
</html>
45 changes: 45 additions & 0 deletions test/fixtures/wpt/hr-time/timeOrigin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!doctype html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
const windowOrigin = performance.timeOrigin;

test(() => {
// Use a 30ms cushion when comparing with Date() to account for inaccuracy.
const startTime = Date.now();
assert_greater_than_equal(startTime + 30, windowOrigin, 'Date.now() should be at least as large as the window timeOrigin.');
const startNow = performance.now();
assert_less_than_equal(startTime, windowOrigin + startNow + 30, 'Date.now() should be close to window timeOrigin.');
}, 'Window timeOrigin is close to Date.now() when there is no system clock adjustment.');

const workerScript = 'postMessage({timeOrigin: performance.timeOrigin})';
const blob = new Blob([workerScript]);

async_test(function(t) {
const beforeWorkerCreation = performance.now();
const worker = new Worker(URL.createObjectURL(blob));
worker.addEventListener('message', t.step_func_done(function(event) {
const workerOrigin = event.data.timeOrigin;
assert_greater_than_equal(workerOrigin, windowOrigin + beforeWorkerCreation, 'Worker timeOrigin should be greater than the window timeOrigin.');
const afterWorkerCreation = performance.now();
assert_less_than_equal(workerOrigin - windowOrigin, afterWorkerCreation, 'Window and worker timeOrigins should be close.');
}));
}, 'Window and worker timeOrigins are close when created one after another.');

async_test(function(t) {
this.step_timeout(function() {
const workerCreation = performance.now();
const worker = new Worker(URL.createObjectURL(blob));
worker.addEventListener('message', t.step_func_done(function(event) {
const workerOrigin = event.data.timeOrigin;
assert_greater_than_equal(workerOrigin - windowOrigin, 200, 'We waited 200ms to spawn the second worker, so its timeOrigin should be greater than that of the window.');
}));
}, 200);
}, 'Window and worker timeOrigins differ when worker is created after a delay.');
</script>
</body>
</html>
Loading

0 comments on commit 880aab0

Please sign in to comment.