Flutter Engine
The Flutter Engine
mock_binary_messenger.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
5#include "flutter/shell/platform/linux/testing/mock_binary_messenger.h"
6#include "flutter/shell/platform/linux/testing/mock_binary_messenger_response_handle.h"
7
8using namespace flutter::testing;
9
10G_DECLARE_FINAL_TYPE(FlMockBinaryMessenger,
11 fl_mock_binary_messenger,
12 FL,
13 MOCK_BINARY_MESSENGER,
14 GObject)
15
16struct _FlMockBinaryMessenger {
17 GObject parent_instance;
19};
20
21static FlBinaryMessenger* fl_mock_binary_messenger_new(
22 MockBinaryMessenger* mock) {
23 FlMockBinaryMessenger* self = FL_MOCK_BINARY_MESSENGER(
24 g_object_new(fl_mock_binary_messenger_get_type(), nullptr));
25 self->mock = mock;
26 return FL_BINARY_MESSENGER(self);
27}
28
29MockBinaryMessenger::MockBinaryMessenger()
30 : instance_(fl_mock_binary_messenger_new(this)) {}
31
33 if (FL_IS_BINARY_MESSENGER(instance_)) {
34 g_clear_object(&instance_);
35 }
36}
37
38MockBinaryMessenger::operator FlBinaryMessenger*() {
39 return instance_;
40}
41
42bool MockBinaryMessenger::HasMessageHandler(const gchar* channel) const {
43 return message_handlers_.at(channel) != nullptr;
44}
45
47 const gchar* channel,
49 gpointer user_data) {
50 message_handlers_[channel] = handler;
51 user_datas_[channel] = user_data;
52}
53
54void MockBinaryMessenger::ReceiveMessage(const gchar* channel,
55 GBytes* message) {
56 FlBinaryMessengerMessageHandler handler = message_handlers_[channel];
57 if (response_handles_[channel] == nullptr) {
58 response_handles_[channel] = FL_BINARY_MESSENGER_RESPONSE_HANDLE(
60 }
61 handler(instance_, channel, message, response_handles_[channel],
62 user_datas_[channel]);
63}
64
66 FlBinaryMessengerInterface* iface);
67
69 FlMockBinaryMessenger,
70 fl_mock_binary_messenger,
71 G_TYPE_OBJECT,
72 G_IMPLEMENT_INTERFACE(fl_binary_messenger_get_type(),
74
75static void fl_mock_binary_messenger_class_init(
76 FlMockBinaryMessengerClass* klass) {}
77
79 FlBinaryMessenger* messenger,
80 const gchar* channel,
82 gpointer user_data,
83 GDestroyNotify destroy_notify) {
84 g_return_if_fail(FL_IS_MOCK_BINARY_MESSENGER(messenger));
85 FlMockBinaryMessenger* self = FL_MOCK_BINARY_MESSENGER(messenger);
86 self->mock->SetMessageHandler(channel, handler, user_data);
87 self->mock->fl_binary_messenger_set_message_handler_on_channel(
88 messenger, channel, handler, user_data, destroy_notify);
89}
90
92 FlBinaryMessenger* messenger,
93 FlBinaryMessengerResponseHandle* response_handle,
94 GBytes* response,
95 GError** error) {
96 g_return_val_if_fail(FL_IS_MOCK_BINARY_MESSENGER(messenger), false);
97 FlMockBinaryMessenger* self = FL_MOCK_BINARY_MESSENGER(messenger);
98 return self->mock->fl_binary_messenger_send_response(
99 messenger, response_handle, response, error);
100}
101
103 FlBinaryMessenger* messenger,
104 const gchar* channel,
105 GBytes* message,
106 GCancellable* cancellable,
107 GAsyncReadyCallback callback,
108 gpointer user_data) {
109 g_return_if_fail(FL_IS_MOCK_BINARY_MESSENGER(messenger));
110 FlMockBinaryMessenger* self = FL_MOCK_BINARY_MESSENGER(messenger);
111 self->mock->fl_binary_messenger_send_on_channel(
112 messenger, channel, message, cancellable, callback, user_data);
113}
114
116 FlBinaryMessenger* messenger,
117 GAsyncResult* result,
118 GError** error) {
119 g_return_val_if_fail(FL_IS_MOCK_BINARY_MESSENGER(messenger), nullptr);
120 FlMockBinaryMessenger* self = FL_MOCK_BINARY_MESSENGER(messenger);
121 return self->mock->fl_binary_messenger_send_on_channel_finish(messenger,
122 result, error);
123}
124
126 FlBinaryMessenger* messenger,
127 const gchar* channel,
128 int64_t new_size) {
129 g_return_if_fail(FL_IS_MOCK_BINARY_MESSENGER(messenger));
130 FlMockBinaryMessenger* self = FL_MOCK_BINARY_MESSENGER(messenger);
131 self->mock->fl_binary_messenger_resize_channel(messenger, channel, new_size);
132}
133
135 FlBinaryMessenger* messenger,
136 const gchar* channel,
137 bool warns) {
138 g_return_if_fail(FL_IS_MOCK_BINARY_MESSENGER(messenger));
139 FlMockBinaryMessenger* self = FL_MOCK_BINARY_MESSENGER(messenger);
140 self->mock->fl_binary_messenger_set_warns_on_channel_overflow(messenger,
141 channel, warns);
142}
143
145 FlBinaryMessengerInterface* iface) {
146 iface->set_message_handler_on_channel =
148 iface->send_response = fl_mock_binary_messenger_send_response;
149 iface->send_on_channel = fl_mock_binary_messenger_send_on_channel;
150 iface->send_on_channel_finish =
152 iface->resize_channel = fl_mock_binary_messenger_resize_channel;
153 iface->set_warns_on_channel_overflow =
155}
156
157static void fl_mock_binary_messenger_init(FlMockBinaryMessenger* self) {}
bool HasMessageHandler(const gchar *channel) const
void ReceiveMessage(const gchar *channel, GBytes *message)
void SetMessageHandler(const gchar *channel, FlBinaryMessengerMessageHandler handler, gpointer user_data)
G_MODULE_EXPORT GObject typedef void(* FlBinaryMessengerMessageHandler)(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
const uint8_t uint32_t uint32_t GError ** error
GAsyncResult * result
Win32Message message
static void fl_mock_binary_messenger_set_message_handler_on_channel(FlBinaryMessenger *messenger, const gchar *channel, FlBinaryMessengerMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
static void fl_mock_binary_messenger_iface_init(FlBinaryMessengerInterface *iface)
static FlBinaryMessenger * fl_mock_binary_messenger_new(MockBinaryMessenger *mock)
G_DECLARE_FINAL_TYPE(FlMockBinaryMessenger, fl_mock_binary_messenger, FL, MOCK_BINARY_MESSENGER, GObject) struct _FlMockBinaryMessenger
static void fl_mock_binary_messenger_resize_channel(FlBinaryMessenger *messenger, const gchar *channel, int64_t new_size)
static gboolean fl_mock_binary_messenger_send_response(FlBinaryMessenger *messenger, FlBinaryMessengerResponseHandle *response_handle, GBytes *response, GError **error)
static void fl_mock_binary_messenger_set_warns_on_channel_overflow(FlBinaryMessenger *messenger, const gchar *channel, bool warns)
static void fl_mock_binary_messenger_send_on_channel(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
static void fl_mock_binary_messenger_init(FlMockBinaryMessenger *self)
G_DEFINE_TYPE_WITH_CODE(FlMockBinaryMessenger, fl_mock_binary_messenger, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(fl_binary_messenger_get_type(), fl_mock_binary_messenger_iface_init)) static void fl_mock_binary_messenger_class_init(FlMockBinaryMessengerClass *klass)
static GBytes * fl_mock_binary_messenger_send_on_channel_finish(FlBinaryMessenger *messenger, GAsyncResult *result, GError **error)
FlMockBinaryMessengerResponseHandle * fl_mock_binary_messenger_response_handle_new()
void * user_data