Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | |
Arthur Sonzogni | 9715797 | 2024-12-10 08:41:10 | [diff] [blame] | 7 | #include <array> |
dcheng | c0e39d57 | 2016-04-19 06:15:17 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 10 | #include "base/command_line.h" |
| 11 | #include "base/environment.h" |
| 12 | #include "build/build_config.h" |
| 13 | #include "chrome/test/base/in_process_browser_test.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 14 | #include "content/public/test/browser_test.h" |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 15 | #include "ui/base/ui_base_switches.h" |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | // A class that over-writes the system locale only in a scope. To emulate the |
| 20 | // specified environment on Linux, this class over-writes a LC_ALL environment |
| 21 | // variable when creating a LocaleTest object and restore it with the original |
| 22 | // value when deleting the object. (This environment variable may affect other |
| 23 | // tests and we have to restore it regardless of the results of LocaleTests.) |
| 24 | class ScopedLocale { |
| 25 | public: |
| 26 | explicit ScopedLocale(const char* locale) : locale_(locale) { |
Xiaohan Wang | 55ae2c01 | 2022-01-20 21:49:11 | [diff] [blame] | 27 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 28 | old_locale_ = getenv("LC_ALL"); |
| 29 | |
Arthur Sonzogni | 9715797 | 2024-12-10 08:41:10 | [diff] [blame] | 30 | struct Locales { |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 31 | const char* chrome_locale; |
| 32 | const char* system_locale; |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 33 | }; |
Arthur Sonzogni | 9715797 | 2024-12-10 08:41:10 | [diff] [blame] | 34 | static const auto kLocales = std::to_array<Locales>({ |
| 35 | {"da", "da_DK.UTF-8"}, |
| 36 | {"he", "he_IL.UTF-8"}, |
| 37 | {"zh-TW", "zh_TW.UTF-8"}, |
| 38 | }); |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 39 | bool found_locale = false; |
Daniel Cheng | 7d9e3d5 | 2022-02-26 09:03:24 | [diff] [blame] | 40 | for (size_t i = 0; i < std::size(kLocales); ++i) { |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 41 | if (kLocales[i].chrome_locale == locale) { |
| 42 | found_locale = true; |
| 43 | setenv("LC_ALL", kLocales[i].system_locale, 1); |
| 44 | } |
| 45 | } |
| 46 | DCHECK(found_locale); |
| 47 | #endif |
| 48 | } |
| 49 | |
| 50 | ~ScopedLocale() { |
Xiaohan Wang | 55ae2c01 | 2022-01-20 21:49:11 | [diff] [blame] | 51 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 52 | std::unique_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 53 | if (old_locale_) { |
| 54 | env->SetVar("LC_ALL", old_locale_); |
| 55 | } else { |
| 56 | env->UnSetVar("LC_ALL"); |
| 57 | } |
| 58 | #endif |
| 59 | } |
| 60 | |
| 61 | const std::string& locale() { return locale_; } |
| 62 | |
| 63 | private: |
| 64 | std::string locale_; |
Xiaohan Wang | 55ae2c01 | 2022-01-20 21:49:11 | [diff] [blame] | 65 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 66 | const char* old_locale_; |
[email protected] | 281bc5b | 2012-06-27 16:07:34 | [diff] [blame] | 67 | #endif |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | // A base class for tests used in this file. This class over-writes the system |
| 71 | // locale and run Chrome with a '--lang' option. To add a new LocaleTest, add a |
| 72 | // class derived from this class and call the constructor with the locale name |
| 73 | // used by Chrome. |
| 74 | class LocaleTestBase : public InProcessBrowserTest { |
| 75 | public: |
| 76 | explicit LocaleTestBase(const char* locale) : locale_(locale) { |
| 77 | } |
| 78 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 79 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 80 | command_line->AppendSwitchASCII(switches::kLang, locale_.locale()); |
| 81 | } |
| 82 | |
| 83 | protected: |
| 84 | ScopedLocale locale_; |
| 85 | }; |
| 86 | |
| 87 | // Test classes that run Chrome on the Danish locale, the Hebrew locale, and |
| 88 | // the Traditional-Chinese locale, respectively. |
| 89 | class LocaleTestDanish : public LocaleTestBase { |
| 90 | public: |
| 91 | LocaleTestDanish() : LocaleTestBase("da") { |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | class LocaleTestHebrew : public LocaleTestBase { |
| 96 | public: |
| 97 | LocaleTestHebrew() : LocaleTestBase("he") { |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | class LocaleTestTraditionalChinese : public LocaleTestBase { |
| 102 | public: |
| 103 | LocaleTestTraditionalChinese() : LocaleTestBase("zh-TW") { |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | } // namespace |
| 108 | |
| 109 | // Start Chrome and shut it down on the Danish locale, the Hebrew locale, and |
| 110 | // the Traditional-Chinese locale, respectively. These tests do not need any |
| 111 | // code here because they just verify we can start Chrome and shut it down |
| 112 | // cleanly on these locales. |
[email protected] | 77dac8d | 2013-12-11 03:02:47 | [diff] [blame] | 113 | IN_PROC_BROWSER_TEST_F(LocaleTestDanish, TestStart) { |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 114 | } |
| 115 | |
[email protected] | 77dac8d | 2013-12-11 03:02:47 | [diff] [blame] | 116 | IN_PROC_BROWSER_TEST_F(LocaleTestHebrew, TestStart) { |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | 77dac8d | 2013-12-11 03:02:47 | [diff] [blame] | 119 | IN_PROC_BROWSER_TEST_F(LocaleTestTraditionalChinese, TestStart) { |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 120 | } |