Flutter Engine
The Flutter Engine
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
test_binary_messenger.h
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#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_TEST_BINARY_MESSENGER_H_
6#define FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_TEST_BINARY_MESSENGER_H_
7
8#include <functional>
9#include <map>
10#include <string>
11
12#include "flutter/fml/logging.h"
13#include "flutter/fml/macros.h"
14#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h"
15
16namespace flutter {
17
18// A trivial BinaryMessenger implementation for use in tests.
19class TestBinaryMessenger : public BinaryMessenger {
20 public:
21 using SendHandler = std::function<void(const std::string& channel,
22 const uint8_t* message,
23 size_t message_size,
24 BinaryReply reply)>;
25
26 // Creates a new messenge that forwards all calls to |send_handler|.
27 explicit TestBinaryMessenger(SendHandler send_handler = nullptr)
28 : send_handler_(std::move(send_handler)) {}
29
30 virtual ~TestBinaryMessenger() = default;
31
32 // Simulates a message from the engine on the given channel.
33 //
34 // Returns false if no handler is registered on that channel.
35 bool SimulateEngineMessage(const std::string& channel,
36 const uint8_t* message,
37 size_t message_size,
38 BinaryReply reply) {
39 auto handler = registered_handlers_.find(channel);
40 if (handler == registered_handlers_.end()) {
41 return false;
42 }
43 (handler->second)(message, message_size, reply);
44 return true;
45 }
46
47 // |flutter::BinaryMessenger|
48 void Send(const std::string& channel,
49 const uint8_t* message,
50 size_t message_size,
51 BinaryReply reply) const override {
52 // If something under test sends a message, the test should be handling it.
53 FML_DCHECK(send_handler_);
54 send_handler_(channel, message, message_size, reply);
55 }
56
57 // |flutter::BinaryMessenger|
58 void SetMessageHandler(const std::string& channel,
59 BinaryMessageHandler handler) override {
60 if (handler) {
61 registered_handlers_[channel] = handler;
62 } else {
63 registered_handlers_.erase(channel);
64 }
65 }
66
67 private:
68 // Handler to call for SendMessage.
69 SendHandler send_handler_;
70
71 // Mapping of channel name to registered handlers.
72 std::map<std::string, BinaryMessageHandler> registered_handlers_;
73
74 FML_DISALLOW_COPY_AND_ASSIGN(TestBinaryMessenger);
75};
76
77} // namespace flutter
78
79#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_TESTING_TEST_BINARY_MESSENGER_H_
void Send(const std::string &channel, const uint8_t *message, size_t message_size, BinaryReply reply) const override
virtual ~TestBinaryMessenger()=default
void SetMessageHandler(const std::string &channel, BinaryMessageHandler handler) override
TestBinaryMessenger(SendHandler send_handler=nullptr)
std::function< void(const std::string &channel, const uint8_t *message, size_t message_size, BinaryReply reply)> SendHandler
bool SimulateEngineMessage(const std::string &channel, const uint8_t *message, size_t message_size, BinaryReply reply)
#define FML_DCHECK(condition)
Definition: logging.h:103
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27
Dart_NativeFunction function
Definition: fuchsia.cc:51
Win32Message message
std::function< void(const uint8_t *message, size_t message_size, BinaryReply reply)> BinaryMessageHandler
std::function< void(const uint8_t *reply, size_t reply_size)> BinaryReply
Definition: ref_ptr.h:256