blob: eaee957136f0e4c66c52f165289fcc58574376ff [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
Ben Murdochbb1529c2013-08-08 10:24:53 +01005#include "ui/base/l10n/time_format.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +00006
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"
Ben Murdochbb1529c2013-08-08 10:24:53 +010011#include "ui/base/resource/resource_bundle.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000012
Ben Murdochbb1529c2013-08-08 10:24:53 +010013namespace ui {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000014namespace {
15
16using base::TimeDelta;
17
18void 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
27TEST(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.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000054TEST(TimeFormat, RelativeDate) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000055 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
Ben Murdochbb1529c2013-08-08 10:24:53 +010073} // namespace ui