|
| 1 | +// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved. |
| 2 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 3 | +// Use of this source code is governed by a BSD-style license that can be |
| 4 | +// found in the LICENSE file. |
| 5 | + |
| 6 | +#ifndef EMBEDDER_TESTING_TEST_BINARY_MESSENGER_H_ |
| 7 | +#define EMBEDDER_TESTING_TEST_BINARY_MESSENGER_H_ |
| 8 | + |
| 9 | +#include <cassert> |
| 10 | +#include <functional> |
| 11 | +#include <map> |
| 12 | +#include <string> |
| 13 | + |
| 14 | +#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" |
| 15 | + |
| 16 | +namespace flutter { |
| 17 | + |
| 18 | +// A trivial BinaryMessenger implementation for use in tests. |
| 19 | +class 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 | + // Prevent copying. |
| 33 | + TestBinaryMessenger(TestBinaryMessenger const&) = delete; |
| 34 | + TestBinaryMessenger& operator=(TestBinaryMessenger const&) = delete; |
| 35 | + |
| 36 | + // Simulates a message from the engine on the given channel. |
| 37 | + // |
| 38 | + // Returns false if no handler is registered on that channel. |
| 39 | + bool SimulateEngineMessage(const std::string& channel, |
| 40 | + const uint8_t* message, |
| 41 | + size_t message_size, |
| 42 | + BinaryReply reply) { |
| 43 | + auto handler = registered_handlers_.find(channel); |
| 44 | + if (handler == registered_handlers_.end()) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + (handler->second)(message, message_size, reply); |
| 48 | + return true; |
| 49 | + } |
| 50 | + |
| 51 | + // |flutter::BinaryMessenger| |
| 52 | + void Send(const std::string& channel, |
| 53 | + const uint8_t* message, |
| 54 | + size_t message_size, |
| 55 | + BinaryReply reply) const override { |
| 56 | + // If something under test sends a message, the test should be handling it. |
| 57 | + assert(send_handler_); |
| 58 | + send_handler_(channel, message, message_size, reply); |
| 59 | + } |
| 60 | + |
| 61 | + // |flutter::BinaryMessenger| |
| 62 | + void SetMessageHandler(const std::string& channel, |
| 63 | + BinaryMessageHandler handler) override { |
| 64 | + if (handler) { |
| 65 | + registered_handlers_[channel] = handler; |
| 66 | + } else { |
| 67 | + registered_handlers_.erase(channel); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private: |
| 72 | + // Handler to call for SendMessage. |
| 73 | + SendHandler send_handler_; |
| 74 | + |
| 75 | + // Mapping of channel name to registered handlers. |
| 76 | + std::map<std::string, BinaryMessageHandler> registered_handlers_; |
| 77 | +}; |
| 78 | + |
| 79 | +} // namespace flutter |
| 80 | + |
| 81 | +#endif // EMBEDDER_TESTING_TEST_BINARY_MESSENGER_H_ |
0 commit comments