Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 86 additions & 35 deletions shell/platform/embedder/tests/embedder_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2128,21 +2128,33 @@ TEST_F(EmbedderTest, KeyDataIsCorrectlySerialized) {
message_latch->Signal();
};

auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext);
EmbedderConfigBuilder builder(context);
builder.SetSoftwareRendererConfig();
builder.SetDartEntrypoint("key_data_echo");
auto platform_task_runner = CreateNewThread("platform_thread");

UniqueEngine engine;
fml::AutoResetWaitableEvent ready;
context.AddNativeCallback(
"SignalNativeTest",
CREATE_NATIVE_ENTRY(
[&ready](Dart_NativeArguments args) { ready.Signal(); }));
platform_task_runner->PostTask([&]() {
auto& context =
GetEmbedderContext(EmbedderTestContextType::kSoftwareContext);
EmbedderConfigBuilder builder(context);
builder.SetSoftwareRendererConfig();
builder.SetDartEntrypoint("key_data_echo");
builder.SetPlatformMessageCallback(
[&](const FlutterPlatformMessage* message) {
FlutterEngineSendPlatformMessageResponse(
engine.get(), message->response_handle, nullptr, 0);
});
context.AddNativeCallback(
"SignalNativeTest",
CREATE_NATIVE_ENTRY(
[&ready](Dart_NativeArguments args) { ready.Signal(); }));

context.AddNativeCallback("EchoKeyEvent",
CREATE_NATIVE_ENTRY(native_echo_event));
context.AddNativeCallback("EchoKeyEvent",
CREATE_NATIVE_ENTRY(native_echo_event));

engine = builder.LaunchEngine();
ASSERT_TRUE(engine.is_valid());
});

auto engine = builder.LaunchEngine();
ASSERT_TRUE(engine.is_valid());
ready.Wait();

// A normal down event
Expand All @@ -2156,8 +2168,10 @@ TEST_F(EmbedderTest, KeyDataIsCorrectlySerialized) {
.synthesized = false,
.device_type = kFlutterKeyEventDeviceTypeKeyboard,
};
FlutterEngineSendKeyEvent(engine.get(), &down_event_upper_a, nullptr,
nullptr);
platform_task_runner->PostTask([&]() {
FlutterEngineSendKeyEvent(engine.get(), &down_event_upper_a, nullptr,
nullptr);
});
message_latch->Wait();

ExpectKeyEventEq(echoed_event, down_event_upper_a);
Expand All @@ -2174,8 +2188,10 @@ TEST_F(EmbedderTest, KeyDataIsCorrectlySerialized) {
.synthesized = false,
.device_type = kFlutterKeyEventDeviceTypeKeyboard,
};
FlutterEngineSendKeyEvent(engine.get(), &repeat_event_wide_char, nullptr,
nullptr);
platform_task_runner->PostTask([&]() {
FlutterEngineSendKeyEvent(engine.get(), &repeat_event_wide_char, nullptr,
nullptr);
});
message_latch->Wait();

ExpectKeyEventEq(echoed_event, repeat_event_wide_char);
Expand All @@ -2192,11 +2208,20 @@ TEST_F(EmbedderTest, KeyDataIsCorrectlySerialized) {
.synthesized = true,
.device_type = kFlutterKeyEventDeviceTypeKeyboard,
};
FlutterEngineSendKeyEvent(engine.get(), &up_event, nullptr, nullptr);
platform_task_runner->PostTask([&]() {
FlutterEngineSendKeyEvent(engine.get(), &up_event, nullptr, nullptr);
});
message_latch->Wait();

ExpectKeyEventEq(echoed_event, up_event);
EXPECT_EQ(echoed_char, 0llu);

fml::AutoResetWaitableEvent shutdown_latch;
platform_task_runner->PostTask([&]() {
engine.reset();
shutdown_latch.Signal();
});
shutdown_latch.Wait();
}

