Chromium Code Reviews| 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/message_loop/message_loop.h" | |
| 6 #include "base/prefs/pref_service.h" | |
| 7 #include "base/strings/stringprintf.h" | |
| 8 #include "base/task_runner.h" | |
| 9 #include "chrome/browser/browser_process.h" | |
| 10 #include "chrome/browser/chrome_notification_types.h" | |
| 11 #include "chrome/browser/chromeos/customization_document.h" | |
| 12 #include "chrome/browser/chromeos/login/login_display_host_impl.h" | |
| 13 #include "chrome/browser/chromeos/login/login_wizard.h" | |
| 14 #include "chrome/common/pref_names.h" | |
| 15 #include "chrome/test/base/in_process_browser_test.h" | |
| 16 #include "chromeos/ime/input_methods.h" | |
| 17 #include "chromeos/system/statistics_provider.h" | |
| 18 #include "content/public/browser/notification_service.h" | |
| 19 #include "content/public/browser/web_contents.h" | |
| 20 #include "content/public/test/browser_test_utils.h" | |
| 21 #include "content/public/test/test_utils.h" | |
| 22 #include "ui/base/l10n/l10n_util.h" | |
| 23 | |
| 24 namespace base { | |
| 25 class TaskRunner; | |
| 26 } | |
| 27 | |
| 28 namespace chromeos { | |
| 29 namespace system { | |
| 30 | |
| 31 // Custom StatisticsProvider that will return each set of region settings. | |
| 32 class FakeStatisticsProvider : public StatisticsProvider { | |
| 33 public: | |
| 34 virtual ~FakeStatisticsProvider() {} | |
| 35 | |
| 36 virtual void StartLoadingMachineStatistics( | |
| 37 const scoped_refptr<base::TaskRunner>& file_task_runner, | |
| 38 bool load_oem_manifest) OVERRIDE { | |
| 39 } | |
| 40 | |
| 41 // Populates the named machine statistic. | |
| 42 virtual bool GetMachineStatistic(const std::string& name, | |
| 43 std::string* result) OVERRIDE { | |
| 44 if (name == "initial_locale") | |
| 45 *result = initial_locale_; | |
| 46 else if (name == "keyboard_layout") | |
| 47 *result = keyboard_layout_; | |
| 48 else | |
| 49 return false; | |
| 50 | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 virtual bool GetMachineFlag(const std::string& name, bool* result) OVERRIDE { | |
| 55 return false; | |
| 56 } | |
| 57 | |
| 58 virtual void Shutdown() OVERRIDE { | |
| 59 } | |
| 60 | |
| 61 // Sets locale using the given element of kInputMethods. | |
| 62 void SetLocale(size_t index) { | |
|
Alexander Alekseev
2014/02/07 20:57:15
After https://codereview.chromium.org/144363006 in
| |
| 63 std::string locale(input_method::kInputMethods[index].language_code); | |
| 64 | |
| 65 // Set locale to first language in list. | |
| 66 std::string::size_type comma_pos = locale.find(','); | |
| 67 locale = std::string(locale, 0, comma_pos); | |
| 68 | |
| 69 std::string resolved_locale; | |
| 70 if (l10n_util::CheckAndResolveLocale(locale, &resolved_locale)) | |
| 71 initial_locale_ = resolved_locale; | |
| 72 else | |
| 73 initial_locale_ = locale; | |
| 74 } | |
| 75 | |
| 76 // Sets up the test with the region at the given element of kInputMethods. | |
| 77 bool SelectRegion(size_t index) { | |
| 78 if (index >= arraysize(input_method::kInputMethods)) | |
| 79 return false; | |
| 80 | |
| 81 SetLocale(index); | |
| 82 is_login_keyboard_ = input_method::kInputMethods[index].is_login_keyboard; | |
| 83 keyboard_layout_ = input_method::kInputMethods[index].input_method_id; | |
| 84 return true; | |
| 85 } | |
| 86 | |
| 87 bool is_login_keyboard() { | |
| 88 return is_login_keyboard_; | |
| 89 } | |
| 90 | |
| 91 std::string initial_locale() { | |
| 92 return initial_locale_; | |
| 93 } | |
| 94 | |
| 95 std::string keyboard_layout() { | |
| 96 return keyboard_layout_; | |
| 97 } | |
| 98 | |
| 99 private: | |
| 100 bool is_login_keyboard_; | |
| 101 std::string initial_locale_; | |
| 102 std::string keyboard_layout_; | |
| 103 }; | |
| 104 | |
| 105 } // namespace system | |
| 106 | |
| 107 namespace { | |
| 108 // OOBE constants. | |
| 109 const char* kLocaleSelect = "language-select"; | |
| 110 const char* kKeyboardSelect = "keyboard-select"; | |
| 111 const char* kUSLayout = "xkb:us::eng"; | |
| 112 } | |
| 113 | |
| 114 class OobeLocalizationTest : public InProcessBrowserTest { | |
| 115 public: | |
| 116 OobeLocalizationTest() { | |
| 117 statistics_provider_.reset(new system::FakeStatisticsProvider()); | |
| 118 // Set the instance returned by GetInstance() for testing. | |
| 119 system::StatisticsProvider::SetTestProvider(statistics_provider_.get()); | |
| 120 } | |
| 121 | |
| 122 void JsExpect(const std::string& expression) { | |
| 123 bool result; | |
| 124 chromeos::LoginDisplayHostImpl* display_host = | |
| 125 static_cast<chromeos::LoginDisplayHostImpl*>( | |
| 126 chromeos::LoginDisplayHostImpl::default_host()); | |
| 127 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | |
| 128 display_host->GetOobeUI()->web_ui()->GetWebContents(), | |
| 129 "window.domAutomationController.send(!!(" + expression + "));", | |
| 130 &result)); | |
| 131 ASSERT_TRUE(result) << expression; | |
| 132 } | |
| 133 | |
| 134 void VerifySelectedOption(const char* select_id, const char* value) { | |
| 135 JsExpect(base::StringPrintf( | |
| 136 "(function () {\n" | |
| 137 " var select = document.querySelector('#%s');\n" | |
| 138 " if (!select)\n" | |
| 139 " return false;\n" | |
| 140 " return select.options[select.selectedIndex].value == '%s';\n" | |
| 141 "})()", select_id, value)); | |
| 142 } | |
| 143 | |
| 144 void VerifyOptionExists(const char* select_id, const char* value) { | |
| 145 JsExpect(base::StringPrintf( | |
| 146 "(function () {\n" | |
| 147 " var select = document.querySelector('#%s');\n" | |
| 148 " if (!select)\n" | |
| 149 " return false;\n" | |
| 150 " for (var i = 0; i < select.options.length; i++) {\n" | |
| 151 " if (select.options[i].value == '%s')\n" | |
| 152 " return true;\n" | |
| 153 " }\n" | |
| 154 " return false;\n" | |
| 155 "})()", select_id, value)); | |
| 156 } | |
| 157 | |
| 158 protected: | |
| 159 // Runs the test for each locale in kInputMethods from (offset) to | |
| 160 // (offset + num_runs). | |
| 161 void RunLocalizationTest(size_t offset, int num_runs) { | |
| 162 // These locales are the ones we can make available at the network screen. | |
| 163 const std::vector<std::string>& locales = l10n_util::GetAvailableLocales(); | |
| 164 | |
| 165 int i = 0; | |
| 166 while (statistics_provider()->SelectRegion(i + offset) && | |
| 167 i < num_runs) { | |
| 168 // Initialize StartupCustomizationDocument with fake statistics provider | |
| 169 StartupCustomizationDocument::GetInstance()->Init(statistics_provider()); | |
| 170 | |
| 171 const std::string& locale = statistics_provider()->initial_locale(); | |
| 172 const std::string& layout = statistics_provider()->keyboard_layout(); | |
| 173 | |
| 174 // Bring up the OOBE network screen | |
| 175 chromeos::ShowLoginWizard(chromeos::WizardController::kNetworkScreenName); | |
| 176 content::WindowedNotificationObserver( | |
| 177 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, | |
| 178 content::NotificationService::AllSources()).Wait(); | |
| 179 | |
| 180 // Check that initial locale is available and selected (locale list isn't | |
| 181 // huge and each string is just a few chars). | |
| 182 if (std::find(locales.begin(), locales.end(), locale) != locales.end()) | |
| 183 VerifySelectedOption(kLocaleSelect, locale.c_str()); | |
| 184 | |
| 185 // Check that the keyboard is available and selected. | |
| 186 if (statistics_provider()->is_login_keyboard()) | |
| 187 VerifySelectedOption(kKeyboardSelect, layout.c_str()); | |
| 188 | |
| 189 // Make sure we have a fallback keyboard. | |
| 190 VerifyOptionExists(kKeyboardSelect, kUSLayout); | |
| 191 | |
| 192 // Shut down the display host. | |
| 193 chromeos::LoginDisplayHostImpl::default_host()->Finalize(); | |
| 194 base::MessageLoopForUI::current()->RunUntilIdle(); | |
| 195 | |
| 196 // Clear the locale pref so the statistics provider is pinged next time. | |
| 197 g_browser_process->local_state()->SetString(prefs::kApplicationLocale, | |
| 198 std::string()); | |
| 199 i++; | |
| 200 } | |
| 201 } | |
| 202 | |
| 203 system::FakeStatisticsProvider* statistics_provider() { | |
| 204 return statistics_provider_.get(); | |
| 205 } | |
| 206 | |
| 207 private: | |
| 208 scoped_ptr<system::FakeStatisticsProvider> statistics_provider_; | |
| 209 | |
| 210 DISALLOW_COPY_AND_ASSIGN(OobeLocalizationTest); | |
| 211 }; | |
| 212 | |
| 213 // 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
| |
| 214 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen1) { | |
| 215 RunLocalizationTest(0, 5); | |
| 216 } | |
| 217 | |
| 218 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen2) { | |
| 219 RunLocalizationTest(5, 5); | |
| 220 } | |
| 221 | |
| 222 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen3) { | |
| 223 RunLocalizationTest(10, 5); | |
| 224 } | |
| 225 | |
| 226 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen4) { | |
| 227 RunLocalizationTest(15, 5); | |
| 228 } | |
| 229 | |
| 230 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen5) { | |
| 231 RunLocalizationTest(20, 5); | |
| 232 } | |
| 233 | |
| 234 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen6) { | |
| 235 RunLocalizationTest(25, 5); | |
| 236 } | |
| 237 | |
| 238 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen7) { | |
| 239 RunLocalizationTest(30, 5); | |
| 240 } | |
| 241 | |
| 242 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen8) { | |
| 243 RunLocalizationTest(35, 5); | |
| 244 } | |
| 245 | |
| 246 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen9) { | |
| 247 RunLocalizationTest(40, 5); | |
| 248 } | |
| 249 | |
| 250 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen10) { | |
| 251 RunLocalizationTest(45, 5); | |
| 252 } | |
| 253 | |
| 254 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen11) { | |
| 255 RunLocalizationTest(50, 5); | |
| 256 } | |
| 257 | |
| 258 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen12) { | |
| 259 RunLocalizationTest(55, 5); | |
| 260 } | |
| 261 | |
| 262 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen13) { | |
| 263 RunLocalizationTest(60, 5); | |
| 264 } | |
| 265 | |
| 266 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreen14) { | |
| 267 RunLocalizationTest(65, 5); | |
| 268 } | |
| 269 | |
| 270 } // namespace chromeos | |
| OLD | NEW |