| Flutter Linux Embedder |

fl_settings_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 "flutter/shell/platform/linux/fl_settings_handler.h"
6 #include "flutter/shell/platform/embedder/embedder.h"
7 #include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
8 #include "flutter/shell/platform/linux/fl_binary_messenger_private.h"
9 #include "flutter/shell/platform/linux/fl_engine_private.h"
10 #include "flutter/shell/platform/linux/testing/fl_mock_binary_messenger.h"
11 #include "flutter/shell/platform/linux/testing/mock_settings.h"
12 #include "flutter/testing/testing.h"
13
14 #include "gmock/gmock.h"
15 #include "gtest/gtest.h"
16
17 TEST(FlSettingsHandlerTest, AlwaysUse24HourFormat) {
18 ::testing::NiceMock<flutter::testing::MockSettings> settings;
19
20 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
21 g_autoptr(FlEngine) engine =
22 FL_ENGINE(g_object_new(fl_engine_get_type(), "binary-messenger",
23 FL_BINARY_MESSENGER(messenger), nullptr));
24 g_autoptr(FlSettingsHandler) handler = fl_settings_handler_new(engine);
25
26 EXPECT_CALL(settings, fl_settings_get_clock_format(
27 ::testing::Eq<FlSettings*>(settings)))
28 .WillOnce(::testing::Return(FL_CLOCK_FORMAT_12H))
29 .WillOnce(::testing::Return(FL_CLOCK_FORMAT_24H));
30
31 gboolean called = FALSE;
32 fl_mock_binary_messenger_set_json_message_channel(
33 messenger, "flutter/settings",
34 [](FlMockBinaryMessenger* messenger, GTask* task, FlValue* message,
35 gpointer user_data) {
36 gboolean* called = static_cast<gboolean*>(user_data);
37 *called = TRUE;
38
39 EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_MAP);
40 FlValue* value =
41 fl_value_lookup_string(message, "alwaysUse24HourFormat");
42 EXPECT_NE(value, nullptr);
43 EXPECT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_BOOL);
44 EXPECT_FALSE(fl_value_get_bool(value));
45
46 return fl_value_new_null();
47 },
48 &called);
49 fl_settings_handler_start(handler, settings);
50 EXPECT_TRUE(called);
51
52 called = FALSE;
53 fl_mock_binary_messenger_set_json_message_channel(
54 messenger, "flutter/settings",
55 [](FlMockBinaryMessenger* messenger, GTask* task, FlValue* message,
56 gpointer user_data) {
57 gboolean* called = static_cast<gboolean*>(user_data);
58 *called = TRUE;
59
60 EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_MAP);
61 FlValue* value =
62 fl_value_lookup_string(message, "alwaysUse24HourFormat");
63 EXPECT_NE(value, nullptr);
64 EXPECT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_BOOL);
65 EXPECT_TRUE(fl_value_get_bool(value));
66
67 return fl_value_new_null();
68 },
69 &called);
70 fl_settings_emit_changed(settings);
71 EXPECT_TRUE(called);
72
73 fl_binary_messenger_shutdown(FL_BINARY_MESSENGER(messenger));
74 }
75
76 TEST(FlSettingsHandlerTest, PlatformBrightness) {
77 ::testing::NiceMock<flutter::testing::MockSettings> settings;
78
79 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
80 g_autoptr(FlEngine) engine =
81 FL_ENGINE(g_object_new(fl_engine_get_type(), "binary-messenger",
82 FL_BINARY_MESSENGER(messenger), nullptr));
83 g_autoptr(FlSettingsHandler) handler = fl_settings_handler_new(engine);
84
85 EXPECT_CALL(settings, fl_settings_get_color_scheme(
86 ::testing::Eq<FlSettings*>(settings)))
87 .WillOnce(::testing::Return(FL_COLOR_SCHEME_LIGHT))
88 .WillOnce(::testing::Return(FL_COLOR_SCHEME_DARK));
89
90 gboolean called = FALSE;
91 fl_mock_binary_messenger_set_json_message_channel(
92 messenger, "flutter/settings",
93 [](FlMockBinaryMessenger* messenger, GTask* task, FlValue* message,
94 gpointer user_data) {
95 gboolean* called = static_cast<gboolean*>(user_data);
96 *called = TRUE;
97
98 EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_MAP);
99 FlValue* value = fl_value_lookup_string(message, "platformBrightness");
100 EXPECT_NE(value, nullptr);
101 EXPECT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
102 EXPECT_STREQ(fl_value_get_string(value), "light");
103
104 return fl_value_new_null();
105 },
106 &called);
107 fl_settings_handler_start(handler, settings);
108 EXPECT_TRUE(called);
109
110 called = FALSE;
111 fl_mock_binary_messenger_set_json_message_channel(
112 messenger, "flutter/settings",
113 [](FlMockBinaryMessenger* messenger, GTask* task, FlValue* message,
114 gpointer user_data) {
115 gboolean* called = static_cast<gboolean*>(user_data);
116 *called = TRUE;
117
118 EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_MAP);
119 FlValue* value = fl_value_lookup_string(message, "platformBrightness");
120 EXPECT_NE(value, nullptr);
121 EXPECT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
122 EXPECT_STREQ(fl_value_get_string(value), "dark");
123
124 return fl_value_new_null();
125 },
126 &called);
127 fl_settings_emit_changed(settings);
128 EXPECT_TRUE(called);
129
130 fl_binary_messenger_shutdown(FL_BINARY_MESSENGER(messenger));
131 }
132
133 TEST(FlSettingsHandlerTest, TextScaleFactor) {
134 ::testing::NiceMock<flutter::testing::MockSettings> settings;
135
136 g_autoptr(FlMockBinaryMessenger) messenger = fl_mock_binary_messenger_new();
137 g_autoptr(FlEngine) engine =
138 FL_ENGINE(g_object_new(fl_engine_get_type(), "binary-messenger",
139 FL_BINARY_MESSENGER(messenger), nullptr));
140 g_autoptr(FlSettingsHandler) handler = fl_settings_handler_new(engine);
141
142 EXPECT_CALL(settings, fl_settings_get_text_scaling_factor(
143 ::testing::Eq<FlSettings*>(settings)))
144 .WillOnce(::testing::Return(1.0))
145 .WillOnce(::testing::Return(2.0));
146
147 gboolean called = FALSE;
148 fl_mock_binary_messenger_set_json_message_channel(
149 messenger, "flutter/settings",
150 [](FlMockBinaryMessenger* messenger, GTask* task, FlValue* message,
151 gpointer user_data) {
152 gboolean* called = static_cast<gboolean*>(user_data);
153 *called = TRUE;
154
155 EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_MAP);
156 FlValue* value = fl_value_lookup_string(message, "textScaleFactor");
157 EXPECT_NE(value, nullptr);
158 EXPECT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT);
159 EXPECT_EQ(fl_value_get_float(value), 1.0);
160
161 return fl_value_new_null();
162 },
163 &called);
164 fl_settings_handler_start(handler, settings);
165 EXPECT_TRUE(called);
166
167 called = FALSE;
168 fl_mock_binary_messenger_set_json_message_channel(
169 messenger, "flutter/settings",
170 [](FlMockBinaryMessenger* messenger, GTask* task, FlValue* message,
171 gpointer user_data) {
172 gboolean* called = static_cast<gboolean*>(user_data);
173 *called = TRUE;
174
175 EXPECT_EQ(fl_value_get_type(message), FL_VALUE_TYPE_MAP);
176 FlValue* value = fl_value_lookup_string(message, "textScaleFactor");
177 EXPECT_NE(value, nullptr);
178 EXPECT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT);
179 EXPECT_EQ(fl_value_get_float(value), 2.0);
180
181 return fl_value_new_null();
182 },
183 &called);
184 fl_settings_emit_changed(settings);
185 EXPECT_TRUE(called);
186
187 fl_binary_messenger_shutdown(FL_BINARY_MESSENGER(messenger));
188 }
189
190 // MOCK_ENGINE_PROC is leaky by design
191 // NOLINTBEGIN(clang-analyzer-core.StackAddressEscape)
192 TEST(FlSettingsHandlerTest, AccessibilityFeatures) {
193 g_autoptr(FlDartProject) project = fl_dart_project_new();
194 g_autoptr(FlEngine) engine = fl_engine_new(project);
195
196 g_autoptr(GError) error = nullptr;
197 EXPECT_TRUE(fl_engine_start(engine, &error));
198 EXPECT_EQ(error, nullptr);
199
200 std::vector<FlutterAccessibilityFeature> calls;
201 fl_engine_get_embedder_api(engine)->UpdateAccessibilityFeatures =
202 MOCK_ENGINE_PROC(
203 UpdateAccessibilityFeatures,
204 ([&calls](auto engine, FlutterAccessibilityFeature features) {
205 calls.push_back(features);
206 return kSuccess;
207 }));
208
209 g_autoptr(FlSettingsHandler) handler = fl_settings_handler_new(engine);
210
211 ::testing::NiceMock<flutter::testing::MockSettings> settings;
212
213 EXPECT_CALL(settings, fl_settings_get_enable_animations(
214 ::testing::Eq<FlSettings*>(settings)))
215 .WillOnce(::testing::Return(false))
216 .WillOnce(::testing::Return(true))
217 .WillOnce(::testing::Return(false))
218 .WillOnce(::testing::Return(true));
219
220 EXPECT_CALL(settings, fl_settings_get_high_contrast(
221 ::testing::Eq<FlSettings*>(settings)))
222 .WillOnce(::testing::Return(true))
223 .WillOnce(::testing::Return(false))
224 .WillOnce(::testing::Return(false))
225 .WillOnce(::testing::Return(true));
226
227 fl_settings_handler_start(handler, settings);
228 EXPECT_THAT(calls, ::testing::SizeIs(1));
229 EXPECT_EQ(calls.back(), static_cast<FlutterAccessibilityFeature>(
230 kFlutterAccessibilityFeatureDisableAnimations |
231 kFlutterAccessibilityFeatureHighContrast));
232
233 fl_settings_emit_changed(settings);
234 EXPECT_THAT(calls, ::testing::SizeIs(2));
235 EXPECT_EQ(calls.back(), static_cast<FlutterAccessibilityFeature>(0));
236
237 fl_settings_emit_changed(settings);
238 EXPECT_THAT(calls, ::testing::SizeIs(3));
239 EXPECT_EQ(calls.back(), static_cast<FlutterAccessibilityFeature>(
240 kFlutterAccessibilityFeatureDisableAnimations));
241
242 fl_settings_emit_changed(settings);
243 EXPECT_THAT(calls, ::testing::SizeIs(4));
244 EXPECT_EQ(calls.back(), static_cast<FlutterAccessibilityFeature>(
245 kFlutterAccessibilityFeatureHighContrast));
246 }
247 // NOLINTEND(clang-analyzer-core.StackAddressEscape)
fl_binary_messenger_shutdown
void fl_binary_messenger_shutdown(FlBinaryMessenger *self)
Definition: fl_binary_messenger.cc:500
fl_settings_handler_new
FlSettingsHandler * fl_settings_handler_new(FlEngine *engine)
Definition: fl_settings_handler.cc:74
FL_VALUE_TYPE_MAP
@ FL_VALUE_TYPE_MAP
Definition: fl_value.h:74
fl_engine_get_embedder_api
FlutterEngineProcTable * fl_engine_get_embedder_api(FlEngine *self)
Definition: fl_engine.cc:661
fl_settings_get_color_scheme
FlColorScheme fl_settings_get_color_scheme(FlSettings *self)
Definition: fl_settings.cc:34
FL_CLOCK_FORMAT_12H
@ FL_CLOCK_FORMAT_12H
Definition: fl_settings.h:22
FL_CLOCK_FORMAT_24H
@ FL_CLOCK_FORMAT_24H
Definition: fl_settings.h:23
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
fl_settings_get_enable_animations
gboolean fl_settings_get_enable_animations(FlSettings *self)
Definition: fl_settings.cc:38
fl_value_new_null
G_MODULE_EXPORT FlValue * fl_value_new_null()
Definition: fl_value.cc:251
fl_value_get_bool
G_MODULE_EXPORT bool fl_value_get_bool(FlValue *self)
Definition: fl_value.cc:661
fl_dart_project_new
G_MODULE_EXPORT FlDartProject * fl_dart_project_new()
Definition: fl_dart_project.cc:50
fl_value_lookup_string
G_MODULE_EXPORT FlValue * fl_value_lookup_string(FlValue *self, const gchar *key)
Definition: fl_value.cc:811
fl_value_get_string
const G_MODULE_EXPORT gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:682
user_data
G_BEGIN_DECLS G_MODULE_EXPORT FlValue gpointer user_data
Definition: fl_event_channel.h:90
fl_value_get_type
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:466
fl_settings_get_high_contrast
gboolean fl_settings_get_high_contrast(FlSettings *self)
Definition: fl_settings.cc:42
FL_VALUE_TYPE_STRING
@ FL_VALUE_TYPE_STRING
Definition: fl_value.h:68
fl_settings_emit_changed
void fl_settings_emit_changed(FlSettings *self)
Definition: fl_settings.cc:50
fl_settings_handler.h
fl_engine_private.h
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
fl_value_get_float
G_MODULE_EXPORT double fl_value_get_float(FlValue *self)
Definition: fl_value.cc:675
fl_settings_get_text_scaling_factor
gdouble fl_settings_get_text_scaling_factor(FlSettings *self)
Definition: fl_settings.cc:46
fl_settings_handler_start
void fl_settings_handler_start(FlSettingsHandler *self, FlSettings *settings)
Definition: fl_settings_handler.cc:88
fl_binary_messenger_private.h
fl_engine_new
G_MODULE_EXPORT FlEngine * fl_engine_new(FlDartProject *project)
Definition: fl_engine.cc:524
FL_COLOR_SCHEME_LIGHT
@ FL_COLOR_SCHEME_LIGHT
Definition: fl_settings.h:34
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
TEST
TEST(FlSettingsHandlerTest, AlwaysUse24HourFormat)
Definition: fl_settings_handler_test.cc:17
FL_COLOR_SCHEME_DARK
@ FL_COLOR_SCHEME_DARK
Definition: fl_settings.h:35
fl_engine_start
gboolean fl_engine_start(FlEngine *self, GError **error)
Definition: fl_engine.cc:544
FL_VALUE_TYPE_FLOAT
@ FL_VALUE_TYPE_FLOAT
Definition: fl_value.h:67
FL_VALUE_TYPE_BOOL
@ FL_VALUE_TYPE_BOOL
Definition: fl_value.h:65
value
uint8_t value
Definition: fl_standard_message_codec.cc:36
fl_settings_get_clock_format
FlClockFormat fl_settings_get_clock_format(FlSettings *self)
Definition: fl_settings.cc:30