-
Notifications
You must be signed in to change notification settings - Fork 481
Expand file tree
/
Copy pathSampleGraph.test.tsx
More file actions
397 lines (340 loc) · 13.1 KB
/
Copy pathSampleGraph.test.tsx
File metadata and controls
397 lines (340 loc) · 13.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* 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 { Provider } from 'react-redux';
import { render } from 'firefox-profiler/test/fixtures/testing-library';
import { fireEvent } from '@testing-library/react';
import { selectedThreadSelectors } from 'firefox-profiler/selectors/per-thread';
import { ensureExists } from 'firefox-profiler/utils/types';
import { TimelineTrackThread } from 'firefox-profiler/components/timeline/TrackThread';
import type { DrawOperation } from '../fixtures/mocks/canvas-context';
import {
autoMockCanvasContext,
flushDrawLog,
} from '../fixtures/mocks/canvas-context';
import { mockRaf } from '../fixtures/mocks/request-animation-frame';
import { storeWithProfile } from '../fixtures/stores';
import {
fireFullClick,
getMouseEvent,
addRootOverlayElement,
removeRootOverlayElement,
} from '../fixtures/utils';
import { getProfileFromTextSamples } from '../fixtures/profiles/processed-profile';
import {
autoMockElementSize,
setMockedElementSize,
} from '../fixtures/mocks/element-size';
import {
autoMockIntersectionObserver,
triggerIntersectionObservers,
} from '../fixtures/mocks/intersection-observer';
import { triggerResizeObservers } from '../fixtures/mocks/resize-observer';
import type {
Profile,
IndexIntoSamplesTable,
CssPixels,
} from 'firefox-profiler/types';
// Mocking the ActivityGraph because we don't want to see the content/draw log
// of it in these tests. It has its own tests.
jest.mock('firefox-profiler/components/shared/thread/ActivityGraph', () => ({
ThreadActivityGraph: 'thread-activity-graph',
}));
// The following constants determine the size of the drawn graph.
const SAMPLE_COUNT = 8;
const PIXELS_PER_SAMPLE = 10;
const GRAPH_WIDTH = PIXELS_PER_SAMPLE * SAMPLE_COUNT;
const GRAPH_HEIGHT = 10;
function getSamplesPixelPosition(
sampleIndex: IndexIntoSamplesTable
): CssPixels {
// Compute the pixel position of the exact sample.
return sampleIndex * PIXELS_PER_SAMPLE;
}
function getSamplesProfile() {
return getProfileFromTextSamples(`
A[cat:DOM] A[cat:DOM] A[cat:DOM] A[cat:DOM] A[cat:DOM] A[cat:DOM] A[cat:DOM] A[cat:DOM]
B B B B B B B B
C C H[cat:Other] H[cat:Other] H[cat:Other] H[cat:Other] H[cat:Other] C
D F[cat:Graphics] I I I I I F[cat:Graphics]
E G G
`).profile;
}
describe('SampleGraph', function () {
autoMockCanvasContext();
autoMockElementSize({ width: GRAPH_WIDTH, height: GRAPH_HEIGHT });
autoMockIntersectionObserver();
beforeEach(addRootOverlayElement);
afterEach(removeRootOverlayElement);
function setup(profile: Profile = getSamplesProfile()) {
const store = storeWithProfile(profile);
const { getState, dispatch } = store;
const flushRafCalls = mockRaf();
const renderResult = render(
<Provider store={store}>
<TimelineTrackThread
threadsKey={0}
trackType="expanded"
trackName="Test Track"
/>
</Provider>
);
// WithSize uses requestAnimationFrame
flushRafCalls();
const sampleGraphCanvas = ensureExists(
document.querySelector('.threadSampleGraphCanvas'),
`Couldn't find the sample graph canvas, with selector .threadSampleGraphCanvas`
) as HTMLElement;
const thread = profile.threads[0];
const { stringArray, funcTable } = profile.shared;
// Perform a click on the sample graph.
function clickSampleGraph(index: IndexIntoSamplesTable) {
fireFullClick(sampleGraphCanvas, {
pageX: getSamplesPixelPosition(index),
offsetX: getSamplesPixelPosition(index),
pageY: GRAPH_HEIGHT / 2,
});
}
// Hover over the sample graph.
function hoverSampleGraph(index: IndexIntoSamplesTable) {
fireEvent(
sampleGraphCanvas,
getMouseEvent('mousemove', {
pageX: getSamplesPixelPosition(index),
offsetX: getSamplesPixelPosition(index),
pageY: GRAPH_HEIGHT / 2,
})
);
}
// This function gets the selected call node path as a list of function names.
function getCallNodePath() {
return selectedThreadSelectors
.getSelectedCallNodePath(getState())
.map((funcIndex) => stringArray[funcTable.name[funcIndex]]);
}
/**
* Coordinate the flushing of the requestAnimationFrame and the draw calls.
*/
function getContextDrawCalls() {
flushRafCalls();
return flushDrawLog();
}
return {
...renderResult,
dispatch,
getState,
profile,
thread,
store,
sampleGraphCanvas,
clickSampleGraph,
hoverSampleGraph,
getCallNodePath,
getContextDrawCalls,
};
}
it('redraws when the system theme changes', () => {
const { getContextDrawCalls } = setup();
// Flush the initial draw calls.
getContextDrawCalls();
// Simulate a theme change.
window.dispatchEvent(new CustomEvent('profiler-theme-change'));
const drawCalls = getContextDrawCalls();
expect(drawCalls.some(([operation]) => operation === 'fillRect')).toBe(
true
);
});
it('matches the component snapshot', () => {
const { sampleGraphCanvas } = setup();
expect(sampleGraphCanvas).toMatchSnapshot();
});
it('matches the 2d canvas draw snapshot', () => {
setup();
expect(flushDrawLog()).toMatchSnapshot();
});
/**
* The ThreadSampleGraph is not a connected component. It's easiest to test it
* as once it's connected to the Redux store in the TrackThread.
*/
describe('ThreadSampleGraph', function () {
it('selects the full call node path when clicked', function () {
const { clickSampleGraph, getCallNodePath } = setup();
// The full call node at this sample is:
// A -> B -> C -> F -> G
clickSampleGraph(1);
expect(getCallNodePath()).toEqual(['A', 'B', 'C', 'F', 'G']);
// The full call node at this sample is:
// A -> B -> H -> I
clickSampleGraph(2);
expect(getCallNodePath()).toEqual(['A', 'B', 'H', 'I']);
});
it('clicking outside of any sample removes any selection', function () {
const { clickSampleGraph, getCallNodePath, sampleGraphCanvas } = setup();
// Starting while nothing is selected.
expect(getCallNodePath()).toEqual([]);
// Selecting the sample with index 1.
// The full call node at this sample is:
// A -> B -> C -> F -> G
clickSampleGraph(1);
expect(getCallNodePath()).toEqual(['A', 'B', 'C', 'F', 'G']);
// Now we are selecting outside of the sample, which should remove the selection.
fireFullClick(sampleGraphCanvas, {
pageX: getSamplesPixelPosition(1) + PIXELS_PER_SAMPLE / 2,
offsetX: getSamplesPixelPosition(1) + PIXELS_PER_SAMPLE / 2,
pageY: GRAPH_HEIGHT / 2,
});
expect(getCallNodePath()).toEqual([]);
});
it('shows the correct tooltip when hovered', function () {
const { hoverSampleGraph, getCallNodePath } = setup();
// Hovering the sample with index 1.
// The full call node at this sample is:
// A -> B -> C -> F -> G
hoverSampleGraph(1);
// We didn't click, so selection should not change in the selected node path.
expect(getCallNodePath()).toEqual([]);
// Make sure that we have a tooltip.
expect(
ensureExists(
document.querySelector('.tooltip'),
'A tooltip component must exist for this test.'
)
).toMatchSnapshot();
});
it('does not show a tooltip when outside of a sample is hovered', function () {
const { hoverSampleGraph, getCallNodePath, sampleGraphCanvas } = setup();
// The full call node at this sample is:
// A -> B -> C -> F -> G
hoverSampleGraph(1);
// We didn't click, so selection should not change.
expect(getCallNodePath()).toEqual([]);
// Make sure that we have a tooltip.
expect(document.querySelector('.tooltip')).toBeTruthy();
// Now we are hovering outside of the samples.
fireEvent(
sampleGraphCanvas,
getMouseEvent('mousemove', {
pageX: getSamplesPixelPosition(1) + PIXELS_PER_SAMPLE / 2,
offsetX: getSamplesPixelPosition(1) + PIXELS_PER_SAMPLE / 2,
pageY: GRAPH_HEIGHT / 2,
})
);
// There should be no tooltip this time
expect(document.querySelector('.tooltip')).toBeFalsy();
});
});
});
describe('SampleGraph with intersection observer', function () {
autoMockCanvasContext();
autoMockElementSize({ width: GRAPH_WIDTH, height: GRAPH_HEIGHT });
// Do not automatically trigger the intersection observers.
autoMockIntersectionObserver(false);
function setup(profile: Profile = getSamplesProfile()) {
const store = storeWithProfile(profile);
const { getState, dispatch } = store;
const flushRafCalls = mockRaf();
const renderResult = render(
<Provider store={store}>
<TimelineTrackThread
threadsKey={0}
trackType="expanded"
trackName="Test Track"
/>
</Provider>
);
// WithSize uses requestAnimationFrame
flushRafCalls();
/**
* Coordinate the flushing of the requestAnimationFrame and the draw calls.
*/
function getContextDrawCalls(): DrawOperation[] {
flushRafCalls();
return flushDrawLog();
}
return {
...renderResult,
dispatch,
getState,
profile,
store,
getContextDrawCalls,
};
}
it('will not draw before the intersection observer', () => {
const { getContextDrawCalls } = setup();
const drawCalls = getContextDrawCalls();
// There are other canvases inside the TrackThread too. We want to make sure
// that sample graph is not drawn yet.
expect(drawCalls.some(([operation]) => operation === 'fillRect')).toBe(
false
);
});
it('will not draw after the intersection observer if it is not intersecting', () => {
const { getContextDrawCalls } = setup();
let drawCalls = getContextDrawCalls();
// There are other canvases inside the TrackThread too. We want to make sure
// that sample graph is not drawn yet.
expect(drawCalls.some(([operation]) => operation === 'beginPath')).toBe(
false
);
// Now let's trigger the intersection observer and make sure that it still
// doesn't draw it.
triggerIntersectionObservers({ isIntersecting: false });
drawCalls = getContextDrawCalls();
expect(drawCalls.some(([operation]) => operation === 'fillRect')).toBe(
false
);
});
it('will draw after the intersection observer if it is intersecting', () => {
const { getContextDrawCalls } = setup();
let drawCalls = getContextDrawCalls();
// There are other canvases inside the TrackThread too. We want to make sure
// that sample graph is not drawn yet.
expect(drawCalls.some(([operation]) => operation === 'fillRect')).toBe(
false
);
// Now let's trigger the intersection observer and make sure that it draws it.
triggerIntersectionObservers({ isIntersecting: true });
drawCalls = getContextDrawCalls();
expect(drawCalls.some(([operation]) => operation === 'fillRect')).toBe(
true
);
});
it('will redraw after it becomes visible again', () => {
const { getContextDrawCalls } = setup();
let drawCalls = getContextDrawCalls();
// There are other canvases inside the TrackThread too. We want to make sure
// that activity graph is not drawn yet.
expect(drawCalls.some(([operation]) => operation === 'fillRect')).toBe(
false
);
// Now let's trigger the intersection observer and make sure that it draws it.
triggerIntersectionObservers({ isIntersecting: true });
drawCalls = getContextDrawCalls();
expect(drawCalls.some(([operation]) => operation === 'fillRect')).toBe(
true
);
// Now it goes out of view again. Make sure that we don't redraw.
triggerIntersectionObservers({ isIntersecting: false });
drawCalls = getContextDrawCalls();
expect(drawCalls.some(([operation]) => operation === 'fillRect')).toBe(
false
);
// Send out the resize with a width change.
// By changing the "fake" result of getBoundingClientRect, we ensure that
// the pure components rerender because their `width` props change.
setMockedElementSize({ width: GRAPH_WIDTH * 2, height: GRAPH_HEIGHT });
triggerResizeObservers();
drawCalls = getContextDrawCalls();
// It should still be not drawn yet.
expect(drawCalls.some(([operation]) => operation === 'fillRect')).toBe(
false
);
// Now let's trigger the intersection observer again and make sure that it redraws.
triggerIntersectionObservers({ isIntersecting: true });
drawCalls = getContextDrawCalls();
expect(drawCalls.some(([operation]) => operation === 'fillRect')).toBe(
true
);
});
});