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 3 // found in the LICENSE file. 4 5 #include <time.h> 6 7 #include "base/basictypes.h" 8 #include "base/string16.h" 9 #include "base/time.h" 10 #include "base/utf_string_conversions.h" 11 #include "chrome/common/time_format.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 14 using base::Time; 15 using base::TimeDelta; 16 17 TEST(TimeFormat, RelativeDate) { 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); 38 string16 expected_left = expected + ASCIIToUTF16(" left"); 39 string16 expected_ago = expected + ASCIIToUTF16(" ago"); 40 EXPECT_EQ(expected, TimeFormat::TimeRemainingShort(delta)); 41 EXPECT_EQ(expected_left, TimeFormat::TimeRemaining(delta)); 42 EXPECT_EQ(expected_ago, TimeFormat::TimeElapsed(delta)); 43 } 44 45 } // namespace 46 47 TEST(TimeFormat, FormatTime) { 48 const TimeDelta one_day = TimeDelta::FromDays(1); 49 const TimeDelta three_days = TimeDelta::FromDays(3); 50 const TimeDelta one_hour = TimeDelta::FromHours(1); 51 const TimeDelta four_hours = TimeDelta::FromHours(4); 52 const TimeDelta one_min = TimeDelta::FromMinutes(1); 53 const TimeDelta three_mins = TimeDelta::FromMinutes(3); 54 const TimeDelta one_sec = TimeDelta::FromSeconds(1); 55 const TimeDelta five_secs = TimeDelta::FromSeconds(5); 56 const TimeDelta twohundred_millisecs = TimeDelta::FromMilliseconds(200); 57 58 // 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'. 60 TestTimeFormats(twohundred_millisecs, "0 secs"); 61 TestTimeFormats(one_sec - twohundred_millisecs, "0 secs"); 62 TestTimeFormats(one_sec + twohundred_millisecs, "1 sec"); 63 TestTimeFormats(five_secs + twohundred_millisecs, "5 secs"); 64 TestTimeFormats(one_min + five_secs, "1 min"); 65 TestTimeFormats(three_mins + twohundred_millisecs, "3 mins"); 66 TestTimeFormats(one_hour + five_secs, "1 hour"); 67 TestTimeFormats(four_hours + five_secs, "4 hours"); 68 TestTimeFormats(one_day + five_secs, "1 day"); 69 TestTimeFormats(three_days, "3 days"); 70 TestTimeFormats(three_days + four_hours, "3 days"); 71 } 72