Made WebRTC tests report JS errors better.
[chromium-blink-merge.git] / base / i18n / time_formatting_unittest.cc
blob03a3aa3ec77f957c3f26d501e1425f9eb9c2389d
1 // Copyright (c) 2011 The Chromium 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.
5 #include "base/i18n/time_formatting.h"
7 #include "base/i18n/rtl.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/time/time.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/icu/source/common/unicode/uversion.h"
13 namespace base {
14 namespace {
16 const Time::Exploded kTestDateTimeExploded = {
17 2011, 4, 6, 30, // Sat, Apr 30, 2011
18 15, 42, 7, 0 // 15:42:07.000
21 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault12h) {
22 // Test for a locale defaulted to 12h clock.
23 // As an instance, we use third_party/icu/source/data/locales/en.txt.
24 i18n::SetICUDefaultLocale("en_US");
26 Time time(Time::FromLocalExploded(kTestDateTimeExploded));
27 string16 clock24h(ASCIIToUTF16("15:42"));
28 string16 clock12h_pm(ASCIIToUTF16("3:42 PM"));
29 string16 clock12h(ASCIIToUTF16("3:42"));
31 // The default is 12h clock.
32 EXPECT_EQ(clock12h_pm, TimeFormatTimeOfDay(time));
33 EXPECT_EQ(k12HourClock, GetHourClockType());
34 // k{Keep,Drop}AmPm should not affect for 24h clock.
35 EXPECT_EQ(clock24h,
36 TimeFormatTimeOfDayWithHourClockType(time,
37 k24HourClock,
38 kKeepAmPm));
39 EXPECT_EQ(clock24h,
40 TimeFormatTimeOfDayWithHourClockType(time,
41 k24HourClock,
42 kDropAmPm));
43 // k{Keep,Drop}AmPm affects for 12h clock.
44 EXPECT_EQ(clock12h_pm,
45 TimeFormatTimeOfDayWithHourClockType(time,
46 k12HourClock,
47 kKeepAmPm));
48 EXPECT_EQ(clock12h,
49 TimeFormatTimeOfDayWithHourClockType(time,
50 k12HourClock,
51 kDropAmPm));
54 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault24h) {
55 // Test for a locale defaulted to 24h clock.
56 // As an instance, we use third_party/icu/source/data/locales/en_GB.txt.
57 i18n::SetICUDefaultLocale("en_GB");
59 Time time(Time::FromLocalExploded(kTestDateTimeExploded));
60 string16 clock24h(ASCIIToUTF16("15:42"));
61 #if U_ICU_VERSION_MAJOR_NUM >= 50
62 string16 clock12h_pm(ASCIIToUTF16("3:42 pm"));
63 #else
64 // TODO(phajdan.jr): Clean up after bundled ICU gets updated to 50.
65 string16 clock12h_pm(ASCIIToUTF16("3:42 PM"));
66 #endif
67 string16 clock12h(ASCIIToUTF16("3:42"));
69 // The default is 24h clock.
70 EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time));
71 EXPECT_EQ(k24HourClock, GetHourClockType());
72 // k{Keep,Drop}AmPm should not affect for 24h clock.
73 EXPECT_EQ(clock24h,
74 TimeFormatTimeOfDayWithHourClockType(time,
75 k24HourClock,
76 kKeepAmPm));
77 EXPECT_EQ(clock24h,
78 TimeFormatTimeOfDayWithHourClockType(time,
79 k24HourClock,
80 kDropAmPm));
81 // k{Keep,Drop}AmPm affects for 12h clock.
82 EXPECT_EQ(clock12h_pm,
83 TimeFormatTimeOfDayWithHourClockType(time,
84 k12HourClock,
85 kKeepAmPm));
86 EXPECT_EQ(clock12h,
87 TimeFormatTimeOfDayWithHourClockType(time,
88 k12HourClock,
89 kDropAmPm));
92 TEST(TimeFormattingTest, TimeFormatTimeOfDayJP) {
93 // Test for a locale that uses different mark than "AM" and "PM".
94 // As an instance, we use third_party/icu/source/data/locales/ja.txt.
95 i18n::SetICUDefaultLocale("ja_JP");
97 Time time(Time::FromLocalExploded(kTestDateTimeExploded));
98 string16 clock24h(ASCIIToUTF16("15:42"));
99 string16 clock12h_pm(WideToUTF16(L"\x5348\x5f8c" L"3:42"));
100 string16 clock12h(ASCIIToUTF16("3:42"));
102 // The default is 24h clock.
103 EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time));
104 EXPECT_EQ(k24HourClock, GetHourClockType());
105 // k{Keep,Drop}AmPm should not affect for 24h clock.
106 EXPECT_EQ(clock24h,
107 TimeFormatTimeOfDayWithHourClockType(time,
108 k24HourClock,
109 kKeepAmPm));
110 EXPECT_EQ(clock24h,
111 TimeFormatTimeOfDayWithHourClockType(time,
112 k24HourClock,
113 kDropAmPm));
114 // k{Keep,Drop}AmPm affects for 12h clock.
115 EXPECT_EQ(clock12h_pm,
116 TimeFormatTimeOfDayWithHourClockType(time,
117 k12HourClock,
118 kKeepAmPm));
119 EXPECT_EQ(clock12h,
120 TimeFormatTimeOfDayWithHourClockType(time,
121 k12HourClock,
122 kDropAmPm));
125 TEST(TimeFormattingTest, TimeFormatDateUS) {
126 // See third_party/icu/source/data/locales/en.txt.
127 // The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy".
128 i18n::SetICUDefaultLocale("en_US");
130 Time time(Time::FromLocalExploded(kTestDateTimeExploded));
132 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time));
133 EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time));
135 #if U_ICU_VERSION_MAJOR_NUM >= 50
136 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"),
137 TimeFormatShortDateAndTime(time));
138 #else
139 // TODO(phajdan.jr): Clean up after bundled ICU gets updated to 50.
140 EXPECT_EQ(ASCIIToUTF16("4/30/11 3:42:07 PM"),
141 TimeFormatShortDateAndTime(time));
142 #endif
144 #if U_ICU_VERSION_MAJOR_NUM >= 50
145 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 at 3:42:07 PM"),
146 TimeFormatFriendlyDateAndTime(time));
147 #else
148 // TODO(phajdan.jr): Clean up after bundled ICU gets updated to 50.
149 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 3:42:07 PM"),
150 TimeFormatFriendlyDateAndTime(time));
151 #endif
153 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"),
154 TimeFormatFriendlyDate(time));
157 TEST(TimeFormattingTest, TimeFormatDateGB) {
158 // See third_party/icu/source/data/locales/en_GB.txt.
159 // The date patterns are "EEEE, d MMMM y", "d MMM y", and "dd/MM/yyyy".
160 i18n::SetICUDefaultLocale("en_GB");
162 Time time(Time::FromLocalExploded(kTestDateTimeExploded));
164 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time));
165 EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time));
166 EXPECT_EQ(ASCIIToUTF16("30/04/2011 15:42:07"),
167 TimeFormatShortDateAndTime(time));
168 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 15:42:07"),
169 TimeFormatFriendlyDateAndTime(time));
170 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"),
171 TimeFormatFriendlyDate(time));
174 } // namespace
175 } // namespace base