forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobalTrack.test.js
309 lines (276 loc) · 10.4 KB
/
GlobalTrack.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
/* 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 { stripIndent } from 'common-tags';
import {
render,
screen,
act,
} from 'firefox-profiler/test/fixtures/testing-library';
import {
changeSelectedThreads,
hideGlobalTrack,
} from '../../actions/profile-view';
import { TimelineGlobalTrack } from '../../components/timeline/GlobalTrack';
import { getGlobalTracks, getRightClickedTrack } from '../../selectors/profile';
import { getFirstSelectedThreadIndex } from '../../selectors/url-state';
import { ensureExists } from '../../utils/flow';
import {
autoMockCanvasContext,
flushDrawLog,
} from '../fixtures/mocks/canvas-context';
import { getProfileWithNiceTracks } from '../fixtures/profiles/tracks';
import {
getProfileFromTextSamples,
getProfileWithMarkers,
} from '../fixtures/profiles/processed-profile';
import { storeWithProfile } from '../fixtures/stores';
import { fireFullClick, fireFullContextMenu } from '../fixtures/utils';
import { autoMockElementSize } from '../fixtures/mocks/element-size';
import { mockRaf } from '../fixtures/mocks/request-animation-frame';
import { autoMockIntersectionObserver } from '../fixtures/mocks/intersection-observer';
import { selectedThreadSelectors } from '../../selectors/per-thread';
describe('timeline/GlobalTrack', function () {
autoMockCanvasContext();
// Some child components render to canvas.
autoMockElementSize({ width: 400, height: 400 });
autoMockIntersectionObserver();
/**
* getProfileWithNiceTracks() looks like: [
* 'show [thread GeckoMain default]', // Track index 0
* 'show [thread GeckoMain tab]', // Track index 1 (default)
* ' - show [thread DOM Worker]',
* ' - show [thread Style]',
* 'show [process]', // Track index 2
* ' - show [thread NoMain]'
* ]
*/
const GECKOMAIN_TAB_TRACK_INDEX = 1;
const NO_THREAD_TRACK_INDEX = 2;
const PRIVATE_TRACK_INDEX = 3;
const CONTAINER_TRACK_INDEX = 4;
function getGlobalTrackProfile() {
const profile = getProfileWithNiceTracks();
{
// Add another thread to highlight a thread-less global process track.
const {
profile: {
threads: [thread],
},
} = getProfileFromTextSamples('A');
thread.name = 'NoMain';
thread.pid = '5555';
profile.threads.push(thread);
}
{
// Add another thread launched in a fission browser to handle a private
// browsing window.
const {
profile: {
threads: [thread],
},
} = getProfileFromTextSamples('A');
thread.name = 'Private';
thread['eTLD+1'] = 'https://example.org';
thread.pid = '6666';
thread.tid = 6666;
thread.isPrivateBrowsing = true;
profile.threads.push(thread);
}
{
// Add another thread launched in a fission browser to handle a tab in a
// non-default container.
const {
profile: {
threads: [thread],
},
} = getProfileFromTextSamples('A');
thread.name = 'InContainer';
thread['eTLD+1'] = 'https://example.org';
thread.pid = '7777';
thread.tid = 7777;
thread.userContextId = 3;
profile.threads.push(thread);
}
return profile;
}
function setup(
trackIndex = GECKOMAIN_TAB_TRACK_INDEX,
profile = getGlobalTrackProfile()
) {
const store = storeWithProfile(profile);
const { getState, dispatch } = store;
const trackReference = { type: 'global', trackIndex };
const tracks = getGlobalTracks(getState());
const track = tracks[trackIndex];
const setInitialSelected = () => {};
if (track.type !== 'process') {
throw new Error('Expected a process track.');
}
const threadIndex = track.mainThreadIndex;
if (threadIndex !== null) {
// The assertions are simpler if the GeckoMain tab thread is not already selected.
dispatch(changeSelectedThreads(new Set([threadIndex + 1])));
}
// WithSize uses requestAnimationFrame
const flushRafCalls = mockRaf();
const renderResult = render(
<Provider store={store}>
<TimelineGlobalTrack
trackIndex={trackIndex}
trackReference={trackReference}
setInitialSelected={setInitialSelected}
/>
</Provider>
);
flushRafCalls();
const { container } = renderResult;
const getGlobalTrackLabel = () =>
ensureExists(
container.querySelector('.timelineTrackLabel'),
`Couldn't find the track label with selector .timelineTrackLabel`
);
const getGlobalTrackRow = () =>
ensureExists(
container.querySelector('.timelineTrackGlobalRow'),
`Couldn't find the track global row with selector .timelineTrackGlobalRow`
);
return {
...renderResult,
dispatch,
getState,
profile,
store,
trackReference,
trackIndex,
threadIndex,
getGlobalTrackLabel,
getGlobalTrackRow,
};
}
it('matches the snapshot of a global process track', () => {
const { container } = setup(GECKOMAIN_TAB_TRACK_INDEX);
expect(container.firstChild).toMatchSnapshot();
});
it('matches the snapshot of a global process track without a thread', () => {
const { container } = setup(NO_THREAD_TRACK_INDEX);
expect(container.firstChild).toMatchSnapshot();
});
it('displays the private browsing status of a fission thread', () => {
setup(PRIVATE_TRACK_INDEX);
const track = screen.getByText('Private');
// $FlowExpectError The parent element is an HTML Element but Flow doesn't know that.
expect(ensureExists(track.parentElement).title).toBe(stripIndent`
Private
Thread: "Private" (6666)
Process: "default" (6666)
Private Browsing: Yes
`);
});
it('displays the container status of a fission thread', () => {
setup(CONTAINER_TRACK_INDEX);
const track = screen.getByText('InContainer');
// $FlowExpectError The parent element is an HTML Element but Flow doesn't know that.
expect(ensureExists(track.parentElement).title).toBe(stripIndent`
InContainer
Thread: "InContainer" (7777)
Process: "default" (7777)
Container Id: 3
`);
});
it('has the correct selectors into useful parts of the component', function () {
const { getGlobalTrackLabel, getGlobalTrackRow } = setup();
expect(getGlobalTrackLabel()).toHaveTextContent('Content ProcessPID: 222');
expect(getGlobalTrackRow()).toBeTruthy();
});
it('starts out not being selected', function () {
const { getState, getGlobalTrackRow, threadIndex, trackReference } =
setup();
expect(getRightClickedTrack(getState())).not.toEqual(trackReference);
expect(getFirstSelectedThreadIndex(getState())).not.toBe(threadIndex);
expect(getGlobalTrackRow()).not.toHaveClass('selected');
});
it('can select a thread by clicking the label', () => {
const { getState, getGlobalTrackLabel, getGlobalTrackRow, threadIndex } =
setup();
expect(getFirstSelectedThreadIndex(getState())).not.toBe(threadIndex);
fireFullClick(getGlobalTrackLabel());
expect(getFirstSelectedThreadIndex(getState())).toBe(threadIndex);
expect(getGlobalTrackRow()).toHaveClass('selected');
});
it('can right click a thread', () => {
const { getState, getGlobalTrackLabel, threadIndex, trackReference } =
setup();
fireFullContextMenu(getGlobalTrackLabel());
expect(getRightClickedTrack(getState())).toEqual(trackReference);
expect(getFirstSelectedThreadIndex(getState())).not.toBe(threadIndex);
});
it('can select a thread by clicking the row', () => {
const { getState, getGlobalTrackRow, threadIndex } = setup();
expect(getFirstSelectedThreadIndex(getState())).not.toBe(threadIndex);
fireFullClick(getGlobalTrackRow());
expect(getFirstSelectedThreadIndex(getState())).toBe(threadIndex);
});
it('will render a stub div if the track is hidden', () => {
const { container, trackIndex, dispatch } = setup();
act(() => {
dispatch(hideGlobalTrack(trackIndex));
});
expect(container.querySelector('.timelineTrackHidden')).toBeTruthy();
expect(container.querySelector('.timelineTrack')).toBeFalsy();
});
function getTaskProfile(showMarkersInTimeline) {
const profile = getProfileWithMarkers([
['Task 1', 0, 10, ({ type: 'task' }: any)],
['Task 2', 20, 30, ({ type: 'task' }: any)],
['Task 3', 40, 50, ({ type: 'task' }: any)],
['Task 4', 60, 70, ({ type: 'task' }: any)],
]);
profile.meta.markerSchema = [
{
name: 'task',
display: ['timeline-overview'],
data: [],
},
];
const [thread] = profile.threads;
thread.name = 'Task Thread';
thread.showMarkersInTimeline = showMarkersInTimeline;
return profile;
}
it('will render markers with a timeline-overview schema', () => {
const showMarkersInTimeline = true;
const trackIndex = 0;
const profile = getTaskProfile(showMarkersInTimeline);
const { container, getState } = setup(trackIndex, profile);
// The markers overview graph is present.
expect(screen.getByTestId('TimelineMarkersOverview')).toBeInTheDocument();
// The markers are present in the track.
expect(
selectedThreadSelectors.getTimelineOverviewMarkerIndexes(getState())
).toEqual([0, 1, 2, 3]);
// Check the snapshots, as this will capture all of the draw calls for the markers.
expect(container.firstChild).toMatchSnapshot();
expect(flushDrawLog()).toMatchSnapshot();
});
it('will not render markers to the timeline-overview when showMarkersInTimeline is not set', () => {
const showMarkersInTimeline = false;
const trackIndex = 0;
const profile = getTaskProfile(showMarkersInTimeline);
const { container, getState } = setup(trackIndex, profile);
// The markers overview graph is now not present.
expect(
screen.queryByTestId('TimelineMarkersOverview')
).not.toBeInTheDocument();
// The markers are still present here.
expect(
selectedThreadSelectors.getTimelineOverviewMarkerIndexes(getState())
).toEqual([0, 1, 2, 3]);
// Check the snapshots, as this will capture the fact that nothing is rendered.
expect(container.firstChild).toMatchSnapshot();
expect(flushDrawLog()).toMatchSnapshot();
});
});