icu_test_util.cc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/test/icu_test_util.h"
  5. #include "base/base_switches.h"
  6. #include "base/command_line.h"
  7. #include "base/i18n/icu_util.h"
  8. #include "base/i18n/rtl.h"
  9. #include "third_party/icu/source/common/unicode/uloc.h"
  10. #include "third_party/icu/source/i18n/unicode/timezone.h"
  11. namespace base {
  12. namespace test {
  13. ScopedRestoreICUDefaultLocale::ScopedRestoreICUDefaultLocale()
  14. : ScopedRestoreICUDefaultLocale(std::string()) {}
  15. ScopedRestoreICUDefaultLocale::ScopedRestoreICUDefaultLocale(
  16. const std::string& locale)
  17. : default_locale_(uloc_getDefault()) {
  18. if (!locale.empty())
  19. i18n::SetICUDefaultLocale(locale.data());
  20. }
  21. ScopedRestoreICUDefaultLocale::~ScopedRestoreICUDefaultLocale() {
  22. i18n::SetICUDefaultLocale(default_locale_.data());
  23. }
  24. ScopedRestoreDefaultTimezone::ScopedRestoreDefaultTimezone(const char* zoneid) {
  25. original_zone_.reset(icu::TimeZone::createDefault());
  26. icu::TimeZone::adoptDefault(icu::TimeZone::createTimeZone(zoneid));
  27. }
  28. ScopedRestoreDefaultTimezone::~ScopedRestoreDefaultTimezone() {
  29. icu::TimeZone::adoptDefault(original_zone_.release());
  30. }
  31. void InitializeICUForTesting() {
  32. if (!CommandLine::ForCurrentProcess()->HasSwitch(
  33. switches::kTestDoNotInitializeIcu)) {
  34. i18n::AllowMultipleInitializeCallsForTesting();
  35. i18n::InitializeICU();
  36. }
  37. }
  38. } // namespace test
  39. } // namespace base