|
6 | 6 |
|
7 | 7 | import type { CssPixels } from 'firefox-profiler/types'; |
8 | 8 | import { Provider } from 'react-redux'; |
9 | | -import { fireEvent } from '@testing-library/react'; |
| 9 | +import { fireEvent, act } from '@testing-library/react'; |
10 | 10 |
|
11 | 11 | import { render } from 'firefox-profiler/test/fixtures/testing-library'; |
| 12 | +import { commitRange } from 'firefox-profiler/actions/profile-view'; |
| 13 | +import { getThreadSelectors } from 'firefox-profiler/selectors/per-thread'; |
12 | 14 | import { TrackCustomMarker } from '../../components/timeline/TrackCustomMarker'; |
13 | 15 | import { ensureExists } from '../../utils/types'; |
14 | 16 |
|
@@ -46,9 +48,13 @@ function getMarkerPixelPosition(time: number): CssPixels { |
46 | 48 | return (time * GRAPH_WIDTH) / SAMPLE_COUNT; |
47 | 49 | } |
48 | 50 |
|
49 | | -function setup() { |
| 51 | +function setup( |
| 52 | + values: number[] = Array(SAMPLE_COUNT) |
| 53 | + .fill(0) |
| 54 | + .map((_, i) => i) |
| 55 | +) { |
50 | 56 | const { profile, stringTable } = getProfileFromTextSamples( |
51 | | - Array(SAMPLE_COUNT).fill('A').join(' ') |
| 57 | + Array(values.length).fill('A').join(' ') |
52 | 58 | ); |
53 | 59 | const markerStringIndex = stringTable.indexForString('Marker'); |
54 | 60 | const threadIndex = 0; |
@@ -83,9 +89,10 @@ function setup() { |
83 | 89 | thread.markers.category.push(4); |
84 | 90 | thread.markers.length++; |
85 | 91 | }; |
86 | | - for (let i = 0; i < SAMPLE_COUNT; i++) { |
87 | | - addMarker(i, i, i * 2); |
88 | | - } |
| 92 | + |
| 93 | + values.forEach((value, index) => { |
| 94 | + addMarker(index, value, value * 2); |
| 95 | + }); |
89 | 96 | const store = storeWithProfile(profile); |
90 | 97 | const { getState, dispatch } = store; |
91 | 98 | const flushRafCalls = mockRaf(); |
@@ -142,6 +149,7 @@ function setup() { |
142 | 149 | flushRafCalls, |
143 | 150 | getMarkerDot, |
144 | 151 | getContextDrawCalls, |
| 152 | + markerStringIndex, |
145 | 153 | }; |
146 | 154 | } |
147 | 155 |
|
@@ -300,3 +308,89 @@ describe('TrackCustomMarker with intersection observer', function () { |
300 | 308 | ); |
301 | 309 | }); |
302 | 310 | }); |
| 311 | + |
| 312 | +describe('TrackCustomMarker with committed range scaling', function () { |
| 313 | + autoMockCanvasContext(); |
| 314 | + autoMockElementSize({ width: GRAPH_WIDTH, height: GRAPH_HEIGHT }); |
| 315 | + autoMockIntersectionObserver(); |
| 316 | + beforeEach(addRootOverlayElement); |
| 317 | + afterEach(removeRootOverlayElement); |
| 318 | + |
| 319 | + it('uses the committed range scaling selector correctly', function () { |
| 320 | + // Create markers with values: [1, 100, 2, 3] at times 0, 1, 2, 3 |
| 321 | + // Full range min/max would be 1-100, but committed range 2-3 should be 2-3 |
| 322 | + const { profile, dispatch, getState, markerStringIndex } = setup([ |
| 323 | + 1, 100, 2, 3, |
| 324 | + ]); |
| 325 | + |
| 326 | + // Get the selectors to test them directly |
| 327 | + const state = getState(); |
| 328 | + const threadSelectors = getThreadSelectors(0); |
| 329 | + const markerSchema = ensureExists( |
| 330 | + profile.meta.markerSchema.find((schema) => schema.name === 'Marker') |
| 331 | + ); |
| 332 | + const markerTrackSelectors = threadSelectors.getMarkerTrackSelectors( |
| 333 | + markerSchema, |
| 334 | + markerStringIndex |
| 335 | + ); |
| 336 | + |
| 337 | + // Get initial samples (full range) - should have min=1, max=200 (because second line = first * 2) |
| 338 | + const fullRangeSamples = |
| 339 | + markerTrackSelectors.getCollectedCustomMarkerSamples(state); |
| 340 | + const fullRangeValueBounds = |
| 341 | + markerTrackSelectors.getCommittedRangeMarkerSampleValueBounds(state); |
| 342 | + expect(fullRangeValueBounds.minNumber).toBe(1); |
| 343 | + expect(fullRangeValueBounds.maxNumber).toBe(200); // 100 * 2 from second line |
| 344 | + |
| 345 | + // Commit range from 2ms to 3.5ms to include only the markers at times 2ms and 3ms. |
| 346 | + // The 2.1 value is to exclude from the range the end of the marker at 1ms. |
| 347 | + act(() => { |
| 348 | + dispatch(commitRange(2.1, 3.5)); |
| 349 | + }); |
| 350 | + |
| 351 | + // Get committed range samples - should include markers at 2ms[2,4] and 3ms[3,6] |
| 352 | + const newState = getState(); |
| 353 | + const committedRangeSamples = |
| 354 | + markerTrackSelectors.getCollectedCustomMarkerSamples(newState); |
| 355 | + const committedRangeValueBounds = |
| 356 | + markerTrackSelectors.getCommittedRangeMarkerSampleValueBounds(newState); |
| 357 | + expect(committedRangeValueBounds.minNumber).toBe(2); |
| 358 | + expect(committedRangeValueBounds.maxNumber).toBe(6); |
| 359 | + |
| 360 | + // Verify the arrays are the same length (same structure) |
| 361 | + expect(committedRangeSamples.numbersPerLine).toHaveLength( |
| 362 | + fullRangeSamples.numbersPerLine.length |
| 363 | + ); |
| 364 | + expect(committedRangeSamples.markerIndexes).toHaveLength( |
| 365 | + fullRangeSamples.markerIndexes.length |
| 366 | + ); |
| 367 | + }); |
| 368 | + |
| 369 | + it('handles edge case where committed range has identical values', function () { |
| 370 | + // Create markers with identical values in the committed range |
| 371 | + const { profile, dispatch, getState, markerStringIndex } = setup([ |
| 372 | + 1, 100, 5, 5, |
| 373 | + ]); |
| 374 | + |
| 375 | + const threadSelectors = getThreadSelectors(0); |
| 376 | + const markerSchema = ensureExists( |
| 377 | + profile.meta.markerSchema.find((schema) => schema.name === 'Marker') |
| 378 | + ); |
| 379 | + const markerTrackSelectors = threadSelectors.getMarkerTrackSelectors( |
| 380 | + markerSchema, |
| 381 | + markerStringIndex |
| 382 | + ); |
| 383 | + |
| 384 | + // Focus on the range with identical values at times 2ms and 3ms (values [5,10] and [5,10]) |
| 385 | + act(() => { |
| 386 | + dispatch(commitRange(2.1, 3.5)); |
| 387 | + }); |
| 388 | + |
| 389 | + // Should not crash when min === max - both markers have first=5, second=10 |
| 390 | + const state = getState(); |
| 391 | + const committedRangeValueBounds = |
| 392 | + markerTrackSelectors.getCommittedRangeMarkerSampleValueBounds(state); |
| 393 | + expect(committedRangeValueBounds.minNumber).toBe(5); |
| 394 | + expect(committedRangeValueBounds.maxNumber).toBe(10); // 5 * 2 from second line |
| 395 | + }); |
| 396 | +}); |
0 commit comments