| Flutter Windows Embedder |

system_utils_unittests.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 <cstring>
6 #include <cwchar>
7
8 #include "flutter/fml/macros.h"
9 #include "flutter/shell/platform/windows/system_utils.h"
10 #include "flutter/shell/platform/windows/testing/mock_windows_proc_table.h"
11 #include "gtest/gtest.h"
12
13 namespace flutter {
14 namespace testing {
15
16 TEST(SystemUtils, GetPreferredLanguageInfo) {
17 WindowsProcTable proc_table;
18 std::vector<LanguageInfo> languages =
19 GetPreferredLanguageInfo(WindowsProcTable());
20 // There should be at least one language.
21 ASSERT_GE(languages.size(), 1);
22 // The info should have a valid languge.
23 EXPECT_GE(languages[0].language.size(), 2);
24 }
25
26 TEST(SystemUtils, GetPreferredLanguages) {
27 MockWindowsProcTable proc_table;
28 EXPECT_CALL(proc_table, GetThreadPreferredUILanguages)
29 .WillRepeatedly(
30 [](DWORD flags, PULONG count, PZZWSTR languages, PULONG size) {
31 // Languages string ends in a double-null.
32 static const wchar_t lang[] = L"en-US\0";
33 static const size_t lang_len = sizeof(lang) / sizeof(wchar_t);
34 static const int cnt = 1;
35 if (languages == nullptr) {
36 *size = lang_len;
37 *count = cnt;
38 } else if (*size >= lang_len) {
39 memcpy(languages, lang, lang_len * sizeof(wchar_t));
40 }
41 return TRUE;
42 });
43 std::vector<std::wstring> languages = GetPreferredLanguages(proc_table);
44 // There should be at least one language.
45 ASSERT_GE(languages.size(), 1);
46 // The language should be non-empty.
47 EXPECT_FALSE(languages[0].empty());
48 // There should not be a trailing null from the parsing step.
49 EXPECT_EQ(languages[0].size(), wcslen(languages[0].c_str()));
50 EXPECT_EQ(languages[0], L"en-US");
51 }
52
53 TEST(SystemUtils, ParseLanguageNameGeneric) {
54 LanguageInfo info = ParseLanguageName(L"en");
55 EXPECT_EQ(info.language, "en");
56 EXPECT_TRUE(info.region.empty());
57 EXPECT_TRUE(info.script.empty());
58 }
59
60 TEST(SystemUtils, ParseLanguageNameWithRegion) {
61 LanguageInfo info = ParseLanguageName(L"hu-HU");
62 EXPECT_EQ(info.language, "hu");
63 EXPECT_EQ(info.region, "HU");
64 EXPECT_TRUE(info.script.empty());
65 }
66
67 TEST(SystemUtils, ParseLanguageNameWithScript) {
68 LanguageInfo info = ParseLanguageName(L"us-Latn");
69 EXPECT_EQ(info.language, "us");
70 EXPECT_TRUE(info.region.empty());
71 EXPECT_EQ(info.script, "Latn");
72 }
73
74 TEST(SystemUtils, ParseLanguageNameWithRegionAndScript) {
75 LanguageInfo info = ParseLanguageName(L"uz-Latn-UZ");
76 EXPECT_EQ(info.language, "uz");
77 EXPECT_EQ(info.region, "UZ");
78 EXPECT_EQ(info.script, "Latn");
79 }
80
81 TEST(SystemUtils, ParseLanguageNameWithSuplementalLanguage) {
82 LanguageInfo info = ParseLanguageName(L"en-US-x-fabricam");
83 EXPECT_EQ(info.language, "en");
84 EXPECT_EQ(info.region, "US");
85 EXPECT_TRUE(info.script.empty());
86 }
87
88 // Ensure that ISO 639-2/T codes are handled.
89 TEST(SystemUtils, ParseLanguageNameWithThreeCharacterLanguage) {
90 LanguageInfo info = ParseLanguageName(L"ale-ZZ");
91 EXPECT_EQ(info.language, "ale");
92 EXPECT_EQ(info.region, "ZZ");
93 EXPECT_TRUE(info.script.empty());
94 }
95
96 TEST(SystemUtils, GetUserTimeFormat) {
97 // The value varies based on machine; just ensure that something is returned.
98 EXPECT_FALSE(GetUserTimeFormat().empty());
99 }
100
101 TEST(SystemUtils, Prefer24HourTimeHandlesEmptyFormat) {
102 EXPECT_FALSE(Prefer24HourTime(L""));
103 }
104
105 TEST(SystemUtils, Prefer24HourTimeHandles12Hour) {
106 EXPECT_FALSE(Prefer24HourTime(L"h:mm:ss tt"));
107 }
108
109 TEST(SystemUtils, Prefer24HourTimeHandles24Hour) {
110 EXPECT_TRUE(Prefer24HourTime(L"HH:mm:ss"));
111 }
112
113 } // namespace testing
114 } // namespace flutter
flutter::LanguageInfo::script
std::string script
Definition: system_utils.h:26
flutter::GetPreferredLanguageInfo
std::vector< LanguageInfo > GetPreferredLanguageInfo(const WindowsProcTable &windows_proc_table)
Definition: system_utils.cc:15
flutter::GetUserTimeFormat
std::wstring GetUserTimeFormat()
Definition: system_utils.cc:101
system_utils.h
flutter::Prefer24HourTime
bool Prefer24HourTime(std::wstring time_format)
Definition: system_utils.cc:113
flutter::ParseLanguageName
LanguageInfo ParseLanguageName(std::wstring language_name)
Definition: system_utils.cc:71
flutter::LanguageInfo::language
std::string language
Definition: system_utils.h:24
flutter::LanguageInfo
Definition: system_utils.h:23
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::WindowsProcTable
Definition: windows_proc_table.h:17
flutter::testing::TEST
TEST(AccessibilityBridgeWindows, GetParent)
Definition: accessibility_bridge_windows_unittests.cc:235
flutter::LanguageInfo::region
std::string region
Definition: system_utils.h:25
flutter::GetPreferredLanguages
std::vector< std::wstring > GetPreferredLanguages(const WindowsProcTable &windows_proc_table)
Definition: system_utils.cc:45