Flutter Linux Embedder
fl_platform_handler_test.cc File Reference
#include <gtk/gtk.h>
#include "flutter/shell/platform/linux/fl_binary_messenger_private.h"
#include "flutter/shell/platform/linux/fl_method_codec_private.h"
#include "flutter/shell/platform/linux/fl_platform_handler.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_json_method_codec.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_method_codec.h"
#include "flutter/shell/platform/linux/testing/fl_test.h"
#include "flutter/shell/platform/linux/testing/mock_binary_messenger.h"
#include "flutter/testing/testing.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

Go to the source code of this file.

Classes

class  MethodCallMatcher
 

Functions

 MATCHER_P (SuccessResponse, result, "")
 
static ::testing::Matcher< GBytes * > MethodCall (const std::string &name, ::testing::Matcher< FlValue * > args)
 
 MATCHER_P (FlValueEq, value, "equal to "+::testing::PrintToString(value))
 
 G_DECLARE_FINAL_TYPE (FlTestApplication, fl_test_application, FL, TEST_APPLICATION, GtkApplication) struct _FlTestApplication
 
 G_DEFINE_TYPE (FlTestApplication, fl_test_application, gtk_application_get_type()) static void fl_test_application_startup(GApplication *application)
 
static void fl_test_application_activate (GApplication *application)
 
static void fl_test_application_dispose (GObject *object)
 
static void fl_test_application_class_init (FlTestApplicationClass *klass)
 
static void fl_test_application_init (FlTestApplication *self)
 
FlTestApplication * fl_test_application_new (gboolean *dispose_called)
 
 TEST (FlPlatformHandlerTest, PlaySound)
 
 TEST (FlPlatformHandlerTest, ExitApplication)
 
 TEST (FlPlatformHandlerTest, ExitApplicationDispose)
 

Function Documentation

◆ fl_test_application_activate()

static void fl_test_application_activate ( GApplication *  application)
static

Definition at line 116 of file fl_platform_handler_test.cc.

116  {
117  G_APPLICATION_CLASS(fl_test_application_parent_class)->activate(application);
118 
119  ::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
120  g_autoptr(FlPlatformHandler) handler = fl_platform_handler_new(messenger);
121  EXPECT_NE(handler, nullptr);
122  g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
123 
124  g_autoptr(FlValue) exit_result = fl_value_new_map();
125  fl_value_set_string_take(exit_result, "response",
126  fl_value_new_string("exit"));
127  EXPECT_CALL(messenger,
129  ::testing::Eq<FlBinaryMessenger*>(messenger), ::testing::_,
130  SuccessResponse(exit_result), ::testing::_))
131  .WillOnce(::testing::Return(true));
132 
133  // Request app exit.
134  g_autoptr(FlValue) args = fl_value_new_map();
135  fl_value_set_string_take(args, "type", fl_value_new_string("required"));
136  g_autoptr(GBytes) message = fl_method_codec_encode_method_call(
137  FL_METHOD_CODEC(codec), "System.exitApplication", args, nullptr);
138  messenger.ReceiveMessage("flutter/platform", message);
139 }

References args, fl_binary_messenger_send_response(), fl_json_method_codec_new(), fl_method_codec_encode_method_call(), fl_platform_handler_new(), fl_value_new_map(), fl_value_new_string(), and fl_value_set_string_take().

Referenced by fl_test_application_class_init().

◆ fl_test_application_class_init()

static void fl_test_application_class_init ( FlTestApplicationClass *  klass)
static

Definition at line 149 of file fl_platform_handler_test.cc.

149  {
150  G_OBJECT_CLASS(klass)->dispose = fl_test_application_dispose;
151  G_APPLICATION_CLASS(klass)->startup = fl_test_application_startup;
152  G_APPLICATION_CLASS(klass)->activate = fl_test_application_activate;
153 }

References fl_test_application_activate(), and fl_test_application_dispose().

◆ fl_test_application_dispose()

static void fl_test_application_dispose ( GObject *  object)
static

Definition at line 141 of file fl_platform_handler_test.cc.

141  {
142  FlTestApplication* self = FL_TEST_APPLICATION(object);
143 
144  *self->dispose_called = true;
145 
146  G_OBJECT_CLASS(fl_test_application_parent_class)->dispose(object);
147 }

Referenced by fl_test_application_class_init().

◆ fl_test_application_init()

static void fl_test_application_init ( FlTestApplication *  self)
static

Definition at line 155 of file fl_platform_handler_test.cc.

155 {}

◆ fl_test_application_new()

FlTestApplication* fl_test_application_new ( gboolean *  dispose_called)

Definition at line 157 of file fl_platform_handler_test.cc.

