| Tom McKee | 29134be | 2021-02-19 17:57:29 | [diff] [blame] | 1 | <!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 McKee | 9b89c07 | 2021-03-09 22:44:10 | [diff] [blame] | 11 | // Returns a promise that settles once the given path has been fetched as an | 
|  | 12 | // image resource. | 
| Tom McKee | 29134be | 2021-02-19 17:57:29 | [diff] [blame] | 13 | function 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 McKee | 9b89c07 | 2021-03-09 22:44:10 | [diff] [blame] | 21 | // Returns a promise that settles once the given path has been fetched as a | 
|  | 22 | // font resource. | 
|  | 23 | function 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 McKee | bd23e12 | 2021-03-03 15:32:21 | [diff] [blame] | 36 | function 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 |  | 
|  | 45 | function assert_zeroed(entry, attributes) { | 
|  | 46 | attributes.forEach(attribute => { | 
|  | 47 | assert_equals(entry[attribute], 0, `${attribute} should be 0`); | 
|  | 48 | }); | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | function 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 |  | 
|  | 58 | function 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 McKee | 7a9388b | 2021-03-09 16:47:20 | [diff] [blame] | 65 | function assert_http_resource(entry) { | 
| Tom McKee | bd23e12 | 2021-03-03 15:32:21 | [diff] [blame] | 66 | 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 McKee | 7a9388b | 2021-03-09 16:47:20 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
| Tom McKee | e4c9d94 | 2021-03-10 14:25:31 | [diff] [blame] | 96 | function 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 McKee | 7179a41 | 2021-03-10 13:23:46 | [diff] [blame] | 114 | // Given a resource-loader and a PerformanceResourceTiming validator, loads a | 
|  | 115 | // resource and validates the resulting entry. | 
|  | 116 | function 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 McKee | 7a9388b | 2021-03-09 16:47:20 | [diff] [blame] | 121 |  | 
| Tom McKee | 7179a41 | 2021-03-10 13:23:46 | [diff] [blame] | 122 | await load_resource(); | 
| Tom McKee | 9b89c07 | 2021-03-09 22:44:10 | [diff] [blame] | 123 |  | 
| Tom McKee | 7179a41 | 2021-03-10 13:23:46 | [diff] [blame] | 124 | 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 |  | 
|  | 133 | attribute_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 |  | 
|  | 142 | attribute_test( | 
|  | 143 | () => load_font("/fonts/Ahem.ttf"), | 
|  | 144 | assert_http_resource, | 
|  | 145 | "Font resources should generate conformant entries"); | 
| Tom McKee | 29134be | 2021-02-19 17:57:29 | [diff] [blame] | 146 |  | 
| Tom McKee | e4c9d94 | 2021-03-10 14:25:31 | [diff] [blame] | 147 | attribute_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 McKee | 29134be | 2021-02-19 17:57:29 | [diff] [blame] | 151 | </script> | 
|  | 152 | </head> | 
|  | 153 | <body> | 
|  | 154 | <h1>Description</h1> | 
|  | 155 | <p>This test validates that PerformanceResourceTiming entries' attributes are | 
|  | 156 | populated with the correct values.</p> | 
| Tom McKee | 9b89c07 | 2021-03-09 22:44:10 | [diff] [blame] | 157 | <div id="forFonts"></div> | 
| Tom McKee | 29134be | 2021-02-19 17:57:29 | [diff] [blame] | 158 | </body> | 
|  | 159 | </html> |