summaryrefslogtreecommitdiffstats
path: root/gfx/tests/browser/browser_native_font_cache_macos.js
blob: 919a5c7a72b4b41ea9d7d1265d4d3922e2bd0e55 (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
110
111
112
113
114
115
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

add_task(async () => {
  // Create a tab that loads a system font.
  const CROSS_ORIGIN_DOMAIN = "https://example.com";
  const TARGET_URL = `${CROSS_ORIGIN_DOMAIN}/browser/gfx/tests/browser/file_native_font_cache_macos.html`;
  await BrowserTestUtils.withNewTab(
    { gBrowser, url: TARGET_URL },
    async browser => {
      await SpecialPowers.spawn(browser, [], async () => {
        // Capture a snapshot of the tab, which will load the system font in the
        // parent process.
        const TARGET_WIDTH = 200;
        const TARGET_HEIGHT = 100;

        const rect = new content.window.DOMRect(
          0,
          0,
          TARGET_WIDTH,
          TARGET_HEIGHT
        );
        await SpecialPowers.snapshotContext(content.window, rect, "white");
      });
    }
  );

  // Now create a tab that shows the memory reporter.
  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "about:memory" },
    async browser => {
      // Click the "Measure" button.
      await SpecialPowers.spawn(browser, [], () => {
        let measureButton = content.document.getElementById("measureButton");
        measureButton.click();
      });

      // Copy the page text and check for an expected start with string.
      let copiedText = await new Promise(resolve => {
        const REPORT_TIMEOUT_MS = 15 * 1e3;
        const EXPECTED_START_WITH = "Main Process";
        let mostRecentTextOnClipboard = "";

        SimpleTest.waitForClipboard(
          textOnClipboard => {
            mostRecentTextOnClipboard = textOnClipboard;
            const gotExpected = textOnClipboard.startsWith(EXPECTED_START_WITH);
            if (!gotExpected) {
              // Try copying again.
              EventUtils.synthesizeKey("A", { accelKey: true });
              EventUtils.synthesizeKey("C", { accelKey: true });
            }
            return gotExpected;
          },
          () => {
            EventUtils.synthesizeKey("A", { accelKey: true });
            EventUtils.synthesizeKey("C", { accelKey: true });
          },
          () => {
            resolve(mostRecentTextOnClipboard);
          },
          () => {
            info(`Didn't find expected text within ${REPORT_TIMEOUT_MS}ms.`);
            dump("*******ACTUAL*******\n");
            dump("<<<" + mostRecentTextOnClipboard + ">>>\n");
            dump("********************\n");
            resolve("");
          },
          "text/plain",
          REPORT_TIMEOUT_MS
        );
      });

      isnot(copiedText, "", "Got some text from clipboard.");

      // Search the copied text for our desired pattern. Initially, check for
      // a line with "native-font-resource-mac". If that exists, ensure that it
      // has less than a maximum MB. If that doesn't exist, check instead for
      // a line with "gfx" before the "Other Measurements" section. If that
      // exists, it is tested against the same MB limit. If it doesn't exist,
      // that is an indication that "gfx" doesn't occur in the first section
      // "Explicit Allocations', and therefore isn't holding memory at all.
      const MB_EXCLUSIVE_MAX = 20;
      const nfrm_line = /^.*?(\d+)\.\d+ MB.*-- native-font-resource-mac/m;
      const nfrm_match = nfrm_line.exec(copiedText);
      if (nfrm_match) {
        const nfrm_mb = nfrm_match[1];
        ok(
          nfrm_mb < MB_EXCLUSIVE_MAX,
          `native-font-resource-mac ${nfrm_mb} MB should be less than ${MB_EXCLUSIVE_MAX} MB.`
        );
      } else {
        // Figure out where the "Other Measurements" section begins.
        const om_line = /^Other Measurements$/m;
        const om_match = om_line.exec(copiedText);

        // Find the first gfx line, and if it occurs before the "Other
        // Measurements" section, check its size.
        const gfx_line = /^.*?(\d+)\.\d+ MB.*-- gfx/m;
        const gfx_match = gfx_line.exec(copiedText);
        if (gfx_match && gfx_match.index < om_match.index) {
          const gfx_mb = gfx_match[1];
          ok(
            gfx_mb < MB_EXCLUSIVE_MAX,
            `Explicit Allocations gfx ${gfx_mb} MB should be less than ${MB_EXCLUSIVE_MAX} MB.`
          );
        } else {
          ok(true, "Explicit Allocations gfx is not listed.");
        }
      }
    }
  );
});