13 #include "gtest/gtest.h"
19 class TestBinaryMessenger :
public BinaryMessenger {
21 void Send(
const std::string& channel,
23 const size_t message_size,
26 int length =
static_cast<int>(message_size);
31 void SetMessageHandler(
const std::string& channel,
33 last_message_handler_channel_ = channel;
34 last_message_handler_ = handler;
37 bool send_called() {
return send_called_; }
39 std::string last_message_handler_channel() {
40 return last_message_handler_channel_;
45 std::vector<uint8_t> last_message() {
return last_message_; }
48 mutable bool send_called_ =
false;
49 std::string last_message_handler_channel_;
51 mutable std::vector<uint8_t> last_message_;
58 TEST(BasicMessageChannelTest, Registration) {
59 TestBinaryMessenger messenger;
60 const std::string channel_name(
"some_channel");
64 bool callback_called =
false;
65 const std::string message_value(
"hello");
67 [&callback_called, message_value](
const auto&
message,
auto reply) {
68 callback_called =
true;
71 EXPECT_EQ(std::get<std::string>(
message), message_value);
72 EXPECT_NE(reply,
nullptr);
74 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
75 EXPECT_NE(messenger.last_message_handler(),
nullptr);
79 messenger.last_message_handler()(
81 [](
const uint8_t* reply,
const size_t reply_size) {});
82 EXPECT_EQ(callback_called,
true);
86 TEST(BasicMessageChannelTest, Unregistration) {
87 TestBinaryMessenger messenger;
88 const std::string channel_name(
"some_channel");
93 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
94 EXPECT_NE(messenger.last_message_handler(),
nullptr);
97 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
98 EXPECT_EQ(messenger.last_message_handler(),
nullptr);
103 TEST(BasicMessageChannelTest, Resize) {
104 TestBinaryMessenger messenger;
105 const std::string channel_name(
"flutter/test");
119 const int expected_message_size = 29;
121 EXPECT_EQ(messenger.send_called(),
true);
122 EXPECT_EQ(
static_cast<int>(messenger.last_message().size()),
123 expected_message_size);
125 int expected[expected_message_size] = {
126 7, 6, 114, 101, 115, 105, 122, 101, 12, 2, 7, 12, 102, 108, 117,
127 116, 116, 101, 114, 47, 116, 101, 115, 116, 3, 3, 0, 0, 0};
128 for (
int i = 0; i < expected_message_size; i++) {
129 EXPECT_EQ(messenger.last_message()[i], expected[i]);
135 TEST(BasicMessageChannelTest, SetWarnsOnOverflow) {
136 TestBinaryMessenger messenger;
138 const std::string channel_name(
"flutter/test");
151 const int expected_message_size = 27;
153 EXPECT_EQ(messenger.send_called(),
true);
154 EXPECT_EQ(
static_cast<int>(messenger.last_message().size()),
155 expected_message_size);
157 int expected[expected_message_size] = {
158 7, 8, 111, 118, 101, 114, 102, 108, 111, 119, 12, 2, 7, 12,
159 102, 108, 117, 116, 116, 101, 114, 47, 116, 101, 115, 116, 1};
160 for (
int i = 0; i < expected_message_size; i++) {
161 EXPECT_EQ(messenger.last_message()[i], expected[i]);