test_action-import-snapshot-dominator-tree.js (3034B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 /** 7 * Tests `importSnapshotAndCensus()` when importing snapshots from the dominator 8 * tree view. The snapshot is expected to be loaded and its dominator tree 9 * should be computed. 10 */ 11 12 const { 13 snapshotState, 14 dominatorTreeState, 15 viewState, 16 treeMapState, 17 } = require("resource://devtools/client/memory/constants.js"); 18 const { 19 importSnapshotAndCensus, 20 } = require("resource://devtools/client/memory/actions/io.js"); 21 const { 22 changeViewAndRefresh, 23 } = require("resource://devtools/client/memory/actions/view.js"); 24 25 add_task(async function () { 26 const front = new StubbedMemoryFront(); 27 const heapWorker = new HeapAnalysesClient(); 28 await front.attach(); 29 const store = Store(); 30 const { subscribe, dispatch, getState } = store; 31 32 dispatch(changeViewAndRefresh(viewState.DOMINATOR_TREE, heapWorker)); 33 equal( 34 getState().view.state, 35 viewState.DOMINATOR_TREE, 36 "We should now be in the DOMINATOR_TREE view" 37 ); 38 39 let i = 0; 40 const expected = [ 41 "IMPORTING", 42 "READING", 43 "READ", 44 "treeMap:SAVING", 45 "treeMap:SAVED", 46 "dominatorTree:COMPUTING", 47 "dominatorTree:FETCHING", 48 "dominatorTree:LOADED", 49 ]; 50 const expectStates = () => { 51 const snapshot = getState().snapshots[0]; 52 if (snapshot && hasExpectedState(snapshot, expected[i])) { 53 ok(true, `Found expected state ${expected[i]}`); 54 i++; 55 } 56 }; 57 58 const unsubscribe = subscribe(expectStates); 59 const snapshotPath = await front.saveHeapSnapshot(); 60 dispatch(importSnapshotAndCensus(heapWorker, snapshotPath)); 61 62 await waitUntilState(store, () => i === expected.length); 63 unsubscribe(); 64 equal( 65 i, 66 expected.length, 67 "importSnapshotAndCensus() produces the correct " + 68 "sequence of states in a snapshot" 69 ); 70 equal( 71 getState().snapshots[0].dominatorTree.state, 72 dominatorTreeState.LOADED, 73 "imported snapshot's dominator tree is in LOADED state" 74 ); 75 ok(getState().snapshots[0].selected, "imported snapshot is selected"); 76 }); 77 78 /** 79 * Check that the provided snapshot is in the expected state. The expected state 80 * is a snapshotState by default. If the expected state is prefixed by 81 * dominatorTree, a dominatorTree is expected on the provided snapshot, in the 82 * corresponding state from dominatorTreeState. 83 */ 84 function hasExpectedState(snapshot, expectedState) { 85 const isDominatorState = expectedState.indexOf("dominatorTree:") === 0; 86 if (isDominatorState) { 87 const state = 88 dominatorTreeState[expectedState.replace("dominatorTree:", "")]; 89 return snapshot.dominatorTree && snapshot.dominatorTree.state === state; 90 } 91 92 const isTreeMapState = expectedState.indexOf("treeMap:") === 0; 93 if (isTreeMapState) { 94 const state = treeMapState[expectedState.replace("treeMap:", "")]; 95 return snapshot.treeMap && snapshot.treeMap.state === state; 96 } 97 98 const state = snapshotState[expectedState]; 99 return snapshot.state === state; 100 }