Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / login_ui_keyboard_browsertest.cc
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/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"
18
19 namespace chromeos {
20
21 namespace {
22
23 const char kTestUser1[] = "test-user1@gmail.com";
24 const char kTestUser2[] = "test-user2@gmail.com";
25 const char kTestUser3[] = "test-user3@gmail.com";
26
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:colemak:eng");
33   chromeos::input_method::InputMethodManager::Get()->MigrateInputMethods(out);
34 }
35
36 class FocusPODWaiter {
37  public:
38   FocusPODWaiter() : focused_(false), runner_(new content::MessageLoopRunner) {
39     GetOobeUI()
40         ->signin_screen_handler_for_test()
41         ->SetFocusPODCallbackForTesting(
42             base::Bind(&FocusPODWaiter::OnFocusPOD, base::Unretained(this)));
43   }
44   ~FocusPODWaiter() {
45     GetOobeUI()
46         ->signin_screen_handler_for_test()
47         ->SetFocusPODCallbackForTesting(base::Closure());
48   }
49
50   void OnFocusPOD() {
51     focused_ = true;
52     if (runner_.get())
53       base::MessageLoopForUI::current()->PostTask(
54           FROM_HERE,
55           base::Bind(&FocusPODWaiter::ExitMessageLoop, base::Unretained(this)));
56   }
57
58   void ExitMessageLoop() { runner_->Quit(); }
59
60   void Wait() {
61     if (focused_)
62       return;
63     runner_->Run();
64     GetOobeUI()
65         ->signin_screen_handler_for_test()
66         ->SetFocusPODCallbackForTesting(base::Closure());
67     runner_ = NULL;
68   }
69
70  private:
71   OobeUI* GetOobeUI() {
72     OobeUI* oobe_ui =
73         static_cast<chromeos::LoginDisplayHostImpl*>(
74             chromeos::LoginDisplayHostImpl::default_host())->GetOobeUI();
75     CHECK(oobe_ui);
76     return oobe_ui;
77   }
78
79   bool focused_;
80
81   scoped_refptr<content::MessageLoopRunner> runner_;
82 };
83
84 }  // anonymous namespace
85
86 class LoginUIKeyboardTest : public chromeos::LoginManagerTest {
87  public:
88   LoginUIKeyboardTest() : LoginManagerTest(false) {}
89   virtual ~LoginUIKeyboardTest() {}
90
91   virtual void SetUpOnMainThread() override {
92     user_input_methods.push_back("xkb:fr::fra");
93     user_input_methods.push_back("xkb:de::ger");
94
95     chromeos::input_method::InputMethodManager::Get()->MigrateInputMethods(
96         &user_input_methods);
97
98     LoginManagerTest::SetUpOnMainThread();
99   }
100
101   // Should be called from PRE_ test so that local_state is saved to disk, and
102   // reloaded in the main test.
103   void InitUserLRUInputMethod() {
104     PrefService* local_state = g_browser_process->local_state();
105
106     input_method::SetUserLRUInputMethodPreferenceForTesting(
107         kTestUser1, user_input_methods[0], local_state);
108     input_method::SetUserLRUInputMethodPreferenceForTesting(
109         kTestUser2, user_input_methods[1], local_state);
110   }
111
112  protected:
113   std::vector<std::string> user_input_methods;
114 };
115
116 IN_PROC_BROWSER_TEST_F(LoginUIKeyboardTest, PRE_CheckPODScreenDefault) {
117   RegisterUser(kTestUser1);
118   RegisterUser(kTestUser2);
119
120   StartupUtils::MarkOobeCompleted();
121 }
122
123 // Check default IME initialization, when there is no IME configuration in
124 // local_state.
125 IN_PROC_BROWSER_TEST_F(LoginUIKeyboardTest, CheckPODScreenDefault) {
126   js_checker().ExpectEQ("$('pod-row').pods.length", 2);
127
128   std::vector<std::string> expected_input_methods;
129   Append_en_US_InputMethods(&expected_input_methods);
130
131   EXPECT_EQ(expected_input_methods,
132             input_method::InputMethodManager::Get()
133                 ->GetActiveIMEState()
134                 ->GetActiveInputMethodIds());
135 }
136
137 IN_PROC_BROWSER_TEST_F(LoginUIKeyboardTest, PRE_CheckPODScreenWithUsers) {
138   RegisterUser(kTestUser1);
139   RegisterUser(kTestUser2);
140
141   InitUserLRUInputMethod();
142
143   StartupUtils::MarkOobeCompleted();
144 }
145
146 // Hangs flakily. See http://crbug.com/421450.
147 IN_PROC_BROWSER_TEST_F(LoginUIKeyboardTest, DISABLED_CheckPODScreenWithUsers) {
148   js_checker().ExpectEQ("$('pod-row').pods.length", 2);
149
150   EXPECT_EQ(user_input_methods[0],
151             input_method::InputMethodManager::Get()
152                 ->GetActiveIMEState()
153                 ->GetCurrentInputMethod()
154                 .id());
155
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]);
160
161   EXPECT_EQ(expected_input_methods,
162             input_method::InputMethodManager::Get()
163                 ->GetActiveIMEState()
164                 ->GetActiveInputMethodIds());
165
166   FocusPODWaiter waiter;
167   js_checker().Evaluate("$('pod-row').focusPod($('pod-row').pods[1])");
168   waiter.Wait();
169
170   EXPECT_EQ(user_input_methods[1],
171             input_method::InputMethodManager::Get()
172                 ->GetActiveIMEState()
173                 ->GetCurrentInputMethod()
174                 .id());
175 }
176
177 class LoginUIKeyboardTestWithUsersAndOwner : public chromeos::LoginManagerTest {
178  public:
179   LoginUIKeyboardTestWithUsersAndOwner() : LoginManagerTest(false) {}
180   virtual ~LoginUIKeyboardTestWithUsersAndOwner() {}
181
182   virtual void SetUpCommandLine(CommandLine* command_line) override {
183     LoginManagerTest::SetUpCommandLine(command_line);
184     command_line->AppendSwitch(switches::kStubCrosSettings);
185
186     LoginManagerTest::SetUpCommandLine(command_line);
187   }
188
189   virtual 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");
193
194     chromeos::input_method::InputMethodManager::Get()->MigrateInputMethods(
195         &user_input_methods);
196
197     CrosSettings::Get()->SetString(kDeviceOwner, kTestUser3);
198
199     LoginManagerTest::SetUpOnMainThread();
200   }
201
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();
206
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);
213
214     local_state->SetString(language_prefs::kPreferredKeyboardLayout,
215                            user_input_methods[2]);
216   }
217
218   void CheckGaiaKeyboard();
219
220  protected:
221   std::vector<std::string> user_input_methods;
222 };
223
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);
230
231   EXPECT_EQ(expected_input_methods,
232             input_method::InputMethodManager::Get()
233                 ->GetActiveIMEState()
234                 ->GetActiveInputMethodIds());
235 }
236
237 IN_PROC_BROWSER_TEST_F(LoginUIKeyboardTestWithUsersAndOwner,
238                        PRE_CheckPODScreenKeyboard) {
239   RegisterUser(kTestUser1);
240   RegisterUser(kTestUser2);
241   RegisterUser(kTestUser3);
242
243   InitUserLRUInputMethod();
244
245   StartupUtils::MarkOobeCompleted();
246 }
247
248 IN_PROC_BROWSER_TEST_F(LoginUIKeyboardTestWithUsersAndOwner,
249                        CheckPODScreenKeyboard) {
250   js_checker().ExpectEQ("$('pod-row').pods.length", 3);
251
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]);
259
260   EXPECT_EQ(expected_input_methods,
261             input_method::InputMethodManager::Get()
262                 ->GetActiveIMEState()
263                 ->GetActiveInputMethodIds());
264
265   // Switch to Gaia.
266   js_checker().Evaluate("$('add-user-button').click()");
267   OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN).Wait();
268   CheckGaiaKeyboard();
269
270   // Switch back.
271   js_checker().Evaluate("$('cancel-add-user-button').click()");
272   OobeScreenWaiter(OobeDisplay::SCREEN_ACCOUNT_PICKER).Wait();
273
274   EXPECT_EQ(expected_input_methods,
275             input_method::InputMethodManager::Get()
276                 ->GetActiveIMEState()
277                 ->GetActiveInputMethodIds());
278 }
279 }  // namespace chromeos