Flutter Engine
fl_message_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_message_codec.h"
6 #include "gtest/gtest.h"
7 
8 G_DECLARE_FINAL_TYPE(FlTestCodec, fl_test_codec, FL, TEST_CODEC, FlMessageCodec)
9 
10 // Implement the FlMessageCodec API for the following tests to check it works as
11 // expected.
12 struct _FlTestCodec {
13  FlMessageCodec parent_instance;
14 };
15 
16 G_DEFINE_TYPE(FlTestCodec, fl_test_codec, fl_message_codec_get_type())
17 
18 // Implements FlMessageCodec::encode_message.
19 static GBytes* fl_test_codec_encode_message(FlMessageCodec* codec,
20  FlValue* value,
21  GError** error) {
22  EXPECT_TRUE(FL_IS_TEST_CODEC(codec));
23 
24  if (fl_value_get_type(value) == FL_VALUE_TYPE_INT) {
25  char c = '0' + fl_value_get_int(value);
26  return g_bytes_new(&c, 1);
27  } else {
29  "ERROR");
30  return nullptr;
31  }
32 }
33 
34 // Implements FlMessageCodec::decode_message.
35 static FlValue* fl_test_codec_decode_message(FlMessageCodec* codec,
36  GBytes* message,
37  GError** error) {
38  EXPECT_TRUE(FL_IS_TEST_CODEC(codec));
39 
40  size_t data_length;
41  const uint8_t* data =
42  static_cast<const uint8_t*>(g_bytes_get_data(message, &data_length));
43  if (data_length < 1) {
44  g_set_error(error, FL_MESSAGE_CODEC_ERROR,
46  return FALSE;
47  }
48  if (data_length > 1) {
49  g_set_error(error, FL_MESSAGE_CODEC_ERROR,
51  "FL_MESSAGE_CODEC_ERROR_ADDITIONAL_DATA");
52  return FALSE;
53  }
54 
55  return fl_value_new_int(data[0] - '0');
56 }
57 
58 static void fl_test_codec_class_init(FlTestCodecClass* klass) {
59  FL_MESSAGE_CODEC_CLASS(klass)->encode_message = fl_test_codec_encode_message;
60  FL_MESSAGE_CODEC_CLASS(klass)->decode_message = fl_test_codec_decode_message;
61 }
62 
63 static void fl_test_codec_init(FlTestCodec* self) {}
64 
65 static FlTestCodec* fl_test_codec_new() {
66  return FL_TEST_CODEC(g_object_new(fl_test_codec_get_type(), nullptr));
67 }
68 
69 TEST(FlMessageCodecTest, EncodeMessage) {
70  g_autoptr(FlTestCodec) codec = fl_test_codec_new();
71 
72  g_autoptr(FlValue) value = fl_value_new_int(1);
73  g_autoptr(GError) error = nullptr;
74  g_autoptr(GBytes) message =
75  fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error);
76  EXPECT_NE(message, nullptr);
77  EXPECT_EQ(error, nullptr);
78  EXPECT_EQ(g_bytes_get_size(message), static_cast<gsize>(1));
79  EXPECT_EQ(static_cast<const uint8_t*>(g_bytes_get_data(message, nullptr))[0],
80  '1');
81 }
82 
83 TEST(FlMessageCodecTest, EncodeMessageError) {
84  g_autoptr(FlTestCodec) codec = fl_test_codec_new();
85 
86  g_autoptr(FlValue) value = fl_value_new_null();
87  g_autoptr(GError) error = nullptr;
88  g_autoptr(GBytes) message =
89  fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error);
90  EXPECT_EQ(message, nullptr);
91  EXPECT_TRUE(g_error_matches(error, FL_MESSAGE_CODEC_ERROR,
93 }
94 
95 TEST(FlMessageCodecTest, DecodeMessageEmpty) {
96  g_autoptr(FlTestCodec) codec = fl_test_codec_new();
97  g_autoptr(GBytes) message = g_bytes_new(nullptr, 0);
98 
99  g_autoptr(GError) error = nullptr;
100  g_autoptr(FlValue) value =
101  fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
102  EXPECT_EQ(value, nullptr);
103  EXPECT_TRUE(g_error_matches(error, FL_MESSAGE_CODEC_ERROR,
105 }
106 
107 TEST(FlMessageCodecTest, DecodeMessage) {
108  g_autoptr(FlTestCodec) codec = fl_test_codec_new();
109  uint8_t data[] = {'1'};
110  g_autoptr(GBytes) message = g_bytes_new(data, 1);
111 
112  g_autoptr(GError) error = nullptr;
113  g_autoptr(FlValue) value =
114  fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
115  EXPECT_NE(value, nullptr);
116  EXPECT_EQ(error, nullptr);
117 
118  ASSERT_TRUE(fl_value_get_type(value) == FL_VALUE_TYPE_INT);
119  EXPECT_EQ(fl_value_get_int(value), 1);
120 }
121 
122 TEST(FlMessageCodecTest, DecodeMessageExtraData) {
123  g_autoptr(FlTestCodec) codec = fl_test_codec_new();
124  uint8_t data[] = {'1', '2'};
125  g_autoptr(GBytes) message = g_bytes_new(data, 2);
126 
127  g_autoptr(GError) error = nullptr;
128  g_autoptr(FlValue) value =
129  fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
130  EXPECT_EQ(value, nullptr);
131  EXPECT_TRUE(g_error_matches(error, FL_MESSAGE_CODEC_ERROR,
133 }
TEST(FlMessageCodecTest, EncodeMessage)
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:395
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:39
FlMethodResponse GError ** error
static FlTestCodec * fl_test_codec_new()
G_MODULE_EXPORT int64_t fl_value_get_int(FlValue *self)
Definition: fl_value.cc:582
#define FL_MESSAGE_CODEC_ERROR
FlMessageCodec parent_instance
static FlValue * fl_test_codec_decode_message(FlMessageCodec *codec, GBytes *message, GError **error)
uint8_t value
static GBytes * fl_test_codec_encode_message(FlMessageCodec *codec, FlValue *value, GError **error)
static void fl_test_codec_init(FlTestCodec *self)
static void fl_test_codec_class_init(FlTestCodecClass *klass)
G_MODULE_EXPORT FlValue * fl_value_new_int(int64_t value)
Definition: fl_value.cc:233
G_MODULE_EXPORT GType fl_message_codec_get_type()
G_MODULE_EXPORT GBytes * fl_message_codec_encode_message(FlMessageCodec *self, FlValue *message, GError **error)
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
G_BEGIN_DECLS G_DECLARE_FINAL_TYPE(FlKeyEventPlugin, fl_key_event_plugin, FL, KEY_EVENT_PLUGIN, GObject)
G_MODULE_EXPORT FlValue * fl_message_codec_decode_message(FlMessageCodec *self, GBytes *message, GError **error)
G_BEGIN_DECLS FL
G_MODULE_EXPORT FlValue * fl_value_new_null()
Definition: fl_value.cc:222