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/cpp/client_wrapper/include/flutter/event_channel.h"
6 
7 #include <memory>
8 #include <string>
9 
10 #include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/binary_messenger.h"
11 #include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h"
12 #include "flutter/shell/platform/common/cpp/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 // Pseudo test when user re-registers or call OnListen to the same channel.
144 // Confirm that OnCancel is called and OnListen is called again
145 // when user re-registers the same channel that has already started
146 // communication.
147 TEST(EventChannelTest, ReRegistration) {
148  TestBinaryMessenger messenger;
149  const std::string channel_name("some_channel");
151  EventChannel channel(&messenger, channel_name, &codec);
152 
153  bool on_listen_called = false;
154  bool on_cancel_called = false;
155  auto handler = std::make_unique<StreamHandlerFunctions<>>(
156  [&on_listen_called](const EncodableValue* arguments,
157  std::unique_ptr<EventSink<>>&& events)
158  -> std::unique_ptr<StreamHandlerError<>> {
159  on_listen_called = true;
160  return nullptr;
161  },
162  [&on_cancel_called](const EncodableValue* arguments)
163  -> std::unique_ptr<StreamHandlerError<>> {
164  on_cancel_called = true;
165  return nullptr;
166  });
167  channel.SetStreamHandler(std::move(handler));
168  EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
169  EXPECT_NE(messenger.last_message_handler(), nullptr);
170 
171  // Send dummy listen message.
172  MethodCall<> call("listen", nullptr);
173  auto message = codec.EncodeMethodCall(call);
174  messenger.last_message_handler()(
175  message->data(), message->size(),
176  [](const uint8_t* reply, const size_t reply_size) {});
177  EXPECT_EQ(on_listen_called, true);
178 
179  // Send second dummy message to test StreamHandler's OnCancel
180  // method is called before OnListen method is called.
181  on_listen_called = false;
182  message = codec.EncodeMethodCall(call);
183  messenger.last_message_handler()(
184  message->data(), message->size(),
185  [](const uint8_t* reply, const size_t reply_size) {});
186 
187  // Check results.
188  EXPECT_EQ(on_cancel_called, true);
189  EXPECT_EQ(on_listen_called, true);
190 }
191 
192 } // namespace flutter
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