OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <script src="/resources/testharness.js"></script> |
| 3 <script src="/resources/testharnessreport.js"></script> |
| 4 <script> |
| 5 const XHR_SYNC_URL = '/resources/dummy.xml?t=syncxhr'; |
| 6 const XHR_ASYNC_URL = '/resources/dummy.xml?t=asyncxhr'; |
| 7 const FETCH_URL = '/resources/dummy.xml?t=fetch'; |
| 8 var sizes = {}; |
| 9 sizes[XHR_SYNC_URL] = sizes[XHR_ASYNC_URL] = sizes[FETCH_URL] = 60; |
| 10 |
| 11 const totalAsyncResources = 2; |
| 12 var seenAsyncResources = 0; |
| 13 var seenResources = 0; |
| 14 var t = async_test('PerformanceResourceTiming sizes XHR and Fetch test'); |
| 15 |
| 16 function asyncResourceLoaded(eventOrText) { |
| 17 ++seenAsyncResources; |
| 18 if (seenAsyncResources == totalAsyncResources) |
| 19 setTimeout(t.step_func(checkResourceSizes), 0); |
| 20 } |
| 21 |
| 22 function checkSizeFields(entry, expectedSize) { |
| 23 assert_equals(entry.decodedBodySize, expectedSize, 'decodedBodySize'); |
| 24 assert_equals(entry.encodedBodySize, expectedSize, 'encodedBodySize'); |
| 25 // Because of caching, the value of transferSize is sensitive to the |
| 26 // execution order of layout tests, and so the result of the test should not |
| 27 // depend on it. |
| 28 assert_true(entry.transferSize !== undefined, |
| 29 'transferSize should be defined'); |
| 30 } |
| 31 |
| 32 function checkResourceSizes() { |
| 33 var expectedResources = Object.keys(sizes).length; |
| 34 var entries = performance.getEntriesByType('resource'); |
| 35 for (var entry of entries) { |
| 36 var urlObject = new URL(entry.name); |
| 37 var urlKey = urlObject.pathname + urlObject.search; |
| 38 var size = sizes[urlKey]; |
| 39 if (size) { |
| 40 checkSizeFields(entry, size); |
| 41 ++seenResources; |
| 42 } |
| 43 } |
| 44 assert_equals(seenResources, expectedResources, |
| 45 'seenResources'); |
| 46 t.done(); |
| 47 } |
| 48 |
| 49 function runTest() { |
| 50 var sync = new XMLHttpRequest(); |
| 51 sync.open('GET', XHR_SYNC_URL, false); |
| 52 sync.send(); |
| 53 var async = new XMLHttpRequest(); |
| 54 async.open('GET', XHR_ASYNC_URL); |
| 55 async.onload = t.step_func(asyncResourceLoaded); |
| 56 async.onerror = t.step_func(() => assert_unreached('Async XHR error')); |
| 57 async.send(); |
| 58 fetch(FETCH_URL) |
| 59 .then(response => response.text()) |
| 60 .then(t.step_func(asyncResourceLoaded)) |
| 61 .catch(t.step_func(() => assert_unreached('Fetch error'))); |
| 62 } |
| 63 |
| 64 window.onload = t.step_func(runTest); |
| 65 </script> |
OLD | NEW |