Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / devtools / client / memory / test / chrome / head.js
blob4061ffa26441d1563f2701497ba5d13cf55852c1
1 /* Any copyright is dedicated to the Public Domain.
2    http://creativecommons.org/publicdomain/zero/1.0/ */
4 "use strict";
6 var { BrowserLoader } = ChromeUtils.importESModule(
7   "resource://devtools/shared/loader/browser-loader.sys.mjs"
8 );
9 var { require } = BrowserLoader({
10   baseURI: "resource://devtools/client/memory/",
11   window,
12 });
13 var { Assert } = ChromeUtils.importESModule(
14   "resource://testing-common/Assert.sys.mjs"
17 var EXPECTED_DTU_ASSERT_FAILURE_COUNT = 0;
19 SimpleTest.registerCleanupFunction(function () {
20   if (
21     DevToolsUtils.assertionFailureCount !== EXPECTED_DTU_ASSERT_FAILURE_COUNT
22   ) {
23     ok(
24       false,
25       "Should have had the expected number of DevToolsUtils.assert() failures." +
26         "Expected " +
27         EXPECTED_DTU_ASSERT_FAILURE_COUNT +
28         ", got " +
29         DevToolsUtils.assertionFailureCount
30     );
31   }
32 });
34 var DevToolsUtils = require("resource://devtools/shared/DevToolsUtils.js");
35 var { immutableUpdate } = DevToolsUtils;
37 var constants = require("resource://devtools/client/memory/constants.js");
38 var {
39   censusDisplays,
40   diffingState,
41   labelDisplays,
42   dominatorTreeState,
43   snapshotState,
44   viewState,
45   censusState,
46 } = constants;
48 const { L10N } = require("resource://devtools/client/memory/utils.js");
50 var models = require("resource://devtools/client/memory/models.js");
52 var Immutable = require("resource://devtools/client/shared/vendor/immutable.js");
53 var React = require("resource://devtools/client/shared/vendor/react.js");
54 const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
55 var ReactDOM = require("resource://devtools/client/shared/vendor/react-dom.js");
56 var { createFactory } = React;
57 var Heap = createFactory(
58   require("resource://devtools/client/memory/components/Heap.js")
60 var CensusTreeItem = createFactory(
61   require("resource://devtools/client/memory/components/CensusTreeItem.js")
63 var DominatorTreeComponent = createFactory(
64   require("resource://devtools/client/memory/components/DominatorTree.js")
66 var DominatorTreeItem = createFactory(
67   require("resource://devtools/client/memory/components/DominatorTreeItem.js")
69 var ShortestPaths = createFactory(
70   require("resource://devtools/client/memory/components/ShortestPaths.js")
72 var TreeMap = createFactory(
73   require("resource://devtools/client/memory/components/TreeMap.js")
75 var SnapshotListItem = createFactory(
76   require("resource://devtools/client/memory/components/SnapshotListItem.js")
78 var List = createFactory(
79   require("resource://devtools/client/memory/components/List.js")
81 var Toolbar = createFactory(
82   require("resource://devtools/client/memory/components/Toolbar.js")
85 // All tests are asynchronous.
86 SimpleTest.waitForExplicitFinish();
88 var noop = () => {};
90 var TEST_CENSUS_TREE_ITEM_PROPS = Object.freeze({
91   item: Object.freeze({
92     bytes: 10,
93     count: 1,
94     totalBytes: 10,
95     totalCount: 1,
96     name: "foo",
97     children: [
98       Object.freeze({
99         bytes: 10,
100         count: 1,
101         totalBytes: 10,
102         totalCount: 1,
103         name: "bar",
104       }),
105     ],
106   }),
107   depth: 0,
108   arrow: ">",
109   focused: true,
110   getPercentBytes: () => 50,
111   getPercentCount: () => 50,
112   showSign: false,
113   onViewSourceInDebugger: noop,
114   inverted: false,
117 // Counter for mock DominatorTreeNode ids.
118 var TEST_NODE_ID_COUNTER = 0;
121  * Create a mock DominatorTreeNode for testing, with sane defaults. Override any
122  * property by providing it on `opts`. Optionally pass child nodes as well.
124  * @param {Object} opts
125  * @param {Array<DominatorTreeNode>?} children
127  * @returns {DominatorTreeNode}
128  */
129 function makeTestDominatorTreeNode(opts, children) {
130   const nodeId = TEST_NODE_ID_COUNTER++;
132   const node = Object.assign(
133     {
134       nodeId,
135       label: ["other", "SomeType"],
136       shallowSize: 1,
137       retainedSize: (children || []).reduce(
138         (size, c) => size + c.retainedSize,
139         1
140       ),
141       parentId: undefined,
142       children,
143       moreChildrenAvailable: true,
144     },
145     opts
146   );
148   if (children && children.length) {
149     children.map(c => {
150       c.parentId = node.nodeId;
151     });
152   }
154   return node;
157 var TEST_DOMINATOR_TREE = Object.freeze({
158   dominatorTreeId: 666,
159   root: (function makeTree(depth = 0) {
160     let children;
161     if (depth <= 3) {
162       children = [
163         makeTree(depth + 1),
164         makeTree(depth + 1),
165         makeTree(depth + 1),
166       ];
167     }
168     return makeTestDominatorTreeNode({}, children);
169   })(),
170   expanded: new Set(),
171   focused: null,
172   error: null,
173   display: labelDisplays.coarseType,
174   activeFetchRequestCount: null,
175   state: dominatorTreeState.LOADED,
178 var TEST_DOMINATOR_TREE_PROPS = Object.freeze({
179   dominatorTree: TEST_DOMINATOR_TREE,
180   onLoadMoreSiblings: noop,
181   onViewSourceInDebugger: noop,
182   onExpand: noop,
183   onCollapse: noop,
186 var TEST_SHORTEST_PATHS_PROPS = Object.freeze({
187   graph: Object.freeze({
188     nodes: [
189       { id: 1, label: ["other", "SomeType"] },
190       { id: 2, label: ["other", "SomeType"] },
191       { id: 3, label: ["other", "SomeType"] },
192     ],
193     edges: [
194       { from: 1, to: 2, name: "1->2" },
195       { from: 1, to: 3, name: "1->3" },
196       { from: 2, to: 3, name: "2->3" },
197     ],
198   }),
201 var TEST_SNAPSHOT = Object.freeze({
202   id: 1337,
203   selected: true,
204   path: "/fake/path/to/snapshot",
205   census: Object.freeze({
206     report: Object.freeze({
207       objects: Object.freeze({ count: 4, bytes: 400 }),
208       scripts: Object.freeze({ count: 3, bytes: 300 }),
209       strings: Object.freeze({ count: 2, bytes: 200 }),
210       other: Object.freeze({ count: 1, bytes: 100 }),
211     }),
212     display: Object.freeze({
213       displayName: "Test Display",
214       tooltip: "Test display tooltup",
215       inverted: false,
216       breakdown: Object.freeze({
217         by: "coarseType",
218         objects: Object.freeze({ by: "count", count: true, bytes: true }),
219         scripts: Object.freeze({ by: "count", count: true, bytes: true }),
220         strings: Object.freeze({ by: "count", count: true, bytes: true }),
221         other: Object.freeze({ by: "count", count: true, bytes: true }),
222       }),
223     }),
224     state: censusState.SAVED,
225     inverted: false,
226     filter: null,
227     expanded: new Set(),
228     focused: null,
229     parentMap: Object.freeze(Object.create(null)),
230   }),
231   dominatorTree: TEST_DOMINATOR_TREE,
232   error: null,
233   imported: false,
234   creationTime: 0,
235   state: snapshotState.READ,
238 var TEST_HEAP_PROPS = Object.freeze({
239   onSnapshotClick: noop,
240   onLoadMoreSiblings: noop,
241   onCensusExpand: noop,
242   onCensusCollapse: noop,
243   onDominatorTreeExpand: noop,
244   onDominatorTreeCollapse: noop,
245   onCensusFocus: noop,
246   onDominatorTreeFocus: noop,
247   onViewSourceInDebugger: noop,
248   diffing: null,
249   view: { state: viewState.CENSUS },
250   snapshot: TEST_SNAPSHOT,
251   sizes: Object.freeze({ shortestPathsSize: 0.5 }),
252   onShortestPathsResize: noop,
255 var TEST_TOOLBAR_PROPS = Object.freeze({
256   censusDisplays: [
257     censusDisplays.coarseType,
258     censusDisplays.allocationStack,
259     censusDisplays.invertedAllocationStack,
260   ],
261   censusDisplay: censusDisplays.coarseType,
262   onTakeSnapshotClick: noop,
263   onImportClick: noop,
264   onCensusDisplayChange: noop,
265   onToggleRecordAllocationStacks: noop,
266   allocations: models.allocations,
267   onToggleInverted: noop,
268   inverted: false,
269   filterString: null,
270   setFilterString: noop,
271   diffing: null,
272   onToggleDiffing: noop,
273   view: { state: viewState.CENSUS },
274   onViewChange: noop,
275   labelDisplays: [labelDisplays.coarseType, labelDisplays.allocationStack],
276   labelDisplay: labelDisplays.coarseType,
277   onLabelDisplayChange: noop,
278   snapshots: [],
281 function makeTestCensusNode() {
282   return {
283     name: "Function",
284     bytes: 100,
285     totalBytes: 100,
286     count: 100,
287     totalCount: 100,
288     children: [],
289   };
292 var TEST_TREE_MAP_PROPS = Object.freeze({
293   treeMap: Object.freeze({
294     report: {
295       name: null,
296       bytes: 0,
297       totalBytes: 400,
298       count: 0,
299       totalCount: 400,
300       children: [
301         {
302           name: "objects",
303           bytes: 0,
304           totalBytes: 200,
305           count: 0,
306           totalCount: 200,
307           children: [makeTestCensusNode(), makeTestCensusNode()],
308         },
309         {
310           name: "other",
311           bytes: 0,
312           totalBytes: 200,
313           count: 0,
314           totalCount: 200,
315           children: [makeTestCensusNode(), makeTestCensusNode()],
316         },
317       ],
318     },
319   }),
322 var TEST_SNAPSHOT_LIST_ITEM_PROPS = Object.freeze({
323   onClick: noop,
324   onSave: noop,
325   onDelete: noop,
326   item: TEST_SNAPSHOT,
327   index: 1234,
330 function onNextAnimationFrame(fn) {
331   return () => requestAnimationFrame(() => requestAnimationFrame(fn));
335  * Render the provided ReactElement in the provided HTML container.
336  * Returns a Promise that will resolve the rendered element as a React
337  * component.
338  */
339 function renderComponent(element, container) {
340   return new Promise(resolve => {
341     const component = ReactDOM.render(
342       element,
343       container,
344       onNextAnimationFrame(() => {
345         dumpn("Rendered = " + container.innerHTML);
346         resolve(component);
347       })
348     );
349   });
352 function dumpn(msg) {
353   dump(`MEMORY-TEST: ${msg}\n`);