| 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 namespace ui { | 13 namespace ui { |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 using base::TimeDelta; | 16 using base::TimeDelta; |
| 17 | 17 |
| 18 void TestTimeFormats(const TimeDelta& delta, const char* expected_ascii) { | 18 void TestTimeFormats(const TimeDelta& delta, const char* expected_ascii) { |
| 19 string16 expected = ASCIIToUTF16(expected_ascii); | 19 base::string16 expected = ASCIIToUTF16(expected_ascii); |
| 20 string16 expected_left = expected + ASCIIToUTF16(" left"); | 20 base::string16 expected_left = expected + ASCIIToUTF16(" left"); |
| 21 string16 expected_ago = expected + ASCIIToUTF16(" ago"); | 21 base::string16 expected_ago = expected + ASCIIToUTF16(" ago"); |
| 22 EXPECT_EQ(expected, TimeFormat::TimeRemainingShort(delta)); | 22 EXPECT_EQ(expected, TimeFormat::TimeRemainingShort(delta)); |
| 23 EXPECT_EQ(expected_left, TimeFormat::TimeRemaining(delta)); | 23 EXPECT_EQ(expected_left, TimeFormat::TimeRemaining(delta)); |
| 24 EXPECT_EQ(expected_ago, TimeFormat::TimeElapsed(delta)); | 24 EXPECT_EQ(expected_ago, TimeFormat::TimeElapsed(delta)); |
| 25 } | 25 } |
| 26 | 26 |
| 27 TEST(TimeFormat, FormatTime) { | 27 TEST(TimeFormat, FormatTime) { |
| 28 const TimeDelta one_day = TimeDelta::FromDays(1); | 28 const TimeDelta one_day = TimeDelta::FromDays(1); |
| 29 const TimeDelta three_days = TimeDelta::FromDays(3); | 29 const TimeDelta three_days = TimeDelta::FromDays(3); |
| 30 const TimeDelta one_hour = TimeDelta::FromHours(1); | 30 const TimeDelta one_hour = TimeDelta::FromHours(1); |
| 31 const TimeDelta four_hours = TimeDelta::FromHours(4); | 31 const TimeDelta four_hours = TimeDelta::FromHours(4); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 46 TestTimeFormats(one_hour + five_secs, "1 hour"); | 46 TestTimeFormats(one_hour + five_secs, "1 hour"); |
| 47 TestTimeFormats(four_hours + five_secs, "4 hours"); | 47 TestTimeFormats(four_hours + five_secs, "4 hours"); |
| 48 TestTimeFormats(one_day + five_secs, "1 day"); | 48 TestTimeFormats(one_day + five_secs, "1 day"); |
| 49 TestTimeFormats(three_days, "3 days"); | 49 TestTimeFormats(three_days, "3 days"); |
| 50 TestTimeFormats(three_days + four_hours, "3 days"); | 50 TestTimeFormats(three_days + four_hours, "3 days"); |
| 51 } | 51 } |
| 52 | 52 |
| 53 // crbug.com/159388: This test fails when daylight savings time ends. | 53 // crbug.com/159388: This test fails when daylight savings time ends. |
| 54 TEST(TimeFormat, RelativeDate) { | 54 TEST(TimeFormat, RelativeDate) { |
| 55 base::Time now = base::Time::Now(); | 55 base::Time now = base::Time::Now(); |
| 56 string16 today_str = TimeFormat::RelativeDate(now, NULL); | 56 base::string16 today_str = TimeFormat::RelativeDate(now, NULL); |
| 57 EXPECT_EQ(ASCIIToUTF16("Today"), today_str); | 57 EXPECT_EQ(ASCIIToUTF16("Today"), today_str); |
| 58 | 58 |
| 59 base::Time yesterday = now - TimeDelta::FromDays(1); | 59 base::Time yesterday = now - TimeDelta::FromDays(1); |
| 60 string16 yesterday_str = TimeFormat::RelativeDate(yesterday, NULL); | 60 base::string16 yesterday_str = TimeFormat::RelativeDate(yesterday, NULL); |
| 61 EXPECT_EQ(ASCIIToUTF16("Yesterday"), yesterday_str); | 61 EXPECT_EQ(ASCIIToUTF16("Yesterday"), yesterday_str); |
| 62 | 62 |
| 63 base::Time two_days_ago = now - TimeDelta::FromDays(2); | 63 base::Time two_days_ago = now - TimeDelta::FromDays(2); |
| 64 string16 two_days_ago_str = TimeFormat::RelativeDate(two_days_ago, NULL); | 64 base::string16 two_days_ago_str = |
| 65 TimeFormat::RelativeDate(two_days_ago, NULL); |
| 65 EXPECT_TRUE(two_days_ago_str.empty()); | 66 EXPECT_TRUE(two_days_ago_str.empty()); |
| 66 | 67 |
| 67 base::Time a_week_ago = now - TimeDelta::FromDays(7); | 68 base::Time a_week_ago = now - TimeDelta::FromDays(7); |
| 68 string16 a_week_ago_str = TimeFormat::RelativeDate(a_week_ago, NULL); | 69 base::string16 a_week_ago_str = TimeFormat::RelativeDate(a_week_ago, NULL); |
| 69 EXPECT_TRUE(a_week_ago_str.empty()); | 70 EXPECT_TRUE(a_week_ago_str.empty()); |
| 70 } | 71 } |
| 71 | 72 |
| 72 } // namespace | 73 } // namespace |
| 73 } // namespace ui | 74 } // namespace ui |
| OLD | NEW |