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