| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #include "platform/text/LocaleMac.h" | 26 #include "platform/text/LocaleMac.h" |
| 27 | 27 |
| 28 #include "platform/DateComponents.h" | 28 #include "platform/DateComponents.h" |
| 29 #include "platform/testing/TestingPlatformSupport.h" | 29 #include "platform/testing/TestingPlatformSupport.h" |
| 30 #include "public/platform/Platform.h" | 30 #include "public/platform/Platform.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 32 #include "wtf/DateMath.h" | 32 #include "wtf/DateMath.h" |
| 33 #include "wtf/MathExtras.h" | 33 #include "wtf/MathExtras.h" |
| 34 #include "wtf/PassOwnPtr.h" |
| 34 #include "wtf/text/CString.h" | 35 #include "wtf/text/CString.h" |
| 35 #include <memory> | |
| 36 | 36 |
| 37 namespace blink { | 37 namespace blink { |
| 38 | 38 |
| 39 class LocalePlatformSupport : public TestingPlatformSupport { | 39 class LocalePlatformSupport : public TestingPlatformSupport { |
| 40 public: | 40 public: |
| 41 WebString queryLocalizedString(WebLocalizedString::Name /*name*/) override | 41 WebString queryLocalizedString(WebLocalizedString::Name /*name*/) override |
| 42 { | 42 { |
| 43 return WebString::fromUTF8("Week $2, $1"); | 43 return WebString::fromUTF8("Week $2, $1"); |
| 44 } | 44 } |
| 45 }; | 45 }; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 73 return date; | 73 return date; |
| 74 } | 74 } |
| 75 | 75 |
| 76 double msForDate(int year, int month, int day) | 76 double msForDate(int year, int month, int day) |
| 77 { | 77 { |
| 78 return dateToDaysFrom1970(year, month, day) * msPerDay; | 78 return dateToDaysFrom1970(year, month, day) * msPerDay; |
| 79 } | 79 } |
| 80 | 80 |
| 81 String formatWeek(const String& localeString, const String& isoString) | 81 String formatWeek(const String& localeString, const String& isoString) |
| 82 { | 82 { |
| 83 std::unique_ptr<LocaleMac> locale = LocaleMac::create(localeString); | 83 OwnPtr<LocaleMac> locale = LocaleMac::create(localeString); |
| 84 DateComponents date; | 84 DateComponents date; |
| 85 unsigned end; | 85 unsigned end; |
| 86 date.parseWeek(isoString, 0, end); | 86 date.parseWeek(isoString, 0, end); |
| 87 return locale->formatDateTime(date); | 87 return locale->formatDateTime(date); |
| 88 } | 88 } |
| 89 | 89 |
| 90 String formatMonth(const String& localeString, const String& isoString, bool
useShortFormat) | 90 String formatMonth(const String& localeString, const String& isoString, bool
useShortFormat) |
| 91 { | 91 { |
| 92 std::unique_ptr<LocaleMac> locale = LocaleMac::create(localeString); | 92 OwnPtr<LocaleMac> locale = LocaleMac::create(localeString); |
| 93 DateComponents date; | 93 DateComponents date; |
| 94 unsigned end; | 94 unsigned end; |
| 95 date.parseMonth(isoString, 0, end); | 95 date.parseMonth(isoString, 0, end); |
| 96 return locale->formatDateTime(date, (useShortFormat ? Locale::FormatType
Short : Locale::FormatTypeMedium)); | 96 return locale->formatDateTime(date, (useShortFormat ? Locale::FormatType
Short : Locale::FormatTypeMedium)); |
| 97 } | 97 } |
| 98 | 98 |
| 99 String formatDate(const String& localeString, int year, int month, int day) | 99 String formatDate(const String& localeString, int year, int month, int day) |
| 100 { | 100 { |
| 101 std::unique_ptr<LocaleMac> locale = LocaleMac::create(localeString); | 101 OwnPtr<LocaleMac> locale = LocaleMac::create(localeString); |
| 102 return locale->formatDateTime(getDateComponents(year, month, day)); | 102 return locale->formatDateTime(getDateComponents(year, month, day)); |
| 103 } | 103 } |
| 104 | 104 |
| 105 String formatTime(const String& localeString, int hour, int minute, int seco
nd, int millisecond, bool useShortFormat) | 105 String formatTime(const String& localeString, int hour, int minute, int seco
nd, int millisecond, bool useShortFormat) |
| 106 { | 106 { |
| 107 std::unique_ptr<LocaleMac> locale = LocaleMac::create(localeString); | 107 OwnPtr<LocaleMac> locale = LocaleMac::create(localeString); |
| 108 return locale->formatDateTime(getTimeComponents(hour, minute, second, mi
llisecond), (useShortFormat ? Locale::FormatTypeShort : Locale::FormatTypeMedium
)); | 108 return locale->formatDateTime(getTimeComponents(hour, minute, second, mi
llisecond), (useShortFormat ? Locale::FormatTypeShort : Locale::FormatTypeMedium
)); |
| 109 } | 109 } |
| 110 | 110 |
| 111 unsigned firstDayOfWeek(const String& localeString) | 111 unsigned firstDayOfWeek(const String& localeString) |
| 112 { | 112 { |
| 113 std::unique_ptr<LocaleMac> locale = LocaleMac::create(localeString); | 113 OwnPtr<LocaleMac> locale = LocaleMac::create(localeString); |
| 114 return locale->firstDayOfWeek(); | 114 return locale->firstDayOfWeek(); |
| 115 } | 115 } |
| 116 | 116 |
| 117 String monthLabel(const String& localeString, unsigned index) | 117 String monthLabel(const String& localeString, unsigned index) |
| 118 { | 118 { |
| 119 std::unique_ptr<LocaleMac> locale = LocaleMac::create(localeString); | 119 OwnPtr<LocaleMac> locale = LocaleMac::create(localeString); |
| 120 return locale->monthLabels()[index]; | 120 return locale->monthLabels()[index]; |
| 121 } | 121 } |
| 122 | 122 |
| 123 String weekDayShortLabel(const String& localeString, unsigned index) | 123 String weekDayShortLabel(const String& localeString, unsigned index) |
| 124 { | 124 { |
| 125 std::unique_ptr<LocaleMac> locale = LocaleMac::create(localeString); | 125 OwnPtr<LocaleMac> locale = LocaleMac::create(localeString); |
| 126 return locale->weekDayShortLabels()[index]; | 126 return locale->weekDayShortLabels()[index]; |
| 127 } | 127 } |
| 128 | 128 |
| 129 bool isRTL(const String& localeString) | 129 bool isRTL(const String& localeString) |
| 130 { | 130 { |
| 131 std::unique_ptr<LocaleMac> locale = LocaleMac::create(localeString); | 131 OwnPtr<LocaleMac> locale = LocaleMac::create(localeString); |
| 132 return locale->isRTL(); | 132 return locale->isRTL(); |
| 133 } | 133 } |
| 134 | 134 |
| 135 String monthFormat(const String& localeString) | 135 String monthFormat(const String& localeString) |
| 136 { | 136 { |
| 137 std::unique_ptr<LocaleMac> locale = LocaleMac::create(localeString); | 137 OwnPtr<LocaleMac> locale = LocaleMac::create(localeString); |
| 138 return locale->monthFormat(); | 138 return locale->monthFormat(); |
| 139 } | 139 } |
| 140 | 140 |
| 141 String timeFormat(const String& localeString) | 141 String timeFormat(const String& localeString) |
| 142 { | 142 { |
| 143 std::unique_ptr<LocaleMac> locale = LocaleMac::create(localeString); | 143 OwnPtr<LocaleMac> locale = LocaleMac::create(localeString); |
| 144 return locale->timeFormat(); | 144 return locale->timeFormat(); |
| 145 } | 145 } |
| 146 | 146 |
| 147 String shortTimeFormat(const String& localeString) | 147 String shortTimeFormat(const String& localeString) |
| 148 { | 148 { |
| 149 std::unique_ptr<LocaleMac> locale = LocaleMac::create(localeString); | 149 OwnPtr<LocaleMac> locale = LocaleMac::create(localeString); |
| 150 return locale->shortTimeFormat(); | 150 return locale->shortTimeFormat(); |
| 151 } | 151 } |
| 152 | 152 |
| 153 String shortMonthLabel(const String& localeString, unsigned index) | 153 String shortMonthLabel(const String& localeString, unsigned index) |
| 154 { | 154 { |
| 155 std::unique_ptr<LocaleMac> locale = LocaleMac::create(localeString); | 155 OwnPtr<LocaleMac> locale = LocaleMac::create(localeString); |
| 156 return locale->shortMonthLabels()[index]; | 156 return locale->shortMonthLabels()[index]; |
| 157 } | 157 } |
| 158 | 158 |
| 159 String standAloneMonthLabel(const String& localeString, unsigned index) | 159 String standAloneMonthLabel(const String& localeString, unsigned index) |
| 160 { | 160 { |
| 161 std::unique_ptr<LocaleMac> locale = LocaleMac::create(localeString); | 161 OwnPtr<LocaleMac> locale = LocaleMac::create(localeString); |
| 162 return locale->standAloneMonthLabels()[index]; | 162 return locale->standAloneMonthLabels()[index]; |
| 163 } | 163 } |
| 164 | 164 |
| 165 String shortStandAloneMonthLabel(const String& localeString, unsigned index) | 165 String shortStandAloneMonthLabel(const String& localeString, unsigned index) |
| 166 { | 166 { |
| 167 std::unique_ptr<LocaleMac> locale = LocaleMac::create(localeString); | 167 OwnPtr<LocaleMac> locale = LocaleMac::create(localeString); |
| 168 return locale->shortStandAloneMonthLabels()[index]; | 168 return locale->shortStandAloneMonthLabels()[index]; |
| 169 } | 169 } |
| 170 | 170 |
| 171 String timeAMPMLabel(const String& localeString, unsigned index) | 171 String timeAMPMLabel(const String& localeString, unsigned index) |
| 172 { | 172 { |
| 173 std::unique_ptr<LocaleMac> locale = LocaleMac::create(localeString); | 173 OwnPtr<LocaleMac> locale = LocaleMac::create(localeString); |
| 174 return locale->timeAMPMLabels()[index]; | 174 return locale->timeAMPMLabels()[index]; |
| 175 } | 175 } |
| 176 | 176 |
| 177 String decimalSeparator(const String& localeString) | 177 String decimalSeparator(const String& localeString) |
| 178 { | 178 { |
| 179 std::unique_ptr<LocaleMac> locale = LocaleMac::create(localeString); | 179 OwnPtr<LocaleMac> locale = LocaleMac::create(localeString); |
| 180 return locale->localizedDecimalSeparator(); | 180 return locale->localizedDecimalSeparator(); |
| 181 } | 181 } |
| 182 }; | 182 }; |
| 183 | 183 |
| 184 TEST_F(LocaleMacTest, formatWeek) | 184 TEST_F(LocaleMacTest, formatWeek) |
| 185 { | 185 { |
| 186 LocalePlatformSupport support; | 186 LocalePlatformSupport support; |
| 187 EXPECT_STREQ("Week 04, 2005", formatWeek("en_US", "2005-W04").utf8().data())
; | 187 EXPECT_STREQ("Week 04, 2005", formatWeek("en_US", "2005-W04").utf8().data())
; |
| 188 EXPECT_STREQ("Week 52, 2005", formatWeek("en_US", "2005-W52").utf8().data())
; | 188 EXPECT_STREQ("Week 52, 2005", formatWeek("en_US", "2005-W52").utf8().data())
; |
| 189 } | 189 } |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 } | 354 } |
| 355 | 355 |
| 356 TEST_F(LocaleMacTest, invalidLocale) | 356 TEST_F(LocaleMacTest, invalidLocale) |
| 357 { | 357 { |
| 358 EXPECT_STREQ(monthLabel("en_US", January).utf8().data(), monthLabel("foo", J
anuary).utf8().data()); | 358 EXPECT_STREQ(monthLabel("en_US", January).utf8().data(), monthLabel("foo", J
anuary).utf8().data()); |
| 359 EXPECT_STREQ(decimalSeparator("en_US").utf8().data(), decimalSeparator("foo"
).utf8().data()); | 359 EXPECT_STREQ(decimalSeparator("en_US").utf8().data(), decimalSeparator("foo"
).utf8().data()); |
| 360 } | 360 } |
| 361 | 361 |
| 362 static void testNumberIsReversible(const AtomicString& localeString, const char*
original, const char* shouldHave = 0) | 362 static void testNumberIsReversible(const AtomicString& localeString, const char*
original, const char* shouldHave = 0) |
| 363 { | 363 { |
| 364 std::unique_ptr<LocaleMac> locale = LocaleMac::create(localeString); | 364 OwnPtr<LocaleMac> locale = LocaleMac::create(localeString); |
| 365 String localized = locale->convertToLocalizedNumber(original); | 365 String localized = locale->convertToLocalizedNumber(original); |
| 366 if (shouldHave) | 366 if (shouldHave) |
| 367 EXPECT_TRUE(localized.contains(shouldHave)); | 367 EXPECT_TRUE(localized.contains(shouldHave)); |
| 368 String converted = locale->convertFromLocalizedNumber(localized); | 368 String converted = locale->convertFromLocalizedNumber(localized); |
| 369 EXPECT_STREQ(original, converted.utf8().data()); | 369 EXPECT_STREQ(original, converted.utf8().data()); |
| 370 } | 370 } |
| 371 | 371 |
| 372 void testNumbers(const AtomicString& localeString, const char* decimalSeparatorS
houldBe = 0) | 372 void testNumbers(const AtomicString& localeString, const char* decimalSeparatorS
houldBe = 0) |
| 373 { | 373 { |
| 374 testNumberIsReversible(localeString, "123456789012345678901234567890"); | 374 testNumberIsReversible(localeString, "123456789012345678901234567890"); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 387 testNumbers("es_ES"); | 387 testNumbers("es_ES"); |
| 388 testNumbers("fa"); | 388 testNumbers("fa"); |
| 389 testNumbers("ja_JP"); | 389 testNumbers("ja_JP"); |
| 390 testNumbers("ko_KR"); | 390 testNumbers("ko_KR"); |
| 391 testNumbers("zh_CN"); | 391 testNumbers("zh_CN"); |
| 392 testNumbers("zh_HK"); | 392 testNumbers("zh_HK"); |
| 393 testNumbers("zh_TW"); | 393 testNumbers("zh_TW"); |
| 394 } | 394 } |
| 395 | 395 |
| 396 } // namespace blink | 396 } // namespace blink |
| OLD | NEW |