Chromium Code Reviews| Index: chrome/browser/chromeos/customization_document_browsertest.cc |
| diff --git a/chrome/browser/chromeos/customization_document_browsertest.cc b/chrome/browser/chromeos/customization_document_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bebab868fa6459520b5ec4c4cf093ee278887daa |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/customization_document_browsertest.cc |
| @@ -0,0 +1,257 @@ |
| +// Copyright 2014 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/bind.h" |
| +#include "base/command_line.h" |
| +#include "base/strings/string_split.h" |
| +#include "base/strings/string_util.h" |
| +#include "base/threading/thread_restrictions.h" |
| +#include "chrome/browser/chromeos/base/locale_util.h" |
| +#include "chrome/browser/chromeos/customization_document.h" |
| +#include "chrome/browser/ui/webui/chromeos/login/l10n_util.h" |
| +#include "chrome/test/base/in_process_browser_test.h" |
| +#include "chromeos/system/fake_statistics_provider.h" |
| +#include "chromeos/system/statistics_provider.h" |
| +#include "content/public/test/test_utils.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| + |
| +namespace chromeos { |
| + |
| +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.
|
| + |
| +namespace { |
| +class LanguageSwitchedWaiter { |
| + public: |
| + explicit LanguageSwitchedWaiter(SwitchLanguageCallback callback) |
| + : callback_(callback), |
| + finished_(false), |
| + 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.
|
| + ~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.
|
| + |
| + void ExitMessageLoop(const std::string& required, |
| + const std::string& actual, |
| + const bool success) { |
| + finished_ = true; |
| + runner_->Quit(); |
| + callback_.Run(required, actual, success); |
| + } |
| + |
| + void Wait() { |
| + if (finished_) |
| + return; |
| + runner_->Run(); |
| + } |
| + |
| + scoped_ptr<SwitchLanguageCallback> Callback() { |
| + 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.
|
| + return scoped_ptr<SwitchLanguageCallback>( |
| + new SwitchLanguageCallback( |
| + base::Bind(&LanguageSwitchedWaiter::ExitMessageLoop, |
| + base::Unretained(this)))).Pass(); |
| + } |
| + |
| + private: |
| + SwitchLanguageCallback callback_; |
| + bool finished_; |
| + scoped_refptr<content::MessageLoopRunner> runner_; |
| + DISALLOW_COPY_AND_ASSIGN(LanguageSwitchedWaiter); |
| +}; |
| + |
| +// Several language IDs are actually aliases to another IDs, so real language |
| +// ID is reported as "loaded" when alias is requested. |
| +std::string GetExpectedLanguage(const std::string& required) { |
| + std::string expected = required; |
| + 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.
|
| + expected = "en-GB"; |
| + } else if (required == "en-CA") { |
| + expected = "en-GB"; |
| + } else if (required == "en-NZ") { |
| + expected = "en-GB"; |
| + } else if (required == "en-ZA") { |
| + expected = "en-GB"; |
| + } else if (required == "fr-CA") { |
| + expected = "fr"; |
| + } else if (required == "no") { |
| + expected = "nb"; |
| + } else if (required == "iw") { |
| + expected = "he"; |
| + } |
| + return expected; |
| +} |
| + |
| +void VerifyLanguageSwitched(const std::string& required, |
| + const std::string& actual, |
| + const bool success) { |
| + EXPECT_TRUE(success) << "SwitchLanguage failed: required='" << required |
| + << "', actual='" << actual << "', success=" << success; |
| + EXPECT_EQ(GetExpectedLanguage(required), actual) |
| + << "SwitchLanguage failed: required='" << required << "', actual='" |
| + << actual << "', success=" << success; |
| +} |
| + |
| +std::string Print(const std::vector<std::string>& locales) { |
| + std::string result("{"); |
| + for (size_t i = 0; i < locales.size(); ++i) { |
| + if (i != 0) { |
| + result += ", "; |
| + } |
| + result += "'"; |
| + result += locales[i]; |
| + result += "'"; |
| + } |
| + result += "}"; |
| + return result; |
| +} |
| + |
| +const char* kVPDInitialLocales[] = { |
| + "ar,bg,bn,ca,cs,da,de,el,en-AU,en-CA,en-GB,en-NZ,en-US,en-ZA,es,es-419,et," |
| + "fa,fi,fil,fr,fr-CA,gu,he,hi,hr,hu,id,it,ja,kn,ko,lt,lv,ml,mr,ms,nl,nb,no," |
| + "pl,pt-BR,pt-PT,ro,ru,sk,sl,sr,sv,ta,te,th,tr,vi,zh-CN,zh-TW", |
| +}; |
| + |
| +const std::vector<std::string> languages_available = { |
| + "ar", |
| + "bg", |
| + "bn", |
| + "ca", |
| + "cs", |
| + "da", |
| + "de", |
| + "el", |
| + "en-AU", |
| + "en-CA", |
| + "en-GB", |
| + "en-NZ", |
| + "en-US", |
| + "en-ZA", |
| + "es", |
| + "es-419", |
| + "et", |
| + "fa", |
| + "fi", |
| + "fil", |
| + "fr", |
| + "fr-CA", |
| + "gu", |
| + "he", |
| + "hi", |
| + "hr", |
| + "hu", |
| + "id", |
| + "it", |
| + "ja", |
| + "kn", |
| + "ko", |
| + "lt", |
| + "lv", |
| + "ml", |
| + "mr", |
| + "ms", |
| + "nl", |
| + "nb", |
| + "no", |
| + "pl", |
| + "pt-BR", |
| + "pt-PT", |
| + "ro", |
| + "ru", |
| + "sk", |
| + "sl", |
| + "sr", |
| + "sv", |
| + "ta", |
| + "te", |
| + "th", |
| + "tr", |
| + "vi", |
| + "zh-CN", |
| + "zh-TW" |
| +}; |
| + |
| +} // anonymous namespace |
| + |
| +class CustomizationLocaleTest : public InProcessBrowserTest { |
| + public: |
| + CustomizationLocaleTest() {} |
| + 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.
|
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_F(CustomizationLocaleTest, CheckAvailableLocales) { |
| + for (size_t i = 0; i < languages_available.size(); ++i) { |
| + LanguageSwitchedWaiter waiter(base::Bind(&VerifyLanguageSwitched)); |
| + chromeos::locale_util::SwitchLanguage( |
| + languages_available[i], true, true, waiter.Callback()); |
| + waiter.Wait(); |
| + { |
| + std::string resolved_locale; |
| + base::ThreadRestrictions::ScopedAllowIO allow_io; |
| + l10n_util::CheckAndResolveLocale(languages_available[i], |
| + &resolved_locale); |
| + EXPECT_EQ(GetExpectedLanguage(languages_available[i]), resolved_locale) |
| + << "CheckAndResolveLocale() failed for language='" |
| + << languages_available[i] << "'"; |
| + } |
| + } |
| +} |
| + |
| +class CustomizationVPDTest : public InProcessBrowserTest, |
| + public testing::WithParamInterface<const char*> { |
| + public: |
| + CustomizationVPDTest() |
| + : statistics_provider_(new system::FakeStatisticsProvider()) { |
| + // Set the instance returned by GetInstance() for testing. |
| + system::StatisticsProvider::SetTestProvider(statistics_provider_.get()); |
| + statistics_provider_->set_locale(GetParam()); |
| + statistics_provider_->set_keyboard_layout(""); |
| + } |
| + |
| + 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.
|
| + |
| + private: |
| + scoped_ptr<system::FakeStatisticsProvider> statistics_provider_; |
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_P(CustomizationVPDTest, GetUILanguageList) { |
| + std::vector<std::string> locales; |
| + base::SplitString(GetParam(), ',', &locales); |
| + |
| + for (std::string& l : locales) { |
| + base::TrimString(l, " ", &l); |
| + } |
| + EXPECT_EQ(locales, |
| + 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.
|
| + ->configured_locales()) |
| + << "Test failed for initial_locale='" << GetParam() |
| + << "', locales=" << Print(locales); |
| + |
| + scoped_ptr<base::ListValue> ui_language_list = GetUILanguageList(NULL, ""); |
| + EXPECT_GE(ui_language_list->GetSize(), locales.size()) |
| + << "Test failed for initial_locale='" << GetParam() << "'"; |
| + |
| + for (size_t i = 0; i < ui_language_list->GetSize(); ++i) { |
| + base::DictionaryValue* language_info = NULL; |
| + ASSERT_TRUE(ui_language_list->GetDictionary(i, &language_info)) |
| + << "Test failed for initial_locale='" << GetParam() << "', i=" << i; |
| + |
| + std::string value; |
| + ASSERT_TRUE(language_info->GetString("value", &value)) |
| + << "Test failed for initial_locale='" << GetParam() << "', i=" << i; |
| + |
| + if (i < locales.size()) { |
| + EXPECT_EQ(locales[i], value) << "Test failed for initial_locale='" |
| + << GetParam() << "', i=" << i; |
| + } else { |
| + EXPECT_EQ(chromeos::kMostRelevantLanguagesDivider, value) |
|
Dmitry Polukhin
2014/10/03 07:49:52
Ditto.
Alexander Alekseev
2014/10/03 12:59:17
Done.
|
| + << "Test failed for initial_locale='" << GetParam() << "', i=" << i; |
| + break; |
| + } |
| + } |
| +} |
| + |
| +INSTANTIATE_TEST_CASE_P(StringSequence, |
| + CustomizationVPDTest, |
| + testing::ValuesIn(kVPDInitialLocales)); |
| + |
| +} // namespace chromeos |