8#include "flutter/shell/platform/windows/client_wrapper/include/flutter/plugin_registrar_windows.h"
9#include "flutter/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h"
10#include "gmock/gmock.h"
11#include "gtest/gtest.h"
17using ::testing::Return;
20class 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;
57class 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_;
76TEST(PluginRegistrarWindowsTest, GetView) {
77 auto windows_api = std::make_unique<TestWindowsApi>();
78 EXPECT_CALL(*windows_api, PluginRegistrarGetView)
82 auto test_api =
static_cast<TestWindowsApi*
>(scoped_api_stub.
stub());
86 EXPECT_NE(registrar.
GetView(),
nullptr);
89TEST(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));
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);
109TEST(PluginRegistrarWindowsTest, PluginDestroyedBeforeRegistrar) {
110 auto windows_api = std::make_unique<TestWindowsApi>();
111 EXPECT_CALL(*windows_api, PluginRegistrarGetView)
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);
131TEST(PluginRegistrarWindowsTest, RegisterUnregister) {
132 auto windows_api = std::make_unique<TestWindowsApi>();
133 EXPECT_CALL(*windows_api, PluginRegistrarGetView).WillOnce(Return(
nullptr));
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);
161TEST(PluginRegistrarWindowsTest, CallsRegisteredDelegates) {
162 auto windows_api = std::make_unique<TestWindowsApi>();
163 EXPECT_CALL(*windows_api, PluginRegistrarGetView).WillOnce(Return(
nullptr));
165 auto test_api =
static_cast<TestWindowsApi*
>(scoped_api_stub.
stub());
170 bool called_a =
false;
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;
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);
195 EXPECT_FALSE(handled);
198TEST(PluginRegistrarWindowsTest, StopsOnceHandled) {
199 auto windows_api = std::make_unique<TestWindowsApi>();
200 EXPECT_CALL(*windows_api, PluginRegistrarGetView).WillOnce(Return(
nullptr));
202 auto test_api =
static_cast<TestWindowsApi*
>(scoped_api_stub.
stub());
206 bool called_a =
false;
210 return std::optional<LRESULT>(7);
212 bool called_b =
false;
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);
228 EXPECT_NE(called_a, called_b);
int RegisterTopLevelWindowProcDelegate(WindowProcDelegate delegate)
std::shared_ptr< FlutterView > GetViewById(FlutterViewId view_id) const
void UnregisterTopLevelWindowProcDelegate(int proc_id)
void AddPlugin(std::unique_ptr< Plugin > plugin)
StubFlutterWindowsApi * stub()
bool(* FlutterDesktopWindowProcCallback)(HWND, UINT, WPARAM, LPARAM, void *, LRESULT *result)
int64_t FlutterDesktopViewId
struct FlutterDesktopView * FlutterDesktopViewRef
TEST(FrameTimingsRecorderTest, RecordVsync)
std::function< std::optional< LRESULT >(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)> WindowProcDelegate
#define EXPECT_TRUE(handle)