-
Notifications
You must be signed in to change notification settings - Fork 481
Expand file tree
/
Copy pathTrackThread.test.tsx
More file actions
322 lines (281 loc) · 10.1 KB
/
Copy pathTrackThread.test.tsx
File metadata and controls
322 lines (281 loc) · 10.1 KB
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/. */
import type { Profile, FileIoPayload } from 'firefox-profiler/types';
import { Provider } from 'react-redux';
import { oneLine } from 'common-tags';
import { render, act } from 'firefox-profiler/test/fixtures/testing-library';
import {
changeTimelineType,
changeInvertCallstack,
changeSelectedCallNode,
} from '../../actions/profile-view';
import { TimelineTrackThread } from '../../components/timeline/TrackThread';
import { getPreviewSelection } from '../../selectors/profile';
import { selectedThreadSelectors } from '../../selectors/per-thread';
import { ensureExists } from '../../utils/types';
import {
autoMockCanvasContext,
flushDrawLog,
} from '../fixtures/mocks/canvas-context';
import { mockRaf } from '../fixtures/mocks/request-animation-frame';
import { storeWithProfile } from '../fixtures/stores';
import type { FakeMouseEventInit } from '../fixtures/utils';
import {
addRootOverlayElement,
removeRootOverlayElement,
fireFullClick,
} from '../fixtures/utils';
import type { TestDefinedMarker } from '../fixtures/profiles/processed-profile';
import {
getProfileFromTextSamples,
getProfileWithMarkers,
} from '../fixtures/profiles/processed-profile';
import { autoMockElementSize } from '../fixtures/mocks/element-size';
import { autoMockIntersectionObserver } from '../fixtures/mocks/intersection-observer';
// The graph is 400 pixels wide based on the element size mock. Each stack is
// 100 pixels wide. Use the value 50 to click in the middle of this stack, and
// incrementing by steps of 100 pixels to get to the next stack.
const GRAPH_WIDTH = 400;
const GRAPH_HEIGHT = 50;
/**
* This test is asserting behavior more for the ThreadStackGraph component. The
* ThreadActivityGraph component was added as a new default. Currently this test
* only checks the older behavior.
*/
describe('timeline/TrackThread', function () {
beforeEach(addRootOverlayElement);
afterEach(removeRootOverlayElement);
autoMockCanvasContext();
autoMockElementSize({ width: GRAPH_WIDTH, height: GRAPH_HEIGHT });
autoMockIntersectionObserver();
function getSamplesProfile() {
return getProfileFromTextSamples(`
a d g j
b e h k
c f i l
`).profile;
}
function getMarkersProfile(
testMarkers: TestDefinedMarker[] = [
['Marker A', 0],
['Marker B', 1],
['Marker C', 2],
['Marker D', 3],
]
) {
const profile = getProfileWithMarkers(testMarkers);
const [thread] = profile.threads;
thread.name = 'GeckoMain';
thread.isMainThread = true;
thread.processType = 'default';
return profile;
}
function setup(profile: Profile) {
const store = storeWithProfile(profile);
const { getState, dispatch } = store;
const threadIndex = 0;
const flushRafCalls = mockRaf();
type Coordinate = { pageX: number; pageY: number };
// Look through the draw log and find the center of a specific fillRect
// call. This is a good way to know where the canvas drew something.
function getFillRectCenterByIndex(log: any[], index: number): Coordinate {
type FillRectCall = [string, number, number, number, number];
const calls: FillRectCall[] = log.filter(
(call) => call[0] === 'fillRect'
);
const call = calls[index];
if (!call) {
console.error(log);
throw new Error(`Could not find a fillRect call at ${index}.`);
}
const [, x, y, w, h] = call;
return { pageX: x + w * 0.5, pageY: y + h * 0.5 };
}
// Note: These tests were first written with the timeline using the ThreadStackGraph.
// This is not the default view, so dispatch an action to change to the older default
// view.
store.dispatch(changeTimelineType('stack'));
const renderResult = render(
<Provider store={store}>
<TimelineTrackThread
threadsKey={threadIndex}
trackType="expanded"
trackName="Test Track"
/>
</Provider>
);
const { container } = renderResult;
// WithSize uses requestAnimationFrame
flushRafCalls();
const stackGraphCanvas = () =>
ensureExists(
container.querySelector('.threadStackGraphCanvas'),
`Couldn't find the stack graph canvas, with selector .threadStackGraphCanvas`
) as HTMLElement;
const markerCanvas = () =>
ensureExists(
container.querySelector(oneLine`
.timelineMarkersGeckoMain
.timelineMarkersCanvas
`),
`Couldn't find the marker canvas`
) as HTMLElement;
return {
...renderResult,
dispatch,
getState,
profile,
thread: profile.threads[0],
shared: profile.shared,
store,
threadIndex,
stackGraphCanvas,
markerCanvas,
getFillRectCenterByIndex,
};
}
it('matches the snapshot for the component', () => {
const { container } = setup(getSamplesProfile());
expect(container.firstChild).toMatchSnapshot();
});
it('matches the 2d canvas draw snapshot', () => {
setup(getSamplesProfile());
expect(flushDrawLog()).toMatchSnapshot();
});
it('can click a stack in the stack graph in normal call trees', function () {
const { getState, stackGraphCanvas, profile, getFillRectCenterByIndex } =
setup(getSamplesProfile());
const log = flushDrawLog();
// Provide a quick helper for nicely asserting the call node path.
const getCallNodePath = () =>
selectedThreadSelectors
.getSelectedCallNodePath(getState())
.map(
(funcIndex) =>
profile.shared.stringArray[profile.shared.funcTable.name[funcIndex]]
);
fireFullClick(stackGraphCanvas(), getFillRectCenterByIndex(log, 0));
expect(getCallNodePath()).toEqual(['a', 'b', 'c']);
fireFullClick(stackGraphCanvas(), getFillRectCenterByIndex(log, 1));
expect(getCallNodePath()).toEqual(['d', 'e', 'f']);
fireFullClick(stackGraphCanvas(), getFillRectCenterByIndex(log, 2));
expect(getCallNodePath()).toEqual(['g', 'h', 'i']);
fireFullClick(stackGraphCanvas(), getFillRectCenterByIndex(log, 3));
expect(getCallNodePath()).toEqual(['j', 'k', 'l']);
});
it('can click a stack in the stack graph in inverted call trees', function () {
const {
dispatch,
getState,
stackGraphCanvas,
profile,
shared,
getFillRectCenterByIndex,
} = setup(getSamplesProfile());
// Provide a quick helper for nicely asserting the call node path.
const getCallNodePath = () =>
selectedThreadSelectors
.getSelectedCallNodePath(getState())
.map(
(funcIndex) =>
profile.shared.stringArray[shared.funcTable.name[funcIndex]]
);
function changeInvertCallstackAndGetDrawLog(value: boolean) {
// We don't want a selected stack graph to change fillRect ordering.
act(() => {
dispatch(changeSelectedCallNode(0, []));
});
flushDrawLog();
act(() => {
dispatch(changeInvertCallstack(value));
});
return flushDrawLog();
}
// Switch to "inverted" mode to test with this state
{
const log = changeInvertCallstackAndGetDrawLog(true);
fireFullClick(stackGraphCanvas(), getFillRectCenterByIndex(log, 0));
expect(getCallNodePath()).toEqual(['c']);
fireFullClick(stackGraphCanvas(), getFillRectCenterByIndex(log, 2));
expect(getCallNodePath()).toEqual(['i']);
}
{
// Switch back to "uninverted" mode
const log = changeInvertCallstackAndGetDrawLog(false);
fireFullClick(stackGraphCanvas(), getFillRectCenterByIndex(log, 0));
expect(getCallNodePath()).toEqual(['a', 'b', 'c']);
fireFullClick(stackGraphCanvas(), getFillRectCenterByIndex(log, 2));
expect(getCallNodePath()).toEqual(['g', 'h', 'i']);
}
});
it('can click a marker', function () {
const { getState, markerCanvas, getFillRectCenterByIndex } = setup(
getMarkersProfile([
['DOMEvent', 0, 4],
['DOMEvent', 4, 8],
])
);
const log = flushDrawLog();
function clickAndGetMarkerName(event: FakeMouseEventInit) {
fireFullClick(markerCanvas(), event);
return getPreviewSelection(getState());
}
// Currently markers are drawn with 3 fillRects, the middle of the three is the
// big interesting one. If this test breaks, likely the drawing strategy
// has changed.
const determineIndex = (i: number) => i * 3 + 1;
expect(
clickAndGetMarkerName(getFillRectCenterByIndex(log, determineIndex(0)))
).toMatchObject({
selectionStart: 0,
selectionEnd: 4,
});
expect(
clickAndGetMarkerName(getFillRectCenterByIndex(log, determineIndex(1)))
).toMatchObject({
selectionStart: 4,
selectionEnd: 8,
});
});
it('does not add disk io markers if none are present', function () {
const noMarkers: TestDefinedMarker[] = [];
const { queryByTestId } = setup(getMarkersProfile(noMarkers));
expect(queryByTestId('TimelineMarkersFileIo')).not.toBeInTheDocument();
});
it('adds file io markers if they are present', function () {
const fileIoMarker: TestDefinedMarker[] = [
[
'FileIO',
2,
3,
{
type: 'FileIO',
source: 'PoisionOIInterposer',
filename: '/foo/bar/',
operation: 'read/write',
} as FileIoPayload,
],
];
const { getByTestId } = setup(getMarkersProfile(fileIoMarker));
expect(getByTestId('TimelineMarkersFileIo')).toBeInTheDocument();
});
it('does not add off-thread file io markers even if they are present', function () {
const fileIoMarker: TestDefinedMarker[] = [
[
'FileIO',
2,
3,
{
type: 'FileIO',
source: 'PoisionOIInterposer',
filename: '/foo/bar/',
operation: 'read/write',
threadId: 123,
} as FileIoPayload,
],
];
const { queryByTestId } = setup(getMarkersProfile(fileIoMarker));
expect(queryByTestId('TimelineMarkersFileIo')).not.toBeInTheDocument();
});
});