Flutter Engine
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  BinaryMessageHandler last_message_handler() { return last_message_handler_; }
37 
38  private:
39  std::string last_message_handler_channel_;
40  BinaryMessageHandler last_message_handler_;
41 };
42 
43 } // namespace
44 
45 // Tests that SetStreamHandler sets a handler that correctly interacts with
46 // the binary messenger.
47 TEST(EventChannelTest, Registration) {
48  TestBinaryMessenger messenger;
49  const std::string channel_name("some_channel");
51  EventChannel channel(&messenger, channel_name, &codec);
52 
53  bool on_listen_called = false;
54  auto handler = std::make_unique<StreamHandlerFunctions<>>(
55  [&on_listen_called](const EncodableValue* arguments,
56  std::unique_ptr<EventSink<>>&& events)
57  -> std::unique_ptr<StreamHandlerError<>> {
58  on_listen_called = true;
59  return nullptr;
60  },
61  [](const EncodableValue* arguments)
62  -> std::unique_ptr<StreamHandlerError<>> { return nullptr; });
63  channel.SetStreamHandler(std::move(handler));
64  EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
65  EXPECT_NE(messenger.last_message_handler(), nullptr);
66 
67  // Send dummy listen message.
68  MethodCall<> call("listen", nullptr);
69  auto message = codec.EncodeMethodCall(call);
70  messenger.last_message_handler()(
71  message->data(), message->size(),
72  [](const uint8_t* reply, const size_t reply_size) {});
73 
74  // Check results.
75  EXPECT_EQ(on_listen_called, true);
76 }
77 
78 // Tests that SetStreamHandler with a null handler unregisters the handler.
79 TEST(EventChannelTest, Unregistration) {
80  TestBinaryMessenger messenger;
81  const std::string channel_name("some_channel");
83  EventChannel channel(&messenger, channel_name, &codec);
84 
85  auto handler = std::make_unique<StreamHandlerFunctions<>>(
86  [](const EncodableValue* arguments, std::unique_ptr<EventSink<>>&& events)
87  -> std::unique_ptr<StreamHandlerError<>> { return nullptr; },
88  [](const EncodableValue* arguments)
89  -> std::unique_ptr<StreamHandlerError<>> { return nullptr; });
90  channel.SetStreamHandler(std::move(handler));
91  EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
92  EXPECT_NE(messenger.last_message_handler(), nullptr);
93 
94  channel.SetStreamHandler(nullptr);
95  EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
96  EXPECT_EQ(messenger.last_message_handler(), nullptr);
97 }
98 
99 // Test that OnCancel callback sequence.
100 TEST(EventChannelTest, Cancel) {
101  TestBinaryMessenger messenger;
102  const std::string channel_name("some_channel");
104  EventChannel channel(&messenger, channel_name, &codec);
105 
106  bool on_listen_called = false;
107  bool on_cancel_called = false;
108  auto handler = std::make_unique<StreamHandlerFunctions<>>(
109  [&on_listen_called](const EncodableValue* arguments,
110  std::unique_ptr<EventSink<>>&& events)
111  -> std::unique_ptr<StreamHandlerError<>> {
112  on_listen_called = true;
113  return nullptr;
114  },
115  [&on_cancel_called](const EncodableValue* arguments)
116  -> std::unique_ptr<StreamHandlerError<>> {
117  on_cancel_called = true;
118  return nullptr;
119  });
120  channel.SetStreamHandler(std::move(handler));
121  EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
122  EXPECT_NE(messenger.last_message_handler(), nullptr);
123 
124  // Send dummy listen message.
125  MethodCall<> call_listen("listen", nullptr);
126  auto message = codec.EncodeMethodCall(call_listen);
127  messenger.last_message_handler()(
128  message->data(), message->size(),
129  [](const uint8_t* reply, const size_t reply_size) {});
130  EXPECT_EQ(on_listen_called, true);
131 
132  // Send dummy cancel message.
133  MethodCall<> call_cancel("cancel", nullptr);
134  message = codec.EncodeMethodCall(call_cancel);
135  messenger.last_message_handler()(
136  message->data(), message->size(),
137  [](const uint8_t* reply, const size_t reply_size) {});
138 
139  // Check results.
140  EXPECT_EQ(on_cancel_called, true);
141 }
142 
143 // Tests that OnCancel in not called on registration.
144 TEST(EventChannelTest, ListenNotCancel) {
145  TestBinaryMessenger messenger;
146  const std::string channel_name("some_channel");
148  EventChannel channel(&messenger, channel_name, &codec);
149 
150  bool on_listen_called = false;
151  bool on_cancel_called = false;
152  auto handler = std::make_unique<StreamHandlerFunctions<>>(
153  [&on_listen_called](const EncodableValue* arguments,
154  std::unique_ptr<EventSink<>>&& events)
155  -> std::unique_ptr<StreamHandlerError<>> {
156  on_listen_called = true;
157  return nullptr;
158  },
159  [&on_cancel_called](const EncodableValue* arguments)
160  -> std::unique_ptr<StreamHandlerError<>> {
161  on_cancel_called = true;
162  return nullptr;
163  });
164  channel.SetStreamHandler(std::move(handler));
165  EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
166  EXPECT_NE(messenger.last_message_handler(), nullptr);
167 
168  // Send dummy listen message.
169  MethodCall<> call_listen("listen", nullptr);
170  auto message = codec.EncodeMethodCall(call_listen);
171  messenger.last_message_handler()(
172  message->data(), message->size(),
173  [](const uint8_t* reply, const size_t reply_size) {});
174 
175  // Check results.
176  EXPECT_EQ(on_listen_called, true);
177  EXPECT_EQ(on_cancel_called, false);
178 }
179 
180 // Pseudo test when user re-registers or call OnListen to the same channel.
181 // Confirm that OnCancel is called and OnListen is called again
182 // when user re-registers the same channel that has already started
183 // communication.
184 TEST(EventChannelTest, ReRegistration) {
185  TestBinaryMessenger messenger;
186  const std::string channel_name("some_channel");
188  EventChannel channel(&messenger, channel_name, &codec);
189 
190  bool on_listen_called = false;
191  bool on_cancel_called = false;
192  auto handler = std::make_unique<StreamHandlerFunctions<>>(
193  [&on_listen_called](const EncodableValue* arguments,
194  std::unique_ptr<EventSink<>>&& events)
195  -> std::unique_ptr<StreamHandlerError<>> {
196  on_listen_called = true;
197  return nullptr;
198  },
199  [&on_cancel_called](const EncodableValue* arguments)
200  -> std::unique_ptr<StreamHandlerError<>> {
201  on_cancel_called = true;
202  return nullptr;
203  });
204  channel.SetStreamHandler(std::move(handler));
205  EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
206  EXPECT_NE(messenger.last_message_handler(), nullptr);
207 
208  // Send dummy listen message.
209  MethodCall<> call("listen", nullptr);
210  auto message = codec.EncodeMethodCall(call);
211  messenger.last_message_handler()(
212  message->data(), message->size(),
213  [](const uint8_t* reply, const size_t reply_size) {});
214  EXPECT_EQ(on_listen_called, true);
215 
216  // Send second dummy message to test StreamHandler's OnCancel
217  // method is called before OnListen method is called.
218  on_listen_called = false;
219  message = codec.EncodeMethodCall(call);
220  messenger.last_message_handler()(
221  message->data(), message->size(),
222  [](const uint8_t* reply, const size_t reply_size) {});
223 
224  // Check results.
225  EXPECT_EQ(on_cancel_called, true);
226  EXPECT_EQ(on_listen_called, true);
227 }
228 
229 } // namespace flutter
static FlutterBinaryMessengerConnection SetMessageHandler(NSObject< FlutterBinaryMessenger > *messenger, NSString *name, FlutterBinaryMessageHandler handler, NSObject< FlutterTaskQueue > *taskQueue)
void Send(const std::string &channel, const uint8_t *message, size_t message_size, BinaryReply reply) const override
void SetStreamHandler(std::unique_ptr< StreamHandler< T >> handler)
Definition: event_channel.h:50
std::function< void(const uint8_t *reply, size_t reply_size)> BinaryReply
std::unique_ptr< std::vector< uint8_t > > EncodeMethodCall(const MethodCall< T > &method_call) const
Definition: method_codec.h:48
static const StandardMethodCodec & GetInstance(const StandardCodecSerializer *serializer=nullptr)
TEST(DartServiceIsolateTest, CanAddAndRemoveHandles)
std::function< void(const uint8_t *message, size_t message_size, BinaryReply reply)> BinaryMessageHandler