Skip to content

Commit

Permalink
Make Route dispatching memory events. (#133721)
Browse files Browse the repository at this point in the history
  • Loading branch information
polina-c authored Aug 31, 2023
1 parent 727b9fd commit 956999a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
13 changes: 12 additions & 1 deletion packages/flutter/lib/src/widgets/navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,15 @@ abstract class Route<T> {
///
/// If the [settings] are not provided, an empty [RouteSettings] object is
/// used instead.
Route({ RouteSettings? settings }) : _settings = settings ?? const RouteSettings();
Route({ RouteSettings? settings }) : _settings = settings ?? const RouteSettings() {
if (kFlutterMemoryAllocationsEnabled) {
MemoryAllocations.instance.dispatchObjectCreated(
library: 'package:flutter/widgets.dart',
className: '$Route<$T>',
object: this,
);
}
}

/// The navigator that the route is in, if any.
NavigatorState? get navigator => _navigator;
Expand Down Expand Up @@ -503,6 +511,9 @@ abstract class Route<T> {
void dispose() {
_navigator = null;
_restorationScopeId.dispose();
if (kFlutterMemoryAllocationsEnabled) {
MemoryAllocations.instance.dispatchObjectDisposed(object: this);
}
}

/// Whether this route is the top-most route on the navigator.
Expand Down
5 changes: 4 additions & 1 deletion packages/flutter/test/material/app_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,10 @@ void main() {
// TODO(polina-c): remove after fixing
// https://github.com/flutter/flutter/issues/133695
leakTrackingTestConfig: const LeakTrackingTestConfig(
notDisposedAllowList: <String, int?> {'ValueNotifier<String?>': 3},
notDisposedAllowList: <String, int?> {
'ValueNotifier<String?>': 3,
'MaterialPageRoute<dynamic>': 3,
},
));

testWidgetsWithLeakTracking('Make sure initialRoute is only used the first time', (WidgetTester tester) async {
Expand Down
36 changes: 36 additions & 0 deletions packages/flutter/test/widgets/navigator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';

import 'navigator_utils.dart';
import 'observer_tester.dart';
Expand Down Expand Up @@ -627,6 +628,41 @@ void main() {
expect(observations[2].previous, '/A');
});

testWidgetsWithLeakTracking('$Route dispatches memory events', (WidgetTester tester) async {
Future<void> createAndDisposeRoute() async {
final GlobalKey<NavigatorState> nav = GlobalKey<NavigatorState>();
await tester.pumpWidget(
MaterialApp(
navigatorKey: nav,
home: const Scaffold(
body: Text('home'),
)
)
);

nav.currentState!.push(MaterialPageRoute<void>(builder: (_) => const Placeholder())); // This should create a route
await tester.pumpAndSettle();

nav.currentState!.pop();
await tester.pumpAndSettle(); // this should dispose the route.
}

final List<ObjectEvent> events = <ObjectEvent>[];
void listener(ObjectEvent event) {
if (event.object.runtimeType == MaterialPageRoute<void>) {
events.add(event);
}
}
MemoryAllocations.instance.addListener(listener);

await createAndDisposeRoute();
expect(events, hasLength(2));
expect(events.first, isA<ObjectCreated>());
expect(events.last, isA<ObjectDisposed>());

MemoryAllocations.instance.removeListener(listener);
});

testWidgets('Route didAdd and dispose in same frame work', (WidgetTester tester) async {
// Regression Test for https://github.com/flutter/flutter/issues/61346.
Widget buildNavigator() {
Expand Down

0 comments on commit 956999a

Please sign in to comment.