From b9e3249f9d44c8843af5d44d6f41f61ef1c9b564 Mon Sep 17 00:00:00 2001 From: Evan Stade Date: Mon, 14 Jan 2019 17:52:28 +0000 Subject: [PATCH] Make timezone unittest less dependent on environment Instead of only testing the current default timezone, test all timezones. Noticed this while looking into crbug.com/920094 Bug: none Change-Id: Ida24c063cefb384037fc77c3673ff2d73ab7354f Reviewed-on: https://chromium-review.googlesource.com/c/1407437 Reviewed-by: Jungshik Shin Commit-Queue: Evan Stade Cr-Commit-Position: refs/heads/master@{#622505} --- base/i18n/timezone_unittest.cc | 36 ++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/base/i18n/timezone_unittest.cc b/base/i18n/timezone_unittest.cc index 57467dced1b44b..b364cf79dd310a 100644 --- a/base/i18n/timezone_unittest.cc +++ b/base/i18n/timezone_unittest.cc @@ -5,22 +5,34 @@ #include "base/i18n/timezone.h" #include "testing/gtest/include/gtest/gtest.h" +#include "third_party/icu/source/common/unicode/strenum.h" +#include "third_party/icu/source/common/unicode/unistr.h" +#include "third_party/icu/source/i18n/unicode/timezone.h" namespace base { namespace { -TEST(TimezoneTest, CountryCodeForCurrentTimezone) { - std::string country_code = CountryCodeForCurrentTimezone(); - // On some systems (such as Android or some flavors of Linux), ICU may come up - // empty. With https://chromium-review.googlesource.com/c/512282/ , ICU will - // not fail any more. See also http://bugs.icu-project.org/trac/ticket/13208 . - // Even with that, ICU returns '001' (world) for region-agnostic timezones - // such as Etc/UTC and |CountryCodeForCurrentTimezone| returns an empty - // string so that the next fallback can be tried by a customer. - // TODO(jshin): Revise this to test for actual timezones using - // use ScopedRestoreICUDefaultTimezone. - if (!country_code.empty()) - EXPECT_EQ(2U, country_code.size()) << "country_code = " << country_code; +TEST(TimezoneTest, CountryCodeForTimezones) { + std::unique_ptr timezones( + icu::TimeZone::createEnumeration()); + + UErrorCode status; + while (const icu::UnicodeString* timezone = timezones->snext(status)) { + icu::TimeZone::adoptDefault(icu::TimeZone::createTimeZone(*timezone)); + + std::string country_code = CountryCodeForCurrentTimezone(); + // On some systems (such as Android or some flavors of Linux), ICU may come + // up empty. With https://chromium-review.googlesource.com/c/512282/ , ICU + // will not fail any more. See also + // http://bugs.icu-project.org/trac/ticket/13208 . Even with that, ICU + // returns '001' (world) for region-agnostic timezones such as Etc/UTC and + // |CountryCodeForCurrentTimezone| returns an empty string so that the next + // fallback can be tried by a customer. + if (!country_code.empty()) + EXPECT_EQ(2U, country_code.size()) << "country_code = " << country_code; + } + + icu::TimeZone::adoptDefault(nullptr); } } // namespace