OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <locale.h> | |
5 #include <time.h> | 6 #include <time.h> |
6 | 7 |
7 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/logging.h" | |
8 #include "base/string16.h" | 10 #include "base/string16.h" |
9 #include "base/time.h" | 11 #include "base/time.h" |
10 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
11 #include "chrome/common/time_format.h" | 13 #include "chrome/common/time_format.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
13 | 15 |
14 using base::Time; | 16 using base::Time; |
15 using base::TimeDelta; | 17 using base::TimeDelta; |
16 | 18 |
19 namespace { | |
20 | |
21 // A helper class that gets the current locale on construction, and restores | |
22 // it on destruction. | |
23 class ScopedChangeLocale { | |
Paweł Hajdan Jr.
2011/05/11 19:10:44
Sorry for not finding it earlier (I tried, but fai
| |
24 public: | |
25 ScopedChangeLocale() { | |
26 stored_locale_ = setlocale(LC_ALL, NULL); | |
27 } | |
28 virtual ~ScopedChangeLocale() { | |
29 setlocale(LC_ALL, stored_locale_); | |
30 } | |
31 | |
32 private: | |
33 const char *stored_locale_; | |
Paweł Hajdan Jr.
2011/05/11 19:10:44
nit: Star on the wrong side, should be const char*
| |
34 | |
35 DISALLOW_COPY_AND_ASSIGN(ScopedChangeLocale); | |
36 }; | |
37 | |
38 } // namespace | |
Paweł Hajdan Jr.
2011/05/11 19:10:44
nit: Two spaces between "}" and "// namespace".
| |
39 | |
17 TEST(TimeFormat, RelativeDate) { | 40 TEST(TimeFormat, RelativeDate) { |
18 Time now = Time::Now(); | 41 Time now = Time::Now(); |
19 string16 today_str = TimeFormat::RelativeDate(now, NULL); | 42 string16 today_str = TimeFormat::RelativeDate(now, NULL); |
20 EXPECT_EQ(ASCIIToUTF16("Today"), today_str); | 43 EXPECT_EQ(ASCIIToUTF16("Today"), today_str); |
21 | 44 |
22 Time yesterday = now - TimeDelta::FromDays(1); | 45 Time yesterday = now - TimeDelta::FromDays(1); |
23 string16 yesterday_str = TimeFormat::RelativeDate(yesterday, NULL); | 46 string16 yesterday_str = TimeFormat::RelativeDate(yesterday, NULL); |
24 EXPECT_EQ(ASCIIToUTF16("Yesterday"), yesterday_str); | 47 EXPECT_EQ(ASCIIToUTF16("Yesterday"), yesterday_str); |
25 | 48 |
26 Time two_days_ago = now - TimeDelta::FromDays(2); | 49 Time two_days_ago = now - TimeDelta::FromDays(2); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 TestTimeFormats(one_sec + twohundred_millisecs, "1 sec"); | 85 TestTimeFormats(one_sec + twohundred_millisecs, "1 sec"); |
63 TestTimeFormats(five_secs + twohundred_millisecs, "5 secs"); | 86 TestTimeFormats(five_secs + twohundred_millisecs, "5 secs"); |
64 TestTimeFormats(one_min + five_secs, "1 min"); | 87 TestTimeFormats(one_min + five_secs, "1 min"); |
65 TestTimeFormats(three_mins + twohundred_millisecs, "3 mins"); | 88 TestTimeFormats(three_mins + twohundred_millisecs, "3 mins"); |
66 TestTimeFormats(one_hour + five_secs, "1 hour"); | 89 TestTimeFormats(one_hour + five_secs, "1 hour"); |
67 TestTimeFormats(four_hours + five_secs, "4 hours"); | 90 TestTimeFormats(four_hours + five_secs, "4 hours"); |
68 TestTimeFormats(one_day + five_secs, "1 day"); | 91 TestTimeFormats(one_day + five_secs, "1 day"); |
69 TestTimeFormats(three_days, "3 days"); | 92 TestTimeFormats(three_days, "3 days"); |
70 TestTimeFormats(three_days + four_hours, "3 days"); | 93 TestTimeFormats(three_days + four_hours, "3 days"); |
71 } | 94 } |
95 | |
96 TEST(TimeFormat, FAILS_DecimalPointNotDot) { | |
97 // Some locales use a comma ',' instead of a dot '.' as the separator for | |
98 // decimal digits. The icu library wasn't handling this, leading to "1" | |
99 // being internally converted to "+1,0e00" and ultimately leading to "NaN". | |
100 // This showed up on the browser on estimated download time, for example. | |
101 // http://crbug.com/60476 | |
102 | |
103 ScopedChangeLocale scoped_locale; | |
104 | |
105 const char *locales[] = { "fr_FR", "fr_FR.utf8", "fr_FR.UTF-8" }; | |
106 const char *locale; | |
107 for (uint32 i = 0; i < arraysize(locales); ++i) { | |
108 locale = setlocale(LC_ALL, locales[i]); | |
109 if (locale != NULL) | |
110 break; | |
111 } | |
112 | |
113 if (locale == NULL) { | |
114 LOG(WARNING) << "Skipping test because machine doesn't have the fr_FR " | |
115 << "locale installed."; | |
116 return; | |
117 } | |
118 | |
119 string16 one_min = TimeFormat::TimeRemainingShort(TimeDelta::FromMinutes(1)); | |
120 EXPECT_EQ(ASCIIToUTF16("1 min"), one_min) << "fr_FR locale generates " | |
121 << one_min; | |
122 } | |
OLD | NEW |