6 #include "gtest/gtest.h"
9 #include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
14 #include "flutter/shell/platform/linux/testing/fl_test.h"
20 TEST(FlEngineTest, WindowMetrics) {
25 embedder_api->SendWindowMetricsEvent = MOCK_ENGINE_PROC(
26 SendWindowMetricsEvent,
27 ([&called](
auto engine,
const FlutterWindowMetricsEvent*
event) {
29 EXPECT_EQ(
event->width,
static_cast<size_t>(3840));
30 EXPECT_EQ(
event->height,
static_cast<size_t>(2160));
31 EXPECT_EQ(
event->pixel_ratio, 2.0);
36 g_autoptr(GError)
error =
nullptr;
38 EXPECT_EQ(
error,
nullptr);
45 TEST(FlEngineTest, MousePointer) {
50 embedder_api->SendPointerEvent = MOCK_ENGINE_PROC(
52 ([&called](
auto engine,
const FlutterPointerEvent* events,
53 size_t events_count) {
55 EXPECT_EQ(events_count,
static_cast<size_t>(1));
56 EXPECT_EQ(events[0].phase, kDown);
57 EXPECT_EQ(events[0].timestamp,
static_cast<size_t>(1234567890));
58 EXPECT_EQ(events[0].x, 800);
59 EXPECT_EQ(events[0].y, 600);
60 EXPECT_EQ(events[0].device,
static_cast<int32_t
>(0));
61 EXPECT_EQ(events[0].signal_kind, kFlutterPointerSignalKindScroll);
62 EXPECT_EQ(events[0].scroll_delta_x, 1.2);
63 EXPECT_EQ(events[0].scroll_delta_y, -3.4);
64 EXPECT_EQ(events[0].device_kind, kFlutterPointerDeviceKindMouse);
65 EXPECT_EQ(events[0].buttons, kFlutterPointerButtonMouseSecondary);
70 g_autoptr(GError)
error =
nullptr;
72 EXPECT_EQ(
error,
nullptr);
74 kFlutterPointerDeviceKindMouse, 1.2, -3.4,
75 kFlutterPointerButtonMouseSecondary);
81 TEST(FlEngineTest, PointerPanZoom) {
86 embedder_api->SendPointerEvent = MOCK_ENGINE_PROC(
88 ([&called](
auto engine,
const FlutterPointerEvent* events,
89 size_t events_count) {
91 EXPECT_EQ(events_count,
static_cast<size_t>(1));
92 EXPECT_EQ(events[0].phase, kPanZoomUpdate);
93 EXPECT_EQ(events[0].timestamp,
static_cast<size_t>(1234567890));
94 EXPECT_EQ(events[0].x, 800);
95 EXPECT_EQ(events[0].y, 600);
96 EXPECT_EQ(events[0].device,
static_cast<int32_t
>(1));
97 EXPECT_EQ(events[0].signal_kind, kFlutterPointerSignalKindNone);
98 EXPECT_EQ(events[0].pan_x, 1.5);
99 EXPECT_EQ(events[0].pan_y, 2.5);
100 EXPECT_EQ(events[0].scale, 3.5);
101 EXPECT_EQ(events[0].rotation, 4.5);
102 EXPECT_EQ(events[0].device_kind, kFlutterPointerDeviceKindTrackpad);
103 EXPECT_EQ(events[0].buttons, 0);
108 g_autoptr(GError)
error =
nullptr;
110 EXPECT_EQ(
error,
nullptr);
112 kPanZoomUpdate, 1.5, 2.5, 3.5, 4.5);
118 TEST(FlEngineTest, DispatchSemanticsAction) {
123 embedder_api->DispatchSemanticsAction = MOCK_ENGINE_PROC(
124 DispatchSemanticsAction,
125 ([&called](
auto engine, uint64_t
id, FlutterSemanticsAction action,
126 const uint8_t* data,
size_t data_length) {
127 EXPECT_EQ(
id,
static_cast<uint64_t
>(42));
128 EXPECT_EQ(action, kFlutterSemanticsActionTap);
129 EXPECT_EQ(data_length,
static_cast<size_t>(4));
130 EXPECT_EQ(data[0],
't');
131 EXPECT_EQ(data[1],
'e');
132 EXPECT_EQ(data[2],
's');
133 EXPECT_EQ(data[3],
't');
139 g_autoptr(GError)
error =
nullptr;
141 EXPECT_EQ(
error,
nullptr);
142 g_autoptr(GBytes) data = g_bytes_new_static(
"test", 4);
150 TEST(FlEngineTest, PlatformMessage) {
155 FlutterEngineSendPlatformMessageFnPtr old_handler =
156 embedder_api->SendPlatformMessage;
157 embedder_api->SendPlatformMessage = MOCK_ENGINE_PROC(
159 ([&called, old_handler](
auto engine,
160 const FlutterPlatformMessage* message) {
161 if (strcmp(message->channel,
"test") != 0) {
162 return old_handler(engine, message);
167 EXPECT_EQ(message->message_size,
static_cast<size_t>(4));
168 EXPECT_EQ(message->message[0],
't');
169 EXPECT_EQ(message->message[1],
'e');
170 EXPECT_EQ(message->message[2],
's');
171 EXPECT_EQ(message->message[3],
't');
176 g_autoptr(GError)
error =
nullptr;
178 EXPECT_EQ(
error,
nullptr);
179 g_autoptr(GBytes) message = g_bytes_new_static(
"test", 4);
187 TEST(FlEngineTest, PlatformMessageResponse) {
192 embedder_api->SendPlatformMessageResponse = MOCK_ENGINE_PROC(
193 SendPlatformMessageResponse,
194 ([&called](
auto engine,
195 const FlutterPlatformMessageResponseHandle* handle,
196 const uint8_t* data,
size_t data_length) {
201 reinterpret_cast<const FlutterPlatformMessageResponseHandle*
>(42));
202 EXPECT_EQ(data_length,
static_cast<size_t>(4));
203 EXPECT_EQ(data[0],
't');
204 EXPECT_EQ(data[1],
'e');
205 EXPECT_EQ(data[2],
's');
206 EXPECT_EQ(data[3],
't');
211 g_autoptr(GError)
error =
nullptr;
213 EXPECT_EQ(
error,
nullptr);
214 g_autoptr(GBytes) response = g_bytes_new_static(
"test", 4);
216 engine,
reinterpret_cast<const FlutterPlatformMessageResponseHandle*
>(42),
218 EXPECT_EQ(
error,
nullptr);
224 TEST(FlEngineTest, SettingsPlugin) {
229 embedder_api->SendPlatformMessage = MOCK_ENGINE_PROC(
231 ([&called](
auto engine,
const FlutterPlatformMessage* message) {
234 EXPECT_STREQ(message->channel,
"flutter/settings");
237 g_autoptr(GBytes) data =
238 g_bytes_new(message->message, message->message_size);
239 g_autoptr(GError)
error =
nullptr;
241 FL_MESSAGE_CODEC(codec), data, &
error);
242 EXPECT_NE(settings,
nullptr);
243 EXPECT_EQ(
error,
nullptr);
247 EXPECT_NE(text_scale_factor,
nullptr);
250 FlValue* always_use_24hr_format =
252 EXPECT_NE(always_use_24hr_format,
nullptr);
258 EXPECT_NE(platform_brightness,
nullptr);
264 g_autoptr(GError)
error =
nullptr;
266 EXPECT_EQ(
error,
nullptr);
272 int* count =
reinterpret_cast<int*
>(
user_data);
277 int* count =
reinterpret_cast<int*
>(
user_data);
282 TEST(FlEngineTest, OnPreEngineRestart) {
286 OnPreEngineRestartCallback
callback;
287 void* callback_user_data;
290 embedder_api->Initialize = MOCK_ENGINE_PROC(
291 Initialize, ([&
callback, &callback_user_data, &called](
292 size_t version,
const FlutterRendererConfig* config,
294 FLUTTER_API_SYMBOL(FlutterEngine) * engine_out) {
302 g_autoptr(GError)
error =
nullptr;
304 EXPECT_EQ(
error,
nullptr);
326 g_object_unref(engine);
327 EXPECT_EQ(count, 11);
330 TEST(FlEngineTest, DartEntrypointArgs) {
333 GPtrArray* args_array = g_ptr_array_new();
334 g_ptr_array_add(args_array,
const_cast<char*
>(
"arg_one"));
335 g_ptr_array_add(args_array,
const_cast<char*
>(
"arg_two"));
336 g_ptr_array_add(args_array,
const_cast<char*
>(
"arg_three"));
337 g_ptr_array_add(args_array,
nullptr);
338 gchar**
args =
reinterpret_cast<gchar**
>(g_ptr_array_free(args_array,
false));
342 g_autoptr(FlEngine) engine = make_mock_engine_with_project(project);
346 embedder_api->Initialize = MOCK_ENGINE_PROC(
347 Initialize, ([&called, &set_args =
args](
348 size_t version,
const FlutterRendererConfig* config,
350 FLUTTER_API_SYMBOL(FlutterEngine) * engine_out) {
352 EXPECT_NE(set_args,
args->dart_entrypoint_argv);
353 EXPECT_EQ(
args->dart_entrypoint_argc, 3);
358 g_autoptr(GError)
error =
nullptr;
360 EXPECT_EQ(
error,
nullptr);
366 gchar* initial_language = g_strdup(g_getenv(
"LANGUAGE"));
367 g_setenv(
"LANGUAGE",
"de:en_US",
TRUE);
370 g_autoptr(FlEngine) engine = make_mock_engine_with_project(project);
374 embedder_api->UpdateLocales = MOCK_ENGINE_PROC(
375 UpdateLocales, ([&called](
auto engine,
const FlutterLocale** locales,
376 size_t locales_count) {
379 EXPECT_EQ(locales_count,
static_cast<size_t>(4));
381 EXPECT_STREQ(locales[0]->language_code,
"de");
382 EXPECT_STREQ(locales[0]->country_code,
nullptr);
383 EXPECT_STREQ(locales[0]->script_code,
nullptr);
384 EXPECT_STREQ(locales[0]->variant_code,
nullptr);
386 EXPECT_STREQ(locales[1]->language_code,
"en");
387 EXPECT_STREQ(locales[1]->country_code,
"US");
388 EXPECT_STREQ(locales[1]->script_code,
nullptr);
389 EXPECT_STREQ(locales[1]->variant_code,
nullptr);
391 EXPECT_STREQ(locales[2]->language_code,
"en");
392 EXPECT_STREQ(locales[2]->country_code,
nullptr);
393 EXPECT_STREQ(locales[2]->script_code,
nullptr);
394 EXPECT_STREQ(locales[2]->variant_code,
nullptr);
396 EXPECT_STREQ(locales[3]->language_code,
"C");
397 EXPECT_STREQ(locales[3]->country_code,
nullptr);
398 EXPECT_STREQ(locales[3]->script_code,
nullptr);
399 EXPECT_STREQ(locales[3]->variant_code,
nullptr);
404 g_autoptr(GError)
error =
nullptr;
406 EXPECT_EQ(
error,
nullptr);
410 if (initial_language) {
411 g_setenv(
"LANGUAGE", initial_language,
TRUE);
413 g_unsetenv(
"LANGUAGE");
415 g_free(initial_language);
418 TEST(FlEngineTest, SwitchesEmpty) {
422 unsetenv(
"FLUTTER_ENGINE_SWITCHES");
426 EXPECT_EQ(switches->len, 0U);
429 TEST(FlEngineTest, SendWindowStateEvent) {
435 embedder_api->SendPlatformMessage = MOCK_ENGINE_PROC(
437 ([&called, &
state](
auto engine,
const FlutterPlatformMessage* message) {
438 EXPECT_STREQ(message->channel,
"flutter/lifecycle");
441 g_autoptr(GBytes) data =
442 g_bytes_new(message->message, message->message_size);
443 g_autoptr(GError)
error =
nullptr;
445 FL_MESSAGE_CODEC(codec), data, &
error);
465 #ifndef FLUTTER_RELEASE
469 setenv(
"FLUTTER_ENGINE_SWITCHES",
"2", 1);
470 setenv(
"FLUTTER_ENGINE_SWITCH_1",
"abc", 1);
471 setenv(
"FLUTTER_ENGINE_SWITCH_2",
"foo=\"bar, baz\"", 1);
474 EXPECT_EQ(switches->len, 2U);
475 EXPECT_STREQ(
static_cast<const char*
>(g_ptr_array_index(switches, 0)),
477 EXPECT_STREQ(
static_cast<const char*
>(g_ptr_array_index(switches, 1)),
478 "--foo=\"bar, baz\"");
480 unsetenv(
"FLUTTER_ENGINE_SWITCHES");
481 unsetenv(
"FLUTTER_ENGINE_SWITCH_1");
482 unsetenv(
"FLUTTER_ENGINE_SWITCH_2");
484 #endif // !FLUTTER_RELEASE