OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "ui/base/l10n/time_format.h" | 5 #include "ui/base/l10n/time_format.h" |
6 | 6 |
7 #include "base/strings/string16.h" | 7 #include "base/strings/string16.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "ui/base/resource/resource_bundle.h" | 11 #include "ui/base/resource/resource_bundle.h" |
12 | 12 |
13 using base::ASCIIToUTF16; | 13 using base::ASCIIToUTF16; |
14 | 14 |
15 namespace ui { | 15 namespace ui { |
16 namespace { | 16 namespace { |
17 | 17 |
18 using base::TimeDelta; | 18 using base::TimeDelta; |
19 | 19 |
20 void TestTimeFormats(const TimeDelta& delta, const char* expected_ascii) { | 20 void TestTimeFormats(const TimeDelta& delta, const char* expected_ascii) { |
21 base::string16 expected = ASCIIToUTF16(expected_ascii); | 21 const base::string16 expected(ASCIIToUTF16(expected_ascii)); |
22 base::string16 expected_left = expected + ASCIIToUTF16(" left"); | 22 const base::string16 sec(ASCIIToUTF16("sec")); |
23 base::string16 expected_ago = expected + ASCIIToUTF16(" ago"); | 23 const base::string16 min(ASCIIToUTF16("min")); |
24 | |
25 // expected_long_minute has "min" replaced by "minute". | |
26 base::string16 expected_long_minute = expected; | |
27 if (expected_long_minute.find(min) != base::string16::npos) | |
28 expected_long_minute.insert(expected_long_minute.find(min) + min.length(), | |
29 ASCIIToUTF16("ute")); | |
30 | |
31 // expected_long has {"sec","min"} replaced by {"second","minute"}. | |
32 base::string16 expected_long = expected_long_minute; | |
33 if (expected_long.find(sec) != base::string16::npos) | |
34 expected_long.insert(expected_long.find(sec) + sec.length(), | |
35 ASCIIToUTF16("ond")); | |
jungshik at Google
2014/02/03 18:03:01
This string manipulation is really hacky, I'm afra
| |
36 | |
37 EXPECT_EQ(expected + ASCIIToUTF16(" ago"), TimeFormat::TimeElapsed(delta)); | |
38 EXPECT_EQ(expected + ASCIIToUTF16(" left"), TimeFormat::TimeRemaining(delta)); | |
39 EXPECT_EQ(expected_long_minute + ASCIIToUTF16(" left"), | |
40 TimeFormat::TimeRemainingLong(delta)); | |
24 EXPECT_EQ(expected, TimeFormat::TimeRemainingShort(delta)); | 41 EXPECT_EQ(expected, TimeFormat::TimeRemainingShort(delta)); |
25 EXPECT_EQ(expected_left, TimeFormat::TimeRemaining(delta)); | 42 EXPECT_EQ(expected_long, TimeFormat::TimeDurationLong(delta)); |
26 EXPECT_EQ(expected_ago, TimeFormat::TimeElapsed(delta)); | |
27 } | 43 } |
28 | 44 |
29 TEST(TimeFormat, FormatTime) { | 45 TEST(TimeFormat, FormatTime) { |
30 const TimeDelta one_day = TimeDelta::FromDays(1); | 46 const TimeDelta one_day = TimeDelta::FromDays(1); |
31 const TimeDelta one_hour = TimeDelta::FromHours(1); | 47 const TimeDelta one_hour = TimeDelta::FromHours(1); |
32 const TimeDelta one_min = TimeDelta::FromMinutes(1); | 48 const TimeDelta one_min = TimeDelta::FromMinutes(1); |
33 const TimeDelta one_second = TimeDelta::FromSeconds(1); | 49 const TimeDelta one_second = TimeDelta::FromSeconds(1); |
34 const TimeDelta one_millisecond = TimeDelta::FromMilliseconds(1); | 50 const TimeDelta one_millisecond = TimeDelta::FromMilliseconds(1); |
35 const TimeDelta zero = TimeDelta::FromMilliseconds(0); | 51 const TimeDelta zero = TimeDelta::FromMilliseconds(0); |
36 | 52 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 TimeFormat::RelativeDate(two_days_ago, NULL); | 86 TimeFormat::RelativeDate(two_days_ago, NULL); |
71 EXPECT_TRUE(two_days_ago_str.empty()); | 87 EXPECT_TRUE(two_days_ago_str.empty()); |
72 | 88 |
73 base::Time a_week_ago = now - TimeDelta::FromDays(7); | 89 base::Time a_week_ago = now - TimeDelta::FromDays(7); |
74 base::string16 a_week_ago_str = TimeFormat::RelativeDate(a_week_ago, NULL); | 90 base::string16 a_week_ago_str = TimeFormat::RelativeDate(a_week_ago, NULL); |
75 EXPECT_TRUE(a_week_ago_str.empty()); | 91 EXPECT_TRUE(a_week_ago_str.empty()); |
76 } | 92 } |
77 | 93 |
78 } // namespace | 94 } // namespace |
79 } // namespace ui | 95 } // namespace ui |
OLD | NEW |