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/system/statistics_provider.h" | |
| 17 #include "content/public/browser/notification_service.h" | |
| 18 #include "content/public/browser/web_contents.h" | |
| 19 #include "content/public/test/browser_test_utils.h" | |
| 20 #include "content/public/test/test_utils.h" | |
| 21 | |
| 22 namespace base { | |
| 23 class TaskRunner; | |
| 24 } | |
| 25 | |
| 26 namespace chromeos { | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 // OOBE constants. | |
| 31 const char* kLocaleSelect = "language-select"; | |
| 32 const char* kKeyboardSelect = "keyboard-select"; | |
| 33 | |
| 34 const char* kUSLayout = "xkb:us::eng"; | |
| 35 | |
| 36 } | |
| 37 | |
| 38 namespace system { | |
| 39 | |
| 40 // Custom StatisticsProvider that will return each set of region settings. | |
| 41 class FakeStatisticsProvider : public StatisticsProvider { | |
| 42 public: | |
| 43 virtual ~FakeStatisticsProvider() {} | |
| 44 | |
| 45 void set_locale(const std::string& locale) { | |
| 46 initial_locale_ = locale; | |
| 47 } | |
| 48 | |
| 49 void set_keyboard_layout(const std::string& keyboard_layout) { | |
| 50 keyboard_layout_ = keyboard_layout; | |
| 51 } | |
| 52 | |
| 53 private: | |
| 54 // StatisticsProvider overrides. | |
| 55 virtual void StartLoadingMachineStatistics( | |
| 56 const scoped_refptr<base::TaskRunner>& file_task_runner, | |
| 57 bool load_oem_manifest) OVERRIDE { | |
| 58 } | |
| 59 | |
| 60 // Populates the named machine statistic for initial_locale and | |
| 61 // keyboard_layout only. | |
| 62 virtual bool GetMachineStatistic(const std::string& name, | |
| 63 std::string* result) OVERRIDE { | |
| 64 if (name == "initial_locale") | |
| 65 *result = initial_locale_; | |
| 66 else if (name == "keyboard_layout") | |
| 67 *result = keyboard_layout_; | |
| 68 else | |
| 69 return false; | |
| 70 | |
| 71 return true; | |
| 72 } | |
| 73 | |
| 74 virtual bool GetMachineFlag(const std::string& name, bool* result) OVERRIDE { | |
| 75 return false; | |
| 76 } | |
| 77 | |
| 78 virtual void Shutdown() OVERRIDE { | |
| 79 } | |
| 80 | |
| 81 std::string initial_locale_; | |
| 82 std::string keyboard_layout_; | |
| 83 }; | |
| 84 | |
| 85 } // namespace system | |
| 86 | |
| 87 class OobeLocalizationTest : public InProcessBrowserTest { | |
| 88 public: | |
| 89 OobeLocalizationTest(); | |
| 90 | |
| 91 // Returns true if the expression was successfully executed and returned true. | |
| 92 bool JsExecute(const std::string& expression); | |
| 93 | |
| 94 // Verifies that the comma-separated |values| corresponds with the first | |
| 95 // values in |select_id|, optionally checking for an options group label after | |
| 96 // the first set of options. | |
| 97 void VerifyInitialOptions(const char* select_id, | |
| 98 const char* values, | |
| 99 bool check_separator); | |
| 100 | |
| 101 // Verifies that |value| exists in |select_id|. | |
| 102 void VerifyOptionExists(const char* select_id, const char* value); | |
| 103 | |
| 104 protected: | |
| 105 // Runs the test for the given locale and keyboard layout. | |
| 106 void RunLocalizationTest(const std::string& initial_locale, | |
| 107 const std::string& keyboard_layout, | |
| 108 const std::string& expected_locale, | |
| 109 const std::string& expected_keyboard_layout); | |
| 110 | |
| 111 private: | |
| 112 scoped_ptr<system::FakeStatisticsProvider> statistics_provider_; | |
| 113 | |
| 114 DISALLOW_COPY_AND_ASSIGN(OobeLocalizationTest); | |
| 115 }; | |
| 116 | |
| 117 OobeLocalizationTest::OobeLocalizationTest() { | |
| 118 statistics_provider_.reset(new system::FakeStatisticsProvider()); | |
| 119 // Set the instance returned by GetInstance() for testing. | |
| 120 system::StatisticsProvider::SetTestProvider(statistics_provider_.get()); | |
| 121 } | |
| 122 | |
| 123 bool OobeLocalizationTest::JsExecute(const std::string& expression) { | |
|
achuithb
2014/02/12 23:17:20
Much nicer - thanks!
| |
| 124 chromeos::LoginDisplayHostImpl* display_host = | |
| 125 static_cast<chromeos::LoginDisplayHostImpl*>( | |
| 126 chromeos::LoginDisplayHostImpl::default_host()); | |
| 127 const std::string js("window.domAutomationController.send(!!(" + | |
| 128 expression + "));"); | |
| 129 | |
| 130 bool result; | |
| 131 if (content::ExecuteScriptAndExtractBool( | |
| 132 display_host->GetOobeUI()->web_ui()->GetWebContents(), js, &result)) | |
| 133 return result; | |
| 134 | |
| 135 // Execution failed. | |
| 136 return false; | |
| 137 } | |
| 138 | |
| 139 void OobeLocalizationTest::VerifyInitialOptions(const char* select_id, | |
| 140 const char* values, | |
| 141 bool check_separator) { | |
| 142 const std::string expression = base::StringPrintf( | |
| 143 "(function () {\n" | |
| 144 " var select = document.querySelector('#%s');\n" | |
| 145 " if (!select)\n" | |
| 146 " return false;\n" | |
| 147 " var values = '%s'.split(',');\n" | |
| 148 " var correct = select.selectedIndex == 0;\n" | |
| 149 " for (var i = 0; i < values.length && correct; i++) {\n" | |
| 150 " if (select.options[i].value != values[i])\n" | |
| 151 " correct = false;\n" | |
| 152 " }\n" | |
| 153 " if (%d && correct)\n" | |
| 154 " correct = select.children[values.length].tagName === 'OPTGROUP';\n" | |
| 155 " return correct;\n" | |
| 156 "})()", select_id, values, check_separator); | |
| 157 ASSERT_TRUE(JsExecute(expression)) << expression; | |
| 158 } | |
| 159 | |
| 160 void OobeLocalizationTest::VerifyOptionExists(const char* select_id, | |
| 161 const char* value) { | |
| 162 const std::string expression = base::StringPrintf( | |
| 163 "(function () {\n" | |
| 164 " var select = document.querySelector('#%s');\n" | |
| 165 " if (!select)\n" | |
| 166 " return false;\n" | |
| 167 " for (var i = 0; i < select.options.length; i++) {\n" | |
| 168 " if (select.options[i].value == '%s')\n" | |
| 169 " return true;\n" | |
| 170 " }\n" | |
| 171 " return false;\n" | |
| 172 "})()", select_id, value); | |
| 173 ASSERT_TRUE(JsExecute(expression)) << expression; | |
| 174 } | |
| 175 | |
| 176 void OobeLocalizationTest::RunLocalizationTest( | |
| 177 const std::string& initial_locale, | |
| 178 const std::string& keyboard_layout, | |
| 179 const std::string& expected_locale, | |
| 180 const std::string& expected_keyboard_layout) { | |
| 181 statistics_provider_->set_locale(initial_locale); | |
| 182 statistics_provider_->set_keyboard_layout(keyboard_layout); | |
| 183 | |
| 184 // Initialize StartupCustomizationDocument with fake statistics provider. | |
| 185 StartupCustomizationDocument::GetInstance()->Init( | |
| 186 statistics_provider_.get()); | |
| 187 | |
| 188 // Bring up the OOBE network screen. | |
| 189 chromeos::ShowLoginWizard(chromeos::WizardController::kNetworkScreenName); | |
| 190 content::WindowedNotificationObserver( | |
| 191 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, | |
| 192 content::NotificationService::AllSources()).Wait(); | |
| 193 | |
| 194 VerifyInitialOptions(kLocaleSelect, expected_locale.c_str(), true); | |
| 195 VerifyInitialOptions(kKeyboardSelect, | |
| 196 expected_keyboard_layout.c_str(), | |
| 197 false); | |
| 198 | |
| 199 // Make sure we have a fallback keyboard. | |
| 200 VerifyOptionExists(kKeyboardSelect, kUSLayout); | |
| 201 | |
| 202 // Shut down the display host. | |
| 203 chromeos::LoginDisplayHostImpl::default_host()->Finalize(); | |
| 204 base::MessageLoopForUI::current()->RunUntilIdle(); | |
| 205 | |
| 206 // Clear the locale pref so the statistics provider is pinged next time. | |
| 207 g_browser_process->local_state()->SetString(prefs::kApplicationLocale, | |
| 208 std::string()); | |
| 209 } | |
| 210 | |
| 211 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenNonLatin) { | |
| 212 // For a non-Latin keyboard layout like Russian, we expect to see the US | |
| 213 // keyboard. | |
| 214 RunLocalizationTest("ru", "xkb:ru::rus", | |
| 215 "ru", kUSLayout); | |
| 216 | |
| 217 // IMEs do not load at OOBE, so we just expect to see the (Latin) Japanese | |
| 218 // keyboard. | |
| 219 RunLocalizationTest("ja", "xkb:jp::jpn", | |
| 220 "ja", "xkb:jp::jpn"); | |
| 221 } | |
| 222 | |
| 223 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenKeyboardLayout) { | |
| 224 // We don't use the Icelandic locale but the Icelandic keyboard layout | |
| 225 // should still be selected when specified as the default. | |
| 226 RunLocalizationTest("en-US", "xkb:is::ice", | |
| 227 "en-US", "xkb:is::ice"); | |
| 228 } | |
| 229 | |
| 230 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenFullLatin) { | |
| 231 // French Swiss keyboard. | |
| 232 RunLocalizationTest("fr", "xkb:ch:fr:fra", | |
| 233 "fr", "xkb:ch:fr:fra"); | |
| 234 | |
| 235 // German Swiss keyboard. | |
| 236 RunLocalizationTest("de", "xkb:ch::ger", | |
| 237 "de", "xkb:ch::ger"); | |
| 238 } | |
| 239 | |
| 240 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenMultipleLocales) { | |
| 241 RunLocalizationTest("es,en-US,nl", "xkb:be::nld", | |
| 242 "es,en-US,nl", "xkb:be::nld"); | |
| 243 | |
| 244 RunLocalizationTest("ru,de", "xkb:ru::rus", | |
| 245 "ru,de", kUSLayout); | |
| 246 } | |
| 247 | |
| 248 } // namespace chromeos | |
| OLD | NEW |