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

Commit 2c5c73b

Browse files
committed
Added integration test for platform channels on windows.
1 parent cd2937e commit 2c5c73b

File tree

2 files changed

+83
-18
lines changed

2 files changed

+83
-18
lines changed

shell/platform/windows/fixtures/main.dart

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'dart:io' as io;
6+
import 'dart:typed_data' show ByteData;
67
import 'dart:ui' as ui;
78

89
// Signals a waiting latch in the native test.
@@ -20,13 +21,24 @@ bool signalBoolReturn() native 'SignalBoolReturn';
2021
// Notify the native test that the first frame has been scheduled.
2122
void notifyFirstFrameScheduled() native 'NotifyFirstFrameScheduled';
2223

23-
void main() {
24-
}
24+
void main() {}
2525

2626
@pragma('vm:entry-point')
27-
void customEntrypoint() {
27+
void hiPlatformChannels() {
28+
ui.channelBuffers.setListener('hi',
29+
(ByteData? data, ui.PlatformMessageResponseCallback callback) async {
30+
ui.PlatformDispatcher.instance.sendPlatformMessage('hi', data,
31+
(ByteData? reply) {
32+
ui.PlatformDispatcher.instance
33+
.sendPlatformMessage('hi', reply, (ByteData? reply) {});
34+
});
35+
callback(null);
36+
});
2837
}
2938

39+
@pragma('vm:entry-point')
40+
void customEntrypoint() {}
41+
3042
@pragma('vm:entry-point')
3143
void verifyNativeFunction() {
3244
signal();
@@ -51,8 +63,8 @@ void readPlatformExecutable() {
5163
@pragma('vm:entry-point')
5264
void drawHelloWorld() {
5365
ui.PlatformDispatcher.instance.onBeginFrame = (Duration duration) {
54-
final ui.ParagraphBuilder paragraphBuilder = ui.ParagraphBuilder(ui.ParagraphStyle())
55-
..addText('Hello world');
66+
final ui.ParagraphBuilder paragraphBuilder =
67+
ui.ParagraphBuilder(ui.ParagraphStyle())..addText('Hello world');
5668
final ui.Paragraph paragraph = paragraphBuilder.build();
5769

5870
paragraph.layout(const ui.ParagraphConstraints(width: 800.0));

shell/platform/windows/flutter_windows_engine_unittests.cc

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
99
#include "flutter/shell/platform/windows/testing/engine_modifier.h"
1010
#include "flutter/shell/platform/windows/testing/test_keyboard.h"
11+
#include "flutter/shell/platform/windows/testing/windows_test.h"
12+
#include "fml/synchronization/waitable_event.h"
1113
#include "gtest/gtest.h"
1214

1315
// winbase.h defines GetCurrentTime as a macro.
@@ -40,7 +42,9 @@ std::unique_ptr<FlutterWindowsEngine> GetTestEngine() {
4042
}
4143
} // namespace
4244

43-
TEST(FlutterWindowsEngine, RunDoesExpectedInitialization) {
45+
class FlutterWindowsEngineTest : public WindowsTest {};
46+
47+
TEST_F(FlutterWindowsEngineTest, RunDoesExpectedInitialization) {
4448
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
4549
EngineModifier modifier(engine.get());
4650

@@ -148,7 +152,7 @@ TEST(FlutterWindowsEngine, RunDoesExpectedInitialization) {
148152
modifier.ReleaseSurfaceManager();
149153
}
150154

151-
TEST(FlutterWindowsEngine, ConfiguresFrameVsync) {
155+
TEST_F(FlutterWindowsEngineTest, ConfiguresFrameVsync) {
152156
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
153157
EngineModifier modifier(engine.get());
154158
bool on_vsync_called = false;
@@ -174,7 +178,7 @@ TEST(FlutterWindowsEngine, ConfiguresFrameVsync) {
174178
EXPECT_TRUE(on_vsync_called);
175179
}
176180

177-
TEST(FlutterWindowsEngine, RunWithoutANGLEUsesSoftware) {
181+
TEST_F(FlutterWindowsEngineTest, RunWithoutANGLEUsesSoftware) {
178182
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
179183
EngineModifier modifier(engine.get());
180184

@@ -226,7 +230,7 @@ TEST(FlutterWindowsEngine, RunWithoutANGLEUsesSoftware) {
226230
modifier.embedder_api().Shutdown = [](auto engine) { return kSuccess; };
227231
}
228232

229-
TEST(FlutterWindowsEngine, SendPlatformMessageWithoutResponse) {
233+
TEST_F(FlutterWindowsEngineTest, SendPlatformMessageWithoutResponse) {
230234
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
231235
EngineModifier modifier(engine.get());
232236

@@ -252,7 +256,56 @@ TEST(FlutterWindowsEngine, SendPlatformMessageWithoutResponse) {
252256
EXPECT_TRUE(called);
253257
}
254258

255-
TEST(FlutterWindowsEngine, SendPlatformMessageWithResponse) {
259+
TEST_F(FlutterWindowsEngineTest, PlatformMessageRoundTrip) {
260+
FlutterDesktopEngineProperties properties = {};
261+
properties.assets_path = GetContext().GetAssetsPath().c_str();
262+
properties.icu_data_path = GetContext().GetIcuDataPath().c_str();
263+
properties.dart_entrypoint = "hiPlatformChannels";
264+
265+
FlutterProjectBundle project(properties);
266+
auto engine = std::make_unique<FlutterWindowsEngine>(project);
267+
268+
EngineModifier modifier(engine.get());
269+
modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; };
270+
271+
auto binary_messenger =
272+
std::make_unique<BinaryMessengerImpl>(engine->messenger());
273+
274+
engine->Run();
275+
bool did_call_callback = false;
276+
bool did_call_reply = false;
277+
bool did_call_dart_reply = false;
278+
std::string channel = "hi";
279+
binary_messenger->SetMessageHandler(
280+
channel,
281+
[&did_call_callback, &did_call_dart_reply](
282+
const uint8_t* message, size_t message_size, BinaryReply reply) {
283+
if (message_size == 5) {
284+
EXPECT_EQ(message[0], static_cast<uint8_t>('h'));
285+
char response[] = {'b', 'y', 'e'};
286+
reply(reinterpret_cast<uint8_t*>(response), 3);
287+
did_call_callback = true;
288+
} else {
289+
EXPECT_EQ(message_size, 3);
290+
EXPECT_EQ(message[0], static_cast<uint8_t>('b'));
291+
did_call_dart_reply = true;
292+
}
293+
});
294+
char payload[] = {'h', 'e', 'l', 'l', 'o'};
295+
binary_messenger->Send(
296+
channel, reinterpret_cast<uint8_t*>(payload), 5,
297+
[&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'));
300+
did_call_reply = true;
301+
});
302+
// Rely on timeout mechanism in CI.
303+
while (!did_call_callback && !did_call_reply && !did_call_dart_reply) {
304+
engine->task_runner()->ProcessTasks();
305+
}
306+
}
307+
308+
TEST_F(FlutterWindowsEngineTest, SendPlatformMessageWithResponse) {
256309
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
257310
EngineModifier modifier(engine.get());
258311

@@ -310,7 +363,7 @@ TEST(FlutterWindowsEngine, SendPlatformMessageWithResponse) {
310363
EXPECT_TRUE(send_message_called);
311364
}
312365

313-
TEST(FlutterWindowsEngine, DispatchSemanticsAction) {
366+
TEST_F(FlutterWindowsEngineTest, DispatchSemanticsAction) {
314367
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
315368
EngineModifier modifier(engine.get());
316369

@@ -334,7 +387,7 @@ TEST(FlutterWindowsEngine, DispatchSemanticsAction) {
334387
EXPECT_TRUE(called);
335388
}
336389

337-
TEST(FlutterWindowsEngine, SetsThreadPriority) {
390+
TEST_F(FlutterWindowsEngineTest, SetsThreadPriority) {
338391
WindowsPlatformThreadPrioritySetter(FlutterThreadPriority::kBackground);
339392
EXPECT_EQ(GetThreadPriority(GetCurrentThread()),
340393
THREAD_PRIORITY_BELOW_NORMAL);
@@ -355,7 +408,7 @@ TEST(FlutterWindowsEngine, SetsThreadPriority) {
355408
EXPECT_EQ(GetThreadPriority(GetCurrentThread()), THREAD_PRIORITY_NORMAL);
356409
}
357410

358-
TEST(FlutterWindowsEngine, AddPluginRegistrarDestructionCallback) {
411+
TEST_F(FlutterWindowsEngineTest, AddPluginRegistrarDestructionCallback) {
359412
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
360413
EngineModifier modifier(engine.get());
361414

@@ -385,7 +438,7 @@ TEST(FlutterWindowsEngine, AddPluginRegistrarDestructionCallback) {
385438
EXPECT_EQ(result2, 2);
386439
}
387440

388-
TEST(FlutterWindowsEngine, ScheduleFrame) {
441+
TEST_F(FlutterWindowsEngineTest, ScheduleFrame) {
389442
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
390443
EngineModifier modifier(engine.get());
391444

@@ -400,7 +453,7 @@ TEST(FlutterWindowsEngine, ScheduleFrame) {
400453
EXPECT_TRUE(called);
401454
}
402455

403-
TEST(FlutterWindowsEngine, SetNextFrameCallback) {
456+
TEST_F(FlutterWindowsEngineTest, SetNextFrameCallback) {
404457
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
405458
EngineModifier modifier(engine.get());
406459

@@ -415,14 +468,14 @@ TEST(FlutterWindowsEngine, SetNextFrameCallback) {
415468
EXPECT_TRUE(called);
416469
}
417470

418-
TEST(FlutterWindowsEngine, GetExecutableName) {
471+
TEST_F(FlutterWindowsEngineTest, GetExecutableName) {
419472
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
420473
EXPECT_EQ(engine->GetExecutableName(), "flutter_windows_unittests.exe");
421474
}
422475

423476
// Ensure that after setting or resetting the high contrast feature,
424477
// the corresponding status flag can be retrieved from the engine.
425-
TEST(FlutterWindowsEngine, UpdateHighContrastFeature) {
478+
TEST_F(FlutterWindowsEngineTest, UpdateHighContrastFeature) {
426479
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
427480
EngineModifier modifier(engine.get());
428481

@@ -447,7 +500,7 @@ TEST(FlutterWindowsEngine, UpdateHighContrastFeature) {
447500
EXPECT_FALSE(engine->high_contrast_enabled());
448501
}
449502

450-
TEST(FlutterWindowsEngine, PostRasterThreadTask) {
503+
TEST_F(FlutterWindowsEngineTest, PostRasterThreadTask) {
451504
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
452505
EngineModifier modifier(engine.get());
453506

0 commit comments

Comments
 (0)