forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl-handling.test.js
1986 lines (1752 loc) · 62.4 KB
/
url-handling.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
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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 { oneLineTrim } from 'common-tags';
import * as urlStateSelectors from '../selectors/url-state';
import {
addTransformToStack,
changeCallTreeSearchString,
changeImplementationFilter,
changeMarkersSearchString,
changeNetworkSearchString,
changeProfileName,
changeSelectedThreads,
changeGlobalTrackOrder,
changeLocalTrackOrder,
commitRange,
setDataSource,
updateBottomBoxContentsAndMaybeOpen,
closeBottomBox,
} from '../actions/profile-view';
import { changeSelectedTab, changeProfilesToCompare } from '../actions/app';
import {
stateFromLocation,
getQueryStringFromUrlState,
urlFromState,
CURRENT_URL_VERSION,
upgradeLocationToCurrentVersion,
UrlUpgradeError,
} from '../app-logic/url-handling';
import { blankStore } from './fixtures/stores';
import {
viewProfile,
changeTimelineTrackOrganization,
} from '../actions/receive-profile';
import type {
Profile,
StartEndRange,
Store,
State,
} from 'firefox-profiler/types';
import getNiceProfile from './fixtures/profiles/call-nodes';
import queryString from 'query-string';
import {
getHumanReadableTracks,
getProfileWithNiceTracks,
} from './fixtures/profiles/tracks';
import {
getProfileFromTextSamples,
addActiveTabInformationToProfile,
} from './fixtures/profiles/processed-profile';
import { selectedThreadSelectors } from '../selectors/per-thread';
import {
encodeUintArrayForUrlComponent,
encodeUintSetForUrlComponent,
} from '../utils/uintarray-encoding';
import {
getActiveTabGlobalTracks,
getActiveTabResourceTracks,
getProfile,
} from '../selectors/profile';
import { getView } from '../selectors/app';
import { SYMBOL_SERVER_URL } from '../app-logic/constants';
import { getThreadsKey } from '../profile-logic/profile-data';
function _getStoreWithURL(
settings: {
pathname?: string,
search?: string,
hash?: string,
v?: number | false, // If v is false, do not add a v parameter to the search string.
} = {},
profile: Profile | null = getNiceProfile()
) {
const { pathname, hash, search, v } = Object.assign(
{
pathname: '/public/1ecd7a421948995171a4bb483b7bcc8e1868cc57/calltree/',
hash: '',
search: '',
v: CURRENT_URL_VERSION,
},
settings
);
// Provide some defaults to the search string as needed.
const query = Object.assign(
{
// Ensure that the URL has a version.
v,
// Ensure there is a thread index.
thread: 0,
},
queryString.parse(search.substr(1), { arrayFormat: 'bracket' })
);
const newUrlState = stateFromLocation({
pathname,
search: '?' + queryString.stringify(query, { arrayFormat: 'bracket' }),
hash,
});
const store = blankStore();
store.dispatch({
type: 'UPDATE_URL_STATE',
newUrlState,
});
if (profile) {
store.dispatch(viewProfile(profile));
}
return store;
}
function getQueryStringFromState(state: State) {
const urlState = urlStateSelectors.getUrlState(state);
const queryString = getQueryStringFromUrlState(urlState);
return queryString;
}
// Serialize the URL of the current state, and create a new store from that URL.
function _getStoreFromStateAfterUrlRoundtrip(state: State): Store {
const profile = getProfile(state);
const urlState = urlStateSelectors.getUrlState(state);
const url = urlFromState(urlState);
const newUrlState = stateFromLocation(
new URL(url, 'https://profiler.firefox.com')
);
const store = blankStore();
store.dispatch({
type: 'UPDATE_URL_STATE',
newUrlState,
});
if (profile) {
store.dispatch(viewProfile(profile));
}
return store;
}
describe('selectedThread', function () {
function dispatchUrlWithThread(store, threadIndexSet) {
const serializedSelectedThreads =
encodeUintSetForUrlComponent(threadIndexSet);
const newUrlState = stateFromLocation({
pathname: '/public/1ecd7a421948995171a4bb483b7bcc8e1868cc57/calltree/',
search: `?thread=${serializedSelectedThreads}&v=${CURRENT_URL_VERSION}`,
hash: '',
});
store.dispatch({
type: 'UPDATE_URL_STATE',
newUrlState,
});
}
function setup(threadIndexSet) {
const store = blankStore();
dispatchUrlWithThread(store, threadIndexSet);
const { profile } = getProfileFromTextSamples('A', 'B', 'C', 'D');
Object.assign(profile.threads[0], {
name: 'GeckoMain',
pid: '123',
});
Object.assign(profile.threads[1], {
name: 'Compositor',
pid: '123',
});
Object.assign(profile.threads[2], {
name: 'GeckoMain',
processType: 'tab',
pid: '246',
});
Object.assign(profile.threads[3], {
name: 'GeckoMain',
processType: 'tab',
pid: '789',
});
store.dispatch(viewProfile(profile));
return store;
}
it('selects the right thread when receiving a profile from web', function () {
const { getState } = setup(new Set([1]));
expect(urlStateSelectors.getSelectedThreadIndexes(getState())).toEqual(
new Set([1])
);
});
it('selects a default thread when a wrong thread has been requested', function () {
const { getState } = setup(new Set([100]));
// "2" is the content process' main tab
expect(urlStateSelectors.getSelectedThreadIndexes(getState())).toEqual(
new Set([2])
);
});
it('selects the right threads (multi selection) when receiving a profile from web', function () {
const { getState } = setup(new Set([0, 2]));
expect(urlStateSelectors.getSelectedThreadIndexes(getState())).toEqual(
new Set([0, 2])
);
});
});
describe('url handling tracks', function () {
function initWithSearchParams(search: string) {
return _getStoreWithURL({ search }, getProfileWithNiceTracks());
}
describe('global tracks', function () {
it('creates tracks without any set search parameters', function () {
const { getState } = initWithSearchParams('');
expect(getHumanReadableTracks(getState())).toEqual([
'show [thread GeckoMain default] SELECTED',
'show [thread GeckoMain tab]',
' - show [thread DOM Worker]',
' - show [thread Style]',
]);
});
it('can reorder global tracks', function () {
const { getState } = initWithSearchParams('?globalTrackOrder=10');
expect(getHumanReadableTracks(getState())).toEqual([
'show [thread GeckoMain tab]',
' - show [thread DOM Worker]',
' - show [thread Style]',
'show [thread GeckoMain default] SELECTED',
]);
});
it('can hide tracks', function () {
const { getState } = initWithSearchParams('?hiddenGlobalTracks=1');
expect(getHumanReadableTracks(getState())).toEqual([
'show [thread GeckoMain default] SELECTED',
'hide [thread GeckoMain tab]',
' - show [thread DOM Worker]',
' - show [thread Style]',
]);
});
it('will not accept invalid tracks in the thread order', function () {
const { getState } = initWithSearchParams('?globalTrackOrder=102');
// This will result in being the default order.
expect(urlStateSelectors.getGlobalTrackOrder(getState())).toEqual([0, 1]);
});
it('will not accept invalid hidden threads', function () {
const { getState } = initWithSearchParams(
'?hiddenGlobalTracks=089&thread=1'
);
expect(urlStateSelectors.getHiddenGlobalTracks(getState())).toEqual(
new Set([0])
);
});
it('keeps the order at reload', function () {
// In this test, we want to have indexes greater than 10. So we generate
// 15 threads with the same information.
const aLotOfThreads = Array.from({ length: 15 }, () => 'A');
const { profile } = getProfileFromTextSamples(...aLotOfThreads);
// Set a different pid for each thread, so that they're only global tracks.
profile.threads.forEach((thread, i) => {
thread.pid = `${i}`;
});
const store = _getStoreWithURL({}, profile);
store.dispatch(
changeGlobalTrackOrder([
5, 4, 2, 11, 9, 1, 12, 13, 14, 3, 7, 8, 10, 6, 0,
])
);
const previousOrder = urlStateSelectors.getGlobalTrackOrder(
store.getState()
);
// This simulates a page reload.
const storeAfterReload = _getStoreFromStateAfterUrlRoundtrip(
store.getState()
);
expect(
urlStateSelectors.getGlobalTrackOrder(storeAfterReload.getState())
).toEqual(previousOrder);
});
});
describe('local tracks', function () {
it('can reorder local tracks', function () {
const { dispatch, getState } = initWithSearchParams('');
expect(getHumanReadableTracks(getState())).toEqual([
'show [thread GeckoMain default] SELECTED',
'show [thread GeckoMain tab]',
' - show [thread DOM Worker]',
' - show [thread Style]',
]);
// The URL parameter for localTrackOrderByPid should be empty.
expect(getQueryStringFromState(getState())).not.toContain(
'localTrackOrderByPid='
);
// Change the order of Style and DOM Worker
dispatch(changeLocalTrackOrder('222', [1, 0]));
expect(getHumanReadableTracks(getState())).toEqual([
'show [thread GeckoMain default] SELECTED',
'show [thread GeckoMain tab]',
' - show [thread Style]',
' - show [thread DOM Worker]',
]);
// Now the URL parameter for localTrackOrderByPid should contain this change.
expect(getQueryStringFromState(getState())).toContain(
'localTrackOrderByPid=222-10'
);
const previousOrder =
urlStateSelectors.getLocalTrackOrderByPid(getState());
// This simulates a page reload.
const storeAfterReload = _getStoreFromStateAfterUrlRoundtrip(getState());
expect(
urlStateSelectors.getLocalTrackOrderByPid(storeAfterReload.getState())
).toEqual(previousOrder);
expect(getHumanReadableTracks(storeAfterReload.getState())).toEqual([
'show [thread GeckoMain default] SELECTED',
'show [thread GeckoMain tab]',
' - show [thread Style]',
' - show [thread DOM Worker]',
]);
});
it('can hide local tracks', function () {
const { getState } = initWithSearchParams(
'?hiddenLocalTracksByPid=222-1'
);
expect(getHumanReadableTracks(getState())).toEqual([
'show [thread GeckoMain default] SELECTED',
'show [thread GeckoMain tab]',
' - show [thread DOM Worker]',
' - hide [thread Style]',
]);
});
// This is a test for issue https://github.com/firefox-devtools/profiler/issues/1389
it('can select a local track without mixing track and thread indexes', function () {
// We're building a very specific profile, where local track indexes and
// thread indexes could be confused. This is easier if we have local
// tracks for the first process, because then the thread indexes and the
// local track indexes are off by one.
const { profile } = getProfileFromTextSamples('A', 'B', 'C');
const [thread1, thread2, thread3] = profile.threads;
thread1.name = 'GeckoMain';
thread1.isMainThread = true;
thread1.pid = '111';
thread2.name = 'DOM Worker';
thread2.processType = 'tab';
thread2.pid = '111';
thread3.name = 'Style';
thread3.processType = 'tab';
thread3.pid = '111';
const { getState } = _getStoreWithURL(
// In this search query, we want to hide the second local track of the
// process with PID 111 (`111-1`), that is the "Style" thread, and
// select the second thread (`thread=1`), that is the "DOM Worker"
// thread, which is the local track `111-0`.
// This ensures that we don't confuse local track and thread indexes
// when selecting threads (see issue #1389).
{ search: '?hiddenLocalTracksByPid=111-1&thread=1' },
profile
);
expect(getHumanReadableTracks(getState())).toEqual([
'show [thread GeckoMain default]',
' - show [thread DOM Worker] SELECTED',
' - hide [thread Style]',
]);
});
});
describe('legacy thread information', function () {
it('handles legacy thread ordering', function () {
// Flip the threads around
const { getState } = initWithSearchParams('?threadOrder=3-2-1-0');
expect(getHumanReadableTracks(getState())).toEqual([
'show [thread GeckoMain tab]',
' - show [thread Style]',
' - show [thread DOM Worker]',
'show [thread GeckoMain default] SELECTED',
]);
});
it('handles legacy thread hiding', function () {
// Flip the threads around
const { getState } = initWithSearchParams('?hiddenThreads=0-2');
expect(getHumanReadableTracks(getState())).toEqual([
'hide [thread GeckoMain default]',
'show [thread GeckoMain tab] SELECTED',
' - hide [thread DOM Worker]',
' - show [thread Style]',
]);
});
});
});
describe('search strings', function () {
it('properly handles the call tree search string stacks with 1 item', function () {
const { getState } = _getStoreWithURL({ search: '?search=string' });
expect(urlStateSelectors.getCurrentSearchString(getState())).toBe('string');
expect(urlStateSelectors.getSearchStrings(getState())).toEqual(['string']);
});
it('properly handles the call tree search string stacks with several items', function () {
const { getState } = _getStoreWithURL({
search: '?search=string,foo,%20bar',
});
expect(urlStateSelectors.getCurrentSearchString(getState())).toBe(
'string,foo, bar'
);
expect(urlStateSelectors.getSearchStrings(getState())).toEqual([
'string',
'foo',
'bar',
]);
});
it('properly handles marker search strings', function () {
const { getState } = _getStoreWithURL({
search: '?markerSearch=otherString',
});
expect(urlStateSelectors.getMarkersSearchString(getState())).toBe(
'otherString'
);
});
it('properly handles showUserTimings strings', function () {
const { getState } = _getStoreWithURL({ search: '' });
expect(urlStateSelectors.getShowUserTimings(getState())).toBe(false);
});
it('defaults to not showing user timings', function () {
const { getState } = _getStoreWithURL();
expect(urlStateSelectors.getShowUserTimings(getState())).toBe(false);
});
it('serializes the call tree search strings in the URL', function () {
const { getState, dispatch } = _getStoreWithURL();
const callTreeSearchString = 'some, search, string';
dispatch(changeCallTreeSearchString(callTreeSearchString));
['calltree', 'stack-chart', 'flame-graph'].forEach((tabSlug) => {
dispatch(changeSelectedTab(tabSlug));
const queryString = getQueryStringFromState(getState());
expect(queryString).toContain(
`search=${encodeURIComponent(callTreeSearchString)}`
);
});
});
it('serializes the marker search string in the URL', function () {
const { getState, dispatch } = _getStoreWithURL();
const markerSearchString = 'abc';
dispatch(changeMarkersSearchString(markerSearchString));
['marker-chart', 'marker-table'].forEach((tabSlug) => {
dispatch(changeSelectedTab(tabSlug));
const queryString = getQueryStringFromState(getState());
expect(queryString).toContain(`markerSearch=${markerSearchString}`);
});
});
it('serializes the network search string in the URL', function () {
const { getState, dispatch } = _getStoreWithURL();
const networkSearchString = 'abc';
dispatch(changeNetworkSearchString(networkSearchString));
dispatch(changeSelectedTab('network-chart'));
const queryString = getQueryStringFromState(getState());
expect(queryString).toContain(`networkSearch=${networkSearchString}`);
});
});
describe('profileName', function () {
it('serializes the profileName in the URL', function () {
const { getState, dispatch } = _getStoreWithURL();
const profileName = 'Good Profile';
dispatch(changeProfileName(profileName));
const queryString = getQueryStringFromState(getState());
expect(queryString).toContain(
`profileName=${encodeURIComponent(profileName)}`
);
});
it('reflects in the state from URL', function () {
const { getState } = _getStoreWithURL({
search: '?profileName=XXX',
});
expect(urlStateSelectors.getProfileNameFromUrl(getState())).toBe('XXX');
expect(urlStateSelectors.getProfileNameWithDefault(getState())).toBe('XXX');
});
it('provides default values for when no profile name is given', function () {
const { getState } = _getStoreWithURL();
expect(urlStateSelectors.getProfileNameFromUrl(getState())).toBe(null);
expect(urlStateSelectors.getProfileNameWithDefault(getState())).toBe(
'Firefox'
);
});
});
describe('ctxId', function () {
it('serializes the ctxId in the URL', function () {
const { profile } = addActiveTabInformationToProfile(
getProfileWithNiceTracks()
);
const { getState, dispatch } = _getStoreWithURL({}, profile);
const tabID = 123;
dispatch(changeTimelineTrackOrganization({ type: 'active-tab', tabID }));
const queryString = getQueryStringFromState(getState());
expect(queryString).toContain(`ctxId=${tabID}`);
});
it('reflects in the state from URL', function () {
const { profile } = addActiveTabInformationToProfile(
getProfileWithNiceTracks()
);
const { getState } = _getStoreWithURL(
{
search: '?ctxId=123&view=active-tab',
},
profile
);
expect(urlStateSelectors.getTimelineTrackOrganization(getState())).toEqual({
type: 'active-tab',
tabID: 123,
});
});
it('returns the full view when ctxId is not specified', function () {
const { profile } = addActiveTabInformationToProfile(
getProfileWithNiceTracks()
);
const { getState } = _getStoreWithURL({}, profile);
expect(urlStateSelectors.getTimelineTrackOrganization(getState())).toEqual({
type: 'full',
});
});
it('should use the finalizeActiveTabProfileView path and initialize active tab profile view state', function () {
const {
profile,
parentInnerWindowIDsWithChildren,
iframeInnerWindowIDsWithChild,
} = addActiveTabInformationToProfile(getProfileWithNiceTracks());
profile.threads[0].frameTable.innerWindowID[0] =
parentInnerWindowIDsWithChildren;
profile.threads[1].frameTable.innerWindowID[0] =
iframeInnerWindowIDsWithChild;
const { getState } = _getStoreWithURL(
{
search: '?view=active-tab&ctxId=123',
},
profile
);
const globalTracks = getActiveTabGlobalTracks(getState());
expect(globalTracks.length).toBe(1);
expect(globalTracks).toEqual([
{
type: 'tab',
threadIndexes: new Set([0]),
threadsKey: 0,
},
]);
// TODO: Resource track type will be changed soon.
const resourceTracks = getActiveTabResourceTracks(getState());
expect(resourceTracks).toEqual([
{
name: 'Page #2',
type: 'sub-frame',
threadIndex: 1,
},
]);
});
it('should remove other full view url states if present', function () {
const { profile } = addActiveTabInformationToProfile(
getProfileWithNiceTracks()
);
const { getState } = _getStoreWithURL(
{
search:
'?ctxId=123&view=active-tab&globalTrackOrder=3w0&hiddenGlobalTracks=45&hiddenLocalTracksByPid=111-1&thread=0',
},
profile
);
const newUrl = new URL(
urlFromState(urlStateSelectors.getUrlState(getState())),
'https://profiler.firefox.com'
);
// The url states that are relevant to full view should be stripped out.
expect(newUrl.search).toEqual(
`?ctxId=123&thread=0&v=${CURRENT_URL_VERSION}&view=active-tab`
);
});
it('if not present in the URL, still manages to load the active tab view', function () {
const { profile } = addActiveTabInformationToProfile(
getProfileWithNiceTracks()
);
const { getState } = _getStoreWithURL(
{
search: '?view=active-tab',
},
profile
);
expect(getView(getState()).phase).toEqual('DATA_LOADED');
expect(urlStateSelectors.getTimelineTrackOrganization(getState())).toEqual({
type: 'active-tab',
tabID: null,
});
});
});
describe('committed ranges', function () {
describe('serialization', () => {
it('serializes when there is no range', () => {
const { getState } = _getStoreWithURL();
const queryString = getQueryStringFromState(getState());
expect(queryString).not.toContain(`range=`);
});
it('serializes when there is 1 range', () => {
const { getState, dispatch } = _getStoreWithURL();
dispatch(commitRange(1514.587845, 25300));
const queryString = getQueryStringFromState(getState());
expect(queryString).toContain(`range=1514m23786`); // 1.514s + 23786ms
});
it('serializes when rounding down the start', () => {
const { getState, dispatch } = _getStoreWithURL();
dispatch(commitRange(1510.58, 1519.59));
const queryString = getQueryStringFromState(getState());
expect(queryString).toContain(`range=1510m10`); // 1.510s + 10ms
});
it('serializes when the duration is 0', () => {
const { getState, dispatch } = _getStoreWithURL();
dispatch(commitRange(1514, 1514));
const queryString = getQueryStringFromState(getState());
// In the following regexp we want to especially assert that the duration
// isn't 0. That's why there's this negative look-ahead assertion.
// Therefore here we're matching a start at 1.514s, and a non-zero
// duration.
expect(queryString).toMatch(/range=1514000000n(?!0)/);
});
it('serializes when there are several ranges', () => {
const { getState, dispatch } = _getStoreWithURL();
dispatch(commitRange(1514.587845, 25300));
dispatch(commitRange(1800, 1800.1));
const queryString = getQueryStringFromState(getState());
// 1- 1.5145s + 23786ms
// 2- 1.8s + 100µs
expect(queryString).toContain(`range=1514m23786~1800000u100`);
});
it('serializes when there is a small range', () => {
const { getState, dispatch } = _getStoreWithURL();
dispatch(commitRange(1000.08, 1000.09));
const queryString = getQueryStringFromState(getState());
expect(queryString).toContain(`range=1000080u10`); // 1s and 80µs + 10µs
});
it('serializes when there is a very small range', () => {
const { getState, dispatch } = _getStoreWithURL();
dispatch(commitRange(1000.00008, 1000.0001));
const queryString = getQueryStringFromState(getState());
expect(queryString).toContain(`range=1000000080n20`); // 1s and 80ns + 20ns
});
});
describe('parsing', () => {
it('deserializes when there is no range', () => {
const { getState } = _getStoreWithURL();
expect(urlStateSelectors.getAllCommittedRanges(getState())).toEqual([]);
});
it('deserializes when there is 1 range', () => {
const { getState } = _getStoreWithURL({
search: '?range=1600m5000',
});
expect(urlStateSelectors.getAllCommittedRanges(getState())).toEqual([
{ start: 1600, end: 6600 },
]);
});
it('deserializes when there are several ranges', () => {
const { getState } = _getStoreWithURL({
search: '?range=1600m5000~2245m24',
});
expect(urlStateSelectors.getAllCommittedRanges(getState())).toEqual([
{ start: 1600, end: 6600 },
{ start: 2245, end: 2269 },
]);
});
it('deserializes when there is a small range', () => {
const { getState } = _getStoreWithURL({
search: '?range=1678900u100',
});
expect(urlStateSelectors.getAllCommittedRanges(getState())).toEqual([
{ start: 1678.9, end: 1679 },
]);
});
it('deserializes when there is a very small range', () => {
const { getState } = _getStoreWithURL({
search: '?range=1678123900n100',
});
const [committedRange] =
urlStateSelectors.getAllCommittedRanges(getState());
expect(committedRange.start).toBeCloseTo(1678.1239);
expect(committedRange.end).toBeCloseTo(1678.124);
});
it('is permissive with invalid input', () => {
jest.spyOn(console, 'error').mockImplementation(() => {});
const { getState } = _getStoreWithURL({
search: '?range=invalid~2245m24',
});
expect(urlStateSelectors.getAllCommittedRanges(getState())).toEqual([
{ start: 2245, end: 2269 },
]);
expect(console.error).toHaveBeenCalled();
});
});
describe('serializing and parsing', () => {
function getQueryStringForRanges(
ranges: $ReadOnlyArray<StartEndRange>
): string {
const { getState, dispatch } = _getStoreWithURL();
ranges.forEach(({ start, end }) => dispatch(commitRange(start, end)));
return getQueryStringFromState(getState());
}
function setup(ranges: $ReadOnlyArray<StartEndRange>) {
const queryString = getQueryStringForRanges(ranges);
return _getStoreWithURL({
search: '?' + queryString,
});
}
it('can parse the serialized values', () => {
const { getState } = setup([
{ start: 1514.587845, end: 25300 },
{ start: 1800, end: 1800.1 },
{ start: 1800.00008, end: 1800.0001 },
]);
expect(urlStateSelectors.getAllCommittedRanges(getState())).toEqual([
{ start: 1514, end: 25300 },
{ start: 1800, end: 1800.1 },
{ start: 1800.00008, end: 1800.0001 },
]);
});
it('will round values near the threshold', () => {
const { getState } = setup([{ start: 50000, end: 50009.9 }]);
expect(urlStateSelectors.getAllCommittedRanges(getState())).toEqual([
{ start: 50000, end: 50010 },
]);
});
it('supports negative start values', () => {
const { getState } = setup([{ start: -1000, end: 1000 }]);
expect(urlStateSelectors.getAllCommittedRanges(getState())).toEqual([
{ start: -1000, end: 1000 },
]);
});
it('supports zero start values', () => {
const { getState } = setup([{ start: 0, end: 1000 }]);
expect(urlStateSelectors.getAllCommittedRanges(getState())).toEqual([
{ start: 0, end: 1000 },
]);
});
});
});
describe('implementation', function () {
function setup(settings, profile) {
const store = _getStoreWithURL(settings, profile);
function getQueryString() {
return getQueryStringFromState(store.getState());
}
return {
...store,
getQueryString,
};
}
describe('serializing', () => {
it('skips the value "combined"', () => {
const { dispatch, getQueryString } = setup();
expect(getQueryString()).not.toContain('implementation=');
dispatch(changeImplementationFilter('combined'));
expect(getQueryString()).not.toContain('implementation=');
});
it.each(['js', 'cpp'])(
'can serialize the value "%s"',
(implementationFilter) => {
const { getQueryString, dispatch } = setup();
dispatch(changeImplementationFilter(implementationFilter));
expect(getQueryString()).toContain(
`implementation=${implementationFilter}`
);
}
);
});
describe('parsing', () => {
it('deserializes when there is no implementation value', () => {
const { getState } = setup();
expect(urlStateSelectors.getImplementationFilter(getState())).toBe(
'combined'
);
});
it.each(['js', 'cpp'])(
'deserializes known value %s',
(implementationFilter) => {
const { getState } = setup({
search: `?implementation=${implementationFilter}`,
});
expect(urlStateSelectors.getImplementationFilter(getState())).toBe(
implementationFilter
);
}
);
it('deserializes unknown values', () => {
const { getState } = setup({
search: '?implementation=foobar',
});
expect(urlStateSelectors.getImplementationFilter(getState())).toBe(
'combined'
);
});
});
});
describe('url upgrading', function () {
describe('version 1: legacy URL serialized call tree filters', function () {
/**
* Originally transform stacks were called call tree filters. This test asserts that
* the upgrade process works correctly.
*/
it('can upgrade callTreeFilters to transforms', function () {
const { getState } = _getStoreWithURL({
search:
'?callTreeFilters=prefix-012~prefixjs-123~postfix-234~postfixjs-345',
v: false,
});
const transforms = selectedThreadSelectors.getTransformStack(getState());
expect(transforms).toEqual([
{
type: 'focus-subtree',
callNodePath: [0, 1, 2],
implementation: 'combined',
inverted: false,
},
{
type: 'focus-subtree',
callNodePath: [1, 2, 3],
implementation: 'js',
inverted: false,
},
{
type: 'focus-subtree',
callNodePath: [2, 3, 4],
implementation: 'combined',
inverted: true,
},
{
type: 'focus-subtree',
callNodePath: [3, 4, 5],
implementation: 'js',
inverted: true,
},
]);
});
});
describe('version 2: split apart timeline tab', function () {
it('switches to the stack chart when given a timeline tab', function () {
const { getState } = _getStoreWithURL({
pathname: '/public/e71ce9584da34298627fb66ac7f2f245ba5edbf5/timeline/',
v: 1,
});
expect(urlStateSelectors.getSelectedTab(getState())).toBe('stack-chart');
});
it('switches to the marker-table when given a markers tab', function () {
const { getState } = _getStoreWithURL({
pathname: '/public/e71ce9584da34298627fb66ac7f2f245ba5edbf5/markers/',
v: false,
});
expect(urlStateSelectors.getSelectedTab(getState())).toBe('marker-table');
});
});
describe('version 3: remove platform only option', function () {
it('switches to the stack chart when given a timeline tab', function () {
const { getState } = _getStoreWithURL({
pathname: '/public/e71ce9584da34298627fb66ac7f2f245ba5edbf5/timeline/',
search: '?hidePlatformDetails',
v: 2,
});
expect(urlStateSelectors.getImplementationFilter(getState())).toBe('js');
});
});
describe('version 4: Add relevantForJs frames to JS callNodePaths', function () {
it('can upgrade a simple stack with one relevantForJs frame in the middle', function () {
const {
profile,
funcNamesDictPerThread: [funcNamesDictPerThread],
} = getProfileFromTextSamples(`
A
B.js
C.js
DrelevantForJs
E
F
G.js
`);
profile.threads[0].funcTable.relevantForJS[
funcNamesDictPerThread.DrelevantForJs
] = true;
const callNodePathBefore = [
funcNamesDictPerThread['B.js'],
funcNamesDictPerThread['C.js'],
funcNamesDictPerThread['G.js'],
];
// Upgrader
const callNodeString = encodeUintArrayForUrlComponent(callNodePathBefore);
// focus-subtree transform with js implementation filter.
const transformString = 'f-js-' + callNodeString;
const { query } = upgradeLocationToCurrentVersion(
{
pathname: '',
hash: '',
query: {
thread: '0',
implementation: 'js',
transforms: transformString,
v: '3',
},
},
profile