Add shortMonthLabels and shortStandAloneMonthLabels to Localizer
[WebKit-https.git] / Source / WebKit / chromium / tests / LocaleWinTest.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "LocaleWin.h"
33
34 #include "DateComponents.h"
35 #include <gtest/gtest.h>
36 #include <wtf/DateMath.h>
37 #include <wtf/MathExtras.h>
38 #include <wtf/PassOwnPtr.h>
39 #include <wtf/text/CString.h>
40
41 using namespace WebCore;
42 using namespace std;
43
44 class LocaleWinTest : public ::testing::Test {
45 protected:
46     enum {
47         January = 0, February, March,
48         April, May, June,
49         July, August, September,
50         October, November, December,
51     };
52
53     enum {
54         Sunday = 0, Monday, Tuesday,
55         Wednesday, Thursday, Friday,
56         Saturday,
57     };
58
59     // See http://msdn.microsoft.com/en-us/goglobal/bb964664.aspx
60     // Note that some locales are country-neutral.
61     enum {
62         ArabicEG = 0x0C01, // ar-eg
63         ChineseCN = 0x0804, // zh-cn
64         ChineseHK = 0x0C04, // zh-hk
65         ChineseTW = 0x0404, // zh-tw
66         German = 0x0407, // de
67         EnglishUS = 0x409, // en-us
68         FrenchFR = 0x40C, // fr
69         JapaneseJP = 0x411, // ja
70         KoreanKR = 0x0412, // ko
71         Persian = 0x0429, // fa
72         Spanish = 0x040A, // es
73     };
74
75     DateComponents dateComponents(int year, int month, int day)
76     {
77         DateComponents date;
78         date.setMillisecondsSinceEpochForDate(msForDate(year, month, day));
79         return date;
80     }
81
82     double msForDate(int year, int month, int day)
83     {
84         return dateToDaysFrom1970(year, month, day) * msPerDay;
85     }
86
87     String formatDate(LCID lcid, int year, int month, int day)
88     {
89         OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
90         return locale->formatDateTime(dateComponents(year, month, day));
91     }
92
93     double parseDate(LCID lcid, const String& dateString)
94     {
95         OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
96         return locale->parseDateTime(dateString, DateComponents::Date);
97     }
98
99 #if ENABLE(CALENDAR_PICKER)
100     String dateFormatText(LCID lcid)
101     {
102         OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
103         return locale->dateFormatText();
104     }
105
106     unsigned firstDayOfWeek(LCID lcid)
107     {
108         OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
109         return locale->firstDayOfWeek();
110     }
111
112     String monthLabel(LCID lcid, unsigned index)
113     {
114         OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
115         return locale->monthLabels()[index];
116     }
117
118     String weekDayShortLabel(LCID lcid, unsigned index)
119     {
120         OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
121         return locale->weekDayShortLabels()[index];
122     }
123
124     bool isRTL(LCID lcid)
125     {
126         OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
127         return locale->isRTL();
128     }
129 #endif
130
131 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
132     String monthFormat(LCID lcid)
133     {
134         OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
135         return locale->monthFormat();
136     }
137
138     String timeFormat(LCID lcid)
139     {
140         OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
141         return locale->timeFormat();
142     }
143
144     String shortTimeFormat(LCID lcid)
145     {
146         OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
147         return locale->shortTimeFormat();
148     }
149
150     String shortMonthLabel(LCID lcid, unsigned index)
151     {
152         OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
153         return locale->shortMonthLabels()[index];
154     }
155
156     String timeAMPMLabel(LCID lcid, unsigned index)
157     {
158         OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
159         return locale->timeAMPMLabels()[index];
160     }
161
162     String decimalSeparator(LCID lcid)
163     {
164         OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
165         return locale->localizedDecimalSeparator();
166     }
167 #endif
168 };
169
170 TEST_F(LocaleWinTest, TestLocalizedDateFormatText)
171 {
172     EXPECT_STREQ("year/month/day", LocaleWin::dateFormatText("y/M/d", "year", "month", "day").utf8().data());
173     EXPECT_STREQ("year/month/day", LocaleWin::dateFormatText("yy/MM/dd", "year", "month", "day").utf8().data());
174     EXPECT_STREQ("year/month/day", LocaleWin::dateFormatText("yyy/MMM/ddd", "year", "month", "day").utf8().data());
175     EXPECT_STREQ("year/month/day", LocaleWin::dateFormatText("yyyy/MMMM/dddd", "year", "month", "day").utf8().data());
176     EXPECT_STREQ("/month/day, year", LocaleWin::dateFormatText("/MM/dd, yyyy", "year", "month", "day").utf8().data());
177     EXPECT_STREQ("month/day, year=year.", LocaleWin::dateFormatText("MM/dd, 'year='yyyy.", "year", "month", "day").utf8().data());
178     EXPECT_STREQ("month-day 'year", LocaleWin::dateFormatText("MM-dd ''yyy", "year", "month", "day").utf8().data());
179     EXPECT_STREQ("month-day aaa'bbb year", LocaleWin::dateFormatText("MM-dd 'aaa''bbb' yyy", "year", "month", "day").utf8().data());
180     EXPECT_STREQ("year/month/day/year/month/day", LocaleWin::dateFormatText("yyyy/MMMM/dddd/yyyy/MMMM/dddd", "year", "month", "day").utf8().data());
181     EXPECT_STREQ("YY/mm/DD", LocaleWin::dateFormatText("YY/mm/DD", "year", "month", "day").utf8().data());
182 }
183
184 TEST_F(LocaleWinTest, TestParse)
185 {
186     OwnPtr<LocaleWin> locale = LocaleWin::create(EnglishUS);
187
188     EXPECT_EQ(msForDate(2012, April, 27), locale->parseDate("MM/dd/yy", 2012, "04/27/12"));
189     EXPECT_EQ(msForDate(2062, April, 27), locale->parseDate("MM/dd/yy", 2012, "04/27/62"));
190     EXPECT_EQ(msForDate(1963, April, 27), locale->parseDate("MM/dd/yy", 2012, "04/27/63"));
191     EXPECT_EQ(msForDate(2012, April, 27), locale->parseDate("MM/dd/yy", 2012, "4/27/2012"));
192     EXPECT_EQ(msForDate(2012, April, 27), locale->parseDate("MM/dd/yy", 2012, "Apr/27/2012"));
193     EXPECT_EQ(msForDate(2, April, 27), locale->parseDate("MM/d/yy", 2012, "April/27/2"));
194     EXPECT_EQ(msForDate(2, April, 27), locale->parseDate("MM/d/yy", 2012, "april/27/2"));
195     EXPECT_TRUE(isnan(locale->parseDate("MM/d/yy", 2012, "april/27")));
196     EXPECT_TRUE(isnan(locale->parseDate("MM/d/yy", 2012, "april/27/")));
197     EXPECT_TRUE(isnan(locale->parseDate("MM/d/yy", 2012, " april/27/")));
198
199     EXPECT_EQ(msForDate(12, April, 7), locale->parseDate("MMM/d/yyyy", 2012, "04/7/12"));
200     EXPECT_EQ(msForDate(62, April, 7), locale->parseDate("MMM/d/yyyy", 2012, "04/07/62"));
201     EXPECT_EQ(msForDate(63, April, 7), locale->parseDate("MMM/d/yyyy", 2012, "04/07/63"));
202     EXPECT_EQ(msForDate(2012, April, 7), locale->parseDate("MMM/d/yyyy", 2012, "4/7/2012"));
203     EXPECT_EQ(msForDate(2012, May, 7), locale->parseDate("MMM/d/yyyy", 2012, "May/007/2012"));
204     EXPECT_EQ(msForDate(2, May, 27), locale->parseDate("MM/d/yyyy", 2012, "May/0027/2"));
205     EXPECT_TRUE(isnan(locale->parseDate("MM/d/yyyy", 2012, "May///0027///2")));
206     EXPECT_TRUE(isnan(locale->parseDate("MM/d/yyyy", 2012, "Mayyyyyy/0027/2")));
207
208     EXPECT_EQ(msForDate(2012, April, 27), locale->parseDate("MMMM/dd/y", 2012, "04/27/2"));
209     EXPECT_EQ(msForDate(2017, April, 27), locale->parseDate("MMMM/dd/y", 2012, "04/27/7"));
210     EXPECT_EQ(msForDate(2008, April, 27), locale->parseDate("MMMM/dd/y", 2012, "04/27/8"));
211     EXPECT_EQ(msForDate(2012, April, 27), locale->parseDate("MMMM/dd/y", 2012, "4/27/2012"));
212     EXPECT_EQ(msForDate(2012, December, 27), locale->parseDate("MMMM/dd/y", 2012, "December/27/2012"));
213     EXPECT_EQ(msForDate(2012, November, 27), locale->parseDate("MMMM/d/y", 2012, "November/27/2"));
214     EXPECT_TRUE(isnan(locale->parseDate("MMMM/d/y", 2012, "November 27 2")));
215     EXPECT_TRUE(isnan(locale->parseDate("MMMM/d/y", 2012, "November 32 2")));
216     EXPECT_TRUE(isnan(locale->parseDate("MMMM/d/y", 2012, "-1/-1/-1")));
217
218     OwnPtr<LocaleWin> persian = LocaleWin::create(Persian);
219     // U+06F1 U+06F6 / U+06F0 U+06F8 / 2012
220     EXPECT_EQ(msForDate(2012, August, 16), persian->parseDate("dd/MM/yyyy", 2012, String::fromUTF8("\xDB\xB1\xDB\xB6/\xDB\xB0\xDB\xB8/2012")));
221 }
222
223 TEST_F(LocaleWinTest, formatDate)
224 {
225     EXPECT_STREQ("04/27/2005", formatDate(EnglishUS, 2005, April, 27).utf8().data());
226     EXPECT_STREQ("27/04/2005", formatDate(FrenchFR, 2005, April, 27).utf8().data());
227     EXPECT_STREQ("2005/04/27", formatDate(JapaneseJP, 2005, April, 27).utf8().data());
228 }
229
230 TEST_F(LocaleWinTest, parseDate)
231 {
232     EXPECT_EQ(msForDate(2005, April, 27), parseDate(EnglishUS, "April/27/2005"));
233     EXPECT_EQ(msForDate(2005, April, 27), parseDate(FrenchFR, "27/avril/2005"));
234     EXPECT_EQ(msForDate(2005, April, 27), parseDate(JapaneseJP, "2005/04/27"));
235 }
236
237 #if ENABLE(CALENDAR_PICKER)
238 TEST_F(LocaleWinTest, dateFormatText)
239 {
240     EXPECT_STREQ("Month/Day/Year", dateFormatText(EnglishUS).utf8().data());
241     EXPECT_STREQ("Day/Month/Year", dateFormatText(FrenchFR).utf8().data());
242     EXPECT_STREQ("Year/Month/Day", dateFormatText(JapaneseJP).utf8().data());
243 }
244
245 TEST_F(LocaleWinTest, firstDayOfWeek)
246 {
247     EXPECT_EQ(Sunday, firstDayOfWeek(EnglishUS));
248     EXPECT_EQ(Monday, firstDayOfWeek(FrenchFR));
249     EXPECT_EQ(Sunday, firstDayOfWeek(JapaneseJP));
250 }
251
252 TEST_F(LocaleWinTest, monthLabels)
253 {
254     EXPECT_STREQ("January", monthLabel(EnglishUS, January).utf8().data());
255     EXPECT_STREQ("June", monthLabel(EnglishUS, June).utf8().data());
256     EXPECT_STREQ("December", monthLabel(EnglishUS, December).utf8().data());
257
258     EXPECT_STREQ("janvier", monthLabel(FrenchFR, January).utf8().data());
259     EXPECT_STREQ("juin", monthLabel(FrenchFR, June).utf8().data());
260     EXPECT_STREQ("d\xC3\xA9" "cembre", monthLabel(FrenchFR, December).utf8().data());
261
262     EXPECT_STREQ("1\xE6\x9C\x88", monthLabel(JapaneseJP, January).utf8().data());
263     EXPECT_STREQ("6\xE6\x9C\x88", monthLabel(JapaneseJP, June).utf8().data());
264     EXPECT_STREQ("12\xE6\x9C\x88", monthLabel(JapaneseJP, December).utf8().data());
265 }
266
267 TEST_F(LocaleWinTest, weekDayShortLabels)
268 {
269     EXPECT_STREQ("Sun", weekDayShortLabel(EnglishUS, Sunday).utf8().data());
270     EXPECT_STREQ("Wed", weekDayShortLabel(EnglishUS, Wednesday).utf8().data());
271     EXPECT_STREQ("Sat", weekDayShortLabel(EnglishUS, Saturday).utf8().data());
272
273     EXPECT_STREQ("dim.", weekDayShortLabel(FrenchFR, Sunday).utf8().data());
274     EXPECT_STREQ("mer.", weekDayShortLabel(FrenchFR, Wednesday).utf8().data());
275     EXPECT_STREQ("sam.", weekDayShortLabel(FrenchFR, Saturday).utf8().data());
276
277     EXPECT_STREQ("\xE6\x97\xA5", weekDayShortLabel(JapaneseJP, Sunday).utf8().data());
278     EXPECT_STREQ("\xE6\xB0\xB4", weekDayShortLabel(JapaneseJP, Wednesday).utf8().data());
279     EXPECT_STREQ("\xE5\x9C\x9F", weekDayShortLabel(JapaneseJP, Saturday).utf8().data());
280 }
281
282 TEST_F(LocaleWinTest, isRTL)
283 {
284     EXPECT_TRUE(isRTL(ArabicEG));
285     EXPECT_FALSE(isRTL(EnglishUS));
286 }
287
288 #endif
289
290 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
291 TEST_F(LocaleWinTest, dateFormat)
292 {
293     EXPECT_STREQ("y'-'M'-'d", LocaleWin::dateFormat("y-M-d").utf8().data());
294     EXPECT_STREQ("''yy'-'''MM'''-'dd", LocaleWin::dateFormat("''yy-''MM''-dd").utf8().data());
295     EXPECT_STREQ("yyyy'-''''-'MMM'''''-'dd", LocaleWin::dateFormat("yyyy-''''-MMM''''-dd").utf8().data());
296     EXPECT_STREQ("yyyy'-'''''MMMM'-'dd", LocaleWin::dateFormat("yyyy-''''MMMM-dd").utf8().data());
297 }
298
299 TEST_F(LocaleWinTest, monthFormat)
300 {
301     EXPECT_STREQ("MMMM', 'yyyy", monthFormat(EnglishUS).utf8().data());
302     EXPECT_STREQ("MMMM' 'yyyy", monthFormat(FrenchFR).utf8().data());
303     EXPECT_STREQ("yyyy'\xE5\xB9\xB4'M'\xE6\x9C\x88'", monthFormat(JapaneseJP).utf8().data());
304 }
305
306 TEST_F(LocaleWinTest, timeFormat)
307 {
308     EXPECT_STREQ("h:mm:ss a", timeFormat(EnglishUS).utf8().data());
309     EXPECT_STREQ("HH:mm:ss", timeFormat(FrenchFR).utf8().data());
310     EXPECT_STREQ("H:mm:ss", timeFormat(JapaneseJP).utf8().data());
311 }
312
313 TEST_F(LocaleWinTest, shortTimeFormat)
314 {
315     EXPECT_STREQ("h:mm:ss a", shortTimeFormat(EnglishUS).utf8().data());
316     EXPECT_STREQ("HH:mm:ss", shortTimeFormat(FrenchFR).utf8().data());
317     EXPECT_STREQ("H:mm:ss", shortTimeFormat(JapaneseJP).utf8().data());
318 }
319
320 TEST_F(LocaleWinTest, shortMonthLabels)
321 {
322     EXPECT_STREQ("Jan", shortMonthLabel(EnglishUS, 0).utf8().data());
323     EXPECT_STREQ("Dec", shortMonthLabel(EnglishUS, 11).utf8().data());
324     EXPECT_STREQ("janv.", shortMonthLabel(FrenchFR, 0).utf8().data());
325     EXPECT_STREQ("d\xC3\xA9" "c.", shortMonthLabel(FrenchFR, 11).utf8().data());
326     EXPECT_STREQ("1", shortMonthLabel(JapaneseJP, 0).utf8().data());
327     EXPECT_STREQ("12", shortMonthLabel(JapaneseJP, 11).utf8().data());
328 }
329
330 TEST_F(LocaleWinTest, timeAMPMLabels)
331 {
332     EXPECT_STREQ("AM", timeAMPMLabel(EnglishUS, 0).utf8().data());
333     EXPECT_STREQ("PM", timeAMPMLabel(EnglishUS, 1).utf8().data());
334
335     EXPECT_STREQ("", timeAMPMLabel(FrenchFR, 0).utf8().data());
336     EXPECT_STREQ("", timeAMPMLabel(FrenchFR, 1).utf8().data());
337
338     EXPECT_STREQ("\xE5\x8D\x88\xE5\x89\x8D", timeAMPMLabel(JapaneseJP, 0).utf8().data());
339     EXPECT_STREQ("\xE5\x8D\x88\xE5\xBE\x8C", timeAMPMLabel(JapaneseJP, 1).utf8().data());
340 }
341
342 TEST_F(LocaleWinTest, decimalSeparator)
343 {
344     EXPECT_STREQ(".", decimalSeparator(EnglishUS).utf8().data());
345     EXPECT_STREQ(",", decimalSeparator(FrenchFR).utf8().data());
346 }
347 #endif
348
349 static void testNumberIsReversible(LCID lcid, const char* original, const char* shouldHave = 0)
350 {
351     OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
352     String localized = locale->convertToLocalizedNumber(original);
353     if (shouldHave)
354         EXPECT_TRUE(localized.contains(shouldHave));
355     String converted = locale->convertFromLocalizedNumber(localized);
356     EXPECT_STREQ(original, converted.utf8().data());
357 }
358
359 void testNumbers(LCID lcid)
360 {
361     testNumberIsReversible(lcid, "123456789012345678901234567890");
362     testNumberIsReversible(lcid, "-123.456");
363     testNumberIsReversible(lcid, ".456");
364     testNumberIsReversible(lcid, "-0.456");
365 }
366
367 TEST_F(LocaleWinTest, localizedNumberRoundTrip)
368 {
369     testNumberIsReversible(EnglishUS, "123456789012345678901234567890");
370     testNumberIsReversible(EnglishUS, "-123.456", ".");
371     testNumberIsReversible(EnglishUS, ".456", ".");
372     testNumberIsReversible(EnglishUS, "-0.456", ".");
373
374     testNumberIsReversible(FrenchFR, "123456789012345678901234567890");
375     testNumberIsReversible(FrenchFR, "-123.456", ",");
376     testNumberIsReversible(FrenchFR, ".456", ",");
377     testNumberIsReversible(FrenchFR, "-0.456", ",");
378
379     // Test some of major locales.
380     testNumbers(ArabicEG);
381     testNumbers(German);
382     testNumbers(Spanish);
383     testNumbers(Persian);
384     testNumbers(JapaneseJP);
385     testNumbers(KoreanKR);
386     testNumbers(ChineseCN);
387     testNumbers(ChineseHK);
388     testNumbers(ChineseTW);
389 }