13 #include "gtest/gtest.h"
19 class TestBinaryMessenger :
public BinaryMessenger {
21 void Send(
const std::string& channel,
22 const uint8_t* message,
26 last_reply_handler_ = reply;
27 int length =
static_cast<int>(message_size);
29 std::vector<uint8_t>(message, message +
length *
sizeof(uint8_t));
32 void SetMessageHandler(
const std::string& channel,
34 last_message_handler_channel_ = channel;
35 last_message_handler_ = handler;
38 bool send_called() {
return send_called_; }
40 BinaryReply last_reply_handler() {
return last_reply_handler_; }
42 std::string last_message_handler_channel() {
43 return last_message_handler_channel_;
48 std::vector<uint8_t> last_message() {
return last_message_; }
51 mutable bool send_called_ =
false;
53 std::string last_message_handler_channel_;
55 mutable std::vector<uint8_t> last_message_;
62 TEST(MethodChannelTest, Registration) {
63 TestBinaryMessenger messenger;
64 const std::string channel_name(
"some_channel");
68 bool callback_called =
false;
69 const std::string method_name(
"hello");
71 [&callback_called, method_name](
const auto& call,
auto result) {
72 callback_called =
true;
75 EXPECT_EQ(call.method_name(), method_name);
76 EXPECT_NE(
result,
nullptr);
79 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
80 EXPECT_NE(messenger.last_message_handler(),
nullptr);
85 messenger.last_message_handler()(
86 message->data(), message->size(),
87 [](
const uint8_t* reply,
size_t reply_size) {});
88 EXPECT_TRUE(callback_called);
92 TEST(MethodChannelTest, Unregistration) {
93 TestBinaryMessenger messenger;
94 const std::string channel_name(
"some_channel");
99 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
100 EXPECT_NE(messenger.last_message_handler(),
nullptr);
103 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
104 EXPECT_EQ(messenger.last_message_handler(),
nullptr);
107 TEST(MethodChannelTest, InvokeWithoutResponse) {
108 TestBinaryMessenger messenger;
109 const std::string channel_name(
"some_channel");
114 EXPECT_TRUE(messenger.send_called());
115 EXPECT_EQ(messenger.last_reply_handler(),
nullptr);
118 TEST(MethodChannelTest, InvokeWithResponse) {
119 TestBinaryMessenger messenger;
120 const std::string channel_name(
"some_channel");
124 bool received_reply =
false;
125 const std::string reply =
"bar";
126 auto result_handler = std::make_unique<MethodResultFunctions<>>(
128 received_reply =
true;
129 EXPECT_EQ(std::get<std::string>(*success_value), reply);
133 channel.
InvokeMethod(
"foo",
nullptr, std::move(result_handler));
134 EXPECT_TRUE(messenger.send_called());
135 ASSERT_NE(messenger.last_reply_handler(),
nullptr);
139 std::unique_ptr<std::vector<uint8_t>> encoded_reply =
141 messenger.last_reply_handler()(encoded_reply->data(), encoded_reply->size());
142 EXPECT_TRUE(received_reply);
145 TEST(MethodChannelTest, InvokeNotImplemented) {
146 TestBinaryMessenger messenger;
147 const std::string channel_name(
"some_channel");
151 bool received_not_implemented =
false;
152 auto result_handler = std::make_unique<MethodResultFunctions<>>(
154 [&received_not_implemented]() { received_not_implemented =
true; });
156 channel.
InvokeMethod(
"foo",
nullptr, std::move(result_handler));
157 EXPECT_EQ(messenger.send_called(),
true);
158 ASSERT_NE(messenger.last_reply_handler(),
nullptr);
161 messenger.last_reply_handler()(
nullptr, 0);
162 EXPECT_TRUE(received_not_implemented);
167 TEST(MethodChannelTest, Resize) {
168 TestBinaryMessenger messenger;
169 const std::string channel_name(
"flutter/test");
183 const int expected_message_size = 29;
185 EXPECT_EQ(messenger.send_called(),
true);
186 EXPECT_EQ(
static_cast<int>(messenger.last_message().size()),
187 expected_message_size);
189 int expected[expected_message_size] = {
190 7, 6, 114, 101, 115, 105, 122, 101, 12, 2, 7, 12, 102, 108, 117,
191 116, 116, 101, 114, 47, 116, 101, 115, 116, 3, 3, 0, 0, 0};
192 for (
int i = 0;
i < expected_message_size;
i++) {
193 EXPECT_EQ(messenger.last_message()[
i], expected[
i]);
199 TEST(MethodChannelTest, SetWarnsOnOverflow) {
200 TestBinaryMessenger messenger;
202 const std::string channel_name(
"flutter/test");
211 const int expected_message_size = 27;
213 EXPECT_EQ(messenger.send_called(),
true);
214 EXPECT_EQ(
static_cast<int>(messenger.last_message().size()),
215 expected_message_size);
217 int expected[expected_message_size] = {
218 7, 8, 111, 118, 101, 114, 102, 108, 111, 119, 12, 2, 7, 12,
219 102, 108, 117, 116, 116, 101, 114, 47, 116, 101, 115, 116, 1};
220 for (
int i = 0;
i < expected_message_size;
i++) {
221 EXPECT_EQ(messenger.last_message()[
i], expected[
i]);