Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / base / i18n / time_formatting_unittest.cc
blobdf0c1ed5ea1ec121b7e7d77b1cfbf1e89e36f088
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/memory/scoped_ptr.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/time/time.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/icu/source/common/unicode/uversion.h"
13 #include "third_party/icu/source/i18n/unicode/calendar.h"
14 #include "third_party/icu/source/i18n/unicode/timezone.h"
16 namespace base {
17 namespace {
19 const Time::Exploded kTestDateTimeExploded = {
20 2011, 4, 6, 30, // Sat, Apr 30, 2011
21 15, 42, 7, 0 // 15:42:07.000
24 base::string16 GetShortTimeZone() {
25 scoped_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
26 icu::UnicodeString name;
27 zone->getDisplayName(true, icu::TimeZone::SHORT, name);
28 return base::string16(name.getBuffer(), name.length());
31 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault12h) {
32 // Test for a locale defaulted to 12h clock.
33 // As an instance, we use third_party/icu/source/data/locales/en.txt.
34 i18n::SetICUDefaultLocale("en_US");
36 Time time(Time::FromLocalExploded(kTestDateTimeExploded));
37 string16 clock24h(ASCIIToUTF16("15:42"));
38 string16 clock12h_pm(ASCIIToUTF16("3:42 PM"));
39 string16 clock12h(ASCIIToUTF16("3:42"));
41 // The default is 12h clock.
42 EXPECT_EQ(clock12h_pm, TimeFormatTimeOfDay(time));
43 EXPECT_EQ(k12HourClock, GetHourClockType());
44 // k{Keep,Drop}AmPm should not affect for 24h clock.
45 EXPECT_EQ(clock24h,
46 TimeFormatTimeOfDayWithHourClockType(time,
47 k24HourClock,
48 kKeepAmPm));
49 EXPECT_EQ(clock24h,
50 TimeFormatTimeOfDayWithHourClockType(time,
51 k24HourClock,
52 kDropAmPm));
53 // k{Keep,Drop}AmPm affects for 12h clock.
54 EXPECT_EQ(clock12h_pm,
55 TimeFormatTimeOfDayWithHourClockType(time,
56 k12HourClock,
57 kKeepAmPm));
58 EXPECT_EQ(clock12h,
59 TimeFormatTimeOfDayWithHourClockType(time,
60 k12HourClock,
61 kDropAmPm));
64 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault24h) {
65 // Test for a locale defaulted to 24h clock.
66 // As an instance, we use third_party/icu/source/data/locales/en_GB.txt.
67 i18n::SetICUDefaultLocale("en_GB");
69 Time time(Time::FromLocalExploded(kTestDateTimeExploded));
70 string16 clock24h(ASCIIToUTF16("15:42"));
71 string16 clock12h_pm(ASCIIToUTF16("3:42 pm"));
72 string16 clock12h(ASCIIToUTF16("3:42"));
74 // The default is 24h clock.
75 EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time));
76 EXPECT_EQ(k24HourClock, GetHourClockType());
77 // k{Keep,Drop}AmPm should not affect for 24h clock.
78 EXPECT_EQ(clock24h,
79 TimeFormatTimeOfDayWithHourClockType(time,
80 k24HourClock,
81 kKeepAmPm));
82 EXPECT_EQ(clock24h,
83 TimeFormatTimeOfDayWithHourClockType(time,
84 k24HourClock,
85 kDropAmPm));
86 // k{Keep,Drop}AmPm affects for 12h clock.
87 EXPECT_EQ(clock12h_pm,
88 TimeFormatTimeOfDayWithHourClockType(time,
89 k12HourClock,
90 kKeepAmPm));
91 EXPECT_EQ(clock12h,
92 TimeFormatTimeOfDayWithHourClockType(time,
93 k12HourClock,
94 kDropAmPm));
97 TEST(TimeFormattingTest, TimeFormatTimeOfDayJP) {
98 // Test for a locale that uses different mark than "AM" and "PM".
99 // As an instance, we use third_party/icu/source/data/locales/ja.txt.
100 i18n::SetICUDefaultLocale("ja_JP");
102 Time time(Time::FromLocalExploded(kTestDateTimeExploded));
103 string16 clock24h(ASCIIToUTF16("15:42"));
104 string16 clock12h_pm(WideToUTF16(L"\x5348\x5f8c" L"3:42"));
105 string16 clock12h(ASCIIToUTF16("3:42"));
107 // The default is 24h clock.
108 EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time));
109 EXPECT_EQ(k24HourClock, GetHourClockType());
110 // k{Keep,Drop}AmPm should not affect for 24h clock.
111 EXPECT_EQ(clock24h,
112 TimeFormatTimeOfDayWithHourClockType(time,
113 k24HourClock,
114 kKeepAmPm));
115 EXPECT_EQ(clock24h,
116 TimeFormatTimeOfDayWithHourClockType(time,
117 k24HourClock,
118 kDropAmPm));
119 // k{Keep,Drop}AmPm affects for 12h clock.
120 EXPECT_EQ(clock12h_pm,
121 TimeFormatTimeOfDayWithHourClockType(time,
122 k12HourClock,
123 kKeepAmPm));
124 EXPECT_EQ(clock12h,
125 TimeFormatTimeOfDayWithHourClockType(time,
126 k12HourClock,
127 kDropAmPm));
130 TEST(TimeFormattingTest, TimeFormatDateUS) {
131 // See third_party/icu/source/data/locales/en.txt.
132 // The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy".
133 i18n::SetICUDefaultLocale("en_US");
135 Time time(Time::FromLocalExploded(kTestDateTimeExploded));
137 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time));
138 EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time));
140 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"),
141 TimeFormatShortDateAndTime(time));
142 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM ") + GetShortTimeZone(),
143 TimeFormatShortDateAndTimeWithTimeZone(time));
145 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 at 3:42:07 PM"),
146 TimeFormatFriendlyDateAndTime(time));
148 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"),
149 TimeFormatFriendlyDate(time));
152 TEST(TimeFormattingTest, TimeFormatDateGB) {
153 // See third_party/icu/source/data/locales/en_GB.txt.
154 // The date patterns are "EEEE, d MMMM y", "d MMM y", and "dd/MM/yyyy".
155 i18n::SetICUDefaultLocale("en_GB");
157 Time time(Time::FromLocalExploded(kTestDateTimeExploded));
159 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time));
160 EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time));
161 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07"),
162 TimeFormatShortDateAndTime(time));
163 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07 ") + GetShortTimeZone(),
164 TimeFormatShortDateAndTimeWithTimeZone(time));
165 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 at 15:42:07"),
166 TimeFormatFriendlyDateAndTime(time));
167 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"),
168 TimeFormatFriendlyDate(time));
171 } // namespace
172 } // namespace base