Skip to content

Commit

Permalink
Make timezone unittest less dependent on environment
Browse files Browse the repository at this point in the history
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 <jshin@chromium.org>
Commit-Queue: Evan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/master@{#622505}
  • Loading branch information
Evan Stade authored and Commit Bot committed Jan 14, 2019
1 parent 87e2aa2 commit b9e3249
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions base/i18n/timezone_unittest.cc
Expand Up @@ -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<icu::StringEnumeration> 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
Expand Down

0 comments on commit b9e3249

Please sign in to comment.