| Nicolas Pena | 6e9d51e | 2019-01-30 15:25:00 | [diff] [blame] | 1 | <!DOCTYPE HTML> |
| 2 | <meta charset=utf-8> |
| 3 | <title>Element Timing: multiple images</title> |
| 4 | <body> |
| 5 | <style> |
| 6 | body { |
| 7 | margin: 0; |
| 8 | } |
| 9 | #img1 { |
| 10 | display: block; |
| 11 | margin-left: auto; |
| 12 | margin-right: auto; |
| 13 | } |
| 14 | #img2 { |
| 15 | margin-top:150px; |
| 16 | margin-left:50px; |
| 17 | } |
| 18 | </style> |
| 19 | <script src="/resources/testharness.js"></script> |
| 20 | <script src="/resources/testharnessreport.js"></script> |
| 21 | <script src="resources/element-timing-helpers.js"></script> |
| 22 | <script> |
| 23 | let beforeRender, image1Observed=0, image2Observed=0, image3Observed=0; |
| 24 | async_test(function (t) { |
| James Graham | 163424c | 2019-05-17 16:47:03 | [diff] [blame] | 25 | if (!window.PerformanceElementTiming) { |
| 26 | assert_unreached("PerformanceElementTiming is not implemented"); |
| 27 | } |
| Nicolas Pena | 6e9d51e | 2019-01-30 15:25:00 | [diff] [blame] | 28 | const observer = new PerformanceObserver( |
| 29 | t.step_func(function(entryList) { |
| 30 | entryList.getEntries().forEach( entry => { |
| Nicolás Peña Moreno | 84bcab6 | 2019-03-06 17:00:47 | [diff] [blame] | 31 | if (entry.identifier === 'image1') { |
| Nicolas Pena | 6e9d51e | 2019-01-30 15:25:00 | [diff] [blame] | 32 | if (image1Observed) { |
| 33 | assert_unreached("Observer received image1 more than once"); |
| 34 | t.done(); |
| 35 | } |
| 36 | image1Observed = 1; |
| Nicolás Peña Moreno | e42570f | 2019-10-21 21:23:09 | [diff] [blame] | 37 | const pathname1 = window.location.origin + '/element-timing/resources/square100.png'; |
| Nicolás Peña Moreno | 15d2855 | 2019-04-01 19:03:08 | [diff] [blame] | 38 | // The images do not contain ID, so expect an empty ID. |
| Nicolás Peña Moreno | 66bb6b4 | 2019-05-01 17:40:58 | [diff] [blame] | 39 | checkElement(entry, pathname1, 'image1', 'img1', beforeRender, |
| 40 | document.getElementById('img1')); |
| Nicolas Pena | 6e9d51e | 2019-01-30 15:25:00 | [diff] [blame] | 41 | // This image is horizontally centered. |
| 42 | // Using abs and comparing to 1 because the viewport sizes could be odd. |
| 43 | // If a size is odd, then image cannot be in the pure center, but left |
| 44 | // and right should still be very close to their estimated coordinates. |
| 45 | assert_less_than_equal(Math.abs(entry.intersectionRect.left - |
| 46 | (document.documentElement.clientWidth / 2 - 50)), 1, |
| 47 | 'left of rect for image1'); |
| 48 | assert_less_than_equal(Math.abs(entry.intersectionRect.right - |
| 49 | (document.documentElement.clientWidth / 2 + 50)), 1, |
| 50 | 'right of rect for image1'); |
| 51 | assert_equals(entry.intersectionRect.top, 0, 'top of rect for image1'); |
| 52 | assert_equals(entry.intersectionRect.bottom, |
| 53 | 100, 'bottom of rect for image1'); |
| Nicolás Peña Moreno | cf8e881 | 2019-04-01 17:12:02 | [diff] [blame] | 54 | checkNaturalSize(entry, 100, 100); |
| Nicolas Pena | 6e9d51e | 2019-01-30 15:25:00 | [diff] [blame] | 55 | } |
| Nicolás Peña Moreno | 84bcab6 | 2019-03-06 17:00:47 | [diff] [blame] | 56 | else if (entry.identifier === 'image2') { |
| Nicolas Pena | 6e9d51e | 2019-01-30 15:25:00 | [diff] [blame] | 57 | if (image2Observed) { |
| 58 | assert_unreached("Observer received image2 more than once"); |
| 59 | t.done(); |
| 60 | } |
| 61 | image2Observed = 1; |
| Nicolás Peña Moreno | e42570f | 2019-10-21 21:23:09 | [diff] [blame] | 62 | const pathname2 = window.location.origin + '/element-timing/resources/square20.png'; |
| Nicolás Peña Moreno | 66bb6b4 | 2019-05-01 17:40:58 | [diff] [blame] | 63 | checkElement(entry, pathname2, 'image2', 'img2', beforeRender, |
| 64 | document.getElementById('img2')); |
| Nicolas Pena | 6e9d51e | 2019-01-30 15:25:00 | [diff] [blame] | 65 | // This image should be below image 1, and should respect the margin. |
| 66 | checkRect(entry, [50, 250, 250, 450], "of image2"); |
| Nicolás Peña Moreno | cf8e881 | 2019-04-01 17:12:02 | [diff] [blame] | 67 | checkNaturalSize(entry, 20, 20); |
| Nicolas Pena | 6e9d51e | 2019-01-30 15:25:00 | [diff] [blame] | 68 | } |
| Nicolás Peña Moreno | 84bcab6 | 2019-03-06 17:00:47 | [diff] [blame] | 69 | else if (entry.identifier === 'image3') { |
| Nicolas Pena | 6e9d51e | 2019-01-30 15:25:00 | [diff] [blame] | 70 | if (image3Observed) { |
| 71 | assert_unreached("Observer received image3 more than once"); |
| 72 | t.done(); |
| 73 | } |
| 74 | image3Observed = 1; |
| Nicolás Peña Moreno | e42570f | 2019-10-21 21:23:09 | [diff] [blame] | 75 | const pathname3 = window.location.origin + '/element-timing/resources/circle.svg'; |
| Nicolás Peña Moreno | 66bb6b4 | 2019-05-01 17:40:58 | [diff] [blame] | 76 | checkElement(entry, pathname3, 'image3', 'img3', beforeRender, |
| 77 | document.getElementById('img3')); |
| Nicolas Pena | 6e9d51e | 2019-01-30 15:25:00 | [diff] [blame] | 78 | // This image is just to the right of image2. |
| 79 | checkRect(entry, [250, 450, 250, 450], "of image3"); |
| Nicolás Peña Moreno | cf8e881 | 2019-04-01 17:12:02 | [diff] [blame] | 80 | checkNaturalSize(entry, 200, 200); |
| Nicolas Pena | 6e9d51e | 2019-01-30 15:25:00 | [diff] [blame] | 81 | } |
| 82 | else { |
| Nicolás Peña Moreno | 84bcab6 | 2019-03-06 17:00:47 | [diff] [blame] | 83 | assert_unreached("Received an unexpected identifier."); |
| Nicolas Pena | 6e9d51e | 2019-01-30 15:25:00 | [diff] [blame] | 84 | t.done(); |
| 85 | } |
| 86 | if (image1Observed && image2Observed && image3Observed) { |
| 87 | t.done(); |
| 88 | } |
| 89 | }); |
| 90 | }) |
| 91 | ); |
| 92 | observer.observe({entryTypes: ['element']}); |
| 93 | function addImage(number, source, width=0) { |
| 94 | const img = document.createElement('img'); |
| 95 | img.src = source; |
| Nicolás Peña Moreno | 15d2855 | 2019-04-01 19:03:08 | [diff] [blame] | 96 | // Set a different id and elementtiming value for each image. |
| Nicolas Pena | 6e9d51e | 2019-01-30 15:25:00 | [diff] [blame] | 97 | img.id = 'img' + number; |
| 98 | img.setAttribute('elementtiming', 'image' + number); |
| 99 | if (width !== 0) |
| 100 | img.width = width; |
| 101 | document.body.appendChild(img); |
| 102 | } |
| 103 | // Add the images during onload to be sure that the observer is registered in |
| 104 | // time to observe the element timing. |
| 105 | window.onload = () => { |
| 106 | addImage(1, 'resources/square100.png'); |
| 107 | // Use requestAnimationFrame and a timeout to ensure that the images are |
| 108 | // processed in the order we want. |
| 109 | requestAnimationFrame( () => { |
| 110 | t.step_timeout( () => { |
| 111 | // Set the size equal to that of image3 to make positioning easier. |
| 112 | addImage(2, 'resources/square20.png', 200); |
| 113 | requestAnimationFrame( () => { |
| 114 | t.step_timeout( () => { |
| 115 | addImage(3, 'resources/circle.svg'); |
| 116 | }, 0); |
| 117 | }); |
| 118 | }, 0); |
| 119 | }); |
| 120 | beforeRender = performance.now(); |
| 121 | }; |
| 122 | }, 'PerformanceObserver can observe multiple image elements.'); |
| 123 | </script> |
| 124 | </body> |