forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransformShortcuts.test.js
322 lines (289 loc) · 10 KB
/
TransformShortcuts.test.js
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow
import * as React from 'react';
import { Provider } from 'react-redux';
import { render, act } from 'firefox-profiler/test/fixtures/testing-library';
import { getProfileFromTextSamples } from '../fixtures/profiles/processed-profile';
import { storeWithProfile } from '../fixtures/stores';
import {
changeSelectedCallNode,
changeRightClickedCallNode,
} from '../../actions/profile-view';
import { FlameGraph } from '../../components/flame-graph';
import { selectedThreadSelectors } from 'firefox-profiler/selectors';
import { ensureExists, objectEntries } from '../../utils/flow';
import { fireFullKeyPress } from '../fixtures/utils';
import { autoMockCanvasContext } from '../fixtures/mocks/canvas-context';
import { ProfileCallTreeView } from '../../components/calltree/ProfileCallTreeView';
import { StackChart } from 'firefox-profiler/components/stack-chart';
import type {
Transform,
CallNodePath,
IndexIntoFuncTable,
IndexIntoResourceTable,
IndexIntoCategoryList,
} from 'firefox-profiler/types';
type KeyPressOptions = { key: string, ... };
type TestSetup = {|
getTransform: () => null | Transform,
pressKey: (options: KeyPressOptions) => void,
expectedCallNodePath: CallNodePath,
// This should be expectedCallNodePath[expectedCallNodePath.length - 1], but this simplifies tests a bit.
expectedFuncIndex: IndexIntoFuncTable,
expectedResourceIndex: IndexIntoResourceTable,
expectedCategory: IndexIntoCategoryList,
|};
function testTransformKeyboardShortcuts(setup: () => TestSetup) {
it('handles focus subtree', () => {
const { pressKey, getTransform, expectedCallNodePath } = setup();
pressKey({ key: 'F' });
expect(getTransform()).toMatchObject({
type: 'focus-subtree',
callNodePath: expectedCallNodePath,
});
});
it('handles focus-function', () => {
const { pressKey, getTransform, expectedFuncIndex } = setup();
pressKey({ key: 'f' });
expect(getTransform()).toEqual({
type: 'focus-function',
funcIndex: expectedFuncIndex,
});
});
it('handles focus-category', () => {
const { pressKey, getTransform, expectedCategory } = setup();
pressKey({ key: 'g' });
expect(getTransform()).toEqual({
type: 'focus-category',
category: expectedCategory,
});
});
it('handles merge call node', () => {
const { pressKey, getTransform, expectedCallNodePath } = setup();
pressKey({ key: 'M' });
expect(getTransform()).toMatchObject({
type: 'merge-call-node',
callNodePath: expectedCallNodePath,
});
});
it('handles merge function', () => {
const { pressKey, getTransform, expectedFuncIndex } = setup();
pressKey({ key: 'm' });
const transform = getTransform();
expect(transform).toEqual({
type: 'merge-function',
funcIndex: expectedFuncIndex,
});
});
it('handles drop function', () => {
const { pressKey, getTransform, expectedFuncIndex } = setup();
pressKey({ key: 'd' });
expect(getTransform()).toEqual({
type: 'drop-function',
funcIndex: expectedFuncIndex,
});
});
it('handles collapse resource', () => {
const { pressKey, getTransform, expectedResourceIndex } = setup();
pressKey({ key: 'C' });
expect(getTransform()).toMatchObject({
type: 'collapse-resource',
resourceIndex: expectedResourceIndex,
});
});
it('handles collapse recursion', () => {
const { pressKey, getTransform, expectedFuncIndex } = setup();
pressKey({ key: 'r' });
expect(getTransform()).toMatchObject({
type: 'collapse-recursion',
funcIndex: expectedFuncIndex,
});
});
it('handles collapse direct recursion', () => {
const { pressKey, getTransform, expectedFuncIndex } = setup();
pressKey({ key: 'R' });
expect(getTransform()).toMatchObject({
type: 'collapse-direct-recursion',
funcIndex: expectedFuncIndex,
});
});
it('handles collapse function subtree', () => {
const { pressKey, getTransform, expectedFuncIndex } = setup();
pressKey({ key: 'c' });
expect(getTransform()).toEqual({
type: 'collapse-function-subtree',
funcIndex: expectedFuncIndex,
});
});
it('ignores shortcuts with modifiers', () => {
const { pressKey, getTransform } = setup();
pressKey({ key: 'c', ctrlKey: true });
pressKey({ key: 'c', metaKey: true });
expect(getTransform()).toEqual(null);
});
}
// This is a generic setup that's used in all of our testcases.
function setupStore(childrenToRender) {
const {
profile,
funcNamesDictPerThread: [funcNames],
} = getProfileFromTextSamples(`
A A A
B[lib:XUL] B[lib:XUL] B[lib:XUL]
B[lib:XUL] B[lib:XUL] B[lib:XUL]
B[lib:XUL] B[lib:XUL] B[lib:XUL]
C C H
D F I
E E
`);
const store = storeWithProfile(profile);
const { getState } = store;
render(<Provider store={store}>{childrenToRender}</Provider>);
return {
store,
funcNames,
getTransform: () => {
const stack = selectedThreadSelectors.getTransformStack(getState());
switch (stack.length) {
case 0:
return null;
case 1:
return stack[0];
default:
throw new Error('This test assumes there is only one transform.');
}
},
};
}
// This returns a function that makes it easy to simulate a keypress on a
// specific element identified by a class name.
const pressKeyBuilder = (className) => (options: KeyPressOptions) => {
const div = ensureExists(
document.querySelector('.' + className),
`Couldn't find the content div with selector .${className}`
);
fireFullKeyPress(div, options);
};
/* eslint-disable jest/no-standalone-expect */
// Disable the jest/no-standalone-expect rule because eslint doesn't know that
// these expectations will run in a test block later.
// These actions will be used to generate use cases for each of the supported panels.
const actions = {
'a selected node': ({ dispatch, getState }, { A, B }) => {
act(() => {
dispatch(changeSelectedCallNode(0, [A, B]));
});
// We also check expectations after these dispatch, because if the node path
// is invalid, we can have null values, which would give a useless test.
expect(
selectedThreadSelectors.getSelectedCallNodeIndex(getState())
).not.toBeNull();
expect(
selectedThreadSelectors.getRightClickedCallNodeIndex(getState())
).toBeNull();
},
'a right clicked node': ({ dispatch, getState }, { A, B }) => {
act(() => {
dispatch(changeSelectedCallNode(0, []));
});
act(() => {
dispatch(changeRightClickedCallNode(0, [A, B]));
});
// We also check expectations after these dispatch, because if the node path
// is invalid, we can have null values, which would give a useless test.
expect(
selectedThreadSelectors.getSelectedCallNodeIndex(getState())
).toBeNull();
expect(
selectedThreadSelectors.getRightClickedCallNodeIndex(getState())
).not.toBeNull();
},
'both a selected and a right clicked node': (
{ dispatch, getState },
{ A, B, H }
) => {
act(() => {
dispatch(changeSelectedCallNode(0, [A, B, B, B, H]));
});
act(() => {
dispatch(changeRightClickedCallNode(0, [A, B]));
});
// We also check expectations after these dispatch, because if the node path
// is invalid, we can have null values, which would give a useless test.
expect(
selectedThreadSelectors.getSelectedCallNodeIndex(getState())
).not.toBeNull();
expect(
selectedThreadSelectors.getRightClickedCallNodeIndex(getState())
).not.toBeNull();
},
};
/* eslint-enable jest/no-standalone-expect */
autoMockCanvasContext();
describe('flame graph transform shortcuts', () => {
for (const [name, action] of objectEntries(actions)) {
describe(`with ${name}`, () => {
testTransformKeyboardShortcuts(() => {
const { store, funcNames, getTransform } = setupStore(<FlameGraph />);
const { A, B } = funcNames;
action(store, funcNames);
return {
getTransform,
// take either a key as a string, or a full event if we need more
// information like modifier keys.
pressKey: pressKeyBuilder('flameGraphContent'),
expectedCallNodePath: [A, B],
expectedFuncIndex: B,
expectedResourceIndex: 0,
expectedCategory: 0,
};
});
});
}
});
describe('CallTree transform shortcuts', () => {
for (const [name, action] of objectEntries(actions)) {
describe(`with ${name}`, () => {
testTransformKeyboardShortcuts(() => {
const { store, funcNames, getTransform } = setupStore(
<ProfileCallTreeView />
);
const { A, B } = funcNames;
action(store, funcNames);
return {
getTransform,
// take either a key as a string, or a full event if we need more
// information like modifier keys.
pressKey: pressKeyBuilder('treeViewBody'),
expectedCallNodePath: [A, B],
expectedFuncIndex: B,
expectedResourceIndex: 0,
expectedCategory: 0,
};
});
});
}
});
describe('stack chart transform shortcuts', () => {
for (const [name, action] of objectEntries(actions)) {
describe(`with ${name}`, () => {
testTransformKeyboardShortcuts(() => {
const { store, funcNames, getTransform } = setupStore(<StackChart />);
const { A, B } = funcNames;
action(store, funcNames);
return {
getTransform,
// take either a key as a string, or a full event if we need more
// information like modifier keys.
pressKey: pressKeyBuilder('stackChartContent'),
expectedCallNodePath: [A, B],
expectedFuncIndex: B,
expectedResourceIndex: 0,
expectedCategory: 0,
};
});
});
}
});