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