Commit 5c459e8d authored by David Dorwin's avatar David Dorwin Committed by Commit Bot

[fuchsia] Enable TimeTest.LocalTimeT to actually test local time

Fuchsia uses ICU rather than POSIX functions for Exploded time, but the
ICU time zone was not being set, so this test was always comparing UTC
time to UTC time.
The tests relies on localtime_r() to obtain the local time for
comparison, but POSIX local time functions always use UTC on Fuchsia so
the test must manually account for the time offset.

time_exploded_icu.cc::CreateCalendar() is removed.

Bug: 985946
Test: TimeTest.LocalTimeT fails if the |is_local| check in
Change-Id: I69701c37f5eea1eedd5020d46a2401818e73da16
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1952524Reviewed-by: default avatarWez <wez@chromium.org>
Reviewed-by: default avatarYuri Wiitala <miu@chromium.org>
Commit-Queue: David Dorwin <ddorwin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#723434}
parent a78920ef
......@@ -64,30 +64,12 @@ string16 TimeDurationFormatWithSecondsString(const TimeDelta& delta,
return str;
}
class ScopedRestoreDefaultTimezone {
public:
ScopedRestoreDefaultTimezone(const char* zoneid) {
original_zone_.reset(icu::TimeZone::createDefault());
icu::TimeZone::adoptDefault(icu::TimeZone::createTimeZone(zoneid));
}
~ScopedRestoreDefaultTimezone() {
icu::TimeZone::adoptDefault(original_zone_.release());
}
ScopedRestoreDefaultTimezone(const ScopedRestoreDefaultTimezone&) = delete;
ScopedRestoreDefaultTimezone& operator=(const ScopedRestoreDefaultTimezone&) =
delete;
private:
std::unique_ptr<icu::TimeZone> original_zone_;
};
TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault12h) {
// Test for a locale defaulted to 12h clock.
// As an instance, we use third_party/icu/source/data/locales/en.txt.
test::ScopedRestoreICUDefaultLocale restore_locale;
i18n::SetICUDefaultLocale("en_US");
ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
test::ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
Time time;
EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &time));
......@@ -125,7 +107,7 @@ TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault24h) {
// As an instance, we use third_party/icu/source/data/locales/en_GB.txt.
test::ScopedRestoreICUDefaultLocale restore_locale;
i18n::SetICUDefaultLocale("en_GB");
ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
test::ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
Time time;
EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &time));
......@@ -163,7 +145,7 @@ TEST(TimeFormattingTest, TimeFormatTimeOfDayJP) {
// As an instance, we use third_party/icu/source/data/locales/ja.txt.
test::ScopedRestoreICUDefaultLocale restore_locale;
i18n::SetICUDefaultLocale("ja_JP");
ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
test::ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
Time time;
EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &time));
......@@ -190,7 +172,7 @@ TEST(TimeFormattingTest, TimeFormatTimeOfDayDE) {
// German uses 24h by default, but uses 'AM', 'PM' for 12h format.
test::ScopedRestoreICUDefaultLocale restore_locale;
i18n::SetICUDefaultLocale("de");
ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
test::ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
Time time;
EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &time));
......@@ -226,7 +208,7 @@ TEST(TimeFormattingTest, TimeFormatDateUS) {
// The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy".
test::ScopedRestoreICUDefaultLocale restore_locale;
i18n::SetICUDefaultLocale("en_US");
ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
test::ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
Time time;
EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &time));
......@@ -253,7 +235,7 @@ TEST(TimeFormattingTest, TimeFormatDateGB) {
// The date patterns are "EEEE, d MMMM y", "d MMM y", and "dd/MM/yyyy".
test::ScopedRestoreICUDefaultLocale restore_locale;
i18n::SetICUDefaultLocale("en_GB");
ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
test::ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
Time time;
EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &time));
......@@ -273,7 +255,7 @@ TEST(TimeFormattingTest, TimeFormatDateGB) {
TEST(TimeFormattingTest, TimeFormatWithPattern) {
test::ScopedRestoreICUDefaultLocale restore_locale;
ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
test::ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
Time time;
EXPECT_TRUE(Time::FromUTCExploded(kTestDateTimeExploded, &time));
......@@ -392,7 +374,7 @@ TEST(TimeFormattingTest, TimeDurationFormatWithSeconds) {
TEST(TimeFormattingTest, TimeIntervalFormat) {
test::ScopedRestoreICUDefaultLocale restore_locale;
i18n::SetICUDefaultLocale("en_US");
ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
test::ScopedRestoreDefaultTimezone la_time("America/Los_Angeles");
const Time::Exploded kTestIntervalEndTimeExploded = {
2011, 5, 6, 28, // Sat, May 28, 2012
......
......@@ -9,6 +9,7 @@
#include "base/i18n/icu_util.h"
#include "base/i18n/rtl.h"
#include "third_party/icu/source/common/unicode/uloc.h"
#include "third_party/icu/source/i18n/unicode/timezone.h"
namespace base {
namespace test {
......@@ -27,6 +28,15 @@ ScopedRestoreICUDefaultLocale::~ScopedRestoreICUDefaultLocale() {
i18n::SetICUDefaultLocale(default_locale_.data());
}
ScopedRestoreDefaultTimezone::ScopedRestoreDefaultTimezone(const char* zoneid) {
original_zone_.reset(icu::TimeZone::createDefault());
icu::TimeZone::adoptDefault(icu::TimeZone::createTimeZone(zoneid));
}
ScopedRestoreDefaultTimezone::~ScopedRestoreDefaultTimezone() {
icu::TimeZone::adoptDefault(original_zone_.release());
}
void InitializeICUForTesting() {
if (!CommandLine::ForCurrentProcess()->HasSwitch(
switches::kTestDoNotInitializeIcu)) {
......
......@@ -8,6 +8,11 @@
#include <string>
#include "base/macros.h"
#include "third_party/icu/source/common/unicode/uversion.h"
U_NAMESPACE_BEGIN
class TimeZone;
U_NAMESPACE_END
namespace base {
namespace test {
......@@ -24,7 +29,25 @@ class ScopedRestoreICUDefaultLocale {
private:
const std::string default_locale_;
DISALLOW_COPY_AND_ASSIGN(ScopedRestoreICUDefaultLocale);
ScopedRestoreICUDefaultLocale(const ScopedRestoreICUDefaultLocale&) = delete;
ScopedRestoreICUDefaultLocale& operator=(
const ScopedRestoreICUDefaultLocale&) = delete;
};
// In unit tests, prefer ScopedRestoreDefaultTimezone over
// calling icu::TimeZone::adoptDefault() directly. This scoper makes it
// harder to accidentally forget to reset the locale.
class ScopedRestoreDefaultTimezone {
public:
ScopedRestoreDefaultTimezone(const char* zoneid);
~ScopedRestoreDefaultTimezone();
ScopedRestoreDefaultTimezone(const ScopedRestoreDefaultTimezone&) = delete;
ScopedRestoreDefaultTimezone& operator=(const ScopedRestoreDefaultTimezone&) =
delete;
private:
std::unique_ptr<icu::TimeZone> original_zone_;
};
void InitializeICUForTesting();
......
......@@ -22,6 +22,8 @@
#if defined(OS_ANDROID)
#include "base/android/jni_android.h"
#elif defined(OS_FUCHSIA)
#include "base/test/icu_test_util.h"
#elif defined(OS_WIN)
#include <windows.h>
#endif
......@@ -91,6 +93,15 @@ TEST(TimeTestOutOfBounds, FromExplodedOutOfBoundsTime) {
// See also pr_time_unittests.cc
class TimeTest : public testing::Test {
protected:
#if defined(OS_FUCHSIA)
// POSIX local time functions always use UTC on Fuchsia. As this is not very
// interesting for any "local" tests, set a different default ICU timezone for
// the test. This only affects code that uses ICU, such as Exploded time.
// Chicago is a non-Pacific time zone known to observe daylight saving time.
TimeTest() : chicago_time_("America/Chicago") {}
test::ScopedRestoreDefaultTimezone chicago_time_;
#endif
void SetUp() override {
// Use mktime to get a time_t, and turn it into a PRTime by converting
// seconds to microseconds. Use 15th Oct 2007 12:45:00 local. This
......@@ -188,10 +199,21 @@ TEST_F(TimeTest, LocalTimeT) {
// C library time and exploded time.
time_t now_t_1 = time(nullptr);
struct tm tms;
#if defined(OS_WIN)
localtime_s(&tms, &now_t_1);
#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
#elif defined(OS_POSIX)
localtime_r(&now_t_1, &tms);
#elif defined(OS_FUCHSIA)
// POSIX local time functions always use UTC on Fuchsia, so set a known time
// zone and manually obtain the local |tms| values by using an adjusted input.
// Hawaii does not observe daylight saving time, which is useful for having a
// constant offset below.
const int kHonoluluOffsetHours = -10;
const int kHonoluluOffsetSeconds = kHonoluluOffsetHours * 60 * 60;
test::ScopedRestoreDefaultTimezone honolulu_time("Pacific/Honolulu");
time_t adjusted_now_t_1 = now_t_1 + kHonoluluOffsetSeconds;
localtime_r(&adjusted_now_t_1, &tms);
#endif
// Convert to ours.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment