Flutter Engine
The Flutter Engine
system_utils_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/glfw/system_utils.h"
6
7#include <cstdlib>
8
9#include "gtest/gtest.h"
10
11namespace flutter {
12namespace {
13
14// This is a helper for setting up the different environment variables to
15// specific strings, calling GetPreferredLanguageInfo, and then restoring those
16// environment variables to any previously existing values.
17std::vector<LanguageInfo> SetAndRestoreLanguageAroundGettingLanguageInfo(
18 const char* language,
19 const char* lc_all,
20 const char* lc_messages,
21 const char* lang) {
22 std::vector<const char*> env_vars{
23 "LANGUAGE",
24 "LC_ALL",
25 "LC_MESSAGES",
26 "LANG",
27 };
28 std::map<const char*, const char*> new_values{
29 {env_vars[0], language},
30 {env_vars[1], lc_all},
31 {env_vars[2], lc_messages},
32 {env_vars[3], lang},
33 };
34 std::map<const char*, const char*> prior_values;
35 for (auto var : env_vars) {
36 const char* value = getenv(var);
37 if (value != nullptr) {
38 prior_values.emplace(var, value);
39 }
40 const char* new_value = new_values.at(var);
41 if (new_value != nullptr) {
42 setenv(var, new_value, 1);
43 } else {
44 unsetenv(var);
45 }
46 }
47
48 std::vector<LanguageInfo> languages = GetPreferredLanguageInfo();
49
50 for (auto [var, value] : prior_values) {
51 setenv(var, value, 1);
52 }
53
54 return languages;
55}
56
57TEST(FlutterGlfwSystemUtilsTest, GetPreferredLanuageInfoFull) {
58 const char* locale_string = "en_GB.ISO-8859-1@euro:en_US:sv:zh_CN.UTF-8";
59
60 std::vector<LanguageInfo> languages =
61 SetAndRestoreLanguageAroundGettingLanguageInfo(locale_string, nullptr,
62 nullptr, nullptr);
63
64 EXPECT_EQ(languages.size(), 15UL);
65
66 EXPECT_STREQ(languages[0].language.c_str(), "en");
67 EXPECT_STREQ(languages[0].territory.c_str(), "GB");
68 EXPECT_STREQ(languages[0].codeset.c_str(), "ISO-8859-1");
69 EXPECT_STREQ(languages[0].modifier.c_str(), "euro");
70
71 EXPECT_STREQ(languages[1].language.c_str(), "en");
72 EXPECT_STREQ(languages[1].territory.c_str(), "GB");
73 EXPECT_STREQ(languages[1].codeset.c_str(), "");
74 EXPECT_STREQ(languages[1].modifier.c_str(), "euro");
75
76 EXPECT_STREQ(languages[2].language.c_str(), "en");
77 EXPECT_STREQ(languages[2].territory.c_str(), "");
78 EXPECT_STREQ(languages[2].codeset.c_str(), "ISO-8859-1");
79 EXPECT_STREQ(languages[2].modifier.c_str(), "euro");
80
81 EXPECT_STREQ(languages[3].language.c_str(), "en");
82 EXPECT_STREQ(languages[3].territory.c_str(), "");
83 EXPECT_STREQ(languages[3].codeset.c_str(), "");
84 EXPECT_STREQ(languages[3].modifier.c_str(), "euro");
85
86 EXPECT_STREQ(languages[4].language.c_str(), "en");
87 EXPECT_STREQ(languages[4].territory.c_str(), "GB");
88 EXPECT_STREQ(languages[4].codeset.c_str(), "ISO-8859-1");
89 EXPECT_STREQ(languages[4].modifier.c_str(), "");
90
91 EXPECT_STREQ(languages[5].language.c_str(), "en");
92 EXPECT_STREQ(languages[5].territory.c_str(), "GB");
93 EXPECT_STREQ(languages[5].codeset.c_str(), "");
94 EXPECT_STREQ(languages[5].modifier.c_str(), "");
95
96 EXPECT_STREQ(languages[6].language.c_str(), "en");
97 EXPECT_STREQ(languages[6].territory.c_str(), "");
98 EXPECT_STREQ(languages[6].codeset.c_str(), "ISO-8859-1");
99 EXPECT_STREQ(languages[6].modifier.c_str(), "");
100
101 EXPECT_STREQ(languages[7].language.c_str(), "en");
102 EXPECT_STREQ(languages[7].territory.c_str(), "");
103 EXPECT_STREQ(languages[7].codeset.c_str(), "");
104 EXPECT_STREQ(languages[7].modifier.c_str(), "");
105
106 EXPECT_STREQ(languages[8].language.c_str(), "en");
107 EXPECT_STREQ(languages[8].territory.c_str(), "US");
108 EXPECT_STREQ(languages[8].codeset.c_str(), "");
109 EXPECT_STREQ(languages[8].modifier.c_str(), "");
110
111 EXPECT_STREQ(languages[9].language.c_str(), "en");
112 EXPECT_STREQ(languages[9].territory.c_str(), "");
113 EXPECT_STREQ(languages[9].codeset.c_str(), "");
114 EXPECT_STREQ(languages[9].modifier.c_str(), "");
115
116 EXPECT_STREQ(languages[10].language.c_str(), "sv");
117 EXPECT_STREQ(languages[10].territory.c_str(), "");
118 EXPECT_STREQ(languages[10].codeset.c_str(), "");
119 EXPECT_STREQ(languages[10].modifier.c_str(), "");
120
121 EXPECT_STREQ(languages[11].language.c_str(), "zh");
122 EXPECT_STREQ(languages[11].territory.c_str(), "CN");
123 EXPECT_STREQ(languages[11].codeset.c_str(), "UTF-8");
124 EXPECT_STREQ(languages[11].modifier.c_str(), "");
125
126 EXPECT_STREQ(languages[12].language.c_str(), "zh");
127 EXPECT_STREQ(languages[12].territory.c_str(), "CN");
128 EXPECT_STREQ(languages[12].codeset.c_str(), "");
129 EXPECT_STREQ(languages[12].modifier.c_str(), "");
130
131 EXPECT_STREQ(languages[13].language.c_str(), "zh");
132 EXPECT_STREQ(languages[13].territory.c_str(), "");
133 EXPECT_STREQ(languages[13].codeset.c_str(), "UTF-8");
134 EXPECT_STREQ(languages[13].modifier.c_str(), "");
135
136 EXPECT_STREQ(languages[14].language.c_str(), "zh");
137 EXPECT_STREQ(languages[14].territory.c_str(), "");
138 EXPECT_STREQ(languages[14].codeset.c_str(), "");
139 EXPECT_STREQ(languages[14].modifier.c_str(), "");
140}
141
142TEST(FlutterGlfwSystemUtilsTest, GetPreferredLanuageInfoWeird) {
143 const char* locale_string = "tt_RU@iqtelif.UTF-8";
144 std::vector<LanguageInfo> languages =
145 SetAndRestoreLanguageAroundGettingLanguageInfo(locale_string, nullptr,
146 nullptr, nullptr);
147
148 EXPECT_EQ(languages.size(), 4UL);
149
150 EXPECT_STREQ(languages[0].language.c_str(), "tt");
151 EXPECT_STREQ(languages[0].territory.c_str(), "RU");
152 EXPECT_STREQ(languages[0].codeset.c_str(), "");
153 EXPECT_STREQ(languages[0].modifier.c_str(), "iqtelif.UTF-8");
154
155 EXPECT_STREQ(languages[1].language.c_str(), "tt");
156 EXPECT_STREQ(languages[1].territory.c_str(), "");
157 EXPECT_STREQ(languages[1].codeset.c_str(), "");
158 EXPECT_STREQ(languages[1].modifier.c_str(), "iqtelif.UTF-8");
159
160 EXPECT_STREQ(languages[2].language.c_str(), "tt");
161 EXPECT_STREQ(languages[2].territory.c_str(), "RU");
162 EXPECT_STREQ(languages[2].codeset.c_str(), "");
163 EXPECT_STREQ(languages[2].modifier.c_str(), "");
164
165 EXPECT_STREQ(languages[3].language.c_str(), "tt");
166 EXPECT_STREQ(languages[3].territory.c_str(), "");
167 EXPECT_STREQ(languages[3].codeset.c_str(), "");
168 EXPECT_STREQ(languages[3].modifier.c_str(), "");
169}
170
171TEST(FlutterGlfwSystemUtilsTest, GetPreferredLanuageInfoEmpty) {
172 const char* locale_string = "";
173 std::vector<LanguageInfo> languages =
174 SetAndRestoreLanguageAroundGettingLanguageInfo(
175 locale_string, locale_string, locale_string, locale_string);
176
177 EXPECT_EQ(languages.size(), 1UL);
178
179 EXPECT_STREQ(languages[0].language.c_str(), "C");
180 EXPECT_TRUE(languages[0].territory.empty());
181 EXPECT_TRUE(languages[0].codeset.empty());
182 EXPECT_TRUE(languages[0].modifier.empty());
183}
184
185TEST(FlutterGlfwSystemUtilsTest, GetPreferredLanuageInfoEnvVariableOrdering1) {
186 const char* language = "de";
187 const char* lc_all = "en";
188 const char* lc_messages = "zh";
189 const char* lang = "tt";
190
191 std::vector<LanguageInfo> languages =
192 SetAndRestoreLanguageAroundGettingLanguageInfo(language, lc_all,
193 lc_messages, lang);
194
195 EXPECT_EQ(languages.size(), 1UL);
196 EXPECT_STREQ(languages[0].language.c_str(), language);
197}
198
199TEST(FlutterGlfwSystemUtilsTest, GetPreferredLanuageInfoEnvVariableOrdering2) {
200 const char* lc_all = "en";
201 const char* lc_messages = "zh";
202 const char* lang = "tt";
203
204 std::vector<LanguageInfo> languages =
205 SetAndRestoreLanguageAroundGettingLanguageInfo(nullptr, lc_all,
206 lc_messages, lang);
207
208 EXPECT_EQ(languages.size(), 1UL);
209 EXPECT_STREQ(languages[0].language.c_str(), lc_all);
210}
211
212TEST(FlutterGlfwSystemUtilsTest, GetPreferredLanuageInfoEnvVariableOrdering3) {
213 const char* lc_messages = "zh";
214 const char* lang = "tt";
215
216 std::vector<LanguageInfo> languages =
217 SetAndRestoreLanguageAroundGettingLanguageInfo(nullptr, nullptr,
218 lc_messages, lang);
219
220 EXPECT_EQ(languages.size(), 1UL);
221 EXPECT_STREQ(languages[0].language.c_str(), lc_messages);
222}
223
224TEST(FlutterGlfwSystemUtilsTest, GetPreferredLanuageInfoEnvVariableOrdering4) {
225 const char* lang = "tt";
226
227 std::vector<LanguageInfo> languages =
228 SetAndRestoreLanguageAroundGettingLanguageInfo(nullptr, nullptr, nullptr,
229 lang);
230
231 EXPECT_EQ(languages.size(), 1UL);
232 EXPECT_STREQ(languages[0].language.c_str(), lang);
233}
234
235TEST(FlutterGlfwSystemUtilsTest, GetPreferredLanuageInfoEnvVariableOrdering5) {
236 std::vector<LanguageInfo> languages =
237 SetAndRestoreLanguageAroundGettingLanguageInfo(nullptr, nullptr, nullptr,
238 nullptr);
239
240 EXPECT_EQ(languages.size(), 1UL);
241 EXPECT_STREQ(languages[0].language.c_str(), "C");
242}
243
244TEST(FlutterGlfwSystemUtilsTest, ConvertToFlutterLocaleEmpty) {
245 std::vector<LanguageInfo> languages;
246
247 std::vector<FlutterLocale> locales = ConvertToFlutterLocale(languages);
248
249 EXPECT_TRUE(locales.empty());
250}
251
252TEST(FlutterGlfwSystemUtilsTest, ConvertToFlutterLocaleNonEmpty) {
253 std::vector<LanguageInfo> languages;
254 languages.push_back(LanguageInfo{"en", "US", "", ""});
255 languages.push_back(LanguageInfo{"tt", "RU", "", "iqtelif.UTF-8"});
256 languages.push_back(LanguageInfo{"sv", "", "", ""});
257 languages.push_back(LanguageInfo{"de", "DE", "UTF-8", "euro"});
258 languages.push_back(LanguageInfo{"zh", "CN", "UTF-8", ""});
259
260 std::vector<FlutterLocale> locales = ConvertToFlutterLocale(languages);
261
262 EXPECT_EQ(locales.size(), 5UL);
263
264 EXPECT_EQ(locales[0].struct_size, sizeof(FlutterLocale));
265 EXPECT_STREQ(locales[0].language_code, "en");
266 EXPECT_STREQ(locales[0].country_code, "US");
267 EXPECT_EQ(locales[0].script_code, nullptr);
268 EXPECT_EQ(locales[0].variant_code, nullptr);
269
270 EXPECT_STREQ(locales[1].language_code, "tt");
271 EXPECT_STREQ(locales[1].country_code, "RU");
272 EXPECT_EQ(locales[1].script_code, nullptr);
273 EXPECT_STREQ(locales[1].variant_code, "iqtelif.UTF-8");
274
275 EXPECT_STREQ(locales[2].language_code, "sv");
276 EXPECT_EQ(locales[2].country_code, nullptr);
277 EXPECT_EQ(locales[2].script_code, nullptr);
278 EXPECT_EQ(locales[2].variant_code, nullptr);
279
280 EXPECT_STREQ(locales[3].language_code, "de");
281 EXPECT_STREQ(locales[3].country_code, "DE");
282 EXPECT_STREQ(locales[3].script_code, "UTF-8");
283 EXPECT_STREQ(locales[3].variant_code, "euro");
284
285 EXPECT_STREQ(locales[4].language_code, "zh");
286 EXPECT_STREQ(locales[4].country_code, "CN");
287 EXPECT_STREQ(locales[4].script_code, "UTF-8");
288 EXPECT_EQ(locales[4].variant_code, nullptr);
289}
290
291} // namespace
292} // namespace flutter
uint8_t value
TEST(FrameTimingsRecorderTest, RecordVsync)
std::vector< FlutterLocale > ConvertToFlutterLocale(const std::vector< LanguageInfo > &languages)
std::vector< LanguageInfo > GetPreferredLanguageInfo()
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678