Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / login / l10n_util_unittest.cc
blobaf1187525cca5751bfe2b2e7612fcff7c5c39f1a
1 // Copyright 2014 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.
5 #include "chrome/browser/ui/webui/chromeos/login/l10n_util.h"
7 #include "base/at_exit.h"
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/macros.h"
11 #include "base/memory/singleton.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/values.h"
15 #include "chrome/browser/chromeos/customization/customization_document.h"
16 #include "chrome/browser/chromeos/input_method/input_method_configuration.h"
17 #include "chrome/browser/ui/webui/chromeos/login/l10n_util_test_util.h"
18 #include "chromeos/system/statistics_provider.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "ui/base/ime/chromeos/component_extension_ime_manager.h"
22 namespace chromeos {
24 namespace {
26 class MachineStatisticsInitializer {
27 public:
28 MachineStatisticsInitializer();
30 static MachineStatisticsInitializer* GetInstance();
32 private:
33 DISALLOW_COPY_AND_ASSIGN(MachineStatisticsInitializer);
36 MachineStatisticsInitializer::MachineStatisticsInitializer() {
37 base::MessageLoop loop;
38 chromeos::system::StatisticsProvider::GetInstance()
39 ->StartLoadingMachineStatistics(loop.task_runner(), false);
40 loop.RunUntilIdle();
43 // static
44 MachineStatisticsInitializer* MachineStatisticsInitializer::GetInstance() {
45 return Singleton<MachineStatisticsInitializer>::get();
48 void VerifyOnlyUILanguages(const base::ListValue& list) {
49 for (size_t i = 0; i < list.GetSize(); ++i) {
50 const base::DictionaryValue* dict;
51 ASSERT_TRUE(list.GetDictionary(i, &dict));
52 std::string code;
53 ASSERT_TRUE(dict->GetString("code", &code));
54 EXPECT_NE("is", code)
55 << "Icelandic is an example language which has input method "
56 << "but can't use it as UI language.";
60 void VerifyLanguageCode(const base::ListValue& list,
61 size_t index,
62 const std::string& expected_code) {
63 const base::DictionaryValue* dict;
64 ASSERT_TRUE(list.GetDictionary(index, &dict));
65 std::string actual_code;
66 ASSERT_TRUE(dict->GetString("code", &actual_code));
67 EXPECT_EQ(expected_code, actual_code)
68 << "Wrong language code at index " << index << ".";
71 } // namespace
73 class L10nUtilTest : public testing::Test {
74 public:
75 L10nUtilTest();
76 ~L10nUtilTest() override;
78 // testing::Test:
79 void SetUp() override;
80 void TearDown() override;
82 void SetInputMethods1();
83 void SetInputMethods2();
85 private:
86 base::ShadowingAtExitManager at_exit_manager_;
88 MockInputMethodManagerWithInputMethods* input_manager_;
91 L10nUtilTest::L10nUtilTest()
92 : input_manager_(new MockInputMethodManagerWithInputMethods) {
95 L10nUtilTest::~L10nUtilTest() {
98 void L10nUtilTest::SetUp() {
99 chromeos::input_method::InitializeForTesting(input_manager_);
100 input_manager_->SetComponentExtensionIMEManager(
101 make_scoped_ptr(new ComponentExtensionIMEManager));
102 MachineStatisticsInitializer::GetInstance(); // Ignore result.
105 void L10nUtilTest::TearDown() {
106 chromeos::input_method::Shutdown();
109 void L10nUtilTest::SetInputMethods1() {
110 input_manager_->AddInputMethod("xkb:us::eng", "us", "en-US");
111 input_manager_->AddInputMethod("xkb:fr::fra", "fr", "fr");
112 input_manager_->AddInputMethod("xkb:be::fra", "be", "fr");
113 input_manager_->AddInputMethod("xkb:is::ice", "is", "is");
116 void L10nUtilTest::SetInputMethods2() {
117 input_manager_->AddInputMethod("xkb:us::eng", "us", "en-US");
118 input_manager_->AddInputMethod("xkb:ch:fr:fra", "ch(fr)", "fr");
119 input_manager_->AddInputMethod("xkb:ch::ger", "ch", "de");
120 input_manager_->AddInputMethod("xkb:it::ita", "it", "it");
121 input_manager_->AddInputMethod("xkb:is::ice", "is", "is");
124 TEST_F(L10nUtilTest, GetUILanguageList) {
125 SetInputMethods1();
127 // This requires initialized StatisticsProvider (see L10nUtilTest()).
128 scoped_ptr<base::ListValue> list(GetUILanguageList(NULL, std::string()));
130 VerifyOnlyUILanguages(*list);
133 TEST_F(L10nUtilTest, FindMostRelevantLocale) {
134 base::ListValue available_locales;
135 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
136 dict->SetString("value", "de");
137 available_locales.Append(dict.release());
138 dict.reset(new base::DictionaryValue);
139 dict->SetString("value", "fr");
140 available_locales.Append(dict.release());
141 dict.reset(new base::DictionaryValue);
142 dict->SetString("value", "en-GB");
143 available_locales.Append(dict.release());
145 std::vector<std::string> most_relevant_language_codes;
146 EXPECT_EQ("en-US", FindMostRelevantLocale(most_relevant_language_codes,
147 available_locales,
148 "en-US"));
150 most_relevant_language_codes.push_back("xx");
151 EXPECT_EQ("en-US", FindMostRelevantLocale(most_relevant_language_codes,
152 available_locales,
153 "en-US"));
155 most_relevant_language_codes.push_back("fr");
156 EXPECT_EQ("fr", FindMostRelevantLocale(most_relevant_language_codes,
157 available_locales,
158 "en-US"));
160 most_relevant_language_codes.push_back("de");
161 EXPECT_EQ("fr", FindMostRelevantLocale(most_relevant_language_codes,
162 available_locales,
163 "en-US"));
166 void InitStartupCustomizationDocumentForTesting(const std::string& manifest) {
167 StartupCustomizationDocument::GetInstance()->LoadManifestFromString(manifest);
168 StartupCustomizationDocument::GetInstance()->Init(
169 chromeos::system::StatisticsProvider::GetInstance());
172 const char kStartupManifest[] =
173 "{\n"
174 " \"version\": \"1.0\",\n"
175 " \"initial_locale\" : \"fr,en-US,de,is,it\",\n"
176 " \"initial_timezone\" : \"Europe/Zurich\",\n"
177 " \"keyboard_layout\" : \"xkb:ch:fr:fra\",\n"
178 " \"registration_url\" : \"http://www.google.com\",\n"
179 " \"setup_content\" : {\n"
180 " \"default\" : {\n"
181 " \"help_page\" : \"file:///opt/oem/help/en-US/help.html\",\n"
182 " \"eula_page\" : \"file:///opt/oem/eula/en-US/eula.html\",\n"
183 " },\n"
184 " },"
185 "}";
187 TEST_F(L10nUtilTest, GetUILanguageListMulti) {
188 InitStartupCustomizationDocumentForTesting(kStartupManifest);
189 SetInputMethods2();
191 // This requires initialized StatisticsProvider (see L10nUtilTest()).
192 scoped_ptr<base::ListValue> list(GetUILanguageList(NULL, std::string()));
194 VerifyOnlyUILanguages(*list);
196 // (4 languages (except Icelandic) + divider) = 5 + all other languages
197 ASSERT_LE(5u, list->GetSize());
199 VerifyLanguageCode(*list, 0, "fr");
200 VerifyLanguageCode(*list, 1, "en-US");
201 VerifyLanguageCode(*list, 2, "de");
202 VerifyLanguageCode(*list, 3, "it");
203 VerifyLanguageCode(*list, 4, kMostRelevantLanguagesDivider);
206 TEST_F(L10nUtilTest, GetUILanguageListWithMostRelevant) {
207 std::vector<std::string> most_relevant_language_codes;
208 most_relevant_language_codes.push_back("it");
209 most_relevant_language_codes.push_back("de");
210 most_relevant_language_codes.push_back("nonexistent");
212 // This requires initialized StatisticsProvider (see L10nUtilTest()).
213 scoped_ptr<base::ListValue>
214 list(GetUILanguageList(&most_relevant_language_codes, std::string()));
216 VerifyOnlyUILanguages(*list);
218 ASSERT_LE(3u, list->GetSize());
220 VerifyLanguageCode(*list, 0, "it");
221 VerifyLanguageCode(*list, 1, "de");
222 VerifyLanguageCode(*list, 2, kMostRelevantLanguagesDivider);
225 } // namespace chromeos