1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 #include "base/command_line.h"
10 #include "base/environment.h"
11 #include "build/build_config.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "content/public/test/browser_test.h"
14 #include "ui/base/ui_base_switches.h"
18 // A class that over-writes the system locale only in a scope. To emulate the
19 // specified environment on Linux, this class over-writes a LC_ALL environment
20 // variable when creating a LocaleTest object and restore it with the original
21 // value when deleting the object. (This environment variable may affect other
22 // tests and we have to restore it regardless of the results of LocaleTests.)
25 explicit ScopedLocale(const char* locale) : locale_(locale) {
26 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
27 old_locale_ = getenv("LC_ALL");
30 const char* chrome_locale;
31 const char* system_locale;
33 { "da", "da_DK.UTF-8" },
34 { "he", "he_IL.UTF-8" },
35 { "zh-TW", "zh_TW.UTF-8" }
37 bool found_locale = false;
38 for (size_t i = 0; i < std::size(kLocales); ++i) {
39 if (kLocales[i].chrome_locale == locale) {
41 setenv("LC_ALL", kLocales[i].system_locale, 1);
49 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
50 std::unique_ptr<base::Environment> env(base::Environment::Create());
52 env->SetVar("LC_ALL", old_locale_);
54 env->UnSetVar("LC_ALL");
59 const std::string& locale() { return locale_; }
63 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
64 const char* old_locale_;
68 // A base class for tests used in this file. This class over-writes the system
69 // locale and run Chrome with a '--lang' option. To add a new LocaleTest, add a
70 // class derived from this class and call the constructor with the locale name
72 class LocaleTestBase : public InProcessBrowserTest {
74 explicit LocaleTestBase(const char* locale) : locale_(locale) {
77 void SetUpCommandLine(base::CommandLine* command_line) override {
78 command_line->AppendSwitchASCII(switches::kLang, locale_.locale());
85 // Test classes that run Chrome on the Danish locale, the Hebrew locale, and
86 // the Traditional-Chinese locale, respectively.
87 class LocaleTestDanish : public LocaleTestBase {
89 LocaleTestDanish() : LocaleTestBase("da") {
93 class LocaleTestHebrew : public LocaleTestBase {
95 LocaleTestHebrew() : LocaleTestBase("he") {
99 class LocaleTestTraditionalChinese : public LocaleTestBase {
101 LocaleTestTraditionalChinese() : LocaleTestBase("zh-TW") {
107 // Start Chrome and shut it down on the Danish locale, the Hebrew locale, and
108 // the Traditional-Chinese locale, respectively. These tests do not need any
109 // code here because they just verify we can start Chrome and shut it down
110 // cleanly on these locales.
111 IN_PROC_BROWSER_TEST_F(LocaleTestDanish, TestStart) {
114 IN_PROC_BROWSER_TEST_F(LocaleTestHebrew, TestStart) {
117 IN_PROC_BROWSER_TEST_F(LocaleTestTraditionalChinese, TestStart) {