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 virtual void StartLoadingMachineStatistics( | |
55 const scoped_refptr<base::TaskRunner>& file_task_runner, | |
56 bool load_oem_manifest) OVERRIDE { | |
57 } | |
58 | |
59 // Populates the named machine statistic for initial_locale and | |
60 // keyboard_layout only. | |
61 virtual bool GetMachineStatistic(const std::string& name, | |
62 std::string* result) OVERRIDE { | |
63 if (name == "initial_locale") | |
64 *result = initial_locale_; | |
65 else if (name == "keyboard_layout") | |
66 *result = keyboard_layout_; | |
67 else | |
68 return false; | |
69 | |
70 return true; | |
71 } | |
72 | |
73 virtual bool GetMachineFlag(const std::string& name, bool* result) OVERRIDE { | |
74 return false; | |
75 } | |
76 | |
77 virtual void Shutdown() OVERRIDE { | |
78 } | |
79 | |
80 std::string initial_locale_; | |
81 std::string keyboard_layout_; | |
82 }; | |
83 | |
84 } // namespace system | |
85 | |
86 class OobeLocalizationTest : public InProcessBrowserTest { | |
87 public: | |
88 OobeLocalizationTest(); | |
89 | |
90 // Returns true if the expression was successfully executed and returned true. | |
91 bool JsExecute(const std::string& expression); | |
92 | |
93 // Verifies that |value| is selected in |select_id|. | |
94 void VerifySelectedOption(const char* select_id, const char* value); | |
95 | |
96 // Verifies that |value| exists in |select_id|. | |
97 void VerifyOptionExists(const char* select_id, const char* value); | |
98 | |
99 protected: | |
100 // Runs the test for the given locale and keyboard layout. | |
101 void RunLocalizationTest(const std::string& initial_locale, | |
102 const std::string& keyboard_layout, | |
103 const std::string& expected_locale, | |
104 const std::string& expected_keyboard_layout); | |
105 | |
106 private: | |
107 scoped_ptr<system::FakeStatisticsProvider> statistics_provider_; | |
108 | |
109 DISALLOW_COPY_AND_ASSIGN(OobeLocalizationTest); | |
110 }; | |
111 | |
112 OobeLocalizationTest::OobeLocalizationTest() { | |
113 statistics_provider_.reset(new system::FakeStatisticsProvider()); | |
114 // Set the instance returned by GetInstance() for testing. | |
115 system::StatisticsProvider::SetTestProvider(statistics_provider_.get()); | |
116 } | |
117 | |
118 bool OobeLocalizationTest::JsExecute(const std::string& expression) { | |
119 bool result; | |
120 chromeos::LoginDisplayHostImpl* display_host = | |
121 static_cast<chromeos::LoginDisplayHostImpl*>( | |
122 chromeos::LoginDisplayHostImpl::default_host()); | |
123 return content::ExecuteScriptAndExtractBool( | |
124 display_host->GetOobeUI()->web_ui()->GetWebContents(), | |
125 "window.domAutomationController.send(!!(" + expression + "));", | |
126 &result) && result; | |
127 } | |
128 | |
129 void OobeLocalizationTest::VerifySelectedOption(const char* select_id, | |
130 const char* value) { | |
131 std::string expression = base::StringPrintf( | |
132 "(function () {\n" | |
133 " var select = document.querySelector('#%s');\n" | |
134 " if (!select)\n" | |
135 " return false;\n" | |
136 " return select.options[select.selectedIndex].value == '%s';\n" | |
137 "})()", select_id, value); | |
138 ASSERT_TRUE(JsExecute(expression)) << expression; | |
139 } | |
140 | |
141 void OobeLocalizationTest::VerifyOptionExists(const char* select_id, | |
142 const char* value) { | |
143 std::string expression = base::StringPrintf( | |
144 "(function () {\n" | |
145 " var select = document.querySelector('#%s');\n" | |
146 " if (!select)\n" | |
147 " return false;\n" | |
148 " for (var i = 0; i < select.options.length; i++) {\n" | |
149 " if (select.options[i].value == '%s')\n" | |
150 " return true;\n" | |
151 " }\n" | |
152 " return false;\n" | |
153 "})()", select_id, value); | |
154 ASSERT_TRUE(JsExecute(expression)) << expression; | |
155 } | |
156 | |
157 void OobeLocalizationTest::RunLocalizationTest( | |
158 const std::string& initial_locale, | |
159 const std::string& keyboard_layout, | |
160 const std::string& expected_locale, | |
161 const std::string& expected_keyboard_layout) { | |
162 statistics_provider_->set_locale(initial_locale); | |
163 statistics_provider_->set_keyboard_layout(keyboard_layout); | |
164 | |
165 // Initialize StartupCustomizationDocument with fake statistics provider | |
166 StartupCustomizationDocument::GetInstance()->Init( | |
167 statistics_provider_.get()); | |
168 | |
169 // Bring up the OOBE network screen | |
170 chromeos::ShowLoginWizard(chromeos::WizardController::kNetworkScreenName); | |
171 content::WindowedNotificationObserver( | |
172 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, | |
173 content::NotificationService::AllSources()).Wait(); | |
174 | |
175 VerifySelectedOption(kLocaleSelect, expected_locale.c_str()); | |
Alexander Alekseev
2014/02/11 14:17:24
Could you please verify, that list of locales from
michaelpg
2014/02/11 19:17:13
I've updated this function and added the NetworkSc
| |
176 VerifySelectedOption(kKeyboardSelect, expected_keyboard_layout.c_str()); | |
177 | |
178 // Make sure we have a fallback keyboard. | |
179 VerifyOptionExists(kKeyboardSelect, kUSLayout); | |
180 | |
181 // Shut down the display host. | |
182 chromeos::LoginDisplayHostImpl::default_host()->Finalize(); | |
183 base::MessageLoopForUI::current()->RunUntilIdle(); | |
184 | |
185 // Clear the locale pref so the statistics provider is pinged next time. | |
186 g_browser_process->local_state()->SetString(prefs::kApplicationLocale, | |
187 std::string()); | |
188 } | |
189 | |
190 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenNonLatin) { | |
191 // For a non-Latin keyboard layout like Russian, we expect to see the US | |
192 // keyboard. | |
193 RunLocalizationTest("ru", "xkb:ru::rus", | |
194 "ru", kUSLayout); | |
195 | |
196 // IMEs do not load at OOBE, so we just expect to see the (Latin) Japanese | |
197 // keyboard. | |
198 RunLocalizationTest("ja", "xkb:jp::jpn", | |
199 "ja", "xkb:jp::jpn"); | |
200 } | |
201 | |
202 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenKeyboardLayout) { | |
203 // We don't use the Icelandic locale but the Icelandic keyboard layout | |
204 // should still be selected when specified as the default. | |
205 RunLocalizationTest("en-US", "xkb:is::ice", | |
206 "en-US", "xkb:is::ice"); | |
207 } | |
208 | |
209 IN_PROC_BROWSER_TEST_F(OobeLocalizationTest, NetworkScreenFullLatin) { | |
210 // French Swiss keyboard. | |
211 RunLocalizationTest("fr", "xkb:ch:fr:fra", | |
212 "fr", "xkb:ch:fr:fra"); | |
213 | |
214 // German Swiss keyboard. | |
215 RunLocalizationTest("de", "xkb:ch::ger", | |
216 "de", "xkb:ch::ger"); | |
217 } | |
218 | |
219 } // namespace chromeos | |
OLD | NEW |