157  {
158  FlTestApplication* self = FL_TEST_APPLICATION(
159  g_object_new(fl_test_application_get_type(), nullptr));
160 
161  // Don't try and register on D-Bus.
162  g_application_set_application_id(G_APPLICATION(self), "dev.flutter.GtkTest");
163  g_application_set_flags(G_APPLICATION(self), G_APPLICATION_NON_UNIQUE);
164 
165  // Added to stop compiler complaining about an unused function.
166  FL_IS_TEST_APPLICATION(self);
167 
168  self->dispose_called = dispose_called;
169 
170  return self;
171 }

Referenced by TEST().

◆ G_DECLARE_FINAL_TYPE()

G_DECLARE_FINAL_TYPE ( FlTestApplication  ,
fl_test_application  ,
FL  ,
TEST_APPLICATION  ,
GtkApplication   
)

Definition at line 92 of file fl_platform_handler_test.cc.

98  {
99  GtkApplication parent_instance;
100  gboolean* dispose_called;
101 };

◆ G_DEFINE_TYPE()

G_DEFINE_TYPE ( FlTestApplication  ,
fl_test_application  ,
gtk_application_get_type()   
)

Definition at line 103 of file fl_platform_handler_test.cc.

107  {
108  G_APPLICATION_CLASS(fl_test_application_parent_class)->startup(application);
109 
110  // Add a window to this application, which will hold a reference to the
111  // application and stop it disposing. See
112  // https://gitlab.gnome.org/GNOME/gtk/-/issues/6190
113  gtk_application_window_new(GTK_APPLICATION(application));
114 }

◆ MATCHER_P() [1/2]

MATCHER_P ( FlValueEq  ,
value  ,
"equal to "+  ::testing::PrintToStringvalue 
)

Definition at line 88 of file fl_platform_handler_test.cc.

88  {
89  return fl_value_equal(arg, value);
90 }

References fl_value_equal(), and value.

◆ MATCHER_P() [2/2]

MATCHER_P ( SuccessResponse  ,
result  ,
""   
)

Definition at line 19 of file fl_platform_handler_test.cc.

19  {
20  g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
21  g_autoptr(FlMethodResponse) response =
22  fl_method_codec_decode_response(FL_METHOD_CODEC(codec), arg, nullptr);
23  if (fl_value_equal(fl_method_response_get_result(response, nullptr),
24  result)) {
25  return true;
26  }
27  *result_listener << ::testing::PrintToString(response);
28  return false;
29 }

References fl_json_method_codec_new(), fl_method_codec_decode_response(), fl_method_response_get_result(), fl_value_equal(), and result.

◆ MethodCall()

static ::testing::Matcher<GBytes*> MethodCall ( const std::string &  name,
::testing::Matcher< FlValue * >  args 
)

Definition at line 82 of file fl_platform_handler_test.cc.

84  {
85  return MethodCallMatcher(::testing::StrEq(name), std::move(args));
86 }

References args.

Referenced by TEST().

◆ TEST() [1/3]

TEST ( FlPlatformHandlerTest  ,
ExitApplication   
)

Definition at line 193 of file fl_platform_handler_test.cc.

193  {
194  ::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
195 
196  g_autoptr(FlPlatformHandler) handler = fl_platform_handler_new(messenger);
197  EXPECT_NE(handler, nullptr);
198  g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
199 
200  g_autoptr(FlValue) null = fl_value_new_null();
201  ON_CALL(messenger, fl_binary_messenger_send_response(
202  ::testing::Eq<FlBinaryMessenger*>(messenger),
203  ::testing::_, SuccessResponse(null), ::testing::_))
204  .WillByDefault(testing::Return(TRUE));
205 
206  // Indicate that the binding is initialized.
207  g_autoptr(GError) error = nullptr;
208  g_autoptr(GBytes) init_message = fl_method_codec_encode_method_call(
209  FL_METHOD_CODEC(codec), "System.initializationComplete", nullptr, &error);
210  messenger.ReceiveMessage("flutter/platform", init_message);
211 
212  g_autoptr(FlValue) request_args = fl_value_new_map();
213  fl_value_set_string_take(request_args, "type",
214  fl_value_new_string("cancelable"));
215  EXPECT_CALL(messenger,
217  ::testing::Eq<FlBinaryMessenger*>(messenger),
218  ::testing::StrEq("flutter/platform"),
219  MethodCall("System.requestAppExit", FlValueEq(request_args)),
220  ::testing::_, ::testing::_, ::testing::_));
221 
222  g_autoptr(FlValue) args = fl_value_new_map();
223  fl_value_set_string_take(args, "type", fl_value_new_string("cancelable"));
224  g_autoptr(GBytes) message = fl_method_codec_encode_method_call(
225  FL_METHOD_CODEC(codec), "System.exitApplication", args, nullptr);
226  messenger.ReceiveMessage("flutter/platform", message);
227 }

References args, error, fl_binary_messenger_send_on_channel(), fl_binary_messenger_send_response(), fl_json_method_codec_new(), fl_method_codec_encode_method_call(), fl_platform_handler_new(), fl_value_new_map(), fl_value_new_null(), fl_value_new_string(), fl_value_set_string_take(), MethodCall(), and TRUE.

