| Flutter Linux Embedder |

fl_string_codec_test.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "flutter/shell/platform/linux/public/flutter_linux/fl_string_codec.h"
6 #include "flutter/shell/platform/linux/testing/fl_test.h"
7 #include "gtest/gtest.h"
8
9 // Encodes a message using a FlStringCodec. Return a hex string with the encoded
10 // binary output.
11 static gchar* encode_message(FlValue* value) {
12 g_autoptr(FlStringCodec) codec = fl_string_codec_new();
13 g_autoptr(GError) error = nullptr;
14 g_autoptr(GBytes) message =
15 fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error);
16 EXPECT_NE(message, nullptr);
17 EXPECT_EQ(error, nullptr);
18
19 return bytes_to_hex_string(message);
20 }
21
22 // Encodes a message using a FlStringCodec. Expect the given error.
23 static void encode_message_error(FlValue* value, GQuark domain, int code) {
24 g_autoptr(FlStringCodec) codec = fl_string_codec_new();
25 g_autoptr(GError) error = nullptr;
26 g_autoptr(GBytes) message =
27 fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error);
28 EXPECT_EQ(message, nullptr);
29 EXPECT_TRUE(g_error_matches(error, domain, code));
30 }
31
32 // Decodes a message using a FlStringCodec. The binary data is given in the form
33 // of a hex string.
34 static FlValue* decode_message(const char* hex_string) {
35 g_autoptr(FlStringCodec) codec = fl_string_codec_new();
36 g_autoptr(GBytes) message = hex_string_to_bytes(hex_string);
37 g_autoptr(GError) error = nullptr;
38 g_autoptr(FlValue) value =
39 fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
40 EXPECT_EQ(error, nullptr);
41 EXPECT_NE(value, nullptr);
42 return fl_value_ref(value);
43 }
44
45 TEST(FlStringCodecTest, EncodeData) {
46 g_autoptr(FlValue) value = fl_value_new_string("hello");
47 g_autofree gchar* hex_string = encode_message(value);
48 EXPECT_STREQ(hex_string, "68656c6c6f");
49 }
50
51 TEST(FlStringCodecTest, EncodeEmpty) {
52 g_autoptr(FlValue) value = fl_value_new_string("");
53 g_autofree gchar* hex_string = encode_message(value);
54 EXPECT_STREQ(hex_string, "");
55 }
56
57 TEST(FlStringCodecTest, EncodeNullptr) {
58 encode_message_error(nullptr, FL_MESSAGE_CODEC_ERROR,
59 FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE);
60 }
61
62 TEST(FlStringCodecTest, EncodeUnknownType) {
63 g_autoptr(FlValue) value = fl_value_new_null();
64 encode_message_error(value, FL_MESSAGE_CODEC_ERROR,
65 FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE);
66 }
67
68 TEST(FlStringCodecTest, DecodeData) {
69 g_autoptr(FlValue) value = decode_message("68656c6c6f");
70 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
71 ASSERT_STREQ(fl_value_get_string(value), "hello");
72 }
73
74 TEST(FlStringCodecTest, DecodeEmpty) {
75 g_autoptr(FlValue) value = decode_message("");
76 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
77 ASSERT_STREQ(fl_value_get_string(value), "");
78 }
79
80 TEST(FlStringCodecTest, EncodeDecode) {
81 g_autoptr(FlStringCodec) codec = fl_string_codec_new();
82
83 g_autoptr(FlValue) input = fl_value_new_string("hello");
84
85 g_autoptr(GError) error = nullptr;
86 g_autoptr(GBytes) message =
87 fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), input, &error);
88 EXPECT_NE(message, nullptr);
89 EXPECT_EQ(error, nullptr);
90
91 g_autoptr(FlValue) output =
92 fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
93 EXPECT_EQ(error, nullptr);
94 EXPECT_NE(output, nullptr);
95
96 ASSERT_TRUE(fl_value_equal(input, output));
97 }
fl_message_codec_encode_message
G_MODULE_EXPORT GBytes * fl_message_codec_encode_message(FlMessageCodec *self, FlValue *message, GError **error)
Definition: fl_message_codec.cc:17
fl_message_codec_decode_message
G_MODULE_EXPORT FlValue * fl_message_codec_decode_message(FlMessageCodec *self, GBytes *message, GError **error)
Definition: fl_message_codec.cc:33
FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE
@ FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE
Definition: fl_message_codec.h:36
FL_MESSAGE_CODEC_ERROR
#define FL_MESSAGE_CODEC_ERROR
Definition: fl_message_codec.h:30
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
value
uint8_t value
Definition: fl_standard_message_codec.cc:36
fl_string_codec_new
G_MODULE_EXPORT FlStringCodec * fl_string_codec_new()
Definition: fl_string_codec.cc:53
fl_string_codec.h
encode_message
static gchar * encode_message(FlValue *value)
Definition: fl_string_codec_test.cc:11
encode_message_error
static void encode_message_error(FlValue *value, GQuark domain, int code)
Definition: fl_string_codec_test.cc:23
TEST
TEST(FlStringCodecTest, EncodeData)
Definition: fl_string_codec_test.cc:45
decode_message
static FlValue * decode_message(const char *hex_string)
Definition: fl_string_codec_test.cc:34
fl_value_new_string
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276
fl_value_get_type
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:466
fl_value_ref
G_MODULE_EXPORT FlValue * fl_value_ref(FlValue *self)
Definition: fl_value.cc:394
fl_value_new_null
G_MODULE_EXPORT FlValue * fl_value_new_null()
Definition: fl_value.cc:251
fl_value_equal
G_MODULE_EXPORT bool fl_value_equal(FlValue *a, FlValue *b)
Definition: fl_value.cc:471
fl_value_get_string
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:682
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
FL_VALUE_TYPE_STRING
@ FL_VALUE_TYPE_STRING
Definition: fl_value.h:68