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