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