summaryrefslogtreecommitdiffstats
path: root/toolkit/components/aboutmemory/tests/test_aboutmemory4.xul
blob: f2c752ac57cd606d6f1bb963bd6877d7e964d4d6 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
<window title="about:memory"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>

  <!-- This file tests the loading of memory reports from file when specified
       in about:memory's URL (via the "file=" suffix). -->

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml"></body>

  <!-- test code goes here -->
  <script type="application/javascript">
  <![CDATA[
  "use strict";

  function makePathname(aFilename) {
    let file = Components.classes["@mozilla.org/file/directory_service;1"]
               .getService(Components.interfaces.nsIProperties)
               .get("CurWorkD", Components.interfaces.nsIFile);
    file.append("chrome");
    file.append("toolkit");
    file.append("components");
    file.append("aboutmemory");
    file.append("tests");
    file.append(aFilename);
    return file.path;
  }

  // Load the given file into the frame, then copy+paste the entire frame and
  // check that the cut text matches what we expect.
  function test(aFilename, aExpected, aNext) {
    let frame = document.createElementNS("http://www.w3.org/1999/xhtml", "iframe")
    frame.height = 300;
    frame.src = "about:memory?file=" + makePathname(aFilename);
    document.documentElement.appendChild(frame);
    frame.focus();

    // Initialize the clipboard contents.
    SpecialPowers.clipboardCopyString("initial clipboard value");

    let numFailures = 0, maxFailures = 30;

    // Because the file load is async, we don't know when it will finish and
    // the output will show up.  So we poll.
    function copyPasteAndCheck() {
      // Copy and paste frame contents, and filter out non-deterministic
      // differences.
      synthesizeKey("A", {accelKey: true});
      synthesizeKey("C", {accelKey: true});
      let actual = SpecialPowers.getClipboardData("text/unicode");
      actual = actual.replace(/\(pid \d+\)/, "(pid NNN)");

      if (actual.trim() === aExpected.trim()) {
        SimpleTest.ok(true, "Clipboard has the expected contents");
        aNext();
      } else {
        numFailures++;
        if (numFailures === maxFailures) {
          ok(false, "pasted text doesn't match");
          dump("******EXPECTED******\n");
          dump(aExpected);
          dump("*******ACTUAL*******\n");
          dump(actual);
          dump("********************\n");
          SimpleTest.finish();
        } else {
          setTimeout(copyPasteAndCheck, 100);
        }
      }
    }
    copyPasteAndCheck();
  }

  // Returns a function that chains together multiple test() calls.
  function chain(aFrameIds) {
    let x = aFrameIds.shift();
    if (x) {
      return function() { test(x.filename, x.expected, chain(aFrameIds)); }
    } else {
      return function() { SimpleTest.finish(); };
    }
  }

  // This is pretty simple output, but that's ok;  this file is about testing
  // the loading of data from file.  If we got this far, we're doing fine.
  let expectedGood =
"\
Explicit-only process\n\
\n\
WARNING: the 'heap-allocated' memory reporter does not work for this platform and/or configuration. This means that 'heap-unclassified' is not shown and the 'explicit' tree shows less memory than it should.\n\
Explicit Allocations\n\
\n\
0.10 MB (100.0%) -- explicit\n\
└──0.10 MB (100.0%) ── a/b\n\
\n\
Other Measurements\n\
\n\
End of Explicit-only process\n\
Heap-unclassified process\n\
Explicit Allocations\n\
\n\
250.00 MB (100.0%) -- explicit\n\
├──200.00 MB (80.00%) ── heap-unclassified\n\
└───50.00 MB (20.00%) ── a/b\n\
\n\
Other Measurements\n\
\n\
250.00 MB ── heap-allocated\n\
\n\
End of Heap-unclassified process\n\
Main Process (pid NNN)\n\
Explicit Allocations\n\
\n\
250.00 MB (100.0%) -- explicit\n\
├──200.00 MB (80.00%) ── heap-unclassified\n\
└───50.00 MB (20.00%) ── a/b\n\
\n\
Other Measurements\n\
\n\
0.00 MB (100.0%) -- compartments\n\
└──0.00 MB (100.0%) ── system/a\n\
\n\
0.00 MB (100.0%) -- ghost-windows\n\
└──0.00 MB (100.0%) ── a\n\
\n\
0.30 MB (100.0%) -- other\n\
├──0.20 MB (66.67%) ── a\n\
└──0.10 MB (33.33%) ── b\n\
\n\
0.00 MB (100.0%) -- pss\n\
└──0.00 MB (100.0%) ── a\n\
\n\
0.00 MB (100.0%) -- rss\n\
└──0.00 MB (100.0%) ── a\n\
\n\
0.00 MB (100.0%) -- size\n\
└──0.00 MB (100.0%) ── a\n\
\n\
0.00 MB (100.0%) -- swap\n\
└──0.00 MB (100.0%) ── a\n\
\n\
250.00 MB ── heap-allocated\n\
\n\
End of Main Process (pid NNN)\n\
Other-only process\n\
Other Measurements\n\
\n\
0.19 MB (100.0%) -- a\n\
├──0.10 MB (50.00%) ── b\n\
└──0.10 MB (50.00%) ── c\n\
\n\
0.48 MB ── heap-allocated\n\
\n\
End of Other-only process\n\
";

  // This is the output for a malformed data file.
  let expectedBad =
"\
Error: Invalid memory report(s): missing 'hasMozMallocUsableSize' property";

  let frames = [
    // This loads a pre-existing file that is valid.
    { filename: "memory-reports-good.json", expected: expectedGood },

    // This loads a pre-existing file that is valid.
    { filename: "memory-reports-bad.json", expected: expectedBad }
  ];

  SimpleTest.waitForFocus(chain(frames));

  SimpleTest.waitForExplicitFinish();
  ]]>
  </script>
</window>