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