[M120 Migration]Fix for crash during chrome exit
[platform/framework/web/chromium-efl.git] / chrome / browser / locale_tests_browsertest.cc
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stddef.h>
6
7 #include <memory>
8
9 #include "base/command_line.h"
10 #include "base/environment.h"
11 #include "build/build_config.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "content/public/test/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 BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
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" },
35       { "zh-TW", "zh_TW.UTF-8" }
36     };
37     bool found_locale = false;
38     for (size_t i = 0; i < std::size(kLocales); ++i) {
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 BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
50     std::unique_ptr<base::Environment> env(base::Environment::Create());
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_;
63 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
64   const char* old_locale_;
65 #endif
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
77   void SetUpCommandLine(base::CommandLine* command_line) override {
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.
111 IN_PROC_BROWSER_TEST_F(LocaleTestDanish, TestStart) {
112 }
113
114 IN_PROC_BROWSER_TEST_F(LocaleTestHebrew, TestStart) {
115 }
116
117 IN_PROC_BROWSER_TEST_F(LocaleTestTraditionalChinese, TestStart) {
118 }