Chromium Code Reviews| Index: chrome/browser/chromeos/login/oobe_localization_browsertest.cc |
| diff --git a/chrome/browser/chromeos/login/oobe_localization_browsertest.cc b/chrome/browser/chromeos/login/oobe_localization_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4f241db8f6708338e66907a17fc48a35ab512089 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/login/oobe_localization_browsertest.cc |
| @@ -0,0 +1,270 @@ |
| +// 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/message_loop/message_loop.h" |
| +#include "base/prefs/pref_service.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "base/task_runner.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/chrome_notification_types.h" |
| +#include "chrome/browser/chromeos/customization_document.h" |
| +#include "chrome/browser/chromeos/login/login_display_host_impl.h" |
| +#include "chrome/browser/chromeos/login/login_wizard.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chrome/test/base/in_process_browser_test.h" |
| +#include "chromeos/ime/input_methods.h" |
| +#include "chromeos/system/statistics_provider.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/test/browser_test_utils.h" |
| +#include "content/public/test/test_utils.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| + |
| +namespace base { |
| +class TaskRunner; |
| +} |
| + |
| +namespace chromeos { |
| +namespace system { |
| + |
| +// Custom StatisticsProvider that will return each set of region settings. |
| +class FakeStatisticsProvider : public StatisticsProvider { |
| + public: |
| + virtual ~FakeStatisticsProvider() {} |
| + |
| + virtual void StartLoadingMachineStatistics( |
| + const scoped_refptr<base::TaskRunner>& file_task_runner, |
| + bool load_oem_manifest) OVERRIDE { |
| + } |
| + |
| + // Populates the named machine statistic. |
| + virtual bool GetMachineStatistic(const std::string& name, |
| + std::string* result) OVERRIDE { |
| + if (name == "initial_locale") |
| + *result = initial_locale_; |
| + else if (name == "keyboard_layout") |
| + *result = keyboard_layout_; |
| + else |
| + return false; |
| + |
| + return true; |
| + } |
| + |
| + virtual bool GetMachineFlag(const std::string& name, bool* result) OVERRIDE { |
| + return false; |
| + } |
| + |
| + virtual void Shutdown() OVERRIDE { |
| + } |
| + |
| + // Sets locale using the given element of kInputMethods. |
| + void SetLocale(size_t index) { |
|
Alexander Alekseev
2014/02/07 20:57:15
After https://codereview.chromium.org/144363006 in
|
| + std::string locale(input_method::kInputMethods[index].language_code); |
| + |
| + // Set locale to first language in list. |
| + std::string::size_type comma_pos = locale.find(','); |
| + locale = std::string(locale, 0, comma_pos); |
| + |
| + std::string resolved_locale; |
| + if (l10n_util::CheckAndResolveLocale(locale, &resolved_locale)) |
| + initial_locale_ = resolved_locale; |
| + else |
| + initial_locale_ = locale; |
| + } |
| + |
| + // Sets up the test with the region at the given element of kInputMethods. |
| + bool SelectRegion(size_t index) { |
| + if (index >= arraysize(input_method::kInputMethods)) |
| + return false; |
| + |
| + SetLocale(index); |
| + is_login_keyboard_ = input_method::kInputMethods[index].is_login_keyboard; |
| + keyboard_layout_ = input_method::kInputMethods[index].input_method_id; |
| + return true; |
| + } |
| + |
| + bool is_login_keyboard() { |
| + return is_login_keyboard_; |
| + } |
| + |
| + std::string initial_locale() { |
| + return initial_locale_; |
| + } |
| + |
| + std::string keyboard_layout() { |
| + return keyboard_layout_; |
| + } |
| + |
| + private: |
| + bool is_login_keyboard_; |
| + std::string initial_locale_; |
| + std::string keyboard_layout_; |
| +}; |
| + |
| +} // namespace system |
| + |
| +namespace { |
| +// OOBE constants. |
| +const char* kLocaleSelect = "language-select"; |
| +const char* kKeyboardSelect = "keyboard-select"; |
| +const char* kUSLayout = "xkb:us::eng"; |
| +} |
| + |
| +class OobeLocalizationTest : public InProcessBrowserTest { |
| + public: |
| + OobeLocalizationTest() { |
| + statistics_provider_.reset(new system::FakeStatisticsProvider()); |
| + // Set the instance returned by GetInstance() for testing. |
| + system::StatisticsProvider::SetTestProvider(statistics_provider_.get()); |
| + } |
| + |
| + void JsExpect(const std::string& expression) { |
| + bool result; |
| + chromeos::LoginDisplayHostImpl* display_host = |
| + static_cast<chromeos::LoginDisplayHostImpl*>( |
| + chromeos::LoginDisplayHostImpl::default_host()); |
| + ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
| + display_host->GetOobeUI()->web_ui()->GetWebContents(), |
| + "window.domAutomationController.send(!!(" + expression + "));", |
| + &result)); |
| + ASSERT_TRUE(result) << expression; |
| + } |
| + |
| + void VerifySelectedOption(const char* select_id, const char* value) { |
| + JsExpect(base::StringPrintf( |
| + "(function () {\n" |
| + " var select = document.querySelector('#%s');\n" |
| + " if (!select)\n" |
| + " return false;\n" |
| + " return select.options[select.selectedIndex].value == '%s';\n" |
| + "})()", select_id, value)); |
| + } |
| + |
| + void VerifyOptionExists(const char* select_id, const char* value) { |
| + JsExpect(base::StringPrintf( |
| + "(function () {\n" |
| + " var select = document.querySelector('#%s');\n" |
| + " if (!select)\n" |
| + " return false;\n" |
| + " for (var i = 0; i < select.options.length; i++) {\n" |
| + " if (select.options[i].value == '%s')\n" |
| + " return true;\n" |
| + " }\n" |
| + " return false;\n" |
| + "})()", select_id, value)); |
| + } |
| + |
| + protected: |
| + // Runs the test for each locale in kInputMethods from (offset) to |
| + // (offset + num_runs). |
| + void RunLocalizationTest(size_t offset, int num_runs) { |
| + // These locales are the ones we can make available at the network screen. |
| + const std::vector<std::string>& locales = l10n_util::GetAvailableLocales(); |
| + |
| + int i = 0; |
| + while (statistics_provider()->SelectRegion(i + offset) && |
| + i < num_runs) { |
| + // Initialize StartupCustomizationDocument with fake statistics provider |
| + StartupCustomizationDocument::GetInstance()->Init(statistics_provider()); |
| + |
| + const std::string& locale = statistics_provider()->initial_locale(); |
| + const std::string& layout = statistics_provider()->keyboard_layout(); |
| + |
| + // Bring up the OOBE network screen |
| + chromeos::ShowLoginWizard(chromeos::WizardController::kNetworkScreenName); |
| + content::WindowedNotificationObserver( |
| + chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, |
| + content::NotificationService::AllSources()).Wait(); |
| + |
| + // Check that initial locale is available and selected (locale list isn't |
| + // huge and each string is just a few chars). |
| + if (std::find(locales.begin(), locales.end(), locale) != locales.end()) |
| + VerifySelectedOption(kLocaleSelect, locale.c_str()); |
| + |
| + // Check that the keyboard is available and selected. |
| + if (statistics_provider()->is_login_keyboard()) |
| + VerifySelectedOption(kKeyboardSelect, layout.c_str()); |
| + |
| + // Make sure we have a fallback keyboard. |
| + VerifyOptionExists(kKeyboardSelect, kUSLayout); |
| + |
| + // Shut down the display host. |
| + chromeos::LoginDisplayHostImpl::default_host()->Finalize(); |
| + base::MessageLoopForUI::current()->RunUntilIdle(); |
| + |
| + // Clear the locale pref so the statistics provider is pinged next time. |
| + g_browser_process->local_state()->SetString(prefs::kApplicationLocale, |
| + std::string()); |
| + i++; |
| + } |
| + } |
| + |
| + system::FakeStatisticsProvider* statistics_provider() { |
| + return statistics_provider_.get(); |
| + } |
| + |
| + private: |
| + scoped_ptr<system::FakeStatisticsProvider> statistics_provider_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(OobeLocalizationTest); |
| +}; |
| + |
| +// To avoid timeout, run tests in sets of 5. |
|
michaelpg
2014/02/07 02:11:59
Better ideas? There are like 55 languages and each
|
| +IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen1) { |
| + RunLocalizationTest(0, 5); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen2) { |
| + RunLocalizationTest(5, 5); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen3) { |
| + RunLocalizationTest(10, 5); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen4) { |
| + RunLocalizationTest(15, 5); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen5) { |
| + RunLocalizationTest(20, 5); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen6) { |
| + RunLocalizationTest(25, 5); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen7) { |
| + RunLocalizationTest(30, 5); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen8) { |
| + RunLocalizationTest(35, 5); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen9) { |
| + RunLocalizationTest(40, 5); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen10) { |
| + RunLocalizationTest(45, 5); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen11) { |
| + RunLocalizationTest(50, 5); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen12) { |
| + RunLocalizationTest(55, 5); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen13) { |
| + RunLocalizationTest(60, 5); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen14) { |
| + RunLocalizationTest(65, 5); |
| +} |
| + |
| +} // namespace chromeos |