summaryrefslogtreecommitdiffstats
path: root/gfx/layers/apz/test/mochitest/browser_test_background_tab_scroll.js
blob: 4ce820019911184118e904ca91f0113deade92ba (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
add_task(async function test_main() {
  // Load page in the background. This will cause the first-paint of the
  // tab (which has ScrollPositionUpdate instances) to get sent to the
  // compositor, but the parent process RefLayer won't be pointing to this
  // tab so APZ never sees the ScrollPositionUpdate instances.

  let url =
    "http://mochi.test:8888/browser/gfx/layers/apz/test/mochitest/helper_background_tab_scroll.html#scrolltarget";
  let backgroundTab = BrowserTestUtils.addTab(gBrowser, url);
  let browser = backgroundTab.linkedBrowser;
  await BrowserTestUtils.browserLoaded(browser, false, url);
  dump("Done loading background tab\n");

  // Switch to the foreground, to let the APZ tree get built.
  await BrowserTestUtils.switchTab(gBrowser, backgroundTab);
  dump("Switched background tab to foreground\n");

  // Verify main-thread scroll position is where we expect
  let scrollPos = await ContentTask.spawn(browser, null, function () {
    return content.window.scrollY;
  });
  is(scrollPos, 5000, "Expected background tab to be at scroll pos 5000");

  // Trigger an APZ-side scroll via native wheel event, followed by some code
  // to ensure APZ's repaint requests to arrive at the main-thread. If
  // things are working properly, the main thread will accept the repaint
  // requests and update the main-thread scroll position. If the APZ side
  // is sending incorrect scroll generations in the repaint request, then
  // the main thread will fail to clear the main-thread scroll origin (which
  // was set by the scroll to the #scrolltarget anchor), and so will not
  // accept APZ's scroll position updates.
  let contentScrollFunction = async function () {
    await content.window.wrappedJSObject.promiseNativeWheelAndWaitForWheelEvent(
      content.window,
      100,
      100,
      0,
      200
    );

    // Advance some/all frames of the APZ wheel animation
    let utils = content.window.SpecialPowers.getDOMWindowUtils(content.window);
    for (var i = 0; i < 10; i++) {
      utils.advanceTimeAndRefresh(16);
    }
    utils.restoreNormalRefresh();
    // Flush pending APZ repaints, then read the main-thread scroll
    // position
    await content.window.wrappedJSObject.promiseOnlyApzControllerFlushed(
      content.window
    );
    return content.window.scrollY;
  };
  scrollPos = await ContentTask.spawn(browser, null, contentScrollFunction);

  // Verify main-thread scroll position has changed
  ok(
    scrollPos < 5000,
    `Expected background tab to have scrolled up, is at ${scrollPos}`
  );

  // Cleanup
  let tabClosed = BrowserTestUtils.waitForTabClosing(backgroundTab);
  BrowserTestUtils.removeTab(backgroundTab);
  await tabClosed;
});