Flutter Windows Embedder
basic_message_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 
6 
7 #include <memory>
8 #include <string>
9 
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  send_called_ = true;
26  int length = static_cast<int>(message_size);
27  last_message_ =
28  std::vector<uint8_t>(message, message + length * sizeof(uint8_t));
29  }
30 
31  void SetMessageHandler(const std::string& channel,
32  BinaryMessageHandler handler) override {
33  last_message_handler_channel_ = channel;
34  last_message_handler_ = handler;
35  }
36 
37  bool send_called() { return send_called_; }
38 
39  std::string last_message_handler_channel() {
40  return last_message_handler_channel_;
41  }
42 
43  BinaryMessageHandler last_message_handler() { return last_message_handler_; }
44 
45  std::vector<uint8_t> last_message() { return last_message_; }
46 
47  private:
48  mutable bool send_called_ = false;
49  std::string last_message_handler_channel_;
50  BinaryMessageHandler last_message_handler_;
51  mutable std::vector<uint8_t> last_message_;
52 };
53 
54 } // namespace
55 
56 // Tests that SetMessageHandler sets a handler that correctly interacts with
57 // the binary messenger.
58 TEST(BasicMessageChannelTest, Registration) {
59  TestBinaryMessenger messenger;
60  const std::string channel_name("some_channel");
62  BasicMessageChannel channel(&messenger, channel_name, &codec);
63 
64  bool callback_called = false;
65  const std::string message_value("hello");
66  channel.SetMessageHandler(
67  [&callback_called, message_value](const auto& message, auto reply) {
68  callback_called = true;
69  // Ensure that the wrapper received a correctly decoded message and a
70  // reply.
71  EXPECT_EQ(std::get<std::string>(message), message_value);
72  EXPECT_NE(reply, nullptr);
73  });
74  EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
75  EXPECT_NE(messenger.last_message_handler(), nullptr);
76  // Send a test message to trigger the handler test assertions.
77  auto message = codec.EncodeMessage(EncodableValue(message_value));
78 
79  messenger.last_message_handler()(
80  message->data(), message->size(),
81  [](const uint8_t* reply, const size_t reply_size) {});
82  EXPECT_EQ(callback_called, true);
83 }
84 
85 // Tests that SetMessageHandler with a null handler unregisters the handler.
86 TEST(BasicMessageChannelTest, Unregistration) {
87  TestBinaryMessenger messenger;
88  const std::string channel_name("some_channel");
89  BasicMessageChannel channel(&messenger, channel_name,
91 
92  channel.SetMessageHandler([](const auto& message, auto reply) {});
93  EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
94  EXPECT_NE(messenger.last_message_handler(), nullptr);
95 
96  channel.SetMessageHandler(nullptr);
97  EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
98  EXPECT_EQ(messenger.last_message_handler(), nullptr);
99 }
100 
101 // Tests that calling Resize generates the binary message expected by the Dart
102 // implementation.
103 TEST(BasicMessageChannelTest, Resize) {
104  TestBinaryMessenger messenger;
105  const std::string channel_name("flutter/test");
106  BasicMessageChannel channel(&messenger, channel_name,
108 
109  channel.Resize(3);
110 
111  // Because the Dart implementation for the control channel implements its own
112  // custom deserialization logic, this test compares the generated bytes array
113  // to the expected one (for instance, the deserialization logic expects the
114  // size parameter of the resize method call to be an uint32).
115  //
116  // The expected content was created from the following Dart code:
117  // MethodCall call = MethodCall('resize', ['flutter/test',3]);
118  // StandardMethodCodec().encodeMethodCall(call).buffer.asUint8List();
119  const int expected_message_size = 29;
120 
121  EXPECT_EQ(messenger.send_called(), true);
122  EXPECT_EQ(static_cast<int>(messenger.last_message().size()),
123  expected_message_size);
124 
125  int expected[expected_message_size] = {
126  7, 6, 114, 101, 115, 105, 122, 101, 12, 2, 7, 12, 102, 108, 117,
127  116, 116, 101, 114, 47, 116, 101, 115, 116, 3, 3, 0, 0, 0};
128  for (int i = 0; i < expected_message_size; i++) {
129  EXPECT_EQ(messenger.last_message()[i], expected[i]);
130  }
131 }
132 
133 // Tests that calling SetWarnsOnOverflow generates the binary message expected
134 // by the Dart implementation.
135 TEST(BasicMessageChannelTest, SetWarnsOnOverflow) {
136  TestBinaryMessenger messenger;
137 
138  const std::string channel_name("flutter/test");
139  BasicMessageChannel channel(&messenger, channel_name,
141 
142  channel.SetWarnsOnOverflow(false);
143 
144  // Because the Dart implementation for the control channel implements its own
145  // custom deserialization logic, this test compares the generated bytes array
146  // to the expected one.
147  //
148  // The expected content was created from the following Dart code:
149  // MethodCall call = MethodCall('overflow',['flutter/test', true]);
150  // StandardMethodCodec().encodeMethodCall(call).buffer.asUint8List();
151  const int expected_message_size = 27;
152 
153  EXPECT_EQ(messenger.send_called(), true);
154  EXPECT_EQ(static_cast<int>(messenger.last_message().size()),
155  expected_message_size);
156 
157  int expected[expected_message_size] = {
158  7, 8, 111, 118, 101, 114, 102, 108, 111, 119, 12, 2, 7, 12,
159  102, 108, 117, 116, 116, 101, 114, 47, 116, 101, 115, 116, 1};
160  for (int i = 0; i < expected_message_size; i++) {
161  EXPECT_EQ(messenger.last_message()[i], expected[i]);
162  }
163 }
164 
165 } // namespace flutter
flutter::BasicMessageChannel::SetWarnsOnOverflow
void SetWarnsOnOverflow(bool warns)
Definition: basic_message_channel.h:137
flutter::BasicMessageChannel::SetMessageHandler
void SetMessageHandler(const MessageHandler< T > &handler) const
Definition: basic_message_channel.h:92
standard_method_codec.h
flutter::TEST
TEST(FlutterEngineTest, CreateDestroy)
Definition: flutter_engine_unittests.cc:103
binary_messenger.h
standard_message_codec.h
flutter::BasicMessageChannel
Definition: basic_message_channel.h:56
flutter::BinaryReply
std::function< void(const uint8_t *reply, size_t reply_size)> BinaryReply
Definition: binary_messenger.h:17
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::StandardMessageCodec
Definition: standard_message_codec.h:18
basic_message_channel.h
flutter::EncodableValue
Definition: encodable_value.h:165
flutter::StandardMessageCodec::GetInstance
static const StandardMessageCodec & GetInstance(const StandardCodecSerializer *serializer=nullptr)
Definition: standard_codec.cc:293
flutter::MessageCodec::EncodeMessage
std::unique_ptr< std::vector< uint8_t > > EncodeMessage(const T &message) const
Definition: message_codec.h:45
message
Win32Message message
Definition: keyboard_unittests.cc:137
flutter::BasicMessageChannel::Resize
void Resize(int new_size)
Definition: basic_message_channel.h:128
flutter::BinaryMessageHandler
std::function< void(const uint8_t *message, size_t message_size, BinaryReply reply)> BinaryMessageHandler
Definition: binary_messenger.h:24