TEST_F(EmbedderTest, KeyDataAreBuffered) {
Expand Down Expand Up @@ -2225,21 +2250,32 @@ TEST_F(EmbedderTest, KeyDataAreBuffered) {
message_latch->Signal();
};

auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext);
EmbedderConfigBuilder builder(context);
builder.SetSoftwareRendererConfig();
builder.SetDartEntrypoint("key_data_late_echo");
auto platform_task_runner = CreateNewThread("platform_thread");

UniqueEngine engine;
fml::AutoResetWaitableEvent ready;
context.AddNativeCallback(
"SignalNativeTest",
CREATE_NATIVE_ENTRY(
[&ready](Dart_NativeArguments args) { ready.Signal(); }));
platform_task_runner->PostTask([&]() {
auto& context =
GetEmbedderContext(EmbedderTestContextType::kSoftwareContext);
EmbedderConfigBuilder builder(context);
builder.SetSoftwareRendererConfig();
builder.SetDartEntrypoint("key_data_late_echo");
builder.SetPlatformMessageCallback(
[&](const FlutterPlatformMessage* message) {
FlutterEngineSendPlatformMessageResponse(
engine.get(), message->response_handle, nullptr, 0);
});
context.AddNativeCallback(
"SignalNativeTest",
CREATE_NATIVE_ENTRY(
[&ready](Dart_NativeArguments args) { ready.Signal(); }));

context.AddNativeCallback("EchoKeyEvent",
CREATE_NATIVE_ENTRY(native_echo_event));
context.AddNativeCallback("EchoKeyEvent",
CREATE_NATIVE_ENTRY(native_echo_event));

auto engine = builder.LaunchEngine();
ASSERT_TRUE(engine.is_valid());
engine = builder.LaunchEngine();
ASSERT_TRUE(engine.is_valid());
});
ready.Wait();

FlutterKeyEvent sample_event{
Expand All @@ -2254,7 +2290,11 @@ TEST_F(EmbedderTest, KeyDataAreBuffered) {

// Send an event.
sample_event.timestamp = 1.0l;
FlutterEngineSendKeyEvent(engine.get(), &sample_event, nullptr, nullptr);
platform_task_runner->PostTask([&]() {
FlutterEngineSendKeyEvent(engine.get(), &sample_event, nullptr, nullptr);
message_latch->Signal();
});
message_latch->Wait();

// Should not receive echos because the callback is not set yet.
EXPECT_EQ(echoed_events.size(), 0u);
Expand All @@ -2273,11 +2313,13 @@ TEST_F(EmbedderTest, KeyDataAreBuffered) {
.response_handle = response_handle,
};

FlutterEngineResult result =
FlutterEngineSendPlatformMessage(engine.get(), &message);
ASSERT_EQ(result, kSuccess);
platform_task_runner->PostTask([&]() {
FlutterEngineResult result =
FlutterEngineSendPlatformMessage(engine.get(), &message);
ASSERT_EQ(result, kSuccess);

FlutterPlatformMessageReleaseResponseHandle(engine.get(), response_handle);
FlutterPlatformMessageReleaseResponseHandle(engine.get(), response_handle);
});

// message_latch->Wait();
message_latch->Wait();
Expand All @@ -2286,11 +2328,20 @@ TEST_F(EmbedderTest, KeyDataAreBuffered) {

// Send a second event.
sample_event.timestamp = 10.0l;
FlutterEngineSendKeyEvent(engine.get(), &sample_event, nullptr, nullptr);
platform_task_runner->PostTask([&]() {
FlutterEngineSendKeyEvent(engine.get(), &sample_event, nullptr, nullptr);
});
message_latch->Wait();

// The event should be echoed, too.
EXPECT_EQ(echoed_events.size(), 2u);

fml::AutoResetWaitableEvent shutdown_latch;
platform_task_runner->PostTask([&]() {
engine.reset();
shutdown_latch.Signal();
});
shutdown_latch.Wait();
}

TEST_F(EmbedderTest, KeyDataResponseIsCorrectlyInvoked) {
Expand Down