OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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/memory/scoped_ptr.h" | |
7 #include "base/message_loop.h" | |
8 #include "chrome/browser/chromeos/cros/cros_in_process_browser_test.h" | |
9 #include "chrome/browser/chromeos/dbus/mock_dbus_thread_manager.h" | |
10 #include "chrome/browser/chromeos/dbus/mock_power_manager_client.h" | |
11 #include "chrome/browser/chromeos/login/mock_authenticator.h" | |
12 #include "chrome/browser/chromeos/login/screen_locker.h" | |
13 #include "chrome/browser/chromeos/login/screen_locker_tester.h" | |
14 #include "chrome/browser/chromeos/login/user_manager.h" | |
15 #include "chrome/browser/profiles/profile_manager.h" | |
16 #include "chrome/browser/ui/browser.h" | |
17 #include "chrome/browser/ui/browser_window.h" | |
18 #include "chrome/browser/ui/views/browser_dialogs.h" | |
19 #include "chrome/common/chrome_notification_types.h" | |
20 #include "chrome/common/chrome_switches.h" | |
21 #include "chrome/test/base/ui_test_utils.h" | |
22 #include "content/public/browser/notification_service.h" | |
23 #include "testing/gmock/include/gmock/gmock.h" | |
24 #include "testing/gtest/include/gtest/gtest.h" | |
25 #include "ui/ui_controls/ui_controls.h" | |
26 #include "ui/views/widget/widget.h" | |
27 | |
28 #if defined(TOOLKIT_USES_GTK) | |
29 #include "ui/views/widget/native_widget_gtk.h" | |
30 #endif | |
31 | |
32 namespace { | |
33 | |
34 // An object that wait for lock state and fullscreen state. | |
35 class Waiter : public content::NotificationObserver { | |
36 public: | |
37 explicit Waiter(Browser* browser) | |
38 : browser_(browser), | |
39 running_(false) { | |
40 registrar_.Add(this, | |
41 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, | |
42 content::NotificationService::AllSources()); | |
43 #if defined(TOOLKIT_USES_GTK) | |
44 handler_id_ = g_signal_connect( | |
45 G_OBJECT(browser_->window()->GetNativeHandle()), | |
46 "window-state-event", | |
47 G_CALLBACK(OnWindowStateEventThunk), | |
48 this); | |
49 #else | |
50 registrar_.Add(this, | |
51 chrome::NOTIFICATION_FULLSCREEN_CHANGED, | |
52 content::NotificationService::AllSources()); | |
53 #endif | |
54 } | |
55 | |
56 virtual ~Waiter() { | |
57 #if defined(TOOLKIT_USES_GTK) | |
58 g_signal_handler_disconnect( | |
59 G_OBJECT(browser_->window()->GetNativeHandle()), | |
60 handler_id_); | |
61 #endif | |
62 } | |
63 | |
64 virtual void Observe(int type, | |
65 const content::NotificationSource& source, | |
66 const content::NotificationDetails& details) { | |
67 DCHECK(type == chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED || | |
68 type == chrome::NOTIFICATION_FULLSCREEN_CHANGED); | |
69 if (running_) | |
70 MessageLoop::current()->Quit(); | |
71 } | |
72 | |
73 // Wait until the two conditions are met. | |
74 void Wait(bool locker_state, bool fullscreen) { | |
75 running_ = true; | |
76 scoped_ptr<chromeos::test::ScreenLockerTester> | |
77 tester(chromeos::ScreenLocker::GetTester()); | |
78 while (tester->IsLocked() != locker_state || | |
79 browser_->window()->IsFullscreen() != fullscreen) { | |
80 ui_test_utils::RunMessageLoop(); | |
81 } | |
82 // Make sure all pending tasks are executed. | |
83 ui_test_utils::RunAllPendingInMessageLoop(); | |
84 running_ = false; | |
85 } | |
86 | |
87 #if defined(TOOLKIT_USES_GTK) | |
88 CHROMEGTK_CALLBACK_1(Waiter, gboolean, OnWindowStateEvent, | |
89 GdkEventWindowState*); | |
90 #endif | |
91 | |
92 private: | |
93 Browser* browser_; | |
94 #if defined(TOOLKIT_USES_GTK) | |
95 gulong handler_id_; | |
96 #endif | |
97 content::NotificationRegistrar registrar_; | |
98 | |
99 // Are we currently running the message loop? | |
100 bool running_; | |
101 | |
102 DISALLOW_COPY_AND_ASSIGN(Waiter); | |
103 }; | |
104 | |
105 #if defined(TOOLKIT_USES_GTK) | |
106 gboolean Waiter::OnWindowStateEvent(GtkWidget* widget, | |
107 GdkEventWindowState* event) { | |
108 MessageLoop::current()->Quit(); | |
109 return false; | |
110 } | |
111 #endif | |
112 | |
113 } // namespace | |
114 | |
115 namespace chromeos { | |
116 | |
117 class ScreenLockerTest : public CrosInProcessBrowserTest { | |
118 public: | |
119 ScreenLockerTest() : mock_power_manager_client_(NULL) { | |
120 } | |
121 | |
122 protected: | |
123 MockPowerManagerClient* mock_power_manager_client_; | |
124 | |
125 // Test the no password mode with different unlock scheme given by | |
126 // |unlock| function. | |
127 void TestNoPassword(void (unlock)(views::Widget*)) { | |
128 EXPECT_CALL(*mock_power_manager_client_, NotifyScreenUnlockRequested()) | |
129 .Times(1) | |
130 .RetiresOnSaturation(); | |
131 EXPECT_CALL(*mock_power_manager_client_, NotifyScreenLockCompleted()) | |
132 .Times(1) | |
133 .RetiresOnSaturation(); | |
134 UserManager::Get()->GuestUserLoggedIn(); | |
135 ScreenLocker::Show(); | |
136 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester()); | |
137 tester->EmulateWindowManagerReady(); | |
138 ui_test_utils::WindowedNotificationObserver lock_state_observer( | |
139 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, | |
140 content::NotificationService::AllSources()); | |
141 if (!chromeos::ScreenLocker::GetTester()->IsLocked()) | |
142 lock_state_observer.Wait(); | |
143 EXPECT_TRUE(tester->IsLocked()); | |
144 tester->InjectMockAuthenticator("", ""); | |
145 | |
146 unlock(tester->GetWidget()); | |
147 | |
148 ui_test_utils::RunAllPendingInMessageLoop(); | |
149 EXPECT_TRUE(tester->IsLocked()); | |
150 | |
151 // Emulate LockScreen request from PowerManager (via SessionManager). | |
152 ScreenLocker::Hide(); | |
153 ui_test_utils::RunAllPendingInMessageLoop(); | |
154 EXPECT_FALSE(tester->IsLocked()); | |
155 } | |
156 | |
157 void LockScreenWithUser(test::ScreenLockerTester* tester, | |
158 const std::string& user) { | |
159 UserManager::Get()->UserLoggedIn(user); | |
160 ScreenLocker::Show(); | |
161 tester->EmulateWindowManagerReady(); | |
162 ui_test_utils::WindowedNotificationObserver lock_state_observer( | |
163 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, | |
164 content::NotificationService::AllSources()); | |
165 if (!tester->IsLocked()) | |
166 lock_state_observer.Wait(); | |
167 EXPECT_TRUE(tester->IsLocked()); | |
168 } | |
169 | |
170 private: | |
171 virtual void SetUpInProcessBrowserTestFixture() { | |
172 MockDBusThreadManager* mock_dbus_thread_manager = | |
173 new MockDBusThreadManager; | |
174 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager); | |
175 mock_power_manager_client_ = static_cast<MockPowerManagerClient*>( | |
176 DBusThreadManager::Get()->GetPowerManagerClient()); | |
177 cros_mock_->InitStatusAreaMocks(); | |
178 EXPECT_CALL(*mock_power_manager_client_, AddObserver(testing::_)) | |
179 .Times(1) | |
180 .RetiresOnSaturation(); | |
181 EXPECT_CALL(*mock_power_manager_client_, NotifyScreenUnlockCompleted()) | |
182 .Times(1) | |
183 .RetiresOnSaturation(); | |
184 // Expectations for the status are on the screen lock window. | |
185 cros_mock_->SetStatusAreaMocksExpectations(); | |
186 // Expectations for the status area on the browser window. | |
187 cros_mock_->SetStatusAreaMocksExpectations(); | |
188 } | |
189 | |
190 virtual void SetUpCommandLine(CommandLine* command_line) { | |
191 command_line->AppendSwitchASCII(switches::kLoginProfile, "user"); | |
192 command_line->AppendSwitch(switches::kNoFirstRun); | |
193 } | |
194 | |
195 DISALLOW_COPY_AND_ASSIGN(ScreenLockerTest); | |
196 }; | |
197 | |
198 // Temporarily disabling all screen locker tests while investigating the | |
199 // issue crbug.com/78764. | |
200 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestBasic) { | |
201 EXPECT_CALL(*mock_power_manager_client_, NotifyScreenUnlockRequested()) | |
202 .Times(1) | |
203 .RetiresOnSaturation(); | |
204 EXPECT_CALL(*mock_power_manager_client_, NotifyScreenLockCompleted()) | |
205 .Times(1) | |
206 .RetiresOnSaturation(); | |
207 UserManager::Get()->UserLoggedIn("user"); | |
208 ScreenLocker::Show(); | |
209 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester()); | |
210 tester->EmulateWindowManagerReady(); | |
211 ui_test_utils::WindowedNotificationObserver lock_state_observer( | |
212 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, | |
213 content::NotificationService::AllSources()); | |
214 if (!chromeos::ScreenLocker::GetTester()->IsLocked()) | |
215 lock_state_observer.Wait(); | |
216 | |
217 // Test to make sure that the widget is actually appearing and is of | |
218 // reasonable size, preventing a regression of | |
219 // http://code.google.com/p/chromium-os/issues/detail?id=5987 | |
220 gfx::Rect lock_bounds = tester->GetChildWidget()->GetWindowScreenBounds(); | |
221 EXPECT_GT(lock_bounds.width(), 10); | |
222 EXPECT_GT(lock_bounds.height(), 10); | |
223 | |
224 tester->InjectMockAuthenticator("user", "pass"); | |
225 EXPECT_TRUE(tester->IsLocked()); | |
226 tester->EnterPassword("fail"); | |
227 ui_test_utils::RunAllPendingInMessageLoop(); | |
228 EXPECT_TRUE(tester->IsLocked()); | |
229 tester->EnterPassword("pass"); | |
230 ui_test_utils::RunAllPendingInMessageLoop(); | |
231 // Successful authentication simply send a unlock request to PowerManager. | |
232 EXPECT_TRUE(tester->IsLocked()); | |
233 | |
234 // Emulate LockScreen request from PowerManager (via SessionManager). | |
235 // TODO(oshima): Find out better way to handle this in mock. | |
236 ScreenLocker::Hide(); | |
237 ui_test_utils::RunAllPendingInMessageLoop(); | |
238 EXPECT_FALSE(tester->IsLocked()); | |
239 } | |
240 | |
241 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestFullscreenExit) { | |
242 EXPECT_CALL(*mock_power_manager_client_, NotifyScreenUnlockRequested()) | |
243 .Times(1) | |
244 .RetiresOnSaturation(); | |
245 EXPECT_CALL(*mock_power_manager_client_, NotifyScreenLockCompleted()) | |
246 .Times(1) | |
247 .RetiresOnSaturation(); | |
248 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester()); | |
249 { | |
250 Waiter waiter(browser()); | |
251 browser()->ToggleFullscreenMode(); | |
252 waiter.Wait(false /* not locked */, true /* full screen */); | |
253 EXPECT_TRUE(browser()->window()->IsFullscreen()); | |
254 EXPECT_FALSE(tester->IsLocked()); | |
255 } | |
256 { | |
257 Waiter waiter(browser()); | |
258 UserManager::Get()->UserLoggedIn("user"); | |
259 ScreenLocker::Show(); | |
260 tester->EmulateWindowManagerReady(); | |
261 waiter.Wait(true /* locked */, false /* full screen */); | |
262 EXPECT_FALSE(browser()->window()->IsFullscreen()); | |
263 EXPECT_TRUE(tester->IsLocked()); | |
264 } | |
265 tester->InjectMockAuthenticator("user", "pass"); | |
266 tester->EnterPassword("pass"); | |
267 ui_test_utils::RunAllPendingInMessageLoop(); | |
268 ScreenLocker::Hide(); | |
269 ui_test_utils::RunAllPendingInMessageLoop(); | |
270 EXPECT_FALSE(tester->IsLocked()); | |
271 } | |
272 | |
273 void MouseMove(views::Widget* widget) { | |
274 ui_controls::SendMouseMove(10, 10); | |
275 } | |
276 | |
277 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, | |
278 DISABLED_TestNoPasswordWithMouseMove) { | |
279 TestNoPassword(MouseMove); | |
280 } | |
281 | |
282 void MouseClick(views::Widget* widget) { | |
283 ui_controls::SendMouseClick(ui_controls::RIGHT); | |
284 } | |
285 | |
286 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, | |
287 DISABLED_TestNoPasswordWithMouseClick) { | |
288 TestNoPassword(MouseClick); | |
289 } | |
290 | |
291 void SimulateKeyPress(views::Widget* widget, ui::KeyboardCode key_code) { | |
292 ui_controls::SendKeyPress(widget->GetNativeWindow(), | |
293 key_code, false, false, false, false); | |
294 } | |
295 | |
296 void UnlockKeyPress(views::Widget* widget) { | |
297 SimulateKeyPress(widget, ui::VKEY_SPACE); | |
298 } | |
299 | |
300 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, DISABLED_TestNoPasswordWithKeyPress) { | |
301 TestNoPassword(UnlockKeyPress); | |
302 } | |
303 | |
304 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestShowTwice) { | |
305 EXPECT_CALL(*mock_power_manager_client_, NotifyScreenLockCompleted()) | |
306 .Times(2) | |
307 .RetiresOnSaturation(); | |
308 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester()); | |
309 LockScreenWithUser(tester.get(), "user"); | |
310 | |
311 // Ensure there's a profile or this test crashes. | |
312 ProfileManager::GetDefaultProfile(); | |
313 | |
314 // Calling Show again simply send LockCompleted signal. | |
315 ScreenLocker::Show(); | |
316 EXPECT_TRUE(tester->IsLocked()); | |
317 | |
318 // Close the locker to match expectations. | |
319 ScreenLocker::Hide(); | |
320 ui_test_utils::RunAllPendingInMessageLoop(); | |
321 EXPECT_FALSE(tester->IsLocked()); | |
322 } | |
323 | |
324 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, DISABLED_TestEscape) { | |
325 EXPECT_CALL(*mock_power_manager_client_, NotifyScreenLockCompleted()) | |
326 .Times(1) | |
327 .RetiresOnSaturation(); | |
328 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester()); | |
329 LockScreenWithUser(tester.get(), "user"); | |
330 | |
331 // Ensure there's a profile or this test crashes. | |
332 ProfileManager::GetDefaultProfile(); | |
333 | |
334 tester->SetPassword("password"); | |
335 EXPECT_EQ("password", tester->GetPassword()); | |
336 // Escape clears the password. | |
337 SimulateKeyPress(tester->GetWidget(), ui::VKEY_ESCAPE); | |
338 ui_test_utils::RunAllPendingInMessageLoop(); | |
339 EXPECT_EQ("", tester->GetPassword()); | |
340 | |
341 // Close the locker to match expectations. | |
342 ScreenLocker::Hide(); | |
343 ui_test_utils::RunAllPendingInMessageLoop(); | |
344 EXPECT_FALSE(tester->IsLocked()); | |
345 } | |
346 | |
347 } // namespace chromeos | |
OLD | NEW |