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/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 TestTimeFormats(59 * one_min + 30 * one_second - one_millisecond, "59 mins"); | 65 TestTimeFormats(59 * one_min + 30 * one_second - one_millisecond, "59 mins"); |
66 TestTimeFormats(59 * one_min + 30 * one_second, "1 hour"); | 66 TestTimeFormats(59 * one_min + 30 * one_second, "1 hour"); |
67 TestTimeFormats(1 * one_hour + 30 * one_min - one_millisecond, "1 hour"); | 67 TestTimeFormats(1 * one_hour + 30 * one_min - one_millisecond, "1 hour"); |
68 TestTimeFormats(1 * one_hour + 30 * one_min, "2 hours"); | 68 TestTimeFormats(1 * one_hour + 30 * one_min, "2 hours"); |
69 TestTimeFormats(23 * one_hour + 30 * one_min - one_millisecond, "23 hours"); | 69 TestTimeFormats(23 * one_hour + 30 * one_min - one_millisecond, "23 hours"); |
70 TestTimeFormats(23 * one_hour + 30 * one_min, "1 day"); | 70 TestTimeFormats(23 * one_hour + 30 * one_min, "1 day"); |
71 TestTimeFormats(1 * one_day + 12 * one_hour - one_millisecond, "1 day"); | 71 TestTimeFormats(1 * one_day + 12 * one_hour - one_millisecond, "1 day"); |
72 TestTimeFormats(1 * one_day + 12 * one_hour, "2 days"); | 72 TestTimeFormats(1 * one_day + 12 * one_hour, "2 days"); |
73 } | 73 } |
74 | 74 |
| 75 void TestRemainingLong(const TimeDelta& delta, const std::string& expected) { |
| 76 EXPECT_EQ(TimeFormat::TimeRemainingLong(delta), ASCIIToUTF16(expected)); |
| 77 } |
| 78 |
| 79 TEST_F(TimeFormatTest, TimeRemainingLong) { |
| 80 const TimeDelta one_day(TimeDelta::FromDays(1)); |
| 81 const TimeDelta one_hour(TimeDelta::FromHours(1)); |
| 82 const TimeDelta one_min(TimeDelta::FromMinutes(1)); |
| 83 const TimeDelta one_second(TimeDelta::FromSeconds(1)); |
| 84 const TimeDelta one_millisecond(TimeDelta::FromMilliseconds(1)); |
| 85 const TimeDelta zero(TimeDelta::FromMilliseconds(0)); |
| 86 |
| 87 TestRemainingLong(zero, "0 seconds left"); |
| 88 TestRemainingLong(499 * one_millisecond, "0 seconds left"); |
| 89 TestRemainingLong(500 * one_millisecond, "1 second left"); |
| 90 TestRemainingLong(one_second + 499 * one_millisecond, "1 second left"); |
| 91 TestRemainingLong(one_second + 500 * one_millisecond, "2 seconds left"); |
| 92 TestRemainingLong(59 * one_second + 499 * one_millisecond, "59 seconds left"); |
| 93 TestRemainingLong(59 * one_second + 500 * one_millisecond, "1 minute left"); |
| 94 TestRemainingLong(one_min + 30 * one_second - one_millisecond, |
| 95 "1 minute left"); |
| 96 TestRemainingLong(one_min + 30 * one_second, "2 minutes left"); |
| 97 TestRemainingLong(59 * one_min + 30 * one_second - one_millisecond, |
| 98 "59 minutes left"); |
| 99 TestRemainingLong(59 * one_min + 30 * one_second, "1 hour left"); |
| 100 TestRemainingLong(one_hour + 30 * one_min - one_millisecond, "1 hour left"); |
| 101 TestRemainingLong(one_hour + 30 * one_min, "2 hours left"); |
| 102 TestRemainingLong(23 * one_hour + 30 * one_min - one_millisecond, |
| 103 "23 hours left"); |
| 104 TestRemainingLong(23 * one_hour + 30 * one_min, "1 day left"); |
| 105 TestRemainingLong(one_day + 12 * one_hour - one_millisecond, "1 day left"); |
| 106 TestRemainingLong(one_day + 12 * one_hour, "2 days left"); |
| 107 } |
| 108 |
75 // crbug.com/159388: This test fails when daylight savings time ends. | 109 // crbug.com/159388: This test fails when daylight savings time ends. |
76 TEST_F(TimeFormatTest, RelativeDate) { | 110 TEST_F(TimeFormatTest, RelativeDate) { |
77 base::Time now = base::Time::Now(); | 111 base::Time now = base::Time::Now(); |
78 base::string16 today_str = TimeFormat::RelativeDate(now, NULL); | 112 base::string16 today_str = TimeFormat::RelativeDate(now, NULL); |
79 EXPECT_EQ(ASCIIToUTF16("Today"), today_str); | 113 EXPECT_EQ(ASCIIToUTF16("Today"), today_str); |
80 | 114 |
81 base::Time yesterday = now - TimeDelta::FromDays(1); | 115 base::Time yesterday = now - TimeDelta::FromDays(1); |
82 base::string16 yesterday_str = TimeFormat::RelativeDate(yesterday, NULL); | 116 base::string16 yesterday_str = TimeFormat::RelativeDate(yesterday, NULL); |
83 EXPECT_EQ(ASCIIToUTF16("Yesterday"), yesterday_str); | 117 EXPECT_EQ(ASCIIToUTF16("Yesterday"), yesterday_str); |
84 | 118 |
85 base::Time two_days_ago = now - TimeDelta::FromDays(2); | 119 base::Time two_days_ago = now - TimeDelta::FromDays(2); |
86 base::string16 two_days_ago_str = | 120 base::string16 two_days_ago_str = |
87 TimeFormat::RelativeDate(two_days_ago, NULL); | 121 TimeFormat::RelativeDate(two_days_ago, NULL); |
88 EXPECT_TRUE(two_days_ago_str.empty()); | 122 EXPECT_TRUE(two_days_ago_str.empty()); |
89 | 123 |
90 base::Time a_week_ago = now - TimeDelta::FromDays(7); | 124 base::Time a_week_ago = now - TimeDelta::FromDays(7); |
91 base::string16 a_week_ago_str = TimeFormat::RelativeDate(a_week_ago, NULL); | 125 base::string16 a_week_ago_str = TimeFormat::RelativeDate(a_week_ago, NULL); |
92 EXPECT_TRUE(a_week_ago_str.empty()); | 126 EXPECT_TRUE(a_week_ago_str.empty()); |
93 } | 127 } |
94 | 128 |
95 } // namespace | 129 } // namespace |
96 } // namespace ui | 130 } // namespace ui |
OLD | NEW |