Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / chromeos / login / login_ui_keyboard_browsertest.cc
blob67bedea6129d087cb2648f7132666a91e84322cf
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.
5 #include "base/command_line.h"
6 #include "base/prefs/pref_service.h"
7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/chromeos/input_method/input_method_persistence.h"
9 #include "chrome/browser/chromeos/language_preferences.h"
10 #include "chrome/browser/chromeos/login/login_manager_test.h"
11 #include "chrome/browser/chromeos/login/startup_utils.h"
12 #include "chrome/browser/chromeos/login/test/oobe_screen_waiter.h"
13 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
14 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h"
15 #include "chrome/common/pref_names.h"
16 #include "chromeos/chromeos_switches.h"
17 #include "content/public/test/test_utils.h"
19 namespace chromeos {
21 namespace {
23 const char kTestUser1[] = "test-user1@gmail.com";
24 const char kTestUser2[] = "test-user2@gmail.com";
25 const char kTestUser3[] = "test-user3@gmail.com";
27 void Append_en_US_InputMethods(std::vector<std::string>* out) {
28 out->push_back("xkb:us::eng");
29 out->push_back("xkb:us:intl:eng");
30 out->push_back("xkb:us:altgr-intl:eng");
31 out->push_back("xkb:us:dvorak:eng");
32 out->push_back("xkb:us:dvp:eng");
33 out->push_back("xkb:us:colemak:eng");
34 chromeos::input_method::InputMethodManager::Get()->MigrateInputMethods(out);
37 class FocusPODWaiter {
38 public:
39 FocusPODWaiter() : focused_(false), runner_(new content::MessageLoopRunner) {
40 GetOobeUI()
41 ->signin_screen_handler_for_test()
42 ->SetFocusPODCallbackForTesting(
43 base::Bind(&FocusPODWaiter::OnFocusPOD, base::Unretained(this)));
45 ~FocusPODWaiter() {
46 GetOobeUI()
47 ->signin_screen_handler_for_test()
48 ->SetFocusPODCallbackForTesting(base::Closure());
51 void OnFocusPOD() {
52 focused_ = true;
53 if (runner_.get())
54 base::MessageLoopForUI::current()->PostTask(
55 FROM_HERE,
56 base::Bind(&FocusPODWaiter::ExitMessageLoop, base::Unretained(this)));
59 void ExitMessageLoop() { runner_->Quit(); }
61 void Wait() {
62 if (focused_)
63 return;
64 runner_->Run();
65 GetOobeUI()
66 ->signin_screen_handler_for_test()
67 ->SetFocusPODCallbackForTesting(base::Closure());
68 runner_ = NULL;
71 private:
72 OobeUI* GetOobeUI() {
73 OobeUI* oobe_ui =
74 static_cast<chromeos::LoginDisplayHostImpl*>(
75 chromeos::LoginDisplayHostImpl::default_host())->GetOobeUI();
76 CHECK(oobe_ui);
77 return oobe_ui;
80 bool focused_;
82 scoped_refptr<content::MessageLoopRunner> runner_;
85 } // anonymous namespace
87 class LoginUIKeyboardTest : public chromeos::LoginManagerTest {
88 public:
89 LoginUIKeyboardTest() : LoginManagerTest(false) {}
90 ~LoginUIKeyboardTest() override {}
92 void SetUpOnMainThread() override {
93 user_input_methods.push_back("xkb:fr::fra");
94 user_input_methods.push_back("xkb:de::ger");
96 chromeos::input_method::InputMethodManager::Get()->MigrateInputMethods(
97 &user_input_methods);
99 LoginManagerTest::SetUpOnMainThread();
102 // Should be called from PRE_ test so that local_state is saved to disk, and
103 // reloaded in the main test.
104 void InitUserLRUInputMethod() {
105 PrefService* local_state = g_browser_process->local_state();
107 input_method::SetUserLRUInputMethodPreferenceForTesting(
108 kTestUser1, user_input_methods[0], local_state);
109 input_method::SetUserLRUInputMethodPreferenceForTesting(
110 kTestUser2, user_input_methods[1], local_state);
113 protected:
114 std::vector<std::string> user_input_methods;
117 IN_PROC_BROWSER_TEST_F(LoginUIKeyboardTest, PRE_CheckPODScreenDefault) {
118 RegisterUser(kTestUser1);
119 RegisterUser(kTestUser2);
121 StartupUtils::MarkOobeCompleted();
124 // Check default IME initialization, when there is no IME configuration in
125 // local_state.
126 IN_PROC_BROWSER_TEST_F(LoginUIKeyboardTest, CheckPODScreenDefault) {
127 js_checker().ExpectEQ("$('pod-row').pods.length", 2);
129 std::vector<std::string> expected_input_methods;
130 Append_en_US_InputMethods(&expected_input_methods);
132 EXPECT_EQ(expected_input_methods,
133 input_method::InputMethodManager::Get()
134 ->GetActiveIMEState()
135 ->GetActiveInputMethodIds());
138 IN_PROC_BROWSER_TEST_F(LoginUIKeyboardTest, PRE_CheckPODScreenWithUsers) {
139 RegisterUser(kTestUser1);
140 RegisterUser(kTestUser2);
142 InitUserLRUInputMethod();
144 StartupUtils::MarkOobeCompleted();
147 IN_PROC_BROWSER_TEST_F(LoginUIKeyboardTest, CheckPODScreenWithUsers) {
148 js_checker().ExpectEQ("$('pod-row').pods.length", 2);
150 EXPECT_EQ(user_input_methods[0],
151 input_method::InputMethodManager::Get()
152 ->GetActiveIMEState()
153 ->GetCurrentInputMethod()
154 .id());
156 std::vector<std::string> expected_input_methods;
157 Append_en_US_InputMethods(&expected_input_methods);
158 // Active IM for the first user (active user POD).
159 expected_input_methods.push_back(user_input_methods[0]);
161 EXPECT_EQ(expected_input_methods,
162 input_method::InputMethodManager::Get()
163 ->GetActiveIMEState()
164 ->GetActiveInputMethodIds());
166 FocusPODWaiter waiter;
167 js_checker().Evaluate("$('pod-row').focusPod($('pod-row').pods[1])");
168 waiter.Wait();
170 EXPECT_EQ(user_input_methods[1],
171 input_method::InputMethodManager::Get()
172 ->GetActiveIMEState()
173 ->GetCurrentInputMethod()
174 .id());
177 class LoginUIKeyboardTestWithUsersAndOwner : public chromeos::LoginManagerTest {
178 public:
179 LoginUIKeyboardTestWithUsersAndOwner() : LoginManagerTest(false) {}
180 ~LoginUIKeyboardTestWithUsersAndOwner() override {}
182 void SetUpCommandLine(base::CommandLine* command_line) override {
183 LoginManagerTest::SetUpCommandLine(command_line);
184 command_line->AppendSwitch(switches::kStubCrosSettings);
186 LoginManagerTest::SetUpCommandLine(command_line);
189 void SetUpOnMainThread() override {
190 user_input_methods.push_back("xkb:fr::fra");
191 user_input_methods.push_back("xkb:de::ger");
192 user_input_methods.push_back("xkb:pl::pol");
194 chromeos::input_method::InputMethodManager::Get()->MigrateInputMethods(
195 &user_input_methods);
197 CrosSettings::Get()->SetString(kDeviceOwner, kTestUser3);
199 LoginManagerTest::SetUpOnMainThread();
202 // Should be called from PRE_ test so that local_state is saved to disk, and
203 // reloaded in the main test.
204 void InitUserLRUInputMethod() {
205 PrefService* local_state = g_browser_process->local_state();
207 input_method::SetUserLRUInputMethodPreferenceForTesting(
208 kTestUser1, user_input_methods[0], local_state);
209 input_method::SetUserLRUInputMethodPreferenceForTesting(
210 kTestUser2, user_input_methods[1], local_state);
211 input_method::SetUserLRUInputMethodPreferenceForTesting(
212 kTestUser3, user_input_methods[2], local_state);
214 local_state->SetString(language_prefs::kPreferredKeyboardLayout,
215 user_input_methods[2]);
218 void CheckGaiaKeyboard();
220 protected:
221 std::vector<std::string> user_input_methods;
224 void LoginUIKeyboardTestWithUsersAndOwner::CheckGaiaKeyboard() {
225 std::vector<std::string> expected_input_methods;
226 // kPreferredKeyboardLayout is now set to last focused POD.
227 expected_input_methods.push_back(user_input_methods[0]);
228 // Locale default input methods (the first one also is hardware IM).
229 Append_en_US_InputMethods(&expected_input_methods);
231 EXPECT_EQ(expected_input_methods,
232 input_method::InputMethodManager::Get()
233 ->GetActiveIMEState()
234 ->GetActiveInputMethodIds());
237 IN_PROC_BROWSER_TEST_F(LoginUIKeyboardTestWithUsersAndOwner,
238 PRE_CheckPODScreenKeyboard) {
239 RegisterUser(kTestUser1);
240 RegisterUser(kTestUser2);
241 RegisterUser(kTestUser3);
243 InitUserLRUInputMethod();
245 StartupUtils::MarkOobeCompleted();
248 IN_PROC_BROWSER_TEST_F(LoginUIKeyboardTestWithUsersAndOwner,
249 CheckPODScreenKeyboard) {
250 js_checker().ExpectEQ("$('pod-row').pods.length", 3);
252 std::vector<std::string> expected_input_methods;
253 // Owner input method.
254 expected_input_methods.push_back(user_input_methods[2]);
255 // Locale default input methods (the first one also is hardware IM).
256 Append_en_US_InputMethods(&expected_input_methods);
257 // Active IM for the first user (active user POD).
258 expected_input_methods.push_back(user_input_methods[0]);
260 EXPECT_EQ(expected_input_methods,
261 input_method::InputMethodManager::Get()
262 ->GetActiveIMEState()
263 ->GetActiveInputMethodIds());
265 // Switch to Gaia.
266 js_checker().Evaluate("$('add-user-button').click()");
267 OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN).Wait();
268 CheckGaiaKeyboard();
270 // Switch back.
271 js_checker().Evaluate("$('cancel-add-user-button').click()");
272 OobeScreenWaiter(OobeDisplay::SCREEN_ACCOUNT_PICKER).Wait();
274 EXPECT_EQ(expected_input_methods,
275 input_method::InputMethodManager::Get()
276 ->GetActiveIMEState()
277 ->GetActiveInputMethodIds());
279 } // namespace chromeos