9 #include "flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h"
10 #include "gmock/gmock.h"
11 #include "gtest/gtest.h"
17 using ::testing::Return;
20 class TestWindowsApi :
public testing::StubFlutterWindowsApi {
22 void PluginRegistrarRegisterTopLevelWindowProcDelegate(
25 ++registered_delegate_count_;
26 last_registered_delegate_ = delegate;
32 PluginRegistrarGetViewById,
36 void PluginRegistrarUnregisterTopLevelWindowProcDelegate(
38 --registered_delegate_count_;
41 int registered_delegate_count() {
return registered_delegate_count_; }
44 return last_registered_delegate_;
47 void* last_registered_user_data() {
return last_registered_user_data_; }
50 int registered_delegate_count_ = 0;
52 void* last_registered_user_data_ =
nullptr;
57 class TestPlugin :
public Plugin {
61 TestPlugin(PluginRegistrarWindows* registrar,
62 bool* registrar_valid_at_destruction)
63 : registrar_(registrar),
64 registrar_valid_at_destruction_(registrar_valid_at_destruction) {}
65 virtual ~TestPlugin() {
66 *registrar_valid_at_destruction_ = registrar_->GetView() !=
nullptr;
70 PluginRegistrarWindows* registrar_;
71 bool* registrar_valid_at_destruction_;
76 TEST(PluginRegistrarWindowsTest, GetView) {
77 auto windows_api = std::make_unique<TestWindowsApi>();
78 EXPECT_CALL(*windows_api, PluginRegistrarGetView)
81 testing::ScopedStubFlutterWindowsApi scoped_api_stub(std::move(windows_api));
82 auto test_api =
static_cast<TestWindowsApi*
>(scoped_api_stub.stub());
86 EXPECT_NE(registrar.
GetView(),
nullptr);
89 TEST(PluginRegistrarWindowsTest, GetViewById) {
90 auto windows_api = std::make_unique<TestWindowsApi>();
91 EXPECT_CALL(*windows_api, PluginRegistrarGetView)
92 .WillRepeatedly(Return(
nullptr));
93 EXPECT_CALL(*windows_api, PluginRegistrarGetViewById(123))
95 EXPECT_CALL(*windows_api, PluginRegistrarGetViewById(456))
96 .WillOnce(Return(
nullptr));
98 testing::ScopedStubFlutterWindowsApi scoped_api_stub(std::move(windows_api));
99 auto test_api =
static_cast<TestWindowsApi*
>(scoped_api_stub.stub());
103 EXPECT_EQ(registrar.
GetView(),
nullptr);
104 EXPECT_NE(registrar.
GetViewById(123).get(),
nullptr);
105 EXPECT_EQ(registrar.
GetViewById(456).get(),
nullptr);
109 TEST(PluginRegistrarWindowsTest, PluginDestroyedBeforeRegistrar) {
110 auto windows_api = std::make_unique<TestWindowsApi>();
111 EXPECT_CALL(*windows_api, PluginRegistrarGetView)
113 testing::ScopedStubFlutterWindowsApi scoped_api_stub(std::move(windows_api));
114 auto test_api =
static_cast<TestWindowsApi*
>(scoped_api_stub.stub());
118 auto dummy_registrar_handle =
120 bool registrar_valid_at_destruction =
false;
124 auto plugin = std::make_unique<TestPlugin>(®istrar,
125 ®istrar_valid_at_destruction);
128 EXPECT_TRUE(registrar_valid_at_destruction);
131 TEST(PluginRegistrarWindowsTest, RegisterUnregister) {
132 auto windows_api = std::make_unique<TestWindowsApi>();
133 EXPECT_CALL(*windows_api, PluginRegistrarGetView).WillOnce(Return(
nullptr));
134 testing::ScopedStubFlutterWindowsApi scoped_api_stub(std::move(windows_api));
135 auto test_api =
static_cast<TestWindowsApi*
>(scoped_api_stub.stub());
141 return std::optional<LRESULT>();
144 EXPECT_EQ(test_api->registered_delegate_count(), 1);
148 EXPECT_EQ(test_api->registered_delegate_count(), 1);
153 EXPECT_EQ(test_api->registered_delegate_count(), 1);
156 EXPECT_EQ(test_api->registered_delegate_count(), 0);
158 EXPECT_NE(id_a, id_b);
161 TEST(PluginRegistrarWindowsTest, CallsRegisteredDelegates) {
162 auto windows_api = std::make_unique<TestWindowsApi>();
163 EXPECT_CALL(*windows_api, PluginRegistrarGetView).WillOnce(Return(
nullptr));
164 testing::ScopedStubFlutterWindowsApi scoped_api_stub(std::move(windows_api));
165 auto test_api =
static_cast<TestWindowsApi*
>(scoped_api_stub.stub());
170 bool called_a =
false;
172 HWND hwnd, UINT
message, WPARAM wparam,
175 EXPECT_EQ(hwnd, dummy_hwnd);
177 EXPECT_EQ(wparam, 3);
178 EXPECT_EQ(lparam, 4);
179 return std::optional<LRESULT>();
181 bool called_b =
false;
183 WPARAM wparam, LPARAM lparam) {
185 return std::optional<LRESULT>();
191 bool handled = test_api->last_registered_delegate()(
192 dummy_hwnd, 2, 3, 4, test_api->last_registered_user_data(), &result);
193 EXPECT_TRUE(called_a);
194 EXPECT_TRUE(called_b);
195 EXPECT_FALSE(handled);
198 TEST(PluginRegistrarWindowsTest, StopsOnceHandled) {
199 auto windows_api = std::make_unique<TestWindowsApi>();
200 EXPECT_CALL(*windows_api, PluginRegistrarGetView).WillOnce(Return(
nullptr));
201 testing::ScopedStubFlutterWindowsApi scoped_api_stub(std::move(windows_api));
202 auto test_api =
static_cast<TestWindowsApi*
>(scoped_api_stub.stub());
206 bool called_a =
false;
208 WPARAM wparam, LPARAM lparam) {
210 return std::optional<LRESULT>(7);
212 bool called_b =
false;
214 WPARAM wparam, LPARAM lparam) {
216 return std::optional<LRESULT>(7);
223 bool handled = test_api->last_registered_delegate()(
224 dummy_hwnd, 2, 3, 4, test_api->last_registered_user_data(), &result);
227 EXPECT_TRUE(called_a || called_b);
228 EXPECT_NE(called_a, called_b);
230 EXPECT_TRUE(handled);
231 EXPECT_EQ(result, 7);