| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
| 6 #include "base/prefs/pref_service.h" | 6 #include "base/prefs/pref_service.h" |
| 7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 8 #include "base/task_runner.h" | 8 #include "base/task_runner.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 } // namespace system | 90 } // namespace system |
| 91 | 91 |
| 92 class OobeLocalizationTest : public InProcessBrowserTest { | 92 class OobeLocalizationTest : public InProcessBrowserTest { |
| 93 public: | 93 public: |
| 94 OobeLocalizationTest(); | 94 OobeLocalizationTest(); |
| 95 virtual ~OobeLocalizationTest(); | 95 virtual ~OobeLocalizationTest(); |
| 96 | 96 |
| 97 // Verifies that the comma-separated |values| corresponds with the first | 97 // Verifies that the comma-separated |values| corresponds with the first |
| 98 // values in |select_id|, optionally checking for an options group label after | 98 // values in |select_id|, optionally checking for an options group label after |
| 99 // the first set of options. | 99 // the first set of options. |
| 100 void VerifyInitialOptions(const char* select_id, | 100 bool VerifyInitialOptions(const char* select_id, |
| 101 const char* values, | 101 const char* values, |
| 102 bool check_separator); | 102 bool check_separator); |
| 103 | 103 |
| 104 // Verifies that |value| exists in |select_id|. | 104 // Verifies that |value| exists in |select_id|. |
| 105 void VerifyOptionExists(const char* select_id, const char* value); | 105 bool VerifyOptionExists(const char* select_id, const char* value); |
| 106 | 106 |
| 107 // Dumps OOBE select control (language or keyboard) to string. | 107 // Dumps OOBE select control (language or keyboard) to string. |
| 108 std::string DumpOptions(const char* select_id); | 108 std::string DumpOptions(const char* select_id); |
| 109 | 109 |
| 110 protected: | 110 protected: |
| 111 // Runs the test for the given locale and keyboard layout. | 111 // Runs the test for the given locale and keyboard layout. |
| 112 void RunLocalizationTest(const std::string& initial_locale, | 112 void RunLocalizationTest(const std::string& initial_locale, |
| 113 const std::string& keyboard_layout, | 113 const std::string& keyboard_layout, |
| 114 const std::string& expected_locale, | 114 const std::string& expected_locale, |
| 115 const std::string& expected_keyboard_layout, | 115 const std::string& expected_keyboard_layout, |
| 116 const std::string& expected_keyboard_select_control); | 116 const std::string& expected_keyboard_select_control); |
| 117 | 117 |
| 118 private: | 118 private: |
| 119 scoped_ptr<system::FakeStatisticsProvider> statistics_provider_; | 119 scoped_ptr<system::FakeStatisticsProvider> statistics_provider_; |
| 120 test::JSChecker checker; | 120 test::JSChecker checker; |
| 121 | 121 |
| 122 DISALLOW_COPY_AND_ASSIGN(OobeLocalizationTest); | 122 DISALLOW_COPY_AND_ASSIGN(OobeLocalizationTest); |
| 123 }; | 123 }; |
| 124 | 124 |
| 125 OobeLocalizationTest::OobeLocalizationTest() { | 125 OobeLocalizationTest::OobeLocalizationTest() { |
| 126 statistics_provider_.reset(new system::FakeStatisticsProvider()); | 126 statistics_provider_.reset(new system::FakeStatisticsProvider()); |
| 127 // Set the instance returned by GetInstance() for testing. | 127 // Set the instance returned by GetInstance() for testing. |
| 128 system::StatisticsProvider::SetTestProvider(statistics_provider_.get()); | 128 system::StatisticsProvider::SetTestProvider(statistics_provider_.get()); |
| 129 } | 129 } |
| 130 | 130 |
| 131 OobeLocalizationTest::~OobeLocalizationTest() { | 131 OobeLocalizationTest::~OobeLocalizationTest() { |
| 132 system::StatisticsProvider::SetTestProvider(NULL); | 132 system::StatisticsProvider::SetTestProvider(NULL); |
| 133 } | 133 } |
| 134 | 134 |
| 135 void OobeLocalizationTest::VerifyInitialOptions(const char* select_id, | 135 bool OobeLocalizationTest::VerifyInitialOptions(const char* select_id, |
| 136 const char* values, | 136 const char* values, |
| 137 bool check_separator) { | 137 bool check_separator) { |
| 138 const std::string expression = base::StringPrintf( | 138 const std::string expression = base::StringPrintf( |
| 139 "(function () {\n" | 139 "(function () {\n" |
| 140 " var select = document.querySelector('#%s');\n" | 140 " var select = document.querySelector('#%s');\n" |
| 141 " if (!select)\n" | 141 " if (!select)\n" |
| 142 " return false;\n" | 142 " return false;\n" |
| 143 " var values = '%s'.split(',');\n" | 143 " var values = '%s'.split(',');\n" |
| 144 " var correct = select.selectedIndex == 0;\n" | 144 " var correct = select.selectedIndex == 0;\n" |
| 145 " for (var i = 0; i < values.length && correct; i++) {\n" | 145 " for (var i = 0; i < values.length && correct; i++) {\n" |
| 146 " if (select.options[i].value != values[i])\n" | 146 " if (select.options[i].value != values[i])\n" |
| 147 " correct = false;\n" | 147 " correct = false;\n" |
| 148 " }\n" | 148 " }\n" |
| 149 " if (%d && correct)\n" | 149 " if (%d && correct)\n" |
| 150 " correct = select.children[values.length].tagName === 'OPTGROUP';\n" | 150 " correct = select.children[values.length].tagName === 'OPTGROUP';\n" |
| 151 " return correct;\n" | 151 " return correct;\n" |
| 152 "})()", select_id, values, check_separator); | 152 "})()", select_id, values, check_separator); |
| 153 ASSERT_TRUE(checker.GetBool(expression)) << expression; | 153 const bool execute_status = checker.GetBool(expression); |
| 154 EXPECT_TRUE(execute_status) << expression; |
| 155 return execute_status; |
| 154 } | 156 } |
| 155 | 157 |
| 156 void OobeLocalizationTest::VerifyOptionExists(const char* select_id, | 158 bool OobeLocalizationTest::VerifyOptionExists(const char* select_id, |
| 157 const char* value) { | 159 const char* value) { |
| 158 const std::string expression = base::StringPrintf( | 160 const std::string expression = base::StringPrintf( |
| 159 "(function () {\n" | 161 "(function () {\n" |
| 160 " var select = document.querySelector('#%s');\n" | 162 " var select = document.querySelector('#%s');\n" |
| 161 " if (!select)\n" | 163 " if (!select)\n" |
| 162 " return false;\n" | 164 " return false;\n" |
| 163 " for (var i = 0; i < select.options.length; i++) {\n" | 165 " for (var i = 0; i < select.options.length; i++) {\n" |
| 164 " if (select.options[i].value == '%s')\n" | 166 " if (select.options[i].value == '%s')\n" |
| 165 " return true;\n" | 167 " return true;\n" |
| 166 " }\n" | 168 " }\n" |
| 167 " return false;\n" | 169 " return false;\n" |
| 168 "})()", select_id, value); | 170 "})()", select_id, value); |
| 169 ASSERT_TRUE(checker.GetBool(expression)) << expression; | 171 const bool execute_status = checker.GetBool(expression); |
| 172 EXPECT_TRUE(execute_status) << expression; |
| 173 return execute_status; |
| 170 } | 174 } |
| 171 | 175 |
| 172 std::string OobeLocalizationTest::DumpOptions(const char* select_id) { | 176 std::string OobeLocalizationTest::DumpOptions(const char* select_id) { |
| 173 const std::string expression = base::StringPrintf( | 177 const std::string expression = base::StringPrintf( |
| 174 "\n" | 178 "\n" |
| 175 "(function () {\n" | 179 "(function () {\n" |
| 176 " var selector = '#%s';\n" | 180 " var selector = '#%s';\n" |
| 177 " var divider = ',';\n" | 181 " var divider = ',';\n" |
| 178 " var select = document.querySelector(selector);\n" | 182 " var select = document.querySelector(selector);\n" |
| 179 " if (!select)\n" | 183 " if (!select)\n" |
| 180 " return 'document.querySelector(' + selector + ') failed.';\n" | 184 " return 'document.querySelector(' + selector + ') failed.';\n" |
| 181 " var dumpOptgroup = function(group) {\n" | 185 " var dumpOptgroup = function(group) {\n" |
| 182 " var result = '';\n" | 186 " var result = '';\n" |
| 183 " for (var i = 0; i < group.children.length; i++) {\n" | 187 " for (var i = 0; i < group.children.length; i++) {\n" |
| 184 " if (i > 0) {\n" | 188 " if (i > 0) {\n" |
| 185 " result += divider;\n" | 189 " result += divider;\n" |
| 186 " }\n" | 190 " }\n" |
| 187 " if (group.children[i].value) {\n" | 191 " if (group.children[i].value) {\n" |
| 188 " result += group.children[i].value;\n" | 192 " result += group.children[i].value;\n" |
| 189 " } else {\n" | 193 " } else {\n" |
| 190 " result += '__NO_VALUE__';\n" | 194 " result += '__NO_VALUE__';\n" |
| 191 " }\n" | 195 " }\n" |
| 192 " }\n" | 196 " }\n" |
| 193 " return result;\n" | 197 " return result;\n" |
| 194 " };\n" | 198 " };\n" |
| 195 " var result = '';\n" | 199 " var result = '';\n" |
| 200 " if (select.selectedIndex != 0) {\n" |
| 201 " result += '(selectedIndex=' + select.selectedIndex + \n" |
| 202 " ', selected \"' + select.options[select.selectedIndex].value +\n" |
| 203 " '\")';\n" |
| 204 " }\n" |
| 196 " var children = select.children;\n" | 205 " var children = select.children;\n" |
| 197 " for (var i = 0; i < children.length; i++) {\n" | 206 " for (var i = 0; i < children.length; i++) {\n" |
| 198 " if (i > 0) {\n" | 207 " if (i > 0) {\n" |
| 199 " result += divider;\n" | 208 " result += divider;\n" |
| 200 " }\n" | 209 " }\n" |
| 201 " if (children[i].value) {\n" | 210 " if (children[i].value) {\n" |
| 202 " result += children[i].value;\n" | 211 " result += children[i].value;\n" |
| 203 " } else if (children[i].tagName === 'OPTGROUP') {\n" | 212 " } else if (children[i].tagName === 'OPTGROUP') {\n" |
| 204 " result += '[' + dumpOptgroup(children[i]) + ']';\n" | 213 " result += '[' + dumpOptgroup(children[i]) + ']';\n" |
| 205 " } else {\n" | 214 " } else {\n" |
| 206 " result += '__NO_VALUE__';\n" | 215 " result += '__NO_VALUE__';\n" |
| 207 " }\n" | 216 " }\n" |
| 208 " }\n" | 217 " }\n" |
| 209 " return result;\n" | 218 " return result;\n" |
| 210 "})()\n", | 219 "})()\n", |
| 211 select_id); | 220 select_id); |
| 212 return checker.GetString(expression); | 221 return checker.GetString(expression); |
| 213 } | 222 } |
| 214 | 223 |
| 224 std::string TranslateXKB2Extension(const std::string& src) { |
| 225 std::string result(src); |
| 226 if (!extension_ime_util::UseWrappedExtensionKeyboardLayouts()) |
| 227 return result; |
| 228 // Modifies the expected keyboard select control options for the new |
| 229 // extension based xkb id. |
| 230 size_t pos = 0; |
| 231 std::string repl_old = "xkb:"; |
| 232 std::string repl_new = |
| 233 extension_ime_util::GetInputMethodIDByKeyboardLayout("xkb:"); |
| 234 while ((pos = result.find(repl_old, pos)) != std::string::npos) { |
| 235 result.replace(pos, repl_old.length(), repl_new); |
| 236 pos += repl_new.length(); |
| 237 } |
| 238 return result; |
| 239 } |
| 240 |
| 215 void OobeLocalizationTest::RunLocalizationTest( | 241 void OobeLocalizationTest::RunLocalizationTest( |
| 216 const std::string& initial_locale, | 242 const std::string& initial_locale, |
| 217 const std::string& keyboard_layout, | 243 const std::string& keyboard_layout, |
| 218 const std::string& expected_locale, | 244 const std::string& expected_locale, |
| 219 const std::string& expected_keyboard_layout, | 245 const std::string& expected_keyboard_layout, |
| 220 const std::string& expected_keyboard_select_control) { | 246 const std::string& expected_keyboard_select_control) { |
| 221 statistics_provider_->set_locale(initial_locale); | 247 statistics_provider_->set_locale(initial_locale); |
| 222 statistics_provider_->set_keyboard_layout(keyboard_layout); | 248 statistics_provider_->set_keyboard_layout(keyboard_layout); |
| 223 | 249 |
| 224 // Initialize StartupCustomizationDocument with fake statistics provider. | 250 // Initialize StartupCustomizationDocument with fake statistics provider. |
| 225 StartupCustomizationDocument::GetInstance()->Init( | 251 StartupCustomizationDocument::GetInstance()->Init( |
| 226 statistics_provider_.get()); | 252 statistics_provider_.get()); |
| 227 | 253 |
| 228 g_browser_process->local_state()->SetString( | 254 g_browser_process->local_state()->SetString( |
| 229 prefs::kHardwareKeyboardLayout, keyboard_layout); | 255 prefs::kHardwareKeyboardLayout, keyboard_layout); |
| 230 | 256 |
| 231 input_method::InputMethodManager::Get() | 257 input_method::InputMethodManager::Get() |
| 232 ->GetInputMethodUtil() | 258 ->GetInputMethodUtil() |
| 233 ->InitXkbInputMethodsForTesting(); | 259 ->InitXkbInputMethodsForTesting(); |
| 234 | 260 |
| 235 std::string expected_keyboard_select = expected_keyboard_select_control; | 261 const std::string expected_keyboard_select = |
| 236 if (extension_ime_util::UseWrappedExtensionKeyboardLayouts()) { | 262 TranslateXKB2Extension(expected_keyboard_select_control); |
| 237 // Modifies the expected keyboard select control options for the new | |
| 238 // extension based xkb id. | |
| 239 size_t pos = 0; | |
| 240 std::string repl_old = "xkb:"; | |
| 241 std::string repl_new = | |
| 242 extension_ime_util::GetInputMethodIDByKeyboardLayout("xkb:"); | |
| 243 while ((pos = expected_keyboard_select.find(repl_old, pos)) != | |
| 244 std::string::npos) { | |
| 245 expected_keyboard_select.replace(pos, repl_old.length(), repl_new); | |
| 246 pos += repl_new.length(); | |
| 247 } | |
| 248 } | |
| 249 | 263 |
| 250 // Bring up the OOBE network screen. | 264 // Bring up the OOBE network screen. |
| 251 chromeos::ShowLoginWizard(chromeos::WizardController::kNetworkScreenName); | 265 chromeos::ShowLoginWizard(chromeos::WizardController::kNetworkScreenName); |
| 252 content::WindowedNotificationObserver( | 266 content::WindowedNotificationObserver( |
| 253 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, | 267 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, |
| 254 content::NotificationService::AllSources()).Wait(); | 268 content::NotificationService::AllSources()).Wait(); |
| 255 | 269 |
| 256 checker.set_web_contents(static_cast<chromeos::LoginDisplayHostImpl*>( | 270 checker.set_web_contents(static_cast<chromeos::LoginDisplayHostImpl*>( |
| 257 chromeos::LoginDisplayHostImpl::default_host())-> | 271 chromeos::LoginDisplayHostImpl::default_host())-> |
| 258 GetOobeUI()->web_ui()->GetWebContents()); | 272 GetOobeUI()->web_ui()->GetWebContents()); |
| 259 | 273 |
| 260 VerifyInitialOptions(kLocaleSelect, expected_locale.c_str(), true); | 274 if (!VerifyInitialOptions(kLocaleSelect, expected_locale.c_str(), true)) { |
| 261 VerifyInitialOptions(kKeyboardSelect, | 275 LOG(ERROR) << "Actual value of " << kLocaleSelect << ":\n" |
| 262 extension_ime_util::GetInputMethodIDByKeyboardLayout( | 276 << DumpOptions(kLocaleSelect); |
| 263 expected_keyboard_layout).c_str(), | 277 } |
| 264 false); | 278 if (!VerifyInitialOptions( |
| 279 kKeyboardSelect, |
| 280 TranslateXKB2Extension(expected_keyboard_layout).c_str(), |
| 281 false)) { |
| 282 LOG(ERROR) << "Actual value of " << kKeyboardSelect << ":\n" |
| 283 << DumpOptions(kKeyboardSelect); |
| 284 } |
| 265 | 285 |
| 266 // Make sure we have a fallback keyboard. | 286 // Make sure we have a fallback keyboard. |
| 267 VerifyOptionExists( | 287 if (!VerifyOptionExists(kKeyboardSelect, |
| 268 kKeyboardSelect, | 288 extension_ime_util::GetInputMethodIDByKeyboardLayout( |
| 269 extension_ime_util::GetInputMethodIDByKeyboardLayout(kUSLayout).c_str()); | 289 kUSLayout).c_str())) { |
| 290 LOG(ERROR) << "Actual value of " << kKeyboardSelect << ":\n" |
| 291 << DumpOptions(kKeyboardSelect); |
| 292 } |
| 270 | 293 |
| 271 // Note, that sort order is locale-specific, but is unlikely to change. | 294 // Note, that sort order is locale-specific, but is unlikely to change. |
| 272 // Especially for keyboard layouts. | 295 // Especially for keyboard layouts. |
| 273 EXPECT_EQ(expected_keyboard_select, DumpOptions(kKeyboardSelect)); | 296 EXPECT_EQ(expected_keyboard_select, DumpOptions(kKeyboardSelect)); |
| 274 | 297 |
| 275 // Shut down the display host. | 298 // Shut down the display host. |
| 276 chromeos::LoginDisplayHostImpl::default_host()->Finalize(); | 299 chromeos::LoginDisplayHostImpl::default_host()->Finalize(); |
| 277 base::MessageLoopForUI::current()->RunUntilIdle(); | 300 base::MessageLoopForUI::current()->RunUntilIdle(); |
| 278 | 301 |
| 279 // Clear the locale pref so the statistics provider is pinged next time. | 302 // Clear the locale pref so the statistics provider is pinged next time. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenMultipleLocales) { | 351 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenMultipleLocales) { |
| 329 RunLocalizationTest("es,en-US,nl", "xkb:be::nld", | 352 RunLocalizationTest("es,en-US,nl", "xkb:be::nld", |
| 330 "es,en-US,nl", "xkb:be::nld", | 353 "es,en-US,nl", "xkb:be::nld", |
| 331 "xkb:be::nld,[xkb:es::spa,xkb:latam::spa,xkb:us::eng]"); | 354 "xkb:be::nld,[xkb:es::spa,xkb:latam::spa,xkb:us::eng]"); |
| 332 | 355 |
| 333 RunLocalizationTest("ru,de", "xkb:ru::rus", | 356 RunLocalizationTest("ru,de", "xkb:ru::rus", |
| 334 "ru,de", kUSLayout, | 357 "ru,de", kUSLayout, |
| 335 "xkb:us::eng"); | 358 "xkb:us::eng"); |
| 336 } | 359 } |
| 337 | 360 |
| 361 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenRegionalLocales) { |
| 362 // Syntetic example to test correct merging of different locales. |
| 363 RunLocalizationTest("fr-CH,it-CH,de-CH", |
| 364 "xkb:fr::fra,xkb:it::ita,xkb:de::ger", |
| 365 "fr-CH,it-CH,de-CH", |
| 366 "xkb:fr::fra", |
| 367 "xkb:fr::fra,xkb:it::ita,xkb:de::ger,[" |
| 368 "xkb:be::fra,xkb:ca::fra,xkb:ch:fr:fra," |
| 369 "xkb:ca:multix:fra,xkb:us::eng" |
| 370 "]"); |
| 371 // Another syntetic example. Check that british keyboard is available. |
| 372 RunLocalizationTest("en-AU", |
| 373 "xkb:us::eng", |
| 374 "en-AU", |
| 375 "xkb:us::eng", |
| 376 "xkb:us::eng,[xkb:gb:extd:eng,xkb:gb:dvorak:eng]"); |
| 377 } |
| 378 |
| 338 } // namespace chromeos | 379 } // namespace chromeos |
| OLD | NEW |