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