Unverified Commit 2369897f authored by Kostia Sokolovskyi's avatar Kostia Sokolovskyi Committed by GitHub

AppLifecycleListener should dispatch creation and disposal events. (#137840)

parent 8375dfde
...@@ -73,6 +73,15 @@ class AppLifecycleListener with WidgetsBindingObserver, Diagnosticable { ...@@ -73,6 +73,15 @@ class AppLifecycleListener with WidgetsBindingObserver, Diagnosticable {
this.onStateChange, this.onStateChange,
}) : binding = binding ?? WidgetsBinding.instance, }) : binding = binding ?? WidgetsBinding.instance,
_lifecycleState = (binding ?? WidgetsBinding.instance).lifecycleState { _lifecycleState = (binding ?? WidgetsBinding.instance).lifecycleState {
// TODO(polina-c): stop duplicating code across disposables
// https://github.com/flutter/flutter/issues/137435
if (kFlutterMemoryAllocationsEnabled) {
MemoryAllocations.instance.dispatchObjectCreated(
library: 'package:flutter/widgets.dart',
className: '$AppLifecycleListener',
object: this,
);
}
this.binding.addObserver(this); this.binding.addObserver(this);
} }
...@@ -174,6 +183,11 @@ class AppLifecycleListener with WidgetsBindingObserver, Diagnosticable { ...@@ -174,6 +183,11 @@ class AppLifecycleListener with WidgetsBindingObserver, Diagnosticable {
@mustCallSuper @mustCallSuper
void dispose() { void dispose() {
assert(_debugAssertNotDisposed()); assert(_debugAssertNotDisposed());
// TODO(polina-c): stop duplicating code across disposables
// https://github.com/flutter/flutter/issues/137435
if (kFlutterMemoryAllocationsEnabled) {
MemoryAllocations.instance.dispatchObjectDisposed(object: this);
}
binding.removeObserver(this); binding.removeObserver(this);
assert(() { assert(() {
_debugDisposed = true; _debugDisposed = true;
......
...@@ -10,7 +10,7 @@ import 'package:flutter_test/flutter_test.dart'; ...@@ -10,7 +10,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart'; import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
void main() { void main() {
late AppLifecycleListener listener; AppLifecycleListener? listener;
Future<void> setAppLifeCycleState(AppLifecycleState state) async { Future<void> setAppLifeCycleState(AppLifecycleState state) async {
final ByteData? message = const StringCodec().encodeMessage(state.toString()); final ByteData? message = const StringCodec().encodeMessage(state.toString());
...@@ -40,7 +40,8 @@ void main() { ...@@ -40,7 +40,8 @@ void main() {
}); });
tearDown(() { tearDown(() {
listener.dispose(); listener?.dispose();
listener = null;
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.instance; final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.instance;
binding.resetLifecycleState(); binding.resetLifecycleState();
binding.platformDispatcher.resetInitialLifecycleState(); binding.platformDispatcher.resetInitialLifecycleState();
...@@ -163,6 +164,16 @@ void main() { ...@@ -163,6 +164,16 @@ void main() {
await sendAppExitRequest(); await sendAppExitRequest();
expect(exitRequested, isTrue); expect(exitRequested, isTrue);
}); });
test('AppLifecycleListener dispatches memory events', () async {
await expectLater(
await memoryEvents(
() => AppLifecycleListener(binding: WidgetsBinding.instance).dispose(),
AppLifecycleListener,
),
areCreateAndDispose,
);
});
} }
class TestAppLifecycleListener extends AppLifecycleListener { class TestAppLifecycleListener extends AppLifecycleListener {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment