[Windows] Refactor window destroyed app lifecycle event (flutter/engine#50565)
Improves Windows's lifecycle event for window destruction:
1. Made `FlutterWindowsView` generate the "window hidden" event when the HWND is destroyed instead of the `FlutterWindow`.
1. Before the window would submit this event using a destroyed view:
1. The `FlutterWindowsView` destructor runs to completion.
2. The view owns the window, so the `FlutterWindow` is destroyed
3. The window's destructor generates a "window hidden" event
4. The window uses the destroyed view to notify the engine of the event
2. Adds an app lifecycle integration test to verify the `resumed` and `hidden` events are sent when an app is launched and closed.
3. Removed the `FlutterWindowTest` type as it was unused
Prepares for https://github.com/flutter/flutter/issues/137267
[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -149,7 +149,6 @@ FlutterWindow::FlutterWindow(
|
||||
}
|
||||
|
||||
FlutterWindow::~FlutterWindow() {
|
||||
OnWindowStateEvent(WindowStateEvent::kHide);
|
||||
Destroy();
|
||||
}
|
||||
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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<FlutterWindowsEngine*>(engine.get());
|
||||
EngineModifier modifier{windows_engine};
|
||||
|
||||
auto lifecycle_manager =
|
||||
std::make_unique<MockWindowsLifecycleManager>(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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -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_
|
||||
Reference in New Issue
Block a user