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

Commit 8fd770b

Browse files
committed
added unit test
1 parent 225ca5f commit 8fd770b

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

shell/platform/windows/fixtures/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void hiPlatformChannels() {
3232
ui.PlatformDispatcher.instance
3333
.sendPlatformMessage('hi', reply, (ByteData? reply) {});
3434
});
35-
callback(null);
35+
callback(data);
3636
});
3737
}
3838

shell/platform/windows/flutter_windows_engine_unittests.cc

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,16 +295,70 @@ TEST_F(FlutterWindowsEngineTest, PlatformMessageRoundTrip) {
295295
binary_messenger->Send(
296296
channel, reinterpret_cast<uint8_t*>(payload), 5,
297297
[&did_call_reply](const uint8_t* reply, size_t reply_size) {
298-
EXPECT_EQ(reply_size, 3);
299-
EXPECT_EQ(reply[0], static_cast<uint8_t>('b'));
298+
EXPECT_EQ(reply_size, 5);
299+
EXPECT_EQ(reply[0], static_cast<uint8_t>('h'));
300300
did_call_reply = true;
301301
});
302302
// Rely on timeout mechanism in CI.
303-
while (!did_call_callback && !did_call_reply && !did_call_dart_reply) {
303+
while (!did_call_callback || !did_call_reply || !did_call_dart_reply) {
304304
engine->task_runner()->ProcessTasks();
305305
}
306306
}
307307

308+
TEST_F(FlutterWindowsEngineTest, PlatformMessageRespondOnDifferentThread) {
309+
FlutterDesktopEngineProperties properties = {};
310+
properties.assets_path = GetContext().GetAssetsPath().c_str();
311+
properties.icu_data_path = GetContext().GetIcuDataPath().c_str();
312+
properties.dart_entrypoint = "hiPlatformChannels";
313+
314+
FlutterProjectBundle project(properties);
315+
auto engine = std::make_unique<FlutterWindowsEngine>(project);
316+
317+
EngineModifier modifier(engine.get());
318+
modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; };
319+
320+
auto binary_messenger =
321+
std::make_unique<BinaryMessengerImpl>(engine->messenger());
322+
323+
engine->Run();
324+
bool did_call_callback = false;
325+
bool did_call_reply = false;
326+
bool did_call_dart_reply = false;
327+
std::string channel = "hi";
328+
std::unique_ptr<std::thread> reply_thread;
329+
binary_messenger->SetMessageHandler(
330+
channel,
331+
[&did_call_callback, &did_call_dart_reply, &reply_thread](
332+
const uint8_t* message, size_t message_size, BinaryReply reply) {
333+
if (message_size == 5) {
334+
EXPECT_EQ(message[0], static_cast<uint8_t>('h'));
335+
reply_thread.reset(new std::thread([reply = std::move(reply)]() {
336+
char response[] = {'b', 'y', 'e'};
337+
reply(reinterpret_cast<uint8_t*>(response), 3);
338+
}));
339+
did_call_callback = true;
340+
} else {
341+
EXPECT_EQ(message_size, 3);
342+
EXPECT_EQ(message[0], static_cast<uint8_t>('b'));
343+
did_call_dart_reply = true;
344+
}
345+
});
346+
char payload[] = {'h', 'e', 'l', 'l', 'o'};
347+
binary_messenger->Send(
348+
channel, reinterpret_cast<uint8_t*>(payload), 5,
349+
[&did_call_reply](const uint8_t* reply, size_t reply_size) {
350+
EXPECT_EQ(reply_size, 5);
351+
EXPECT_EQ(reply[0], static_cast<uint8_t>('h'));
352+
did_call_reply = true;
353+
});
354+
// Rely on timeout mechanism in CI.
355+
while (!did_call_callback || !did_call_reply || !did_call_dart_reply) {
356+
engine->task_runner()->ProcessTasks();
357+
}
358+
ASSERT_TRUE(reply_thread);
359+
reply_thread->join();
360+
}
361+
308362
TEST_F(FlutterWindowsEngineTest, SendPlatformMessageWithResponse) {
309363
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
310364
EngineModifier modifier(engine.get());

0 commit comments

Comments
 (0)