Flutter Engine
The Flutter Engine
fl_settings_plugin_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_plugin.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
20MATCHER_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);
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
40TEST(FlSettingsPluginTest, 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(FlSettingsPlugin) plugin = fl_settings_plugin_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
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
60
61 EXPECT_SETTING(messenger, "alwaysUse24HourFormat", use_24h);
62
64}
65
66TEST(FlSettingsPluginTest, 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(FlSettingsPlugin) plugin = fl_settings_plugin_new(engine);
74
75 g_autoptr(FlValue) light = fl_value_new_string("light");
76 g_autoptr(FlValue) dark = fl_value_new_string("dark");
77
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
86
87 EXPECT_SETTING(messenger, "platformBrightness", dark);
88
90}
91
92TEST(FlSettingsPluginTest, 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(FlSettingsPlugin) plugin = fl_settings_plugin_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
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
112
113 EXPECT_SETTING(messenger, "textScaleFactor", two);
114
116}
117
118// MOCK_ENGINE_PROC is leaky by design
119// NOLINTBEGIN(clang-analyzer-core.StackAddressEscape)
120TEST(FlSettingsPluginTest, AccessibilityFeatures) {
121 g_autoptr(FlEngine) engine = make_mock_engine();
123
124 std::vector<FlutterAccessibilityFeature> calls;
126 UpdateAccessibilityFeatures,
127 ([&calls](auto engine, FlutterAccessibilityFeature features) {
128 calls.push_back(features);
129 return kSuccess;
130 }));
131
132 g_autoptr(FlSettingsPlugin) plugin = fl_settings_plugin_new(engine);
133
134 ::testing::NiceMock<flutter::testing::MockSettings> settings;
135
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
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
151 EXPECT_THAT(calls, ::testing::SizeIs(1));
152 EXPECT_EQ(calls.back(), static_cast<FlutterAccessibilityFeature>(
155
157 EXPECT_THAT(calls, ::testing::SizeIs(2));
158 EXPECT_EQ(calls.back(), static_cast<FlutterAccessibilityFeature>(0));
159
161 EXPECT_THAT(calls, ::testing::SizeIs(3));
162 EXPECT_EQ(calls.back(), static_cast<FlutterAccessibilityFeature>(
164
166 EXPECT_THAT(calls, ::testing::SizeIs(4));
167 EXPECT_EQ(calls.back(), static_cast<FlutterAccessibilityFeature>(
169}
170// NOLINTEND(clang-analyzer-core.StackAddressEscape)
FlutterAccessibilityFeature
Definition: embedder.h:91
@ kFlutterAccessibilityFeatureHighContrast
Request that UI be rendered with darker colors.
Definition: embedder.h:105
@ kFlutterAccessibilityFeatureDisableAnimations
Request that animations be disabled or simplified.
Definition: embedder.h:98
@ kSuccess
Definition: embedder.h:73
FlutterEngine engine
Definition: main.cc:68
FlutterEngineProcTable * fl_engine_get_embedder_api(FlEngine *self)
Definition: fl_engine.cc:599
static FlEngine * make_mock_engine()
G_MODULE_EXPORT FlJsonMessageCodec * fl_json_message_codec_new()
G_MODULE_EXPORT FlValue * fl_message_codec_decode_message(FlMessageCodec *self, GBytes *message, GError **error)
gboolean fl_settings_get_high_contrast(FlSettings *self)
Definition: fl_settings.cc:42
gdouble fl_settings_get_text_scaling_factor(FlSettings *self)
Definition: fl_settings.cc:46
void fl_settings_emit_changed(FlSettings *self)
Definition: fl_settings.cc:50
gboolean fl_settings_get_enable_animations(FlSettings *self)
Definition: fl_settings.cc:38
FlColorScheme fl_settings_get_color_scheme(FlSettings *self)
Definition: fl_settings.cc:34
FlClockFormat fl_settings_get_clock_format(FlSettings *self)
Definition: fl_settings.cc:30
@ FL_COLOR_SCHEME_DARK
Definition: fl_settings.h:38
@ FL_COLOR_SCHEME_LIGHT
Definition: fl_settings.h:37
@ FL_CLOCK_FORMAT_12H
Definition: fl_settings.h:23
@ FL_CLOCK_FORMAT_24H
Definition: fl_settings.h:24
FlSettingsPlugin * fl_settings_plugin_new(FlEngine *engine)
void fl_settings_plugin_start(FlSettingsPlugin *self, FlSettings *settings)
TEST(FlSettingsPluginTest, AlwaysUse24HourFormat)
#define EXPECT_SETTING(messenger, key, value)
MATCHER_P2(HasSetting, key, value, "")
uint8_t value
G_MODULE_EXPORT FlValue * fl_value_lookup_string(FlValue *self, const gchar *key)
Definition: fl_value.cc:811
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276
G_MODULE_EXPORT FlValue * fl_value_new_bool(bool value)
Definition: fl_value.cc:255
G_MODULE_EXPORT FlValue * fl_value_new_float(double value)
Definition: fl_value.cc:269
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
Win32Message message
static CStringUniquePtr PrintToString(const char *format,...)
Definition: uri.cc:41
#define MOCK_ENGINE_PROC(proc, mock_impl)
Function-pointer-based versions of the APIs above.
Definition: embedder.h:3319
FlutterEngineUpdateAccessibilityFeaturesFnPtr UpdateAccessibilityFeatures
Definition: embedder.h:3344