6 #include "flutter/shell/platform/linux/testing/fl_test.h"
7 #include "gtest/gtest.h"
15 g_autoptr(GError)
error =
nullptr;
16 g_autoptr(GBytes) message =
18 EXPECT_NE(message,
nullptr);
19 EXPECT_EQ(
error,
nullptr);
21 return bytes_to_hex_string(message);
34 FlMessageCodec* codec) {
35 g_autoptr(GBytes) data = hex_string_to_bytes(hex_string);
36 g_autoptr(GError)
error =
nullptr;
39 EXPECT_EQ(
error,
nullptr);
40 EXPECT_NE(
value,
nullptr);
57 g_autoptr(GBytes) data = hex_string_to_bytes(hex_string);
58 g_autoptr(GError)
error =
nullptr;
61 EXPECT_TRUE(
value ==
nullptr);
62 EXPECT_TRUE(g_error_matches(
error, domain, code));
65 TEST(FlStandardMessageCodecTest, EncodeNullptr) {
67 EXPECT_STREQ(hex_string,
"00");
70 TEST(FlStandardMessageCodecTest, EncodeNull) {
73 EXPECT_STREQ(hex_string,
"00");
76 TEST(FlStandardMessageCodecTest, DecodeNull) {
80 g_autoptr(GBytes) data = g_bytes_new(
nullptr, 0);
81 g_autoptr(GError)
error =
nullptr;
85 EXPECT_FALSE(
value ==
nullptr);
94 TEST(FlStandardMessageCodecTest, EncodeBoolFalse) {
96 EXPECT_STREQ(hex_string,
"02");
99 TEST(FlStandardMessageCodecTest, EncodeBoolTrue) {
101 EXPECT_STREQ(hex_string,
"01");
109 TEST(FlStandardMessageCodecTest, EncodeIntZero) {
111 EXPECT_STREQ(hex_string,
"0300000000");
114 TEST(FlStandardMessageCodecTest, EncodeIntOne) {
116 EXPECT_STREQ(hex_string,
"0301000000");
119 TEST(FlStandardMessageCodecTest, EncodeInt32) {
120 g_autofree gchar* hex_string =
encode_int(0x01234567);
121 EXPECT_STREQ(hex_string,
"0367452301");
124 TEST(FlStandardMessageCodecTest, EncodeInt32Min) {
125 g_autofree gchar* hex_string =
encode_int(G_MININT32);
126 EXPECT_STREQ(hex_string,
"0300000080");
129 TEST(FlStandardMessageCodecTest, EncodeInt32Max) {
130 g_autofree gchar* hex_string =
encode_int(G_MAXINT32);
131 EXPECT_STREQ(hex_string,
"03ffffff7f");
134 TEST(FlStandardMessageCodecTest, EncodeInt64) {
135 g_autofree gchar* hex_string =
encode_int(0x0123456789abcdef);
136 EXPECT_STREQ(hex_string,
"04efcdab8967452301");
139 TEST(FlStandardMessageCodecTest, EncodeInt64Min) {
140 g_autofree gchar* hex_string =
encode_int(G_MININT64);
141 EXPECT_STREQ(hex_string,
"040000000000000080");
144 TEST(FlStandardMessageCodecTest, EncodeInt64Max) {
145 g_autofree gchar* hex_string =
encode_int(G_MAXINT64);
146 EXPECT_STREQ(hex_string,
"04ffffffffffffff7f");
149 TEST(FlStandardMessageCodecTest, DecodeIntZero) {
155 TEST(FlStandardMessageCodecTest, DecodeIntOne) {
161 TEST(FlStandardMessageCodecTest, DecodeInt32) {
167 TEST(FlStandardMessageCodecTest, DecodeInt32Min) {
173 TEST(FlStandardMessageCodecTest, DecodeInt32Max) {
179 TEST(FlStandardMessageCodecTest, DecodeInt64) {
185 TEST(FlStandardMessageCodecTest, DecodeInt64Min) {
191 TEST(FlStandardMessageCodecTest, DecodeInt64Max) {
197 TEST(FlStandardMessageCodecTest, DecodeInt32NoData) {
202 TEST(FlStandardMessageCodecTest, DecodeIntShortData1) {
207 TEST(FlStandardMessageCodecTest, DecodeIntShortData2) {
212 TEST(FlStandardMessageCodecTest, DecodeInt64NoData) {
217 TEST(FlStandardMessageCodecTest, DecodeInt64ShortData1) {
222 TEST(FlStandardMessageCodecTest, DecodeInt64ShortData2) {
232 TEST(FlStandardMessageCodecTest, EncodeFloatZero) {
234 EXPECT_STREQ(hex_string,
"06000000000000000000000000000000");
237 TEST(FlStandardMessageCodecTest, EncodeFloatOne) {
239 EXPECT_STREQ(hex_string,
"0600000000000000000000000000f03f");
242 TEST(FlStandardMessageCodecTest, EncodeFloatMinusOne) {
244 EXPECT_STREQ(hex_string,
"0600000000000000000000000000f0bf");
247 TEST(FlStandardMessageCodecTest, EncodeFloatHalf) {
249 EXPECT_STREQ(hex_string,
"0600000000000000000000000000e03f");
252 TEST(FlStandardMessageCodecTest, EncodeFloatFraction) {
254 EXPECT_STREQ(hex_string,
"0600000000000000182d4454fb210940");
257 TEST(FlStandardMessageCodecTest, EncodeFloatMinusZero) {
259 EXPECT_STREQ(hex_string,
"06000000000000000000000000000080");
262 TEST(FlStandardMessageCodecTest, EncodeFloatNaN) {
264 EXPECT_STREQ(hex_string,
"0600000000000000000000000000f87f");
267 TEST(FlStandardMessageCodecTest, EncodeFloatInfinity) {
269 EXPECT_STREQ(hex_string,
"0600000000000000000000000000f07f");
272 TEST(FlStandardMessageCodecTest, DecodeFloatZero) {
278 TEST(FlStandardMessageCodecTest, DecodeFloatOne) {
284 TEST(FlStandardMessageCodecTest, DecodeFloatMinusOne) {
290 TEST(FlStandardMessageCodecTest, DecodeFloatHalf) {
296 TEST(FlStandardMessageCodecTest, DecodeFloatPi) {
302 TEST(FlStandardMessageCodecTest, DecodeFloatMinusZero) {
308 TEST(FlStandardMessageCodecTest, DecodeFloatNaN) {
314 TEST(FlStandardMessageCodecTest, DecodeFloatInfinity) {
320 TEST(FlStandardMessageCodecTest, DecodeFloatNoData) {
325 TEST(FlStandardMessageCodecTest, DecodeFloatShortData1) {
330 TEST(FlStandardMessageCodecTest, DecodeFloatShortData2) {
340 TEST(FlStandardMessageCodecTest, EncodeStringEmpty) {
342 EXPECT_STREQ(hex_string,
"0700");
345 TEST(FlStandardMessageCodecTest, EncodeStringHello) {
347 EXPECT_STREQ(hex_string,
"070568656c6c6f");
350 TEST(FlStandardMessageCodecTest, EncodeStringEmptySized) {
353 EXPECT_STREQ(hex_string,
"0700");
356 TEST(FlStandardMessageCodecTest, EncodeStringHelloSized) {
359 EXPECT_STREQ(hex_string,
"070548656c6c6f");
362 TEST(FlStandardMessageCodecTest, DecodeStringEmpty) {
368 TEST(FlStandardMessageCodecTest, DecodeStringHello) {
374 TEST(FlStandardMessageCodecTest, DecodeStringNoData) {
379 TEST(FlStandardMessageCodecTest, DecodeStringLengthNoData) {
384 TEST(FlStandardMessageCodecTest, DecodeStringShortData1) {
389 TEST(FlStandardMessageCodecTest, DecodeStringShortData2) {
394 TEST(FlStandardMessageCodecTest, EncodeUint8ListEmpty) {
397 EXPECT_STREQ(hex_string,
"0800");
400 TEST(FlStandardMessageCodecTest, EncodeUint8List) {
401 uint8_t data[] = {0, 1, 2, 3, 4};
404 EXPECT_STREQ(hex_string,
"08050001020304");
407 TEST(FlStandardMessageCodecTest, DecodeUint8ListEmpty) {
413 TEST(FlStandardMessageCodecTest, DecodeUint8List) {
418 EXPECT_EQ(data[0], 0);
419 EXPECT_EQ(data[1], 1);
420 EXPECT_EQ(data[2], 2);
421 EXPECT_EQ(data[3], 3);
422 EXPECT_EQ(data[4], 4);
425 TEST(FlStandardMessageCodecTest, DecodeUint8ListNoData) {
430 TEST(FlStandardMessageCodecTest, DecodeUint8ListLengthNoData) {
435 TEST(FlStandardMessageCodecTest, DecodeUint8ListShortData1) {
440 TEST(FlStandardMessageCodecTest, DecodeUint8ListShortData2) {
445 TEST(FlStandardMessageCodecTest, EncodeInt32ListEmpty) {
448 EXPECT_STREQ(hex_string,
"09000000");
451 TEST(FlStandardMessageCodecTest, EncodeInt32List) {
452 int32_t data[] = {0, -1, 2, -3, 4};
455 EXPECT_STREQ(hex_string,
"0905000000000000ffffffff02000000fdffffff04000000");
458 TEST(FlStandardMessageCodecTest, DecodeInt32ListEmpty) {
464 TEST(FlStandardMessageCodecTest, DecodeInt32List) {
466 decode_message(
"0905000000000000ffffffff02000000fdffffff04000000");
469 EXPECT_EQ(data[0], 0);
470 EXPECT_EQ(data[1], -1);
471 EXPECT_EQ(data[2], 2);
472 EXPECT_EQ(data[3], -3);
473 EXPECT_EQ(data[4], 4);
476 TEST(FlStandardMessageCodecTest, DecodeInt32ListNoData) {
481 TEST(FlStandardMessageCodecTest, DecodeInt32ListLengthNoData) {
486 TEST(FlStandardMessageCodecTest, DecodeInt32ListShortData1) {
491 TEST(FlStandardMessageCodecTest, DecodeInt32ListShortData2) {
497 TEST(FlStandardMessageCodecTest, EncodeInt64ListEmpty) {
500 EXPECT_STREQ(hex_string,
"0a00000000000000");
503 TEST(FlStandardMessageCodecTest, EncodeInt64List) {
504 int64_t data[] = {0, -1, 2, -3, 4};
509 "0a050000000000000000000000000000ffffffffffffffff0200000000000000fdffffff"
510 "ffffffff0400000000000000");
513 TEST(FlStandardMessageCodecTest, DecodeInt64ListEmpty) {
519 TEST(FlStandardMessageCodecTest, DecodeInt64List) {
521 "0a050000000000000000000000000000ffffffffffffffff0200000000000000fdffffff"
522 "ffffffff0400000000000000");
525 EXPECT_EQ(data[0], 0);
526 EXPECT_EQ(data[1], -1);
527 EXPECT_EQ(data[2], 2);
528 EXPECT_EQ(data[3], -3);
529 EXPECT_EQ(data[4], 4);
532 TEST(FlStandardMessageCodecTest, DecodeInt64ListNoData) {
537 TEST(FlStandardMessageCodecTest, DecodeInt64ListLengthNoData) {
542 TEST(FlStandardMessageCodecTest, DecodeInt64ListShortData1) {
547 TEST(FlStandardMessageCodecTest, DecodeInt64ListShortData2) {
549 "0a050000000000000000000000000000ffffffffffffffff0200000000000000fdffffff"
555 TEST(FlStandardMessageCodecTest, EncodeFloat32ListEmpty) {
558 EXPECT_STREQ(hex_string,
"0e000000");
561 TEST(FlStandardMessageCodecTest, EncodeFloat32List) {
562 float data[] = {0.0f, -0.5f, 0.25f, -0.125f, 0.00625f};
565 EXPECT_STREQ(hex_string,
"0e05000000000000000000bf0000803e000000becdcccc3b");
568 TEST(FlStandardMessageCodecTest, DecodeFloat32ListEmpty) {
574 TEST(FlStandardMessageCodecTest, DecodeFloat32List) {
576 decode_message(
"0e05000000000000000000bf0000803e000000becdcccc3b");
579 EXPECT_FLOAT_EQ(data[0], 0.0f);
580 EXPECT_FLOAT_EQ(data[1], -0.5f);
581 EXPECT_FLOAT_EQ(data[2], 0.25f);
582 EXPECT_FLOAT_EQ(data[3], -0.125f);
583 EXPECT_FLOAT_EQ(data[4], 0.00625f);
586 TEST(FlStandardMessageCodecTest, DecodeFloat32ListNoData) {
591 TEST(FlStandardMessageCodecTest, DecodeFloat32ListLengthNoData) {
596 TEST(FlStandardMessageCodecTest, DecodeFloat32ListShortData1) {
601 TEST(FlStandardMessageCodecTest, DecodeFloat32ListShortData2) {
607 TEST(FlStandardMessageCodecTest, EncodeFloatListEmpty) {
610 EXPECT_STREQ(hex_string,
"0b00000000000000");
613 TEST(FlStandardMessageCodecTest, EncodeFloatList) {
614 double data[] = {0, -0.5, 0.25, -0.125, 0.00625};
619 "0b050000000000000000000000000000000000000000e0bf000000000000d03f00000000"
620 "0000c0bf9a9999999999793f");
623 TEST(FlStandardMessageCodecTest, DecodeFloatListEmpty) {
629 TEST(FlStandardMessageCodecTest, DecodeFloatList) {
631 "0b050000000000000000000000000000000000000000e0bf000000000000d03f00000000"
632 "0000c0bf9a9999999999793f");
635 EXPECT_FLOAT_EQ(data[0], 0.0);
636 EXPECT_FLOAT_EQ(data[1], -0.5);
637 EXPECT_FLOAT_EQ(data[2], 0.25);
638 EXPECT_FLOAT_EQ(data[3], -0.125);
639 EXPECT_FLOAT_EQ(data[4], 0.00625);
642 TEST(FlStandardMessageCodecTest, DecodeFloatListNoData) {
647 TEST(FlStandardMessageCodecTest, DecodeFloatListLengthNoData) {
652 TEST(FlStandardMessageCodecTest, DecodeFloatListShortData1) {
657 TEST(FlStandardMessageCodecTest, DecodeFloatListShortData2) {
659 "0b050000000000000000000000000000000000000000e0bf000000000000d03f00000000"
665 TEST(FlStandardMessageCodecTest, EncodeListEmpty) {
668 EXPECT_STREQ(hex_string,
"0c00");
671 TEST(FlStandardMessageCodecTest, EncodeListTypes) {
683 "0c070001032a00000006000000000000182d4454fb210940070568656c6c6f0c000d00");
686 TEST(FlStandardMessageCodecTest, EncodeListNested) {
689 for (
int i = 0;
i < 10;
i++) {
700 EXPECT_STREQ(hex_string,
701 "0c020c05030000000003020000000304000000030600000003080000000c"
702 "0503010000000303000000030500000003070000000309000000");
705 TEST(FlStandardMessageCodecTest, DecodeListEmpty) {
711 TEST(FlStandardMessageCodecTest, DecodeListTypes) {
713 "0c070001032a00000006000000000000182d4454fb210940070568656c6c6f0c000d00");
733 static_cast<size_t>(0));
737 static_cast<size_t>(0));
740 TEST(FlStandardMessageCodecTest, DecodeListNested) {
742 "0c020c05030000000003020000000304000000030600000003080000000c"
743 "0503010000000303000000030500000003070000000309000000");
752 for (
int i = 0;
i < 5;
i++) {
763 TEST(FlStandardMessageCodecTest, DecodeListNoData) {
768 TEST(FlStandardMessageCodecTest, DecodeListLengthNoData) {
773 TEST(FlStandardMessageCodecTest, DecodeListShortData1) {
778 TEST(FlStandardMessageCodecTest, DecodeListShortData2) {
780 "0c070001032a00000006000000000000182d4454fb210940070568656c6c6f0c000d",
784 TEST(FlStandardMessageCodecTest, EncodeDecodeLargeList) {
788 for (
int i = 0;
i < 65535;
i++) {
792 g_autoptr(GError)
error =
nullptr;
793 g_autoptr(GBytes) message =
795 EXPECT_NE(message,
nullptr);
796 EXPECT_EQ(
error,
nullptr);
798 g_autoptr(
FlValue) decoded_value =
800 EXPECT_EQ(
error,
nullptr);
801 EXPECT_NE(
value,
nullptr);
806 TEST(FlStandardMessageCodecTest, EncodeMapEmpty) {
809 EXPECT_STREQ(hex_string,
"0d00");
812 TEST(FlStandardMessageCodecTest, EncodeMapKeyTypes) {
825 EXPECT_STREQ(hex_string,
826 "0d070007046e756c6c010704626f6f6c032a0000000703696e7406000000000"
827 "0182d4454fb2109400705666c6f6174070568656c6c6f0706737472696e670c"
828 "0007046c6973740d0007036d6170");
831 TEST(FlStandardMessageCodecTest, EncodeMapValueTypes) {
844 EXPECT_STREQ(hex_string,
845 "0d0707046e756c6c000704626f6f6c010703696e74032a0000000705666c6f6"
846 "17406000000000000182d4454fb2109400706737472696e67070568656c6c6f"
847 "07046c6973740c0007036d61700d00");
850 TEST(FlStandardMessageCodecTest, EncodeMapNested) {
853 const char* numbers[] = {
"zero",
"one",
"two",
"three",
nullptr};
854 for (
int i = 0; numbers[
i] !=
nullptr;
i++) {
864 EXPECT_STREQ(hex_string,
865 "0d02070a7374722d746f2d696e740d0407047a65726f030000000007036f6e6"
866 "50301000000070374776f0302000000070574687265650303000000070a696e"
867 "742d746f2d7374720d04030000000007047a65726f030100000007036f6e650"
868 "302000000070374776f030300000007057468726565");
871 TEST(FlStandardMessageCodecTest, DecodeMapEmpty) {
877 TEST(FlStandardMessageCodecTest, DecodeMapKeyTypes) {
879 "0d070007046e756c6c010704626f6f6c032a0000000703696e74060000000000182d4454"
880 "fb2109400705666c6f6174070568656c6c6f0706737472696e670c0007046c6973740d00"
922 static_cast<size_t>(0));
930 static_cast<size_t>(0));
936 TEST(FlStandardMessageCodecTest, DecodeMapValueTypes) {
938 "0d0707046e756c6c000704626f6f6c010703696e74032a0000000705666c6f6174060000"
939 "00000000182d4454fb2109400706737472696e67070568656c6c6f07046c6973740c0007"
984 static_cast<size_t>(0));
992 static_cast<size_t>(0));
995 TEST(FlStandardMessageCodecTest, DecodeMapNested) {
997 "0d02070a7374722d746f2d696e740d0407047a65726f030000000007036f6e6503010000"
998 "00070374776f0302000000070574687265650303000000070a696e742d746f2d7374720d"
999 "04030000000007047a65726f030100000007036f6e650302000000070374776f03030000"
1000 "0007057468726565");
1020 const char* numbers[] = {
"zero",
"one",
"two",
"three",
nullptr};
1021 for (
int i = 0; numbers[
i] !=
nullptr;
i++) {
1042 TEST(FlStandardMessageCodecTest, DecodeMapNoData) {
1047 TEST(FlStandardMessageCodecTest, DecodeMapLengthNoData) {
1052 TEST(FlStandardMessageCodecTest, DecodeMapShortData1) {
1057 TEST(FlStandardMessageCodecTest, DecodeMapShortData2) {
1059 "0d0707046e756c6c000704626f6f6c010703696e74032a0000000705666c6f6174060000"
1060 "00000000182d4454fb2109400706737472696e67070568656c6c6f07046c6973740c0007"
1065 TEST(FlStandardMessageCodecTest, EncodeDecodeLargeMap) {
1069 for (
int i = 0;
i < 512;
i++) {
1070 g_autofree gchar* key = g_strdup_printf(
"key%d",
i);
1074 g_autoptr(GError)
error =
nullptr;
1075 g_autoptr(GBytes) message =
1077 EXPECT_NE(message,
nullptr);
1078 EXPECT_EQ(
error,
nullptr);
1080 g_autoptr(
FlValue) decoded_value =
1082 EXPECT_EQ(
error,
nullptr);
1083 EXPECT_NE(
value,
nullptr);
1088 TEST(FlStandardMessageCodecTest, DecodeUnknownType) {
1094 fl_test_standard_message_codec,
1096 TEST_STANDARD_MESSAGE_CODEC,
1097 FlStandardMessageCodec)
1099 struct _FlTestStandardMessageCodec {
1100 FlStandardMessageCodec parent_instance;
1104 fl_test_standard_message_codec,
1105 fl_standard_message_codec_get_type())
1107 static gboolean write_custom_value1(FlStandardMessageCodec* codec,
1132 FlStandardMessageCodec* codec,
1143 return FL_STANDARD_MESSAGE_CODEC_CLASS(
1144 fl_test_standard_message_codec_parent_class)
1165 g_strndup(
static_cast<const gchar*
>(g_bytes_get_data(
buffer,
nullptr)) +
1182 FlStandardMessageCodec* codec,
1189 }
else if (
type == 129) {
1192 return FL_STANDARD_MESSAGE_CODEC_CLASS(
1193 fl_test_standard_message_codec_parent_class)
1199 FlTestStandardMessageCodecClass* klass) {
1200 FL_STANDARD_MESSAGE_CODEC_CLASS(klass)->write_value =
1202 FL_STANDARD_MESSAGE_CODEC_CLASS(klass)->read_value_of_type =
1207 FlTestStandardMessageCodec*
self) {
1209 FL_IS_TEST_STANDARD_MESSAGE_CODEC(
self);
1213 return FL_TEST_STANDARD_MESSAGE_CODEC(
1214 g_object_new(fl_test_standard_message_codec_get_type(),
nullptr));
1217 TEST(FlStandardMessageCodecTest, DecodeCustomType) {
1218 g_autoptr(FlTestStandardMessageCodec) codec =
1228 TEST(FlStandardMessageCodecTest, DecodeCustomTypes) {
1229 g_autoptr(FlTestStandardMessageCodec) codec =
1232 FL_MESSAGE_CODEC(codec));
1245 TEST(FlStandardMessageCodecTest, EncodeCustomType) {
1247 g_autoptr(FlTestStandardMessageCodec) codec =
1249 g_autofree gchar* hex_string =
1251 EXPECT_STREQ(hex_string,
"800568656c6c6f");
1254 TEST(FlStandardMessageCodecTest, EncodeCustomTypes) {
1258 g_autoptr(FlTestStandardMessageCodec) codec =
1260 g_autofree gchar* hex_string =
1262 EXPECT_STREQ(hex_string,
"0c02800568656c6c6f81");
1265 TEST(FlStandardMessageCodecTest, EncodeDecode) {
1277 g_autoptr(GError)
error =
nullptr;
1278 g_autoptr(GBytes) message =
1280 EXPECT_NE(message,
nullptr);
1281 EXPECT_EQ(
error,
nullptr);
1285 EXPECT_EQ(
error,
nullptr);
1286 EXPECT_NE(output,
nullptr);