From 2d2423b0f2f9cc1aeb9efad44dff1f8b1c760863 Mon Sep 17 00:00:00 2001 From: Victoria Ashworth <15619084+vashworth@users.noreply.github.com> Date: Tue, 28 Nov 2023 13:53:09 -0600 Subject: [PATCH] Skip unexpected events in MultiPlatformViewBackgroundForegroundScenario (flutter/engine#48456) Fixes https://github.com/flutter/flutter/issues/138193. Was first attempted to fix in https://github.com/flutter/engine/pull/48096, but that was not reliable since it's all asynchronous. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../scenario_app/lib/src/platform_view.dart | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/engine/src/flutter/testing/scenario_app/lib/src/platform_view.dart b/engine/src/flutter/testing/scenario_app/lib/src/platform_view.dart index d232546f76..e2e8d8a3fb 100644 --- a/engine/src/flutter/testing/scenario_app/lib/src/platform_view.dart +++ b/engine/src/flutter/testing/scenario_app/lib/src/platform_view.dart @@ -422,6 +422,7 @@ class MultiPlatformViewBackgroundForegroundScenario extends Scenario required this.secondId, }) { _nextFrame = _firstFrame; + channelBuffers.setListener('flutter/lifecycle', _onPlatformMessage); } /// The platform view identifier to use for the first platform view. @@ -437,13 +438,7 @@ class MultiPlatformViewBackgroundForegroundScenario extends Scenario _nextFrame(); } - bool _firstFrameBegan = false; - void _firstFrame() { - if (!_firstFrameBegan) { - channelBuffers.setListener('flutter/lifecycle', _onPlatformMessage); - } - _firstFrameBegan = true; final SceneBuilder builder = SceneBuilder(); builder.pushOffset(50, 600); @@ -515,6 +510,13 @@ class MultiPlatformViewBackgroundForegroundScenario extends Scenario PlatformMessageResponseCallback? callback, ) { final String message = utf8.decode(data!.buffer.asUint8List()); + + // The expected first event should be 'AppLifecycleState.resumed', but + // occasionally it will receive 'AppLifecycleState.inactive' first. Skip + // any messages until 'AppLifecycleState.resumed' is received. + if (_lastLifecycleState.isEmpty && message != 'AppLifecycleState.resumed') { + return; + } if (_lastLifecycleState == 'AppLifecycleState.inactive' && message == 'AppLifecycleState.resumed') { _nextFrame = _secondFrame;