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