From 3da57d17f501da6fa4e09993f141dd02bb758472 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 5 Jun 2023 14:04:35 -0400 Subject: [PATCH] test: update user-timing web-platform tests PR-URL: https://github.com/nodejs/node/pull/48321 Reviewed-By: Antoine du Hamel Reviewed-By: Filip Skokan --- test/fixtures/wpt/README.md | 2 +- .../idlharness-shadowrealm.window.js | 2 + .../wpt/user-timing/mark-errors.any.js | 59 +++++++++++++++---- .../wpt/user-timing/measure-l3.any.js | 8 +-- ...ure_associated_with_navigation_timing.html | 9 +++ .../performance-measure-invalid.worker.js | 7 +++ .../resources/webperftestharness.js | 4 +- .../structured-serialize-detail.any.js | 2 + test/fixtures/wpt/versions.json | 2 +- 9 files changed, 75 insertions(+), 20 deletions(-) create mode 100644 test/fixtures/wpt/user-timing/idlharness-shadowrealm.window.js diff --git a/test/fixtures/wpt/README.md b/test/fixtures/wpt/README.md index 0466bf4c556a22..cf75d8cf184ee8 100644 --- a/test/fixtures/wpt/README.md +++ b/test/fixtures/wpt/README.md @@ -28,7 +28,7 @@ Last update: - resources: https://github.com/web-platform-tests/wpt/tree/fbf1e7d247/resources - streams: https://github.com/web-platform-tests/wpt/tree/9e5ef42bd3/streams - url: https://github.com/web-platform-tests/wpt/tree/7c5c3cc125/url -- user-timing: https://github.com/web-platform-tests/wpt/tree/df24fb604e/user-timing +- user-timing: https://github.com/web-platform-tests/wpt/tree/5ae85bf826/user-timing - wasm/jsapi: https://github.com/web-platform-tests/wpt/tree/cde25e7e3c/wasm/jsapi - wasm/webapi: https://github.com/web-platform-tests/wpt/tree/fd1b23eeaa/wasm/webapi - WebCryptoAPI: https://github.com/web-platform-tests/wpt/tree/17b7ca10fd/WebCryptoAPI diff --git a/test/fixtures/wpt/user-timing/idlharness-shadowrealm.window.js b/test/fixtures/wpt/user-timing/idlharness-shadowrealm.window.js new file mode 100644 index 00000000000000..340da96f749d49 --- /dev/null +++ b/test/fixtures/wpt/user-timing/idlharness-shadowrealm.window.js @@ -0,0 +1,2 @@ +// META: script=/resources/idlharness-shadowrealm.js +idl_test_shadowrealm(["user-timing"], ["hr-time", "performance-timeline", "dom"]); diff --git a/test/fixtures/wpt/user-timing/mark-errors.any.js b/test/fixtures/wpt/user-timing/mark-errors.any.js index dcd36695e22f2c..39bafc045cb721 100644 --- a/test/fixtures/wpt/user-timing/mark-errors.any.js +++ b/test/fixtures/wpt/user-timing/mark-errors.any.js @@ -1,15 +1,50 @@ -test(function() { - assert_throws_js(TypeError, function() { self.performance.mark("mark1", 123); }, "Number passed as a dict argument should cause type-error.") -}, "Number should be rejected as the mark-options.") +// If you're testing an API that constructs a PerformanceMark, add your test here. +// See the for loop below for details. +const markConstructionTests = [ + { + testName: "Number should be rejected as the mark-options.", + testFunction: function(newMarkFunction) { + assert_throws_js(TypeError, function() { newMarkFunction("mark1", 123); }, "Number passed as a dict argument should cause type-error."); + }, + }, -test(function() { - assert_throws_js(TypeError, function() { self.performance.mark("mark1", NaN); }, "NaN passed as a dict argument should cause type-error.") -}, "NaN should be rejected as the mark-options.") + { + testName: "NaN should be rejected as the mark-options.", + testFunction: function(newMarkFunction) { + assert_throws_js(TypeError, function() { newMarkFunction("mark1", NaN); }, "NaN passed as a dict argument should cause type-error."); + }, + }, -test(function() { - assert_throws_js(TypeError, function() { self.performance.mark("mark1", Infinity); }, "Infinity passed as a dict argument should cause type-error.") -}, "Infinity should be rejected as the mark-options.") + { + testName: "Infinity should be rejected as the mark-options.", + testFunction: function(newMarkFunction) { + assert_throws_js(TypeError, function() { newMarkFunction("mark1", Infinity); }, "Infinity passed as a dict argument should cause type-error."); + }, + }, -test(function() { - assert_throws_js(TypeError, function() { self.performance.mark("mark1", "string"); }, "String passed as a dict argument should cause type-error.") -}, "String should be rejected as the mark-options.") + { + testName: "String should be rejected as the mark-options.", + testFunction: function(newMarkFunction) { + assert_throws_js(TypeError, function() { newMarkFunction("mark1", "string"); }, "String passed as a dict argument should cause type-error.") + }, + }, + + { + testName: "Negative startTime in mark-options should be rejected", + testFunction: function(newMarkFunction) { + assert_throws_js(TypeError, function() { newMarkFunction("mark1", {startTime: -1}); }, "Negative startTime should cause type-error.") + }, + }, +]; + +// There are multiple function calls that can construct a mark using the same arguments so we run +// each test on each construction method here, avoiding duplication. +for (let testInfo of markConstructionTests) { + test(function() { + testInfo.testFunction(self.performance.mark); + }, `[performance.mark]: ${testInfo.testName}`); + + test(function() { + testInfo.testFunction((markName, obj) => new PerformanceMark(markName, obj)); + }, `[new PerformanceMark]: ${testInfo.testName}`); +} diff --git a/test/fixtures/wpt/user-timing/measure-l3.any.js b/test/fixtures/wpt/user-timing/measure-l3.any.js index 24c27c483515ed..642b55ab635e2c 100644 --- a/test/fixtures/wpt/user-timing/measure-l3.any.js +++ b/test/fixtures/wpt/user-timing/measure-l3.any.js @@ -17,12 +17,12 @@ test(function() { performance.clearMarks(); performance.clearMeasures(); const markEntry = performance.mark("mark", {startTime: 123}); - const endMin = performance.now(); + const endMin = Number(performance.now().toFixed(2)); const measureEntry = performance.measure("A", "mark", undefined); - const endMax = performance.now(); + const endMax = Number(performance.now().toFixed(2)); assert_equals(measureEntry.startTime, markEntry.startTime); - assert_greater_than_equal(endTime(measureEntry), endMin); - assert_greater_than_equal(endMax, endTime(measureEntry)); + assert_greater_than_equal(Number(endTime(measureEntry).toFixed(2)), endMin); + assert_greater_than_equal(endMax, Number(endTime(measureEntry).toFixed(2))); }, "When the start mark is given and the end is unprovided, the start time of the measure entry should be the start mark's time, the end should be now."); test(function() { diff --git a/test/fixtures/wpt/user-timing/measure_associated_with_navigation_timing.html b/test/fixtures/wpt/user-timing/measure_associated_with_navigation_timing.html index 702e3d5f87f39e..a874ad9172026b 100644 --- a/test/fixtures/wpt/user-timing/measure_associated_with_navigation_timing.html +++ b/test/fixtures/wpt/user-timing/measure_associated_with_navigation_timing.html @@ -34,6 +34,15 @@ test_greater_than(0, context.getEntriesByName('negativeValue', 'measure')[0].duration, 'Measure of current mark to navigationStart should be negative value.'); test_equals(context.getEntriesByName('loadTime', 'measure')[0].duration + context.getEntriesByName('loadEventEnd2a', 'measure')[0].duration, context.getEntriesByName('nav2a', 'measure')[0].duration, 'loadTime plus loadEventEnd to a mark "a" should equal to navigationStart to "a".'); + // We later assert that time has passed between setting one set of marks and another set. + // However, this assertion will fail if the test executes fast enough such that the marks occur + // at the same clock time. This is more likely in browsers such as Firefox that reduce the + // precision of the clock exposed through this API to mitigate timing attacks. To mitigate the + // test failure, we sleep. Firefox may round timestamps to the nearest millisecond in either + // direction - e.g. 10ms & 11.999ms may both round to 11ms - so we need to sleep at least 2ms to + // avoid test failures. To be safe, we sleep 3ms. + sleep_milliseconds(3); + // Following cases test for scenarios that measure names are tied twice. mark_names.forEach(function(name) { context.mark(name); diff --git a/test/fixtures/wpt/user-timing/performance-measure-invalid.worker.js b/test/fixtures/wpt/user-timing/performance-measure-invalid.worker.js index 29efb729992cc6..bab3c35dcb6db5 100644 --- a/test/fixtures/wpt/user-timing/performance-measure-invalid.worker.js +++ b/test/fixtures/wpt/user-timing/performance-measure-invalid.worker.js @@ -6,4 +6,11 @@ test(() => { }); }, "When converting 'navigationStart' to a timestamp, the global object has to be a Window object."); +test(() => { + assert_throws_js(TypeError, () => { + performance.mark('navigationStart'); + performance.measure('name', 'navigationStart', 'navigationStart'); + }); +}, "When converting 'navigationStart' to a timestamp and a mark named 'navigationStart' exists, the global object has to be a Window object."); + done(); diff --git a/test/fixtures/wpt/user-timing/resources/webperftestharness.js b/test/fixtures/wpt/user-timing/resources/webperftestharness.js index 2fbd0210de906d..9627e18a03e49b 100644 --- a/test/fixtures/wpt/user-timing/resources/webperftestharness.js +++ b/test/fixtures/wpt/user-timing/resources/webperftestharness.js @@ -110,12 +110,12 @@ function sleep_milliseconds(n) function test_greater_than(value, greater_than, msg, properties) { - wp_test(function () { assert_true(value > greater_than, msg); }, msg, properties); + wp_test(function () { assert_greater_than(value, greater_than, msg); }, msg, properties); } function test_greater_or_equals(value, greater_than, msg, properties) { - wp_test(function () { assert_true(value >= greater_than, msg); }, msg, properties); + wp_test(function () { assert_greater_than_equal(value, greater_than, msg); }, msg, properties); } function test_not_equals(value, notequals, msg, properties) diff --git a/test/fixtures/wpt/user-timing/structured-serialize-detail.any.js b/test/fixtures/wpt/user-timing/structured-serialize-detail.any.js index 78771b2f7663d4..dcceffde278df1 100644 --- a/test/fixtures/wpt/user-timing/structured-serialize-detail.any.js +++ b/test/fixtures/wpt/user-timing/structured-serialize-detail.any.js @@ -10,6 +10,7 @@ test(function() { performance.clearMarks(); const detail = { randomInfo: 123 } const markEntry = performance.mark("A", { detail }); + assert_equals(markEntry.detail.randomInfo, detail.randomInfo); assert_not_equals(markEntry.detail, detail); }, "The detail property in the mark method should be structured-clone."); @@ -31,6 +32,7 @@ test(function() { performance.clearMeasures(); const detail = { randomInfo: 123 } const measureEntry = performance.measure("A", { start: 0, detail }); + assert_equals(measureEntry.detail.randomInfo, detail.randomInfo); assert_not_equals(measureEntry.detail, detail); }, "The detail property in the measure method should be structured-clone."); diff --git a/test/fixtures/wpt/versions.json b/test/fixtures/wpt/versions.json index 424bd4df67fed0..5ffa879207c1bf 100644 --- a/test/fixtures/wpt/versions.json +++ b/test/fixtures/wpt/versions.json @@ -72,7 +72,7 @@ "path": "url" }, "user-timing": { - "commit": "df24fb604e2d40528ac1d1b5dd970e32fc5c2978", + "commit": "5ae85bf8267ac617833dc013dee9774c9e2a18b7", "path": "user-timing" }, "wasm/jsapi": {