blob: 75d814aabc98286e2a4984398e66c31b9c06755d [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
dchengc0e39d572016-04-19 06:15:177#include <memory>
8
[email protected]5e6a84e92012-04-10 02:44:089#include "base/command_line.h"
10#include "base/environment.h"
avi6846aef2015-12-26 01:09:3811#include "base/macros.h"
[email protected]5e6a84e92012-04-10 02:44:0812#include "build/build_config.h"
13#include "chrome/test/base/in_process_browser_test.h"
14#include "ui/base/ui_base_switches.h"
15
16namespace {
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.)
23class 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]b7b532802013-11-23 01:37:4935 { "zh-TW", "zh_TW.UTF-8" }
[email protected]5e6a84e92012-04-10 02:44:0836 };
37 bool found_locale = false;
viettrungluu9e65ad12014-10-16 04:22:2638 for (size_t i = 0; i < arraysize(kLocales); ++i) {
[email protected]5e6a84e92012-04-10 02:44:0839 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)
dcheng4af48582016-04-19 00:29:3550 std::unique_ptr<base::Environment> env(base::Environment::Create());
[email protected]5e6a84e92012-04-10 02:44:0851 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]281bc5b2012-06-27 16:07:3463#if defined(OS_LINUX)
[email protected]5e6a84e92012-04-10 02:44:0864 const char* old_locale_;
[email protected]281bc5b2012-06-27 16:07:3465#endif
[email protected]5e6a84e92012-04-10 02:44:0866};
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.
72class LocaleTestBase : public InProcessBrowserTest {
73 public:
74 explicit LocaleTestBase(const char* locale) : locale_(locale) {
75 }
76
avi556c05022014-12-22 23:31:4377 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]5e6a84e92012-04-10 02:44:0878 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.
87class LocaleTestDanish : public LocaleTestBase {
88 public:
89 LocaleTestDanish() : LocaleTestBase("da") {
90 }
91};
92
93class LocaleTestHebrew : public LocaleTestBase {
94 public:
95 LocaleTestHebrew() : LocaleTestBase("he") {
96 }
97};
98
99class 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]77dac8d2013-12-11 03:02:47111IN_PROC_BROWSER_TEST_F(LocaleTestDanish, TestStart) {
[email protected]5e6a84e92012-04-10 02:44:08112}
113
[email protected]77dac8d2013-12-11 03:02:47114IN_PROC_BROWSER_TEST_F(LocaleTestHebrew, TestStart) {
[email protected]5e6a84e92012-04-10 02:44:08115}
116
[email protected]77dac8d2013-12-11 03:02:47117IN_PROC_BROWSER_TEST_F(LocaleTestTraditionalChinese, TestStart) {
[email protected]5e6a84e92012-04-10 02:44:08118}