| Flutter macOS 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
5 #include "flutter/shell/platform/common/client_wrapper/include/flutter/basic_message_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/standard_message_codec.h"
12 #include "gtest/gtest.h"
13
14 namespace flutter {
15
16 namespace {
17
18 class TestBinaryMessenger : public BinaryMessenger {
19 public:
20 void Send(const std::string& channel,
21 const uint8_t* message,
22 const size_t message_size,
23 BinaryReply reply) const override {}
24
25 void SetMessageHandler(const std::string& channel,
26 BinaryMessageHandler handler) override {
27 last_message_handler_channel_ = channel;
28 last_message_handler_ = handler;
29 }
30
31 std::string last_message_handler_channel() {
32 return last_message_handler_channel_;
33 }
34
35 BinaryMessageHandler last_message_handler() { return last_message_handler_; }
36
37 private:
38 std::string last_message_handler_channel_;
39 BinaryMessageHandler last_message_handler_;
40 };
41
42 } // namespace
43
44 // Tests that SetMessageHandler sets a handler that correctly interacts with
45 // the binary messenger.
46 TEST(BasicMessageChannelTest, Registration) {
47 TestBinaryMessenger messenger;
48 const std::string channel_name("some_channel");
49 const StandardMessageCodec& codec = StandardMessageCodec::GetInstance();
50 BasicMessageChannel channel(&messenger, channel_name, &codec);
51
52 bool callback_called = false;
53 const std::string message_value("hello");
54 channel.SetMessageHandler(
55 [&callback_called, message_value](const auto& message, auto reply) {
56 callback_called = true;
57 // Ensure that the wrapper received a correctly decoded message and a
58 // reply.
59 EXPECT_EQ(std::get<std::string>(message), message_value);
60 EXPECT_NE(reply, nullptr);
61 });
62 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
63 EXPECT_NE(messenger.last_message_handler(), nullptr);
64 // Send a test message to trigger the handler test assertions.
65 auto message = codec.EncodeMessage(EncodableValue(message_value));
66
67 messenger.last_message_handler()(
68 message->data(), message->size(),
69 [](const uint8_t* reply, const size_t reply_size) {});
70 EXPECT_EQ(callback_called, true);
71 }
72
73 // Tests that SetMessageHandler with a null handler unregisters the handler.
74 TEST(BasicMessageChannelTest, Unregistration) {
75 TestBinaryMessenger messenger;
76 const std::string channel_name("some_channel");
77 BasicMessageChannel channel(&messenger, channel_name,
78 &flutter::StandardMessageCodec::GetInstance());
79
80 channel.SetMessageHandler([](const auto& message, auto reply) {});
81 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
82 EXPECT_NE(messenger.last_message_handler(), nullptr);
83
84 channel.SetMessageHandler(nullptr);
85 EXPECT_EQ(messenger.last_message_handler_channel(), channel_name);
86 EXPECT_EQ(messenger.last_message_handler(), nullptr);
87 }
88
89 } // namespace flutter
flutter::TEST
TEST(BasicMessageChannelTest, Registration)
Definition: basic_message_channel_unittests.cc:46
flutter::BasicMessageChannel::SetMessageHandler
void SetMessageHandler(const MessageHandler< T > &handler) const
Definition: basic_message_channel.h:72
binary_messenger.h
standard_message_codec.h
flutter::BasicMessageChannel
Definition: basic_message_channel.h:36
SetMessageHandler
static FlutterBinaryMessengerConnection SetMessageHandler(NSObject< FlutterBinaryMessenger > *messenger, NSString *name, FlutterBinaryMessageHandler handler, NSObject< FlutterTaskQueue > *taskQueue)
Definition: FlutterChannels.mm:50
flutter::BinaryReply
std::function< void(const uint8_t *reply, size_t reply_size)> BinaryReply
Definition: binary_messenger.h:17
flutter
Definition: AccessibilityBridgeMac.h:16
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
flutter::BinaryMessageHandler
std::function< void(const uint8_t *message, size_t message_size, BinaryReply reply)> BinaryMessageHandler
Definition: binary_messenger.h:24