| Flutter Linux Embedder |

fl_platform_handler_test.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 <gtk/gtk.h>
6
7 #include "flutter/shell/platform/linux/fl_binary_messenger_private.h"
8 #include "flutter/shell/platform/linux/fl_platform_handler.h"
9 #include "flutter/shell/platform/linux/public/flutter_linux/fl_method_codec.h"
10 #include "flutter/shell/platform/linux/testing/fl_mock_binary_messenger.h"
11 #include "flutter/shell/platform/linux/testing/fl_test.h"
12
13 #include "gmock/gmock.h"
14 #include "gtest/gtest.h"
15
16 G_DECLARE_FINAL_TYPE(FlTestApplication,
17 fl_test_application,
18 FL,
19 TEST_APPLICATION,
20 GtkApplication)
21
22 struct _FlTestApplication {
23 GtkApplication parent_instance;
24 gboolean* dispose_called;
25 };
26
27 G_DEFINE_TYPE(FlTestApplication,
28 fl_test_application,
29 gtk_application_get_type())
30
31 static void fl_test_application_startup(GApplication* application) {
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 }
39
40 static void fl_test_application_activate(GApplication* application) {
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();
51 fl_value_set_string_take(args, "type", fl_value_new_string("required"));
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"));
64 EXPECT_TRUE(fl_value_equal(fl_method_success_response_get_result(
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 }
73
74 static void fl_test_application_dispose(GObject* object) {
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 }
81
82 static void fl_test_application_class_init(FlTestApplicationClass* klass) {
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 }
87
88 static void fl_test_application_init(FlTestApplication* self) {}
89
90 FlTestApplication* fl_test_application_new(gboolean* dispose_called) {
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 }
105
106 TEST(FlPlatformHandlerTest, PlaySound) {
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();
124 EXPECT_TRUE(fl_value_equal(fl_method_success_response_get_result(
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 }
133
134 TEST(FlPlatformHandlerTest, ExitApplication) {
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();
154 EXPECT_TRUE(fl_value_equal(fl_method_success_response_get_result(
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"));
195 EXPECT_TRUE(fl_value_equal(fl_method_success_response_get_result(
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 }
209
210 TEST(FlPlatformHandlerTest, ExitApplicationDispose) {
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 }
fl_binary_messenger_shutdown
void fl_binary_messenger_shutdown(FlBinaryMessenger *self)
Definition: fl_binary_messenger.cc:500
fl_test_application_class_init
static void fl_test_application_class_init(FlTestApplicationClass *klass)
Definition: fl_platform_handler_test.cc:82
G_DECLARE_FINAL_TYPE
G_DECLARE_FINAL_TYPE(FlTestApplication, fl_test_application, FL, TEST_APPLICATION, GtkApplication) struct _FlTestApplication
Definition: fl_platform_handler_test.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:251
fl_test_application_new
FlTestApplication * fl_test_application_new(gboolean *dispose_called)
Definition: fl_platform_handler_test.cc:90
G_DEFINE_TYPE
G_DEFINE_TYPE(FlTestApplication, fl_test_application, gtk_application_get_type()) static void fl_test_application_startup(GApplication *application)
Definition: fl_platform_handler_test.cc:27
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_test_application_init
static void fl_test_application_init(FlTestApplication *self)
Definition: fl_platform_handler_test.cc:88
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_platform_handler.h
fl_method_codec.h
FL
FL
Definition: fl_binary_messenger.cc:27
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
fl_binary_messenger_private.h
args
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
Definition: fl_event_channel.h:89
TEST
TEST(FlPlatformHandlerTest, PlaySound)
Definition: fl_platform_handler_test.cc:106
fl_value_new_string
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276