-
-
Notifications
You must be signed in to change notification settings - Fork 221
/
sentry_navigator_observer_test.dart
786 lines (629 loc) · 22.1 KB
/
sentry_navigator_observer_test.dart
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
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:sentry_flutter/src/sentry_native.dart';
import 'package:sentry_flutter/src/sentry_native_channel.dart';
import 'package:sentry/src/sentry_tracer.dart';
import 'mocks.dart';
import 'mocks.mocks.dart';
void main() {
late Fixture fixture;
PageRoute<dynamic> route(RouteSettings? settings) => PageRouteBuilder<void>(
pageBuilder: (_, __, ___) => Container(),
settings: settings,
);
void _whenAnyStart(MockHub mockHub, ISentrySpan thenReturnSpan) {
when(mockHub.startTransactionWithContext(
any,
bindToScope: anyNamed('bindToScope'),
waitForChildren: anyNamed('waitForChildren'),
autoFinishAfter: anyNamed('autoFinishAfter'),
trimEnd: anyNamed('trimEnd'),
onFinish: anyNamed('onFinish'),
customSamplingContext: anyNamed('customSamplingContext'),
startTimestamp: anyNamed('startTimestamp'),
)).thenReturn(thenReturnSpan);
}
setUp(() {
SentryNative().reset();
fixture = Fixture();
});
tearDown(() {
SentryNative().reset();
});
group('NativeFrames', () {
test('transaction start begins frames collection', () async {
final currentRoute = route(RouteSettings(name: 'Current Route'));
final mockHub = _MockHub();
final native = SentryNative();
final mockNativeChannel = MockNativeChannel();
native.setNativeChannel(mockNativeChannel);
final tracer = getMockSentryTracer();
_whenAnyStart(mockHub, tracer);
final sut = fixture.getSut(hub: mockHub);
sut.didPush(currentRoute, null);
// Handle internal async method calls.
await Future.delayed(const Duration(milliseconds: 10), () {
expect(mockNativeChannel.numberOfBeginNativeFramesCalls, 1);
});
});
test('transaction finish adds native frames to tracer', () async {
final currentRoute = route(RouteSettings(name: 'Current Route'));
final options = SentryOptions(dsn: fakeDsn);
options.tracesSampleRate = 1;
final hub = Hub(options);
final nativeFrames = NativeFrames(3, 2, 1);
final mockNativeChannel = MockNativeChannel();
mockNativeChannel.nativeFrames = nativeFrames;
final mockNative = SentryNative();
mockNative.setNativeChannel(mockNativeChannel);
final sut = fixture.getSut(
hub: hub,
autoFinishAfter: Duration(milliseconds: 50),
);
sut.didPush(currentRoute, null);
// Get ref to created transaction
// ignore: invalid_use_of_internal_member
SentryTracer? actualTransaction;
hub.configureScope((scope) {
// ignore: invalid_use_of_internal_member
actualTransaction = scope.span as SentryTracer;
});
await Future<void>.delayed(Duration(milliseconds: 500));
expect(mockNativeChannel.numberOfEndNativeFramesCalls, 1);
final measurements = actualTransaction?.measurements ?? {};
expect(measurements.length, 3);
final expectedTotal = SentryMeasurement.totalFrames(3);
final expectedSlow = SentryMeasurement.slowFrames(2);
final expectedFrozen = SentryMeasurement.frozenFrames(1);
for (final item in measurements.entries) {
final measurement = item.value;
if (measurement.name == expectedTotal.name) {
expect(measurement.value, expectedTotal.value);
} else if (measurement.name == expectedSlow.name) {
expect(measurement.value, expectedSlow.value);
} else if (measurement.name == expectedFrozen.name) {
expect(measurement.value, expectedFrozen.value);
}
}
});
});
group('$SentryNavigatorObserver', () {
test('didPush starts transaction', () {
const name = 'Current Route';
final currentRoute = route(RouteSettings(name: name));
const op = 'navigation';
final hub = _MockHub();
final span = getMockSentryTracer(name: name);
when(span.context).thenReturn(SentrySpanContext(operation: op));
_whenAnyStart(hub, span);
final sut = fixture.getSut(
hub: hub,
autoFinishAfter: Duration(seconds: 5),
);
sut.didPush(currentRoute, null);
final context = verify(hub.startTransactionWithContext(
captureAny,
waitForChildren: true,
autoFinishAfter: anyNamed('autoFinishAfter'),
trimEnd: true,
onFinish: anyNamed('onFinish'),
)).captured.single as SentryTransactionContext;
expect(context.name, name);
hub.configureScope((scope) {
expect(scope.span?.context.operation, op);
expect(scope.span, span);
});
});
test('do not bind transaction to scope if no op', () {
final currentRoute = route(RouteSettings(name: 'Current Route'));
final hub = _MockHub();
final span = NoOpSentrySpan();
_whenAnyStart(hub, span);
final sut = fixture.getSut(
hub: hub,
autoFinishAfter: Duration(seconds: 5),
);
sut.didPush(currentRoute, null);
verify(hub.startTransactionWithContext(
any,
waitForChildren: true,
autoFinishAfter: Duration(seconds: 5),
trimEnd: true,
onFinish: anyNamed('onFinish'),
));
hub.configureScope((scope) {
expect(scope.span, null);
expect(scope.transaction, null);
});
});
test('route with empty name does not start transaction', () {
final currentRoute = route(null);
final hub = _MockHub();
final span = getMockSentryTracer();
when(span.context).thenReturn(SentrySpanContext(operation: 'op'));
_whenAnyStart(hub, span);
final sut = fixture.getSut(hub: hub);
sut.didPush(currentRoute, null);
verifyNever(hub.startTransactionWithContext(
any,
waitForChildren: true,
autoFinishAfter: anyNamed('autoFinishAfter'),
trimEnd: true,
onFinish: anyNamed('onFinish'),
));
hub.configureScope((scope) {
expect(scope.span, null);
});
});
test('no transaction on opt-out', () {
final currentRoute = route(RouteSettings(name: 'Current Route'));
final hub = _MockHub();
final span = getMockSentryTracer();
when(span.context).thenReturn(SentrySpanContext(operation: 'op'));
_whenAnyStart(hub, span);
final sut = fixture.getSut(hub: hub, enableAutoTransactions: false);
sut.didPush(currentRoute, null);
verifyNever(hub.startTransactionWithContext(
any,
waitForChildren: true,
autoFinishAfter: anyNamed('autoFinishAfter'),
trimEnd: true,
onFinish: anyNamed('onFinish'),
));
hub.configureScope((scope) {
expect(scope.span, null);
});
});
test('do not bind to scope if already set', () {
final currentRoute = route(RouteSettings(name: 'Current Route'));
final hub = _MockHub();
hub.scope.span = NoOpSentrySpan();
final span = getMockSentryTracer();
when(span.context).thenReturn(SentrySpanContext(operation: 'op'));
_whenAnyStart(hub, span);
final sut = fixture.getSut(hub: hub);
sut.didPush(currentRoute, null);
verify(hub.startTransactionWithContext(
any,
waitForChildren: true,
autoFinishAfter: anyNamed('autoFinishAfter'),
trimEnd: true,
onFinish: anyNamed('onFinish'),
));
hub.configureScope((scope) {
expect(scope.span, NoOpSentrySpan());
});
});
test('didPush finishes previous transaction', () {
final firstRoute = route(RouteSettings(name: 'First Route'));
final secondRoute = route(RouteSettings(name: 'Second Route'));
final hub = _MockHub();
final span = getMockSentryTracer();
when(span.context).thenReturn(SentrySpanContext(operation: 'op'));
when(span.status).thenReturn(null);
_whenAnyStart(hub, span);
final sut = fixture.getSut(hub: hub);
sut.didPush(firstRoute, null);
sut.didPush(secondRoute, firstRoute);
verify(span.status = SpanStatus.ok());
verify(span.finish());
});
test('didPop finishes transaction', () async {
final currentRoute = route(RouteSettings(name: 'Current Route'));
final hub = _MockHub();
final span = getMockSentryTracer();
when(span.context).thenReturn(SentrySpanContext(operation: 'op'));
when(span.status).thenReturn(null);
_whenAnyStart(hub, span);
final sut = fixture.getSut(hub: hub);
sut.didPush(currentRoute, null);
sut.didPop(currentRoute, null);
verify(span.status = SpanStatus.ok());
verify(span.finish());
});
test('didPop re-starts previous', () {
final previousRoute = route(RouteSettings(name: 'Previous Route'));
final currentRoute = route(RouteSettings(name: 'Current Route'));
final hub = _MockHub();
final previousSpan = getMockSentryTracer();
when(previousSpan.context).thenReturn(SentrySpanContext(operation: 'op'));
when(previousSpan.status).thenReturn(null);
_whenAnyStart(hub, previousSpan);
final sut = fixture.getSut(hub: hub);
sut.didPop(currentRoute, previousRoute);
verify(hub.startTransactionWithContext(
any,
waitForChildren: true,
autoFinishAfter: anyNamed('autoFinishAfter'),
trimEnd: true,
onFinish: anyNamed('onFinish'),
));
hub.configureScope((scope) {
expect(scope.span, previousSpan);
});
});
test('route arguments are set on transaction', () {
final arguments = {'foo': 'bar'};
final currentRoute = route(RouteSettings(
name: 'Current Route',
arguments: arguments,
));
final hub = _MockHub();
final span = getMockSentryTracer();
when(span.context).thenReturn(SentrySpanContext(operation: 'op'));
when(span.status).thenReturn(null);
_whenAnyStart(hub, span);
final sut = fixture.getSut(hub: hub);
sut.didPush(currentRoute, null);
verify(span.setData('route_settings_arguments', arguments));
});
test('flutter root name is replaced', () {
final rootRoute = route(RouteSettings(name: '/'));
final hub = _MockHub();
final span = getMockSentryTracer(name: '/');
when(span.context).thenReturn(SentrySpanContext(operation: 'op'));
_whenAnyStart(hub, span);
final sut = fixture.getSut(hub: hub);
sut.didPush(rootRoute, null);
final context = verify(hub.startTransactionWithContext(
captureAny,
waitForChildren: true,
autoFinishAfter: anyNamed('autoFinishAfter'),
trimEnd: true,
onFinish: anyNamed('onFinish'),
)).captured.single as SentryTransactionContext;
expect(context.name, 'root ("/")');
hub.configureScope((scope) {
expect(scope.span, span);
});
});
});
group('RouteObserverBreadcrumb', () {
test('happy path with string route agrument', () {
const fromRouteSettings = RouteSettings(
name: 'from',
arguments: 'PageTitle',
);
const toRouteSettings = RouteSettings(
name: 'to',
arguments: 'PageTitle2',
);
final breadcrumb = RouteObserverBreadcrumb(
navigationType: 'didPush',
from: fromRouteSettings,
to: toRouteSettings,
data: {'foo': 'bar'},
);
expect(breadcrumb.category, 'navigation');
expect(breadcrumb.data, <String, dynamic>{
'state': 'didPush',
'from': 'from',
'from_arguments': 'PageTitle',
'to': 'to',
'to_arguments': 'PageTitle2',
'data': {'foo': 'bar'},
});
});
test('happy path with map route agrument', () {
const fromRouteSettings = RouteSettings(
name: 'from',
arguments: 'PageTitle',
);
const toRouteSettings = RouteSettings(
name: 'to',
arguments: {
'foo': 123,
'bar': 'foobar',
},
);
final breadcrumb = RouteObserverBreadcrumb(
navigationType: 'didPush',
from: fromRouteSettings,
to: toRouteSettings,
);
expect(breadcrumb.category, 'navigation');
expect(breadcrumb.data, <String, dynamic>{
'state': 'didPush',
'from': 'from',
'from_arguments': 'PageTitle',
'to': 'to',
'to_arguments': {
'foo': '123',
'bar': 'foobar',
},
});
});
test('routes are null', () {
// both routes are null
final breadcrumb = RouteObserverBreadcrumb(
navigationType: 'didPush',
from: null,
to: null,
);
expect(breadcrumb.category, 'navigation');
expect(breadcrumb.data, <String, dynamic>{
'state': 'didPush',
});
});
test('route arguments are null', () {
const fromRouteSettings = RouteSettings(
name: 'from',
);
const toRouteSettings = RouteSettings(
name: 'to',
arguments: null,
);
// both routes are null
final breadcrumb = RouteObserverBreadcrumb(
navigationType: 'didPush',
from: fromRouteSettings,
to: toRouteSettings,
);
expect(breadcrumb.category, 'navigation');
expect(breadcrumb.data, <String, dynamic>{
'state': 'didPush',
'from': 'from',
'to': 'to',
});
});
test('route names are null', () {
const fromRouteSettings = RouteSettings(name: null, arguments: 'foo');
const toRouteSettings = RouteSettings(
name: null,
arguments: {
'foo': 123,
},
);
// both routes are null
final breadcrumb = RouteObserverBreadcrumb(
navigationType: 'didPush',
from: fromRouteSettings,
to: toRouteSettings,
);
expect(breadcrumb.category, 'navigation');
expect(breadcrumb.data, <String, dynamic>{
'state': 'didPush',
'from_arguments': 'foo',
'to_arguments': {
'foo': '123',
},
});
});
});
group('SentryNavigatorObserver', () {
RouteSettings routeSettings(String? name, [Object? arguments]) =>
RouteSettings(name: name, arguments: arguments);
test('Test recording of Breadcrumbs', () {
final hub = _MockHub();
_whenAnyStart(hub, NoOpSentrySpan());
final observer = fixture.getSut(hub: hub);
final to = routeSettings('to', 'foobar');
final previous = routeSettings('previous', 'foobar');
observer.didPush(route(to), route(previous));
final dynamic breadcrumb =
verify(hub.addBreadcrumb(captureAny)).captured.single as Breadcrumb;
expect(
breadcrumb.data,
RouteObserverBreadcrumb(
navigationType: 'didPush',
from: previous,
to: to,
).data,
);
});
test('No arguments', () {
final hub = _MockHub();
_whenAnyStart(hub, NoOpSentrySpan());
final observer = fixture.getSut(hub: hub);
final to = routeSettings('to');
final previous = routeSettings('previous');
observer.didPush(route(to), route(previous));
final dynamic breadcrumb =
verify(hub.addBreadcrumb(captureAny)).captured.single as Breadcrumb;
expect(
breadcrumb.data,
RouteObserverBreadcrumb(
navigationType: 'didPush',
from: previous,
to: to,
).data,
);
});
test('No arguments & no name', () {
final hub = _MockHub();
_whenAnyStart(hub, NoOpSentrySpan());
final observer = fixture.getSut(hub: hub);
final to = route(null);
final previous = route(null);
observer.didReplace(newRoute: to, oldRoute: previous);
final dynamic breadcrumb =
verify(hub.addBreadcrumb(captureAny)).captured.single as Breadcrumb;
expect(
breadcrumb.data,
RouteObserverBreadcrumb(
navigationType: 'didReplace',
).data,
);
});
test('No RouteSettings', () {
PageRoute<dynamic> route() => PageRouteBuilder<void>(
pageBuilder: (_, __, ___) => Container(),
);
final hub = _MockHub();
final observer = fixture.getSut(hub: hub);
final to = route();
final previous = route();
observer.didPop(to, previous);
final dynamic breadcrumb =
verify(hub.addBreadcrumb(captureAny)).captured.single as Breadcrumb;
expect(
breadcrumb.data,
RouteObserverBreadcrumb(
navigationType: 'didPop',
).data,
);
});
test('route name as transaction', () {
final hub = _MockHub();
_whenAnyStart(hub, NoOpSentrySpan());
final observer = fixture.getSut(
hub: hub,
setRouteNameAsTransaction: true,
);
final to = routeSettings('to');
final previous = routeSettings('previous');
observer.didPush(route(to), route(previous));
expect(hub.scope.transaction, 'to');
observer.didPop(route(to), route(previous));
expect(hub.scope.transaction, 'previous');
observer.didReplace(newRoute: route(to), oldRoute: route(previous));
expect(hub.scope.transaction, 'to');
});
test('route name does nothing if null', () {
final hub = _MockHub();
_whenAnyStart(hub, NoOpSentrySpan());
final observer = fixture.getSut(
hub: hub,
setRouteNameAsTransaction: true,
);
hub.scope.transaction = 'foo bar';
final to = routeSettings(null);
final previous = routeSettings(null);
observer.didPush(route(to), route(previous));
expect(hub.scope.transaction, 'foo bar');
});
test('disabled route as transaction', () {
final hub = _MockHub();
_whenAnyStart(hub, NoOpSentrySpan());
final observer =
fixture.getSut(hub: hub, setRouteNameAsTransaction: false);
final to = routeSettings('to');
final previous = routeSettings('previous');
observer.didPush(route(to), route(previous));
expect(hub.scope.transaction, null);
observer.didPop(route(to), route(previous));
expect(hub.scope.transaction, null);
observer.didReplace(newRoute: route(to), oldRoute: route(previous));
expect(hub.scope.transaction, null);
});
test('modifying route settings', () {
final hub = _MockHub();
_whenAnyStart(hub, NoOpSentrySpan());
final observer = fixture.getSut(
hub: hub,
routeNameExtractor: (settings) {
if (settings != null && settings.name == 'to') {
return settings.copyWith(name: 'changed_to');
}
return settings;
});
final to = routeSettings('to', 'foobar');
final previous = routeSettings('previous', 'foobar');
observer.didPush(route(to), route(previous));
final dynamic breadcrumb =
verify(hub.addBreadcrumb(captureAny)).captured.single as Breadcrumb;
expect(
breadcrumb.data,
RouteObserverBreadcrumb(
navigationType: 'didPush',
from: previous,
to: to.copyWith(name: 'changed_to'),
).data,
);
});
test('add additional data', () {
final hub = _MockHub();
_whenAnyStart(hub, NoOpSentrySpan());
final observer = fixture.getSut(
hub: hub,
additionalInfoProvider: (from, to) {
return <String, dynamic>{'foo': 'bar'};
});
final to = routeSettings('to', 'foobar');
final previous = routeSettings('previous', 'foobar');
observer.didPush(route(to), route(previous));
final dynamic breadcrumb =
verify(hub.addBreadcrumb(captureAny)).captured.single as Breadcrumb;
expect(
breadcrumb.data,
RouteObserverBreadcrumb(
navigationType: 'didPush',
from: previous,
to: to.copyWith(name: 'to'),
data: {'foo': 'bar'},
).data,
);
});
test('route name as transaction with routeNameExtractor', () {
final hub = _MockHub();
_whenAnyStart(hub, NoOpSentrySpan());
final observer = fixture.getSut(
hub: hub,
setRouteNameAsTransaction: true,
routeNameExtractor: (settings) =>
settings?.copyWith(name: '${settings.name}_test'));
final to = routeSettings('to');
final previous = routeSettings('previous');
observer.didPush(route(to), route(previous));
expect(hub.scope.transaction, 'to_test');
observer.didPop(route(to), route(previous));
expect(hub.scope.transaction, 'previous_test');
observer.didReplace(newRoute: route(to), oldRoute: route(previous));
expect(hub.scope.transaction, 'to_test');
});
});
}
class Fixture {
SentryNavigatorObserver getSut({
required Hub hub,
bool enableAutoTransactions = true,
Duration autoFinishAfter = const Duration(seconds: 3),
bool setRouteNameAsTransaction = false,
RouteNameExtractor? routeNameExtractor,
AdditionalInfoExtractor? additionalInfoProvider,
}) {
return SentryNavigatorObserver(
hub: hub,
enableAutoTransactions: enableAutoTransactions,
autoFinishAfter: autoFinishAfter,
setRouteNameAsTransaction: setRouteNameAsTransaction,
routeNameExtractor: routeNameExtractor,
additionalInfoProvider: additionalInfoProvider,
);
}
SentrySpanContext mockContext() {
return SentrySpanContext(operation: 'op');
}
}
class _MockHub extends MockHub {
@override
final options = SentryOptions(dsn: fakeDsn);
late final scope = Scope(options);
@override
FutureOr<void> configureScope(ScopeCallback? callback) async {
await callback?.call(scope);
}
}
ISentrySpan getMockSentryTracer({String? name}) {
final tracer = MockSentryTracer();
when(tracer.name).thenReturn(name ?? 'name');
return tracer;
}
extension RouteSettingsExtensions on RouteSettings {
/// Creates a copy of this route settings object with the given fields
/// replaced with the new values.
/// Flutter 3.6 beta removed copyWith but we use it for testing
RouteSettings copyWith({
String? name,
Object? arguments,
}) {
return RouteSettings(
name: name ?? this.name,
arguments: arguments ?? this.arguments,
);
}
}