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"
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
{
39 FocusPODWaiter() : focused_(false), runner_(new content::MessageLoopRunner
) {
41 ->signin_screen_handler_for_test()
42 ->SetFocusPODCallbackForTesting(
43 base::Bind(&FocusPODWaiter::OnFocusPOD
, base::Unretained(this)));
47 ->signin_screen_handler_for_test()
48 ->SetFocusPODCallbackForTesting(base::Closure());
54 base::MessageLoopForUI::current()->PostTask(
56 base::Bind(&FocusPODWaiter::ExitMessageLoop
, base::Unretained(this)));
59 void ExitMessageLoop() { runner_
->Quit(); }
66 ->signin_screen_handler_for_test()
67 ->SetFocusPODCallbackForTesting(base::Closure());
74 static_cast<chromeos::LoginDisplayHostImpl
*>(
75 chromeos::LoginDisplayHostImpl::default_host())->GetOobeUI();
82 scoped_refptr
<content::MessageLoopRunner
> runner_
;
85 } // anonymous namespace
87 class LoginUIKeyboardTest
: public chromeos::LoginManagerTest
{
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(
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
);
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
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()
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])");
170 EXPECT_EQ(user_input_methods
[1],
171 input_method::InputMethodManager::Get()
172 ->GetActiveIMEState()
173 ->GetCurrentInputMethod()
177 class LoginUIKeyboardTestWithUsersAndOwner
: public chromeos::LoginManagerTest
{
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();
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());
266 js_checker().Evaluate("$('add-user-button').click()");
267 OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN
).Wait();
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