13 #include "gtest/gtest.h"
19 class TestBinaryMessenger :
public BinaryMessenger {
21 void Send(
const std::string& channel,
23 const size_t message_size,
26 void SetMessageHandler(
const std::string& channel,
28 last_message_handler_channel_ = channel;
29 last_message_handler_ = handler;
32 std::string last_message_handler_channel() {
33 return last_message_handler_channel_;
37 return last_message_handler_;
41 std::string last_message_handler_channel_;
49 TEST(EventChannelTest, Registration) {
50 TestBinaryMessenger messenger;
51 const std::string channel_name(
"some_channel");
55 bool on_listen_called =
false;
56 auto handler = std::make_unique<StreamHandlerFunctions<>>(
58 std::unique_ptr<EventSink<>>&& events)
60 on_listen_called =
true;
66 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
67 EXPECT_NE(messenger.last_message_handler(),
nullptr);
72 messenger.last_message_handler()(
74 [](
const uint8_t* reply,
const size_t reply_size) {});
77 EXPECT_EQ(on_listen_called,
true);
81 TEST(EventChannelTest, Unregistration) {
82 TestBinaryMessenger messenger;
83 const std::string channel_name(
"some_channel");
87 auto handler = std::make_unique<StreamHandlerFunctions<>>(
88 [](
const EncodableValue* arguments, std::unique_ptr<EventSink<>>&& events)
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);
102 TEST(EventChannelTest, Cancel) {
103 TestBinaryMessenger messenger;
104 const std::string channel_name(
"some_channel");
108 bool on_listen_called =
false;
109 bool on_cancel_called =
false;
110 auto handler = std::make_unique<StreamHandlerFunctions<>>(
112 std::unique_ptr<EventSink<>>&& events)
114 on_listen_called =
true;
119 on_cancel_called =
true;
123 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
124 EXPECT_NE(messenger.last_message_handler(),
nullptr);
129 messenger.last_message_handler()(
131 [](
const uint8_t* reply,
const size_t reply_size) {});
132 EXPECT_EQ(on_listen_called,
true);
137 messenger.last_message_handler()(
139 [](
const uint8_t* reply,
const size_t reply_size) {});
142 EXPECT_EQ(on_cancel_called,
true);
146 TEST(EventChannelTest, ListenNotCancel) {
147 TestBinaryMessenger messenger;
148 const std::string channel_name(
"some_channel");
152 bool on_listen_called =
false;
153 bool on_cancel_called =
false;
154 auto handler = std::make_unique<StreamHandlerFunctions<>>(
156 std::unique_ptr<EventSink<>>&& events)
158 on_listen_called =
true;
163 on_cancel_called =
true;
167 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
168 EXPECT_NE(messenger.last_message_handler(),
nullptr);
173 messenger.last_message_handler()(
175 [](
const uint8_t* reply,
const size_t reply_size) {});
178 EXPECT_EQ(on_listen_called,
true);
179 EXPECT_EQ(on_cancel_called,
false);
186 TEST(EventChannelTest, ReRegistration) {
187 TestBinaryMessenger messenger;
188 const std::string channel_name(
"some_channel");
192 bool on_listen_called =
false;
193 bool on_cancel_called =
false;
194 auto handler = std::make_unique<StreamHandlerFunctions<>>(
196 std::unique_ptr<EventSink<>>&& events)
198 on_listen_called =
true;
203 on_cancel_called =
true;
207 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
208 EXPECT_NE(messenger.last_message_handler(),
nullptr);
213 messenger.last_message_handler()(
215 [](
const uint8_t* reply,
const size_t reply_size) {});
216 EXPECT_EQ(on_listen_called,
true);
220 on_listen_called =
false;
222 messenger.last_message_handler()(
224 [](
const uint8_t* reply,
const size_t reply_size) {});
227 EXPECT_EQ(on_cancel_called,
true);
228 EXPECT_EQ(on_listen_called,
true);
232 TEST(EventChannelTest, HandlerOutlivesEventChannel) {
233 TestBinaryMessenger messenger;
234 const std::string channel_name(
"some_channel");
237 bool on_listen_called =
false;
238 bool on_cancel_called =
false;
241 auto handler = std::make_unique<StreamHandlerFunctions<>>(
243 std::unique_ptr<EventSink<>>&& events)
245 on_listen_called =
true;
250 on_cancel_called =
true;
257 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
258 EXPECT_NE(messenger.last_message_handler(),
nullptr);
263 messenger.last_message_handler()(
265 [](
const uint8_t* reply,
const size_t reply_size) {});
266 EXPECT_EQ(on_listen_called,
true);
271 messenger.last_message_handler()(
273 [](
const uint8_t* reply,
const size_t reply_size) {});
274 EXPECT_EQ(on_cancel_called,
true);
277 TEST(EventChannelTest, StreamHandlerErrorPassByValue) {
278 std::unique_ptr<StreamHandlerError<>> error =
nullptr;
281 std::string code =
"Code";
282 std::string msg =
"Message";
283 std::unique_ptr<EncodableValue> details =
284 std::make_unique<EncodableValue>(
"Details");
286 std::make_unique<StreamHandlerError<>>(code, msg, std::move(details));
289 ASSERT_NE(error.get(),
nullptr);
290 EXPECT_EQ(error->error_code,
"Code");
291 EXPECT_EQ(error->error_message,
"Message");
292 EXPECT_EQ(std::get<std::string>(*error->error_details),
"Details");
295 TEST(EventChannelTest, StreamHandlerErrorNullptr) {
296 std::unique_ptr<StreamHandlerError<>> error =
297 std::make_unique<StreamHandlerError<>>(
"Code",
"Message",
nullptr);
299 ASSERT_NE(error.get(),
nullptr);
300 EXPECT_FALSE(error->error_details);