input-timestamp.html (2643B)
1 <!DOCTYPE HTML> 2 <meta charset=utf-8> 3 <title>Layout Instability: observe timestamp after user input</title> 4 5 <body> 6 <style> 7 #myDiv { 8 position: relative; 9 width: 300px; 10 height: 100px; 11 background: blue; 12 } 13 14 /* Disable the button's focus ring, which otherwise expands its visual rect by 15 * 1px on all sides, triggering a layout shift event. 16 */ 17 #button { 18 outline: none; 19 } 20 </style> 21 <div id='myDiv'></div> 22 <button id='button'>Generate a 'click' event</button> 23 <script src=/resources/testharness.js></script> 24 <script src=/resources/testharnessreport.js></script> 25 <script src=/resources/testdriver.js></script> 26 <script src=/resources/testdriver-vendor.js></script> 27 <script src=resources/util.js></script> 28 <script src=/event-timing/resources/event-timing-test-utils.js></script> 29 <script> 30 let timeAfterClick; 31 32 promise_test(async t => { 33 assert_implements(window.LayoutShift, 'Layout Instability is not supported.'); 34 // Wait for the initial render to complete. 35 await waitForAnimationFrames(2); 36 37 const startTime = performance.now(); 38 return new Promise(resolve => { 39 const observer = new PerformanceObserver( 40 t.step_func(entryList => { 41 const endTime = performance.now(); 42 assert_equals(entryList.getEntries().length, 1); 43 const entry = entryList.getEntries()[0]; 44 assert_equals(entry.entryType, "layout-shift"); 45 assert_equals(entry.name, ""); 46 assert_greater_than_equal(entry.startTime, startTime); 47 assert_less_than_equal(entry.startTime, endTime); 48 assert_equals(entry.duration, 0.0); 49 // The layout shift value should be: 50 // 300 * (100 + 60) * (60 / maxDimension) / viewport size. 51 assert_equals(entry.value, computeExpectedScore(300 * (100 + 60), 60)); 52 // We should see that there was a click input entry. 53 assert_equals(entry.hadRecentInput, false); 54 assert_greater_than_equal(timeAfterClick, entry.lastInputTime); 55 resolve(); 56 }) 57 ); 58 observer.observe({ entryTypes: ['layout-shift'] }); 59 // User input event 60 clickAndBlockMain('button').then(() => { 61 // 600ms delay 62 step_timeout(function() { 63 timeAfterClick = performance.now(); 64 // Modify the position of the div. 65 document.getElementById('myDiv').style = "top: 60px"; 66 }, 600); 67 }); 68 }); 69 }, 'Layout shift right after user input is observable via PerformanceObserver.'); 70 </script> 71 72 </body>