blob: 5f330bb1fdb59ac2700e2eda5b0cbcea2ef59bd3 [file] [log] [blame]
Tom McKee29134be2021-02-19 17:57:291<!DOCTYPE html>
2<html>
3<head>
4<meta charset="utf-8" />
5<title>Resource Timing: PerformanceResourceTiming attributes</title>
6<link rel="help" href="https://www.w3.org/TR/resource-timing-2/#sec-performanceresourcetiming"/>
7<script src="/resources/testharness.js"></script>
8<script src="/resources/testharnessreport.js"></script>
9<script>
10
Tom McKee9b89c072021-03-09 22:44:1011// Returns a promise that settles once the given path has been fetched as an
12// image resource.
Tom McKee29134be2021-02-19 17:57:2913function load_image(path) {
14 return new Promise(resolve => {
15 const img = new Image();
16 img.onload = img.onerror = resolve;
17 img.src = path;
18 });
19}
20
Tom McKee9b89c072021-03-09 22:44:1021// Returns a promise that settles once the given path has been fetched as a
22// font resource.
23function load_font(path) {
24 document.getElementById('forFonts').innerHTML = `
25 <style>
26 @font-face {
27 font-family: ahem;
28 src: url('${path}');
29 }
30 </style>
31 <div style="font-family: ahem;">This fetches ahem font.</div>
32 `;
33 return document.fonts.ready;
34}
35
Tom McKeebd23e122021-03-03 15:32:2136function assert_ordered(entry, attributes) {
37 let before = attributes[0];
38 attributes.slice(1).forEach(after => {
39 assert_greater_than_equal(entry[after], entry[before],
40 `${after} should be greater than ${before}`);
41 before = after;
42 });
43}
44
45function assert_zeroed(entry, attributes) {
46 attributes.forEach(attribute => {
47 assert_equals(entry[attribute], 0, `${attribute} should be 0`);
48 });
49}
50
51function assert_not_negative(entry, attributes) {
52 attributes.forEach(attribute => {
53 assert_greater_than_equal(entry[attribute], 0,
54 `${attribute} should be greater than or equal to 0`);
55 });
56}
57
58function assert_positive(entry, attributes) {
59 attributes.forEach(attribute => {
60 assert_greater_than(entry[attribute], 0,
61 `${attribute} should be greater than 0`);
62 });
63}
64
Tom McKee7a9388b2021-03-09 16:47:2065function assert_http_resource(entry) {
Tom McKeebd23e122021-03-03 15:32:2166 assert_ordered(entry, [
67 "fetchStart",
68 "domainLookupStart",
69 "domainLookupEnd",
70 "connectStart",
71 "connectEnd",
72 "requestStart",
73 "responseStart",
74 "responseEnd",
75 ]);
76
77 assert_zeroed(entry, [
78 "workerStart",
79 "secureConnectionStart",
80 "redirectStart",
81 "redirectEnd",
82 ]);
83
84 assert_not_negative(entry, [
85 "duration",
86 ]);
87
88 assert_positive(entry, [
89 "fetchStart",
90 "transferSize",
91 "encodedBodySize",
92 "decodedBodySize",
93 ]);
Tom McKee7a9388b2021-03-09 16:47:2094}
95
Tom McKeee4c9d942021-03-10 14:25:3196function assert_same_origin_redirected_resource(entry) {
97 assert_positive(entry, [
98 "redirectStart",
99 ]);
100
101 assert_equals(entry.redirectStart, entry.startTime,
102 "redirectStart should be equal to startTime");
103
104 assert_ordered(entry, [
105 "redirectStart",
106 "redirectEnd",
107 "fetchStart",
108 "domainLookupStart",
109 "domainLookupEnd",
110 "connectStart",
111 ]);
112}
113
Tom McKee7179a412021-03-10 13:23:46114// Given a resource-loader and a PerformanceResourceTiming validator, loads a
115// resource and validates the resulting entry.
116function attribute_test(load_resource, validate, test_label) {
117 promise_test(
118 async () => {
119 // Clear out everything that isn't the one ResourceTiming entry under test.
120 performance.clearResourceTimings();
Tom McKee7a9388b2021-03-09 16:47:20121
Tom McKee7179a412021-03-10 13:23:46122 await load_resource();
Tom McKee9b89c072021-03-09 22:44:10123
Tom McKee7179a412021-03-10 13:23:46124 const entry_list = performance.getEntriesByType("resource");
125 if (entry_list.length != 1) {
126 throw new Error(`There should be one entry for one resource (found ${entry_list.length})`);
127 }
128
129 validate(entry_list[0]);
130 }, test_label);
131}
132
133attribute_test(
134 () => load_image("resources/fake_responses.py#hash=1"),
135 entry => {
136 assert_true(entry.name.includes('#hash=1'),
137 "There should be a hash in the resource name");
138 assert_http_resource(entry);
139 },
140 "Image resources should generate conformant entries");
141
142attribute_test(
143 () => load_font("/fonts/Ahem.ttf"),
144 assert_http_resource,
145 "Font resources should generate conformant entries");
Tom McKee29134be2021-02-19 17:57:29146
Tom McKeee4c9d942021-03-10 14:25:31147attribute_test(
148 () => load_image("/common/redirect.py?location=resources/fake_responses.py"),
149 assert_same_origin_redirected_resource,
150 "Same-origin redirects should populate redirectStart/redirectEnd");
Tom McKee29134be2021-02-19 17:57:29151</script>
152</head>
153<body>
154<h1>Description</h1>
155<p>This test validates that PerformanceResourceTiming entries' attributes are
156populated with the correct values.</p>
Tom McKee9b89c072021-03-09 22:44:10157<div id="forFonts"></div>
Tom McKee29134be2021-02-19 17:57:29158</body>
159</html>