1 // Copyright 2018 PDFium 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 "xfa/fxfa/parser/cxfa_xmllocale.h"
6
7 #include <memory>
8
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace {
12
13 const char kXMLData[] =
14 "<locale name=\"en_US\" desc=\"English(America)\">"
15 "<calendarSymbols name=\"gregorian\"><monthNames><month>January</month>"
16 "<month>February</month>"
17 "<month>March</month>"
18 "<month>April</month>"
19 "<month>May</month>"
20 "<month>June</month>"
21 "<month>July</month>"
22 "<month>August</month>"
23 "<month>September</month>"
24 "<month>October</month>"
25 "<month>November</month>"
26 "<month>December</month>"
27 "</monthNames>"
28 "<monthNames abbr=\"1\"><month>Jan</month>"
29 "<month>Feb</month>"
30 "<month>Mar</month>"
31 "<month>Apr</month>"
32 "<month>May</month>"
33 "<month>Jun</month>"
34 "<month>Jul</month>"
35 "<month>Aug</month>"
36 "<month>Sep</month>"
37 "<month>Oct</month>"
38 "<month>Nov</month>"
39 "<month>Dec</month>"
40 "</monthNames>"
41 "<dayNames><day>Sunday</day>"
42 "<day>Monday</day>"
43 "<day>Tuesday</day>"
44 "<day>Wednesday</day>"
45 "<day>Thursday</day>"
46 "<day>Friday</day>"
47 "<day>Saturday</day>"
48 "</dayNames>"
49 "<dayNames abbr=\"1\"><day>Sun</day>"
50 "<day>Mon</day>"
51 "<day>Tue</day>"
52 "<day>Wed</day>"
53 "<day>Thu</day>"
54 "<day>Fri</day>"
55 "<day>Sat</day>"
56 "</dayNames>"
57 "<meridiemNames><meridiem>AM</meridiem>"
58 "<meridiem>PM</meridiem>"
59 "</meridiemNames>"
60 "<eraNames><era>BC</era>"
61 "<era>AD</era>"
62 "</eraNames>"
63 "</calendarSymbols>"
64 "<datePatterns><datePattern name=\"full\">EEEE, MMMM D, YYYY</datePattern>"
65 "<datePattern name=\"long\">MMMM D, YYYY</datePattern>"
66 "<datePattern name=\"med\">MMM D, YYYY</datePattern>"
67 "<datePattern name=\"short\">M/D/YY</datePattern>"
68 "</datePatterns>"
69 "<timePatterns><timePattern name=\"full\">h:MM:SS A Z</timePattern>"
70 "<timePattern name=\"long\">h:MM:SS A Z</timePattern>"
71 "<timePattern name=\"med\">h:MM:SS A</timePattern>"
72 "<timePattern name=\"short\">h:MM A</timePattern>"
73 "</timePatterns>"
74 "<dateTimeSymbols>GyMdkHmsSEDFwWahKzZ</dateTimeSymbols>"
75 "<numberPatterns><numberPattern name=\"numeric\">z,zz9.zzz</numberPattern>"
76 "<numberPattern name=\"currency\">$z,zz9.99|($z,zz9.99)</numberPattern>"
77 "<numberPattern name=\"percent\">z,zz9%</numberPattern>"
78 "</numberPatterns>"
79 "<numberSymbols><numberSymbol name=\"decimal\">.</numberSymbol>"
80 "<numberSymbol name=\"grouping\">,</numberSymbol>"
81 "<numberSymbol name=\"percent\">%</numberSymbol>"
82 "<numberSymbol name=\"minus\">-</numberSymbol>"
83 "<numberSymbol name=\"zero\">0</numberSymbol>"
84 "</numberSymbols>"
85 "<currencySymbols><currencySymbol name=\"symbol\">$</currencySymbol>"
86 "<currencySymbol name=\"isoname\">USD</currencySymbol>"
87 "<currencySymbol name=\"decimal\">.</currencySymbol>"
88 "</currencySymbols>"
89 "</locale>";
90
CreateLocaleHelper()91 std::unique_ptr<CXFA_XMLLocale> CreateLocaleHelper() {
92 return CXFA_XMLLocale::Create(pdfium::as_writable_bytes(
93 pdfium::make_span(const_cast<char*>(kXMLData), strlen(kXMLData))));
94 }
95
96 } // namespace
97
TEST(CXFA_XMLLocaleTest,Create)98 TEST(CXFA_XMLLocaleTest, Create) {
99 auto locale = CreateLocaleHelper();
100 EXPECT_TRUE(locale != nullptr);
101 }
102
TEST(CXFA_XMLLocaleTest,CreateBadXML)103 TEST(CXFA_XMLLocaleTest, CreateBadXML) {
104 auto locale = CXFA_XMLLocale::Create(pdfium::span<uint8_t>());
105 EXPECT_TRUE(locale == nullptr);
106 }
107
TEST(CXFA_XMLLocaleTest,GetName)108 TEST(CXFA_XMLLocaleTest, GetName) {
109 auto locale = CreateLocaleHelper();
110 ASSERT_TRUE(locale != nullptr);
111
112 EXPECT_EQ(L"en_US", locale->GetName());
113 }
114
TEST(CXFA_XMLLocaleTest,GetNumericSymbols)115 TEST(CXFA_XMLLocaleTest, GetNumericSymbols) {
116 auto locale = CreateLocaleHelper();
117 ASSERT_TRUE(locale != nullptr);
118
119 EXPECT_EQ(L".", locale->GetDecimalSymbol());
120 EXPECT_EQ(L",", locale->GetGroupingSymbol());
121 EXPECT_EQ(L"%", locale->GetPercentSymbol());
122 EXPECT_EQ(L"-", locale->GetMinusSymbol());
123 EXPECT_EQ(L"$", locale->GetCurrencySymbol());
124 }
125
TEST(CXFA_XMLLocaleTest,GetDateTimeSymbols)126 TEST(CXFA_XMLLocaleTest, GetDateTimeSymbols) {
127 auto locale = CreateLocaleHelper();
128 ASSERT_TRUE(locale != nullptr);
129
130 EXPECT_EQ(L"GyMdkHmsSEDFwWahKzZ", locale->GetDateTimeSymbols());
131 }
132
TEST(CXFA_XMLLocaleTest,GetMonthName)133 TEST(CXFA_XMLLocaleTest, GetMonthName) {
134 auto locale = CreateLocaleHelper();
135 ASSERT_TRUE(locale != nullptr);
136
137 EXPECT_EQ(L"", locale->GetMonthName(24, false));
138 EXPECT_EQ(L"", locale->GetMonthName(-5, false));
139 EXPECT_EQ(L"Feb", locale->GetMonthName(1, true));
140 EXPECT_EQ(L"February", locale->GetMonthName(1, false));
141 }
142
TEST(CXFA_XMLLocaleTest,GetDayName)143 TEST(CXFA_XMLLocaleTest, GetDayName) {
144 auto locale = CreateLocaleHelper();
145 ASSERT_TRUE(locale != nullptr);
146
147 EXPECT_EQ(L"", locale->GetDayName(24, false));
148 EXPECT_EQ(L"", locale->GetDayName(-5, false));
149 EXPECT_EQ(L"Mon", locale->GetDayName(1, true));
150 EXPECT_EQ(L"Monday", locale->GetDayName(1, false));
151 }
152
TEST(CXFA_XMLLocaleTest,GetMeridiemName)153 TEST(CXFA_XMLLocaleTest, GetMeridiemName) {
154 auto locale = CreateLocaleHelper();
155 ASSERT_TRUE(locale != nullptr);
156
157 EXPECT_EQ(L"AM", locale->GetMeridiemName(true));
158 EXPECT_EQ(L"PM", locale->GetMeridiemName(false));
159 }
160
TEST(CXFA_XMLLocaleTest,GetEraName)161 TEST(CXFA_XMLLocaleTest, GetEraName) {
162 auto locale = CreateLocaleHelper();
163 ASSERT_TRUE(locale != nullptr);
164
165 EXPECT_EQ(L"AD", locale->GetEraName(true));
166 EXPECT_EQ(L"BC", locale->GetEraName(false));
167 }
168
TEST(CXFA_XMLLocaleTest,GetDatePattern)169 TEST(CXFA_XMLLocaleTest, GetDatePattern) {
170 auto locale = CreateLocaleHelper();
171 ASSERT_TRUE(locale != nullptr);
172
173 EXPECT_EQ(L"M/D/YY",
174 locale->GetDatePattern(FX_LOCALEDATETIMESUBCATEGORY_Short));
175 EXPECT_EQ(L"MMM D, YYYY",
176 locale->GetDatePattern(FX_LOCALEDATETIMESUBCATEGORY_Default));
177 EXPECT_EQ(L"MMM D, YYYY",
178 locale->GetDatePattern(FX_LOCALEDATETIMESUBCATEGORY_Medium));
179 EXPECT_EQ(L"EEEE, MMMM D, YYYY",
180 locale->GetDatePattern(FX_LOCALEDATETIMESUBCATEGORY_Full));
181 EXPECT_EQ(L"MMMM D, YYYY",
182 locale->GetDatePattern(FX_LOCALEDATETIMESUBCATEGORY_Long));
183 }
184
TEST(CXFA_XMLLocaleTest,GetTimePattern)185 TEST(CXFA_XMLLocaleTest, GetTimePattern) {
186 auto locale = CreateLocaleHelper();
187 ASSERT_TRUE(locale != nullptr);
188
189 EXPECT_EQ(L"h:MM A",
190 locale->GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY_Short));
191 EXPECT_EQ(L"h:MM:SS A",
192 locale->GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY_Default));
193 EXPECT_EQ(L"h:MM:SS A",
194 locale->GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY_Medium));
195 EXPECT_EQ(L"h:MM:SS A Z",
196 locale->GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY_Full));
197 EXPECT_EQ(L"h:MM:SS A Z",
198 locale->GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY_Long));
199 }
200
TEST(CXFA_XMLLocaleTest,GetNumPattern)201 TEST(CXFA_XMLLocaleTest, GetNumPattern) {
202 auto locale = CreateLocaleHelper();
203 ASSERT_TRUE(locale != nullptr);
204
205 EXPECT_EQ(L"z,zzz,zzz,zzz,zzz,zzz%",
206 locale->GetNumPattern(FX_LOCALENUMPATTERN_Percent));
207 EXPECT_EQ(L"$z,zzz,zzz,zzz,zzz,zz9.99",
208 locale->GetNumPattern(FX_LOCALENUMPATTERN_Currency));
209 EXPECT_EQ(L"z,zzz,zzz,zzz,zzz,zz9.zzz",
210 locale->GetNumPattern(FX_LOCALENUMPATTERN_Decimal));
211 EXPECT_EQ(L"z,zzz,zzz,zzz,zzz,zzz",
212 locale->GetNumPattern(FX_LOCALENUMPATTERN_Integer));
213 }
214