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_platform_handler.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_method_codec.h"
#include "flutter/shell/platform/linux/testing/fl_mock_binary_messenger.h"
#include "flutter/shell/platform/linux/testing/fl_test.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

Go to the source code of this file.

Functions

 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 40 of file fl_platform_handler_test.cc.

40  {
41  G_APPLICATION_CLASS(fl_test_application_parent_class)->activate(application);
42 
43  g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
44  g_autoptr(FlPlatformHandler) handler =
45  fl_platform_handler_new(FL_BINARY_MESSENGER(messenger));
46  EXPECT_NE(handler, nullptr);
47 
48  // Request app exit.
49  gboolean called = FALSE;
50  g_autoptr(FlValue) args = fl_value_new_map();
52  fl_mock_binary_messenger_invoke_json_method(
53  messenger, "flutter/platform", "System.exitApplication", args,
54  [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
55  gpointer user_data) {
56  gboolean* called = static_cast<gboolean*>(user_data);
57  *called = TRUE;
58 
59  EXPECT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
60 
61  g_autoptr(FlValue) expected_result = fl_value_new_map();
62  fl_value_set_string_take(expected_result, "response",
63  fl_value_new_string("exit"));
65  FL_METHOD_SUCCESS_RESPONSE(response)),
66  expected_result));
67  },
68  &called);
69  EXPECT_TRUE(called);
70 
71  fl_binary_messenger_shutdown(FL_BINARY_MESSENGER(messenger));
72 }

References args, fl_binary_messenger_shutdown(), fl_method_success_response_get_result(), fl_platform_handler_new(), fl_value_equal(), fl_value_new_map(), fl_value_new_string(), fl_value_set_string_take(), TRUE, and user_data.

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 82 of file fl_platform_handler_test.cc.

82  {
83  G_OBJECT_CLASS(klass)->dispose = fl_test_application_dispose;
84  G_APPLICATION_CLASS(klass)->startup = fl_test_application_startup;
85  G_APPLICATION_CLASS(klass)->activate = fl_test_application_activate;
86 }

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 74 of file fl_platform_handler_test.cc.

74  {
75  FlTestApplication* self = FL_TEST_APPLICATION(object);
76 
77  *self->dispose_called = true;
78 
79  G_OBJECT_CLASS(fl_test_application_parent_class)->dispose(object);
80 }

Referenced by fl_test_application_class_init().

◆ fl_test_application_init()

static void fl_test_application_init ( FlTestApplication *  self)
static

Definition at line 88 of file fl_platform_handler_test.cc.

88 {}

◆ fl_test_application_new()

FlTestApplication* fl_test_application_new ( gboolean *  dispose_called)

Definition at line 90 of file fl_platform_handler_test.cc.

90  {
91  FlTestApplication* self = FL_TEST_APPLICATION(
92  g_object_new(fl_test_application_get_type(), nullptr));
93 
94  // Don't try and register on D-Bus.
95  g_application_set_application_id(G_APPLICATION(self), "dev.flutter.GtkTest");
96  g_application_set_flags(G_APPLICATION(self), G_APPLICATION_NON_UNIQUE);
97 
98  // Added to stop compiler complaining about an unused function.
99  FL_IS_TEST_APPLICATION(self);
100 
101  self->dispose_called = dispose_called;
102 
103  return self;
104 }

Referenced by TEST().

◆ G_DECLARE_FINAL_TYPE()

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

Definition at line 16 of file fl_platform_handler_test.cc.

22  {
23  GtkApplication parent_instance;
24  gboolean* dispose_called;
25 };

◆ G_DEFINE_TYPE()

G_DEFINE_TYPE ( FlTestApplication  ,
fl_test_application  ,
gtk_application_get_type()   
)

Definition at line 27 of file fl_platform_handler_test.cc.

31  {
32  G_APPLICATION_CLASS(fl_test_application_parent_class)->startup(application);
33 
34  // Add a window to this application, which will hold a reference to the
35  // application and stop it disposing. See
36  // https://gitlab.gnome.org/GNOME/gtk/-/issues/6190
37  gtk_application_window_new(GTK_APPLICATION(application));
38 }

◆ TEST() [1/3]

TEST ( FlPlatformHandlerTest  ,
ExitApplication   
)

Definition at line 134 of file fl_platform_handler_test.cc.

