OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "base/scoped_ptr.h" | 6 #include "base/scoped_ptr.h" |
7 #include "chrome/browser/automation/ui_controls.h" | 7 #include "chrome/browser/automation/ui_controls.h" |
| 8 #include "chrome/browser/browser.h" |
| 9 #include "chrome/browser/browser_window.h" |
8 #include "chrome/browser/chrome_thread.h" | 10 #include "chrome/browser/chrome_thread.h" |
9 #include "chrome/browser/chromeos/cros/cros_in_process_browser_test.h" | 11 #include "chrome/browser/chromeos/cros/cros_in_process_browser_test.h" |
10 #include "chrome/browser/chromeos/cros/mock_screen_lock_library.h" | 12 #include "chrome/browser/chromeos/cros/mock_screen_lock_library.h" |
11 #include "chrome/browser/chromeos/cros/mock_input_method_library.h" | 13 #include "chrome/browser/chromeos/cros/mock_input_method_library.h" |
12 #include "chrome/browser/chromeos/login/screen_locker.h" | 14 #include "chrome/browser/chromeos/login/screen_locker.h" |
13 #include "chrome/browser/chromeos/login/screen_locker_tester.h" | 15 #include "chrome/browser/chromeos/login/screen_locker_tester.h" |
14 #include "chrome/browser/chromeos/login/mock_authenticator.h" | 16 #include "chrome/browser/chromeos/login/mock_authenticator.h" |
15 #include "chrome/browser/chromeos/login/user_manager.h" | 17 #include "chrome/browser/chromeos/login/user_manager.h" |
16 #include "chrome/browser/views/browser_dialogs.h" | 18 #include "chrome/browser/views/browser_dialogs.h" |
17 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/common/notification_service.h" |
18 #include "chrome/common/notification_type.h" | 21 #include "chrome/common/notification_type.h" |
19 #include "chrome/test/ui_test_utils.h" | 22 #include "chrome/test/ui_test_utils.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "views/controls/textfield/textfield.h" | 25 #include "views/controls/textfield/textfield.h" |
| 26 #include "views/window/window_gtk.h" |
| 27 |
| 28 namespace { |
| 29 |
| 30 // An object that wait for lock state and fullscreen state. |
| 31 class Waiter : public NotificationObserver { |
| 32 public: |
| 33 Waiter(Browser* browser) |
| 34 : browser_(browser) { |
| 35 registrar_.Add(this, |
| 36 NotificationType::SCREEN_LOCK_STATE_CHANGED, |
| 37 NotificationService::AllSources()); |
| 38 handler_id_ = g_signal_connect( |
| 39 G_OBJECT(browser_->window()->GetNativeHandle()), |
| 40 "window-state-event", |
| 41 G_CALLBACK(OnWindowStateEventThunk), |
| 42 this); |
| 43 } |
| 44 |
| 45 ~Waiter() { |
| 46 g_signal_handler_disconnect( |
| 47 G_OBJECT(browser_->window()->GetNativeHandle()), |
| 48 handler_id_); |
| 49 } |
| 50 |
| 51 virtual void Observe(NotificationType type, |
| 52 const NotificationSource& source, |
| 53 const NotificationDetails& details) { |
| 54 DCHECK(type == NotificationType::SCREEN_LOCK_STATE_CHANGED); |
| 55 MessageLoop::current()->Quit(); |
| 56 } |
| 57 |
| 58 // Wait until the two conditions are met. |
| 59 void Wait(bool locker_state, bool fullscreen) { |
| 60 scoped_ptr<chromeos::test::ScreenLockerTester> |
| 61 tester(chromeos::ScreenLocker::GetTester()); |
| 62 while (tester->IsLocked() != locker_state || |
| 63 browser_->window()->IsFullscreen() != fullscreen) { |
| 64 ui_test_utils::RunMessageLoop(); |
| 65 } |
| 66 // Make sure all pending tasks are executed. |
| 67 ui_test_utils::RunAllPendingInMessageLoop(); |
| 68 } |
| 69 |
| 70 CHROMEGTK_CALLBACK_1(Waiter, gboolean, OnWindowStateEvent, |
| 71 GdkEventWindowState*); |
| 72 |
| 73 private: |
| 74 Browser* browser_; |
| 75 gulong handler_id_; |
| 76 NotificationRegistrar registrar_; |
| 77 |
| 78 DISALLOW_COPY_AND_ASSIGN(Waiter); |
| 79 }; |
| 80 |
| 81 gboolean Waiter::OnWindowStateEvent(GtkWidget* widget, |
| 82 GdkEventWindowState* event) { |
| 83 MessageLoop::current()->Quit(); |
| 84 return false; |
| 85 } |
| 86 |
| 87 } // namespace |
23 | 88 |
24 namespace chromeos { | 89 namespace chromeos { |
25 | 90 |
26 class ScreenLockerTest : public CrosInProcessBrowserTest { | 91 class ScreenLockerTest : public CrosInProcessBrowserTest { |
27 public: | 92 public: |
28 ScreenLockerTest() {} | 93 ScreenLockerTest() {} |
29 | 94 |
30 protected: | 95 protected: |
31 | 96 |
32 // Test the no password mode with different unlock scheme given by | 97 // Test the no password mode with different unlock scheme given by |
33 // |unlock| function. | 98 // |unlock| function. |
34 void TestNoPassword(void (unlock)(views::Widget*)) { | 99 void TestNoPassword(void (unlock)(views::Widget*)) { |
35 UserManager::Get()->OffTheRecordUserLoggedIn(); | 100 UserManager::Get()->OffTheRecordUserLoggedIn(); |
36 ScreenLocker::Show(); | 101 ScreenLocker::Show(); |
37 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester()); | 102 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester()); |
38 tester->EmulateWindowManagerReady(); | 103 tester->EmulateWindowManagerReady(); |
39 ui_test_utils::WaitForNotification( | 104 ui_test_utils::WaitForNotification( |
40 NotificationType::SCREEN_LOCK_STATE_CHANGED); | 105 NotificationType::SCREEN_LOCK_STATE_CHANGED); |
41 EXPECT_TRUE(tester->IsOpen()); | 106 EXPECT_TRUE(tester->IsLocked()); |
42 tester->InjectMockAuthenticator("", ""); | 107 tester->InjectMockAuthenticator("", ""); |
43 | 108 |
44 unlock(tester->GetWidget()); | 109 unlock(tester->GetWidget()); |
45 | 110 |
46 ui_test_utils::RunAllPendingInMessageLoop(); | 111 ui_test_utils::RunAllPendingInMessageLoop(); |
47 EXPECT_TRUE(tester->IsOpen()); | 112 EXPECT_TRUE(tester->IsLocked()); |
48 | 113 |
49 // Emulate LockScreen request from PowerManager (via SessionManager). | 114 // Emulate LockScreen request from PowerManager (via SessionManager). |
50 ScreenLocker::Hide(); | 115 ScreenLocker::Hide(); |
51 ui_test_utils::RunAllPendingInMessageLoop(); | 116 ui_test_utils::RunAllPendingInMessageLoop(); |
52 EXPECT_FALSE(tester->IsOpen()); | 117 EXPECT_FALSE(tester->IsLocked()); |
53 } | 118 } |
54 | 119 |
55 private: | 120 private: |
56 virtual void SetUpInProcessBrowserTestFixture() { | 121 virtual void SetUpInProcessBrowserTestFixture() { |
57 InitStatusAreaMocks(); | 122 InitStatusAreaMocks(); |
58 InitMockScreenLockLibrary(); | 123 InitMockScreenLockLibrary(); |
59 EXPECT_CALL(*mock_screen_lock_library_, AddObserver(testing::_)) | 124 EXPECT_CALL(*mock_screen_lock_library_, AddObserver(testing::_)) |
60 .Times(1) | 125 .Times(1) |
61 .RetiresOnSaturation(); | 126 .RetiresOnSaturation(); |
62 EXPECT_CALL(*mock_screen_lock_library_, NotifyScreenLockCompleted()) | 127 EXPECT_CALL(*mock_screen_lock_library_, NotifyScreenLockCompleted()) |
(...skipping 25 matching lines...) Expand all Loading... |
88 .WillRepeatedly((testing::Return(0))) | 153 .WillRepeatedly((testing::Return(0))) |
89 .RetiresOnSaturation(); | 154 .RetiresOnSaturation(); |
90 UserManager::Get()->UserLoggedIn("user"); | 155 UserManager::Get()->UserLoggedIn("user"); |
91 ScreenLocker::Show(); | 156 ScreenLocker::Show(); |
92 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester()); | 157 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester()); |
93 tester->EmulateWindowManagerReady(); | 158 tester->EmulateWindowManagerReady(); |
94 ui_test_utils::WaitForNotification( | 159 ui_test_utils::WaitForNotification( |
95 NotificationType::SCREEN_LOCK_STATE_CHANGED); | 160 NotificationType::SCREEN_LOCK_STATE_CHANGED); |
96 | 161 |
97 tester->InjectMockAuthenticator("user", "pass"); | 162 tester->InjectMockAuthenticator("user", "pass"); |
98 EXPECT_TRUE(tester->IsOpen()); | 163 EXPECT_TRUE(tester->IsLocked()); |
99 tester->EnterPassword("fail"); | 164 tester->EnterPassword("fail"); |
100 ui_test_utils::RunAllPendingInMessageLoop(); | 165 ui_test_utils::RunAllPendingInMessageLoop(); |
101 EXPECT_TRUE(tester->IsOpen()); | 166 EXPECT_TRUE(tester->IsLocked()); |
102 tester->EnterPassword("pass"); | 167 tester->EnterPassword("pass"); |
103 ui_test_utils::RunAllPendingInMessageLoop(); | 168 ui_test_utils::RunAllPendingInMessageLoop(); |
104 // Successful authentication simply send a unlock request to PowerManager. | 169 // Successful authentication simply send a unlock request to PowerManager. |
105 EXPECT_TRUE(tester->IsOpen()); | 170 EXPECT_TRUE(tester->IsLocked()); |
106 | 171 |
107 // Emulate LockScreen request from PowerManager (via SessionManager). | 172 // Emulate LockScreen request from PowerManager (via SessionManager). |
108 // TODO(oshima): Find out better way to handle this in mock. | 173 // TODO(oshima): Find out better way to handle this in mock. |
109 ScreenLocker::Hide(); | 174 ScreenLocker::Hide(); |
110 ui_test_utils::RunAllPendingInMessageLoop(); | 175 ui_test_utils::RunAllPendingInMessageLoop(); |
111 EXPECT_FALSE(tester->IsOpen()); | 176 EXPECT_FALSE(tester->IsLocked()); |
| 177 } |
| 178 |
| 179 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestFullscreenExit) { |
| 180 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester()); |
| 181 { |
| 182 Waiter waiter(browser()); |
| 183 browser()->ToggleFullscreenMode(); |
| 184 waiter.Wait(false /* not locked */, true /* full screen */); |
| 185 EXPECT_TRUE(browser()->window()->IsFullscreen()); |
| 186 EXPECT_FALSE(tester->IsLocked()); |
| 187 } |
| 188 { |
| 189 Waiter waiter(browser()); |
| 190 UserManager::Get()->UserLoggedIn("user"); |
| 191 ScreenLocker::Show(); |
| 192 tester->EmulateWindowManagerReady(); |
| 193 waiter.Wait(true /* locked */, false /* full screen */); |
| 194 EXPECT_FALSE(browser()->window()->IsFullscreen()); |
| 195 EXPECT_TRUE(tester->IsLocked()); |
| 196 } |
| 197 tester->InjectMockAuthenticator("user", "pass"); |
| 198 tester->EnterPassword("pass"); |
| 199 ScreenLocker::Hide(); |
| 200 ui_test_utils::RunAllPendingInMessageLoop(); |
| 201 EXPECT_FALSE(tester->IsLocked()); |
112 } | 202 } |
113 | 203 |
114 void MouseMove(views::Widget* widget) { | 204 void MouseMove(views::Widget* widget) { |
115 ui_controls::SendMouseMove(10, 10); | 205 ui_controls::SendMouseMove(10, 10); |
116 } | 206 } |
117 | 207 |
118 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestNoPasswordWithMouseMove) { | 208 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestNoPasswordWithMouseMove) { |
119 TestNoPassword(MouseMove); | 209 TestNoPassword(MouseMove); |
120 } | 210 } |
121 | 211 |
122 void MouseClick(views::Widget* widget) { | 212 void MouseClick(views::Widget* widget) { |
123 ui_controls::SendMouseClick(ui_controls::RIGHT); | 213 ui_controls::SendMouseClick(ui_controls::RIGHT); |
124 } | 214 } |
125 | 215 |
126 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestNoPasswordWithMouseClick) { | 216 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestNoPasswordWithMouseClick) { |
127 TestNoPassword(MouseClick); | 217 TestNoPassword(MouseClick); |
128 } | 218 } |
129 | 219 |
130 void KeyPress(views::Widget* widget) { | 220 void KeyPress(views::Widget* widget) { |
131 ui_controls::SendKeyPress(GTK_WINDOW(widget->GetNativeView()), | 221 ui_controls::SendKeyPress(GTK_WINDOW(widget->GetNativeView()), |
132 base::VKEY_SPACE, false, false, false, false); | 222 base::VKEY_SPACE, false, false, false, false); |
133 } | 223 } |
134 | 224 |
135 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestNoPasswordWithKeyPress) { | 225 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestNoPasswordWithKeyPress) { |
136 TestNoPassword(KeyPress); | 226 TestNoPassword(KeyPress); |
137 } | 227 } |
138 | 228 |
139 } // namespace chromeos | 229 } // namespace chromeos |
OLD | NEW |