diff --git a/engine/src/flutter/shell/platform/windows/BUILD.gn b/engine/src/flutter/shell/platform/windows/BUILD.gn index b33d4389bb..0f0815cb26 100644 --- a/engine/src/flutter/shell/platform/windows/BUILD.gn +++ b/engine/src/flutter/shell/platform/windows/BUILD.gn @@ -215,8 +215,6 @@ executable("flutter_windows_unittests") { "testing/egl/mock_proc_table.h", "testing/egl/mock_window_surface.h", "testing/engine_modifier.h", - "testing/flutter_window_test.cc", - "testing/flutter_window_test.h", "testing/flutter_windows_engine_builder.cc", "testing/flutter_windows_engine_builder.h", "testing/mock_direct_manipulation.h", diff --git a/engine/src/flutter/shell/platform/windows/flutter_window.cc b/engine/src/flutter/shell/platform/windows/flutter_window.cc index e4361191aa..dfa231b099 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_window.cc @@ -149,7 +149,6 @@ FlutterWindow::FlutterWindow( } FlutterWindow::~FlutterWindow() { - OnWindowStateEvent(WindowStateEvent::kHide); Destroy(); } diff --git a/engine/src/flutter/shell/platform/windows/flutter_window_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_window_unittests.cc index be820f42ef..e2fdb1bdb8 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_window_unittests.cc @@ -3,7 +3,7 @@ // found in the LICENSE file. #include "flutter/fml/macros.h" -#include "flutter/shell/platform/windows/testing/flutter_window_test.h" +#include "flutter/shell/platform/windows/flutter_window.h" #include "flutter/shell/platform/windows/testing/mock_window_binding_handler.h" #include "flutter/shell/platform/windows/testing/mock_window_binding_handler_delegate.h" #include "flutter/shell/platform/windows/testing/wm_builders.h" @@ -11,14 +11,14 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" -using testing::_; -using testing::AnyNumber; -using testing::Invoke; -using testing::Return; - namespace flutter { namespace testing { +using ::testing::_; +using ::testing::AnyNumber; +using ::testing::Invoke; +using ::testing::Return; + namespace { static constexpr int32_t kDefaultPointerDeviceId = 0; @@ -110,7 +110,7 @@ class MockFlutterWindowsView : public FlutterWindowsView { } // namespace TEST(FlutterWindowTest, CreateDestroy) { - FlutterWindowTest window(800, 600); + FlutterWindow window(800, 600); ASSERT_TRUE(TRUE); } @@ -403,38 +403,6 @@ TEST(FlutterWindowTest, CachedLifecycleMessage) { EXPECT_TRUE(restored); } -TEST(FlutterWindowTest, PosthumousWindowMessage) { - MockWindowBindingHandlerDelegate delegate; - int msg_count = 0; - HWND hwnd; - EXPECT_CALL(delegate, OnWindowStateEvent) - .WillRepeatedly([&](HWND hwnd, WindowStateEvent event) { msg_count++; }); - - { - MockFlutterWindow win32window(false); - EXPECT_CALL(win32window, GetWindowHandle).WillRepeatedly([&]() { - return win32window.FlutterWindow::GetWindowHandle(); - }); - EXPECT_CALL(win32window, OnWindowStateEvent) - .WillRepeatedly([&](WindowStateEvent event) { - win32window.FlutterWindow::OnWindowStateEvent(event); - }); - EXPECT_CALL(win32window, OnResize).Times(AnyNumber()); - win32window.SetView(&delegate); - win32window.InitializeChild("Title", 1, 1); - hwnd = win32window.GetWindowHandle(); - SendMessage(hwnd, WM_SIZE, 0, MAKEWORD(1, 1)); - SendMessage(hwnd, WM_SETFOCUS, 0, 0); - - // By setting this to zero before exiting the scope that contains - // win32window, and then checking its value afterwards, enforce that the - // window receive and process messages from its destructor without - // accessing out-of-bounds memory. - msg_count = 0; - } - EXPECT_GE(msg_count, 1); -} - TEST(FlutterWindowTest, UpdateCursor) { FlutterWindow win32window(100, 100); win32window.UpdateFlutterCursor("text"); diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc index b21182e4c9..e012254d97 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_unittests.cc @@ -10,13 +10,16 @@ #include "flutter/fml/synchronization/count_down_latch.h" #include "flutter/fml/synchronization/waitable_event.h" +#include "flutter/shell/platform/common/app_lifecycle_state.h" #include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h" #include "flutter/shell/platform/windows/egl/manager.h" #include "flutter/shell/platform/windows/testing/engine_modifier.h" #include "flutter/shell/platform/windows/testing/windows_test.h" #include "flutter/shell/platform/windows/testing/windows_test_config_builder.h" #include "flutter/shell/platform/windows/testing/windows_test_context.h" +#include "flutter/shell/platform/windows/windows_lifecycle_manager.h" #include "flutter/testing/stream_capture.h" +#include "gmock/gmock.h" #include "gtest/gtest.h" #include "third_party/tonic/converter/dart_converter.h" @@ -36,6 +39,14 @@ class HalfBrokenEGLManager : public egl::Manager { } }; +class MockWindowsLifecycleManager : public WindowsLifecycleManager { + public: + MockWindowsLifecycleManager(FlutterWindowsEngine* engine) + : WindowsLifecycleManager(engine) {} + + MOCK_METHOD(void, SetLifecycleState, (AppLifecycleState), (override)); +}; + } // namespace // Verify that we can fetch a texture registrar. @@ -331,5 +342,51 @@ TEST_F(WindowsTest, SurfaceOptional) { ASSERT_NE(controller, nullptr); } +// Verify the app produces the expected lifecycle events. +TEST_F(WindowsTest, Lifecycle) { + auto& context = GetContext(); + WindowsConfigBuilder builder(context); + EnginePtr engine{builder.InitializeEngine()}; + auto windows_engine = reinterpret_cast(engine.get()); + EngineModifier modifier{windows_engine}; + + auto lifecycle_manager = + std::make_unique(windows_engine); + auto lifecycle_manager_ptr = lifecycle_manager.get(); + modifier.SetLifecycleManager(std::move(lifecycle_manager)); + + EXPECT_CALL(*lifecycle_manager_ptr, + SetLifecycleState(AppLifecycleState::kResumed)) + .WillOnce([lifecycle_manager_ptr](AppLifecycleState state) { + lifecycle_manager_ptr->WindowsLifecycleManager::SetLifecycleState( + state); + }); + + EXPECT_CALL(*lifecycle_manager_ptr, + SetLifecycleState(AppLifecycleState::kHidden)) + .WillOnce([lifecycle_manager_ptr](AppLifecycleState state) { + lifecycle_manager_ptr->WindowsLifecycleManager::SetLifecycleState( + state); + }); + + // Create a controller. This launches the engine and sets the app lifecycle + // to the "resumed" state. + ViewControllerPtr controller{ + FlutterDesktopViewControllerCreate(0, 0, engine.release())}; + + FlutterDesktopViewRef view = + FlutterDesktopViewControllerGetView(controller.get()); + ASSERT_NE(view, nullptr); + + HWND hwnd = FlutterDesktopViewGetHWND(view); + ASSERT_NE(hwnd, nullptr); + + // Give the window a non-zero size to show it. This does not change the app + // lifecycle directly. However, destroying the view will now result in a + // "hidden" app lifecycle event. + ::MoveWindow(hwnd, /* X */ 0, /* Y */ 0, /* nWidth*/ 100, /* nHeight*/ 100, + /* bRepaint*/ false); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc index 91e9306e68..a373bc244b 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc @@ -97,9 +97,13 @@ FlutterWindowsView::FlutterWindowsView( } FlutterWindowsView::~FlutterWindowsView() { - // The engine renders into the view's surface. The engine must be - // shutdown before the view's resources can be destroyed. if (engine_) { + // The view owns the child window. + // Notify the engine the view's child window will no longer be visible. + engine_->OnWindowStateEvent(GetWindowHandle(), WindowStateEvent::kHide); + + // The engine renders into the view's surface. The engine must be + // shutdown before the view's resources can be destroyed. engine_->Stop(); } diff --git a/engine/src/flutter/shell/platform/windows/testing/flutter_window_test.cc b/engine/src/flutter/shell/platform/windows/testing/flutter_window_test.cc deleted file mode 100644 index 690eb28eb4..0000000000 --- a/engine/src/flutter/shell/platform/windows/testing/flutter_window_test.cc +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "flutter/shell/platform/windows/testing/flutter_window_test.h" - -namespace flutter { -namespace testing { - -FlutterWindowTest::FlutterWindowTest(int width, int height) - : FlutterWindow(width, height){}; - -FlutterWindowTest::~FlutterWindowTest() = default; - -} // namespace testing -} // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/testing/flutter_window_test.h b/engine/src/flutter/shell/platform/windows/testing/flutter_window_test.h deleted file mode 100644 index 5003111b39..0000000000 --- a/engine/src/flutter/shell/platform/windows/testing/flutter_window_test.h +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_FLUTTER_WINDOW_TEST_H_ -#define FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_FLUTTER_WINDOW_TEST_H_ - -#include "flutter/shell/platform/windows/flutter_window.h" - -#include "flutter/fml/macros.h" - -namespace flutter { -namespace testing { - -/// Test class for FlutterWindow. -class FlutterWindowTest : public FlutterWindow { - public: - FlutterWindowTest(int width, int height); - virtual ~FlutterWindowTest(); - - private: - FML_DISALLOW_COPY_AND_ASSIGN(FlutterWindowTest); -}; - -} // namespace testing -} // namespace flutter - -#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_FLUTTER_WINDOW_TEST_H_