OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "base/i18n/time_formatting.h" |
| 6 |
| 7 #include "base/time.h" |
| 8 #include "base/utf_string_conversions.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "unicode/locid.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 void SetICUDefaultLocale(const std::string& locale_string) { |
| 15 icu::Locale locale(locale_string.c_str()); |
| 16 UErrorCode error_code = U_ZERO_ERROR; |
| 17 icu::Locale::setDefault(locale, error_code); |
| 18 EXPECT_TRUE(U_SUCCESS(error_code)); |
| 19 } |
| 20 |
| 21 const base::Time::Exploded kTestDateTimeExploded = { |
| 22 2011, 4, 6, 30, // Sat, Apr 30, 2011 |
| 23 15, 42, 7, 0 // 15:42:07.000 |
| 24 }; |
| 25 |
| 26 } // namespace |
| 27 |
| 28 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault12h) { |
| 29 // Test for a locale defaulted to 12h clock. |
| 30 // As an instance, we use third_party/icu/source/data/locales/en.txt. |
| 31 SetICUDefaultLocale("en_US"); |
| 32 |
| 33 base::Time time(base::Time::FromLocalExploded(kTestDateTimeExploded)); |
| 34 string16 clock24h(ASCIIToUTF16("15:42")); |
| 35 string16 clock12h_pm(ASCIIToUTF16("3:42 PM")); |
| 36 string16 clock12h(ASCIIToUTF16("3:42")); |
| 37 |
| 38 // The default is 12h clock. |
| 39 EXPECT_EQ(clock12h_pm, |
| 40 base::TimeFormatTimeOfDay(time)); |
| 41 EXPECT_EQ(base::k12HourClock, base::GetHourClockType()); |
| 42 // k{Keep,Drop}AmPm should not affect for 24h clock. |
| 43 EXPECT_EQ(clock24h, |
| 44 base::TimeFormatTimeOfDayWithHourClockType(time, |
| 45 base::k24HourClock, |
| 46 base::kKeepAmPm)); |
| 47 EXPECT_EQ(clock24h, |
| 48 base::TimeFormatTimeOfDayWithHourClockType(time, |
| 49 base::k24HourClock, |
| 50 base::kDropAmPm)); |
| 51 // k{Keep,Drop}AmPm affects for 12h clock. |
| 52 EXPECT_EQ(clock12h_pm, |
| 53 base::TimeFormatTimeOfDayWithHourClockType(time, |
| 54 base::k12HourClock, |
| 55 base::kKeepAmPm)); |
| 56 EXPECT_EQ(clock12h, |
| 57 base::TimeFormatTimeOfDayWithHourClockType(time, |
| 58 base::k12HourClock, |
| 59 base::kDropAmPm)); |
| 60 } |
| 61 |
| 62 |
| 63 TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault24h) { |
| 64 // Test for a locale defaulted to 24h clock. |
| 65 // As an instance, we use third_party/icu/source/data/locales/en_GB.txt. |
| 66 SetICUDefaultLocale("en_GB"); |
| 67 |
| 68 base::Time time(base::Time::FromLocalExploded(kTestDateTimeExploded)); |
| 69 string16 clock24h(ASCIIToUTF16("15:42")); |
| 70 string16 clock12h_pm(ASCIIToUTF16("3:42 PM")); |
| 71 string16 clock12h(ASCIIToUTF16("3:42")); |
| 72 |
| 73 // The default is 24h clock. |
| 74 EXPECT_EQ(clock24h, |
| 75 base::TimeFormatTimeOfDay(time)); |
| 76 EXPECT_EQ(base::k24HourClock, base::GetHourClockType()); |
| 77 // k{Keep,Drop}AmPm should not affect for 24h clock. |
| 78 EXPECT_EQ(clock24h, |
| 79 base::TimeFormatTimeOfDayWithHourClockType(time, |
| 80 base::k24HourClock, |
| 81 base::kKeepAmPm)); |
| 82 EXPECT_EQ(clock24h, |
| 83 base::TimeFormatTimeOfDayWithHourClockType(time, |
| 84 base::k24HourClock, |
| 85 base::kDropAmPm)); |
| 86 // k{Keep,Drop}AmPm affects for 12h clock. |
| 87 EXPECT_EQ(clock12h_pm, |
| 88 base::TimeFormatTimeOfDayWithHourClockType(time, |
| 89 base::k12HourClock, |
| 90 base::kKeepAmPm)); |
| 91 EXPECT_EQ(clock12h, |
| 92 base::TimeFormatTimeOfDayWithHourClockType(time, |
| 93 base::k12HourClock, |
| 94 base::kDropAmPm)); |
| 95 } |
| 96 |
| 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 SetICUDefaultLocale("ja_JP"); |
| 101 |
| 102 base::Time time(base::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")); |
| 106 |
| 107 // The default is 24h clock. |
| 108 EXPECT_EQ(clock24h, |
| 109 base::TimeFormatTimeOfDay(time)); |
| 110 EXPECT_EQ(base::k24HourClock, base::GetHourClockType()); |
| 111 // k{Keep,Drop}AmPm should not affect for 24h clock. |
| 112 EXPECT_EQ(clock24h, |
| 113 base::TimeFormatTimeOfDayWithHourClockType(time, |
| 114 base::k24HourClock, |
| 115 base::kKeepAmPm)); |
| 116 EXPECT_EQ(clock24h, |
| 117 base::TimeFormatTimeOfDayWithHourClockType(time, |
| 118 base::k24HourClock, |
| 119 base::kDropAmPm)); |
| 120 // k{Keep,Drop}AmPm affects for 12h clock. |
| 121 EXPECT_EQ(clock12h_pm, |
| 122 base::TimeFormatTimeOfDayWithHourClockType(time, |
| 123 base::k12HourClock, |
| 124 base::kKeepAmPm)); |
| 125 EXPECT_EQ(clock12h, |
| 126 base::TimeFormatTimeOfDayWithHourClockType(time, |
| 127 base::k12HourClock, |
| 128 base::kDropAmPm)); |
| 129 } |
| 130 |
| 131 TEST(TimeFormattingTest, TimeFormatDateUS) { |
| 132 // See third_party/icu/source/data/locales/en.txt. |
| 133 // The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy". |
| 134 SetICUDefaultLocale("en_US"); |
| 135 |
| 136 base::Time time(base::Time::FromLocalExploded(kTestDateTimeExploded)); |
| 137 |
| 138 EXPECT_EQ(ASCIIToUTF16("Apr 30, 2011"), |
| 139 base::TimeFormatShortDate(time)); |
| 140 EXPECT_EQ(ASCIIToUTF16("4/30/11"), |
| 141 base::TimeFormatShortDateNumeric(time)); |
| 142 EXPECT_EQ(ASCIIToUTF16("4/30/11 3:42:07 PM"), |
| 143 base::TimeFormatShortDateAndTime(time)); |
| 144 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011 3:42:07 PM"), |
| 145 base::TimeFormatFriendlyDateAndTime(time)); |
| 146 EXPECT_EQ(ASCIIToUTF16("Saturday, April 30, 2011"), |
| 147 base::TimeFormatFriendlyDate(time)); |
| 148 } |
| 149 |
| 150 TEST(TimeFormattingTest, TimeFormatDateGB) { |
| 151 // See third_party/icu/source/data/locales/en_GB.txt. |
| 152 // The date patterns are "EEEE, d MMMM y", "d MMM y", and "dd/MM/yyyy". |
| 153 SetICUDefaultLocale("en_GB"); |
| 154 |
| 155 base::Time time(base::Time::FromLocalExploded(kTestDateTimeExploded)); |
| 156 |
| 157 EXPECT_EQ(ASCIIToUTF16("30 Apr 2011"), |
| 158 base::TimeFormatShortDate(time)); |
| 159 EXPECT_EQ(ASCIIToUTF16("30/04/2011"), |
| 160 base::TimeFormatShortDateNumeric(time)); |
| 161 EXPECT_EQ(ASCIIToUTF16("30/04/2011 15:42:07"), |
| 162 base::TimeFormatShortDateAndTime(time)); |
| 163 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011 15:42:07"), |
| 164 base::TimeFormatFriendlyDateAndTime(time)); |
| 165 EXPECT_EQ(ASCIIToUTF16("Saturday, 30 April 2011"), |
| 166 base::TimeFormatFriendlyDate(time)); |
| 167 } |
OLD | NEW |