From 8f1a430557ec2d3725fee2073e9a1145bab96b14 Mon Sep 17 00:00:00 2001 From: Gary Qian Date: Thu, 7 Mar 2019 20:05:42 -0800 Subject: [PATCH] Initialize the lifecycle state with initial state in window. Roll engine (19 commits) (#28688) git log --oneline --no-merges f4951df193a7966f9ed4da43d555eee0913d84d1..a48cd16e077778a7aa5908e4faa4da07714260f2 a48cd16e0 Update a11y word forward/back enum names (https://github.com/flutter/engine/pull/8073) b5f59ed89 Delay the vsync callback till the frame start time specified by embedder. (https://github.com/flutter/engine/pull/8072) 7426305f5 Mark const extern (https://github.com/flutter/engine/pull/8077) d3f6d7a21 only partial rule revert (https://github.com/flutter/engine/pull/8078) d71bfe58d Only build a full Dart SDK when building for the host system (https://github.com/flutter/engine/pull/8071) de90dbff7 Refactor web configuration/ Add dartdevc (https://github.com/flutter/engine/pull/7978) ff46dd38f Roll src/third_party/skia 4c1ea43a79b5..88b8d1124b72 (8 commits) (https://github.com/flutter/engine/pull/8070) 80c6dd286 Roll src/third_party/skia 692122e3ef23..4c1ea43a79b5 (3 commits) (https://github.com/flutter/engine/pull/8069) 68ed654ea Roll src/third_party/skia 3c957d575c58..692122e3ef23 (6 commits) (https://github.com/flutter/engine/pull/8067) ca0bac4fb Revert "add signal to pointer kinds" (https://github.com/flutter/engine/pull/8066) 3fb627f2c add signal to pointer kinds (https://github.com/flutter/engine/pull/8065) 5a06afa2a Roll src/third_party/skia 801a9c16d81e..3c957d575c58 (19 commits) (https://github.com/flutter/engine/pull/8063) a93d99db9 A11y callback (https://github.com/flutter/engine/pull/8005) 3661d5e43 Re-land "Buffer lifecycle in WindowData" (https://github.com/flutter/engine/pull/8032) 471a2c89a Send scroll events from the macOS shell (https://github.com/flutter/engine/pull/8056) 2fe9c9b5f Roll src/third_party/skia 72542816cadb..801a9c16d81e (46 commits) (https://github.com/flutter/engine/pull/8060) 3335764ae Skip skp files in license check (https://github.com/flutter/engine/pull/8050) 7f16789b2 Remove redundant thread checker in FML. (https://github.com/flutter/engine/pull/8053) 840c5233a Correct URL for Cirrus CI build status badge (https://github.com/flutter/engine/pull/8054) 57c120a29 remove extra source files (https://github.com/flutter/engine/pull/8052) 4773375c6 Used named conditionals for platform specific dependencies and suppress Android and Windows hooks on Mac. (https://github.com/flutter/engine/pull/8051) 70a18b515 Add clang static analysis support to gn wrapper (https://github.com/flutter/engine/pull/8047) b30f9897b Improve elevation bounds for physical shape layers (https://github.com/flutter/engine/pull/8044) e37bd27b4 Fix weak pointer use violations in shell and platform view. (https://github.com/flutter/engine/pull/8046) dd80fc9ff Add engine support for scrollwheel events (https://github.com/flutter/engine/pull/7494) --- bin/internal/engine.version | 2 +- .../flutter/lib/src/scheduler/binding.dart | 18 +++++++++++++++ .../test/scheduler/lifecycle_test.dart | 23 +++++++++++++++++++ packages/flutter_test/lib/src/binding.dart | 6 +++++ packages/flutter_test/lib/src/window.dart | 8 +++++++ 5 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 packages/flutter/test/scheduler/lifecycle_test.dart diff --git a/bin/internal/engine.version b/bin/internal/engine.version index d3e1fb5184..65b5d1b86b 100644 --- a/bin/internal/engine.version +++ b/bin/internal/engine.version @@ -1 +1 @@ -f4951df193a7966f9ed4da43d555eee0913d84d1 +a48cd16e077778a7aa5908e4faa4da07714260f2 diff --git a/packages/flutter/lib/src/scheduler/binding.dart b/packages/flutter/lib/src/scheduler/binding.dart index d0b83853e4..f33ba8601d 100644 --- a/packages/flutter/lib/src/scheduler/binding.dart +++ b/packages/flutter/lib/src/scheduler/binding.dart @@ -193,6 +193,7 @@ mixin SchedulerBinding on BindingBase, ServicesBinding { window.onBeginFrame = _handleBeginFrame; window.onDrawFrame = _handleDrawFrame; SystemChannels.lifecycle.setMessageHandler(_handleLifecycleMessage); + readInitialLifecycleStateFromNativeWindow(); } /// The current [SchedulerBinding], if one has been created. @@ -225,6 +226,23 @@ mixin SchedulerBinding on BindingBase, ServicesBinding { AppLifecycleState get lifecycleState => _lifecycleState; AppLifecycleState _lifecycleState; + /// Initializes the [lifecycleState] with the [initialLifecycleState] from the + /// window. + /// + /// Once the [lifecycleState] is populated through any means (including this + /// method), this method will do nothing. This is because the + /// [initialLifecycleState] may already be stale and it no longer makes sense + /// to use the initial state at dart vm startup as the current state anymore. + /// + /// The latest state should be obtained by subscribing to + /// [WidgetsBindingObserver.didChangeAppLifecycleState]. + @protected + void readInitialLifecycleStateFromNativeWindow() { + if (_lifecycleState == null && _parseAppLifecycleMessage(window.initialLifecycleState) != null) { + _handleLifecycleMessage(window.initialLifecycleState); + } + } + /// Called when the application lifecycle state changes. /// /// Notifies all the observers using diff --git a/packages/flutter/test/scheduler/lifecycle_test.dart b/packages/flutter/test/scheduler/lifecycle_test.dart new file mode 100644 index 0000000000..7506c1475e --- /dev/null +++ b/packages/flutter/test/scheduler/lifecycle_test.dart @@ -0,0 +1,23 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter_test/flutter_test.dart'; +import 'package:flutter/scheduler.dart'; + +void main() { + testWidgets('initialLifecycleState is used to init state paused', (WidgetTester tester) async { + // The lifecycleState is null initially in tests as there is no + // initialLifecycleState. + expect(SchedulerBinding.instance.lifecycleState, equals(null)); + // Mock the Window to provide paused as the AppLifecycleState + final TestWidgetsFlutterBinding binding = tester.binding; + // Use paused as the initial state. + binding.window.initialLifecycleStateTestValue = 'AppLifecycleState.paused'; + binding.readTestInitialLifecycleStateFromNativeWindow(); // Re-attempt the initializaiton. + + // The lifecycleState should now be the state we passed above, + // even though no lifecycle event was fired from the platform. + expect(SchedulerBinding.instance.lifecycleState.toString(), equals('AppLifecycleState.paused')); + }); +} diff --git a/packages/flutter_test/lib/src/binding.dart b/packages/flutter_test/lib/src/binding.dart index eae35756a1..1a4bd3819c 100644 --- a/packages/flutter_test/lib/src/binding.dart +++ b/packages/flutter_test/lib/src/binding.dart @@ -257,6 +257,12 @@ abstract class TestWidgetsFlutterBinding extends BindingBase }); } + /// Re-attempts the initialization of the lifecycle state after providing + /// test values in [TestWindow.initialLifecycleStateTestValue]. + void readTestInitialLifecycleStateFromNativeWindow() { + readInitialLifecycleStateFromNativeWindow(); + } + Size _surfaceSize; /// Artificially changes the surface size to `size` on the Widget binding, diff --git a/packages/flutter_test/lib/src/window.dart b/packages/flutter_test/lib/src/window.dart index 8e5b5e9389..d4111f7d84 100644 --- a/packages/flutter_test/lib/src/window.dart +++ b/packages/flutter_test/lib/src/window.dart @@ -151,6 +151,14 @@ class TestWindow implements Window { _window.onLocaleChanged = callback; } + @override + String get initialLifecycleState => _initialLifecycleStateTestValue; + String _initialLifecycleStateTestValue; + /// Sets a faked initialLifecycleState for testing. + set initialLifecycleStateTestValue(String state) { + _initialLifecycleStateTestValue = state; + } + @override double get textScaleFactor => _textScaleFactorTestValue ?? _window.textScaleFactor; double _textScaleFactorTestValue;