Flutter macOS Embedder
incoming_message_dispatcher_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 
6 
7 #include "gtest/gtest.h"
8 
9 namespace flutter {
11  FlutterDesktopMessengerRef messenger =
12  reinterpret_cast<FlutterDesktopMessengerRef>(0xfeedface);
13  const uint8_t* message_data = reinterpret_cast<const uint8_t*>(0xcafebabe);
14  auto dispatcher = std::make_unique<IncomingMessageDispatcher>(messenger);
15  bool did_call = false;
16  dispatcher->SetMessageCallback(
17  "hello",
18  [](FlutterDesktopMessengerRef messenger,
19  const FlutterDesktopMessage* message, void* user_data) {
20  EXPECT_EQ(messenger,
21  reinterpret_cast<FlutterDesktopMessengerRef>(0xfeedface));
22  EXPECT_EQ(message->message,
23  reinterpret_cast<const uint8_t*>(0xcafebabe));
24  EXPECT_EQ(message->message_size, 123u);
25  *reinterpret_cast<bool*>(user_data) = true;
26  },
27  &did_call);
28  FlutterDesktopMessage message = {
30  .channel = "hello",
31  .message = message_data,
32  .message_size = 123,
33  .response_handle = nullptr,
34  };
35  dispatcher->HandleMessage(message);
36  EXPECT_TRUE(did_call);
37 }
38 
39 TEST(IncomingMessageDispatcher, BlockInputFalse) {
40  FlutterDesktopMessengerRef messenger = nullptr;
41  auto dispatcher = std::make_unique<IncomingMessageDispatcher>(messenger);
42  bool did_call[3] = {false, false, false};
43  dispatcher->SetMessageCallback(
44  "hello",
45  [](FlutterDesktopMessengerRef messenger,
46  const FlutterDesktopMessage* message,
47  void* user_data) { reinterpret_cast<bool*>(user_data)[0] = true; },
48  &did_call);
49  FlutterDesktopMessage message = {
51  .channel = "hello",
52  .message = nullptr,
53  .message_size = 0,
54  .response_handle = nullptr,
55  };
56  dispatcher->HandleMessage(
57  message, [&did_call] { did_call[1] = true; },
58  [&did_call] { did_call[2] = true; });
59  EXPECT_TRUE(did_call[0]);
60  EXPECT_FALSE(did_call[1]);
61  EXPECT_FALSE(did_call[2]);
62 }
63 
64 TEST(IncomingMessageDispatcher, BlockInputTrue) {
65  FlutterDesktopMessengerRef messenger = nullptr;
66  auto dispatcher = std::make_unique<IncomingMessageDispatcher>(messenger);
67  static int counter = 0;
68  int did_call[3] = {-1, -1, -1};
69  dispatcher->EnableInputBlockingForChannel("hello");
70  dispatcher->SetMessageCallback(
71  "hello",
72  [](FlutterDesktopMessengerRef messenger,
73  const FlutterDesktopMessage* message,
74  void* user_data) { reinterpret_cast<int*>(user_data)[counter++] = 1; },
75  &did_call);
76  FlutterDesktopMessage message = {
78  .channel = "hello",
79  .message = nullptr,
80  .message_size = 0,
81  .response_handle = nullptr,
82  };
83  dispatcher->HandleMessage(
84  message, [&did_call] { did_call[counter++] = 0; },
85  [&did_call] { did_call[counter++] = 2; });
86  EXPECT_EQ(did_call[0], 0);
87  EXPECT_EQ(did_call[1], 1);
88  EXPECT_EQ(did_call[2], 2);
89 }
90 
91 } // namespace flutter
flutter::TEST
TEST(BasicMessageChannelTest, Registration)
Definition: basic_message_channel_unittests.cc:58
flutter::IncomingMessageDispatcher
Definition: incoming_message_dispatcher.h:20
user_data
void * user_data
Definition: texture_registrar_unittests.cc:27
FlutterDesktopMessage::message_size
size_t message_size
Definition: flutter_messenger.h:39
flutter
Definition: AccessibilityBridgeMac.h:16
FlutterDesktopMessengerRef
struct FlutterDesktopMessenger * FlutterDesktopMessengerRef
Definition: flutter_messenger.h:19
FlutterDesktopMessage
Definition: flutter_messenger.h:31
incoming_message_dispatcher.h
FlutterDesktopMessage::message
const uint8_t * message
Definition: flutter_messenger.h:37
FlutterDesktopMessage::struct_size
size_t struct_size
Definition: flutter_messenger.h:33