blob: c5521be29523144e76142340df9d74756fb6ad4c [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2012 The Chromium Authors
[email protected]5e6a84e92012-04-10 02:44:082// 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
Arthur Sonzogni97157972024-12-10 08:41:107#include <array>
dchengc0e39d572016-04-19 06:15:178#include <memory>
9
[email protected]5e6a84e92012-04-10 02:44:0810#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 Kasting919ce652020-05-07 10:22:3614#include "content/public/test/browser_test.h"
[email protected]5e6a84e92012-04-10 02:44:0815#include "ui/base/ui_base_switches.h"
16
17namespace {
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.)
24class ScopedLocale {
25 public:
26 explicit ScopedLocale(const char* locale) : locale_(locale) {
Xiaohan Wang55ae2c012022-01-20 21:49:1127#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
[email protected]5e6a84e92012-04-10 02:44:0828 old_locale_ = getenv("LC_ALL");
29
Arthur Sonzogni97157972024-12-10 08:41:1030 struct Locales {
[email protected]5e6a84e92012-04-10 02:44:0831 const char* chrome_locale;
32 const char* system_locale;
[email protected]5e6a84e92012-04-10 02:44:0833 };
Arthur Sonzogni97157972024-12-10 08:41:1034 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]5e6a84e92012-04-10 02:44:0839 bool found_locale = false;
Daniel Cheng7d9e3d52022-02-26 09:03:2440 for (size_t i = 0; i < std::size(kLocales); ++i) {
[email protected]5e6a84e92012-04-10 02:44:0841 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 Wang55ae2c012022-01-20 21:49:1151#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
dcheng4af48582016-04-19 00:29:3552 std::unique_ptr<base::Environment> env(base::Environment::Create());
[email protected]5e6a84e92012-04-10 02:44:0853 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 Wang55ae2c012022-01-20 21:49:1165#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
[email protected]5e6a84e92012-04-10 02:44:0866 const char* old_locale_;
[email protected]281bc5b2012-06-27 16:07:3467#endif
[email protected]5e6a84e92012-04-10 02:44:0868};
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.
74class LocaleTestBase : public InProcessBrowserTest {
75 public:
76 explicit LocaleTestBase(const char* locale) : locale_(locale) {
77 }
78
avi556c05022014-12-22 23:31:4379 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]5e6a84e92012-04-10 02:44:0880 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.
89class LocaleTestDanish : public LocaleTestBase {
90 public:
91 LocaleTestDanish() : LocaleTestBase("da") {
92 }
93};
94
95class LocaleTestHebrew : public LocaleTestBase {
96 public:
97 LocaleTestHebrew() : LocaleTestBase("he") {
98 }
99};
100
101class 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]77dac8d2013-12-11 03:02:47113IN_PROC_BROWSER_TEST_F(LocaleTestDanish, TestStart) {
[email protected]5e6a84e92012-04-10 02:44:08114}
115
[email protected]77dac8d2013-12-11 03:02:47116IN_PROC_BROWSER_TEST_F(LocaleTestHebrew, TestStart) {
[email protected]5e6a84e92012-04-10 02:44:08117}
118
[email protected]77dac8d2013-12-11 03:02:47119IN_PROC_BROWSER_TEST_F(LocaleTestTraditionalChinese, TestStart) {
[email protected]5e6a84e92012-04-10 02:44:08120}