summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/performance/browser_tabclose_grow.js
blob: 7ad43809cdfe004b95cf0d200985de1c787184b1 (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
"use strict";

/**
 * WHOA THERE: We should never be adding new things to EXPECTED_REFLOWS.
 * Instead of adding reflows to the list, you should be modifying your code to
 * avoid the reflow.
 *
 * See https://firefox-source-docs.mozilla.org/performance/bestpractices.html
 * for tips on how to do that.
 */
const EXPECTED_REFLOWS = [
  /**
   * Nothing here! Please don't add anything new!
   */
];

/*
 * This test ensures that there are no unexpected
 * uninterruptible reflows when closing a tab that will
 * cause the existing tabs to grow bigger.
 */
add_task(async function () {
  // Force-enable tab animations
  gReduceMotionOverride = false;

  await ensureNoPreloadedBrowser();
  await disableFxaBadge();

  // The test starts on about:blank and opens an about:blank
  // tab which triggers opening the toolbar since
  // ensureNoPreloadedBrowser sets AboutNewTab.newTabURL to about:blank.
  await SpecialPowers.pushPrefEnv({
    set: [["browser.toolbars.bookmarks.visibility", "never"]],
  });

  // At the time of writing, there are no reflows on tab closing with
  // tab growth. Mochitest will fail if we have no assertions, so we
  // add one here to make sure nobody adds any new ones.
  Assert.equal(
    EXPECTED_REFLOWS.length,
    0,
    "We shouldn't have added any new expected reflows."
  );

  // Compute the number of tabs we can put into the strip without
  // overflowing. If we remove one of the tabs, we know that the
  // remaining tabs will grow to fill the remaining space in the
  // tabstrip.
  const TAB_COUNT_FOR_GROWTH = computeMaxTabCount();
  await createTabs(TAB_COUNT_FOR_GROWTH);

  let lastTab = gBrowser.tabs[gBrowser.tabs.length - 1];
  await BrowserTestUtils.switchTab(gBrowser, lastTab);

  let tabStripRect =
    gBrowser.tabContainer.arrowScrollbox.getBoundingClientRect();

  function isInTabStrip(r) {
    return (
      r.y1 >= tabStripRect.top &&
      r.y2 <= tabStripRect.bottom &&
      r.x1 >= tabStripRect.left &&
      r.x2 <= tabStripRect.right &&
      // It would make sense for each rect to have a width smaller than
      // a tab (ie. tabstrip.width / tabcount), but tabs are small enough
      // that they sometimes get reported in the same rect.
      // So we accept up to the width of n-1 tabs.
      r.w <=
        (gBrowser.tabs.length - 1) *
          Math.ceil(tabStripRect.width / gBrowser.tabs.length)
    );
  }

  await withPerfObserver(
    async function () {
      let switchDone = BrowserTestUtils.waitForEvent(window, "TabSwitchDone");
      let tab = gBrowser.tabs[gBrowser.tabs.length - 1];
      gBrowser.removeTab(tab, { animate: true });
      await BrowserTestUtils.waitForEvent(tab, "TabAnimationEnd");
      await switchDone;
    },
    {
      expectedReflows: EXPECTED_REFLOWS,
      frames: {
        filter: rects => rects.filter(r => !isInTabStrip(r)),
      },
    }
  );

  await removeAllButFirstTab();
});