Upstream version 10.39.225.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 IN_PROC_BROWSER_TEST_F(LoginUIKeyboardTest, CheckPODScreenWithUsers) {
147   js_checker().ExpectEQ("$('pod-row').pods.length", 2);
148
149   EXPECT_EQ(user_input_methods[0],
150             input_method::InputMethodManager::Get()
151                 ->GetActiveIMEState()
152                 ->GetCurrentInputMethod()
153                 .id());
154
155   std::vector<std::string> expected_input_methods;
156   Append_en_US_InputMethods(&expected_input_methods);
157   // Active IM for the first user (active user POD).
158   expected_input_methods.push_back(user_input_methods[0]);
159
160   EXPECT_EQ(expected_input_methods,
161             input_method::InputMethodManager::Get()
162                 ->GetActiveIMEState()
163                 ->GetActiveInputMethodIds());
164
165   FocusPODWaiter waiter;
166   js_checker().Evaluate("$('pod-row').focusPod($('pod-row').pods[1])");
167   waiter.Wait();
168
169   EXPECT_EQ(user_input_methods[1],
170             input_method::InputMethodManager::Get()
171                 ->GetActiveIMEState()
172                 ->GetCurrentInputMethod()
173                 .id());
174 }
175
176 class LoginUIKeyboardTestWithUsersAndOwner : public chromeos::LoginManagerTest {
177  public:
178   LoginUIKeyboardTestWithUsersAndOwner() : LoginManagerTest(false) {}
179   virtual ~LoginUIKeyboardTestWithUsersAndOwner() {}
180
181   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
182     LoginManagerTest::SetUpCommandLine(command_line);
183     command_line->AppendSwitch(switches::kStubCrosSettings);
184
185     LoginManagerTest::SetUpCommandLine(command_line);
186   }
187
188   virtual void SetUpOnMainThread() OVERRIDE {
189     user_input_methods.push_back("xkb:fr::fra");
190     user_input_methods.push_back("xkb:de::ger");
191     user_input_methods.push_back("xkb:pl::pol");
192
193     chromeos::input_method::InputMethodManager::Get()->MigrateInputMethods(
194         &user_input_methods);
195
196     CrosSettings::Get()->SetString(kDeviceOwner, kTestUser3);
197
198     LoginManagerTest::SetUpOnMainThread();
199   }
200
201   // Should be called from PRE_ test so that local_state is saved to disk, and
202   // reloaded in the main test.
203   void InitUserLRUInputMethod() {
204     PrefService* local_state = g_browser_process->local_state();
205
206     input_method::SetUserLRUInputMethodPreferenceForTesting(
207         kTestUser1, user_input_methods[0], local_state);
208     input_method::SetUserLRUInputMethodPreferenceForTesting(
209         kTestUser2, user_input_methods[1], local_state);
210     input_method::SetUserLRUInputMethodPreferenceForTesting(
211         kTestUser3, user_input_methods[2], local_state);
212
213     local_state->SetString(language_prefs::kPreferredKeyboardLayout,
214                            user_input_methods[2]);
215   }
216
217   void CheckGaiaKeyboard();
218
219  protected:
220   std::vector<std::string> user_input_methods;
221 };
222
223 void LoginUIKeyboardTestWithUsersAndOwner::CheckGaiaKeyboard() {
224   std::vector<std::string> expected_input_methods;
225   // kPreferredKeyboardLayout is now set to last focused POD.
226   expected_input_methods.push_back(user_input_methods[0]);
227   // Locale default input methods (the first one also is hardware IM).
228   Append_en_US_InputMethods(&expected_input_methods);
229
230   EXPECT_EQ(expected_input_methods,
231             input_method::InputMethodManager::Get()
232                 ->GetActiveIMEState()
233                 ->GetActiveInputMethodIds());
234 }
235
236 IN_PROC_BROWSER_TEST_F(LoginUIKeyboardTestWithUsersAndOwner,
237                        PRE_CheckPODScreenKeyboard) {
238   RegisterUser(kTestUser1);
239   RegisterUser(kTestUser2);
240   RegisterUser(kTestUser3);
241
242   InitUserLRUInputMethod();
243
244   StartupUtils::MarkOobeCompleted();
245 }
246
247 IN_PROC_BROWSER_TEST_F(LoginUIKeyboardTestWithUsersAndOwner,
248                        CheckPODScreenKeyboard) {
249   js_checker().ExpectEQ("$('pod-row').pods.length", 3);
250
251   std::vector<std::string> expected_input_methods;
252   // Owner input method.
253   expected_input_methods.push_back(user_input_methods[2]);
254   // Locale default input methods (the first one also is hardware IM).
255   Append_en_US_InputMethods(&expected_input_methods);
256   // Active IM for the first user (active user POD).
257   expected_input_methods.push_back(user_input_methods[0]);
258
259   EXPECT_EQ(expected_input_methods,
260             input_method::InputMethodManager::Get()
261                 ->GetActiveIMEState()
262                 ->GetActiveInputMethodIds());
263
264   // Switch to Gaia.
265   js_checker().Evaluate("$('add-user-button').click()");
266   OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN).Wait();
267   CheckGaiaKeyboard();
268
269   // Switch back.
270   js_checker().Evaluate("$('cancel-add-user-button').click()");
271   OobeScreenWaiter(OobeDisplay::SCREEN_ACCOUNT_PICKER).Wait();
272
273   EXPECT_EQ(expected_input_methods,
274             input_method::InputMethodManager::Get()
275                 ->GetActiveIMEState()
276                 ->GetActiveInputMethodIds());
277 }
278 }  // namespace chromeos