Commit 6f8d7ca4 authored by Filip Filmar's avatar Filip Filmar Committed by Commit Bot

[i18n] Reinit icu_util state, correctly

The old code cleared ICU library internal state between test runs, but
not the icu_util internal state.  This caused the new ICU configurations
never to be loaded, since the default behavior of the loader is to make
loads lazy and idempotent.

Clearing the internal state of icu_util enabled loading new
ICU data in each individual test case.

This initialization issue may have contributed to arm64 test failures
that mysteriously came and went a few days back, possibly related to
test run ordering.

Bug: fuchsia:45491, 1047475
Change-Id: Ib9290952ea1955b27c752dcaf78a8a6de7fbf77b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2042057
Commit-Queue: Filip Filmar <fmil@google.com>
Reviewed-by: default avatarDavid Dorwin <ddorwin@chromium.org>
Reviewed-by: default avatarKevin Marshall <kmarshall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#740833}
parent 9d5503a4
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "base/environment.h" #include "base/environment.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/strings/string_util.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "third_party/icu/source/common/unicode/uclean.h" #include "third_party/icu/source/common/unicode/uclean.h"
...@@ -14,13 +15,33 @@ ...@@ -14,13 +15,33 @@
namespace base { namespace base {
namespace i18n { namespace i18n {
namespace {
// Directory path to the tzdata configuration files.
const char kTzDataDirPath[] = "/pkg/base/test/data/tzdata/2019a/44/le";
// File path to the text file containing the expected ICU library revision, for
// example "2019c".
const char kRevisionFilePath[] = "/config/data/tzdata/revision.txt";
} // namespace
class TimeZoneDataTest : public testing::Test { class TimeZoneDataTest : public testing::Test {
protected: protected:
TimeZoneDataTest() : env_(base::Environment::Create()) {} TimeZoneDataTest() : env_(base::Environment::Create()) {}
void SetUp() override { u_cleanup(); } void SetUp() override { ResetIcu(); }
void TearDown() override { u_cleanup(); } void TearDown() override { ResetIcu(); }
// Needed to enable loading of ICU config files that are different from what
// is available in Chromium. Both icu_util and ICU library keep internal
// state so clear both.
void ResetIcu() {
// Clears the state in the reverse order of construction.
u_cleanup();
ResetGlobalsForTesting();
}
void GetActualRevision(std::string* icu_version) { void GetActualRevision(std::string* icu_version) {
UErrorCode err = U_ZERO_ERROR; UErrorCode err = U_ZERO_ERROR;
...@@ -31,44 +52,59 @@ class TimeZoneDataTest : public testing::Test { ...@@ -31,44 +52,59 @@ class TimeZoneDataTest : public testing::Test {
std::unique_ptr<base::Environment> env_; std::unique_ptr<base::Environment> env_;
}; };
TEST_F(TimeZoneDataTest, DISABLED_RevisionFromConfig) { // Loads a file revision.txt from the actual underlying filesystem, which
// Config data is not available on Chromium test runners, so don't actually // contains the tzdata version we expect to be able to load. It then attempts
// run this test there. A marker for this is the presence of revision.txt. // to load this configuration from the default path and compares the version it
if (!base::PathExists(base::FilePath("/config/data/tzdata/revision.txt"))) // obtained from the load with the expected version, failing on version
// mismatch.
//
// In Fuchsia build bot setup, we ensure that the file revision.txt exists, so
// that this test is not skipped. In Chromium build bot setup, this file may
// not be present, in which case we skip running this test.
TEST_F(TimeZoneDataTest, CompareSystemRevisionWithExpected) {
if (!base::PathExists(base::FilePath(kRevisionFilePath))) {
LOG(INFO) << "Skipped test because tzdata config is not present";
return; return;
}
// Ensure that timezone data is loaded from the default location. // Ensure that timezone data is loaded from the default location.
ASSERT_TRUE(env_->UnSetVar("ICU_TIMEZONE_FILES_DIR")); ASSERT_TRUE(env_->UnSetVar("ICU_TIMEZONE_FILES_DIR"));
InitializeICU(); ASSERT_TRUE(InitializeICU());
std::string expected; std::string expected;
ASSERT_TRUE(base::ReadFileToString( ASSERT_TRUE(
base::FilePath("/config/data/tzdata/revision.txt"), &expected)); base::ReadFileToString(base::FilePath(kRevisionFilePath), &expected));
std::string actual; std::string actual;
GetActualRevision(&actual); GetActualRevision(&actual);
EXPECT_EQ(expected, actual); EXPECT_EQ(expected, actual);
} }
TEST_F(TimeZoneDataTest, DISABLED_RevisionFromTestData) { // Verifies that the current version of the ICU library in use can load ICU
// TODO(1047475): Adding more asserts in case they give more hints to the // data in a specific version format (in this case 44). Designed to fail if
// buildbot failures. // the ICU library version used in Chromium drifts from version 44 so much that
ASSERT_TRUE(base::DirectoryExists( // the library is no longer able to load the old tzdata. If the test fails,
base::FilePath("/pkg/base/test/data/tzdata/2019a/44/le"))); // this could be a sign that all platforms Chromium runs on need to upgrade the
ASSERT_TRUE(base::PathExists( // ICU library versions.
base::FilePath("/pkg/base/test/data/tzdata/2019a/44/le/zoneinfo64.res"))); TEST_F(TimeZoneDataTest, TestLoadingTimeZoneDataFromKnownConfigs) {
ASSERT_TRUE(base::PathExists(base::FilePath( ASSERT_TRUE(base::DirectoryExists(base::FilePath(kTzDataDirPath)));
"/pkg/base/test/data/tzdata/2019a/44/le/timezoneTypes.res"))); ASSERT_TRUE(env_->SetVar("ICU_TIMEZONE_FILES_DIR", kTzDataDirPath));
ASSERT_TRUE(base::PathExists(
base::FilePath("/pkg/base/test/data/tzdata/2019a/44/le/metaZones.res"))); EXPECT_TRUE(InitializeICU());
std::string actual;
// Ensure that timezone data is loaded from test data. GetActualRevision(&actual);
ASSERT_TRUE(env_->SetVar("ICU_TIMEZONE_FILES_DIR", EXPECT_EQ("2019a", actual) << "If ICU no longer supports this tzdata "
"/pkg/base/test/data/tzdata/2019a/44/le")); "version, tzdata version needs to be upgraded";
}
TEST_F(TimeZoneDataTest, DoesNotCrashWithInvalidPath) {
ASSERT_TRUE(env_->SetVar("ICU_TIMEZONE_FILES_DIR", "/some/nonexistent/path"));
EXPECT_TRUE(InitializeICU()); EXPECT_TRUE(InitializeICU());
std::string actual; std::string actual;
GetActualRevision(&actual); GetActualRevision(&actual);
EXPECT_EQ("2019a", actual); EXPECT_TRUE(
base::StartsWith(actual, "20", base::CompareCase::INSENSITIVE_ASCII))
<< "Got version: " << actual;
} }
} // namespace i18n } // namespace i18n
......
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