Skip to content

Commit

Permalink
Restore locales after tests
Browse files Browse the repository at this point in the history
Locales should be restored after tests and scoping should be used. Added restores and one restore changed to scoped version.

Review URL: https://codereview.chromium.org/1162603004

Cr-Commit-Position: refs/heads/master@{#344453}
  • Loading branch information
olli.raula authored and Commit bot committed Aug 20, 2015
1 parent 860101c commit 3be75bf
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 5 deletions.
2 changes: 1 addition & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ Raghu Ram Nagaraj <r.nagaraj@samsung.com>
Chanho Park <parkch98@gmail.com>
Payal Pandey <payal.pandey@samsung.com>
Kenneth Strickland <ken.strickland@gmail.com>
Olli Syrjälä <olli.syrjala@intel.com>
Olli Raula (Old name Olli Syrjälä) <olli.raula@intel.com>
Vishal Bhatnagar <vishal.b@samsung.com>
Yunsik Jang <yunsik.jang@lge.com>
Siddharth Bagai <b.siddharth@samsung.com>
Expand Down
2 changes: 2 additions & 0 deletions base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,8 @@ test("base_unittests") {
"task_runner_util_unittest.cc",
"template_util_unittest.cc",
"test/histogram_tester_unittest.cc",
"test/icu_test_util.cc",
"test/icu_test_util.h",
"test/test_pending_task_unittest.cc",
"test/test_reg_util_win_unittest.cc",
"test/trace_event_analyzer_unittest.cc",
Expand Down
2 changes: 2 additions & 0 deletions base/base.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,8 @@
'test/gtest_xml_util.h',
'test/histogram_tester.cc',
'test/histogram_tester.h',
'test/icu_test_util.cc',
'test/icu_test_util.h',
'test/ios/wait_util.h',
'test/ios/wait_util.mm',
'test/launcher/test_launcher.cc',
Expand Down
7 changes: 3 additions & 4 deletions base/i18n/case_conversion_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "base/i18n/case_conversion.h"
#include "base/i18n/rtl.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/icu_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/icu/source/i18n/unicode/usearch.h"

Expand Down Expand Up @@ -58,7 +59,7 @@ TEST(CaseConversionTest, TurkishLocaleConversion) {
const string16 expected_lower(WideToUTF16(L"\x69\x131"));
const string16 expected_upper(WideToUTF16(L"\x49\x49"));

std::string default_locale(uloc_getDefault());
test::ScopedRestoreICUDefaultLocale restore_locale;
i18n::SetICUDefaultLocale("en_US");

string16 result = ToLower(mixed);
Expand All @@ -77,8 +78,6 @@ TEST(CaseConversionTest, TurkishLocaleConversion) {

result = ToUpper(mixed);
EXPECT_EQ(expected_upper_turkish, result);

SetICUDefaultLocale(default_locale.data());
}

TEST(CaseConversionTest, FoldCase) {
Expand All @@ -97,7 +96,7 @@ TEST(CaseConversionTest, FoldCase) {
const string16 turkish(WideToUTF16(L"\x49\x131"));
const string16 turkish_expected(WideToUTF16(L"\x69\x131"));

std::string default_locale(uloc_getDefault());
test::ScopedRestoreICUDefaultLocale restore_locale;
i18n::SetICUDefaultLocale("en_US");
EXPECT_EQ(turkish_expected, FoldCase(turkish));

Expand Down
5 changes: 5 additions & 0 deletions base/i18n/number_formatting_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include "base/i18n/number_formatting.h"
#include "base/i18n/rtl.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/icu_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/icu/source/i18n/unicode/usearch.h"

namespace base {
namespace {
Expand All @@ -27,6 +29,8 @@ TEST(NumberFormattingTest, FormatNumber) {
{-42, "-42", "-42"},
};

test::ScopedRestoreICUDefaultLocale restore_locale;

for (size_t i = 0; i < arraysize(cases); ++i) {
i18n::SetICUDefaultLocale("en");
testing::ResetFormatters();
Expand Down Expand Up @@ -72,6 +76,7 @@ TEST(NumberFormattingTest, FormatDouble) {
{-42.7, 3, "-42.700", "-42,700"},
};

test::ScopedRestoreICUDefaultLocale restore_locale;
for (size_t i = 0; i < arraysize(cases); ++i) {
i18n::SetICUDefaultLocale("en");
testing::ResetFormatters();
Expand Down
4 changes: 4 additions & 0 deletions base/i18n/rtl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/icu_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#include "third_party/icu/source/i18n/unicode/usearch.h"
Expand Down Expand Up @@ -305,6 +306,7 @@ TEST_F(RTLTest, WrapString) {

const bool was_rtl = IsRTL();

test::ScopedRestoreICUDefaultLocale restore_locale;
for (size_t i = 0; i < 2; ++i) {
// Toggle the application default text direction (to try each direction).
SetRTL(!IsRTL());
Expand Down Expand Up @@ -352,6 +354,7 @@ TEST_F(RTLTest, GetDisplayStringInLTRDirectionality) {

const bool was_rtl = IsRTL();

test::ScopedRestoreICUDefaultLocale restore_locale;
for (size_t i = 0; i < 2; ++i) {
// Toggle the application default text direction (to try each direction).
SetRTL(!IsRTL());
Expand Down Expand Up @@ -438,6 +441,7 @@ TEST_F(RTLTest, UnadjustStringForLocaleDirection) {

const bool was_rtl = IsRTL();

test::ScopedRestoreICUDefaultLocale restore_locale;
for (size_t i = 0; i < 2; ++i) {
// Toggle the application default text direction (to try each direction).
SetRTL(!IsRTL());
Expand Down
6 changes: 6 additions & 0 deletions base/i18n/time_formatting_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "base/i18n/rtl.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/icu_test_util.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/icu/source/common/unicode/uversion.h"
Expand Down Expand Up @@ -41,6 +42,7 @@ base::string16 GetShortTimeZone(const Time& time) {
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");

Time time(Time::FromLocalExploded(kTestDateTimeExploded));
Expand Down Expand Up @@ -76,6 +78,7 @@ TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault12h) {
TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault24h) {
// Test for a locale defaulted to 24h clock.
// As an instance, we use third_party/icu/source/data/locales/en_GB.txt.
test::ScopedRestoreICUDefaultLocale restore_locale;
i18n::SetICUDefaultLocale("en_GB");

Time time(Time::FromLocalExploded(kTestDateTimeExploded));
Expand Down Expand Up @@ -111,6 +114,7 @@ TEST(TimeFormattingTest, TimeFormatTimeOfDayDefault24h) {
TEST(TimeFormattingTest, TimeFormatTimeOfDayJP) {
// Test for a locale that uses different mark than "AM" and "PM".
// As an instance, we use third_party/icu/source/data/locales/ja.txt.
test::ScopedRestoreICUDefaultLocale restore_locale;
i18n::SetICUDefaultLocale("ja_JP");

Time time(Time::FromLocalExploded(kTestDateTimeExploded));
Expand Down Expand Up @@ -144,6 +148,7 @@ TEST(TimeFormattingTest, TimeFormatTimeOfDayJP) {
TEST(TimeFormattingTest, TimeFormatDateUS) {
// See third_party/icu/source/data/locales/en.txt.
// The date patterns are "EEEE, MMMM d, y", "MMM d, y", and "M/d/yy".
test::ScopedRestoreICUDefaultLocale restore_locale;
i18n::SetICUDefaultLocale("en_US");

Time time(Time::FromLocalExploded(kTestDateTimeExploded));
Expand All @@ -166,6 +171,7 @@ TEST(TimeFormattingTest, TimeFormatDateUS) {
TEST(TimeFormattingTest, TimeFormatDateGB) {
// See third_party/icu/source/data/locales/en_GB.txt.
// The date patterns are "EEEE, d MMMM y", "d MMM y", and "dd/MM/yyyy".
test::ScopedRestoreICUDefaultLocale restore_locale;
i18n::SetICUDefaultLocale("en_GB");

Time time(Time::FromLocalExploded(kTestDateTimeExploded));
Expand Down
21 changes: 21 additions & 0 deletions base/test/icu_test_util.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/test/icu_test_util.h"

#include "base/i18n/rtl.h"
#include "third_party/icu/source/common/unicode/uloc.h"

namespace base {
namespace test {

ScopedRestoreICUDefaultLocale::ScopedRestoreICUDefaultLocale()
: default_locale_(uloc_getDefault()) {}

ScopedRestoreICUDefaultLocale::~ScopedRestoreICUDefaultLocale() {
i18n::SetICUDefaultLocale(default_locale_.data());
}

} // namespace test
} // namespace base
29 changes: 29 additions & 0 deletions base/test/icu_test_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_ICU_TEST_UTIL_H_
#define BASE_ICU_TEST_UTIL_H_

#include <string>

#include "base/macros.h"

namespace base {
namespace test {

class ScopedRestoreICUDefaultLocale {
public:
ScopedRestoreICUDefaultLocale();
~ScopedRestoreICUDefaultLocale();

private:
std::string default_locale_;

DISALLOW_COPY_AND_ASSIGN(ScopedRestoreICUDefaultLocale);
};

} // namespace test
} // namespace base

#endif // BASE_ICU_TEST_UTIL_H_

0 comments on commit 3be75bf

Please sign in to comment.