-
Notifications
You must be signed in to change notification settings - Fork 470
Expand file tree
/
Copy pathMarkerTable.test.tsx
More file actions
636 lines (553 loc) · 20.7 KB
/
MarkerTable.test.tsx
File metadata and controls
636 lines (553 loc) · 20.7 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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/* 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 { stripIndent } from 'common-tags';
// This module is mocked.
import copy from 'copy-to-clipboard';
import {
render,
screen,
fireEvent,
act,
} from 'firefox-profiler/test/fixtures/testing-library';
import { MarkerTable } from '../../components/marker-table';
import { MaybeMarkerContextMenu } from '../../components/shared/MarkerContextMenu';
import {
updatePreviewSelection,
changeMarkersSearchString,
hideGlobalTrack,
hideLocalTrack,
selectTrackWithModifiers,
} from '../../actions/profile-view';
import { changeSelectedTab } from 'firefox-profiler/actions/app';
import { ensureExists } from '../../utils/types';
import { getEmptyThread } from 'firefox-profiler/profile-logic/data-structures';
import { storeWithProfile } from '../fixtures/stores';
import type { TestDefinedMarker } from '../fixtures/profiles/processed-profile';
import {
getProfileFromTextSamples,
getMarkerTableProfile,
addMarkersToThreadWithCorrespondingSamples,
addIPCMarkerPairToThreads,
getNetworkTrackProfile,
getProfileWithMarkers,
} from '../fixtures/profiles/processed-profile';
import { fireFullClick, fireFullContextMenu } from '../fixtures/utils';
import { autoMockElementSize } from '../fixtures/mocks/element-size';
import {
getProfileWithNiceTracks,
getHumanReadableTracks,
} from '../fixtures/profiles/tracks';
import * as UrlStateSelectors from '../../selectors/url-state';
import { getScrollToSelectionGeneration } from 'firefox-profiler/selectors/profile';
import type { CauseBacktrace } from 'firefox-profiler/types';
describe('MarkerTable', function () {
// Set an arbitrary size that will not kick in any virtualization behavior.
autoMockElementSize({ width: 2000, height: 1000 });
function setup(profile = getMarkerTableProfile()) {
const store = storeWithProfile(profile);
const renderResult = render(
<Provider store={store}>
<>
<MaybeMarkerContextMenu />
<MarkerTable />
</>
</Provider>
);
const { container } = renderResult;
const fixedRows = () =>
Array.from(container.querySelectorAll('.treeViewRowFixedColumns'));
const scrolledRows = () =>
Array.from(container.querySelectorAll('.treeViewRowScrolledColumns'));
const getRowElement = (functionName: string | RegExp) =>
ensureExists(
screen.getByText(functionName).closest('.treeViewRow'),
`Couldn't find the row for node ${String(functionName)}.`
);
const getContextMenu = () =>
ensureExists(
container.querySelector('.react-contextmenu'),
`Couldn't find the context menu.`
);
return {
...renderResult,
...store,
fixedRows,
scrolledRows,
getRowElement,
getContextMenu,
};
}
it('renders some basic markers and updates when needed', () => {
const { container, fixedRows, scrolledRows, dispatch } = setup();
expect(fixedRows()).toHaveLength(7);
expect(scrolledRows()).toHaveLength(7);
expect(container.firstChild).toMatchSnapshot();
/* Check that the table updates properly despite the memoisation. */
act(() => {
dispatch(
updatePreviewSelection({
isModifying: false,
selectionStart: 10,
selectionEnd: 20,
})
);
});
expect(fixedRows()).toHaveLength(2);
expect(scrolledRows()).toHaveLength(2);
});
it('selects a row when left clicking', () => {
const { getByText, getRowElement, getState } = setup();
const initialScrollGeneration = getScrollToSelectionGeneration(getState());
fireFullClick(getByText(/setTimeout/));
expect(getRowElement(/setTimeout/)).toHaveClass('isSelected');
fireFullClick(getByText('foobar'));
expect(getRowElement(/setTimeout/)).not.toHaveClass('isSelected');
expect(getRowElement('foobar')).toHaveClass('isSelected');
// The scroll generation hasn't moved.
expect(getScrollToSelectionGeneration(getState())).toEqual(
initialScrollGeneration
);
});
it('displays a context menu when right clicking', () => {
jest.useFakeTimers();
const { getContextMenu, getRowElement, getByText } = setup();
function checkMenuIsDisplayedForNode(str: string | RegExp) {
expect(getContextMenu()).toHaveClass('react-contextmenu--visible');
// Note that selecting a menu item will close the menu.
fireFullClick(getByText('Copy description'));
expect(copy).toHaveBeenLastCalledWith(expect.stringMatching(str));
}
fireFullContextMenu(getByText(/setTimeout/));
checkMenuIsDisplayedForNode(/setTimeout/);
expect(getRowElement(/setTimeout/)).toHaveClass('isRightClicked');
// Wait that all timers are done before trying again.
act(() => jest.runAllTimers());
// Now try it again by right clicking 2 nodes in sequence.
fireFullContextMenu(getByText(/setTimeout/));
fireFullContextMenu(getByText('foobar'));
checkMenuIsDisplayedForNode('foobar');
expect(getRowElement(/setTimeout/)).not.toHaveClass('isRightClicked');
expect(getRowElement('foobar')).toHaveClass('isRightClicked');
// Wait that all timers are done before trying again.
act(() => jest.runAllTimers());
// And now let's do it again, but this time waiting for timers before
// clicking, because the timer can impact the menu being displayed.
fireFullContextMenu(getByText('NotifyDidPaint'));
fireFullContextMenu(getByText('foobar'));
act(() => jest.runAllTimers());
checkMenuIsDisplayedForNode('foobar');
expect(getRowElement('foobar')).toHaveClass('isRightClicked');
});
it("can copy a marker's cause using the context menu", () => {
jest.useFakeTimers();
// This is a tid we'll reuse later.
const tid = 4444;
// Just a simple profile with 1 thread and a nice stack.
const {
profile,
funcNamesDictPerThread: [{ E }],
} = getProfileFromTextSamples(`
A[lib:libxul.so]
B[lib:libxul.so]
C[lib:libxul.so]
D[lib:libxul.so]
E[lib:libxul.so]
`);
profile.threads[0].name = 'Main Thread';
// Add another thread with a known tid that we'll reuse in the marker's cause.
profile.threads.push(getEmptyThread({ name: 'Another Thread', tid }));
// Add the reflow marker to the first thread.
addMarkersToThreadWithCorrespondingSamples(
profile.threads[0],
profile.shared,
[
getReflowMarker(3, 100, {
tid: tid,
// We're cheating a bit here: E is a funcIndex, but because of how
// getProfileFromTextSamples works internally, this will be the right
// stackIndex too.
stack: E,
time: 1,
}),
]
);
const { getByText } = setup(profile);
fireFullContextMenu(getByText(/Reflow/));
fireFullClick(getByText('Copy call stack'));
expect(copy).toHaveBeenLastCalledWith(stripIndent`
A [libxul.so]
B [libxul.so]
C [libxul.so]
D [libxul.so]
E [libxul.so]
`);
});
it("can copy a marker's page url using the context menu", () => {
// A simple profile that contains markers with page information.
// We will be using `DOMContentLoaded` that has a page url.
const profile = getNetworkTrackProfile();
setup(profile);
// Make sure that a marker without an innerWindowID doesn't have this
// context menu item. `Navigation::Start` doesn't have one.
//
// We are using `getAllByText` here because marker table puts the same text
// to both `type` and `name` columns. But right clicking on either of them
// results in the same menu item.
fireFullContextMenu(screen.getAllByText('FirstContentfulPaint')[0]);
expect(screen.queryByText('Copy page URL')).not.toBeInTheDocument();
// Make sure that a marker with innerWindowID has this context menu item and
// can copy its page url successfully.
fireFullContextMenu(screen.getByText('DOMContentLoaded'));
fireFullClick(screen.getByText('Copy page URL'));
expect(copy).toHaveBeenLastCalledWith(
'https://developer.mozilla.org/en-US/'
);
});
describe('EmptyReasons', () => {
it('shows reasons when a profile has no non-network markers', () => {
const { profile } = getProfileFromTextSamples('A'); // Just a simple profile without any marker.
const { container } = setup(profile);
expect(container.querySelector('.EmptyReasons')).toMatchSnapshot();
});
it('shows reasons when all non-network markers have been filtered out', function () {
const { dispatch, container } = setup();
act(() => {
dispatch(changeMarkersSearchString('MATCH_NOTHING'));
});
expect(container.querySelector('.EmptyReasons')).toMatchSnapshot();
});
});
describe('IPC marker context menu item', () => {
/**
* Using the following tracks:
* [
* 'show [thread GeckoMain default]',
* 'show [thread GeckoMain tab]',
* ' - show [thread DOM Worker]',
* ' - show [thread Style]',
* ]
*/
const parentTrackReference = { type: 'global' as const, trackIndex: 0 };
const tabTrackReference = { type: 'global' as const, trackIndex: 1 };
const domWorkerTrackReference = {
type: 'local',
trackIndex: 0,
pid: '222',
};
const styleTrackReference = {
type: 'local' as const,
trackIndex: 1,
pid: '222',
};
const parentThreadIndex = 0;
const domWorkerThreadIndex = 2;
const styleThreadIndex = 3;
const tabPid = '222';
function setupWithTracksAndIPCMarker() {
const profile = getProfileWithNiceTracks();
addIPCMarkerPairToThreads(
{
startTime: 1,
endTime: 10,
messageSeqno: 1,
},
profile.threads[0], // Parent process
profile.threads[1], // tab process
profile.shared
);
addIPCMarkerPairToThreads(
{
startTime: 11,
endTime: 20,
messageSeqno: 2,
},
profile.threads[0], // Parent process
profile.threads[2], // DOM Worker
profile.shared
);
// Add an incomplete IPC marker to the Style thread.
// We do not add the other marker pair to another thread on purpose.
addMarkersToThreadWithCorrespondingSamples(
profile.threads[3],
profile.shared,
[
[
'IPC',
20,
25,
{
type: 'IPC',
startTime: 20,
endTime: 25,
otherPid: '444',
messageSeqno: 3,
messageType: 'PContent::Msg_PreferenceUpdate',
side: 'parent',
direction: 'sending',
phase: 'endpoint',
sync: false,
niceDirection: `sending to 444`,
},
],
]
);
return setup(profile);
}
it('can switch to another global track', function () {
const { getState } = setupWithTracksAndIPCMarker();
fireFullContextMenu(screen.getByText(/IPCIn/));
fireFullClick(screen.getByText(/Select the sender/));
expect(UrlStateSelectors.getSelectedThreadIndexes(getState())).toEqual(
new Set([parentThreadIndex])
);
});
it('can switch to a hidden global track', function () {
const { getState, dispatch } = setupWithTracksAndIPCMarker();
// Hide the global track first.
act(() => {
dispatch(hideGlobalTrack(parentTrackReference.trackIndex));
});
// Make sure that it's hidden.
expect(getHumanReadableTracks(getState())).toEqual([
'hide [thread GeckoMain default]',
' - hide [ipc GeckoMain]',
'show [thread GeckoMain tab] SELECTED',
' - hide [ipc GeckoMain] SELECTED',
' - show [thread DOM Worker]',
' - hide [ipc DOM Worker]',
' - show [thread Style]',
' - hide [ipc Style]',
]);
// Check the actual behavior now.
fireFullContextMenu(screen.getByText(/IPCIn/));
fireFullClick(screen.getByText(/Select the sender/));
expect(UrlStateSelectors.getSelectedThreadIndexes(getState())).toEqual(
new Set([parentThreadIndex])
);
// Make sure that it's not hidden anymore.
expect(getHumanReadableTracks(getState())).toEqual([
'show [thread GeckoMain default] SELECTED',
' - hide [ipc GeckoMain] SELECTED',
'show [thread GeckoMain tab]',
' - hide [ipc GeckoMain]',
' - show [thread DOM Worker]',
' - hide [ipc DOM Worker]',
' - show [thread Style]',
' - hide [ipc Style]',
]);
});
it('can switch to a local track', function () {
const { getState, dispatch } = setupWithTracksAndIPCMarker();
act(() => {
dispatch(selectTrackWithModifiers(parentTrackReference));
});
// Make sure that we are in the parent process thread.
expect(UrlStateSelectors.getSelectedThreadIndexes(getState())).toEqual(
new Set([parentThreadIndex])
);
// Check if we can switch to the DOM Worker properly.
fireFullContextMenu(screen.getByText(/sent to DOM Worker/));
fireFullClick(screen.getByText(/Select the receiver/));
expect(UrlStateSelectors.getSelectedThreadIndexes(getState())).toEqual(
new Set([domWorkerThreadIndex])
);
});
it('can switch to a hidden local track', function () {
const { getState, dispatch } = setupWithTracksAndIPCMarker();
act(() => {
dispatch(selectTrackWithModifiers(parentTrackReference));
});
// Make sure that we are in the parent process thread.
expect(UrlStateSelectors.getSelectedThreadIndexes(getState())).toEqual(
new Set([parentThreadIndex])
);
// Hide the global and local tracks.
act(() => {
dispatch(hideLocalTrack(tabPid, domWorkerTrackReference.trackIndex));
dispatch(hideGlobalTrack(tabTrackReference.trackIndex));
});
// Make sure that they are hidden.
expect(getHumanReadableTracks(getState())).toEqual([
'show [thread GeckoMain default] SELECTED',
' - hide [ipc GeckoMain] SELECTED',
'hide [thread GeckoMain tab]',
' - hide [ipc GeckoMain]',
' - hide [thread DOM Worker]',
' - hide [ipc DOM Worker]',
' - show [thread Style]',
' - hide [ipc Style]',
]);
// Check the actual behavior now.
fireFullContextMenu(screen.getByText(/sent to DOM Worker/));
fireFullClick(screen.getByText(/Select the receiver/));
expect(UrlStateSelectors.getSelectedThreadIndexes(getState())).toEqual(
new Set([domWorkerThreadIndex])
);
// Make sure that they are not hidden anymore.
expect(getHumanReadableTracks(getState())).toEqual([
'show [thread GeckoMain default]',
' - hide [ipc GeckoMain]',
'show [thread GeckoMain tab]',
' - hide [ipc GeckoMain]',
' - show [thread DOM Worker] SELECTED',
' - hide [ipc DOM Worker] SELECTED',
' - show [thread Style]',
' - hide [ipc Style]',
]);
});
it('does not render when the other thread is not profiled', function () {
const { getState, dispatch } = setupWithTracksAndIPCMarker();
act(() => {
dispatch(selectTrackWithModifiers(styleTrackReference));
});
// Make sure that we are in the Style thread.
expect(UrlStateSelectors.getSelectedThreadIndexes(getState())).toEqual(
new Set([styleThreadIndex])
);
// Silence console logs coming from the component.
jest.spyOn(console, 'warn').mockImplementation(() => {});
// Make sure that it's not in the context menu.
fireFullContextMenu(screen.getByText(/IPCOut/));
expect(screen.queryByText(/Select the/)).not.toBeInTheDocument();
});
});
describe('column resizing', () => {
it('can resize a column, then move it back to its initial size', () => {
const store = storeWithProfile(getMarkerTableProfile());
store.dispatch(changeSelectedTab('marker-table'));
const { unmount } = render(
<Provider store={store}>
<MarkerTable />
</Provider>
);
let dividerForFirstColumn = ensureExists(
document.querySelector('.treeViewColumnDivider')
);
let firstColumn = screen.getByText('Start');
expect(firstColumn).toHaveStyle({ width: '90px' });
fireEvent.mouseDown(dividerForFirstColumn, { clientX: 90 });
const body = ensureExists(document.body);
// Move right
fireEvent.mouseMove(body, { clientX: 100 });
expect(firstColumn).toHaveStyle({ width: '100px' });
// Move left
fireEvent.mouseMove(body, { clientX: 80 });
expect(firstColumn).toHaveStyle({ width: '80px' });
// Release the mouse -> still the same style
fireEvent.mouseUp(body);
expect(firstColumn).toHaveStyle({ width: '80px' });
// Now we'll unmount and render again.
unmount();
render(
<Provider store={store}>
<MarkerTable />
</Provider>
);
// Make sure the first column kept its width
firstColumn = screen.getByText('Start');
expect(firstColumn).toHaveStyle({ width: '80px' });
// Now double click to reset the style.
dividerForFirstColumn = ensureExists(
document.querySelector('.treeViewColumnDivider')
);
fireEvent.dblClick(dividerForFirstColumn, { detail: 2 });
expect(firstColumn).toHaveStyle({ width: '90px' });
});
});
it('can copy the table as plain text', () => {
const { container } = setup();
const button = ensureExists(
container.querySelector('.copyTableButton')
) as HTMLElement;
fireFullClick(button);
const menu = ensureExists(
container.querySelector('.markerCopyTableContextMenu')
) as HTMLElement;
const items = menu.querySelectorAll('[role="menuitem"]');
expect(items.length).toBe(2);
fireFullClick(items[0] as HTMLElement);
const pattern = new RegExp(
'^ +Start +Duration +Name +Details\\n +0s +0s +UserTiming +foobar\\n'
);
expect(copy).toHaveBeenLastCalledWith(expect.stringMatching(pattern));
});
it('can copy the table as markdown', () => {
const { container } = setup();
const button = ensureExists(
container.querySelector('.copyTableButton')
) as HTMLElement;
fireFullClick(button);
const menu = ensureExists(
container.querySelector('.markerCopyTableContextMenu')
) as HTMLElement;
const items = menu.querySelectorAll('[role="menuitem"]');
expect(items.length).toBe(2);
fireFullClick(items[1] as HTMLElement);
const pattern = new RegExp(
'^\\| +Start +\\| +Duration +\\| +Name +\\| +Details +\\|\\n' +
'\\|-+:\\|-+:\\|-+:\\|-+\\|\\n' +
'\\| +0s +\\| +0s +\\| +UserTiming +\\| +foobar +\\|\\n'
);
expect(copy).toHaveBeenLastCalledWith(expect.stringMatching(pattern));
});
it('shows warning when copying 10001+ rows', () => {
jest.useFakeTimers();
const markers: TestDefinedMarker[] = [];
for (let i = 1; i < 10010; i++) {
markers.push([
'UserTiming',
i,
i,
{
type: 'UserTiming',
name: 'foobar',
entryType: 'mark',
},
]);
}
const profile = getProfileWithMarkers(markers);
const { container } = setup(profile);
const button = ensureExists(
container.querySelector('.copyTableButton')
) as HTMLElement;
fireFullClick(button);
const menu = ensureExists(
container.querySelector('.markerCopyTableContextMenu')
) as HTMLElement;
const items = menu.querySelectorAll('[role="menuitem"]');
expect(items.length).toBe(2);
fireFullClick(items[0] as HTMLElement);
const pattern = new RegExp(
'^ +Start +Duration +Name +Details\\n +0s +0s +UserTiming +foobar\\n.+9\\.999s +0s +UserTiming +foobar$',
's'
);
expect(copy).toHaveBeenLastCalledWith(expect.stringMatching(pattern));
const warning = screen.getByText(
'The number of rows exceeds the limit: 10,009 > 10,000. Only the first 10,000 rows will be copied.'
);
expect(warning).toBeInTheDocument();
act(() => jest.runAllTimers());
const warning2 = screen.queryByText(
'The number of rows exceeds the limit: 10,009 > 10,000. Only the first 10,000 rows will be copied.'
);
expect(warning2).not.toBeInTheDocument();
});
});
function getReflowMarker(
startTime: number,
endTime: number,
cause?: CauseBacktrace
): TestDefinedMarker {
return [
'Reflow',
startTime,
endTime,
{
type: 'tracing',
category: 'Paint',
cause,
},
];
}