diff --git a/shell/platform/windows/angle_surface_manager.h b/shell/platform/windows/angle_surface_manager.h index 2f6302b9659ed..d50dc2a7f1e64 100644 --- a/shell/platform/windows/angle_surface_manager.h +++ b/shell/platform/windows/angle_surface_manager.h @@ -65,7 +65,7 @@ class AngleSurfaceManager { // Binds |egl_context_| to the current rendering thread and to the draw and // read surfaces returning a boolean result reflecting success. - bool MakeCurrent(); + virtual bool MakeCurrent(); // Unbinds the current EGL context from the current thread. bool ClearCurrent(); diff --git a/shell/platform/windows/flutter_window_unittests.cc b/shell/platform/windows/flutter_window_unittests.cc index 8059a1ed29e03..e66ccea5f45b3 100644 --- a/shell/platform/windows/flutter_window_unittests.cc +++ b/shell/platform/windows/flutter_window_unittests.cc @@ -12,6 +12,7 @@ #include "gtest/gtest.h" using testing::_; +using testing::AnyNumber; using testing::Invoke; using testing::Return; @@ -133,8 +134,7 @@ TEST(FlutterWindowTest, OnBitmapSurfaceUpdated) { // when the DPI scale is 100% (96 DPI). TEST(FlutterWindowTest, OnCursorRectUpdatedRegularDPI) { MockFlutterWindow win32window; - ON_CALL(win32window, GetDpiScale()).WillByDefault(Return(1.0)); - EXPECT_CALL(win32window, GetDpiScale()).Times(1); + EXPECT_CALL(win32window, GetDpiScale()).WillOnce(Return(1.0)); Rect cursor_rect(Point(10, 20), Size(30, 40)); EXPECT_CALL(win32window, UpdateCursorRect(cursor_rect)).Times(1); @@ -147,8 +147,7 @@ TEST(FlutterWindowTest, OnCursorRectUpdatedRegularDPI) { // when the DPI scale is 150% (144 DPI). TEST(FlutterWindowTest, OnCursorRectUpdatedHighDPI) { MockFlutterWindow win32window; - ON_CALL(win32window, GetDpiScale()).WillByDefault(Return(1.5)); - EXPECT_CALL(win32window, GetDpiScale()).Times(1); + EXPECT_CALL(win32window, GetDpiScale()).WillOnce(Return(1.5)); Rect expected_cursor_rect(Point(15, 30), Size(45, 60)); EXPECT_CALL(win32window, UpdateCursorRect(expected_cursor_rect)).Times(1); @@ -160,7 +159,9 @@ TEST(FlutterWindowTest, OnCursorRectUpdatedHighDPI) { TEST(FlutterWindowTest, OnPointerStarSendsDeviceType) { FlutterWindow win32window(100, 100); MockWindowBindingHandlerDelegate delegate; + EXPECT_CALL(delegate, OnWindowStateEvent).Times(AnyNumber()); win32window.SetView(&delegate); + // Move EXPECT_CALL(delegate, OnPointerMove(10.0, 10.0, kFlutterPointerDeviceKindMouse, @@ -259,6 +260,7 @@ TEST(FlutterWindowTest, OnPointerStarSendsDeviceType) { TEST(FlutterWindowTest, OnScrollCallsGetScrollOffsetMultiplier) { MockFlutterWindow win32window; MockWindowBindingHandlerDelegate delegate; + EXPECT_CALL(win32window, OnWindowStateEvent).Times(AnyNumber()); win32window.SetView(&delegate); ON_CALL(win32window, GetScrollOffsetMultiplier()) @@ -277,6 +279,7 @@ TEST(FlutterWindowTest, OnScrollCallsGetScrollOffsetMultiplier) { TEST(FlutterWindowTest, OnWindowRepaint) { MockFlutterWindow win32window; MockWindowBindingHandlerDelegate delegate; + EXPECT_CALL(win32window, OnWindowStateEvent).Times(AnyNumber()); win32window.SetView(&delegate); EXPECT_CALL(delegate, OnWindowRepaint()).Times(1); @@ -287,6 +290,7 @@ TEST(FlutterWindowTest, OnWindowRepaint) { TEST(FlutterWindowTest, OnThemeChange) { MockFlutterWindow win32window; MockWindowBindingHandlerDelegate delegate; + EXPECT_CALL(win32window, OnWindowStateEvent).Times(AnyNumber()); win32window.SetView(&delegate); EXPECT_CALL(delegate, OnHighContrastChanged).Times(1); @@ -308,9 +312,9 @@ TEST(FlutterWindowTest, AccessibilityNodeWithoutView) { TEST(FlutterWindowTest, AlertNode) { std::unique_ptr win32window = std::make_unique(); - ON_CALL(*win32window, GetPlatformWindow()).WillByDefault(Return(nullptr)); - ON_CALL(*win32window, GetAxFragmentRootDelegate()) - .WillByDefault(Return(nullptr)); + EXPECT_CALL(*win32window.get(), GetAxFragmentRootDelegate()) + .WillRepeatedly(Return(nullptr)); + EXPECT_CALL(*win32window.get(), OnWindowStateEvent).Times(AnyNumber()); MockFlutterWindowsView view(std::move(win32window)); std::wstring message = L"Test alert"; EXPECT_CALL(view, NotifyWinEventWrapper(_, ax::mojom::Event::kAlert)) @@ -337,21 +341,22 @@ TEST(FlutterWindowTest, AlertNode) { TEST(FlutterWindowTest, LifecycleFocusMessages) { MockFlutterWindow win32window; - ON_CALL(win32window, GetPlatformWindow).WillByDefault([]() { - return reinterpret_cast(1); - }); + EXPECT_CALL(win32window, GetPlatformWindow) + .WillRepeatedly(Return(reinterpret_cast(1))); MockWindowBindingHandlerDelegate delegate; - win32window.SetView(&delegate); WindowStateEvent last_event; - ON_CALL(delegate, OnWindowStateEvent) - .WillByDefault([&last_event](HWND hwnd, WindowStateEvent event) { + EXPECT_CALL(delegate, OnWindowStateEvent) + .WillRepeatedly([&last_event](HWND hwnd, WindowStateEvent event) { last_event = event; }); - ON_CALL(win32window, OnWindowStateEvent) - .WillByDefault([&](WindowStateEvent event) { + EXPECT_CALL(win32window, OnWindowStateEvent) + .WillRepeatedly([&](WindowStateEvent event) { win32window.FlutterWindow::OnWindowStateEvent(event); }); + EXPECT_CALL(win32window, OnResize).Times(AnyNumber()); + + win32window.SetView(&delegate); win32window.InjectWindowMessage(WM_SIZE, 0, 0); EXPECT_EQ(last_event, WindowStateEvent::kHide); @@ -368,13 +373,13 @@ TEST(FlutterWindowTest, LifecycleFocusMessages) { TEST(FlutterWindowTest, CachedLifecycleMessage) { MockFlutterWindow win32window; - ON_CALL(win32window, GetPlatformWindow).WillByDefault([]() { - return reinterpret_cast(1); - }); - ON_CALL(win32window, OnWindowStateEvent) - .WillByDefault([&](WindowStateEvent event) { + EXPECT_CALL(win32window, GetPlatformWindow) + .WillRepeatedly(Return(reinterpret_cast(1))); + EXPECT_CALL(win32window, OnWindowStateEvent) + .WillRepeatedly([&](WindowStateEvent event) { win32window.FlutterWindow::OnWindowStateEvent(event); }); + EXPECT_CALL(win32window, OnResize).Times(1); // Restore win32window.InjectWindowMessage(WM_SIZE, 0, MAKEWORD(1, 1)); @@ -385,8 +390,8 @@ TEST(FlutterWindowTest, CachedLifecycleMessage) { MockWindowBindingHandlerDelegate delegate; bool focused = false; bool restored = false; - ON_CALL(delegate, OnWindowStateEvent) - .WillByDefault([&](HWND hwnd, WindowStateEvent event) { + EXPECT_CALL(delegate, OnWindowStateEvent) + .WillRepeatedly([&](HWND hwnd, WindowStateEvent event) { if (event == WindowStateEvent::kFocus) { focused = true; } else if (event == WindowStateEvent::kShow) { @@ -403,18 +408,19 @@ TEST(FlutterWindowTest, PosthumousWindowMessage) { MockWindowBindingHandlerDelegate delegate; int msg_count = 0; HWND hwnd; - ON_CALL(delegate, OnWindowStateEvent) - .WillByDefault([&](HWND hwnd, WindowStateEvent event) { msg_count++; }); + EXPECT_CALL(delegate, OnWindowStateEvent) + .WillRepeatedly([&](HWND hwnd, WindowStateEvent event) { msg_count++; }); { MockFlutterWindow win32window(false); - ON_CALL(win32window, GetPlatformWindow).WillByDefault([&]() { + EXPECT_CALL(win32window, GetPlatformWindow).WillRepeatedly([&]() { return win32window.FlutterWindow::GetPlatformWindow(); }); - ON_CALL(win32window, OnWindowStateEvent) - .WillByDefault([&](WindowStateEvent event) { + 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.GetPlatformWindow(); diff --git a/shell/platform/windows/flutter_windows_engine_unittests.cc b/shell/platform/windows/flutter_windows_engine_unittests.cc index 281663415d505..63c791b109932 100644 --- a/shell/platform/windows/flutter_windows_engine_unittests.cc +++ b/shell/platform/windows/flutter_windows_engine_unittests.cc @@ -640,9 +640,8 @@ TEST_F(FlutterWindowsEngineTest, AlertPlatformMessage) { std::make_unique<::testing::NiceMock>(); ui::AXPlatformNodeDelegateBase parent_delegate; AlertPlatformNodeDelegate delegate(parent_delegate); - ON_CALL(*window_binding_handler, GetAlertDelegate).WillByDefault([&delegate] { - return &delegate; - }); + EXPECT_CALL(*window_binding_handler, GetAlertDelegate) + .WillRepeatedly(Return(&delegate)); MockFlutterWindowsView view(std::move(window_binding_handler)); view.SetEngine(engine.get()); @@ -660,9 +659,9 @@ TEST_F(FlutterWindowsEngineTest, AlertPlatformMessage) { }); bool did_call = false; - ON_CALL(view, NotifyWinEventWrapper) - .WillByDefault([&did_call](ui::AXPlatformNodeWin* node, - ax::mojom::Event event) { did_call = true; }); + EXPECT_CALL(view, NotifyWinEventWrapper) + .WillOnce([&did_call](ui::AXPlatformNodeWin* node, + ax::mojom::Event event) { did_call = true; }); engine->UpdateSemanticsEnabled(true); engine->Run(); @@ -712,13 +711,13 @@ TEST_F(FlutterWindowsEngineTest, TestExit) { EngineModifier modifier(engine.get()); modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; }; auto handler = std::make_unique(engine.get()); - ON_CALL(*handler, Quit) - .WillByDefault( - [&finished](std::optional hwnd, std::optional wparam, - std::optional lparam, - UINT exit_code) { finished = exit_code == 0; }); - ON_CALL(*handler, IsLastWindowOfProcess).WillByDefault([]() { return true; }); - EXPECT_CALL(*handler, Quit).Times(1); + EXPECT_CALL(*handler, SetLifecycleState(AppLifecycleState::kResumed)); + EXPECT_CALL(*handler, Quit) + .WillOnce([&finished](std::optional hwnd, + std::optional wparam, + std::optional lparam, + UINT exit_code) { finished = exit_code == 0; }); + EXPECT_CALL(*handler, IsLastWindowOfProcess).WillRepeatedly(Return(true)); modifier.SetLifecycleManager(std::move(handler)); engine->lifecycle_manager()->BeginProcessingExit(); @@ -738,7 +737,6 @@ TEST_F(FlutterWindowsEngineTest, TestExit) { TEST_F(FlutterWindowsEngineTest, TestExitCancel) { FlutterWindowsEngineBuilder builder{GetContext()}; builder.SetDartEntrypoint("exitTestCancel"); - bool finished = false; bool did_call = false; auto engine = builder.Build(); @@ -750,12 +748,8 @@ TEST_F(FlutterWindowsEngineTest, TestExitCancel) { EngineModifier modifier(engine.get()); modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; }; auto handler = std::make_unique(engine.get()); - ON_CALL(*handler, Quit) - .WillByDefault([&finished](std::optional hwnd, - std::optional wparam, - std::optional lparam, - UINT exit_code) { finished = true; }); - ON_CALL(*handler, IsLastWindowOfProcess).WillByDefault([]() { return true; }); + EXPECT_CALL(*handler, SetLifecycleState(AppLifecycleState::kResumed)); + EXPECT_CALL(*handler, IsLastWindowOfProcess).WillRepeatedly(Return(true)); EXPECT_CALL(*handler, Quit).Times(0); modifier.SetLifecycleManager(std::move(handler)); engine->lifecycle_manager()->BeginProcessingExit(); @@ -783,10 +777,11 @@ TEST_F(FlutterWindowsEngineTest, TestExitCancel) { while (!did_call) { engine->task_runner()->ProcessTasks(); } - - EXPECT_FALSE(finished); } +// TODO(loicsharma): This test is passing incorrectly on the first +// WM_CLOSE message when instead it should pass on the second WM_CLOSE message. +// https://github.com/flutter/flutter/issues/137963 TEST_F(FlutterWindowsEngineTest, TestExitSecondCloseMessage) { FlutterWindowsEngineBuilder builder{GetContext()}; builder.SetDartEntrypoint("exitTestExit"); @@ -801,18 +796,18 @@ TEST_F(FlutterWindowsEngineTest, TestExitSecondCloseMessage) { EngineModifier modifier(engine.get()); modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; }; auto handler = std::make_unique(engine.get()); - auto& handler_obj = *handler; - ON_CALL(handler_obj, IsLastWindowOfProcess).WillByDefault([]() { - return true; - }); - ON_CALL(handler_obj, Quit) - .WillByDefault( - [&handler_obj](std::optional hwnd, std::optional wparam, + EXPECT_CALL(*handler, SetLifecycleState(AppLifecycleState::kResumed)); + // TODO(loicsharma): These should be `EXPECT_CALL`s + // https://github.com/flutter/flutter/issues/137963 + ON_CALL(*handler, IsLastWindowOfProcess).WillByDefault(Return(true)); + ON_CALL(*handler, Quit) + .WillByDefault([handler_ptr = handler.get()]( + std::optional hwnd, std::optional wparam, std::optional lparam, UINT exit_code) { - handler_obj.WindowsLifecycleManager::Quit(hwnd, wparam, lparam, - exit_code); - }); - ON_CALL(handler_obj, DispatchMessage) + handler_ptr->WindowsLifecycleManager::Quit(hwnd, wparam, lparam, + exit_code); + }); + ON_CALL(*handler, DispatchMessage) .WillByDefault( [&engine](HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { engine->window_proc_delegate_manager()->OnTopLevelWindowProc( @@ -863,7 +858,8 @@ TEST_F(FlutterWindowsEngineTest, TestExitCloseMultiWindow) { EngineModifier modifier(engine.get()); modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; }; auto handler = std::make_unique(engine.get()); - ON_CALL(*handler, IsLastWindowOfProcess).WillByDefault([&finished]() { + EXPECT_CALL(*handler, SetLifecycleState(AppLifecycleState::kResumed)); + EXPECT_CALL(*handler, IsLastWindowOfProcess).WillOnce([&finished]() { finished = true; return false; }); @@ -913,10 +909,7 @@ TEST_F(FlutterWindowsEngineTest, EnableApplicationLifecycle) { EngineModifier modifier(engine.get()); modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; }; auto handler = std::make_unique(engine.get()); - ON_CALL(*handler, IsLastWindowOfProcess).WillByDefault([]() { - return false; - }); - EXPECT_CALL(*handler, IsLastWindowOfProcess).Times(1); + EXPECT_CALL(*handler, IsLastWindowOfProcess).WillOnce(Return(false)); modifier.SetLifecycleManager(std::move(handler)); engine->lifecycle_manager()->BeginProcessingExit(); @@ -936,10 +929,7 @@ TEST_F(FlutterWindowsEngineTest, ApplicationLifecycleExternalWindow) { EngineModifier modifier(engine.get()); modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; }; auto handler = std::make_unique(engine.get()); - ON_CALL(*handler, IsLastWindowOfProcess).WillByDefault([]() { - return false; - }); - EXPECT_CALL(*handler, IsLastWindowOfProcess).Times(1); + EXPECT_CALL(*handler, IsLastWindowOfProcess).WillOnce(Return(false)); modifier.SetLifecycleManager(std::move(handler)); engine->lifecycle_manager()->BeginProcessingExit(); @@ -1065,8 +1055,8 @@ TEST_F(FlutterWindowsEngineTest, EnableLifecycleState) { EngineModifier modifier(engine.get()); modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; }; auto handler = std::make_unique(engine.get()); - ON_CALL(*handler, SetLifecycleState) - .WillByDefault([handler_ptr = handler.get()](AppLifecycleState state) { + EXPECT_CALL(*handler, SetLifecycleState) + .WillRepeatedly([handler_ptr = handler.get()](AppLifecycleState state) { handler_ptr->WindowsLifecycleManager::SetLifecycleState(state); }); modifier.SetLifecycleManager(std::move(handler)); @@ -1118,8 +1108,8 @@ TEST_F(FlutterWindowsEngineTest, LifecycleStateToFrom) { EngineModifier modifier(engine.get()); modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; }; auto handler = std::make_unique(engine.get()); - ON_CALL(*handler, SetLifecycleState) - .WillByDefault([handler_ptr = handler.get()](AppLifecycleState state) { + EXPECT_CALL(*handler, SetLifecycleState) + .WillRepeatedly([handler_ptr = handler.get()](AppLifecycleState state) { handler_ptr->WindowsLifecycleManager::SetLifecycleState(state); }); handler->begin_processing_callback = [&]() { enabled_lifecycle = true; }; @@ -1167,6 +1157,7 @@ TEST_F(FlutterWindowsEngineTest, ChannelListenedTo) { bool lifecycle_began = false; auto handler = std::make_unique(engine.get()); + EXPECT_CALL(*handler, SetLifecycleState).Times(1); handler->begin_processing_callback = [&]() { lifecycle_began = true; }; modifier.SetLifecycleManager(std::move(handler)); diff --git a/shell/platform/windows/flutter_windows_view_unittests.cc b/shell/platform/windows/flutter_windows_view_unittests.cc index 859ffd1bd484a..6f4d5fc85650f 100644 --- a/shell/platform/windows/flutter_windows_view_unittests.cc +++ b/shell/platform/windows/flutter_windows_view_unittests.cc @@ -131,6 +131,7 @@ class MockAngleSurfaceManager : public AngleSurfaceManager { (override)); MOCK_METHOD(void, DestroySurface, (), (override)); + MOCK_METHOD(bool, MakeCurrent, (), (override)); MOCK_METHOD(void, SetVSyncEnabled, (bool), (override)); private: @@ -1302,7 +1303,9 @@ TEST(FlutterWindowsViewTest, UpdatesVSyncOnDwmUpdates) { FlutterWindowsView view(std::move(window_binding_handler)); InSequence s; + EXPECT_CALL(*surface_manager.get(), MakeCurrent).WillOnce(Return(true)); EXPECT_CALL(*surface_manager.get(), SetVSyncEnabled(true)).Times(1); + EXPECT_CALL(*surface_manager.get(), MakeCurrent).WillOnce(Return(true)); EXPECT_CALL(*surface_manager.get(), SetVSyncEnabled(false)).Times(1); EXPECT_CALL(*engine.get(), Stop).Times(1); diff --git a/shell/platform/windows/system_utils_unittests.cc b/shell/platform/windows/system_utils_unittests.cc index a4a9bcda114b8..1b53ed7748dc8 100644 --- a/shell/platform/windows/system_utils_unittests.cc +++ b/shell/platform/windows/system_utils_unittests.cc @@ -25,8 +25,8 @@ TEST(SystemUtils, GetPreferredLanguageInfo) { TEST(SystemUtils, GetPreferredLanguages) { MockWindowsProcTable proc_table; - ON_CALL(proc_table, GetThreadPreferredUILanguages) - .WillByDefault( + EXPECT_CALL(proc_table, GetThreadPreferredUILanguages) + .WillRepeatedly( [](DWORD flags, PULONG count, PZZWSTR languages, PULONG size) { // Languages string ends in a double-null. static const wchar_t lang[] = L"en-US\0";