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..170bcbc53c4aa5ff58dac174feb61f83e0e5c616 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/customization_document_browsertest.cc |
| @@ -0,0 +1,214 @@ |
| +// 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 <sstream> |
| + |
| +#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 "content/public/test/test_utils.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| + |
| +namespace chromeos { |
| + |
| +namespace { |
| + |
| +class LanguageSwitchedWaiter { |
| + public: |
| + explicit LanguageSwitchedWaiter(locale_util::SwitchLanguageCallback callback) |
| + : callback_(callback), |
| + finished_(false), |
| + runner_(new content::MessageLoopRunner){}; |
| + ~LanguageSwitchedWaiter(){}; |
| + |
| + 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<locale_util::SwitchLanguageCallback> Callback() { |
| + 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.
|
| + return scoped_ptr<SwitchLanguageCallback>( |
| + new SwitchLanguageCallback( |
| + base::Bind(&LanguageSwitchedWaiter::ExitMessageLoop, |
| + base::Unretained(this)))).Pass(); |
| + } |
| + |
| + private: |
| + locale_util::SwitchLanguageCallback callback_; |
| + bool finished_; |
| + scoped_refptr<content::MessageLoopRunner> runner_; |
| + DISALLOW_COPY_AND_ASSIGN(LanguageSwitchedWaiter); |
| +}; |
| + |
| +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.
|
| + std::string expected = required; |
| + if (required == "en-AU") { |
| + 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::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.
|
| + os << "{"; |
| + for (size_t i = 0; i < locales.size(); ++i) { |
| + if (i != 0) { |
| + os << ", "; |
| + } |
| + os << "'" << locales[i] << "'"; |
| + } |
| + os << "}"; |
| + return os.str(); |
| +} |
| + |
| +const char* kVPDInitialLocales[] = { |
| + "en-AU", |
| + "en-GB", |
| + "en-US", |
| +}; |
| + |
| +const std::vector<std::string> languages_available = { |
| + "en-AU", |
| + "en-GB", |
| + "en-US", |
| + "fr", |
| + "fr-CA", |
| + "he", |
| + "iw", |
| + "nb", |
| + "no", |
| +}; |
| + |
| +} // anonymous namespace |
| + |
| +class CustomizationLocaleTest : public InProcessBrowserTest { |
| + public: |
| + CustomizationLocaleTest() {} |
| + virtual ~CustomizationLocaleTest() {} |
| + |
| + 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.
|
| + InProcessBrowserTest::SetUpCommandLine(command_line); |
| + } |
| + |
| + virtual void SetUpOnMainThread() OVERRIDE { |
|
Dmitry Polukhin
2014/09/30 09:29:54
Ditto.
Alexander Alekseev
2014/10/02 14:05:54
Done.
|
| + InProcessBrowserTest::SetUpOnMainThread(); |
| + } |
| +}; |
| + |
| +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*> { |
|
Dmitry Polukhin
2014/09/30 09:29:54
Indent.
Alexander Alekseev
2014/10/02 14:05:54
Done.
|
| + public: |
| + CustomizationVPDTest() {} |
| + virtual ~CustomizationVPDTest() {} |
| + |
| + 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.
|
| + InProcessBrowserTest::SetUpCommandLine(command_line); |
| + } |
| + |
| + virtual void SetUpOnMainThread() OVERRIDE { |
|
Dmitry Polukhin
2014/09/30 09:29:54
Ditto.
Alexander Alekseev
2014/10/02 14:05:54
Done.
|
| + InProcessBrowserTest::SetUpOnMainThread(); |
| + } |
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_P(CustomizationVPDTest, GetUILanguageList) { |
| + chromeos::SetEnforceInitialLocaleForTesting(GetParam()); |
| + |
| + std::vector<std::string> locales; |
| + base::SplitString(GetParam(), ',', &locales); |
| + |
| + for (std::string& l : locales) { |
| + base::TrimString(l, " ", &l); |
| + } |
| + EXPECT_EQ(locales, |
| + chromeos::StartupCustomizationDocument::GetInstance() |
| + ->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) |
| + << "Test failed for initial_locale='" << GetParam() << "', i=" << i; |
| + break; |
| + } |
| + } |
| +} |
| + |
| +INSTANTIATE_TEST_CASE_P(StringSequence, |
| + CustomizationVPDTest, |
| + testing::ValuesIn(kVPDInitialLocales)); |
| + |
| +} // namespace chromeos |