| 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 <time.h> | 5 #include "chrome/common/time_format.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/string16.h" | 7 #include "base/string16.h" |
| 9 #include "base/time.h" | 8 #include "base/time.h" |
| 10 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/common/time_format.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 11 |
| 14 using base::Time; | 12 namespace { |
| 13 |
| 15 using base::TimeDelta; | 14 using base::TimeDelta; |
| 16 | 15 |
| 17 TEST(TimeFormat, RelativeDate) { | 16 void TestTimeFormats(const TimeDelta& delta, const char* expected_ascii) { |
| 18 Time now = Time::Now(); | |
| 19 string16 today_str = TimeFormat::RelativeDate(now, NULL); | |
| 20 EXPECT_EQ(ASCIIToUTF16("Today"), today_str); | |
| 21 | |
| 22 Time yesterday = now - TimeDelta::FromDays(1); | |
| 23 string16 yesterday_str = TimeFormat::RelativeDate(yesterday, NULL); | |
| 24 EXPECT_EQ(ASCIIToUTF16("Yesterday"), yesterday_str); | |
| 25 | |
| 26 Time two_days_ago = now - TimeDelta::FromDays(2); | |
| 27 string16 two_days_ago_str = TimeFormat::RelativeDate(two_days_ago, NULL); | |
| 28 EXPECT_TRUE(two_days_ago_str.empty()); | |
| 29 | |
| 30 Time a_week_ago = now - TimeDelta::FromDays(7); | |
| 31 string16 a_week_ago_str = TimeFormat::RelativeDate(a_week_ago, NULL); | |
| 32 EXPECT_TRUE(a_week_ago_str.empty()); | |
| 33 } | |
| 34 | |
| 35 namespace { | |
| 36 void TestTimeFormats(const TimeDelta delta, const char* expected_ascii) { | |
| 37 string16 expected = ASCIIToUTF16(expected_ascii); | 17 string16 expected = ASCIIToUTF16(expected_ascii); |
| 38 string16 expected_left = expected + ASCIIToUTF16(" left"); | 18 string16 expected_left = expected + ASCIIToUTF16(" left"); |
| 39 string16 expected_ago = expected + ASCIIToUTF16(" ago"); | 19 string16 expected_ago = expected + ASCIIToUTF16(" ago"); |
| 40 EXPECT_EQ(expected, TimeFormat::TimeRemainingShort(delta)); | 20 EXPECT_EQ(expected, TimeFormat::TimeRemainingShort(delta)); |
| 41 EXPECT_EQ(expected_left, TimeFormat::TimeRemaining(delta)); | 21 EXPECT_EQ(expected_left, TimeFormat::TimeRemaining(delta)); |
| 42 EXPECT_EQ(expected_ago, TimeFormat::TimeElapsed(delta)); | 22 EXPECT_EQ(expected_ago, TimeFormat::TimeElapsed(delta)); |
| 43 } | 23 } |
| 44 | 24 |
| 45 } // namespace | |
| 46 | |
| 47 TEST(TimeFormat, FormatTime) { | 25 TEST(TimeFormat, FormatTime) { |
| 48 const TimeDelta one_day = TimeDelta::FromDays(1); | 26 const TimeDelta one_day = TimeDelta::FromDays(1); |
| 49 const TimeDelta three_days = TimeDelta::FromDays(3); | 27 const TimeDelta three_days = TimeDelta::FromDays(3); |
| 50 const TimeDelta one_hour = TimeDelta::FromHours(1); | 28 const TimeDelta one_hour = TimeDelta::FromHours(1); |
| 51 const TimeDelta four_hours = TimeDelta::FromHours(4); | 29 const TimeDelta four_hours = TimeDelta::FromHours(4); |
| 52 const TimeDelta one_min = TimeDelta::FromMinutes(1); | 30 const TimeDelta one_min = TimeDelta::FromMinutes(1); |
| 53 const TimeDelta three_mins = TimeDelta::FromMinutes(3); | 31 const TimeDelta three_mins = TimeDelta::FromMinutes(3); |
| 54 const TimeDelta one_sec = TimeDelta::FromSeconds(1); | 32 const TimeDelta one_sec = TimeDelta::FromSeconds(1); |
| 55 const TimeDelta five_secs = TimeDelta::FromSeconds(5); | 33 const TimeDelta five_secs = TimeDelta::FromSeconds(5); |
| 56 const TimeDelta twohundred_millisecs = TimeDelta::FromMilliseconds(200); | 34 const TimeDelta twohundred_millisecs = TimeDelta::FromMilliseconds(200); |
| 57 | 35 |
| 58 // TODO(jungshik) : These test only pass when the OS locale is 'en'. | 36 // TODO(jungshik) : These test only pass when the OS locale is 'en'. |
| 59 // We need to add SetUp() and TearDown() to set the locale to 'en'. | 37 // We need to add SetUp() and TearDown() to set the locale to 'en'. |
| 60 TestTimeFormats(twohundred_millisecs, "0 secs"); | 38 TestTimeFormats(twohundred_millisecs, "0 secs"); |
| 61 TestTimeFormats(one_sec - twohundred_millisecs, "0 secs"); | 39 TestTimeFormats(one_sec - twohundred_millisecs, "0 secs"); |
| 62 TestTimeFormats(one_sec + twohundred_millisecs, "1 sec"); | 40 TestTimeFormats(one_sec + twohundred_millisecs, "1 sec"); |
| 63 TestTimeFormats(five_secs + twohundred_millisecs, "5 secs"); | 41 TestTimeFormats(five_secs + twohundred_millisecs, "5 secs"); |
| 64 TestTimeFormats(one_min + five_secs, "1 min"); | 42 TestTimeFormats(one_min + five_secs, "1 min"); |
| 65 TestTimeFormats(three_mins + twohundred_millisecs, "3 mins"); | 43 TestTimeFormats(three_mins + twohundred_millisecs, "3 mins"); |
| 66 TestTimeFormats(one_hour + five_secs, "1 hour"); | 44 TestTimeFormats(one_hour + five_secs, "1 hour"); |
| 67 TestTimeFormats(four_hours + five_secs, "4 hours"); | 45 TestTimeFormats(four_hours + five_secs, "4 hours"); |
| 68 TestTimeFormats(one_day + five_secs, "1 day"); | 46 TestTimeFormats(one_day + five_secs, "1 day"); |
| 69 TestTimeFormats(three_days, "3 days"); | 47 TestTimeFormats(three_days, "3 days"); |
| 70 TestTimeFormats(three_days + four_hours, "3 days"); | 48 TestTimeFormats(three_days + four_hours, "3 days"); |
| 71 } | 49 } |
| 50 |
| 51 TEST(TimeFormat, RelativeDate) { |
| 52 base::Time now = base::Time::Now(); |
| 53 string16 today_str = TimeFormat::RelativeDate(now, NULL); |
| 54 EXPECT_EQ(ASCIIToUTF16("Today"), today_str); |
| 55 |
| 56 base::Time yesterday = now - TimeDelta::FromDays(1); |
| 57 string16 yesterday_str = TimeFormat::RelativeDate(yesterday, NULL); |
| 58 EXPECT_EQ(ASCIIToUTF16("Yesterday"), yesterday_str); |
| 59 |
| 60 base::Time two_days_ago = now - TimeDelta::FromDays(2); |
| 61 string16 two_days_ago_str = TimeFormat::RelativeDate(two_days_ago, NULL); |
| 62 EXPECT_TRUE(two_days_ago_str.empty()); |
| 63 |
| 64 base::Time a_week_ago = now - TimeDelta::FromDays(7); |
| 65 string16 a_week_ago_str = TimeFormat::RelativeDate(a_week_ago, NULL); |
| 66 EXPECT_TRUE(a_week_ago_str.empty()); |
| 67 } |
| 68 |
| 69 } // namespace |
| OLD | NEW |