summaryrefslogtreecommitdiffstats
path: root/devtools/client/memory/test/xpcshell/test_action_diffing_03.js
blob: 5ae90dadb354b45883fbe76a5d77088f716a48db (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test selecting snapshots for diffing.

const {
  diffingState,
  snapshotState,
  viewState,
} = require("resource://devtools/client/memory/constants.js");
const {
  toggleDiffing,
  selectSnapshotForDiffing,
} = require("resource://devtools/client/memory/actions/diffing.js");
const {
  takeSnapshot,
} = require("resource://devtools/client/memory/actions/snapshot.js");
const {
  changeView,
} = require("resource://devtools/client/memory/actions/view.js");

// We test that you (1) cannot select a snapshot that is not in a diffable
// state, and (2) cannot select more than 2 snapshots for diffing. Both attempts
// trigger assertion failures.
EXPECTED_DTU_ASSERT_FAILURE_COUNT = 2;

add_task(async function() {
  const front = new StubbedMemoryFront();
  const heapWorker = new HeapAnalysesClient();
  await front.attach();
  const store = Store();
  const { getState, dispatch } = store;

  dispatch(changeView(viewState.CENSUS));
  equal(getState().diffing, null, "not diffing by default");

  dispatch(takeSnapshot(front, heapWorker));
  dispatch(takeSnapshot(front, heapWorker));
  dispatch(takeSnapshot(front, heapWorker));

  await waitUntilSnapshotState(store, [
    snapshotState.SAVED,
    snapshotState.SAVED,
    snapshotState.SAVED,
  ]);
  dispatch(takeSnapshot(front));

  // Start diffing.
  dispatch(toggleDiffing());
  ok(getState().diffing, "now diffing after toggling");
  equal(getState().diffing.firstSnapshotId, null, "no first snapshot selected");
  equal(
    getState().diffing.secondSnapshotId,
    null,
    "no second snapshot selected"
  );
  equal(
    getState().diffing.state,
    diffingState.SELECTING,
    "should be in diffing state SELECTING"
  );

  // Can't select a snapshot that is not in a diffable state.
  equal(
    getState().snapshots[3].state,
    snapshotState.SAVING,
    "the last snapshot is still in the process of being saved"
  );
  dumpn("Expecting exception:");
  let threw = false;
  try {
    dispatch(selectSnapshotForDiffing(getState().snapshots[3]));
  } catch (error) {
    threw = true;
  }
  ok(
    threw,
    "Should not be able to select snapshots that aren't ready for diffing"
  );

  // Select first snapshot for diffing.
  dispatch(selectSnapshotForDiffing(getState().snapshots[0]));
  ok(getState().diffing, "now diffing after toggling");
  equal(
    getState().diffing.firstSnapshotId,
    getState().snapshots[0].id,
    "first snapshot selected"
  );
  equal(
    getState().diffing.secondSnapshotId,
    null,
    "no second snapshot selected"
  );
  equal(
    getState().diffing.state,
    diffingState.SELECTING,
    "should still be in diffing state SELECTING"
  );

  // Can't diff first snapshot with itself; this is a noop.
  dispatch(selectSnapshotForDiffing(getState().snapshots[0]));
  ok(getState().diffing, "still diffing");
  equal(
    getState().diffing.firstSnapshotId,
    getState().snapshots[0].id,
    "first snapshot still selected"
  );
  equal(
    getState().diffing.secondSnapshotId,
    null,
    "still no second snapshot selected"
  );
  equal(
    getState().diffing.state,
    diffingState.SELECTING,
    "should still be in diffing state SELECTING"
  );

  // Select second snapshot for diffing.
  dispatch(selectSnapshotForDiffing(getState().snapshots[1]));
  ok(getState().diffing, "still diffing");
  equal(
    getState().diffing.firstSnapshotId,
    getState().snapshots[0].id,
    "first snapshot still selected"
  );
  equal(
    getState().diffing.secondSnapshotId,
    getState().snapshots[1].id,
    "second snapshot selected"
  );

  // Can't select more than two snapshots for diffing.
  dumpn("Expecting exception:");
  threw = false;
  try {
    dispatch(selectSnapshotForDiffing(getState().snapshots[2]));
  } catch (error) {
    threw = true;
  }
  ok(threw, "Can't select more than two snapshots for diffing");

  heapWorker.destroy();
  await front.detach();
});