Flutter Engine
 
Loading...
Searching...
No Matches
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
12
13#include "gmock/gmock.h"
14#include "gtest/gtest.h"
15
16G_DECLARE_FINAL_TYPE(FlTestApplication,
17 fl_test_application,
18 FL,
19 TEST_APPLICATION,
20 GtkApplication)
21
22struct _FlTestApplication {
23 GtkApplication parent_instance;
24 gboolean* dispose_called;
25};
26
27G_DEFINE_TYPE(FlTestApplication,
28 fl_test_application,
29 gtk_application_get_type())
30
31static 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
40static 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;
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}
73
74static 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
82static 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
88static void fl_test_application_init(FlTestApplication* self) {}
89
90FlTestApplication* 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
106TEST(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");
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}
133
134TEST(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;
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;
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)
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
185 fl_value_set_string_take(args, "type", fl_value_new_string("cancelable"));
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}
209
210TEST(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}
void fl_binary_messenger_shutdown(FlBinaryMessenger *self)
g_autoptr(GMutexLocker) locker
return TRUE
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
G_MODULE_EXPORT FlValue * fl_method_success_response_get_result(FlMethodSuccessResponse *self)
G_MODULE_EXPORT FlMethodSuccessResponse * fl_method_success_response_new(FlValue *result)
void fl_mock_binary_messenger_invoke_json_method(FlMockBinaryMessenger *self, const gchar *channel, const char *name, FlValue *args, FlMockBinaryMessengerMethodCallback callback, gpointer user_data)
const gchar FlBinaryMessengerMessageHandler handler
FlMockBinaryMessenger * fl_mock_binary_messenger_new()
void fl_mock_binary_messenger_set_json_method_channel(FlMockBinaryMessenger *self, const gchar *channel, FlMockBinaryMessengerMethodChannelHandler handler, gpointer user_data)
FlPlatformHandler * fl_platform_handler_new(FlBinaryMessenger *messenger)
static void fl_test_application_activate(GApplication *application)
static void fl_test_application_dispose(GObject *object)
FlTestApplication * fl_test_application_new(gboolean *dispose_called)
TEST(FlPlatformHandlerTest, PlaySound)
static void fl_test_application_init(FlTestApplication *self)
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_class_init(FlTestApplicationClass *klass)
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition fl_value.cc:366
G_MODULE_EXPORT void fl_value_set_string_take(FlValue *self, const gchar *key, FlValue *value)
Definition fl_value.cc:650
G_MODULE_EXPORT FlValue * fl_value_new_null()
Definition fl_value.cc:251
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition fl_value.cc:276
G_MODULE_EXPORT bool fl_value_equal(FlValue *a, FlValue *b)
Definition fl_value.cc:471
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition fl_value.h:42
const char * name
Definition fuchsia.cc:49