9 #include "flutter/shell/platform/linux/testing/fl_test.h"
10 #include "gtest/gtest.h"
13 fl_test_method_message_codec,
15 TEST_METHOD_MESSAGE_CODEC,
16 FlStandardMessageCodec)
18 struct _FlTestMethodMessageCodec {
19 FlStandardMessageCodec parent_instance;
23 fl_test_method_message_codec,
24 fl_standard_message_codec_get_type())
26 static gboolean write_custom_value(FlStandardMessageCodec* codec,
42 FlStandardMessageCodec* codec,
50 return FL_STANDARD_MESSAGE_CODEC_CLASS(
51 fl_test_method_message_codec_parent_class)
72 g_strndup(
static_cast<const gchar*
>(g_bytes_get_data(
buffer,
nullptr)) +
82 FlStandardMessageCodec* codec,
90 return FL_STANDARD_MESSAGE_CODEC_CLASS(
91 fl_test_method_message_codec_parent_class)
97 FlTestMethodMessageCodecClass* klass) {
98 FL_STANDARD_MESSAGE_CODEC_CLASS(klass)->write_value =
100 FL_STANDARD_MESSAGE_CODEC_CLASS(klass)->read_value_of_type =
106 FL_IS_TEST_METHOD_MESSAGE_CODEC(
self);
110 return FL_TEST_METHOD_MESSAGE_CODEC(
111 g_object_new(fl_test_method_message_codec_get_type(),
nullptr));
120 g_autoptr(GError)
error =
nullptr;
122 FL_METHOD_CODEC(codec), name,
args, &
error);
123 EXPECT_NE(message,
nullptr);
124 EXPECT_EQ(
error,
nullptr);
126 return bytes_to_hex_string(message);
133 g_autoptr(GError)
error =
nullptr;
136 EXPECT_NE(message,
nullptr);
137 EXPECT_EQ(
error,
nullptr);
139 return bytes_to_hex_string(message);
144 const gchar* error_message,
147 g_autoptr(GError)
error =
nullptr;
149 FL_METHOD_CODEC(codec), error_code, error_message, details, &
error);
150 EXPECT_NE(message,
nullptr);
151 EXPECT_EQ(
error,
nullptr);
153 return bytes_to_hex_string(message);
161 g_autoptr(GBytes) message = hex_string_to_bytes(hex_string);
162 g_autoptr(GError)
error =
nullptr;
164 FL_METHOD_CODEC(codec), message, name,
args, &
error);
166 EXPECT_EQ(
error,
nullptr);
174 g_autoptr(GBytes) message = hex_string_to_bytes(hex_string);
175 g_autoptr(GError)
error =
nullptr;
176 g_autofree gchar* name =
nullptr;
179 FL_METHOD_CODEC(codec), message, &name, &
args, &
error);
181 EXPECT_EQ(name,
nullptr);
182 EXPECT_EQ(
args,
nullptr);
183 EXPECT_TRUE(g_error_matches(
error, domain, code));
191 g_autoptr(GBytes) message = hex_string_to_bytes(hex_string);
192 g_autoptr(GError)
error =
nullptr;
193 g_autoptr(FlMethodResponse) response =
195 ASSERT_NE(response,
nullptr);
196 EXPECT_EQ(
error,
nullptr);
197 EXPECT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
199 FL_METHOD_SUCCESS_RESPONSE(response)),
207 const gchar* error_message,
210 g_autoptr(GBytes) message = hex_string_to_bytes(hex_string);
211 g_autoptr(GError)
error =
nullptr;
212 g_autoptr(FlMethodResponse) response =
214 ASSERT_NE(response,
nullptr);
215 EXPECT_EQ(
error,
nullptr);
216 EXPECT_TRUE(FL_IS_METHOD_ERROR_RESPONSE(response));
220 if (error_message ==
nullptr) {
222 FL_METHOD_ERROR_RESPONSE(response)),
226 FL_METHOD_ERROR_RESPONSE(response)),
229 if (details ==
nullptr) {
231 FL_METHOD_ERROR_RESPONSE(response)),
235 FL_METHOD_ERROR_RESPONSE(response)),
244 g_autoptr(GBytes) message = hex_string_to_bytes(hex_string);
245 g_autoptr(GError)
error =
nullptr;
246 g_autoptr(FlMethodResponse) response =
248 EXPECT_EQ(response,
nullptr);
249 EXPECT_TRUE(g_error_matches(
error, domain, code));
252 TEST(FlStandardMethodCodecTest, EncodeMethodCallNullptrArgs) {
254 EXPECT_STREQ(hex_string,
"070568656c6c6f00");
257 TEST(FlStandardMethodCodecTest, EncodeMethodCallNullArgs) {
260 EXPECT_STREQ(hex_string,
"070568656c6c6f00");
263 TEST(FlStandardMethodCodecTest, EncodeMethodCallStringArgs) {
266 EXPECT_STREQ(hex_string,
"070568656c6c6f0705776f726c64");
269 TEST(FlStandardMethodCodecTest, EncodeMethodCallListArgs) {
274 EXPECT_STREQ(hex_string,
"070568656c6c6f0c020705636f756e74032a000000");
277 TEST(FlStandardMethodCodecTest, DecodeMethodCallNullArgs) {
278 g_autofree gchar* name =
nullptr;
281 EXPECT_STREQ(name,
"hello");
285 TEST(FlStandardMethodCodecTest, DecodeMethodCallStringArgs) {
286 g_autofree gchar* name =
nullptr;
289 EXPECT_STREQ(name,
"hello");
294 TEST(FlStandardMethodCodecTest, DecodeMethodCallListArgs) {
295 g_autofree gchar* name =
nullptr;
299 EXPECT_STREQ(name,
"hello");
312 TEST(FlStandardMethodCodecTest, DecodeMethodCallNoData) {
317 TEST(FlStandardMethodCodecTest, DecodeMethodCallNullMethodName) {
322 TEST(FlStandardMethodCodecTest, DecodeMethodCallMissingArgs) {
327 TEST(FlStandardMethodCodecTest, EncodeSuccessEnvelopeNullptr) {
329 EXPECT_STREQ(hex_string,
"0000");
332 TEST(FlStandardMethodCodecTest, EncodeSuccessEnvelopeNull) {
335 EXPECT_STREQ(hex_string,
"0000");
338 TEST(FlStandardMethodCodecTest, EncodeSuccessEnvelopeString) {
341 EXPECT_STREQ(hex_string,
"00070568656c6c6f");
344 TEST(FlStandardMethodCodecTest, EncodeSuccessEnvelopeList) {
349 EXPECT_STREQ(hex_string,
"000c020705636f756e74032a000000");
352 TEST(FlStandardMethodCodecTest, EncodeErrorEnvelopeEmptyCode) {
354 EXPECT_STREQ(hex_string,
"0107000000");
357 TEST(FlStandardMethodCodecTest, EncodeErrorEnvelopeNonMessageOrDetails) {
358 g_autofree gchar* hex_string =
360 EXPECT_STREQ(hex_string,
"0107056572726f720000");
363 TEST(FlStandardMethodCodecTest, EncodeErrorEnvelopeMessage) {
364 g_autofree gchar* hex_string =
366 EXPECT_STREQ(hex_string,
"0107056572726f7207076d65737361676500");
369 TEST(FlStandardMethodCodecTest, EncodeErrorEnvelopeDetails) {
373 g_autofree gchar* hex_string =
375 EXPECT_STREQ(hex_string,
"0107056572726f72000c020705636f756e74032a000000");
378 TEST(FlStandardMethodCodecTest, EncodeErrorEnvelopeMessageAndDetails) {
382 g_autofree gchar* hex_string =
386 "0107056572726f7207076d6573736167650c020705636f756e74032a000000");
389 TEST(FlStandardMethodCodecTest, DecodeResponseSuccessNull) {
394 TEST(FlStandardMethodCodecTest, DecodeResponseSuccessString) {
399 TEST(FlStandardMethodCodecTest, DecodeResponseSuccessList) {
406 TEST(FlStandardMethodCodecTest, DecodeResponseErrorEmptyCode) {
410 TEST(FlStandardMethodCodecTest, DecodeResponseErrorNoMessageOrDetails) {
414 TEST(FlStandardMethodCodecTest, DecodeResponseErrorMessage) {
419 TEST(FlStandardMethodCodecTest, DecodeResponseErrorDetails) {
424 "error",
nullptr, details);
427 TEST(FlStandardMethodCodecTest, DecodeResponseErrorMessageAndDetails) {
432 "0107056572726f7207076d6573736167650c020705636f756e74032a000000",
"error",
436 TEST(FlStandardMethodCodecTest, DecodeResponseSuccessNoData) {
441 TEST(FlStandardMethodCodecTest, DecodeResponseSuccessExtraData) {
446 TEST(FlStandardMethodCodecTest, DecodeResponseErrorNoData) {
451 TEST(FlStandardMethodCodecTest, DecodeResponseErrorMissingMessageAndDetails) {
456 TEST(FlStandardMethodCodecTest, DecodeResponseErrorMissingDetails) {
461 TEST(FlStandardMethodCodecTest, DecodeResponseErrorExtraData) {
466 TEST(FlStandardMethodCodecTest, DecodeResponseNotImplemented) {
468 g_autoptr(GBytes) message = g_bytes_new(
nullptr, 0);
469 g_autoptr(GError)
error =
nullptr;
470 g_autoptr(FlMethodResponse) response =
472 ASSERT_NE(response,
nullptr);
473 EXPECT_EQ(
error,
nullptr);
474 EXPECT_TRUE(FL_IS_METHOD_NOT_IMPLEMENTED_RESPONSE(response));
477 TEST(FlStandardMethodCodecTest, DecodeResponseUnknownEnvelope) {
482 TEST(FlStandardMethodCodecTest, CustomMessageCodec) {
483 g_autoptr(FlTestMethodMessageCodec) message_codec =
485 g_autoptr(FlStandardMethodCodec) codec =
487 FL_STANDARD_MESSAGE_CODEC(message_codec));
489 g_autoptr(GError)
error =
nullptr;
493 EXPECT_NE(message,
nullptr);
494 EXPECT_EQ(
error,
nullptr);
495 g_autofree gchar* hex_string = bytes_to_hex_string(message);
496 EXPECT_STREQ(hex_string,
"00800568656c6c6f");
498 g_autoptr(FlMethodResponse) response =
500 EXPECT_NE(response,
nullptr);
501 EXPECT_EQ(
error,
nullptr);
502 EXPECT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
504 FL_METHOD_SUCCESS_RESPONSE(response));