Make Route dispatching memory events. (#133721)
This commit is contained in:
parent
727b9fd106
commit
956999a4b9
@ -140,7 +140,15 @@ abstract class Route<T> {
|
|||||||
///
|
///
|
||||||
/// If the [settings] are not provided, an empty [RouteSettings] object is
|
/// If the [settings] are not provided, an empty [RouteSettings] object is
|
||||||
/// used instead.
|
/// 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.
|
/// The navigator that the route is in, if any.
|
||||||
NavigatorState? get navigator => _navigator;
|
NavigatorState? get navigator => _navigator;
|
||||||
@ -503,6 +511,9 @@ abstract class Route<T> {
|
|||||||
void dispose() {
|
void dispose() {
|
||||||
_navigator = null;
|
_navigator = null;
|
||||||
_restorationScopeId.dispose();
|
_restorationScopeId.dispose();
|
||||||
|
if (kFlutterMemoryAllocationsEnabled) {
|
||||||
|
MemoryAllocations.instance.dispatchObjectDisposed(object: this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Whether this route is the top-most route on the navigator.
|
/// Whether this route is the top-most route on the navigator.
|
||||||
|
@ -338,7 +338,10 @@ void main() {
|
|||||||
// TODO(polina-c): remove after fixing
|
// TODO(polina-c): remove after fixing
|
||||||
// https://github.com/flutter/flutter/issues/133695
|
// https://github.com/flutter/flutter/issues/133695
|
||||||
leakTrackingTestConfig: const LeakTrackingTestConfig(
|
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 {
|
testWidgetsWithLeakTracking('Make sure initialRoute is only used the first time', (WidgetTester tester) async {
|
||||||
|
@ -10,6 +10,7 @@ import 'package:flutter/rendering.dart';
|
|||||||
import 'package:flutter/scheduler.dart';
|
import 'package:flutter/scheduler.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
|
||||||
|
|
||||||
import 'navigator_utils.dart';
|
import 'navigator_utils.dart';
|
||||||
import 'observer_tester.dart';
|
import 'observer_tester.dart';
|
||||||
@ -627,6 +628,41 @@ void main() {
|
|||||||
expect(observations[2].previous, '/A');
|
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 {
|
testWidgets('Route didAdd and dispose in same frame work', (WidgetTester tester) async {
|
||||||
// Regression Test for https://github.com/flutter/flutter/issues/61346.
|
// Regression Test for https://github.com/flutter/flutter/issues/61346.
|
||||||
Widget buildNavigator() {
|
Widget buildNavigator() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user