Localized date should be based on LDML
[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 timeFormat(LCID lcid)
133     {
134         OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
135         return locale->timeFormat();
136     }
137
138     String shortTimeFormat(LCID lcid)
139     {
140         OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
141         return locale->shortTimeFormat();
142     }
143
144     String timeAMPMLabel(LCID lcid, unsigned index)
145     {
146         OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
147         return locale->timeAMPMLabels()[index];
148     }
149
150     String decimalSeparator(LCID lcid)
151     {
152         OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
153         return locale->localizedDecimalSeparator();
154     }
155 #endif
156 };
157
158 TEST_F(LocaleWinTest, TestLocalizedDateFormatText)
159 {
160     EXPECT_STREQ("year/month/day", LocaleWin::dateFormatText("y/M/d", "year", "month", "day").utf8().data());
161     EXPECT_STREQ("year/month/day", LocaleWin::dateFormatText("yy/MM/dd", "year", "month", "day").utf8().data());
162     EXPECT_STREQ("year/month/day", LocaleWin::dateFormatText("yyy/MMM/ddd", "year", "month", "day").utf8().data());
163     EXPECT_STREQ("year/month/day", LocaleWin::dateFormatText("yyyy/MMMM/dddd", "year", "month", "day").utf8().data());
164     EXPECT_STREQ("/month/day, year", LocaleWin::dateFormatText("/MM/dd, yyyy", "year", "month", "day").utf8().data());
165     EXPECT_STREQ("month/day, year=year.", LocaleWin::dateFormatText("MM/dd, 'year='yyyy.", "year", "month", "day").utf8().data());
166     EXPECT_STREQ("month-day 'year", LocaleWin::dateFormatText("MM-dd ''yyy", "year", "month", "day").utf8().data());
167     EXPECT_STREQ("month-day aaa'bbb year", LocaleWin::dateFormatText("MM-dd 'aaa''bbb' yyy", "year", "month", "day").utf8().data());
168     EXPECT_STREQ("year/month/day/year/month/day", LocaleWin::dateFormatText("yyyy/MMMM/dddd/yyyy/MMMM/dddd", "year", "month", "day").utf8().data());
169     EXPECT_STREQ("YY/mm/DD", LocaleWin::dateFormatText("YY/mm/DD", "year", "month", "day").utf8().data());
170 }
171
172 TEST_F(LocaleWinTest, TestParse)
173 {
174     OwnPtr<LocaleWin> locale = LocaleWin::create(EnglishUS);
175
176     EXPECT_EQ(msForDate(2012, April, 27), locale->parseDate("MM/dd/yy", 2012, "04/27/12"));
177     EXPECT_EQ(msForDate(2062, April, 27), locale->parseDate("MM/dd/yy", 2012, "04/27/62"));
178     EXPECT_EQ(msForDate(1963, April, 27), locale->parseDate("MM/dd/yy", 2012, "04/27/63"));
179     EXPECT_EQ(msForDate(2012, April, 27), locale->parseDate("MM/dd/yy", 2012, "4/27/2012"));
180     EXPECT_EQ(msForDate(2012, April, 27), locale->parseDate("MM/dd/yy", 2012, "Apr/27/2012"));
181     EXPECT_EQ(msForDate(2, April, 27), locale->parseDate("MM/d/yy", 2012, "April/27/2"));
182     EXPECT_EQ(msForDate(2, April, 27), locale->parseDate("MM/d/yy", 2012, "april/27/2"));
183     EXPECT_TRUE(isnan(locale->parseDate("MM/d/yy", 2012, "april/27")));
184     EXPECT_TRUE(isnan(locale->parseDate("MM/d/yy", 2012, "april/27/")));
185     EXPECT_TRUE(isnan(locale->parseDate("MM/d/yy", 2012, " april/27/")));
186
187     EXPECT_EQ(msForDate(12, April, 7), locale->parseDate("MMM/d/yyyy", 2012, "04/7/12"));
188     EXPECT_EQ(msForDate(62, April, 7), locale->parseDate("MMM/d/yyyy", 2012, "04/07/62"));
189     EXPECT_EQ(msForDate(63, April, 7), locale->parseDate("MMM/d/yyyy", 2012, "04/07/63"));
190     EXPECT_EQ(msForDate(2012, April, 7), locale->parseDate("MMM/d/yyyy", 2012, "4/7/2012"));
191     EXPECT_EQ(msForDate(2012, May, 7), locale->parseDate("MMM/d/yyyy", 2012, "May/007/2012"));
192     EXPECT_EQ(msForDate(2, May, 27), locale->parseDate("MM/d/yyyy", 2012, "May/0027/2"));
193     EXPECT_TRUE(isnan(locale->parseDate("MM/d/yyyy", 2012, "May///0027///2")));
194     EXPECT_TRUE(isnan(locale->parseDate("MM/d/yyyy", 2012, "Mayyyyyy/0027/2")));
195
196     EXPECT_EQ(msForDate(2012, April, 27), locale->parseDate("MMMM/dd/y", 2012, "04/27/2"));
197     EXPECT_EQ(msForDate(2017, April, 27), locale->parseDate("MMMM/dd/y", 2012, "04/27/7"));
198     EXPECT_EQ(msForDate(2008, April, 27), locale->parseDate("MMMM/dd/y", 2012, "04/27/8"));
199     EXPECT_EQ(msForDate(2012, April, 27), locale->parseDate("MMMM/dd/y", 2012, "4/27/2012"));
200     EXPECT_EQ(msForDate(2012, December, 27), locale->parseDate("MMMM/dd/y", 2012, "December/27/2012"));
201     EXPECT_EQ(msForDate(2012, November, 27), locale->parseDate("MMMM/d/y", 2012, "November/27/2"));
202     EXPECT_TRUE(isnan(locale->parseDate("MMMM/d/y", 2012, "November 27 2")));
203     EXPECT_TRUE(isnan(locale->parseDate("MMMM/d/y", 2012, "November 32 2")));
204     EXPECT_TRUE(isnan(locale->parseDate("MMMM/d/y", 2012, "-1/-1/-1")));
205
206     OwnPtr<LocaleWin> persian = LocaleWin::create(Persian);
207     // U+06F1 U+06F6 / U+06F0 U+06F8 / 2012
208     EXPECT_EQ(msForDate(2012, August, 16), persian->parseDate("dd/MM/yyyy", 2012, String::fromUTF8("\xDB\xB1\xDB\xB6/\xDB\xB0\xDB\xB8/2012")));
209 }
210
211 TEST_F(LocaleWinTest, formatDate)
212 {
213     EXPECT_STREQ("04/27/2005", formatDate(EnglishUS, 2005, April, 27).utf8().data());
214     EXPECT_STREQ("27/04/2005", formatDate(FrenchFR, 2005, April, 27).utf8().data());
215     EXPECT_STREQ("2005/04/27", formatDate(JapaneseJP, 2005, April, 27).utf8().data());
216 }
217
218 TEST_F(LocaleWinTest, parseDate)
219 {
220     EXPECT_EQ(msForDate(2005, April, 27), parseDate(EnglishUS, "April/27/2005"));
221     EXPECT_EQ(msForDate(2005, April, 27), parseDate(FrenchFR, "27/avril/2005"));
222     EXPECT_EQ(msForDate(2005, April, 27), parseDate(JapaneseJP, "2005/04/27"));
223 }
224
225 #if ENABLE(CALENDAR_PICKER)
226 TEST_F(LocaleWinTest, dateFormatText)
227 {
228     EXPECT_STREQ("Month/Day/Year", dateFormatText(EnglishUS).utf8().data());
229     EXPECT_STREQ("Day/Month/Year", dateFormatText(FrenchFR).utf8().data());
230     EXPECT_STREQ("Year/Month/Day", dateFormatText(JapaneseJP).utf8().data());
231 }
232
233 TEST_F(LocaleWinTest, firstDayOfWeek)
234 {
235     EXPECT_EQ(Sunday, firstDayOfWeek(EnglishUS));
236     EXPECT_EQ(Monday, firstDayOfWeek(FrenchFR));
237     EXPECT_EQ(Sunday, firstDayOfWeek(JapaneseJP));
238 }
239
240 TEST_F(LocaleWinTest, monthLabels)
241 {
242     EXPECT_STREQ("January", monthLabel(EnglishUS, January).utf8().data());
243     EXPECT_STREQ("June", monthLabel(EnglishUS, June).utf8().data());
244     EXPECT_STREQ("December", monthLabel(EnglishUS, December).utf8().data());
245
246     EXPECT_STREQ("janvier", monthLabel(FrenchFR, January).utf8().data());
247     EXPECT_STREQ("juin", monthLabel(FrenchFR, June).utf8().data());
248     EXPECT_STREQ("d\xC3\xA9" "cembre", monthLabel(FrenchFR, December).utf8().data());
249
250     EXPECT_STREQ("1\xE6\x9C\x88", monthLabel(JapaneseJP, January).utf8().data());
251     EXPECT_STREQ("6\xE6\x9C\x88", monthLabel(JapaneseJP, June).utf8().data());
252     EXPECT_STREQ("12\xE6\x9C\x88", monthLabel(JapaneseJP, December).utf8().data());
253 }
254
255 TEST_F(LocaleWinTest, weekDayShortLabels)
256 {
257     EXPECT_STREQ("Sun", weekDayShortLabel(EnglishUS, Sunday).utf8().data());
258     EXPECT_STREQ("Wed", weekDayShortLabel(EnglishUS, Wednesday).utf8().data());
259     EXPECT_STREQ("Sat", weekDayShortLabel(EnglishUS, Saturday).utf8().data());
260
261     EXPECT_STREQ("dim.", weekDayShortLabel(FrenchFR, Sunday).utf8().data());
262     EXPECT_STREQ("mer.", weekDayShortLabel(FrenchFR, Wednesday).utf8().data());
263     EXPECT_STREQ("sam.", weekDayShortLabel(FrenchFR, Saturday).utf8().data());
264
265     EXPECT_STREQ("\xE6\x97\xA5", weekDayShortLabel(JapaneseJP, Sunday).utf8().data());
266     EXPECT_STREQ("\xE6\xB0\xB4", weekDayShortLabel(JapaneseJP, Wednesday).utf8().data());
267     EXPECT_STREQ("\xE5\x9C\x9F", weekDayShortLabel(JapaneseJP, Saturday).utf8().data());
268 }
269
270 TEST_F(LocaleWinTest, isRTL)
271 {
272     EXPECT_TRUE(isRTL(ArabicEG));
273     EXPECT_FALSE(isRTL(EnglishUS));
274 }
275
276 #endif
277
278 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
279 TEST_F(LocaleWinTest, dateFormat)
280 {
281     EXPECT_STREQ("y'-'M'-'d", LocaleWin::dateFormat("y-M-d").utf8().data());
282     EXPECT_STREQ("''yy'-'''MM'''-'dd", LocaleWin::dateFormat("''yy-''MM''-dd").utf8().data());
283     EXPECT_STREQ("yyyy'-''''-'MMM'''''-'dd", LocaleWin::dateFormat("yyyy-''''-MMM''''-dd").utf8().data());
284     EXPECT_STREQ("yyyy'-'''''MMMM'-'dd", LocaleWin::dateFormat("yyyy-''''MMMM-dd").utf8().data());
285 }
286
287 TEST_F(LocaleWinTest, timeFormat)
288 {
289     EXPECT_STREQ("h:mm:ss a", timeFormat(EnglishUS).utf8().data());
290     EXPECT_STREQ("HH:mm:ss", timeFormat(FrenchFR).utf8().data());
291     EXPECT_STREQ("H:mm:ss", timeFormat(JapaneseJP).utf8().data());
292 }
293
294 TEST_F(LocaleWinTest, shortTimeFormat)
295 {
296     EXPECT_STREQ("h:mm:ss a", shortTimeFormat(EnglishUS).utf8().data());
297     EXPECT_STREQ("HH:mm:ss", shortTimeFormat(FrenchFR).utf8().data());
298     EXPECT_STREQ("H:mm:ss", shortTimeFormat(JapaneseJP).utf8().data());
299 }
300
301 TEST_F(LocaleWinTest, timeAMPMLabels)
302 {
303     EXPECT_STREQ("AM", timeAMPMLabel(EnglishUS, 0).utf8().data());
304     EXPECT_STREQ("PM", timeAMPMLabel(EnglishUS, 1).utf8().data());
305
306     EXPECT_STREQ("", timeAMPMLabel(FrenchFR, 0).utf8().data());
307     EXPECT_STREQ("", timeAMPMLabel(FrenchFR, 1).utf8().data());
308
309     EXPECT_STREQ("\xE5\x8D\x88\xE5\x89\x8D", timeAMPMLabel(JapaneseJP, 0).utf8().data());
310     EXPECT_STREQ("\xE5\x8D\x88\xE5\xBE\x8C", timeAMPMLabel(JapaneseJP, 1).utf8().data());
311 }
312
313 TEST_F(LocaleWinTest, decimalSeparator)
314 {
315     EXPECT_STREQ(".", decimalSeparator(EnglishUS).utf8().data());
316     EXPECT_STREQ(",", decimalSeparator(FrenchFR).utf8().data());
317 }
318 #endif
319
320 static void testNumberIsReversible(LCID lcid, const char* original, const char* shouldHave = 0)
321 {
322     OwnPtr<LocaleWin> locale = LocaleWin::create(lcid);
323     String localized = locale->convertToLocalizedNumber(original);
324     if (shouldHave)
325         EXPECT_TRUE(localized.contains(shouldHave));
326     String converted = locale->convertFromLocalizedNumber(localized);
327     EXPECT_STREQ(original, converted.utf8().data());
328 }
329
330 void testNumbers(LCID lcid)
331 {
332     testNumberIsReversible(lcid, "123456789012345678901234567890");
333     testNumberIsReversible(lcid, "-123.456");
334     testNumberIsReversible(lcid, ".456");
335     testNumberIsReversible(lcid, "-0.456");
336 }
337
338 TEST_F(LocaleWinTest, localizedNumberRoundTrip)
339 {
340     testNumberIsReversible(EnglishUS, "123456789012345678901234567890");
341     testNumberIsReversible(EnglishUS, "-123.456", ".");
342     testNumberIsReversible(EnglishUS, ".456", ".");
343     testNumberIsReversible(EnglishUS, "-0.456", ".");
344
345     testNumberIsReversible(FrenchFR, "123456789012345678901234567890");
346     testNumberIsReversible(FrenchFR, "-123.456", ",");
347     testNumberIsReversible(FrenchFR, ".456", ",");
348     testNumberIsReversible(FrenchFR, "-0.456", ",");
349
350     // Test some of major locales.
351     testNumbers(ArabicEG);
352     testNumbers(German);
353     testNumbers(Spanish);
354     testNumbers(Persian);
355     testNumbers(JapaneseJP);
356     testNumbers(KoreanKR);
357     testNumbers(ChineseCN);
358     testNumbers(ChineseHK);
359     testNumbers(ChineseTW);
360 }