10 #include "flutter/shell/platform/common/client_wrapper/testing/stub_flutter_api.h"
11 #include "gtest/gtest.h"
18 class TestApi :
public testing::StubFlutterApi {
21 bool MessengerSend(
const char* channel,
22 const uint8_t* message,
23 const size_t message_size)
override {
24 last_data_sent_ = message;
25 return message_engine_result;
28 bool MessengerSendWithReply(
const char* channel,
29 const uint8_t* message,
30 const size_t message_size,
33 last_data_sent_ = message;
34 return message_engine_result;
37 void MessengerSetCallback(
const char* channel,
40 last_message_callback_set_ =
callback;
43 void PluginRegistrarSetDestructionHandler(
45 last_destruction_callback_set_ =
callback;
48 const uint8_t* last_data_sent() {
return last_data_sent_; }
50 return last_message_callback_set_;
53 return last_destruction_callback_set_;
57 const uint8_t* last_data_sent_ =
nullptr;
64 class TestPluginRegistrar :
public PluginRegistrar {
67 : PluginRegistrar(core_registrar) {}
69 virtual ~TestPluginRegistrar() {
70 if (destruction_callback_) {
71 destruction_callback_();
75 void SetDestructionCallback(std::function<
void()>
callback) {
76 destruction_callback_ = std::move(
callback);
80 std::function<void()> destruction_callback_;
85 class TestPlugin :
public Plugin {
89 TestPlugin(PluginRegistrar* registrar,
bool* registrar_valid_at_destruction)
90 : registrar_(registrar),
91 registrar_valid_at_destruction_(registrar_valid_at_destruction) {}
92 virtual ~TestPlugin() {
93 *registrar_valid_at_destruction_ = registrar_->messenger() !=
nullptr;
97 PluginRegistrar* registrar_;
98 bool* registrar_valid_at_destruction_;
104 TEST(PluginRegistrarTest, PluginDestroyedBeforeRegistrar) {
105 auto dummy_registrar_handle =
107 bool registrar_valid_at_destruction =
false;
111 auto plugin = std::make_unique<TestPlugin>(®istrar,
112 ®istrar_valid_at_destruction);
115 EXPECT_TRUE(registrar_valid_at_destruction);
120 TEST(PluginRegistrarTest, MessengerSend) {
121 testing::ScopedStubFlutterApi scoped_api_stub(std::make_unique<TestApi>());
122 auto test_api =
static_cast<TestApi*
>(scoped_api_stub.stub());
124 auto dummy_registrar_handle =
129 std::vector<uint8_t> message = {1, 2, 3, 4};
130 messenger->
Send(
"some_channel", &message[0], message.size());
131 EXPECT_EQ(test_api->last_data_sent(), &message[0]);
136 TEST(PluginRegistrarTest, MessengerSetMessageHandler) {
137 testing::ScopedStubFlutterApi scoped_api_stub(std::make_unique<TestApi>());
138 auto test_api =
static_cast<TestApi*
>(scoped_api_stub.stub());
140 auto dummy_registrar_handle =
144 const std::string channel_name(
"foo");
148 const size_t message_size,
151 EXPECT_NE(test_api->last_message_callback_set(),
nullptr);
155 EXPECT_EQ(test_api->last_message_callback_set(),
nullptr);
160 TEST(PluginRegistrarTest, ManagerSameInstance) {
164 testing::ScopedStubFlutterApi scoped_api_stub(std::make_unique<TestApi>());
166 auto dummy_registrar_handle =
175 TEST(PluginRegistrarTest, ManagerDifferentInstances) {
179 testing::ScopedStubFlutterApi scoped_api_stub(std::make_unique<TestApi>());
181 auto dummy_registrar_handle_a =
183 auto dummy_registrar_handle_b =
192 TEST(PluginRegistrarTest, ManagerRemovesOnDestruction) {
196 testing::ScopedStubFlutterApi scoped_api_stub(std::make_unique<TestApi>());
197 auto test_api =
static_cast<TestApi*
>(scoped_api_stub.stub());
199 auto dummy_registrar_handle =
202 manager->
GetRegistrar<TestPluginRegistrar>(dummy_registrar_handle);
206 EXPECT_NE(test_api->last_destruction_callback_set(),
nullptr);
207 bool destroyed =
false;
208 wrapper->SetDestructionCallback([&destroyed]() { destroyed =
true; });
209 test_api->last_destruction_callback_set()(dummy_registrar_handle);
210 EXPECT_EQ(destroyed,
true);
213 EXPECT_NE(manager->
GetRegistrar<TestPluginRegistrar>(dummy_registrar_handle),
218 TEST(PluginRegistrarTest, TextureRegistrarNotNull) {
219 auto dummy_registrar_handle =
225 ASSERT_NE(texture_registrar,
nullptr);