| 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_engine_private.h"
9 #include "flutter/shell/platform/linux/public/flutter_linux/fl_binary_messenger.h"
10 #include "flutter/shell/platform/linux/public/flutter_linux/fl_json_message_codec.h"
11 #include "flutter/shell/platform/linux/public/flutter_linux/fl_value.h"
12 #include "flutter/shell/platform/linux/testing/fl_test.h"
13 #include "flutter/shell/platform/linux/testing/mock_binary_messenger.h"
14 #include "flutter/shell/platform/linux/testing/mock_settings.h"
15 #include "flutter/testing/testing.h"
16
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
19
20 MATCHER_P2(HasSetting, key, value, "") {
21 g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new();
22 g_autoptr(FlValue) message =
23 fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), arg, nullptr);
24 if (fl_value_equal(fl_value_lookup_string(message, key), value)) {
25 return true;
26 }
27 *result_listener << ::testing::PrintToString(message);
28 return false;
29 }
30
31 #define EXPECT_SETTING(messenger, key, value) \
32 EXPECT_CALL( \
33 messenger, \
34 fl_binary_messenger_send_on_channel( \
35 ::testing::Eq<FlBinaryMessenger*>(messenger), \
36 ::testing::StrEq("flutter/settings"), HasSetting(key, value), \
37 ::testing::A<GCancellable*>(), ::testing::A<GAsyncReadyCallback>(), \
38 ::testing::A<gpointer>()))
39
40 TEST(FlSettingsHandlerTest, AlwaysUse24HourFormat) {
41 ::testing::NiceMock<flutter::testing::MockSettings> settings;
42 ::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
43
44 g_autoptr(FlEngine) engine =
45 FL_ENGINE(g_object_new(fl_engine_get_type(), "binary-messenger",
46 FL_BINARY_MESSENGER(messenger), nullptr));
47 g_autoptr(FlSettingsHandler) handler = fl_settings_handler_new(engine);
48
49 g_autoptr(FlValue) use_12h = fl_value_new_bool(false);
50 g_autoptr(FlValue) use_24h = fl_value_new_bool(true);
51
52 EXPECT_CALL(settings, fl_settings_get_clock_format(
53 ::testing::Eq<FlSettings*>(settings)))
54 .WillOnce(::testing::Return(FL_CLOCK_FORMAT_12H))
55 .WillOnce(::testing::Return(FL_CLOCK_FORMAT_24H));
56
57 EXPECT_SETTING(messenger, "alwaysUse24HourFormat", use_12h);
58
59 fl_settings_handler_start(handler, settings);
60
61 EXPECT_SETTING(messenger, "alwaysUse24HourFormat", use_24h);
62
63 fl_settings_emit_changed(settings);
64 }
65
66 TEST(FlSettingsHandlerTest, PlatformBrightness) {
67 ::testing::NiceMock<flutter::testing::MockSettings> settings;
68 ::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
69
70 g_autoptr(FlEngine) engine =
71 FL_ENGINE(g_object_new(fl_engine_get_type(), "binary-messenger",
72 FL_BINARY_MESSENGER(messenger), nullptr));
73 g_autoptr(FlSettingsHandler) handler = fl_settings_handler_new(engine);
74
75 g_autoptr(FlValue) light = fl_value_new_string("light");
76 g_autoptr(FlValue) dark = fl_value_new_string("dark");
77
78 EXPECT_CALL(settings, fl_settings_get_color_scheme(
79 ::testing::Eq<FlSettings*>(settings)))
80 .WillOnce(::testing::Return(FL_COLOR_SCHEME_LIGHT))
81 .WillOnce(::testing::Return(FL_COLOR_SCHEME_DARK));
82
83 EXPECT_SETTING(messenger, "platformBrightness", light);
84
85 fl_settings_handler_start(handler, settings);
86
87 EXPECT_SETTING(messenger, "platformBrightness", dark);
88
89 fl_settings_emit_changed(settings);
90 }
91
92 TEST(FlSettingsHandlerTest, TextScaleFactor) {
93 ::testing::NiceMock<flutter::testing::MockSettings> settings;
94 ::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
95
96 g_autoptr(FlEngine) engine =
97 FL_ENGINE(g_object_new(fl_engine_get_type(), "binary-messenger",
98 FL_BINARY_MESSENGER(messenger), nullptr));
99 g_autoptr(FlSettingsHandler) handler = fl_settings_handler_new(engine);
100
101 g_autoptr(FlValue) one = fl_value_new_float(1.0);
102 g_autoptr(FlValue) two = fl_value_new_float(2.0);
103
104 EXPECT_CALL(settings, fl_settings_get_text_scaling_factor(
105 ::testing::Eq<FlSettings*>(settings)))
106 .WillOnce(::testing::Return(1.0))
107 .WillOnce(::testing::Return(2.0));
108
109 EXPECT_SETTING(messenger, "textScaleFactor", one);
110
111 fl_settings_handler_start(handler, settings);
112
113 EXPECT_SETTING(messenger, "textScaleFactor", two);
114
115 fl_settings_emit_changed(settings);
116 }
117
118 // MOCK_ENGINE_PROC is leaky by design
119 // NOLINTBEGIN(clang-analyzer-core.StackAddressEscape)
120 TEST(FlSettingsHandlerTest, AccessibilityFeatures) {
121 g_autoptr(FlEngine) engine = make_mock_engine();
122 FlutterEngineProcTable* embedder_api = fl_engine_get_embedder_api(engine);
123
124 std::vector<FlutterAccessibilityFeature> calls;
125 embedder_api->UpdateAccessibilityFeatures = MOCK_ENGINE_PROC(
126 UpdateAccessibilityFeatures,
127 ([&calls](auto engine, FlutterAccessibilityFeature features) {
128 calls.push_back(features);
129 return kSuccess;
130 }));
131
132 g_autoptr(FlSettingsHandler) handler = fl_settings_handler_new(engine);
133
134 ::testing::NiceMock<flutter::testing::MockSettings> settings;
135
136 EXPECT_CALL(settings, fl_settings_get_enable_animations(
137 ::testing::Eq<FlSettings*>(settings)))
138 .WillOnce(::testing::Return(false))
139 .WillOnce(::testing::Return(true))
140 .WillOnce(::testing::Return(false))
141 .WillOnce(::testing::Return(true));
142
143 EXPECT_CALL(settings, fl_settings_get_high_contrast(
144 ::testing::Eq<FlSettings*>(settings)))
145 .WillOnce(::testing::Return(true))
146 .WillOnce(::testing::Return(false))
147 .WillOnce(::testing::Return(false))
148 .WillOnce(::testing::Return(true));
149
150 fl_settings_handler_start(handler, settings);
151 EXPECT_THAT(calls, ::testing::SizeIs(1));
152 EXPECT_EQ(calls.back(), static_cast<FlutterAccessibilityFeature>(
153 kFlutterAccessibilityFeatureDisableAnimations |
154 kFlutterAccessibilityFeatureHighContrast));
155
156 fl_settings_emit_changed(settings);
157 EXPECT_THAT(calls, ::testing::SizeIs(2));
158 EXPECT_EQ(calls.back(), static_cast<FlutterAccessibilityFeature>(0));
159
160 fl_settings_emit_changed(settings);
161 EXPECT_THAT(calls, ::testing::SizeIs(3));
162 EXPECT_EQ(calls.back(), static_cast<FlutterAccessibilityFeature>(
163 kFlutterAccessibilityFeatureDisableAnimations));
164
165 fl_settings_emit_changed(settings);
166 EXPECT_THAT(calls, ::testing::SizeIs(4));
167 EXPECT_EQ(calls.back(), static_cast<FlutterAccessibilityFeature>(
168 kFlutterAccessibilityFeatureHighContrast));
169 }
170 // NOLINTEND(clang-analyzer-core.StackAddressEscape)
MATCHER_P2
MATCHER_P2(HasSetting, key, value, "")
Definition: fl_settings_handler_test.cc:20
fl_settings_handler_new
FlSettingsHandler * fl_settings_handler_new(FlEngine *engine)
Definition: fl_settings_handler.cc:94
fl_engine_get_embedder_api
FlutterEngineProcTable * fl_engine_get_embedder_api(FlEngine *self)
Definition: fl_engine.cc:637
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:23
FL_CLOCK_FORMAT_24H
@ FL_CLOCK_FORMAT_24H
Definition: fl_settings.h:24
EXPECT_SETTING
#define EXPECT_SETTING(messenger, key, value)
Definition: fl_settings_handler_test.cc:31
fl_value_new_bool
G_MODULE_EXPORT FlValue * fl_value_new_bool(bool value)
Definition: fl_value.cc:255
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
fl_json_message_codec_new
G_MODULE_EXPORT FlJsonMessageCodec * fl_json_message_codec_new()
Definition: fl_json_message_codec.cc:306
fl_json_message_codec.h
fl_settings_get_enable_animations
gboolean fl_settings_get_enable_animations(FlSettings *self)
Definition: fl_settings.cc:38
fl_value_lookup_string
G_MODULE_EXPORT FlValue * fl_value_lookup_string(FlValue *self, const gchar *key)
Definition: fl_value.cc:811
make_mock_engine
static FlEngine * make_mock_engine()
Definition: fl_event_channel_test.cc:24
fl_message_codec_decode_message
G_MODULE_EXPORT FlValue * fl_message_codec_decode_message(FlMessageCodec *self, GBytes *message, GError **error)
Definition: fl_message_codec.cc:33
fl_settings_get_high_contrast
gboolean fl_settings_get_high_contrast(FlSettings *self)
Definition: fl_settings.cc:42
fl_binary_messenger.h
fl_settings_emit_changed
void fl_settings_emit_changed(FlSettings *self)
Definition: fl_settings.cc:50
fl_settings_handler.h
fl_engine_private.h
fl_settings_get_text_scaling_factor
gdouble fl_settings_get_text_scaling_factor(FlSettings *self)
Definition: fl_settings.cc:46
fl_value_equal
G_MODULE_EXPORT bool fl_value_equal(FlValue *a, FlValue *b)
Definition: fl_value.cc:471
fl_settings_handler_start
void fl_settings_handler_start(FlSettingsHandler *self, FlSettings *settings)
Definition: fl_settings_handler.cc:112
FL_COLOR_SCHEME_LIGHT
@ FL_COLOR_SCHEME_LIGHT
Definition: fl_settings.h:37
fl_value.h
TEST
TEST(FlSettingsHandlerTest, AlwaysUse24HourFormat)
Definition: fl_settings_handler_test.cc:40
FL_COLOR_SCHEME_DARK
@ FL_COLOR_SCHEME_DARK
Definition: fl_settings.h:38
fl_value_new_float
G_MODULE_EXPORT FlValue * fl_value_new_float(double value)
Definition: fl_value.cc:269
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
fl_value_new_string
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276