summaryrefslogtreecommitdiffstats
path: root/gfx/layers/apz/test/mochitest/helper_key_scroll.html
blob: 021e2803b7e0a19c18d38bfb9ce0a9e6b75dee41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1383365
-->
<head>
  <meta charset="utf-8">
  <title>Async key scrolling test, helper page</title>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <script src="/tests/SimpleTest/paint_listener.js"></script>
  <script type="application/javascript" src="apz_test_utils.js"></script>
  <script type="application/javascript">
    // --------------------------------------------------------------------
    // Async key scrolling test
    //
    // This test checks that a key scroll occurs asynchronously.
    //
    // The page contains a <div> that is large enough to make the page
    // scrollable. We first synthesize a page down to scroll to the bottom
    // of the page. Once we have reached the bottom of the page, we synthesize
    // a page up to get us back to the top of the page.
    //
    // Once at the top, we request test data from APZ, rebuild the APZC tree
    // structure, and use it to check that async key scrolling happened.
    // --------------------------------------------------------------------

    async function test() {
      // Sanity check
      is(checkHasAsyncKeyScrolled(), false, "expected no async key scrolling before test");

      // Send a key to initiate a page scroll to take us to the bottom of the
      // page. This scroll is done synchronously because APZ doesn't have
      // current focus state at page load.
      let scrollBottomPromise = new Promise(resolve => {
        let checkBottom = function(e) {
          if (window.scrollY < window.scrollMaxY) {
            return;
          }
          info("Reached final scroll position of sync KEY_End scroll");
          window.removeEventListener("scroll", checkBottom);
          resolve();
        };
        window.addEventListener("scroll", checkBottom);
      });

      window.synthesizeKey("KEY_End");
      await scrollBottomPromise;

      // Spin the refresh driver a few times, so that the AsyncScroll instance
      // that was running the main-thread scroll animation finishes up and
      // triggers any repaints that it needs to.
      var utils = SpecialPowers.DOMWindowUtils;
      for (var i = 0; i < 10; i++) {
        utils.advanceTimeAndRefresh(50);
      }
      utils.restoreNormalRefresh();

      // Wait for the APZ to reach a stable state as well, before dispatching
      // the next key input or the default action won't occur.
      await promiseApzFlushedRepaints();

      is(checkHasAsyncKeyScrolled(), false, "expected no async key scrolling before KEY_Home dispatch");

      // This scroll should be asynchronous now that the focus state is up to date.
      let scrollTopPromise = new Promise(resolve => {
        let checkTop = function(e) {
          if (window.scrollY > 0) {
            return;
          }
          info("Reached final scroll position of async KEY_Home scroll");
          window.removeEventListener("scroll", checkTop);
          resolve();
        };
        window.addEventListener("scroll", checkTop);
      });

      window.synthesizeKey("KEY_Home");
      await scrollTopPromise;

      // Wait for APZ to settle and then check that async scrolling happened.
      await promiseApzFlushedRepaints();
      is(checkHasAsyncKeyScrolled(), true, "expected async key scrolling after test");
    }

    function checkHasAsyncKeyScrolled() {
      // Reconstruct the APZC tree structure in the last paint.
      var apzcTree = getLastApzcTree();
      var rcd = findRcdNode(apzcTree);

      if (rcd) {
        return rcd.hasAsyncKeyScrolled === "1";
      }

      info("Last paint rcd is null");
      return false;
    }

    waitUntilApzStable()
      .then(forceLayerTreeToCompositor)
      .then(test)
      .then(subtestDone, subtestFailed);
  </script>
</head>
<body style="height: 500px; overflow: scroll">
  <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1383365">Async key scrolling test</a>
  <!-- Put enough content into the page to make it have a nonzero scroll range -->
  <div style="height: 5000px"></div>
</body>
</html>