◆ TEST() [2/3]

TEST ( FlPlatformHandlerTest  ,
ExitApplicationDispose   
)

Definition at line 229 of file fl_platform_handler_test.cc.

229  {
230  gtk_init(0, nullptr);
231 
232  gboolean dispose_called = false;
233  FlTestApplication* application = fl_test_application_new(&dispose_called);
234 
235  // Run the application, it will quit after startup.
236  g_application_run(G_APPLICATION(application), 0, nullptr);
237 
238  EXPECT_FALSE(dispose_called);
239  g_object_unref(application);
240  EXPECT_TRUE(dispose_called);
241 }

References fl_test_application_new().

◆ TEST() [3/3]

TEST ( FlPlatformHandlerTest  ,
PlaySound   
)

Definition at line 173 of file fl_platform_handler_test.cc.

173  {
174  ::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
175 
176  g_autoptr(FlPlatformHandler) handler = fl_platform_handler_new(messenger);
177  EXPECT_NE(handler, nullptr);
178 
179  g_autoptr(FlValue) args = fl_value_new_string("SystemSoundType.alert");
180  g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
181  g_autoptr(GBytes) message = fl_method_codec_encode_method_call(
182  FL_METHOD_CODEC(codec), "SystemSound.play", args, nullptr);
183 
184  g_autoptr(FlValue) null = fl_value_new_null();
185  EXPECT_CALL(messenger, fl_binary_messenger_send_response(
186  ::testing::Eq<FlBinaryMessenger*>(messenger),
187  ::testing::_, SuccessResponse(null), ::testing::_))
188  .WillOnce(::testing::Return(true));
189 
190  messenger.ReceiveMessage("flutter/platform", message);
191 }

References args, fl_binary_messenger_send_response(), fl_json_method_codec_new(), fl_method_codec_encode_method_call(), fl_platform_handler_new(), fl_value_new_null(), and fl_value_new_string().

fl_json_method_codec_new
G_MODULE_EXPORT FlJsonMethodCodec * fl_json_method_codec_new()
Definition: fl_json_method_codec.cc:205
fl_method_codec_encode_method_call
GBytes * fl_method_codec_encode_method_call(FlMethodCodec *self, const gchar *name, FlValue *args, GError **error)
Definition: fl_method_codec.cc:16
fl_value_set_string_take
G_MODULE_EXPORT void fl_value_set_string_take(FlValue *self, const gchar *key, FlValue *value)
Definition: fl_value.cc:650
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
fl_value_new_null
G_MODULE_EXPORT FlValue * fl_value_new_null()
Definition: fl_value.cc:251
fl_platform_handler_new
FlPlatformHandler * fl_platform_handler_new(FlBinaryMessenger *messenger)
Definition: fl_platform_handler.cc:405
fl_method_response_get_result
G_MODULE_EXPORT FlValue * fl_method_response_get_result(FlMethodResponse *self, GError **error)
Definition: fl_method_response.cc:82
fl_test_application_new
FlTestApplication * fl_test_application_new(gboolean *dispose_called)
Definition: fl_platform_handler_test.cc:157
fl_value_new_map
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:366
fl_test_application_activate
static void fl_test_application_activate(GApplication *application)
Definition: fl_platform_handler_test.cc:116
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
fl_method_codec_decode_response
FlMethodResponse * fl_method_codec_decode_response(FlMethodCodec *self, GBytes *message, GError **error)
Definition: fl_method_codec.cc:62
fl_test_application_dispose
static void fl_test_application_dispose(GObject *object)
Definition: fl_platform_handler_test.cc:141
fl_value_equal
G_MODULE_EXPORT bool fl_value_equal(FlValue *a, FlValue *b)
Definition: fl_value.cc:471
result
GAsyncResult * result
Definition: fl_text_input_handler.cc:106
MethodCallMatcher
Definition: fl_platform_handler_test.cc:31
MethodCall
static ::testing::Matcher< GBytes * > MethodCall(const std::string &name, ::testing::Matcher< FlValue * > args)
Definition: fl_platform_handler_test.cc:82
args
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
Definition: fl_event_channel.h:89
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
fl_binary_messenger_send_response
G_MODULE_EXPORT gboolean fl_binary_messenger_send_response(FlBinaryMessenger *self, FlBinaryMessengerResponseHandle *response_handle, GBytes *response, GError **error)
Definition: fl_binary_messenger.cc:430
value
uint8_t value
Definition: fl_standard_message_codec.cc:36
fl_binary_messenger_send_on_channel
G_MODULE_EXPORT void fl_binary_messenger_send_on_channel(FlBinaryMessenger *self, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
Definition: fl_binary_messenger.cc:443
fl_value_new_string
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276