summaryrefslogtreecommitdiffstats
path: root/chrome/browser/locale_tests_browsertest.cc
blob: 700b4755ddfeb331b8c36b93021826a0e289339d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/command_line.h"
#include "base/environment.h"
#include "build/build_config.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "ui/base/ui_base_switches.h"

namespace {

// A class that over-writes the system locale only in a scope. To emulate the
// specified environment on Linux, this class over-writes a LC_ALL environment
// variable when creating a LocaleTest object and restore it with the original
// value when deleting the object. (This environment variable may affect other
// tests and we have to restore it regardless of the results of LocaleTests.)
class ScopedLocale {
 public:
  explicit ScopedLocale(const char* locale) : locale_(locale) {
#if defined(OS_LINUX)
    old_locale_ = getenv("LC_ALL");

    static const struct {
      const char* chrome_locale;
      const char* system_locale;
    } kLocales[] = {
      { "da", "da_DK.UTF-8" },
      { "he", "he_IL.UTF-8" },
      { "zh-TW", "zh-TW.UTF-8" }
    };
    bool found_locale = false;
    for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kLocales); ++i) {
      if (kLocales[i].chrome_locale == locale) {
        found_locale = true;
        setenv("LC_ALL", kLocales[i].system_locale, 1);
      }
    }
    DCHECK(found_locale);
#endif
  }

  ~ScopedLocale() {
#if defined(OS_LINUX)
    scoped_ptr<base::Environment> env(base::Environment::Create());
    if (old_locale_) {
      env->SetVar("LC_ALL", old_locale_);
    } else {
      env->UnSetVar("LC_ALL");
    }
#endif
  }

  const std::string& locale() { return locale_; }

 private:
  std::string locale_;
#if defined(OS_LINUX)
  const char* old_locale_;
#endif
};

// A base class for tests used in this file. This class over-writes the system
// locale and run Chrome with a '--lang' option. To add a new LocaleTest, add a
// class derived from this class and call the constructor with the locale name
// used by Chrome.
class LocaleTestBase : public InProcessBrowserTest {
 public:
  explicit LocaleTestBase(const char* locale) : locale_(locale) {
  }

  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
    command_line->AppendSwitchASCII(switches::kLang, locale_.locale());
  }

 protected:
  ScopedLocale locale_;
};

// Test classes that run Chrome on the Danish locale, the Hebrew locale, and
// the Traditional-Chinese locale, respectively.
class LocaleTestDanish : public LocaleTestBase {
 public:
  LocaleTestDanish() : LocaleTestBase("da") {
  }
};

class LocaleTestHebrew : public LocaleTestBase {
 public:
  LocaleTestHebrew() : LocaleTestBase("he") {
  }
};

class LocaleTestTraditionalChinese : public LocaleTestBase {
 public:
  LocaleTestTraditionalChinese() : LocaleTestBase("zh-TW") {
  }
};

}  // namespace

// Start Chrome and shut it down on the Danish locale, the Hebrew locale, and
// the Traditional-Chinese locale, respectively. These tests do not need any
// code here because they just verify we can start Chrome and shut it down
// cleanly on these locales.
IN_PROC_BROWSER_TEST_F(LocaleTestDanish, TestStart) {
}

IN_PROC_BROWSER_TEST_F(LocaleTestHebrew, TestStart) {
}

IN_PROC_BROWSER_TEST_F(LocaleTestTraditionalChinese, TestStart) {
}