OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <script src="/resources/testharness.js"></script> |
| 3 <script src="/resources/testharnessreport.js"></script> |
| 4 <style> |
| 5 /* "display: none" prevents loading of <object> resources, so use */ |
| 6 /* "visibility: hidden" instead. */ |
| 7 .invisible { visibility: hidden; } |
| 8 </style> |
| 9 <script> |
| 10 const urls = { |
| 11 '/resources/square20.jpg': ['img', 529], |
| 12 '/resources/square20.gif': ['img', 50], |
| 13 '/resources/square20.png': ['img', 150], |
| 14 '/resources/test.ogv': ['video', 103746], |
| 15 '/resources/test.oga': ['audio', 20787], |
| 16 '/resources/dummy.html': ['iframe', 34], |
| 17 '/resources/dummy.js': ['script', 7], |
| 18 '/resources/dummy.css': ['link', 8], |
| 19 '/svg/resources/greenSquare.svg': ['object', 244], |
| 20 }; |
| 21 |
| 22 const srcFieldExceptions = { |
| 23 'object': 'data', |
| 24 'link': 'href', |
| 25 }; |
| 26 |
| 27 const totalResourceCount = Object.keys(urls).length; |
| 28 const cacheBuster = '?cb=' + Math.random().toString().substr(2); |
| 29 var seenResourceCount = 0; |
| 30 var t = async_test('PerformanceResourceTiming sizes tags test'); |
| 31 |
| 32 function isMediaTag(tag) { |
| 33 return tag === 'video' || tag === 'audio'; |
| 34 } |
| 35 |
| 36 function addTag(url, tag) { |
| 37 var element = document.createElement(tag); |
| 38 var srcfield = srcFieldExceptions[tag]; |
| 39 if (!srcfield) { |
| 40 srcfield = 'src'; |
| 41 } |
| 42 element.setAttribute(srcfield, url + cacheBuster); |
| 43 element.setAttribute('class', 'invisible'); |
| 44 if (tag === 'link') { |
| 45 element.setAttribute('rel', 'stylesheet'); |
| 46 } |
| 47 var onloadevent = isMediaTag(tag) ? 'oncanplaythrough' : 'onload'; |
| 48 element[onloadevent] = t.step_func(() => { |
| 49 // The entry is not guaranteed to be added synchronously by the spec, |
| 50 // although the current implementation does. |
| 51 setTimeout(t.step_func(loaded), 0); |
| 52 }); |
| 53 document.body.appendChild(element); |
| 54 } |
| 55 |
| 56 // Postpone checking until everything is loaded so that the checks will be done |
| 57 // in a deterministic order. |
| 58 function loaded() { |
| 59 ++seenResourceCount; |
| 60 if (seenResourceCount === totalResourceCount) |
| 61 checkAllResources(); |
| 62 } |
| 63 |
| 64 function checkAllResources() { |
| 65 for (relativeUrl of Object.keys(urls).sort()) { |
| 66 checkResourceSizes(relativeUrl + cacheBuster, urls[relativeUrl][1]); |
| 67 } |
| 68 t.done(); |
| 69 } |
| 70 |
| 71 function checkResourceSizes(relativeUrl, expectedSize) { |
| 72 var absoluteUrl = new URL(relativeUrl, location.href).href; |
| 73 var entry = performance.getEntriesByName(absoluteUrl, 'resource')[0]; |
| 74 decodedBodySize = entry.decodedBodySize; |
| 75 assert_equals(entry.decodedBodySize, expectedSize, |
| 76 relativeUrl + ' decodedBodySize'); |
| 77 assert_equals(entry.encodedBodySize, expectedSize, |
| 78 relativeUrl + ' encodedBodySize'); |
| 79 transferSize = entry.transferSize; |
| 80 assert_greater_than(entry.transferSize, expectedSize, |
| 81 relativeUrl + ' transferSize'); |
| 82 } |
| 83 |
| 84 function runTest() { |
| 85 for (var url in urls) { |
| 86 addTag(url, urls[url][0]); |
| 87 } |
| 88 } |
| 89 |
| 90 window.onload = t.step_func(runTest); |
| 91 </script> |
OLD | NEW |