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" |
11 #include "chrome/browser/chromeos/customization_document.h" | 11 #include "chrome/browser/chromeos/customization_document.h" |
12 #include "chrome/browser/chromeos/login/login_display_host_impl.h" | 12 #include "chrome/browser/chromeos/login/login_display_host_impl.h" |
13 #include "chrome/browser/chromeos/login/login_wizard.h" | 13 #include "chrome/browser/chromeos/login/login_wizard.h" |
| 14 #include "chrome/browser/chromeos/login/test/js_checker.h" |
14 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
15 #include "chrome/test/base/in_process_browser_test.h" | 16 #include "chrome/test/base/in_process_browser_test.h" |
16 #include "chromeos/system/statistics_provider.h" | 17 #include "chromeos/system/statistics_provider.h" |
17 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
18 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
19 #include "content/public/test/browser_test_utils.h" | 20 #include "content/public/test/browser_test_utils.h" |
20 #include "content/public/test/test_utils.h" | 21 #include "content/public/test/test_utils.h" |
21 | 22 |
22 namespace base { | 23 namespace base { |
23 class TaskRunner; | 24 class TaskRunner; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 std::string initial_locale_; | 82 std::string initial_locale_; |
82 std::string keyboard_layout_; | 83 std::string keyboard_layout_; |
83 }; | 84 }; |
84 | 85 |
85 } // namespace system | 86 } // namespace system |
86 | 87 |
87 class OobeLocalizationTest : public InProcessBrowserTest { | 88 class OobeLocalizationTest : public InProcessBrowserTest { |
88 public: | 89 public: |
89 OobeLocalizationTest(); | 90 OobeLocalizationTest(); |
90 | 91 |
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 | 92 // Verifies that the comma-separated |values| corresponds with the first |
95 // values in |select_id|, optionally checking for an options group label after | 93 // values in |select_id|, optionally checking for an options group label after |
96 // the first set of options. | 94 // the first set of options. |
97 void VerifyInitialOptions(const char* select_id, | 95 void VerifyInitialOptions(const char* select_id, |
98 const char* values, | 96 const char* values, |
99 bool check_separator); | 97 bool check_separator); |
100 | 98 |
101 // Verifies that |value| exists in |select_id|. | 99 // Verifies that |value| exists in |select_id|. |
102 void VerifyOptionExists(const char* select_id, const char* value); | 100 void VerifyOptionExists(const char* select_id, const char* value); |
103 | 101 |
| 102 // Dumps OOBE select control (language or keyboard) to string. |
| 103 std::string DumpOptions(const char* select_id); |
| 104 |
104 protected: | 105 protected: |
105 // Runs the test for the given locale and keyboard layout. | 106 // Runs the test for the given locale and keyboard layout. |
106 void RunLocalizationTest(const std::string& initial_locale, | 107 void RunLocalizationTest(const std::string& initial_locale, |
107 const std::string& keyboard_layout, | 108 const std::string& keyboard_layout, |
108 const std::string& expected_locale, | 109 const std::string& expected_locale, |
109 const std::string& expected_keyboard_layout); | 110 const std::string& expected_keyboard_layout, |
| 111 const std::string& expected_keyboard_select_control); |
110 | 112 |
111 private: | 113 private: |
112 scoped_ptr<system::FakeStatisticsProvider> statistics_provider_; | 114 scoped_ptr<system::FakeStatisticsProvider> statistics_provider_; |
| 115 test::JSChecker checker; |
113 | 116 |
114 DISALLOW_COPY_AND_ASSIGN(OobeLocalizationTest); | 117 DISALLOW_COPY_AND_ASSIGN(OobeLocalizationTest); |
115 }; | 118 }; |
116 | 119 |
117 OobeLocalizationTest::OobeLocalizationTest() { | 120 OobeLocalizationTest::OobeLocalizationTest() { |
118 statistics_provider_.reset(new system::FakeStatisticsProvider()); | 121 statistics_provider_.reset(new system::FakeStatisticsProvider()); |
119 // Set the instance returned by GetInstance() for testing. | 122 // Set the instance returned by GetInstance() for testing. |
120 system::StatisticsProvider::SetTestProvider(statistics_provider_.get()); | 123 system::StatisticsProvider::SetTestProvider(statistics_provider_.get()); |
121 } | 124 } |
122 | 125 |
123 bool OobeLocalizationTest::JsExecute(const std::string& expression) { | |
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, | 126 void OobeLocalizationTest::VerifyInitialOptions(const char* select_id, |
140 const char* values, | 127 const char* values, |
141 bool check_separator) { | 128 bool check_separator) { |
142 const std::string expression = base::StringPrintf( | 129 const std::string expression = base::StringPrintf( |
143 "(function () {\n" | 130 "(function () {\n" |
144 " var select = document.querySelector('#%s');\n" | 131 " var select = document.querySelector('#%s');\n" |
145 " if (!select)\n" | 132 " if (!select)\n" |
146 " return false;\n" | 133 " return false;\n" |
147 " var values = '%s'.split(',');\n" | 134 " var values = '%s'.split(',');\n" |
148 " var correct = select.selectedIndex == 0;\n" | 135 " var correct = select.selectedIndex == 0;\n" |
149 " for (var i = 0; i < values.length && correct; i++) {\n" | 136 " for (var i = 0; i < values.length && correct; i++) {\n" |
150 " if (select.options[i].value != values[i])\n" | 137 " if (select.options[i].value != values[i])\n" |
151 " correct = false;\n" | 138 " correct = false;\n" |
152 " }\n" | 139 " }\n" |
153 " if (%d && correct)\n" | 140 " if (%d && correct)\n" |
154 " correct = select.children[values.length].tagName === 'OPTGROUP';\n" | 141 " correct = select.children[values.length].tagName === 'OPTGROUP';\n" |
155 " return correct;\n" | 142 " return correct;\n" |
156 "})()", select_id, values, check_separator); | 143 "})()", select_id, values, check_separator); |
157 ASSERT_TRUE(JsExecute(expression)) << expression; | 144 ASSERT_TRUE(checker.GetBool(expression)) << expression; |
158 } | 145 } |
159 | 146 |
160 void OobeLocalizationTest::VerifyOptionExists(const char* select_id, | 147 void OobeLocalizationTest::VerifyOptionExists(const char* select_id, |
161 const char* value) { | 148 const char* value) { |
162 const std::string expression = base::StringPrintf( | 149 const std::string expression = base::StringPrintf( |
163 "(function () {\n" | 150 "(function () {\n" |
164 " var select = document.querySelector('#%s');\n" | 151 " var select = document.querySelector('#%s');\n" |
165 " if (!select)\n" | 152 " if (!select)\n" |
166 " return false;\n" | 153 " return false;\n" |
167 " for (var i = 0; i < select.options.length; i++) {\n" | 154 " for (var i = 0; i < select.options.length; i++) {\n" |
168 " if (select.options[i].value == '%s')\n" | 155 " if (select.options[i].value == '%s')\n" |
169 " return true;\n" | 156 " return true;\n" |
170 " }\n" | 157 " }\n" |
171 " return false;\n" | 158 " return false;\n" |
172 "})()", select_id, value); | 159 "})()", select_id, value); |
173 ASSERT_TRUE(JsExecute(expression)) << expression; | 160 ASSERT_TRUE(checker.GetBool(expression)) << expression; |
| 161 } |
| 162 |
| 163 std::string OobeLocalizationTest::DumpOptions(const char* select_id) { |
| 164 const std::string expression = base::StringPrintf( |
| 165 "\n" |
| 166 "(function () {\n" |
| 167 " var selector = '#%s';\n" |
| 168 " var divider = ',';\n" |
| 169 " var select = document.querySelector(selector);\n" |
| 170 " if (!select)\n" |
| 171 " return 'document.querySelector(' + selector + ') failed.';\n" |
| 172 " var dumpOptgroup = function(group) {\n" |
| 173 " var result = '';\n" |
| 174 " for (var i = 0; i < group.children.length; i++) {\n" |
| 175 " if (i > 0) {\n" |
| 176 " result += divider;\n" |
| 177 " }\n" |
| 178 " if (group.children[i].value) {\n" |
| 179 " result += group.children[i].value;\n" |
| 180 " } else {\n" |
| 181 " result += '__NO_VALUE__';\n" |
| 182 " }\n" |
| 183 " }\n" |
| 184 " return result;\n" |
| 185 " };\n" |
| 186 " var result = '';\n" |
| 187 " var children = select.children;\n" |
| 188 " for (var i = 0; i < children.length; i++) {\n" |
| 189 " if (i > 0) {\n" |
| 190 " result += divider;\n" |
| 191 " }\n" |
| 192 " if (children[i].value) {\n" |
| 193 " result += children[i].value;\n" |
| 194 " } else if (children[i].tagName === 'OPTGROUP') {\n" |
| 195 " result += '[' + dumpOptgroup(children[i]) + ']';\n" |
| 196 " } else {\n" |
| 197 " result += '__NO_VALUE__';\n" |
| 198 " }\n" |
| 199 " }\n" |
| 200 " return result;\n" |
| 201 "})()\n", |
| 202 select_id); |
| 203 return checker.GetString(expression); |
174 } | 204 } |
175 | 205 |
176 void OobeLocalizationTest::RunLocalizationTest( | 206 void OobeLocalizationTest::RunLocalizationTest( |
177 const std::string& initial_locale, | 207 const std::string& initial_locale, |
178 const std::string& keyboard_layout, | 208 const std::string& keyboard_layout, |
179 const std::string& expected_locale, | 209 const std::string& expected_locale, |
180 const std::string& expected_keyboard_layout) { | 210 const std::string& expected_keyboard_layout, |
| 211 const std::string& expected_keyboard_select_control) { |
181 statistics_provider_->set_locale(initial_locale); | 212 statistics_provider_->set_locale(initial_locale); |
182 statistics_provider_->set_keyboard_layout(keyboard_layout); | 213 statistics_provider_->set_keyboard_layout(keyboard_layout); |
183 | 214 |
184 // Initialize StartupCustomizationDocument with fake statistics provider. | 215 // Initialize StartupCustomizationDocument with fake statistics provider. |
185 StartupCustomizationDocument::GetInstance()->Init( | 216 StartupCustomizationDocument::GetInstance()->Init( |
186 statistics_provider_.get()); | 217 statistics_provider_.get()); |
187 | 218 |
188 // Bring up the OOBE network screen. | 219 // Bring up the OOBE network screen. |
189 chromeos::ShowLoginWizard(chromeos::WizardController::kNetworkScreenName); | 220 chromeos::ShowLoginWizard(chromeos::WizardController::kNetworkScreenName); |
190 content::WindowedNotificationObserver( | 221 content::WindowedNotificationObserver( |
191 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, | 222 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, |
192 content::NotificationService::AllSources()).Wait(); | 223 content::NotificationService::AllSources()).Wait(); |
193 | 224 |
| 225 checker.set_web_contents(static_cast<chromeos::LoginDisplayHostImpl*>( |
| 226 chromeos::LoginDisplayHostImpl::default_host())-> |
| 227 GetOobeUI()->web_ui()->GetWebContents()); |
| 228 |
194 VerifyInitialOptions(kLocaleSelect, expected_locale.c_str(), true); | 229 VerifyInitialOptions(kLocaleSelect, expected_locale.c_str(), true); |
195 VerifyInitialOptions(kKeyboardSelect, | 230 VerifyInitialOptions(kKeyboardSelect, |
196 expected_keyboard_layout.c_str(), | 231 expected_keyboard_layout.c_str(), |
197 false); | 232 false); |
198 | 233 |
199 // Make sure we have a fallback keyboard. | 234 // Make sure we have a fallback keyboard. |
200 VerifyOptionExists(kKeyboardSelect, kUSLayout); | 235 VerifyOptionExists(kKeyboardSelect, kUSLayout); |
201 | 236 |
| 237 // Note, that sort order is locale-specific, but is unlikely to change. |
| 238 // Especially for keyboard layouts. |
| 239 EXPECT_EQ(expected_keyboard_select_control, DumpOptions(kKeyboardSelect)); |
| 240 |
202 // Shut down the display host. | 241 // Shut down the display host. |
203 chromeos::LoginDisplayHostImpl::default_host()->Finalize(); | 242 chromeos::LoginDisplayHostImpl::default_host()->Finalize(); |
204 base::MessageLoopForUI::current()->RunUntilIdle(); | 243 base::MessageLoopForUI::current()->RunUntilIdle(); |
205 | 244 |
206 // Clear the locale pref so the statistics provider is pinged next time. | 245 // Clear the locale pref so the statistics provider is pinged next time. |
207 g_browser_process->local_state()->SetString(prefs::kApplicationLocale, | 246 g_browser_process->local_state()->SetString(prefs::kApplicationLocale, |
208 std::string()); | 247 std::string()); |
209 } | 248 } |
210 | 249 |
211 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenNonLatin) { | 250 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenNonLatin) { |
212 // For a non-Latin keyboard layout like Russian, we expect to see the US | 251 // For a non-Latin keyboard layout like Russian, we expect to see the US |
213 // keyboard. | 252 // keyboard. |
214 RunLocalizationTest("ru", "xkb:ru::rus", | 253 RunLocalizationTest("ru", "xkb:ru::rus", |
215 "ru", kUSLayout); | 254 "ru", kUSLayout, |
| 255 "xkb:us::eng"); |
| 256 |
| 257 RunLocalizationTest("ru", "xkb:us::eng,xkb:ru::rus", |
| 258 "ru", kUSLayout, |
| 259 "xkb:us::eng"); |
216 | 260 |
217 // IMEs do not load at OOBE, so we just expect to see the (Latin) Japanese | 261 // IMEs do not load at OOBE, so we just expect to see the (Latin) Japanese |
218 // keyboard. | 262 // keyboard. |
219 RunLocalizationTest("ja", "xkb:jp::jpn", | 263 RunLocalizationTest("ja", "xkb:jp::jpn", |
220 "ja", "xkb:jp::jpn"); | 264 "ja", "xkb:jp::jpn", |
| 265 "xkb:jp::jpn,[xkb:us::eng]"); |
221 } | 266 } |
222 | 267 |
223 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenKeyboardLayout) { | 268 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenKeyboardLayout) { |
224 // We don't use the Icelandic locale but the Icelandic keyboard layout | 269 // We don't use the Icelandic locale but the Icelandic keyboard layout |
225 // should still be selected when specified as the default. | 270 // should still be selected when specified as the default. |
226 RunLocalizationTest("en-US", "xkb:is::ice", | 271 RunLocalizationTest("en-US", "xkb:is::ice", |
227 "en-US", "xkb:is::ice"); | 272 "en-US", "xkb:is::ice", |
| 273 "xkb:is::ice,[" |
| 274 "xkb:us::eng,xkb:us:intl:eng,xkb:us:altgr-intl:eng," |
| 275 "xkb:us:dvorak:eng,xkb:us:colemak:eng]"); |
228 } | 276 } |
229 | 277 |
230 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenFullLatin) { | 278 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenFullLatin) { |
231 // French Swiss keyboard. | 279 // French Swiss keyboard. |
232 RunLocalizationTest("fr", "xkb:ch:fr:fra", | 280 RunLocalizationTest("fr", "xkb:ch:fr:fra", |
233 "fr", "xkb:ch:fr:fra"); | 281 "fr", "xkb:ch:fr:fra", |
| 282 "xkb:ch:fr:fra,[" |
| 283 "xkb:fr::fra,xkb:be::fra,xkb:ca::fra," |
| 284 "xkb:ca:multix:fra,xkb:us::eng]"); |
234 | 285 |
235 // German Swiss keyboard. | 286 // German Swiss keyboard. |
236 RunLocalizationTest("de", "xkb:ch::ger", | 287 RunLocalizationTest("de", "xkb:ch::ger", |
237 "de", "xkb:ch::ger"); | 288 "de", "xkb:ch::ger", |
| 289 "xkb:ch::ger,[" |
| 290 "xkb:de::ger,xkb:de:neo:ger,xkb:be::ger,xkb:us::eng" |
| 291 "]"); |
238 } | 292 } |
239 | 293 |
240 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenMultipleLocales) { | 294 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenMultipleLocales) { |
241 RunLocalizationTest("es,en-US,nl", "xkb:be::nld", | 295 RunLocalizationTest("es,en-US,nl", "xkb:be::nld", |
242 "es,en-US,nl", "xkb:be::nld"); | 296 "es,en-US,nl", "xkb:be::nld", |
| 297 "xkb:be::nld,[xkb:es::spa,xkb:latam::spa,xkb:us::eng]"); |
243 | 298 |
244 RunLocalizationTest("ru,de", "xkb:ru::rus", | 299 RunLocalizationTest("ru,de", "xkb:ru::rus", |
245 "ru,de", kUSLayout); | 300 "ru,de", kUSLayout, |
| 301 "xkb:us::eng"); |
246 } | 302 } |
247 | 303 |
248 } // namespace chromeos | 304 } // namespace chromeos |
OLD | NEW |