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.
5 #include "chrome/browser/chromeos/login/screen_locker.h"
7 #include "base/command_line.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/chromeos/login/mock_authenticator.h"
12 #include "chrome/browser/chromeos/login/screen_locker_tester.h"
13 #include "chrome/browser/chromeos/login/user_manager.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/browser/ui/fullscreen/fullscreen_controller.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/test/base/in_process_browser_test.h"
20 #include "chrome/test/base/ui_test_utils.h"
21 #include "chromeos/chromeos_switches.h"
22 #include "chromeos/dbus/fake_dbus_thread_manager.h"
23 #include "chromeos/dbus/fake_session_manager_client.h"
24 #include "content/public/browser/notification_service.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "ui/base/test/ui_controls.h"
28 #include "ui/compositor/layer_animator.h"
29 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
30 #include "ui/views/widget/widget.h"
33 using testing::AnyNumber;
34 using testing::Return;
38 // An object that wait for lock state and fullscreen state.
39 class Waiter : public content::NotificationObserver {
41 explicit Waiter(Browser* browser)
45 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
46 content::NotificationService::AllSources());
48 chrome::NOTIFICATION_FULLSCREEN_CHANGED,
49 content::NotificationService::AllSources());
55 virtual void Observe(int type,
56 const content::NotificationSource& source,
57 const content::NotificationDetails& details) OVERRIDE {
58 DCHECK(type == chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED ||
59 type == chrome::NOTIFICATION_FULLSCREEN_CHANGED);
61 base::MessageLoop::current()->Quit();
64 // Wait until the two conditions are met.
65 void Wait(bool locker_state, bool fullscreen) {
67 scoped_ptr<chromeos::test::ScreenLockerTester>
68 tester(chromeos::ScreenLocker::GetTester());
69 while (tester->IsLocked() != locker_state ||
70 browser_->window()->IsFullscreen() != fullscreen) {
71 content::RunMessageLoop();
73 // Make sure all pending tasks are executed.
74 content::RunAllPendingInMessageLoop();
80 content::NotificationRegistrar registrar_;
82 // Are we currently running the message loop?
85 DISALLOW_COPY_AND_ASSIGN(Waiter);
92 class ScreenLockerTest : public InProcessBrowserTest {
94 ScreenLockerTest() : fake_session_manager_client_(NULL) {
98 FakeSessionManagerClient* fake_session_manager_client_;
100 void LockScreen(test::ScreenLockerTester* tester) {
101 ScreenLocker::Show();
102 tester->EmulateWindowManagerReady();
103 content::WindowedNotificationObserver lock_state_observer(
104 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
105 content::NotificationService::AllSources());
106 if (!tester->IsLocked())
107 lock_state_observer.Wait();
108 EXPECT_TRUE(tester->IsLocked());
111 // Verifies if LockScreenDismissed() was called once.
112 bool VerifyLockScreenDismissed() {
113 return 1 == fake_session_manager_client_->
114 notify_lock_screen_dismissed_call_count();
118 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
119 FakeDBusThreadManager* fake_dbus_thread_manager =
120 new FakeDBusThreadManager;
121 DBusThreadManager::InitializeForTesting(fake_dbus_thread_manager);
122 InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
123 fake_session_manager_client_ =
124 fake_dbus_thread_manager->fake_session_manager_client();
125 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
126 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
129 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
130 command_line->AppendSwitchASCII(switches::kLoginProfile, "user");
133 scoped_ptr<ui::ScopedAnimationDurationScaleMode> zero_duration_mode_;
135 DISALLOW_COPY_AND_ASSIGN(ScreenLockerTest);
138 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestBasic) {
139 ScreenLocker::Show();
140 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
141 tester->EmulateWindowManagerReady();
142 content::WindowedNotificationObserver lock_state_observer(
143 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
144 content::NotificationService::AllSources());
145 if (!chromeos::ScreenLocker::GetTester()->IsLocked())
146 lock_state_observer.Wait();
148 // Test to make sure that the widget is actually appearing and is of
149 // reasonable size, preventing a regression of
150 // http://code.google.com/p/chromium-os/issues/detail?id=5987
151 gfx::Rect lock_bounds = tester->GetChildWidget()->GetWindowBoundsInScreen();
152 EXPECT_GT(lock_bounds.width(), 10);
153 EXPECT_GT(lock_bounds.height(), 10);
155 tester->InjectMockAuthenticator(UserManager::kStubUser, "pass");
156 EXPECT_TRUE(tester->IsLocked());
157 tester->EnterPassword("fail");
158 content::RunAllPendingInMessageLoop();
159 EXPECT_TRUE(tester->IsLocked());
160 tester->EnterPassword("pass");
161 content::RunAllPendingInMessageLoop();
162 // Successful authentication clears the lock screen and tells the
163 // SessionManager to announce this over DBus.
164 EXPECT_FALSE(tester->IsLocked());
167 fake_session_manager_client_->notify_lock_screen_shown_call_count());
169 EXPECT_TRUE(VerifyLockScreenDismissed());
172 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestFullscreenExit) {
173 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
175 Waiter waiter(browser());
176 browser()->fullscreen_controller()->ToggleFullscreenMode();
177 waiter.Wait(false /* not locked */, true /* full screen */);
178 EXPECT_TRUE(browser()->window()->IsFullscreen());
179 EXPECT_FALSE(tester->IsLocked());
182 Waiter waiter(browser());
183 ScreenLocker::Show();
184 tester->EmulateWindowManagerReady();
185 waiter.Wait(true /* locked */, false /* full screen */);
186 EXPECT_FALSE(browser()->window()->IsFullscreen());
187 EXPECT_TRUE(tester->IsLocked());
191 fake_session_manager_client_->notify_lock_screen_shown_call_count());
193 tester->InjectMockAuthenticator(UserManager::kStubUser, "pass");
194 tester->EnterPassword("pass");
195 content::RunAllPendingInMessageLoop();
196 EXPECT_FALSE(tester->IsLocked());
197 EXPECT_TRUE(VerifyLockScreenDismissed());
200 void SimulateKeyPress(views::Widget* widget, ui::KeyboardCode key_code) {
201 ui_controls::SendKeyPress(widget->GetNativeWindow(),
202 key_code, false, false, false, false);
205 void UnlockKeyPress(views::Widget* widget) {
206 SimulateKeyPress(widget, ui::VKEY_SPACE);
209 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestShowTwice) {
210 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
211 LockScreen(tester.get());
213 // Ensure there's a profile or this test crashes.
214 ProfileManager::GetDefaultProfile();
216 // Calling Show again simply send LockCompleted signal.
217 ScreenLocker::Show();
218 EXPECT_TRUE(tester->IsLocked());
221 fake_session_manager_client_->notify_lock_screen_shown_call_count());
224 // Close the locker to match expectations.
225 ScreenLocker::Hide();
226 content::RunAllPendingInMessageLoop();
227 EXPECT_FALSE(tester->IsLocked());
228 EXPECT_TRUE(VerifyLockScreenDismissed());
231 // TODO(flackr): Find out why the RenderView isn't getting the escape press
232 // and re-enable this test (currently this test is flaky).
233 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, DISABLED_TestEscape) {
234 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
235 LockScreen(tester.get());
237 // Ensure there's a profile or this test crashes.
238 ProfileManager::GetDefaultProfile();
242 fake_session_manager_client_->notify_lock_screen_shown_call_count());
244 tester->SetPassword("password");
245 EXPECT_EQ("password", tester->GetPassword());
246 // Escape clears the password.
247 SimulateKeyPress(tester->GetWidget(), ui::VKEY_ESCAPE);
248 content::RunAllPendingInMessageLoop();
249 EXPECT_EQ("", tester->GetPassword());
251 // Close the locker to match expectations.
252 ScreenLocker::Hide();
253 content::RunAllPendingInMessageLoop();
254 EXPECT_FALSE(tester->IsLocked());
255 EXPECT_TRUE(VerifyLockScreenDismissed());
258 } // namespace chromeos