Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit dbb5284

Browse files
authored
[Windows] Add more cursor plugin tests (#38112)
* [Windows] Add more cursor plugin tests * Tweak * Add dependency
1 parent 6aa4ccd commit dbb5284

File tree

2 files changed

+219
-10
lines changed

2 files changed

+219
-10
lines changed

shell/platform/windows/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ executable("flutter_windows_unittests") {
234234
":flutter_windows_headers",
235235
":flutter_windows_source",
236236
"//flutter/shell/platform/common:common_cpp",
237+
"//flutter/shell/platform/common/client_wrapper:client_wrapper",
237238
"//flutter/shell/platform/embedder:embedder_as_internal_library",
238239
"//flutter/shell/platform/embedder:embedder_test_utils",
239240
"//flutter/testing",

shell/platform/windows/cursor_handler_unittests.cc

+218-10
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,229 @@
66
#include <memory>
77
#include <vector>
88

9+
#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_result_functions.h"
10+
#include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_message_codec.h"
11+
#include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_method_codec.h"
12+
#include "flutter/shell/platform/windows/flutter_windows_view.h"
13+
#include "flutter/shell/platform/windows/testing/mock_window_binding_handler.h"
14+
#include "flutter/shell/platform/windows/testing/test_binary_messenger.h"
915
#include "gmock/gmock.h"
1016
#include "gtest/gtest.h"
1117

1218
namespace flutter {
1319
namespace testing {
14-
TEST(CursorHandlerTest, CreateDummyCursor) {
15-
// Create a 4x4 rawBGRA dummy cursor buffer.
16-
std::vector<uint8_t> buffer;
17-
for (int i = 0; i < 4 * 4 * 4; i++) {
18-
buffer.push_back(0);
19-
}
20-
// Create the cursor from buffer provided above.
21-
auto cursor = GetCursorFromBuffer(buffer, 0.0, 0.0, 4, 4);
22-
// Expect cursor is not null.
23-
EXPECT_NE(cursor, nullptr);
20+
21+
namespace {
22+
using ::testing::_;
23+
using ::testing::NotNull;
24+
using ::testing::Return;
25+
26+
static constexpr char kChannelName[] = "flutter/mousecursor";
27+
28+
static constexpr char kActivateSystemCursorMethod[] = "activateSystemCursor";
29+
static constexpr char kCreateCustomCursorMethod[] =
30+
"createCustomCursor/windows";
31+
static constexpr char kSetCustomCursorMethod[] = "setCustomCursor/windows";
32+
static constexpr char kDeleteCustomCursorMethod[] =
33+
"deleteCustomCursor/windows";
34+
35+
void SimulateCursorMessage(TestBinaryMessenger* messenger,
36+
const std::string& method_name,
37+
std::unique_ptr<EncodableValue> arguments,
38+
MethodResult<EncodableValue>* result_handler) {
39+
MethodCall<> call(method_name, std::move(arguments));
40+
41+
auto message = StandardMethodCodec::GetInstance().EncodeMethodCall(call);
42+
43+
EXPECT_TRUE(messenger->SimulateEngineMessage(
44+
kChannelName, message->data(), message->size(),
45+
[&result_handler](const uint8_t* reply, size_t reply_size) {
46+
StandardMethodCodec::GetInstance().DecodeAndProcessResponseEnvelope(
47+
reply, reply_size, result_handler);
48+
}));
49+
}
50+
51+
} // namespace
52+
53+
TEST(CursorHandlerTest, ActivateSystemCursor) {
54+
TestBinaryMessenger messenger;
55+
MockWindowBindingHandler window;
56+
CursorHandler cursor_handler(&messenger, &window);
57+
58+
EXPECT_CALL(window, UpdateFlutterCursor("click")).Times(1);
59+
60+
bool success = false;
61+
MethodResultFunctions<> result_handler(
62+
[&success](const EncodableValue* result) {
63+
success = true;
64+
EXPECT_EQ(result, nullptr);
65+
},
66+
nullptr, nullptr);
67+
68+
SimulateCursorMessage(&messenger, kActivateSystemCursorMethod,
69+
std::make_unique<EncodableValue>(EncodableMap{
70+
{EncodableValue("device"), EncodableValue(0)},
71+
{EncodableValue("kind"), EncodableValue("click")},
72+
}),
73+
&result_handler);
74+
75+
EXPECT_TRUE(success);
76+
}
77+
78+
TEST(CursorHandlerTest, CreateCustomCursor) {
79+
TestBinaryMessenger messenger;
80+
MockWindowBindingHandler window;
81+
CursorHandler cursor_handler(&messenger, &window);
82+
83+
// Create a 4x4 raw BGRA test cursor buffer.
84+
std::vector<uint8_t> buffer(4 * 4 * 4, 0);
85+
86+
bool success = false;
87+
MethodResultFunctions<> result_handler(
88+
[&success](const EncodableValue* result) {
89+
success = true;
90+
EXPECT_EQ(std::get<std::string>(*result), "hello");
91+
},
92+
nullptr, nullptr);
93+
94+
SimulateCursorMessage(&messenger, kCreateCustomCursorMethod,
95+
std::make_unique<EncodableValue>(EncodableMap{
96+
{EncodableValue("name"), EncodableValue("hello")},
97+
{EncodableValue("buffer"), EncodableValue(buffer)},
98+
{EncodableValue("width"), EncodableValue(4)},
99+
{EncodableValue("height"), EncodableValue(4)},
100+
{EncodableValue("hotX"), EncodableValue(0.0)},
101+
{EncodableValue("hotY"), EncodableValue(0.0)},
102+
}),
103+
&result_handler);
104+
105+
EXPECT_TRUE(success);
106+
}
107+
108+
TEST(CursorHandlerTest, SetCustomCursor) {
109+
TestBinaryMessenger messenger;
110+
MockWindowBindingHandler window;
111+
CursorHandler cursor_handler(&messenger, &window);
112+
113+
// Create a 4x4 raw BGRA test cursor buffer.
114+
std::vector<uint8_t> buffer(4 * 4 * 4, 0);
115+
116+
bool success = false;
117+
MethodResultFunctions<> create_result_handler(nullptr, nullptr, nullptr);
118+
MethodResultFunctions<> set_result_handler(
119+
[&success](const EncodableValue* result) {
120+
success = true;
121+
EXPECT_EQ(result, nullptr);
122+
},
123+
nullptr, nullptr);
124+
125+
EXPECT_CALL(window, SetFlutterCursor(/*cursor=*/NotNull())).Times(1);
126+
127+
SimulateCursorMessage(&messenger, kCreateCustomCursorMethod,
128+
std::make_unique<EncodableValue>(EncodableMap{
129+
{EncodableValue("name"), EncodableValue("hello")},
130+
{EncodableValue("buffer"), EncodableValue(buffer)},
131+
{EncodableValue("width"), EncodableValue(4)},
132+
{EncodableValue("height"), EncodableValue(4)},
133+
{EncodableValue("hotX"), EncodableValue(0.0)},
134+
{EncodableValue("hotY"), EncodableValue(0.0)},
135+
}),
136+
&create_result_handler);
137+
138+
SimulateCursorMessage(&messenger, kSetCustomCursorMethod,
139+
std::make_unique<EncodableValue>(EncodableMap{
140+
{EncodableValue("name"), EncodableValue("hello")},
141+
}),
142+
&set_result_handler);
143+
144+
EXPECT_TRUE(success);
145+
}
146+
147+
TEST(CursorHandlerTest, SetNonexistentCustomCursor) {
148+
TestBinaryMessenger messenger;
149+
MockWindowBindingHandler window;
150+
CursorHandler cursor_handler(&messenger, &window);
151+
152+
bool error = false;
153+
MethodResultFunctions<> result_handler(
154+
nullptr,
155+
[&error](const std::string& error_code, const std::string& error_message,
156+
const EncodableValue* value) {
157+
error = true;
158+
EXPECT_EQ(
159+
error_message,
160+
"The custom cursor identified by the argument key cannot be found");
161+
},
162+
nullptr);
163+
164+
EXPECT_CALL(window, SetFlutterCursor).Times(0);
165+
166+
SimulateCursorMessage(&messenger, kSetCustomCursorMethod,
167+
std::make_unique<EncodableValue>(EncodableMap{
168+
{EncodableValue("name"), EncodableValue("hello")},
169+
}),
170+
&result_handler);
171+
172+
EXPECT_TRUE(error);
173+
}
174+
175+
TEST(CursorHandlerTest, DeleteCustomCursor) {
176+
TestBinaryMessenger messenger;
177+
MockWindowBindingHandler window;
178+
CursorHandler cursor_handler(&messenger, &window);
179+
180+
// Create a 4x4 raw BGRA test cursor buffer.
181+
std::vector<uint8_t> buffer(4 * 4 * 4, 0);
182+
183+
bool success = false;
184+
MethodResultFunctions<> create_result_handler(nullptr, nullptr, nullptr);
185+
MethodResultFunctions<> delete_result_handler(
186+
[&success](const EncodableValue* result) {
187+
success = true;
188+
EXPECT_EQ(result, nullptr);
189+
},
190+
nullptr, nullptr);
191+
192+
SimulateCursorMessage(&messenger, kCreateCustomCursorMethod,
193+
std::make_unique<EncodableValue>(EncodableMap{
194+
{EncodableValue("name"), EncodableValue("hello")},
195+
{EncodableValue("buffer"), EncodableValue(buffer)},
196+
{EncodableValue("width"), EncodableValue(4)},
197+
{EncodableValue("height"), EncodableValue(4)},
198+
{EncodableValue("hotX"), EncodableValue(0.0)},
199+
{EncodableValue("hotY"), EncodableValue(0.0)},
200+
}),
201+
&create_result_handler);
202+
203+
SimulateCursorMessage(&messenger, kDeleteCustomCursorMethod,
204+
std::make_unique<EncodableValue>(EncodableMap{
205+
{EncodableValue("name"), EncodableValue("hello")},
206+
}),
207+
&delete_result_handler);
208+
209+
EXPECT_TRUE(success);
210+
}
211+
212+
TEST(CursorHandlerTest, DeleteNonexistentCustomCursor) {
213+
TestBinaryMessenger messenger;
214+
MockWindowBindingHandler handler;
215+
CursorHandler cursor_handler(&messenger, &handler);
216+
217+
bool success = false;
218+
MethodResultFunctions<> result_handler(
219+
[&success](const EncodableValue* result) {
220+
success = true;
221+
EXPECT_EQ(result, nullptr);
222+
},
223+
nullptr, nullptr);
224+
225+
SimulateCursorMessage(&messenger, kDeleteCustomCursorMethod,
226+
std::make_unique<EncodableValue>(EncodableMap{
227+
{EncodableValue("name"), EncodableValue("fake")},
228+
}),
229+
&result_handler);
230+
231+
EXPECT_TRUE(success);
24232
}
25233

26234
} // namespace testing

0 commit comments

Comments
 (0)