134  {
135  g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
136 
137  g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
138  g_autoptr(FlPlatformHandler) handler =
139  fl_platform_handler_new(FL_BINARY_MESSENGER(messenger));
140  EXPECT_NE(handler, nullptr);
141 
142  // Indicate that the binding is initialized.
143  gboolean called = FALSE;
144  fl_mock_binary_messenger_invoke_json_method(
145  messenger, "flutter/platform", "System.initializationComplete", nullptr,
146  [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
147  gpointer user_data) {
148  gboolean* called = static_cast<gboolean*>(user_data);
149  *called = TRUE;
150 
151  EXPECT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
152 
153  g_autoptr(FlValue) expected_result = fl_value_new_null();
155  FL_METHOD_SUCCESS_RESPONSE(response)),
156  expected_result));
157  },
158  &called);
159  EXPECT_TRUE(called);
160 
161  gboolean request_exit_called = FALSE;
162  fl_mock_binary_messenger_set_json_method_channel(
163  messenger, "flutter/platform",
164  [](FlMockBinaryMessenger* messenger, GTask* task, const gchar* name,
165  FlValue* args, gpointer user_data) {
166  gboolean* called = static_cast<gboolean*>(user_data);
167  *called = TRUE;
168 
169  EXPECT_STREQ(name, "System.requestAppExit");
170 
171  g_autoptr(FlValue) expected_args = fl_value_new_map();
172  fl_value_set_string_take(expected_args, "type",
173  fl_value_new_string("cancelable"));
174  EXPECT_TRUE(fl_value_equal(args, expected_args));
175 
176  // Cancel so it doesn't try and exit this app (i.e. the current test)
177  g_autoptr(FlValue) result = fl_value_new_map();
178  fl_value_set_string_take(result, "response",
179  fl_value_new_string("cancel"));
180  return FL_METHOD_RESPONSE(fl_method_success_response_new(result));
181  },
182  &request_exit_called);
183 
184  g_autoptr(FlValue) args = fl_value_new_map();
185  fl_value_set_string_take(args, "type", fl_value_new_string("cancelable"));
186  fl_mock_binary_messenger_invoke_json_method(
187  messenger, "flutter/platform", "System.exitApplication", args,
188  [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
189  gpointer user_data) {
190  EXPECT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
191 
192  g_autoptr(FlValue) expected_result = fl_value_new_map();
193  fl_value_set_string_take(expected_result, "response",
194  fl_value_new_string("cancel"));
196  FL_METHOD_SUCCESS_RESPONSE(response)),
197  expected_result));
198 
199  g_main_loop_quit(static_cast<GMainLoop*>(user_data));
200  },
201  loop);
202 
203  g_main_loop_run(loop);
204 
205  EXPECT_TRUE(request_exit_called);
206 
207  fl_binary_messenger_shutdown(FL_BINARY_MESSENGER(messenger));
208 }

References args, fl_binary_messenger_shutdown(), fl_method_success_response_get_result(), fl_method_success_response_new(), fl_platform_handler_new(), fl_value_equal(), fl_value_new_map(), fl_value_new_null(), fl_value_new_string(), fl_value_set_string_take(), TRUE, and user_data.

◆ TEST() [2/3]

TEST ( FlPlatformHandlerTest  ,
ExitApplicationDispose   
)

Definition at line 210 of file fl_platform_handler_test.cc.

210  {
211  gtk_init(0, nullptr);
212 
213  gboolean dispose_called = false;
214  FlTestApplication* application = fl_test_application_new(&dispose_called);
215 
216  // Run the application, it will quit after startup.
217  g_application_run(G_APPLICATION(application), 0, nullptr);
218 
219  EXPECT_FALSE(dispose_called);
220  g_object_unref(application);
221  EXPECT_TRUE(dispose_called);
222 }

References fl_test_application_new().

◆ TEST() [3/3]

TEST ( FlPlatformHandlerTest  ,
PlaySound   
)

Definition at line 106 of file fl_platform_handler_test.cc.

106  {
107  g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
108  g_autoptr(FlPlatformHandler) handler =
109  fl_platform_handler_new(FL_BINARY_MESSENGER(messenger));
110  EXPECT_NE(handler, nullptr);
111 
112  gboolean called = FALSE;
113  g_autoptr(FlValue) args = fl_value_new_string("SystemSoundType.alert");
114  fl_mock_binary_messenger_invoke_json_method(
115  messenger, "flutter/platform", "SystemSound.play", args,
116  [](FlMockBinaryMessenger* messenger, FlMethodResponse* response,
117  gpointer user_data) {
118  gboolean* called = static_cast<gboolean*>(user_data);
119  *called = TRUE;
120 
121  EXPECT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
122 
123  g_autoptr(FlValue) expected_result = fl_value_new_null();
125  FL_METHOD_SUCCESS_RESPONSE(response)),
126  expected_result));
127  },
128  &called);
129  EXPECT_TRUE(called);
130 
131  fl_binary_messenger_shutdown(FL_BINARY_MESSENGER(messenger));
132 }

References args, fl_binary_messenger_shutdown(), fl_method_success_response_get_result(), fl_platform_handler_new(), fl_value_equal(), fl_value_new_null(), fl_value_new_string(), TRUE, and user_data.

fl_binary_messenger_shutdown
void fl_binary_messenger_shutdown(FlBinaryMessenger *self)
Definition: fl_binary_messenger.cc:500
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:251
fl_test_application_new
FlTestApplication * fl_test_application_new(gboolean *dispose_called)
Definition: fl_platform_handler_test.cc:90
fl_method_success_response_new
G_MODULE_EXPORT FlMethodSuccessResponse * fl_method_success_response_new(FlValue *result)
Definition: fl_method_response.cc:126
user_data
G_BEGIN_DECLS G_MODULE_EXPORT FlValue gpointer user_data
Definition: fl_event_channel.h:90
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:40
fl_method_success_response_get_result
G_MODULE_EXPORT FlValue * fl_method_success_response_get_result(FlMethodSuccessResponse *self)
Definition: fl_method_response.cc:138
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
fl_test_application_dispose
static void fl_test_application_dispose(GObject *object)
Definition: fl_platform_handler_test.cc:74
fl_value_equal
G_MODULE_EXPORT bool fl_value_equal(FlValue *a, FlValue *b)
Definition: fl_value.cc:471
args
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
Definition: fl_event_channel.h:89
fl_value_new_string
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276