 Chromium Code Reviews
 Chromium Code Reviews Issue 143633003:
  Fix rounding of time interval strings  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 143633003:
  Fix rounding of time interval strings  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| 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 base::string16 expected = ASCIIToUTF16(expected_ascii); | 
| 22 base::string16 expected_left = expected + ASCIIToUTF16(" left"); | 22 base::string16 expected_left = expected + ASCIIToUTF16(" left"); | 
| 23 base::string16 expected_ago = expected + ASCIIToUTF16(" ago"); | 23 base::string16 expected_ago = expected + ASCIIToUTF16(" ago"); | 
| 24 EXPECT_EQ(expected, TimeFormat::TimeRemainingShort(delta)); | 24 EXPECT_EQ(expected, TimeFormat::TimeRemainingShort(delta)); | 
| 25 EXPECT_EQ(expected_left, TimeFormat::TimeRemaining(delta)); | 25 EXPECT_EQ(expected_left, TimeFormat::TimeRemaining(delta)); | 
| 26 EXPECT_EQ(expected_ago, TimeFormat::TimeElapsed(delta)); | 26 EXPECT_EQ(expected_ago, TimeFormat::TimeElapsed(delta)); | 
| 27 } | 27 } | 
| 28 | 28 | 
| 29 TEST(TimeFormat, FormatTime) { | 29 TEST(TimeFormat, FormatTime) { | 
| 30 const TimeDelta one_day = TimeDelta::FromDays(1); | 30 const TimeDelta day = TimeDelta::FromDays(1); | 
| 31 const TimeDelta three_days = TimeDelta::FromDays(3); | 31 const TimeDelta hour = TimeDelta::FromHours(1); | 
| 32 const TimeDelta one_hour = TimeDelta::FromHours(1); | 32 const TimeDelta min = TimeDelta::FromMinutes(1); | 
| 
bartfab (slow)
2014/01/28 15:16:46
Nit: Please avoid abbreviations where possible. Th
 
Thiemo Nagel
2014/01/28 16:08:59
There might be plenty of space here, but it will c
 | |
| 33 const TimeDelta four_hours = TimeDelta::FromHours(4); | 33 const TimeDelta sec = TimeDelta::FromSeconds(1); | 
| 34 const TimeDelta one_min = TimeDelta::FromMinutes(1); | 34 const TimeDelta msec = TimeDelta::FromMilliseconds(1); | 
| 35 const TimeDelta three_mins = TimeDelta::FromMinutes(3); | 35 const TimeDelta zero = TimeDelta::FromMilliseconds(0); | 
| 36 const TimeDelta one_sec = TimeDelta::FromSeconds(1); | |
| 37 const TimeDelta five_secs = TimeDelta::FromSeconds(5); | |
| 38 const TimeDelta twohundred_millisecs = TimeDelta::FromMilliseconds(200); | |
| 39 | 36 | 
| 40 // TODO(jungshik) : These test only pass when the OS locale is 'en'. | 37 // TODO(jungshik) : These test only pass when the OS locale is 'en'. | 
| 41 // We need to add SetUp() and TearDown() to set the locale to 'en'. | 38 // We need to add SetUp() and TearDown() to set the locale to 'en'. | 
| 42 TestTimeFormats(twohundred_millisecs, "0 secs"); | 39 TestTimeFormats(zero, "0 secs"); | 
| 43 TestTimeFormats(one_sec - twohundred_millisecs, "0 secs"); | 40 TestTimeFormats(499*msec, "0 secs"); | 
| 44 TestTimeFormats(one_sec + twohundred_millisecs, "1 sec"); | 41 TestTimeFormats(500*msec, "1 sec"); | 
| 45 TestTimeFormats(five_secs + twohundred_millisecs, "5 secs"); | 42 TestTimeFormats(1*sec + 499*msec, "1 sec"); | 
| 46 TestTimeFormats(one_min + five_secs, "1 min"); | 43 TestTimeFormats(1*sec + 500*msec, "2 secs"); | 
| 47 TestTimeFormats(three_mins + twohundred_millisecs, "3 mins"); | 44 TestTimeFormats(59*sec + 499*msec, "59 secs"); | 
| 48 TestTimeFormats(one_hour + five_secs, "1 hour"); | 45 TestTimeFormats(59*sec + 500*msec, "1 min"); | 
| 49 TestTimeFormats(four_hours + five_secs, "4 hours"); | 46 TestTimeFormats(1*min + 29*sec + 999*msec, "1 min"); | 
| 50 TestTimeFormats(one_day + five_secs, "1 day"); | 47 TestTimeFormats(1*min + 30*sec, "2 mins"); | 
| 51 TestTimeFormats(three_days, "3 days"); | 48 TestTimeFormats(59*min + 29*sec + 999*msec, "59 mins"); | 
| 52 TestTimeFormats(three_days + four_hours, "3 days"); | 49 TestTimeFormats(59*min + 30*sec, "1 hour"); | 
| 50 TestTimeFormats(1*hour + 29*min + 59*sec + 999*msec, "1 hour"); | |
| 51 TestTimeFormats(1*hour + 30*min, "2 hours"); | |
| 52 TestTimeFormats(23*hour + 29*min + 59*sec + 999*msec, "23 hours"); | |
| 53 TestTimeFormats(23*hour + 30*min, "1 day"); | |
| 54 TestTimeFormats(1*day + 11*hour + 59*min + 59*sec + 999*msec, "1 day"); | |
| 55 TestTimeFormats(1*day + 12*hour, "2 days"); | |
| 53 } | 56 } | 
| 54 | 57 | 
| 55 // crbug.com/159388: This test fails when daylight savings time ends. | 58 // crbug.com/159388: This test fails when daylight savings time ends. | 
| 56 TEST(TimeFormat, RelativeDate) { | 59 TEST(TimeFormat, RelativeDate) { | 
| 57 base::Time now = base::Time::Now(); | 60 base::Time now = base::Time::Now(); | 
| 58 base::string16 today_str = TimeFormat::RelativeDate(now, NULL); | 61 base::string16 today_str = TimeFormat::RelativeDate(now, NULL); | 
| 59 EXPECT_EQ(ASCIIToUTF16("Today"), today_str); | 62 EXPECT_EQ(ASCIIToUTF16("Today"), today_str); | 
| 60 | 63 | 
| 61 base::Time yesterday = now - TimeDelta::FromDays(1); | 64 base::Time yesterday = now - TimeDelta::FromDays(1); | 
| 62 base::string16 yesterday_str = TimeFormat::RelativeDate(yesterday, NULL); | 65 base::string16 yesterday_str = TimeFormat::RelativeDate(yesterday, NULL); | 
| 63 EXPECT_EQ(ASCIIToUTF16("Yesterday"), yesterday_str); | 66 EXPECT_EQ(ASCIIToUTF16("Yesterday"), yesterday_str); | 
| 64 | 67 | 
| 65 base::Time two_days_ago = now - TimeDelta::FromDays(2); | 68 base::Time two_days_ago = now - TimeDelta::FromDays(2); | 
| 66 base::string16 two_days_ago_str = | 69 base::string16 two_days_ago_str = | 
| 67 TimeFormat::RelativeDate(two_days_ago, NULL); | 70 TimeFormat::RelativeDate(two_days_ago, NULL); | 
| 68 EXPECT_TRUE(two_days_ago_str.empty()); | 71 EXPECT_TRUE(two_days_ago_str.empty()); | 
| 69 | 72 | 
| 70 base::Time a_week_ago = now - TimeDelta::FromDays(7); | 73 base::Time a_week_ago = now - TimeDelta::FromDays(7); | 
| 71 base::string16 a_week_ago_str = TimeFormat::RelativeDate(a_week_ago, NULL); | 74 base::string16 a_week_ago_str = TimeFormat::RelativeDate(a_week_ago, NULL); | 
| 72 EXPECT_TRUE(a_week_ago_str.empty()); | 75 EXPECT_TRUE(a_week_ago_str.empty()); | 
| 73 } | 76 } | 
| 74 | 77 | 
| 75 } // namespace | 78 } // namespace | 
| 76 } // namespace ui | 79 } // namespace ui | 
| OLD | NEW |