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

Side by Side Diff: base/i18n/time_formatting_unittest.cc

Issue 2745233002: base: Make TimeDurationFormat* report failures. (Closed)
Patch Set: merge Created 3 years, 9 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
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 "base/i18n/time_formatting.h" 5 #include "base/i18n/time_formatting.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/i18n/rtl.h" 9 #include "base/i18n/rtl.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "base/test/icu_test_util.h" 11 #include "base/test/icu_test_util.h"
12 #include "base/time/time.h" 12 #include "base/time/time.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/icu/source/common/unicode/uversion.h" 14 #include "third_party/icu/source/common/unicode/uversion.h"
15 #include "third_party/icu/source/i18n/unicode/calendar.h" 15 #include "third_party/icu/source/i18n/unicode/calendar.h"
16 #include "third_party/icu/source/i18n/unicode/timezone.h" 16 #include "third_party/icu/source/i18n/unicode/timezone.h"
17 #include "third_party/icu/source/i18n/unicode/tzfmt.h" 17 #include "third_party/icu/source/i18n/unicode/tzfmt.h"
18 18
19 namespace base { 19 namespace base {
20 namespace { 20 namespace {
21 21
22 const Time::Exploded kTestDateTimeExploded = { 22 const Time::Exploded kTestDateTimeExploded = {
23 2011, 4, 6, 30, // Sat, Apr 30, 2011 23 2011, 4, 6, 30, // Sat, Apr 30, 2011
24 15, 42, 7, 0 // 15:42:07.000 24 15, 42, 7, 0 // 15:42:07.000
25 }; 25 };
26 26
27 // Returns difference between the local time and GMT formatted as string. 27 // Returns difference between the local time and GMT formatted as string.
28 // This function gets |time| because the difference depends on time, 28 // This function gets |time| because the difference depends on time,
29 // see https://en.wikipedia.org/wiki/Daylight_saving_time for details. 29 // see https://en.wikipedia.org/wiki/Daylight_saving_time for details.
30 base::string16 GetShortTimeZone(const Time& time) { 30 string16 GetShortTimeZone(const Time& time) {
31 UErrorCode status = U_ZERO_ERROR; 31 UErrorCode status = U_ZERO_ERROR;
32 std::unique_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault()); 32 std::unique_ptr<icu::TimeZone> zone(icu::TimeZone::createDefault());
33 std::unique_ptr<icu::TimeZoneFormat> zone_formatter( 33 std::unique_ptr<icu::TimeZoneFormat> zone_formatter(
34 icu::TimeZoneFormat::createInstance(icu::Locale::getDefault(), status)); 34 icu::TimeZoneFormat::createInstance(icu::Locale::getDefault(), status));
35 EXPECT_TRUE(U_SUCCESS(status)); 35 EXPECT_TRUE(U_SUCCESS(status));
36 icu::UnicodeString name; 36 icu::UnicodeString name;
37 zone_formatter->format(UTZFMT_STYLE_SPECIFIC_SHORT, *zone, 37 zone_formatter->format(UTZFMT_STYLE_SPECIFIC_SHORT, *zone,
38 static_cast<UDate>(time.ToDoubleT() * 1000), 38 static_cast<UDate>(time.ToDoubleT() * 1000),
39 name, nullptr); 39 name, nullptr);
40 return base::string16(name.getBuffer(), name.length()); 40 return string16(name.getBuffer(), name.length());
41 }
42
43 // Calls TimeDurationFormat() with |delta| and |width| and returns the resulting
44 // string. On failure, adds a failed expectation and returns an empty string.
45 string16 TimeDurationFormatString(const TimeDelta& delta,
46 DurationFormatWidth width) {
47 string16 str;
48 EXPECT_TRUE(TimeDurationFormat(delta, width, &str))
49 << "Failed to format " << delta.ToInternalValue() << " with width "
50 << width;
51 return str;
52 }
53
54 // Calls TimeDurationFormatWithSeconds() with |delta| and |width| and returns
55 // the resulting string. On failure, adds a failed expectation and returns an
56 // empty string.
57 string16 TimeDurationFormatWithSecondsString(const TimeDelta& delta,
58 DurationFormatWidth width) {
59 string16 str;
60 EXPECT_TRUE(TimeDurationFormatWithSeconds(delta, width, &str))
61 << "Failed to format " << delta.ToInternalValue() << " with width "
62 << width;
63 return str;
41 } 64 }
42 65
43 #if defined(OS_ANDROID) 66 #if defined(OS_ANDROID)
44 #define MAYBE_TimeFormatTimeOfDayDefault12h \ 67 #define MAYBE_TimeFormatTimeOfDayDefault12h \
45 DISABLED_TimeFormatTimeOfDayDefault12h 68 DISABLED_TimeFormatTimeOfDayDefault12h
46 #else 69 #else
47 #define MAYBE_TimeFormatTimeOfDayDefault12h TimeFormatTimeOfDayDefault12h 70 #define MAYBE_TimeFormatTimeOfDayDefault12h TimeFormatTimeOfDayDefault12h
48 #endif 71 #endif
49 TEST(TimeFormattingTest, MAYBE_TimeFormatTimeOfDayDefault12h) { 72 TEST(TimeFormattingTest, MAYBE_TimeFormatTimeOfDayDefault12h) {
50 // Test for a locale defaulted to 12h clock. 73 // Test for a locale defaulted to 12h clock.
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 TimeFormatFriendlyDate(time)); 244 TimeFormatFriendlyDate(time));
222 } 245 }
223 246
224 TEST(TimeFormattingTest, TimeDurationFormat) { 247 TEST(TimeFormattingTest, TimeDurationFormat) {
225 test::ScopedRestoreICUDefaultLocale restore_locale; 248 test::ScopedRestoreICUDefaultLocale restore_locale;
226 TimeDelta delta = TimeDelta::FromMinutes(15 * 60 + 42); 249 TimeDelta delta = TimeDelta::FromMinutes(15 * 60 + 42);
227 250
228 // US English. 251 // US English.
229 i18n::SetICUDefaultLocale("en_US"); 252 i18n::SetICUDefaultLocale("en_US");
230 EXPECT_EQ(ASCIIToUTF16("15 hours, 42 minutes"), 253 EXPECT_EQ(ASCIIToUTF16("15 hours, 42 minutes"),
231 TimeDurationFormat(delta, DURATION_WIDTH_WIDE)); 254 TimeDurationFormatString(delta, DURATION_WIDTH_WIDE));
232 EXPECT_EQ(ASCIIToUTF16("15 hr, 42 min"), 255 EXPECT_EQ(ASCIIToUTF16("15 hr, 42 min"),
233 TimeDurationFormat(delta, DURATION_WIDTH_SHORT)); 256 TimeDurationFormatString(delta, DURATION_WIDTH_SHORT));
234 EXPECT_EQ(ASCIIToUTF16("15h 42m"), 257 EXPECT_EQ(ASCIIToUTF16("15h 42m"),
235 TimeDurationFormat(delta, DURATION_WIDTH_NARROW)); 258 TimeDurationFormatString(delta, DURATION_WIDTH_NARROW));
236 EXPECT_EQ(ASCIIToUTF16("15:42"), 259 EXPECT_EQ(ASCIIToUTF16("15:42"),
237 TimeDurationFormat(delta, DURATION_WIDTH_NUMERIC)); 260 TimeDurationFormatString(delta, DURATION_WIDTH_NUMERIC));
238 261
239 // Danish, with Latin alphabet but different abbreviations and punctuation. 262 // Danish, with Latin alphabet but different abbreviations and punctuation.
240 i18n::SetICUDefaultLocale("da"); 263 i18n::SetICUDefaultLocale("da");
241 EXPECT_EQ(ASCIIToUTF16("15 timer og 42 minutter"), 264 EXPECT_EQ(ASCIIToUTF16("15 timer og 42 minutter"),
242 TimeDurationFormat(delta, DURATION_WIDTH_WIDE)); 265 TimeDurationFormatString(delta, DURATION_WIDTH_WIDE));
243 EXPECT_EQ(ASCIIToUTF16("15 t og 42 min."), 266 EXPECT_EQ(ASCIIToUTF16("15 t og 42 min."),
244 TimeDurationFormat(delta, DURATION_WIDTH_SHORT)); 267 TimeDurationFormatString(delta, DURATION_WIDTH_SHORT));
245 EXPECT_EQ(ASCIIToUTF16("15 t og 42 min"), 268 EXPECT_EQ(ASCIIToUTF16("15 t og 42 min"),
246 TimeDurationFormat(delta, DURATION_WIDTH_NARROW)); 269 TimeDurationFormatString(delta, DURATION_WIDTH_NARROW));
247 EXPECT_EQ(ASCIIToUTF16("15.42"), 270 EXPECT_EQ(ASCIIToUTF16("15.42"),
248 TimeDurationFormat(delta, DURATION_WIDTH_NUMERIC)); 271 TimeDurationFormatString(delta, DURATION_WIDTH_NUMERIC));
249 272
250 // Persian, with non-Arabic numbers. 273 // Persian, with non-Arabic numbers.
251 i18n::SetICUDefaultLocale("fa"); 274 i18n::SetICUDefaultLocale("fa");
252 string16 fa_wide = WideToUTF16( 275 string16 fa_wide = WideToUTF16(
253 L"\x6f1\x6f5\x20\x633\x627\x639\x62a\x20\x648\x20\x6f4\x6f2\x20\x62f\x642" 276 L"\x6f1\x6f5\x20\x633\x627\x639\x62a\x20\x648\x20\x6f4\x6f2\x20\x62f\x642"
254 L"\x6cc\x642\x647"); 277 L"\x6cc\x642\x647");
255 string16 fa_short = WideToUTF16( 278 string16 fa_short = WideToUTF16(
256 L"\x6f1\x6f5\x20\x633\x627\x639\x62a\x60c\x200f\x20\x6f4\x6f2\x20\x62f" 279 L"\x6f1\x6f5\x20\x633\x627\x639\x62a\x60c\x200f\x20\x6f4\x6f2\x20\x62f"
257 L"\x642\x6cc\x642\x647"); 280 L"\x642\x6cc\x642\x647");
258 string16 fa_narrow = WideToUTF16( 281 string16 fa_narrow = WideToUTF16(
259 L"\x6f1\x6f5\x20\x633\x627\x639\x62a\x20\x6f4\x6f2\x20\x62f\x642\x6cc" 282 L"\x6f1\x6f5\x20\x633\x627\x639\x62a\x20\x6f4\x6f2\x20\x62f\x642\x6cc"
260 L"\x642\x647"); 283 L"\x642\x647");
261 string16 fa_numeric = WideToUTF16(L"\x6f1\x6f5\x3a\x6f4\x6f2"); 284 string16 fa_numeric = WideToUTF16(L"\x6f1\x6f5\x3a\x6f4\x6f2");
262 EXPECT_EQ(fa_wide, TimeDurationFormat(delta, DURATION_WIDTH_WIDE)); 285 EXPECT_EQ(fa_wide, TimeDurationFormatString(delta, DURATION_WIDTH_WIDE));
263 EXPECT_EQ(fa_short, TimeDurationFormat(delta, DURATION_WIDTH_SHORT)); 286 EXPECT_EQ(fa_short, TimeDurationFormatString(delta, DURATION_WIDTH_SHORT));
264 EXPECT_EQ(fa_narrow, TimeDurationFormat(delta, DURATION_WIDTH_NARROW)); 287 EXPECT_EQ(fa_narrow, TimeDurationFormatString(delta, DURATION_WIDTH_NARROW));
265 EXPECT_EQ(fa_numeric, TimeDurationFormat(delta, DURATION_WIDTH_NUMERIC)); 288 EXPECT_EQ(fa_numeric,
289 TimeDurationFormatString(delta, DURATION_WIDTH_NUMERIC));
266 } 290 }
267 291
268 TEST(TimeFormattingTest, TimeDurationFormatWithSeconds) { 292 TEST(TimeFormattingTest, TimeDurationFormatWithSeconds) {
269 test::ScopedRestoreICUDefaultLocale restore_locale; 293 test::ScopedRestoreICUDefaultLocale restore_locale;
270 294
271 // US English. 295 // US English.
272 i18n::SetICUDefaultLocale("en_US"); 296 i18n::SetICUDefaultLocale("en_US");
273 297
274 // Test different formats. 298 // Test different formats.
275 TimeDelta delta = TimeDelta::FromSeconds(15 * 3600 + 42 * 60 + 30); 299 TimeDelta delta = TimeDelta::FromSeconds(15 * 3600 + 42 * 60 + 30);
276 EXPECT_EQ(ASCIIToUTF16("15 hours, 42 minutes, 30 seconds"), 300 EXPECT_EQ(ASCIIToUTF16("15 hours, 42 minutes, 30 seconds"),
277 TimeDurationFormatWithSeconds(delta, DURATION_WIDTH_WIDE)); 301 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_WIDE));
278 EXPECT_EQ(ASCIIToUTF16("15 hr, 42 min, 30 sec"), 302 EXPECT_EQ(ASCIIToUTF16("15 hr, 42 min, 30 sec"),
279 TimeDurationFormatWithSeconds(delta, DURATION_WIDTH_SHORT)); 303 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_SHORT));
280 EXPECT_EQ(ASCIIToUTF16("15h 42m 30s"), 304 EXPECT_EQ(ASCIIToUTF16("15h 42m 30s"),
281 TimeDurationFormatWithSeconds(delta, DURATION_WIDTH_NARROW)); 305 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NARROW));
282 EXPECT_EQ(ASCIIToUTF16("15:42:30"), 306 EXPECT_EQ(ASCIIToUTF16("15:42:30"),
283 TimeDurationFormatWithSeconds(delta, DURATION_WIDTH_NUMERIC)); 307 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NUMERIC));
284 308
285 // Test edge case when hour >= 100. 309 // Test edge case when hour >= 100.
286 delta = TimeDelta::FromSeconds(125 * 3600 + 42 * 60 + 30); 310 delta = TimeDelta::FromSeconds(125 * 3600 + 42 * 60 + 30);
287 EXPECT_EQ(ASCIIToUTF16("125 hours, 42 minutes, 30 seconds"), 311 EXPECT_EQ(ASCIIToUTF16("125 hours, 42 minutes, 30 seconds"),
288 TimeDurationFormatWithSeconds(delta, DURATION_WIDTH_WIDE)); 312 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_WIDE));
289 EXPECT_EQ(ASCIIToUTF16("125 hr, 42 min, 30 sec"), 313 EXPECT_EQ(ASCIIToUTF16("125 hr, 42 min, 30 sec"),
290 TimeDurationFormatWithSeconds(delta, DURATION_WIDTH_SHORT)); 314 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_SHORT));
291 EXPECT_EQ(ASCIIToUTF16("125h 42m 30s"), 315 EXPECT_EQ(ASCIIToUTF16("125h 42m 30s"),
292 TimeDurationFormatWithSeconds(delta, DURATION_WIDTH_NARROW)); 316 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NARROW));
293 317
294 // Test edge case when minute = 0. 318 // Test edge case when minute = 0.
295 delta = TimeDelta::FromSeconds(15 * 3600 + 0 * 60 + 30); 319 delta = TimeDelta::FromSeconds(15 * 3600 + 0 * 60 + 30);
296 EXPECT_EQ(ASCIIToUTF16("15 hours, 0 minutes, 30 seconds"), 320 EXPECT_EQ(ASCIIToUTF16("15 hours, 0 minutes, 30 seconds"),
297 TimeDurationFormatWithSeconds(delta, DURATION_WIDTH_WIDE)); 321 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_WIDE));
298 EXPECT_EQ(ASCIIToUTF16("15 hr, 0 min, 30 sec"), 322 EXPECT_EQ(ASCIIToUTF16("15 hr, 0 min, 30 sec"),
299 TimeDurationFormatWithSeconds(delta, DURATION_WIDTH_SHORT)); 323 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_SHORT));
300 EXPECT_EQ(ASCIIToUTF16("15h 0m 30s"), 324 EXPECT_EQ(ASCIIToUTF16("15h 0m 30s"),
301 TimeDurationFormatWithSeconds(delta, DURATION_WIDTH_NARROW)); 325 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NARROW));
302 EXPECT_EQ(ASCIIToUTF16("15:00:30"), 326 EXPECT_EQ(ASCIIToUTF16("15:00:30"),
303 TimeDurationFormatWithSeconds(delta, DURATION_WIDTH_NUMERIC)); 327 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NUMERIC));
304 328
305 // Test edge case when second = 0. 329 // Test edge case when second = 0.
306 delta = TimeDelta::FromSeconds(15 * 3600 + 42 * 60 + 0); 330 delta = TimeDelta::FromSeconds(15 * 3600 + 42 * 60 + 0);
307 EXPECT_EQ(ASCIIToUTF16("15 hours, 42 minutes, 0 seconds"), 331 EXPECT_EQ(ASCIIToUTF16("15 hours, 42 minutes, 0 seconds"),
308 TimeDurationFormatWithSeconds(delta, DURATION_WIDTH_WIDE)); 332 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_WIDE));
309 EXPECT_EQ(ASCIIToUTF16("15 hr, 42 min, 0 sec"), 333 EXPECT_EQ(ASCIIToUTF16("15 hr, 42 min, 0 sec"),
310 TimeDurationFormatWithSeconds(delta, DURATION_WIDTH_SHORT)); 334 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_SHORT));
311 EXPECT_EQ(ASCIIToUTF16("15h 42m 0s"), 335 EXPECT_EQ(ASCIIToUTF16("15h 42m 0s"),
312 TimeDurationFormatWithSeconds(delta, DURATION_WIDTH_NARROW)); 336 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NARROW));
313 EXPECT_EQ(ASCIIToUTF16("15:42:00"), 337 EXPECT_EQ(ASCIIToUTF16("15:42:00"),
314 TimeDurationFormatWithSeconds(delta, DURATION_WIDTH_NUMERIC)); 338 TimeDurationFormatWithSecondsString(delta, DURATION_WIDTH_NUMERIC));
315 } 339 }
316 340
317 } // namespace 341 } // namespace
318 } // namespace base 342 } // namespace base
OLDNEW
« no previous file with comments | « base/i18n/time_formatting.cc ('k') | chrome/browser/ui/task_manager/task_manager_table_model.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698