scheduling-isInputPending.html 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset=utf-8 />
  5. <title>IsInputPending: clicks should be reported during main thread contention</title>
  6. </title>
  7. <div>
  8. Click the button twice and expect two green lines of text.
  9. This test should be run under --enable-blink-features=ExperimentalIsInputPending
  10. </div>
  11. </head>
  12. <button onclick="next()">Start</button>
  13. <p id="preClickResult"></p>
  14. <p id="postClickResult"></p>
  15. <script>
  16. function hasPendingClick() {
  17. return navigator.scheduling.isInputPending(['click']);
  18. }
  19. // |expected| is the expected value from isInputPending(['click']).
  20. function createResult(actual, expected) {
  21. const result = document.createElement('div');
  22. if (actual == expected) {
  23. result.innerHTML = 'PASSED: isInputPending was ' + actual;
  24. result.style = 'color:green';
  25. }
  26. else {
  27. result.innerHTML = 'FAILED: isInputPending was ' + actual;
  28. result.style = 'color:red';
  29. }
  30. return result;
  31. }
  32. let state = "start";
  33. function next() {
  34. switch (state) {
  35. case "start":
  36. // Don't return from the event loop until a click is detected.
  37. while (!hasPendingClick()) {}
  38. const preClickResult = document.querySelector("#preClickResult");
  39. preClickResult.appendChild(createResult(true, true));
  40. state = "clicked";
  41. break;
  42. case "clicked":
  43. // In the click event handler, expect no pending clicks.
  44. const postClickResult = document.querySelector("#postClickResult");
  45. postClickResult.appendChild(createResult(hasPendingClick(), false));
  46. state = "end";
  47. break;
  48. case "end":
  49. break;
  50. }
  51. }
  52. </script>
  53. </html>