Compute can_use_lcd_text using property trees.
[chromium-blink-merge.git] / base / i18n / time_formatting_unittest.cc
blob9e075f382666fcd3a81e01078aaf0942634d72c2
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"));
40 string16 clock24h_millis(ASCIIToUTF16("15:42:07.000"));
42 // The default is 12h clock.
43 EXPECT_EQ(clock12h_pm, TimeFormatTimeOfDay(time));
44 EXPECT_EQ(clock24h_millis, TimeFormatTimeOfDayWithMilliseconds(time));
45 EXPECT_EQ(k12HourClock, GetHourClockType());
46 // k{Keep,Drop}AmPm should not affect for 24h clock.
47 EXPECT_EQ(clock24h,
48 TimeFormatTimeOfDayWithHourClockType(time,
49 k24HourClock,
50 kKeepAmPm));
51 EXPECT_EQ(clock24h,
52 TimeFormatTimeOfDayWithHourClockType(time,
53 k24HourClock,
54 kDropAmPm));
55 // k{Keep,Drop}AmPm affects for 12h clock.
56 EXPECT_EQ(clock12h_pm,
57 TimeFormatTimeOfDayWithHourClockType(time,
58 k12HourClock,
59 kKeepAmPm));
60 EXPECT_EQ(clock12h,
61 TimeFormatTimeOfDayWithHourClockType(time,
62 k12HourClock,
63 kDropAmPm));
66 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault24h) {
67 // Test for a locale defaulted to 24h clock.
68 // As an instance, we use third_party/icu/source/data/locales/en_GB.txt.
69 i18n::SetICUDefaultLocale("en_GB");
71 Time time(Time::FromLocalExploded(kTestDateTimeExploded));
72 string16 clock24h(ASCIIToUTF16("15:42"));
73 string16 clock12h_pm(ASCIIToUTF16("3:42 pm"));
74 string16 clock12h(ASCIIToUTF16("3:42"));
75 string16 clock24h_millis(ASCIIToUTF16("15:42:07.000"));
77 // The default is 24h clock.
78 EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time));
79 EXPECT_EQ(clock24h_millis, TimeFormatTimeOfDayWithMilliseconds(time));
80 EXPECT_EQ(k24HourClock, GetHourClockType());
81 // k{Keep,Drop}AmPm should not affect for 24h clock.
82 EXPECT_EQ(clock24h,
83 TimeFormatTimeOfDayWithHourClockType(time,
84 k24HourClock,
85 kKeepAmPm));
86 EXPECT_EQ(clock24h,
87 TimeFormatTimeOfDayWithHourClockType(time,
88 k24HourClock,
89 kDropAmPm));
90 // k{Keep,Drop}AmPm affects for 12h clock.
91 EXPECT_EQ(clock12h_pm,
92 TimeFormatTimeOfDayWithHourClockType(time,
93 k12HourClock,
94 kKeepAmPm));
95 EXPECT_EQ(clock12h,
96 TimeFormatTimeOfDayWithHourClockType(time,
97 k12HourClock,
98 kDropAmPm));
101 TEST(TimeFormattingTest, TimeFormatTimeOfDayJP) {
102 // Test for a locale that uses different mark than "AM" and "PM".
103 // As an instance, we use third_party/icu/source/data/locales/ja.txt.
104 i18n::SetICUDefaultLocale("ja_JP");
106 Time time(Time::FromLocalExploded(kTestDateTimeExploded));
107 string16 clock24h(ASCIIToUTF16("15:42"));
108 string16 clock12h_pm(WideToUTF16(L"\x5348\x5f8c" L"3:42"));
109 string16 clock12h(ASCIIToUTF16("3:42"));
111 // The default is 24h clock.
112 EXPECT_EQ(clock24h, TimeFormatTimeOfDay(time));
113 EXPECT_EQ(k24HourClock, GetHourClockType());
114 // k{Keep,Drop}AmPm should not affect for 24h clock.
115 EXPECT_EQ(clock24h,
116 TimeFormatTimeOfDayWithHourClockType(time,
117 k24HourClock,
118 kKeepAmPm));
119 EXPECT_EQ(clock24h,
120 TimeFormatTimeOfDayWithHourClockType(time,
121 k24HourClock,
122 kDropAmPm));
123 // k{Keep,Drop}AmPm affects for 12h clock.
124 EXPECT_EQ(clock12h_pm,
125 TimeFormatTimeOfDayWithHourClockType(time,
126 k12HourClock,
127 kKeepAmPm));
128 EXPECT_EQ(clock12h,
129 TimeFormatTimeOfDayWithHourClockType(time,
130 k12HourClock,
131 kDropAmPm));
134 TEST(TimeFormattingTest, TimeFormatDateUS) {
135 // See third_party/icu/source/data/locales/en.txt.
136 // The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy".
137 i18n::SetICUDefaultLocale("en_US");
139 Time time(Time::FromLocalExploded(kTestDateTimeExploded));
141 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), TimeFormatShortDate(time));
142 EXPECT_EQ(ASCIIToUTF16("4/30/11"), TimeFormatShortDateNumeric(time));
144 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM"),
145 TimeFormatShortDateAndTime(time));
146 EXPECT_EQ(ASCIIToUTF16("4/30/11, 3:42:07 PM ") + GetShortTimeZone(),
147 TimeFormatShortDateAndTimeWithTimeZone(time));
149 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 at 3:42:07 PM"),
150 TimeFormatFriendlyDateAndTime(time));
152 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"),
153 TimeFormatFriendlyDate(time));
156 TEST(TimeFormattingTest, TimeFormatDateGB) {
157 // See third_party/icu/source/data/locales/en_GB.txt.
158 // The date patterns are "EEEE, d MMMM y", "d MMM y", and "dd/MM/yyyy".
159 i18n::SetICUDefaultLocale("en_GB");
161 Time time(Time::FromLocalExploded(kTestDateTimeExploded));
163 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), TimeFormatShortDate(time));
164 EXPECT_EQ(ASCIIToUTF16("30/04/2011"), TimeFormatShortDateNumeric(time));
165 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07"),
166 TimeFormatShortDateAndTime(time));
167 EXPECT_EQ(ASCIIToUTF16("30/04/2011, 15:42:07 ") + GetShortTimeZone(),
168 TimeFormatShortDateAndTimeWithTimeZone(time));
169 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 at 15:42:07"),
170 TimeFormatFriendlyDateAndTime(time));
171 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"),
172 TimeFormatFriendlyDate(time));
175 } // namespace
176 } // namespace base