forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrackScreenshots.test.js
411 lines (351 loc) · 13.3 KB
/
TrackScreenshots.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
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
/* 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 type {
Profile,
Thread,
IndexIntoRawMarkerTable,
} from 'firefox-profiler/types';
import * as React from 'react';
import { Provider } from 'react-redux';
import {
render,
fireEvent,
screen,
act,
} from 'firefox-profiler/test/fixtures/testing-library';
import { commitRange } from '../../actions/profile-view';
import { TimelineTrackScreenshots } from '../../components/timeline/TrackScreenshots';
import { Timeline } from '../../components/timeline';
import { ensureExists } from '../../utils/flow';
import { FULL_TRACK_SCREENSHOT_HEIGHT } from '../../app-logic/constants';
import { autoMockCanvasContext } from '../fixtures/mocks/canvas-context';
import { mockRaf } from '../fixtures/mocks/request-animation-frame';
import { storeWithProfile } from '../fixtures/stores';
import {
getMouseEvent,
addRootOverlayElement,
removeRootOverlayElement,
fireFullClick,
} from '../fixtures/utils';
import { getScreenshotTrackProfile } from '../fixtures/profiles/processed-profile';
import { getProfileWithNiceTracks } from '../fixtures/profiles/tracks';
import { getPreviewSelection } from '../../selectors/profile';
import { autoMockDomRect } from 'firefox-profiler/test/fixtures/mocks/domrect.js';
import {
autoMockElementSize,
setMockedElementSize,
} from '../fixtures/mocks/element-size';
import { autoMockIntersectionObserver } from '../fixtures/mocks/intersection-observer';
// Mock out the element size to have a 400 pixel width and some left/top
// positioning.
const TRACK_WIDTH = 400;
const LEFT = 100;
const TOP = 7;
const INITIAL_ELEMENT_SIZE = {
width: TRACK_WIDTH,
height: FULL_TRACK_SCREENSHOT_HEIGHT,
offsetX: LEFT,
offsetY: TOP,
};
describe('timeline/TrackScreenshots', function () {
autoMockDomRect();
autoMockCanvasContext();
autoMockElementSize(INITIAL_ELEMENT_SIZE);
autoMockIntersectionObserver();
beforeEach(addRootOverlayElement);
afterEach(removeRootOverlayElement);
it('matches the component snapshot', () => {
const { container, unmount } = setup();
expect(container.firstChild).toMatchSnapshot();
// Trigger any unmounting behavior handlers, just make sure it doesn't
// throw any errors.
unmount();
});
it('shows a hover when moving the mouse', () => {
const { screenshotHover, moveMouseAndGetImageSize } = setup();
expect(screenshotHover).toThrow();
const size = moveMouseAndGetImageSize(LEFT + 0);
expect(screenshotHover()).toBeTruthy();
expect(size).toEqual({ width: 350, height: 175 });
});
it('sets a preview selection when clicking with the mouse', () => {
const { selectionOverlay, screenshotClick, getState } = setup(
undefined,
<Timeline />
);
expect(selectionOverlay).toThrow();
screenshotClick(LEFT);
const expectedPreviewSelection = {
hasSelection: true,
isModifying: false,
selectionEnd: 1,
selectionStart: 0,
};
expect(getPreviewSelection(getState())).toEqual(expectedPreviewSelection);
expect(selectionOverlay()).toBeTruthy();
});
it(`displays a smaller screenshot when preview selecting, and doesn't change it when clicking if a selection is already present`, () => {
const {
selectionOverlay,
screenshotTrack,
moveMouseAndGetImageSize,
getState,
} = setup(undefined, <Timeline />);
const track = screenshotTrack();
expect(selectionOverlay).toThrow();
// Mousedown then Mousemove will do a selection.
fireEvent(track, getMouseEvent('mousedown', { pageX: LEFT, pageY: TOP }));
const screenShotSize = moveMouseAndGetImageSize(LEFT + 200);
expect(selectionOverlay()).toBeTruthy();
// And we should see a small screenshot hover while preview selecting.
expect(screenShotSize).toEqual({ width: 100, height: 50 });
// click should keep this existing selection
const previouslySelectedPreviewSelection = getPreviewSelection(getState());
fireEvent(
track,
getMouseEvent('mouseup', { pageX: LEFT + 200, pageY: TOP })
);
fireEvent(track, getMouseEvent('click', { pageX: LEFT + 200, pageY: TOP }));
expect(getPreviewSelection(getState())).toEqual({
...previouslySelectedPreviewSelection,
isModifying: false,
});
expect(selectionOverlay()).toBeTruthy();
// but the screenshot hover size should now be bigger
const screenshotSize = moveMouseAndGetImageSize(LEFT + 210);
expect(screenshotSize).toEqual({ width: 350, height: 175 });
});
it('removes the hover when moving the mouse out', () => {
const { screenshotHover, screenshotTrack, moveMouse } = setup();
expect(screenshotHover).toThrow();
moveMouse(LEFT + 0);
expect(screenshotHover()).toBeTruthy();
fireEvent.mouseLeave(screenshotTrack());
expect(screenshotHover).toThrow();
});
it('moves the hover when moving the mouse', () => {
const { moveMouseAndGetLeft } = setup();
//Considering base to be 150, this value is big enough as a base
const base = moveMouseAndGetLeft(150);
// LEFT is 100 and the zoomed in screenshot's width is 350px.
// So hovering between 100 and 175px should give a result of left == 0.
// As soon as we pass 175px the left value should also move up.
expect(moveMouseAndGetLeft(175 + 10)).toBe(base + 10);
expect(moveMouseAndGetLeft(175 + 20)).toBe(base + 20);
});
it('places the hover image in the center of the track if there is enough space', () => {
const { moveMouseAndGetTop } = setup();
const containerTop = 100;
const screenshotHeight = 175; // This is the height of the zoomed-in screenshot.
setMockedElementSize({
...INITIAL_ELEMENT_SIZE,
offsetX: LEFT, // repeating this property for more clarity
offsetY: containerTop,
});
const pageX = LEFT;
const expectedTop = Math.floor(
containerTop + FULL_TRACK_SCREENSHOT_HEIGHT / 2 - screenshotHeight / 2
);
expect(moveMouseAndGetTop(pageX)).toBe(expectedTop);
});
it('makes sure the hover image does not go off the end of the container', () => {
const { moveMouseAndGetLeft } = setup();
const pageX = LEFT + TRACK_WIDTH - 1;
expect(pageX > moveMouseAndGetLeft(pageX)).toBe(true);
});
it('makes sure the hover image does not go off the left side of screen', () => {
const { moveMouseAndGetLeft } = setup();
// Because the zoomed in hover screenshot's size is 350px, it's stuck at the
// left of the window when the mouse is between 100 (the left of
// the track) and 175px.
expect(moveMouseAndGetLeft(100)).toBe(0);
expect(moveMouseAndGetLeft(150)).toBe(0);
expect(moveMouseAndGetLeft(175)).toBe(0);
// Starting at 176px, the hover tooltip starts to move.
expect(moveMouseAndGetLeft(176)).toBe(1);
});
it('makes sure the hover image does not go off the top side of screen', () => {
const { moveMouseAndGetTop } = setup();
const pageX = LEFT;
expect(moveMouseAndGetTop(pageX)).toBe(0);
});
it('renders a screenshot images when zooming into a range without a screenshot start time actually in the range', () => {
const profile = getScreenshotTrackProfile();
const [thread] = profile.threads;
const markerIndexA = thread.markers.length - 3;
const markerIndexB = thread.markers.length - 2;
// We keep the last marker so that the profile's root range is correct.
_setScreenshotMarkersToUnknown(thread, markerIndexA, markerIndexB);
const { dispatch, container } = setup(profile);
act(() => {
dispatch(
commitRange(
ensureExists(thread.markers.startTime[markerIndexA]),
ensureExists(thread.markers.startTime[markerIndexB])
)
);
});
const firstImage = ensureExists(
container.querySelector('.timelineTrackScreenshotImgContainer'),
`Couldn't find at least one screenshot image.`
);
expect(parseInt(firstImage.style.left)).toBeGreaterThanOrEqual(0);
});
it('renders a no images when zooming into a range before screenshots', () => {
const profile = getScreenshotTrackProfile();
const [thread] = profile.threads;
const markerIndexA = 0;
const markerIndexB = 1;
_setScreenshotMarkersToUnknown(thread, markerIndexA, markerIndexB);
const { dispatch, container } = setup(profile);
act(() => {
dispatch(
commitRange(
ensureExists(thread.markers.startTime[markerIndexA]),
ensureExists(thread.markers.startTime[markerIndexB])
)
);
});
expect(container.querySelector('.timelineTrackScreenshotImg')).toBeFalsy();
});
it('is created in the <Timeline /> with a profile with screenshots', function () {
setup(getScreenshotTrackProfile(), <Timeline />);
// The function `getAllByText` throws already if none are found, with a useful Error,
// if it can't find any elements. But we still use `expect` to keep a "test-like"
// assertion, even if it's useless.
expect(screen.getAllByRole('button', { name: 'Screenshots' })).toHaveLength(
3
);
const screenshotTracks = document.querySelectorAll(
'.timelineTrackScreenshot'
);
expect(screenshotTracks).toHaveLength(3);
// Tracks 1 and 3 are rendered in the full length because these windows are
// not closed. They should have the same number of elements.
expect(screenshotTracks[0].childElementCount).toBe(
screenshotTracks[2].childElementCount
);
// Track 2 is closed before the end and therefore should have less DOM elements.
expect(screenshotTracks[1].childElementCount).toBeLessThan(
screenshotTracks[0].childElementCount
);
});
it('is not created in the <Timeline /> with a profile with no screenshots', function () {
const { queryByText } = setup(getProfileWithNiceTracks(), <Timeline />);
expect(queryByText('Screenshots')).not.toBeInTheDocument();
});
});
function setup(
profile: Profile = getScreenshotTrackProfile(),
component = <TimelineTrackScreenshots threadIndex={0} windowId="0" />
) {
const store = storeWithProfile(profile);
const { getState, dispatch } = store;
const flushRafCalls = mockRaf();
// getBoundingClientRect is already mocked by autoMockElementSize.
jest.spyOn(HTMLElement.prototype, 'getClientRects').mockImplementation(() => {
return [
new DOMRect(
LEFT,
TOP,
LEFT + TRACK_WIDTH,
TOP + FULL_TRACK_SCREENSHOT_HEIGHT
),
];
});
const renderResult = render(<Provider store={store}>{component}</Provider>);
const { container } = renderResult;
// WithSize uses requestAnimationFrame
flushRafCalls();
function screenshotHover() {
return ensureExists(
document.querySelector('.timelineTrackScreenshotHover'),
`Couldn't find the screenshot hover element, with selector .timelineTrackScreenshotHover`
);
}
function screenshotHoverImage() {
return ensureExists(
document.querySelector('.timelineTrackScreenshotHoverImg'),
`Couldn't find the screenshot hover element, with selector .timelineTrackScreenshotHoverImg`
);
}
function selectionOverlay() {
return ensureExists(
document.querySelector('.timelineSelectionOverlay'),
`Couldn't find the selection element, with selector .timelineSelectionOverlay`
);
}
function screenshotClick(pageX: number) {
fireFullClick(screenshotTrack(), { pageX, pageY: TOP });
}
function screenshotTrack() {
return ensureExists(
container.querySelector('.timelineTrackScreenshot'),
`Couldn't find the screenshot track, with selector .timelineTrackScreenshot`
);
}
function moveMouse(pageX: number) {
fireEvent(
screenshotTrack(),
getMouseEvent('mousemove', { pageX, pageY: TOP, buttons: 1 })
);
}
function moveMouseAndGetLeft(pageX: number): number {
moveMouse(pageX);
return parseInt(screenshotHover().style.left);
}
function moveMouseAndGetTop(pageX: number): number {
moveMouse(pageX);
return parseInt(screenshotHover().style.top);
}
function moveMouseAndGetImageSize(pageX: number): {|
width: number,
height: number,
|} {
moveMouse(pageX);
const style = screenshotHoverImage().style;
return {
width: parseInt(style.width),
height: parseInt(style.height),
};
}
return {
...renderResult,
dispatch,
getState,
thread: profile.threads[0],
store,
screenshotHover,
screenshotTrack,
moveMouse,
moveMouseAndGetLeft,
moveMouseAndGetTop,
moveMouseAndGetImageSize,
selectionOverlay,
screenshotClick,
};
}
/**
* Take a thread full screenshot markers, and set some to "Unknown" in order to
* create gaps in a screenshot track.
*/
function _setScreenshotMarkersToUnknown(
thread: Thread,
...markerIndexes: IndexIntoRawMarkerTable[]
) {
// Remove off the last few screenshot markers
const unknownStringIndex = thread.stringTable.indexForString('Unknown');
const screenshotStringIndex = thread.stringTable.indexForString(
'CompositorScreenshot'
);
for (const markerIndex of markerIndexes) {
// Double check that we've actually got screenshot markers:
if (thread.markers.name[markerIndex] !== screenshotStringIndex) {
throw new Error('This is not a screenshot marker.');
}
thread.markers.name[markerIndex] = unknownStringIndex;
thread.markers.data[markerIndex] = null;
}
}