17#include "gmock/gmock.h"
18#include "gtest/gtest.h"
19#include "rapidjson/document.h"
26using ::testing::NiceMock;
27using ::testing::Return;
29static constexpr char kChannelName[] =
"flutter/platform";
31static constexpr char kClipboardGetDataMessage[] =
32 "{\"method\":\"Clipboard.getData\",\"args\":\"text/plain\"}";
33static constexpr char kClipboardGetDataFakeContentTypeMessage[] =
34 "{\"method\":\"Clipboard.getData\",\"args\":\"text/madeupcontenttype\"}";
35static constexpr char kClipboardHasStringsMessage[] =
36 "{\"method\":\"Clipboard.hasStrings\",\"args\":\"text/plain\"}";
37static constexpr char kClipboardHasStringsFakeContentTypeMessage[] =
38 "{\"method\":\"Clipboard.hasStrings\",\"args\":\"text/madeupcontenttype\"}";
39static constexpr char kClipboardSetDataMessage[] =
40 "{\"method\":\"Clipboard.setData\",\"args\":{\"text\":\"hello\"}}";
41static constexpr char kClipboardSetDataNullTerminatorMessage[] =
42 "{\"method\":\"Clipboard.setData\",\"args\":{\"text\":"
43 "\"hello\\u0000world\"}}";
44static constexpr char kClipboardSetDataNullTextMessage[] =
45 "{\"method\":\"Clipboard.setData\",\"args\":{\"text\":null}}";
46static constexpr char kClipboardSetDataUnknownTypeMessage[] =
47 "{\"method\":\"Clipboard.setData\",\"args\":{\"madeuptype\":\"hello\"}}";
48static constexpr char kSystemSoundTypeAlertMessage[] =
49 "{\"method\":\"SystemSound.play\",\"args\":\"SystemSoundType.alert\"}";
50static constexpr char kSystemExitApplicationRequiredMessage[] =
51 "{\"method\":\"System.exitApplication\",\"args\":{\"type\":\"required\","
53static constexpr char kSystemExitApplicationCancelableMessage[] =
54 "{\"method\":\"System.exitApplication\",\"args\":{\"type\":\"cancelable\","
56static constexpr char kExitResponseCancelMessage[] =
57 "[{\"response\":\"cancel\"}]";
58static constexpr char kExitResponseExitMessage[] =
"[{\"response\":\"exit\"}]";
62static constexpr int kArbitraryErrorCode = 1;
66class MockPlatformHandler :
public PlatformHandler {
68 explicit MockPlatformHandler(
69 BinaryMessenger* messenger,
70 FlutterWindowsEngine*
engine,
71 std::optional<std::function<std::unique_ptr<ScopedClipboardInterface>()>>
72 scoped_clipboard_provider = std::nullopt)
73 : PlatformHandler(messenger,
engine, scoped_clipboard_provider) {}
75 virtual ~MockPlatformHandler() =
default;
79 (std::unique_ptr<MethodResult<rapidjson::Document>>,
80 std::string_view
key),
84 (std::unique_ptr<MethodResult<rapidjson::Document>>),
89 std::unique_ptr<MethodResult<rapidjson::Document>>),
94 std::unique_ptr<MethodResult<rapidjson::Document>>),
99 (std::optional<HWND> hwnd,
100 std::optional<WPARAM> wparam,
101 std::optional<LPARAM> lparam,
110class MockScopedClipboard :
public ScopedClipboardInterface {
112 MockScopedClipboard() =
default;
113 virtual ~MockScopedClipboard() =
default;
115 MOCK_METHOD(
int, Open, (HWND
window), (
override));
116 MOCK_METHOD(
bool, HasString, (), (
override));
117 MOCK_METHOD((std::variant<std::wstring, int>), GetString, (), (
override));
118 MOCK_METHOD(
int, SetString, (
const std::wstring
string), (
override));
124std::string SimulatePlatformMessage(TestBinaryMessenger* messenger,
127 EXPECT_TRUE(messenger->SimulateEngineMessage(
130 [result = &result](
const uint8_t* reply,
size_t reply_size) {
131 std::string response(reinterpret_cast<const char*>(reply), reply_size);
152 engine_ = builder.Build();
156 std::unique_ptr<FlutterWindowsEngine> engine_;
157 std::unique_ptr<FlutterWindowsView> view_;
165 TestBinaryMessenger messenger;
167 auto clipboard = std::make_unique<MockScopedClipboard>();
169 EXPECT_CALL(*clipboard.get(), Open)
172 EXPECT_CALL(*clipboard.get(), HasString).Times(1).WillOnce(Return(
true));
173 EXPECT_CALL(*clipboard.get(), GetString)
175 .WillOnce(Return(std::wstring(L
"Hello world")));
181 SimulatePlatformMessage(&messenger, kClipboardGetDataMessage);
183 EXPECT_EQ(result,
"[{\"text\":\"Hello world\"}]");
189 TestBinaryMessenger messenger;
193 std::string result = SimulatePlatformMessage(
194 &messenger, kClipboardGetDataFakeContentTypeMessage);
196 EXPECT_EQ(result,
"[\"Clipboard error\",\"Unknown clipboard format\",null]");
202 TestBinaryMessenger messenger;
204 auto clipboard = std::make_unique<MockScopedClipboard>();
206 EXPECT_CALL(*clipboard.get(), Open)
208 .WillOnce(Return(kArbitraryErrorCode));
214 SimulatePlatformMessage(&messenger, kClipboardGetDataMessage);
216 EXPECT_EQ(result,
"[\"Clipboard error\",\"Unable to open clipboard\",1]");
222 TestBinaryMessenger messenger;
224 auto clipboard = std::make_unique<MockScopedClipboard>();
226 EXPECT_CALL(*clipboard.get(), Open)
229 EXPECT_CALL(*clipboard.get(), HasString).Times(1).WillOnce(Return(
true));
230 EXPECT_CALL(*clipboard.get(), GetString)
232 .WillOnce(Return(kArbitraryErrorCode));
238 SimulatePlatformMessage(&messenger, kClipboardGetDataMessage);
240 EXPECT_EQ(result,
"[\"Clipboard error\",\"Unable to get clipboard data\",1]");
246 TestBinaryMessenger messenger;
248 auto clipboard = std::make_unique<MockScopedClipboard>();
250 EXPECT_CALL(*clipboard.get(), Open)
253 EXPECT_CALL(*clipboard.get(), HasString).Times(1).WillOnce(Return(
true));
259 SimulatePlatformMessage(&messenger, kClipboardHasStringsMessage);
261 EXPECT_EQ(result,
"[{\"value\":true}]");
267 TestBinaryMessenger messenger;
269 auto clipboard = std::make_unique<MockScopedClipboard>();
271 EXPECT_CALL(*clipboard.get(), Open)
274 EXPECT_CALL(*clipboard.get(), HasString).Times(1).WillOnce(Return(
false));
280 SimulatePlatformMessage(&messenger, kClipboardHasStringsMessage);
282 EXPECT_EQ(result,
"[{\"value\":false}]");
288 TestBinaryMessenger messenger;
291 std::string result = SimulatePlatformMessage(
292 &messenger, kClipboardHasStringsFakeContentTypeMessage);
294 EXPECT_EQ(result,
"[\"Clipboard error\",\"Unknown clipboard format\",null]");
301 TestBinaryMessenger messenger;
303 auto clipboard = std::make_unique<MockScopedClipboard>();
305 EXPECT_CALL(*clipboard.get(), Open)
313 SimulatePlatformMessage(&messenger, kClipboardHasStringsMessage);
315 EXPECT_EQ(result,
"[{\"value\":false}]");
321 TestBinaryMessenger messenger;
323 auto clipboard = std::make_unique<MockScopedClipboard>();
325 EXPECT_CALL(*clipboard.get(), Open)
327 .WillOnce(Return(kArbitraryErrorCode));
333 SimulatePlatformMessage(&messenger, kClipboardHasStringsMessage);
335 EXPECT_EQ(result,
"[\"Clipboard error\",\"Unable to open clipboard\",1]");
341 TestBinaryMessenger messenger;
343 auto clipboard = std::make_unique<MockScopedClipboard>();
345 EXPECT_CALL(*clipboard.get(), Open)
348 EXPECT_CALL(*clipboard.get(), SetString)
350 .WillOnce([](std::wstring
string) {
351 EXPECT_EQ(
string, L
"hello");
359 SimulatePlatformMessage(&messenger, kClipboardSetDataMessage);
361 EXPECT_EQ(result,
"[null]");
368 TestBinaryMessenger messenger;
370 auto clipboard = std::make_unique<MockScopedClipboard>();
372 EXPECT_CALL(*clipboard.get(), Open)
375 EXPECT_CALL(*clipboard.get(), SetString)
377 .WillOnce([](std::wstring
string) {
378 EXPECT_EQ(
string, L
"hello\uFFFDworld");
385 std::string result = SimulatePlatformMessage(
386 &messenger, kClipboardSetDataNullTerminatorMessage);
388 EXPECT_EQ(result,
"[null]");
395 TestBinaryMessenger messenger;
399 SimulatePlatformMessage(&messenger, kClipboardSetDataNullTextMessage);
401 EXPECT_EQ(result,
"[\"Clipboard error\",\"Unknown clipboard format\",null]");
407 TestBinaryMessenger messenger;
411 SimulatePlatformMessage(&messenger, kClipboardSetDataUnknownTypeMessage);
413 EXPECT_EQ(result,
"[\"Clipboard error\",\"Unknown clipboard format\",null]");
419 TestBinaryMessenger messenger;
421 auto clipboard = std::make_unique<MockScopedClipboard>();
423 EXPECT_CALL(*clipboard.get(), Open)
425 .WillOnce(Return(kArbitraryErrorCode));
431 SimulatePlatformMessage(&messenger, kClipboardSetDataMessage);
433 EXPECT_EQ(result,
"[\"Clipboard error\",\"Unable to open clipboard\",1]");
439 TestBinaryMessenger messenger;
441 auto clipboard = std::make_unique<MockScopedClipboard>();
443 EXPECT_CALL(*clipboard.get(), Open)
446 EXPECT_CALL(*clipboard.get(), SetString)
448 .WillOnce(Return(kArbitraryErrorCode));
454 SimulatePlatformMessage(&messenger, kClipboardSetDataMessage);
456 EXPECT_EQ(result,
"[\"Clipboard error\",\"Unable to set clipboard data\",1]");
462 TestBinaryMessenger messenger;
463 MockPlatformHandler platform_handler(&messenger,
engine());
465 EXPECT_CALL(platform_handler, SystemSoundPlay(
"SystemSoundType.alert", _))
466 .WillOnce([](
const std::string& sound,
472 SimulatePlatformMessage(&messenger, kSystemSoundTypeAlertMessage);
474 EXPECT_EQ(result,
"[null]");
481 TestBinaryMessenger messenger([](
const std::string&
channel,
484 MockPlatformHandler platform_handler(&messenger,
engine());
486 ON_CALL(platform_handler, QuitApplication)
487 .WillByDefault([&exit_code](std::optional<HWND> hwnd,
488 std::optional<WPARAM> wparam,
489 std::optional<LPARAM> lparam,
490 UINT ec) { exit_code = ec; });
491 EXPECT_CALL(platform_handler, QuitApplication).Times(1);
493 std::string result = SimulatePlatformMessage(
494 &messenger, kSystemExitApplicationRequiredMessage);
495 EXPECT_EQ(result,
"[{\"response\":\"exit\"}]");
496 EXPECT_EQ(exit_code, 1);
501 bool called_cancel =
false;
503 TestBinaryMessenger messenger(
504 [&called_cancel](
const std::string&
channel,
const uint8_t*
message,
506 reply(
reinterpret_cast<const uint8_t*
>(kExitResponseCancelMessage),
507 sizeof(kExitResponseCancelMessage));
508 called_cancel =
true;
510 MockPlatformHandler platform_handler(&messenger,
engine());
512 EXPECT_CALL(platform_handler, QuitApplication).Times(0);
514 std::string result = SimulatePlatformMessage(
515 &messenger, kSystemExitApplicationCancelableMessage);
516 EXPECT_EQ(result,
"[{\"response\":\"cancel\"}]");
517 EXPECT_TRUE(called_cancel);
522 bool called_cancel =
false;
525 TestBinaryMessenger messenger(
526 [&called_cancel](
const std::string&
channel,
const uint8_t*
message,
528 reply(
reinterpret_cast<const uint8_t*
>(kExitResponseExitMessage),
529 sizeof(kExitResponseExitMessage));
530 called_cancel =
true;
532 MockPlatformHandler platform_handler(&messenger,
engine());
534 ON_CALL(platform_handler, QuitApplication)
535 .WillByDefault([&exit_code](std::optional<HWND> hwnd,
536 std::optional<WPARAM> wparam,
537 std::optional<LPARAM> lparam,
538 UINT ec) { exit_code = ec; });
539 EXPECT_CALL(platform_handler, QuitApplication).Times(1);
541 std::string result = SimulatePlatformMessage(
542 &messenger, kSystemExitApplicationCancelableMessage);
543 EXPECT_EQ(result,
"[{\"response\":\"cancel\"}]");
544 EXPECT_TRUE(called_cancel);
545 EXPECT_EQ(exit_code, 2);
WindowsTestContext & GetContext()
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
TEST_F(DisplayListTest, Defaults)
it will be possible to load the file into Perfetto s trace viewer use test Running tests that layout and measure text will not yield consistent results across various platforms Enabling this option will make font resolution default to the Ahem test font on all disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
std::function< void(const uint8_t *reply, size_t reply_size)> BinaryReply