5#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_channel.h"
10#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h"
11#include "flutter/shell/platform/common/client_wrapper/include/flutter/method_result_functions.h"
12#include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_method_codec.h"
13#include "gtest/gtest.h"
19class TestBinaryMessenger :
public BinaryMessenger {
21 void Send(
const std::string& channel,
26 last_reply_handler_ = reply;
27 int length =
static_cast<int>(message_size);
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_;
62TEST(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()(
87 [](
const uint8_t* reply,
size_t reply_size) {});
92TEST(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);
107TEST(MethodChannelTest, InvokeWithoutResponse) {
108 TestBinaryMessenger messenger;
109 const std::string channel_name(
"some_channel");
115 EXPECT_EQ(messenger.last_reply_handler(),
nullptr);
118TEST(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));
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());
145TEST(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);
167TEST(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]);
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]);
static void SetWarnsOnOverflow(NSObject< FlutterBinaryMessenger > *binaryMessenger, NSString *channel, BOOL warns)
static FlutterBinaryMessengerConnection SetMessageHandler(NSObject< FlutterBinaryMessenger > *messenger, NSString *name, FlutterBinaryMessageHandler handler, NSObject< FlutterTaskQueue > *taskQueue)
void SetMethodCallHandler(MethodCallHandler< T > handler) const
void Resize(int new_size)
void InvokeMethod(const std::string &method, std::unique_ptr< T > arguments, std::unique_ptr< MethodResult< T > > result=nullptr)
void SetWarnsOnOverflow(bool warns)
std::unique_ptr< std::vector< uint8_t > > EncodeMethodCall(const MethodCall< T > &method_call) const
std::unique_ptr< std::vector< uint8_t > > EncodeSuccessEnvelope(const T *result=nullptr) const
static const StandardMethodCodec & GetInstance(const StandardCodecSerializer *serializer=nullptr)
void Send(const std::string &channel, const uint8_t *message, size_t message_size, BinaryReply reply) const override
std::function< void(const uint8_t *message, size_t message_size, BinaryReply reply)> BinaryMessageHandler
TEST(FrameTimingsRecorderTest, RecordVsync)
std::function< void(const uint8_t *reply, size_t reply_size)> BinaryReply
#define EXPECT_TRUE(handle)