[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | |
dcheng | c0e39d57 | 2016-04-19 06:15:17 | [diff] [blame^] | 7 | #include <memory> |
| 8 | |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 9 | #include "base/command_line.h" |
| 10 | #include "base/environment.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 11 | #include "base/macros.h" |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 12 | #include "build/build_config.h" |
| 13 | #include "chrome/test/base/in_process_browser_test.h" |
| 14 | #include "ui/base/ui_base_switches.h" |
| 15 | |
| 16 | namespace { |
| 17 | |
| 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.) |
| 23 | class ScopedLocale { |
| 24 | public: |
| 25 | explicit ScopedLocale(const char* locale) : locale_(locale) { |
| 26 | #if defined(OS_LINUX) |
| 27 | old_locale_ = getenv("LC_ALL"); |
| 28 | |
| 29 | static const struct { |
| 30 | const char* chrome_locale; |
| 31 | const char* system_locale; |
| 32 | } kLocales[] = { |
| 33 | { "da", "da_DK.UTF-8" }, |
| 34 | { "he", "he_IL.UTF-8" }, |
[email protected] | b7b53280 | 2013-11-23 01:37:49 | [diff] [blame] | 35 | { "zh-TW", "zh_TW.UTF-8" } |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 36 | }; |
| 37 | bool found_locale = false; |
viettrungluu | 9e65ad1 | 2014-10-16 04:22:26 | [diff] [blame] | 38 | for (size_t i = 0; i < arraysize(kLocales); ++i) { |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 39 | if (kLocales[i].chrome_locale == locale) { |
| 40 | found_locale = true; |
| 41 | setenv("LC_ALL", kLocales[i].system_locale, 1); |
| 42 | } |
| 43 | } |
| 44 | DCHECK(found_locale); |
| 45 | #endif |
| 46 | } |
| 47 | |
| 48 | ~ScopedLocale() { |
| 49 | #if defined(OS_LINUX) |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 50 | std::unique_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 51 | if (old_locale_) { |
| 52 | env->SetVar("LC_ALL", old_locale_); |
| 53 | } else { |
| 54 | env->UnSetVar("LC_ALL"); |
| 55 | } |
| 56 | #endif |
| 57 | } |
| 58 | |
| 59 | const std::string& locale() { return locale_; } |
| 60 | |
| 61 | private: |
| 62 | std::string locale_; |
[email protected] | 281bc5b | 2012-06-27 16:07:34 | [diff] [blame] | 63 | #if defined(OS_LINUX) |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 64 | const char* old_locale_; |
[email protected] | 281bc5b | 2012-06-27 16:07:34 | [diff] [blame] | 65 | #endif |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 66 | }; |
| 67 | |
| 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 |
| 71 | // used by Chrome. |
| 72 | class LocaleTestBase : public InProcessBrowserTest { |
| 73 | public: |
| 74 | explicit LocaleTestBase(const char* locale) : locale_(locale) { |
| 75 | } |
| 76 | |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 77 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 78 | command_line->AppendSwitchASCII(switches::kLang, locale_.locale()); |
| 79 | } |
| 80 | |
| 81 | protected: |
| 82 | ScopedLocale locale_; |
| 83 | }; |
| 84 | |
| 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 { |
| 88 | public: |
| 89 | LocaleTestDanish() : LocaleTestBase("da") { |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | class LocaleTestHebrew : public LocaleTestBase { |
| 94 | public: |
| 95 | LocaleTestHebrew() : LocaleTestBase("he") { |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | class LocaleTestTraditionalChinese : public LocaleTestBase { |
| 100 | public: |
| 101 | LocaleTestTraditionalChinese() : LocaleTestBase("zh-TW") { |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | } // namespace |
| 106 | |
| 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. |
[email protected] | 77dac8d | 2013-12-11 03:02:47 | [diff] [blame] | 111 | IN_PROC_BROWSER_TEST_F(LocaleTestDanish, TestStart) { |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 112 | } |
| 113 | |
[email protected] | 77dac8d | 2013-12-11 03:02:47 | [diff] [blame] | 114 | IN_PROC_BROWSER_TEST_F(LocaleTestHebrew, TestStart) { |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 115 | } |
| 116 | |
[email protected] | 77dac8d | 2013-12-11 03:02:47 | [diff] [blame] | 117 | IN_PROC_BROWSER_TEST_F(LocaleTestTraditionalChinese, TestStart) { |
[email protected] | 5e6a84e9 | 2012-04-10 02:44:08 | [diff] [blame] | 118 | } |