blob: 26b82d119bf77dc40454f5caff96f78bd9514e71 [file] [log] [blame]
[email protected]5e6a84e92012-04-10 02:44:081// 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
avi6846aef2015-12-26 01:09:385#include <stddef.h>
6
[email protected]5e6a84e92012-04-10 02:44:087#include "base/command_line.h"
8#include "base/environment.h"
avi6846aef2015-12-26 01:09:389#include "base/macros.h"
danakja9850e12016-04-18 22:28:0810#include "base/memory/scoped_ptr.h"
[email protected]5e6a84e92012-04-10 02:44:0811#include "build/build_config.h"
12#include "chrome/test/base/in_process_browser_test.h"
13#include "ui/base/ui_base_switches.h"
14
15namespace {
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.)
22class 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]b7b532802013-11-23 01:37:4934 { "zh-TW", "zh_TW.UTF-8" }
[email protected]5e6a84e92012-04-10 02:44:0835 };
36 bool found_locale = false;
viettrungluu9e65ad12014-10-16 04:22:2637 for (size_t i = 0; i < arraysize(kLocales); ++i) {
[email protected]5e6a84e92012-04-10 02:44:0838 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)
dcheng4af48582016-04-19 00:29:3549 std::unique_ptr<base::Environment> env(base::Environment::Create());
[email protected]5e6a84e92012-04-10 02:44:0850 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]281bc5b2012-06-27 16:07:3462#if defined(OS_LINUX)
[email protected]5e6a84e92012-04-10 02:44:0863 const char* old_locale_;
[email protected]281bc5b2012-06-27 16:07:3464#endif
[email protected]5e6a84e92012-04-10 02:44:0865};
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.
71class LocaleTestBase : public InProcessBrowserTest {
72 public:
73 explicit LocaleTestBase(const char* locale) : locale_(locale) {
74 }
75
avi556c05022014-12-22 23:31:4376 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]5e6a84e92012-04-10 02:44:0877 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.
86class LocaleTestDanish : public LocaleTestBase {
87 public:
88 LocaleTestDanish() : LocaleTestBase("da") {
89 }
90};
91
92class LocaleTestHebrew : public LocaleTestBase {
93 public:
94 LocaleTestHebrew() : LocaleTestBase("he") {
95 }
96};
97
98class 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]77dac8d2013-12-11 03:02:47110IN_PROC_BROWSER_TEST_F(LocaleTestDanish, TestStart) {
[email protected]5e6a84e92012-04-10 02:44:08111}
112
[email protected]77dac8d2013-12-11 03:02:47113IN_PROC_BROWSER_TEST_F(LocaleTestHebrew, TestStart) {
[email protected]5e6a84e92012-04-10 02:44:08114}
115
[email protected]77dac8d2013-12-11 03:02:47116IN_PROC_BROWSER_TEST_F(LocaleTestTraditionalChinese, TestStart) {
[email protected]5e6a84e92012-04-10 02:44:08117}