Unverified Commit 5b41ee55 authored by Alexander Dahlberg's avatar Alexander Dahlberg Committed by GitHub

Fixed leak and removed no-shuffle tag in binding_test.dart (#88784)

parent 1f5a1f74
......@@ -2,12 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// TODO(gspencergoog): Remove this tag once this test's state leaks/test
// dependencies have been fixed.
// https://github.com/flutter/flutter/issues/85160
// Fails with "flutter test --test-randomize-ordering-seed=382757700"
@Tags(<String>['no-shuffle'])
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
......@@ -56,6 +50,13 @@ void main() {
WidgetsFlutterBinding.ensureInitialized();
});
Future<void> setAppLifeCycleState(AppLifecycleState state) async {
final ByteData? message =
const StringCodec().encodeMessage(state.toString());
await ServicesBinding.instance!.defaultBinaryMessenger
.handlePlatformMessage('flutter/lifecycle', message, (_) {});
}
testWidgets('didHaveMemoryPressure callback', (WidgetTester tester) async {
final MemoryPressureObserver observer = MemoryPressureObserver();
WidgetsBinding.instance!.addObserver(observer);
......@@ -66,25 +67,22 @@ void main() {
});
testWidgets('handleLifecycleStateChanged callback', (WidgetTester tester) async {
final BinaryMessenger defaultBinaryMessenger = ServicesBinding.instance!.defaultBinaryMessenger;
final AppLifecycleStateObserver observer = AppLifecycleStateObserver();
WidgetsBinding.instance!.addObserver(observer);
ByteData message = const StringCodec().encodeMessage('AppLifecycleState.paused')!;
await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { });
setAppLifeCycleState(AppLifecycleState.paused);
expect(observer.lifecycleState, AppLifecycleState.paused);
message = const StringCodec().encodeMessage('AppLifecycleState.resumed')!;
await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { });
setAppLifeCycleState(AppLifecycleState.resumed);
expect(observer.lifecycleState, AppLifecycleState.resumed);
message = const StringCodec().encodeMessage('AppLifecycleState.inactive')!;
await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { });
setAppLifeCycleState(AppLifecycleState.inactive);
expect(observer.lifecycleState, AppLifecycleState.inactive);
message = const StringCodec().encodeMessage('AppLifecycleState.detached')!;
await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { });
setAppLifeCycleState(AppLifecycleState.detached);
expect(observer.lifecycleState, AppLifecycleState.detached);
setAppLifeCycleState(AppLifecycleState.resumed);
});
testWidgets('didPushRoute callback', (WidgetTester tester) async {
......@@ -111,7 +109,8 @@ void main() {
final ByteData message = const JSONMethodCodec().encodeMethodCall(
const MethodCall('pushRouteInformation', testRouteInformation),
);
await ServicesBinding.instance!.defaultBinaryMessenger.handlePlatformMessage('flutter/navigation', message, (_) { });
await ServicesBinding.instance!.defaultBinaryMessenger
.handlePlatformMessage('flutter/navigation', message, (_) {});
expect(observer.pushedRoute, 'testRouteName');
WidgetsBinding.instance!.removeObserver(observer);
});
......@@ -133,7 +132,6 @@ void main() {
WidgetsBinding.instance!.removeObserver(observer);
});
testWidgets('didPushRouteInformation callback with null state', (WidgetTester tester) async {
final PushRouteInformationObserver observer = PushRouteInformationObserver();
WidgetsBinding.instance!.addObserver(observer);
......@@ -152,36 +150,28 @@ void main() {
});
testWidgets('Application lifecycle affects frame scheduling', (WidgetTester tester) async {
final BinaryMessenger defaultBinaryMessenger = ServicesBinding.instance!.defaultBinaryMessenger;
ByteData message;
expect(tester.binding.hasScheduledFrame, isFalse);
message = const StringCodec().encodeMessage('AppLifecycleState.paused')!;
await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { });
setAppLifeCycleState(AppLifecycleState.paused);
expect(tester.binding.hasScheduledFrame, isFalse);
message = const StringCodec().encodeMessage('AppLifecycleState.resumed')!;
await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { });
setAppLifeCycleState(AppLifecycleState.resumed);
expect(tester.binding.hasScheduledFrame, isTrue);
await tester.pump();
expect(tester.binding.hasScheduledFrame, isFalse);
message = const StringCodec().encodeMessage('AppLifecycleState.inactive')!;
await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { });
setAppLifeCycleState(AppLifecycleState.inactive);
expect(tester.binding.hasScheduledFrame, isFalse);
message = const StringCodec().encodeMessage('AppLifecycleState.detached')!;
await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { });
setAppLifeCycleState(AppLifecycleState.detached);
expect(tester.binding.hasScheduledFrame, isFalse);
message = const StringCodec().encodeMessage('AppLifecycleState.inactive')!;
await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { });
setAppLifeCycleState(AppLifecycleState.inactive);
expect(tester.binding.hasScheduledFrame, isTrue);
await tester.pump();
expect(tester.binding.hasScheduledFrame, isFalse);
message = const StringCodec().encodeMessage('AppLifecycleState.paused')!;
await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { });
setAppLifeCycleState(AppLifecycleState.paused);
expect(tester.binding.hasScheduledFrame, isFalse);
tester.binding.scheduleFrame();
......@@ -193,7 +183,9 @@ void main() {
expect(tester.binding.hasScheduledFrame, isFalse);
int frameCount = 0;
tester.binding.addPostFrameCallback((Duration duration) { frameCount += 1; });
tester.binding.addPostFrameCallback((Duration duration) {
frameCount += 1;
});
expect(tester.binding.hasScheduledFrame, isFalse);
await tester.pump(const Duration(milliseconds: 1));
expect(tester.binding.hasScheduledFrame, isFalse);
......@@ -204,8 +196,7 @@ void main() {
expect(frameCount, 1);
// Get the tester back to a resumed state for subsequent tests.
message = const StringCodec().encodeMessage('AppLifecycleState.resumed')!;
await defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { });
setAppLifeCycleState(AppLifecycleState.resumed);
expect(tester.binding.hasScheduledFrame, isTrue);
await tester.pump();
});
......
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