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 <sstream> | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/command_line.h" | |
9 #include "base/strings/string_split.h" | |
10 #include "base/strings/string_util.h" | |
11 #include "base/threading/thread_restrictions.h" | |
12 #include "chrome/browser/chromeos/base/locale_util.h" | |
13 #include "chrome/browser/chromeos/customization_document.h" | |
14 #include "chrome/browser/ui/webui/chromeos/login/l10n_util.h" | |
15 #include "chrome/test/base/in_process_browser_test.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 namespace { | |
23 | |
24 class LanguageSwitchedWaiter { | |
25 public: | |
26 explicit LanguageSwitchedWaiter(locale_util::SwitchLanguageCallback callback) | |
27 : callback_(callback), | |
28 finished_(false), | |
29 runner_(new content::MessageLoopRunner){}; | |
30 ~LanguageSwitchedWaiter(){}; | |
31 | |
32 void ExitMessageLoop(const std::string& required, | |
33 const std::string& actual, | |
34 const bool success) { | |
35 finished_ = true; | |
36 runner_->Quit(); | |
37 callback_.Run(required, actual, success); | |
38 } | |
39 | |
40 void Wait() { | |
41 if (finished_) | |
42 return; | |
43 runner_->Run(); | |
44 } | |
45 | |
46 scoped_ptr<locale_util::SwitchLanguageCallback> Callback() { | |
47 using namespace locale_util; | |
Dmitry Polukhin
2014/09/30 09:29:54
Please write 'using locale_util::SwitchLanguageCal
Alexander Alekseev
2014/10/02 14:05:54
Done.
| |
48 return scoped_ptr<SwitchLanguageCallback>( | |
49 new SwitchLanguageCallback( | |
50 base::Bind(&LanguageSwitchedWaiter::ExitMessageLoop, | |
51 base::Unretained(this)))).Pass(); | |
52 } | |
53 | |
54 private: | |
55 locale_util::SwitchLanguageCallback callback_; | |
56 bool finished_; | |
57 scoped_refptr<content::MessageLoopRunner> runner_; | |
58 DISALLOW_COPY_AND_ASSIGN(LanguageSwitchedWaiter); | |
59 }; | |
60 | |
61 std::string GetExpectedLanguage(const std::string& required) { | |
Dmitry Polukhin
2014/09/30 09:29:54
Please add comment why this mismatch happens.
Alexander Alekseev
2014/10/02 14:05:54
Done.
| |
62 std::string expected = required; | |
63 if (required == "en-AU") { | |
64 expected = "en-GB"; | |
65 } else if (required == "en-CA") { | |
66 expected = "en-GB"; | |
67 } else if (required == "en-NZ") { | |
68 expected = "en-GB"; | |
69 } else if (required == "en-ZA") { | |
70 expected = "en-GB"; | |
71 } else if (required == "fr-CA") { | |
72 expected = "fr"; | |
73 } else if (required == "no") { | |
74 expected = "nb"; | |
75 } else if (required == "iw") { | |
76 expected = "he"; | |
77 } | |
78 return expected; | |
79 } | |
80 | |
81 void VerifyLanguageSwitched(const std::string& required, | |
82 const std::string& actual, | |
83 const bool success) { | |
84 EXPECT_TRUE(success) << "SwitchLanguage failed: required='" << required | |
85 << "', actual='" << actual << "', success=" << success; | |
86 EXPECT_EQ(GetExpectedLanguage(required), actual) | |
87 << "SwitchLanguage failed: required='" << required << "', actual='" | |
88 << actual << "', success=" << success; | |
89 } | |
90 | |
91 std::string Print(const std::vector<std::string>& locales) { | |
92 std::ostringstream os; | |
Dmitry Polukhin
2014/09/30 09:29:55
It is just simple string concatenation, you don't
Alexander Alekseev
2014/10/02 14:05:54
Done.
| |
93 os << "{"; | |
94 for (size_t i = 0; i < locales.size(); ++i) { | |
95 if (i != 0) { | |
96 os << ", "; | |
97 } | |
98 os << "'" << locales[i] << "'"; | |
99 } | |
100 os << "}"; | |
101 return os.str(); | |
102 } | |
103 | |
104 const char* kVPDInitialLocales[] = { | |
105 "en-AU", | |
106 "en-GB", | |
107 "en-US", | |
108 }; | |
109 | |
110 const std::vector<std::string> languages_available = { | |
111 "en-AU", | |
112 "en-GB", | |
113 "en-US", | |
114 "fr", | |
115 "fr-CA", | |
116 "he", | |
117 "iw", | |
118 "nb", | |
119 "no", | |
120 }; | |
121 | |
122 } // anonymous namespace | |
123 | |
124 class CustomizationLocaleTest : public InProcessBrowserTest { | |
125 public: | |
126 CustomizationLocaleTest() {} | |
127 virtual ~CustomizationLocaleTest() {} | |
128 | |
129 virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE { | |
Dmitry Polukhin
2014/09/30 09:29:54
It looks like there is no need to override functio
Alexander Alekseev
2014/10/03 12:59:17
Done.
| |
130 InProcessBrowserTest::SetUpCommandLine(command_line); | |
131 } | |
132 | |
133 virtual void SetUpOnMainThread() OVERRIDE { | |
Dmitry Polukhin
2014/09/30 09:29:54
Ditto.
Alexander Alekseev
2014/10/02 14:05:54
Done.
| |
134 InProcessBrowserTest::SetUpOnMainThread(); | |
135 } | |
136 }; | |
137 | |
138 IN_PROC_BROWSER_TEST_F(CustomizationLocaleTest, CheckAvailableLocales) { | |
139 for (size_t i = 0; i < languages_available.size(); ++i) { | |
140 LanguageSwitchedWaiter waiter(base::Bind(&VerifyLanguageSwitched)); | |
141 chromeos::locale_util::SwitchLanguage( | |
142 languages_available[i], true, true, waiter.Callback()); | |
143 waiter.Wait(); | |
144 { | |
145 std::string resolved_locale; | |
146 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
147 l10n_util::CheckAndResolveLocale(languages_available[i], | |
148 &resolved_locale); | |
149 EXPECT_EQ(GetExpectedLanguage(languages_available[i]), resolved_locale) | |
150 << "CheckAndResolveLocale() failed for language='" | |
151 << languages_available[i] << "'"; | |
152 } | |
153 } | |
154 } | |
155 | |
156 class CustomizationVPDTest : public InProcessBrowserTest, | |
157 public testing::WithParamInterface<const char*> { | |
Dmitry Polukhin
2014/09/30 09:29:54
Indent.
Alexander Alekseev
2014/10/02 14:05:54
Done.
| |
158 public: | |
159 CustomizationVPDTest() {} | |
160 virtual ~CustomizationVPDTest() {} | |
161 | |
162 virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE { | |
Dmitry Polukhin
2014/09/30 09:29:54
Ditto.
Alexander Alekseev
2014/10/02 14:05:54
Done.
| |
163 InProcessBrowserTest::SetUpCommandLine(command_line); | |
164 } | |
165 | |
166 virtual void SetUpOnMainThread() OVERRIDE { | |
Dmitry Polukhin
2014/09/30 09:29:54
Ditto.
Alexander Alekseev
2014/10/02 14:05:54
Done.
| |
167 InProcessBrowserTest::SetUpOnMainThread(); | |
168 } | |
169 }; | |
170 | |
171 IN_PROC_BROWSER_TEST_P(CustomizationVPDTest, GetUILanguageList) { | |
172 chromeos::SetEnforceInitialLocaleForTesting(GetParam()); | |
173 | |
174 std::vector<std::string> locales; | |
175 base::SplitString(GetParam(), ',', &locales); | |
176 | |
177 for (std::string& l : locales) { | |
178 base::TrimString(l, " ", &l); | |
179 } | |
180 EXPECT_EQ(locales, | |
181 chromeos::StartupCustomizationDocument::GetInstance() | |
182 ->configured_locales()) | |
183 << "Test failed for initial_locale='" << GetParam() | |
184 << "', locales=" << Print(locales); | |
185 | |
186 scoped_ptr<base::ListValue> ui_language_list = GetUILanguageList(NULL, ""); | |
187 EXPECT_GE(ui_language_list->GetSize(), locales.size()) | |
188 << "Test failed for initial_locale='" << GetParam() << "'"; | |
189 | |
190 for (size_t i = 0; i < ui_language_list->GetSize(); ++i) { | |
191 base::DictionaryValue* language_info = NULL; | |
192 ASSERT_TRUE(ui_language_list->GetDictionary(i, &language_info)) | |
193 << "Test failed for initial_locale='" << GetParam() << "', i=" << i; | |
194 | |
195 std::string value; | |
196 ASSERT_TRUE(language_info->GetString("value", &value)) | |
197 << "Test failed for initial_locale='" << GetParam() << "', i=" << i; | |
198 | |
199 if (i < locales.size()) { | |
200 EXPECT_EQ(locales[i], value) << "Test failed for initial_locale='" | |
201 << GetParam() << "', i=" << i; | |
202 } else { | |
203 EXPECT_EQ(chromeos::kMostRelevantLanguagesDivider, value) | |
204 << "Test failed for initial_locale='" << GetParam() << "', i=" << i; | |
205 break; | |
206 } | |
207 } | |
208 } | |
209 | |
210 INSTANTIATE_TEST_CASE_P(StringSequence, | |
211 CustomizationVPDTest, | |
212 testing::ValuesIn(kVPDInitialLocales)); | |
213 | |
214 } // namespace chromeos | |
OLD | NEW |