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 {
this.onStateChange,
}) : binding = binding ?? WidgetsBinding.instance,
_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);
}
......@@ -174,6 +183,11 @@ class AppLifecycleListener with WidgetsBindingObserver, Diagnosticable {
@mustCallSuper
void dispose() {
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);
assert(() {
_debugDisposed = true;
......
......@@ -10,7 +10,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
void main() {
late AppLifecycleListener listener;
AppLifecycleListener? listener;
Future<void> setAppLifeCycleState(AppLifecycleState state) async {
final ByteData? message = const StringCodec().encodeMessage(state.toString());
......@@ -40,7 +40,8 @@ void main() {
});
tearDown(() {
listener.dispose();
listener?.dispose();
listener = null;
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.instance;
binding.resetLifecycleState();
binding.platformDispatcher.resetInitialLifecycleState();
......@@ -163,6 +164,16 @@ void main() {
await sendAppExitRequest();
expect(exitRequested, isTrue);
});
test('AppLifecycleListener dispatches memory events', () async {
await expectLater(
await memoryEvents(
() => AppLifecycleListener(binding: WidgetsBinding.instance).dispose(),
AppLifecycleListener,
),
areCreateAndDispose,
);
});
}
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