OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "base/bind.h" | |
6 #include "base/command_line.h" | |
7 #include "base/strings/string_split.h" | |
8 #include "base/strings/string_util.h" | |
9 #include "base/threading/thread_restrictions.h" | |
10 #include "chrome/browser/chromeos/base/locale_util.h" | |
11 #include "chrome/browser/chromeos/customization_document.h" | |
12 #include "chrome/browser/ui/webui/chromeos/login/l10n_util.h" | |
13 #include "chrome/test/base/in_process_browser_test.h" | |
14 #include "chromeos/system/fake_statistics_provider.h" | |
15 #include "chromeos/system/statistics_provider.h" | |
16 #include "content/public/test/test_utils.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 #include "ui/base/l10n/l10n_util.h" | |
19 | |
20 namespace chromeos { | |
21 | |
22 using locale_util::SwitchLanguageCallback; | |
Dmitry Polukhin
2014/10/03 07:49:52
Nit, usually we put such using on global level of
Alexander Alekseev
2014/10/03 12:59:17
Done.
| |
23 | |
24 namespace { | |
25 class LanguageSwitchedWaiter { | |
26 public: | |
27 explicit LanguageSwitchedWaiter(SwitchLanguageCallback callback) | |
28 : callback_(callback), | |
29 finished_(false), | |
30 runner_(new content::MessageLoopRunner){}; | |
Dmitry Polukhin
2014/10/03 07:49:52
There is no need in ';' please remove.
Alexander Alekseev
2014/10/03 12:59:17
Done.
| |
31 ~LanguageSwitchedWaiter(){}; | |
Dmitry Polukhin
2014/10/03 07:49:52
Nit, there is no need in such empty d-tor.
Alexander Alekseev
2014/10/03 12:59:17
Done.
| |
32 | |
33 void ExitMessageLoop(const std::string& required, | |
34 const std::string& actual, | |
35 const bool success) { | |
36 finished_ = true; | |
37 runner_->Quit(); | |
38 callback_.Run(required, actual, success); | |
39 } | |
40 | |
41 void Wait() { | |
42 if (finished_) | |
43 return; | |
44 runner_->Run(); | |
45 } | |
46 | |
47 scoped_ptr<SwitchLanguageCallback> Callback() { | |
48 using namespace locale_util; | |
Dmitry Polukhin
2014/10/03 07:49:52
Remove this line, it is not needed anymore after l
Alexander Alekseev
2014/10/03 12:59:17
Done.
| |
49 return scoped_ptr<SwitchLanguageCallback>( | |
50 new SwitchLanguageCallback( | |
51 base::Bind(&LanguageSwitchedWaiter::ExitMessageLoop, | |
52 base::Unretained(this)))).Pass(); | |
53 } | |
54 | |
55 private: | |
56 SwitchLanguageCallback callback_; | |
57 bool finished_; | |
58 scoped_refptr<content::MessageLoopRunner> runner_; | |
59 DISALLOW_COPY_AND_ASSIGN(LanguageSwitchedWaiter); | |
60 }; | |
61 | |
62 // Several language IDs are actually aliases to another IDs, so real language | |
63 // ID is reported as "loaded" when alias is requested. | |
64 std::string GetExpectedLanguage(const std::string& required) { | |
65 std::string expected = required; | |
66 if (required == "en-AU") { | |
Dmitry Polukhin
2014/10/03 07:49:52
To simplify future test extension, I would create
Alexander Alekseev
2014/10/03 12:59:17
Done.
| |
67 expected = "en-GB"; | |
68 } else if (required == "en-CA") { | |
69 expected = "en-GB"; | |
70 } else if (required == "en-NZ") { | |
71 expected = "en-GB"; | |
72 } else if (required == "en-ZA") { | |
73 expected = "en-GB"; | |
74 } else if (required == "fr-CA") { | |
75 expected = "fr"; | |
76 } else if (required == "no") { | |
77 expected = "nb"; | |
78 } else if (required == "iw") { | |
79 expected = "he"; | |
80 } | |
81 return expected; | |
82 } | |
83 | |
84 void VerifyLanguageSwitched(const std::string& required, | |
85 const std::string& actual, | |
86 const bool success) { | |
87 EXPECT_TRUE(success) << "SwitchLanguage failed: required='" << required | |
88 << "', actual='" << actual << "', success=" << success; | |
89 EXPECT_EQ(GetExpectedLanguage(required), actual) | |
90 << "SwitchLanguage failed: required='" << required << "', actual='" | |
91 << actual << "', success=" << success; | |
92 } | |
93 | |
94 std::string Print(const std::vector<std::string>& locales) { | |
95 std::string result("{"); | |
96 for (size_t i = 0; i < locales.size(); ++i) { | |
97 if (i != 0) { | |
98 result += ", "; | |
99 } | |
100 result += "'"; | |
101 result += locales[i]; | |
102 result += "'"; | |
103 } | |
104 result += "}"; | |
105 return result; | |
106 } | |
107 | |
108 const char* kVPDInitialLocales[] = { | |
109 "ar,bg,bn,ca,cs,da,de,el,en-AU,en-CA,en-GB,en-NZ,en-US,en-ZA,es,es-419,et," | |
110 "fa,fi,fil,fr,fr-CA,gu,he,hi,hr,hu,id,it,ja,kn,ko,lt,lv,ml,mr,ms,nl,nb,no," | |
111 "pl,pt-BR,pt-PT,ro,ru,sk,sl,sr,sv,ta,te,th,tr,vi,zh-CN,zh-TW", | |
112 }; | |
113 | |
114 const std::vector<std::string> languages_available = { | |
115 "ar", | |
116 "bg", | |
117 "bn", | |
118 "ca", | |
119 "cs", | |
120 "da", | |
121 "de", | |
122 "el", | |
123 "en-AU", | |
124 "en-CA", | |
125 "en-GB", | |
126 "en-NZ", | |
127 "en-US", | |
128 "en-ZA", | |
129 "es", | |
130 "es-419", | |
131 "et", | |
132 "fa", | |
133 "fi", | |
134 "fil", | |
135 "fr", | |
136 "fr-CA", | |
137 "gu", | |
138 "he", | |
139 "hi", | |
140 "hr", | |
141 "hu", | |
142 "id", | |
143 "it", | |
144 "ja", | |
145 "kn", | |
146 "ko", | |
147 "lt", | |
148 "lv", | |
149 "ml", | |
150 "mr", | |
151 "ms", | |
152 "nl", | |
153 "nb", | |
154 "no", | |
155 "pl", | |
156 "pt-BR", | |
157 "pt-PT", | |
158 "ro", | |
159 "ru", | |
160 "sk", | |
161 "sl", | |
162 "sr", | |
163 "sv", | |
164 "ta", | |
165 "te", | |
166 "th", | |
167 "tr", | |
168 "vi", | |
169 "zh-CN", | |
170 "zh-TW" | |
171 }; | |
172 | |
173 } // anonymous namespace | |
174 | |
175 class CustomizationLocaleTest : public InProcessBrowserTest { | |
176 public: | |
177 CustomizationLocaleTest() {} | |
178 virtual ~CustomizationLocaleTest() {} | |
Dmitry Polukhin
2014/10/03 07:49:52
You don't need both c-tor and d-tor, compiler will
Alexander Alekseev
2014/10/03 12:59:17
Done.
| |
179 }; | |
180 | |
181 IN_PROC_BROWSER_TEST_F(CustomizationLocaleTest, CheckAvailableLocales) { | |
182 for (size_t i = 0; i < languages_available.size(); ++i) { | |
183 LanguageSwitchedWaiter waiter(base::Bind(&VerifyLanguageSwitched)); | |
184 chromeos::locale_util::SwitchLanguage( | |
185 languages_available[i], true, true, waiter.Callback()); | |
186 waiter.Wait(); | |
187 { | |
188 std::string resolved_locale; | |
189 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
190 l10n_util::CheckAndResolveLocale(languages_available[i], | |
191 &resolved_locale); | |
192 EXPECT_EQ(GetExpectedLanguage(languages_available[i]), resolved_locale) | |
193 << "CheckAndResolveLocale() failed for language='" | |
194 << languages_available[i] << "'"; | |
195 } | |
196 } | |
197 } | |
198 | |
199 class CustomizationVPDTest : public InProcessBrowserTest, | |
200 public testing::WithParamInterface<const char*> { | |
201 public: | |
202 CustomizationVPDTest() | |
203 : statistics_provider_(new system::FakeStatisticsProvider()) { | |
204 // Set the instance returned by GetInstance() for testing. | |
205 system::StatisticsProvider::SetTestProvider(statistics_provider_.get()); | |
206 statistics_provider_->set_locale(GetParam()); | |
207 statistics_provider_->set_keyboard_layout(""); | |
208 } | |
209 | |
210 virtual ~CustomizationVPDTest() {} | |
Dmitry Polukhin
2014/10/03 07:49:52
You don't need it and OVERRIDE is missing.
Alexander Alekseev
2014/10/03 12:59:17
Done.
| |
211 | |
212 private: | |
213 scoped_ptr<system::FakeStatisticsProvider> statistics_provider_; | |
214 }; | |
215 | |
216 IN_PROC_BROWSER_TEST_P(CustomizationVPDTest, GetUILanguageList) { | |
217 std::vector<std::string> locales; | |
218 base::SplitString(GetParam(), ',', &locales); | |
219 | |
220 for (std::string& l : locales) { | |
221 base::TrimString(l, " ", &l); | |
222 } | |
223 EXPECT_EQ(locales, | |
224 chromeos::StartupCustomizationDocument::GetInstance() | |
Dmitry Polukhin
2014/10/03 07:49:52
You already in chromeos:: namespace.
Alexander Alekseev
2014/10/03 12:59:17
Done.
| |
225 ->configured_locales()) | |
226 << "Test failed for initial_locale='" << GetParam() | |
227 << "', locales=" << Print(locales); | |
228 | |
229 scoped_ptr<base::ListValue> ui_language_list = GetUILanguageList(NULL, ""); | |
230 EXPECT_GE(ui_language_list->GetSize(), locales.size()) | |
231 << "Test failed for initial_locale='" << GetParam() << "'"; | |
232 | |
233 for (size_t i = 0; i < ui_language_list->GetSize(); ++i) { | |
234 base::DictionaryValue* language_info = NULL; | |
235 ASSERT_TRUE(ui_language_list->GetDictionary(i, &language_info)) | |
236 << "Test failed for initial_locale='" << GetParam() << "', i=" << i; | |
237 | |
238 std::string value; | |
239 ASSERT_TRUE(language_info->GetString("value", &value)) | |
240 << "Test failed for initial_locale='" << GetParam() << "', i=" << i; | |
241 | |
242 if (i < locales.size()) { | |
243 EXPECT_EQ(locales[i], value) << "Test failed for initial_locale='" | |
244 << GetParam() << "', i=" << i; | |
245 } else { | |
246 EXPECT_EQ(chromeos::kMostRelevantLanguagesDivider, value) | |
Dmitry Polukhin
2014/10/03 07:49:52
Ditto.
Alexander Alekseev
2014/10/03 12:59:17
Done.
| |
247 << "Test failed for initial_locale='" << GetParam() << "', i=" << i; | |
248 break; | |
249 } | |
250 } | |
251 } | |
252 | |
253 INSTANTIATE_TEST_CASE_P(StringSequence, | |
254 CustomizationVPDTest, | |
255 testing::ValuesIn(kVPDInitialLocales)); | |
256 | |
257 } // namespace chromeos | |
OLD | NEW |