Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(465)

Side by Side Diff: ui/base/l10n/time_format_unittest.cc

Issue 139413005: Add tests for TimeRemainingLong() and TimeDurationLong() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/strings/string16.h" 8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/base/resource/resource_bundle.h" 12 #include "ui/base/resource/resource_bundle.h"
13 13
14 using base::ASCIIToUTF16; 14 using base::ASCIIToUTF16;
15 15
16 namespace ui { 16 namespace ui {
17 namespace { 17 namespace {
18 18
19 using base::TimeDelta; 19 using base::TimeDelta;
20 20
21 class TimeFormatTest : public ::testing::Test { 21 class TimeFormatTests : public ::testing::Test {
22 public: 22 protected:
23 static void SetUpTestCase() { 23 TimeFormatTests()
24 : one_day(TimeDelta::FromDays(1)),
25 one_hour(TimeDelta::FromHours(1)),
26 one_min(TimeDelta::FromMinutes(1)),
27 one_second(TimeDelta::FromSeconds(1)),
28 one_millisecond(TimeDelta::FromMilliseconds(1)),
29 zero(TimeDelta::FromMilliseconds(0)) {
24 LoadLocale(ui::ResourceBundle::GetSharedInstance() 30 LoadLocale(ui::ResourceBundle::GetSharedInstance()
25 .GetLocaleFilePath("en-US", true)); 31 .GetLocaleFilePath("en-US", true));
26 } 32 }
27 33 ~TimeFormatTests() { LoadLocale(base::FilePath()); }
28 static void TearDownTestCase() { 34 TimeDelta one_day, one_hour, one_min, one_second, one_millisecond, zero;
29 LoadLocale(base::FilePath());
30 }
31 35
32 private: 36 private:
33 static void LoadLocale(const base::FilePath& file_path) { 37 static void LoadLocale(const base::FilePath& file_path) {
34 ui::ResourceBundle::GetSharedInstance().OverrideLocalePakForTest(file_path); 38 ui::ResourceBundle::GetSharedInstance().OverrideLocalePakForTest(file_path);
35 ui::ResourceBundle::GetSharedInstance().ReloadLocaleResources("en-US"); 39 ui::ResourceBundle::GetSharedInstance().ReloadLocaleResources("en-US");
36 } 40 }
37 }; 41 };
38 42
39 void TestTimeFormats(const TimeDelta& delta, const char* expected_ascii) { 43 void TestElapsed(const TimeDelta& delta, const std::string& expected) {
40 base::string16 expected = ASCIIToUTF16(expected_ascii); 44 EXPECT_EQ(TimeFormat::TimeElapsed(delta), ASCIIToUTF16(expected));
41 base::string16 expected_left = expected + ASCIIToUTF16(" left");
42 base::string16 expected_ago = expected + ASCIIToUTF16(" ago");
43 EXPECT_EQ(expected, TimeFormat::TimeDurationShort(delta));
44 EXPECT_EQ(expected_left, TimeFormat::TimeRemaining(delta));
45 EXPECT_EQ(expected_ago, TimeFormat::TimeElapsed(delta));
46 } 45 }
47 46
48 TEST_F(TimeFormatTest, FormatTime) { 47 TEST_F(TimeFormatTests, TimeElapsed) {
49 const TimeDelta one_day = TimeDelta::FromDays(1); 48 TestElapsed(zero, "0 secs ago");
50 const TimeDelta one_hour = TimeDelta::FromHours(1); 49 TestElapsed(499 * one_millisecond, "0 secs ago");
51 const TimeDelta one_min = TimeDelta::FromMinutes(1); 50 TestElapsed(500 * one_millisecond, "1 sec ago");
52 const TimeDelta one_second = TimeDelta::FromSeconds(1); 51 TestElapsed(one_second + 499 * one_millisecond, "1 sec ago");
53 const TimeDelta one_millisecond = TimeDelta::FromMilliseconds(1); 52 TestElapsed(one_second + 500 * one_millisecond, "2 secs ago");
54 const TimeDelta zero = TimeDelta::FromMilliseconds(0); 53 TestElapsed(59 * one_second + 499 * one_millisecond, "59 secs ago");
54 TestElapsed(59 * one_second + 500 * one_millisecond, "1 min ago");
55 TestElapsed(one_min + 30 * one_second - one_millisecond, "1 min ago");
56 TestElapsed(one_min + 30 * one_second, "2 mins ago");
57 TestElapsed(59 * one_min + 30 * one_second - one_millisecond, "59 mins ago");
58 TestElapsed(59 * one_min + 30 * one_second, "1 hour ago");
59 TestElapsed(one_hour + 30 * one_min - one_millisecond, "1 hour ago");
60 TestElapsed(one_hour + 30 * one_min, "2 hours ago");
61 TestElapsed(23 * one_hour + 30 * one_min - one_millisecond, "23 hours ago");
62 TestElapsed(23 * one_hour + 30 * one_min, "1 day ago");
63 TestElapsed(one_day + 12 * one_hour - one_millisecond, "1 day ago");
64 TestElapsed(one_day + 12 * one_hour, "2 days ago");
65 }
55 66
56 TestTimeFormats(zero, "0 secs"); 67 void TestRemaining(const TimeDelta& delta, const std::string& expected) {
57 TestTimeFormats(499 * one_millisecond, "0 secs"); 68 EXPECT_EQ(TimeFormat::TimeRemaining(delta), ASCIIToUTF16(expected));
58 TestTimeFormats(500 * one_millisecond, "1 sec"); 69 }
59 TestTimeFormats(1 * one_second + 499 * one_millisecond, "1 sec"); 70
60 TestTimeFormats(1 * one_second + 500 * one_millisecond, "2 secs"); 71 TEST_F(TimeFormatTests, TimeRemaining) {
61 TestTimeFormats(59 * one_second + 499 * one_millisecond, "59 secs"); 72 TestRemaining(zero, "0 secs left");
62 TestTimeFormats(59 * one_second + 500 * one_millisecond, "1 min"); 73 TestRemaining(499 * one_millisecond, "0 secs left");
63 TestTimeFormats(1 * one_min + 30 * one_second - one_millisecond, "1 min"); 74 TestRemaining(500 * one_millisecond, "1 sec left");
64 TestTimeFormats(1 * one_min + 30 * one_second, "2 mins"); 75 TestRemaining(one_second + 499 * one_millisecond, "1 sec left");
65 TestTimeFormats(59 * one_min + 30 * one_second - one_millisecond, "59 mins"); 76 TestRemaining(one_second + 500 * one_millisecond, "2 secs left");
66 TestTimeFormats(59 * one_min + 30 * one_second, "1 hour"); 77 TestRemaining(59 * one_second + 499 * one_millisecond, "59 secs left");
67 TestTimeFormats(1 * one_hour + 30 * one_min - one_millisecond, "1 hour"); 78 TestRemaining(59 * one_second + 500 * one_millisecond, "1 min left");
68 TestTimeFormats(1 * one_hour + 30 * one_min, "2 hours"); 79 TestRemaining(one_min + 30 * one_second - one_millisecond, "1 min left");
69 TestTimeFormats(23 * one_hour + 30 * one_min - one_millisecond, "23 hours"); 80 TestRemaining(one_min + 30 * one_second, "2 mins left");
70 TestTimeFormats(23 * one_hour + 30 * one_min, "1 day"); 81 TestRemaining(59 * one_min + 30 * one_second - one_millisecond,
71 TestTimeFormats(1 * one_day + 12 * one_hour - one_millisecond, "1 day"); 82 "59 mins left");
72 TestTimeFormats(1 * one_day + 12 * one_hour, "2 days"); 83 TestRemaining(59 * one_min + 30 * one_second, "1 hour left");
84 TestRemaining(one_hour + 30 * one_min - one_millisecond, "1 hour left");
85 TestRemaining(one_hour + 30 * one_min, "2 hours left");
86 TestRemaining(23 * one_hour + 30 * one_min - one_millisecond,
87 "23 hours left");
88 TestRemaining(23 * one_hour + 30 * one_min, "1 day left");
89 TestRemaining(one_day + 12 * one_hour - one_millisecond, "1 day left");
90 TestRemaining(one_day + 12 * one_hour, "2 days left");
91 }
92
93 void TestRemainingLong(const TimeDelta& delta, const std::string& expected) {
94 EXPECT_EQ(TimeFormat::TimeRemainingLong(delta), ASCIIToUTF16(expected));
95 }
96
97 TEST_F(TimeFormatTests, TimeRemainingLong) {
98 TestRemainingLong(zero, "0 secs left");
99 TestRemainingLong(499 * one_millisecond, "0 secs left");
100 TestRemainingLong(500 * one_millisecond, "1 sec left");
101 TestRemainingLong(one_second + 499 * one_millisecond, "1 sec left");
102 TestRemainingLong(one_second + 500 * one_millisecond, "2 secs left");
103 TestRemainingLong(59 * one_second + 499 * one_millisecond, "59 secs left");
104 TestRemainingLong(59 * one_second + 500 * one_millisecond, "1 minute left");
105 TestRemainingLong(one_min + 30 * one_second - one_millisecond,
106 "1 minute left");
107 TestRemainingLong(one_min + 30 * one_second, "2 minutes left");
108 TestRemainingLong(59 * one_min + 30 * one_second - one_millisecond,
109 "59 minutes left");
110 TestRemainingLong(59 * one_min + 30 * one_second, "1 hour left");
111 TestRemainingLong(one_hour + 30 * one_min - one_millisecond, "1 hour left");
112 TestRemainingLong(one_hour + 30 * one_min, "2 hours left");
113 TestRemainingLong(23 * one_hour + 30 * one_min - one_millisecond,
114 "23 hours left");
115 TestRemainingLong(23 * one_hour + 30 * one_min, "1 day left");
116 TestRemainingLong(one_day + 12 * one_hour - one_millisecond, "1 day left");
117 TestRemainingLong(one_day + 12 * one_hour, "2 days left");
118 }
119
120 void TestDurationShort(const TimeDelta& delta, const std::string& expected) {
121 EXPECT_EQ(TimeFormat::TimeDurationShort(delta), ASCIIToUTF16(expected));
122 }
123
124 TEST_F(TimeFormatTests, TimeDurationShort) {
125 TestDurationShort(zero, "0 secs");
126 TestDurationShort(499 * one_millisecond, "0 secs");
127 TestDurationShort(500 * one_millisecond, "1 sec");
128 TestDurationShort(one_second + 499 * one_millisecond, "1 sec");
129 TestDurationShort(one_second + 500 * one_millisecond, "2 secs");
130 TestDurationShort(59 * one_second + 499 * one_millisecond, "59 secs");
131 TestDurationShort(59 * one_second + 500 * one_millisecond, "1 min");
132 TestDurationShort(one_min + 30 * one_second - one_millisecond, "1 min");
133 TestDurationShort(one_min + 30 * one_second, "2 mins");
134 TestDurationShort(59 * one_min + 30 * one_second - one_millisecond,
135 "59 mins");
136 TestDurationShort(59 * one_min + 30 * one_second, "1 hour");
137 TestDurationShort(one_hour + 30 * one_min - one_millisecond, "1 hour");
138 TestDurationShort(one_hour + 30 * one_min, "2 hours");
139 TestDurationShort(23 * one_hour + 30 * one_min - one_millisecond, "23 hours");
140 TestDurationShort(23 * one_hour + 30 * one_min, "1 day");
141 TestDurationShort(one_day + 12 * one_hour - one_millisecond, "1 day");
142 TestDurationShort(one_day + 12 * one_hour, "2 days");
143 }
144
145 void TestDurationLong(const TimeDelta& delta, const std::string& expected) {
146 EXPECT_EQ(TimeFormat::TimeDurationLong(delta), ASCIIToUTF16(expected));
147 }
148
149 TEST_F(TimeFormatTests, TimeDurationLong) {
150 TestDurationLong(zero, "0 seconds");
151 TestDurationLong(499 * one_millisecond, "0 seconds");
152 TestDurationLong(500 * one_millisecond, "1 second");
153 TestDurationLong(one_second + 499 * one_millisecond, "1 second");
154 TestDurationLong(one_second + 500 * one_millisecond, "2 seconds");
155 TestDurationLong(59 * one_second + 499 * one_millisecond, "59 seconds");
156 TestDurationLong(59 * one_second + 500 * one_millisecond, "1 minute");
157 TestDurationLong(one_min + 30 * one_second - one_millisecond, "1 minute");
158 TestDurationLong(one_min + 30 * one_second, "2 minutes");
159 TestDurationLong(59 * one_min + 30 * one_second - one_millisecond,
160 "59 minutes");
161 TestDurationLong(59 * one_min + 30 * one_second, "1 hour");
162 TestDurationLong(one_hour + 30 * one_min - one_millisecond, "1 hour");
163 TestDurationLong(one_hour + 30 * one_min, "2 hours");
164 TestDurationLong(23 * one_hour + 30 * one_min - one_millisecond, "23 hours");
165 TestDurationLong(23 * one_hour + 30 * one_min, "1 day");
166 TestDurationLong(one_day + 12 * one_hour - one_millisecond, "1 day");
167 TestDurationLong(one_day + 12 * one_hour, "2 days");
73 } 168 }
74 169
75 // crbug.com/159388: This test fails when daylight savings time ends. 170 // crbug.com/159388: This test fails when daylight savings time ends.
76 TEST_F(TimeFormatTest, RelativeDate) { 171 TEST_F(TimeFormatTests, RelativeDate) {
77 base::Time now = base::Time::Now(); 172 base::Time now = base::Time::Now();
78 base::string16 today_str = TimeFormat::RelativeDate(now, NULL); 173 base::string16 today_str = TimeFormat::RelativeDate(now, NULL);
79 EXPECT_EQ(ASCIIToUTF16("Today"), today_str); 174 EXPECT_EQ(ASCIIToUTF16("Today"), today_str);
80 175
81 base::Time yesterday = now - TimeDelta::FromDays(1); 176 base::Time yesterday = now - TimeDelta::FromDays(1);
82 base::string16 yesterday_str = TimeFormat::RelativeDate(yesterday, NULL); 177 base::string16 yesterday_str = TimeFormat::RelativeDate(yesterday, NULL);
83 EXPECT_EQ(ASCIIToUTF16("Yesterday"), yesterday_str); 178 EXPECT_EQ(ASCIIToUTF16("Yesterday"), yesterday_str);
84 179
85 base::Time two_days_ago = now - TimeDelta::FromDays(2); 180 base::Time two_days_ago = now - TimeDelta::FromDays(2);
86 base::string16 two_days_ago_str = 181 base::string16 two_days_ago_str =
87 TimeFormat::RelativeDate(two_days_ago, NULL); 182 TimeFormat::RelativeDate(two_days_ago, NULL);
88 EXPECT_TRUE(two_days_ago_str.empty()); 183 EXPECT_TRUE(two_days_ago_str.empty());
89 184
90 base::Time a_week_ago = now - TimeDelta::FromDays(7); 185 base::Time a_week_ago = now - TimeDelta::FromDays(7);
91 base::string16 a_week_ago_str = TimeFormat::RelativeDate(a_week_ago, NULL); 186 base::string16 a_week_ago_str = TimeFormat::RelativeDate(a_week_ago, NULL);
92 EXPECT_TRUE(a_week_ago_str.empty()); 187 EXPECT_TRUE(a_week_ago_str.empty());
93 } 188 }
94 189
95 } // namespace 190 } // namespace
96 } // namespace ui 191 } // namespace ui
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698