blob: 6b5c5d9e3cb2099774b48c79821335f346606ec8 [file] [log] [blame]
Nicolas Pena6e9d51e2019-01-30 15:25:001<!DOCTYPE HTML>
2<meta charset=utf-8>
3<title>Element Timing: multiple images</title>
4<body>
5<style>
6body {
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 Graham163424c2019-05-17 16:47:0325 if (!window.PerformanceElementTiming) {
26 assert_unreached("PerformanceElementTiming is not implemented");
27 }
Nicolas Pena6e9d51e2019-01-30 15:25:0028 const observer = new PerformanceObserver(
29 t.step_func(function(entryList) {
30 entryList.getEntries().forEach( entry => {
Nicolás Peña Moreno84bcab62019-03-06 17:00:4731 if (entry.identifier === 'image1') {
Nicolas Pena6e9d51e2019-01-30 15:25:0032 if (image1Observed) {
33 assert_unreached("Observer received image1 more than once");
34 t.done();
35 }
36 image1Observed = 1;
Nicolás Peña Morenoe42570f2019-10-21 21:23:0937 const pathname1 = window.location.origin + '/element-timing/resources/square100.png';
Nicolás Peña Moreno15d28552019-04-01 19:03:0838 // The images do not contain ID, so expect an empty ID.
Nicolás Peña Moreno66bb6b42019-05-01 17:40:5839 checkElement(entry, pathname1, 'image1', 'img1', beforeRender,
40 document.getElementById('img1'));
Nicolas Pena6e9d51e2019-01-30 15:25:0041 // 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 Morenocf8e8812019-04-01 17:12:0254 checkNaturalSize(entry, 100, 100);
Nicolas Pena6e9d51e2019-01-30 15:25:0055 }
Nicolás Peña Moreno84bcab62019-03-06 17:00:4756 else if (entry.identifier === 'image2') {
Nicolas Pena6e9d51e2019-01-30 15:25:0057 if (image2Observed) {
58 assert_unreached("Observer received image2 more than once");
59 t.done();
60 }
61 image2Observed = 1;
Nicolás Peña Morenoe42570f2019-10-21 21:23:0962 const pathname2 = window.location.origin + '/element-timing/resources/square20.png';
Nicolás Peña Moreno66bb6b42019-05-01 17:40:5863 checkElement(entry, pathname2, 'image2', 'img2', beforeRender,
64 document.getElementById('img2'));
Nicolas Pena6e9d51e2019-01-30 15:25:0065 // 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 Morenocf8e8812019-04-01 17:12:0267 checkNaturalSize(entry, 20, 20);
Nicolas Pena6e9d51e2019-01-30 15:25:0068 }
Nicolás Peña Moreno84bcab62019-03-06 17:00:4769 else if (entry.identifier === 'image3') {
Nicolas Pena6e9d51e2019-01-30 15:25:0070 if (image3Observed) {
71 assert_unreached("Observer received image3 more than once");
72 t.done();
73 }
74 image3Observed = 1;
Nicolás Peña Morenoe42570f2019-10-21 21:23:0975 const pathname3 = window.location.origin + '/element-timing/resources/circle.svg';
Nicolás Peña Moreno66bb6b42019-05-01 17:40:5876 checkElement(entry, pathname3, 'image3', 'img3', beforeRender,
77 document.getElementById('img3'));
Nicolas Pena6e9d51e2019-01-30 15:25:0078 // This image is just to the right of image2.
79 checkRect(entry, [250, 450, 250, 450], "of image3");
Nicolás Peña Morenocf8e8812019-04-01 17:12:0280 checkNaturalSize(entry, 200, 200);
Nicolas Pena6e9d51e2019-01-30 15:25:0081 }
82 else {
Nicolás Peña Moreno84bcab62019-03-06 17:00:4783 assert_unreached("Received an unexpected identifier.");
Nicolas Pena6e9d51e2019-01-30 15:25:0084 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 Moreno15d28552019-04-01 19:03:0896 // Set a different id and elementtiming value for each image.
Nicolas Pena6e9d51e2019-01-30 15:25:0097 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>