forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fl_settings_plugin_test.cc
170 lines (132 loc) · 6.95 KB
/
fl_settings_plugin_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/shell/platform/linux/fl_settings_plugin.h"
#include "flutter/shell/platform/embedder/embedder.h"
#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
#include "flutter/shell/platform/linux/fl_engine_private.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_binary_messenger.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_json_message_codec.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_value.h"
#include "flutter/shell/platform/linux/testing/fl_test.h"
#include "flutter/shell/platform/linux/testing/mock_binary_messenger.h"
#include "flutter/shell/platform/linux/testing/mock_settings.h"
#include "flutter/testing/testing.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
MATCHER_P2(HasSetting, key, value, "") {
g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new();
g_autoptr(FlValue) message =
fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), arg, nullptr);
if (fl_value_equal(fl_value_lookup_string(message, key), value)) {
return true;
}
*result_listener << ::testing::PrintToString(message);
return false;
}
#define EXPECT_SETTING(messenger, key, value) \
EXPECT_CALL( \
messenger, \
fl_binary_messenger_send_on_channel( \
::testing::Eq<FlBinaryMessenger*>(messenger), \
::testing::StrEq("flutter/settings"), HasSetting(key, value), \
::testing::A<GCancellable*>(), ::testing::A<GAsyncReadyCallback>(), \
::testing::A<gpointer>()))
TEST(FlSettingsPluginTest, AlwaysUse24HourFormat) {
::testing::NiceMock<flutter::testing::MockSettings> settings;
::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
g_autoptr(FlEngine) engine =
FL_ENGINE(g_object_new(fl_engine_get_type(), "binary-messenger",
FL_BINARY_MESSENGER(messenger), nullptr));
g_autoptr(FlSettingsPlugin) plugin = fl_settings_plugin_new(engine);
g_autoptr(FlValue) use_12h = fl_value_new_bool(false);
g_autoptr(FlValue) use_24h = fl_value_new_bool(true);
EXPECT_CALL(settings, fl_settings_get_clock_format(
::testing::Eq<FlSettings*>(settings)))
.WillOnce(::testing::Return(FL_CLOCK_FORMAT_12H))
.WillOnce(::testing::Return(FL_CLOCK_FORMAT_24H));
EXPECT_SETTING(messenger, "alwaysUse24HourFormat", use_12h);
fl_settings_plugin_start(plugin, settings);
EXPECT_SETTING(messenger, "alwaysUse24HourFormat", use_24h);
fl_settings_emit_changed(settings);
}
TEST(FlSettingsPluginTest, PlatformBrightness) {
::testing::NiceMock<flutter::testing::MockSettings> settings;
::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
g_autoptr(FlEngine) engine =
FL_ENGINE(g_object_new(fl_engine_get_type(), "binary-messenger",
FL_BINARY_MESSENGER(messenger), nullptr));
g_autoptr(FlSettingsPlugin) plugin = fl_settings_plugin_new(engine);
g_autoptr(FlValue) light = fl_value_new_string("light");
g_autoptr(FlValue) dark = fl_value_new_string("dark");
EXPECT_CALL(settings, fl_settings_get_color_scheme(
::testing::Eq<FlSettings*>(settings)))
.WillOnce(::testing::Return(FL_COLOR_SCHEME_LIGHT))
.WillOnce(::testing::Return(FL_COLOR_SCHEME_DARK));
EXPECT_SETTING(messenger, "platformBrightness", light);
fl_settings_plugin_start(plugin, settings);
EXPECT_SETTING(messenger, "platformBrightness", dark);
fl_settings_emit_changed(settings);
}
TEST(FlSettingsPluginTest, TextScaleFactor) {
::testing::NiceMock<flutter::testing::MockSettings> settings;
::testing::NiceMock<flutter::testing::MockBinaryMessenger> messenger;
g_autoptr(FlEngine) engine =
FL_ENGINE(g_object_new(fl_engine_get_type(), "binary-messenger",
FL_BINARY_MESSENGER(messenger), nullptr));
g_autoptr(FlSettingsPlugin) plugin = fl_settings_plugin_new(engine);
g_autoptr(FlValue) one = fl_value_new_float(1.0);
g_autoptr(FlValue) two = fl_value_new_float(2.0);
EXPECT_CALL(settings, fl_settings_get_text_scaling_factor(
::testing::Eq<FlSettings*>(settings)))
.WillOnce(::testing::Return(1.0))
.WillOnce(::testing::Return(2.0));
EXPECT_SETTING(messenger, "textScaleFactor", one);
fl_settings_plugin_start(plugin, settings);
EXPECT_SETTING(messenger, "textScaleFactor", two);
fl_settings_emit_changed(settings);
}
// MOCK_ENGINE_PROC is leaky by design
// NOLINTBEGIN(clang-analyzer-core.StackAddressEscape)
TEST(FlSettingsPluginTest, AccessibilityFeatures) {
g_autoptr(FlEngine) engine = make_mock_engine();
FlutterEngineProcTable* embedder_api = fl_engine_get_embedder_api(engine);
std::vector<FlutterAccessibilityFeature> calls;
embedder_api->UpdateAccessibilityFeatures = MOCK_ENGINE_PROC(
UpdateAccessibilityFeatures,
([&calls](auto engine, FlutterAccessibilityFeature features) {
calls.push_back(features);
return kSuccess;
}));
g_autoptr(FlSettingsPlugin) plugin = fl_settings_plugin_new(engine);
::testing::NiceMock<flutter::testing::MockSettings> settings;
EXPECT_CALL(settings, fl_settings_get_enable_animations(
::testing::Eq<FlSettings*>(settings)))
.WillOnce(::testing::Return(false))
.WillOnce(::testing::Return(true))
.WillOnce(::testing::Return(false))
.WillOnce(::testing::Return(true));
EXPECT_CALL(settings, fl_settings_get_high_contrast(
::testing::Eq<FlSettings*>(settings)))
.WillOnce(::testing::Return(true))
.WillOnce(::testing::Return(false))
.WillOnce(::testing::Return(false))
.WillOnce(::testing::Return(true));
fl_settings_plugin_start(plugin, settings);
EXPECT_THAT(calls, ::testing::SizeIs(1));
EXPECT_EQ(calls.back(), static_cast<FlutterAccessibilityFeature>(
kFlutterAccessibilityFeatureDisableAnimations |
kFlutterAccessibilityFeatureHighContrast));
fl_settings_emit_changed(settings);
EXPECT_THAT(calls, ::testing::SizeIs(2));
EXPECT_EQ(calls.back(), static_cast<FlutterAccessibilityFeature>(0));
fl_settings_emit_changed(settings);
EXPECT_THAT(calls, ::testing::SizeIs(3));
EXPECT_EQ(calls.back(), static_cast<FlutterAccessibilityFeature>(
kFlutterAccessibilityFeatureDisableAnimations));
fl_settings_emit_changed(settings);
EXPECT_THAT(calls, ::testing::SizeIs(4));
EXPECT_EQ(calls.back(), static_cast<FlutterAccessibilityFeature>(
kFlutterAccessibilityFeatureHighContrast));
}
// NOLINTEND(clang-analyzer-core.StackAddressEscape)