Skip to content

Commit

Permalink
Also set current route name on replace
Browse files Browse the repository at this point in the history
  • Loading branch information
denrase committed Oct 9, 2023
1 parent 3cef7aa commit 5975e5c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
18 changes: 13 additions & 5 deletions flutter/lib/src/navigation/sentry_navigator_observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
super.didPush(route, previousRoute);

_setCurrentRoute(route);
_setCurrentRouteName(route);
_setCurrentRouteNameAsTransaction(route);

_addBreadcrumb(
type: 'didPush',
Expand All @@ -113,7 +114,9 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
super.didReplace(newRoute: newRoute, oldRoute: oldRoute);

_setCurrentRoute(newRoute);
_setCurrentRouteName(newRoute);
_setCurrentRouteNameAsTransaction(newRoute);

_addBreadcrumb(
type: 'didReplace',
from: oldRoute?.settings,
Expand All @@ -125,7 +128,9 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
super.didPop(route, previousRoute);

_setCurrentRoute(previousRoute);
_setCurrentRouteName(previousRoute);
_setCurrentRouteNameAsTransaction(previousRoute);

_addBreadcrumb(
type: 'didPop',
from: route.settings,
Expand Down Expand Up @@ -156,7 +161,11 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
?.name;
}

Future<void> _setCurrentRoute(Route<dynamic>? route) async {
Future<void> _setCurrentRouteName(Route<dynamic>? route) async {
_currentRouteName = _getRouteName(route);
}

Future<void> _setCurrentRouteNameAsTransaction(Route<dynamic>? route) async {
final name = _getRouteName(route);
if (name == null) {
return;
Expand Down Expand Up @@ -212,7 +221,6 @@ class SentryNavigatorObserver extends RouteObserver<PageRoute<dynamic>> {
}
},
);
_currentRouteName = name;

// if _enableAutoTransactions is enabled but there's no traces sample rate
if (_transaction is NoOpSentrySpan) {
Expand Down
49 changes: 48 additions & 1 deletion flutter/test/sentry_navigator_observer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ void main() {
});
});

test('exposes current route name', () {
test('didPush sets current route name', () {
const name = 'Current Route';
final currentRoute = route(RouteSettings(name: name));

Expand All @@ -387,6 +387,53 @@ void main() {

expect(SentryNavigatorObserver.currentRouteName, 'Current Route');
});

test('didReplace sets new route name', () {
const oldRouteName = 'Old Route';
final oldRoute = route(RouteSettings(name: oldRouteName));
const newRouteName = 'New Route';
final newRoute = route(RouteSettings(name: newRouteName));

const op = 'navigation';
final hub = _MockHub();
final span = getMockSentryTracer(name: oldRouteName);
when(span.context).thenReturn(SentrySpanContext(operation: op));
_whenAnyStart(hub, span);

final sut = fixture.getSut(
hub: hub,
autoFinishAfter: Duration(seconds: 5),
);

sut.didPush(oldRoute, null);
sut.didReplace(newRoute: newRoute, oldRoute: oldRoute);

expect(SentryNavigatorObserver.currentRouteName, 'New Route');
});

test('popRoute sets previous route name', () {
const oldRouteName = 'Old Route';
final oldRoute = route(RouteSettings(name: oldRouteName));
const newRouteName = 'New Route';
final newRoute = route(RouteSettings(name: newRouteName));

const op = 'navigation';
final hub = _MockHub();
final span = getMockSentryTracer(name: oldRouteName);
when(span.context).thenReturn(SentrySpanContext(operation: op));
when(span.status).thenReturn(null);
_whenAnyStart(hub, span);

final sut = fixture.getSut(
hub: hub,
autoFinishAfter: Duration(seconds: 5),
);

sut.didPush(oldRoute, null);
sut.didPop(newRoute, oldRoute);

expect(SentryNavigatorObserver.currentRouteName, 'Old Route');
});
});

group('RouteObserverBreadcrumb', () {
Expand Down

0 comments on commit 5975e5c

Please sign in to comment.