| Flutter Linux Embedder |

event_channel_unittests.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/common/client_wrapper/include/flutter/event_channel.h"
6
7 #include <memory>
8 #include <string>
9
10 #include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h"
11 #include "flutter/shell/platform/common/client_wrapper/include/flutter/event_stream_handler_functions.h"
12 #include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_method_codec.h"
13 #include "gtest/gtest.h"
14
15 namespace flutter {
16
17 namespace {
18
19 class TestBinaryMessenger : public BinaryMessenger {
20 public:
21 void Send(const std::string& channel,
22 const uint8_t* message,
23 const size_t message_size,
24 BinaryReply reply) const override {}
25
26 void SetMessageHandler(const std::string& channel,
27 BinaryMessageHandler handler) override {
28 last_message_handler_channel_ = channel;
29 last_message_handler_ = handler;
30 }
31
32 std::string last_message_handler_channel() {
33 return last_message_handler_channel_;
34 }
35
36 const BinaryMessageHandler& last_message_handler() {
37 return last_message_handler_;
38 }
39
40 private:
41 std::string last_message_handler_channel_;
42 BinaryMessageHandler last_message_handler_;
43 };
44
45 } // namespace
46
47 // Tests that SetStreamHandler sets a handler that correctly interacts with
48 // the binary messenger.
49 TEST(EventChannelTest, Registration) {
50 TestBinaryMessenger messenger;
51 const std::string channel_name("some_channel");
52 const StandardMethodCodec& codec = StandardMethodCodec::GetInstance();
53 EventChannel channel(&messenger, channel_name, &codec);
54
55 bool on_listen_called = false;
56 auto handler = std::make_unique<StreamHandlerFunctions<>>(
57 [&on_listen_called](const EncodableValue* arguments,
58 std::unique_ptr<EventSink<>>&& events)
59 -> std::unique_ptr<StreamHandlerError<>> {
60 on_listen_called = true;
61 return nullptr;
62 },
63 [](const EncodableValue* arguments)
64 -> std::unique_ptr<StreamHandlerError<>> { return nullptr; });
65 channel.SetStreamHandler(std::move(handler));
66 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
67 EXPECT_NE(messenger.last_message_handler(), nullptr);
68
69 // Send test listen message.
70 MethodCall<> call("listen", nullptr);
71 auto message = codec.EncodeMethodCall(call);
72 messenger.last_message_handler()(
73 message->data(), message->size(),
74 [](const uint8_t* reply, const size_t reply_size) {});
75
76 // Check results.
77 EXPECT_EQ(on_listen_called, true);
78 }
79
80 // Tests that SetStreamHandler with a null handler unregisters the handler.
81 TEST(EventChannelTest, Unregistration) {
82 TestBinaryMessenger messenger;
83 const std::string channel_name("some_channel");
84 const StandardMethodCodec& codec = StandardMethodCodec::GetInstance();
85 EventChannel channel(&messenger, channel_name, &codec);
86
87 auto handler = std::make_unique<StreamHandlerFunctions<>>(
88 [](const EncodableValue* arguments, std::unique_ptr<EventSink<>>&& events)
89 -> std::unique_ptr<StreamHandlerError<>> { return nullptr; },
90 [](const EncodableValue* arguments)
91 -> std::unique_ptr<StreamHandlerError<>> { return nullptr; });
92 channel.SetStreamHandler(std::move(handler));
93 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
94 EXPECT_NE(messenger.last_message_handler(), nullptr);
95
96 channel.SetStreamHandler(nullptr);
97 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
98 EXPECT_EQ(messenger.last_message_handler(), nullptr);
99 }
100
101 // Test that OnCancel callback sequence.
102 TEST(EventChannelTest, Cancel) {
103 TestBinaryMessenger messenger;
104 const std::string channel_name("some_channel");
105 const StandardMethodCodec& codec = StandardMethodCodec::GetInstance();
106 EventChannel channel(&messenger, channel_name, &codec);
107
108 bool on_listen_called = false;
109 bool on_cancel_called = false;
110 auto handler = std::make_unique<StreamHandlerFunctions<>>(
111 [&on_listen_called](const EncodableValue* arguments,
112 std::unique_ptr<EventSink<>>&& events)
113 -> std::unique_ptr<StreamHandlerError<>> {
114 on_listen_called = true;
115 return nullptr;
116 },
117 [&on_cancel_called](const EncodableValue* arguments)
118 -> std::unique_ptr<StreamHandlerError<>> {
119 on_cancel_called = true;
120 return nullptr;
121 });
122 channel.SetStreamHandler(std::move(handler));
123 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
124 EXPECT_NE(messenger.last_message_handler(), nullptr);
125
126 // Send test listen message.
127 MethodCall<> call_listen("listen", nullptr);
128 auto message = codec.EncodeMethodCall(call_listen);
129 messenger.last_message_handler()(
130 message->data(), message->size(),
131 [](const uint8_t* reply, const size_t reply_size) {});
132 EXPECT_EQ(on_listen_called, true);
133
134 // Send test cancel message.
135 MethodCall<> call_cancel("cancel", nullptr);
136 message = codec.EncodeMethodCall(call_cancel);
137 messenger.last_message_handler()(
138 message->data(), message->size(),
139 [](const uint8_t* reply, const size_t reply_size) {});
140
141 // Check results.
142 EXPECT_EQ(on_cancel_called, true);
143 }
144
145 // Tests that OnCancel in not called on registration.
146 TEST(EventChannelTest, ListenNotCancel) {
147 TestBinaryMessenger messenger;
148 const std::string channel_name("some_channel");
149 const StandardMethodCodec& codec = StandardMethodCodec::GetInstance();
150 EventChannel channel(&messenger, channel_name, &codec);
151
152 bool on_listen_called = false;
153 bool on_cancel_called = false;
154 auto handler = std::make_unique<StreamHandlerFunctions<>>(
155 [&on_listen_called](const EncodableValue* arguments,
156 std::unique_ptr<EventSink<>>&& events)
157 -> std::unique_ptr<StreamHandlerError<>> {
158 on_listen_called = true;
159 return nullptr;
160 },
161 [&on_cancel_called](const EncodableValue* arguments)
162 -> std::unique_ptr<StreamHandlerError<>> {
163 on_cancel_called = true;
164 return nullptr;
165 });
166 channel.SetStreamHandler(std::move(handler));
167 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
168 EXPECT_NE(messenger.last_message_handler(), nullptr);
169
170 // Send test listen message.
171 MethodCall<> call_listen("listen", nullptr);
172 auto message = codec.EncodeMethodCall(call_listen);
173 messenger.last_message_handler()(
174 message->data(), message->size(),
175 [](const uint8_t* reply, const size_t reply_size) {});
176
177 // Check results.
178 EXPECT_EQ(on_listen_called, true);
179 EXPECT_EQ(on_cancel_called, false);
180 }
181
182 // Pseudo test when user re-registers or call OnListen to the same channel.
183 // Confirm that OnCancel is called and OnListen is called again
184 // when user re-registers the same channel that has already started
185 // communication.
186 TEST(EventChannelTest, ReRegistration) {
187 TestBinaryMessenger messenger;
188 const std::string channel_name("some_channel");
189 const StandardMethodCodec& codec = StandardMethodCodec::GetInstance();
190 EventChannel channel(&messenger, channel_name, &codec);
191
192 bool on_listen_called = false;
193 bool on_cancel_called = false;
194 auto handler = std::make_unique<StreamHandlerFunctions<>>(
195 [&on_listen_called](const EncodableValue* arguments,
196 std::unique_ptr<EventSink<>>&& events)
197 -> std::unique_ptr<StreamHandlerError<>> {
198 on_listen_called = true;
199 return nullptr;
200 },
201 [&on_cancel_called](const EncodableValue* arguments)
202 -> std::unique_ptr<StreamHandlerError<>> {
203 on_cancel_called = true;
204 return nullptr;
205 });
206 channel.SetStreamHandler(std::move(handler));
207 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
208 EXPECT_NE(messenger.last_message_handler(), nullptr);
209
210 // Send test listen message.
211 MethodCall<> call("listen", nullptr);
212 auto message = codec.EncodeMethodCall(call);
213 messenger.last_message_handler()(
214 message->data(), message->size(),
215 [](const uint8_t* reply, const size_t reply_size) {});
216 EXPECT_EQ(on_listen_called, true);
217
218 // Send second test message to test StreamHandler's OnCancel
219 // method is called before OnListen method is called.
220 on_listen_called = false;
221 message = codec.EncodeMethodCall(call);
222 messenger.last_message_handler()(
223 message->data(), message->size(),
224 [](const uint8_t* reply, const size_t reply_size) {});
225
226 // Check results.
227 EXPECT_EQ(on_cancel_called, true);
228 EXPECT_EQ(on_listen_called, true);
229 }
230
231 // Test that the handler is called even if the event channel is destroyed.
232 TEST(EventChannelTest, HandlerOutlivesEventChannel) {
233 TestBinaryMessenger messenger;
234 const std::string channel_name("some_channel");
235 const StandardMethodCodec& codec = StandardMethodCodec::GetInstance();
236
237 bool on_listen_called = false;
238 bool on_cancel_called = false;
239 {
240 EventChannel channel(&messenger, channel_name, &codec);
241 auto handler = std::make_unique<StreamHandlerFunctions<>>(
242 [&on_listen_called](const EncodableValue* arguments,
243 std::unique_ptr<EventSink<>>&& events)
244 -> std::unique_ptr<StreamHandlerError<>> {
245 on_listen_called = true;
246 return nullptr;
247 },
248 [&on_cancel_called](const EncodableValue* arguments)
249 -> std::unique_ptr<StreamHandlerError<>> {
250 on_cancel_called = true;
251 return nullptr;
252 });
253 channel.SetStreamHandler(std::move(handler));
254 }
255
256 // The event channel was destroyed but the handler should still be alive.
257 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
258 EXPECT_NE(messenger.last_message_handler(), nullptr);
259
260 // Send test listen message.
261 MethodCall<> call_listen("listen", nullptr);
262 auto message = codec.EncodeMethodCall(call_listen);
263 messenger.last_message_handler()(
264 message->data(), message->size(),
265 [](const uint8_t* reply, const size_t reply_size) {});
266 EXPECT_EQ(on_listen_called, true);
267
268 // Send test cancel message.
269 MethodCall<> call_cancel("cancel", nullptr);
270 message = codec.EncodeMethodCall(call_cancel);
271 messenger.last_message_handler()(
272 message->data(), message->size(),
273 [](const uint8_t* reply, const size_t reply_size) {});
274 EXPECT_EQ(on_cancel_called, true);
275 }
276
277 TEST(EventChannelTest, StreamHandlerErrorPassByValue) {
278 std::unique_ptr<StreamHandlerError<>> error = nullptr;
279
280 {
281 std::string code = "Code";
282 std::string msg = "Message";
283 std::unique_ptr<EncodableValue> details =
284 std::make_unique<EncodableValue>("Details");
285 error =
286 std::make_unique<StreamHandlerError<>>(code, msg, std::move(details));
287 }
288
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");
293 }
294
295 TEST(EventChannelTest, StreamHandlerErrorNullptr) {
296 std::unique_ptr<StreamHandlerError<>> error =
297 std::make_unique<StreamHandlerError<>>("Code", "Message", nullptr);
298
299 ASSERT_NE(error.get(), nullptr);
300 EXPECT_FALSE(error->error_details);
301 }
302
303 } // namespace flutter
binary_messenger.h
flutter::EncodableValue
Definition: encodable_value.h:166
flutter::EventChannel
Definition: event_channel.h:33
flutter::EventChannel::SetStreamHandler
void SetStreamHandler(std::unique_ptr< StreamHandler< T >> handler)
Definition: event_channel.h:54
flutter::MethodCall
Definition: method_call.h:18
flutter::MethodCodec::EncodeMethodCall
std::unique_ptr< std::vector< uint8_t > > EncodeMethodCall(const MethodCall< T > &method_call) const
Definition: method_codec.h:48
flutter::StandardMethodCodec
Definition: standard_method_codec.h:18
flutter::StandardMethodCodec::GetInstance
static const StandardMethodCodec & GetInstance(const StandardCodecSerializer *serializer=nullptr)
Definition: standard_codec.cc:341
event_channel.h
event_stream_handler_functions.h
message
const char * message
Definition: fl_accessibility_handler.cc:21
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
flutter
Definition: accessibility_bridge.cc:14
flutter::BinaryMessageHandler
std::function< void(const uint8_t *message, size_t message_size, BinaryReply reply)> BinaryMessageHandler
Definition: binary_messenger.h:25
flutter::BinaryReply
std::function< void(const uint8_t *reply, size_t reply_size)> BinaryReply
Definition: binary_messenger.h:18
flutter::TEST
TEST(BasicMessageChannelTest, Registration)
Definition: basic_message_channel_unittests.cc:58
standard_method_codec.h
flutter::StreamHandlerError
Definition: event_stream_handler.h:18