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.
5 #include "chrome/browser/chromeos/login/lock/screen_locker.h"
7 #include "ash/wm/window_state.h"
8 #include "base/command_line.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/chromeos/login/lock/screen_locker_tester.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/fullscreen/fullscreen_controller.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.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/dbus_thread_manager.h"
23 #include "chromeos/dbus/fake_session_manager_client.h"
24 #include "chromeos/login/auth/key.h"
25 #include "chromeos/login/auth/mock_authenticator.h"
26 #include "chromeos/login/auth/user_context.h"
27 #include "chromeos/login/user_names.h"
28 #include "content/public/browser/notification_service.h"
29 #include "testing/gmock/include/gmock/gmock.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31 #include "ui/base/test/ui_controls.h"
32 #include "ui/compositor/layer_animator.h"
33 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
34 #include "ui/views/widget/widget.h"
37 using testing::AnyNumber;
38 using testing::Return;
42 // An object that wait for lock state and fullscreen state.
43 class Waiter : public content::NotificationObserver {
45 explicit Waiter(Browser* browser)
49 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
50 content::NotificationService::AllSources());
52 chrome::NOTIFICATION_FULLSCREEN_CHANGED,
53 content::NotificationService::AllSources());
59 virtual void Observe(int type,
60 const content::NotificationSource& source,
61 const content::NotificationDetails& details) OVERRIDE {
62 DCHECK(type == chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED ||
63 type == chrome::NOTIFICATION_FULLSCREEN_CHANGED);
65 base::MessageLoop::current()->Quit();
68 // Wait until the two conditions are met.
69 void Wait(bool locker_state, bool fullscreen) {
71 scoped_ptr<chromeos::test::ScreenLockerTester>
72 tester(chromeos::ScreenLocker::GetTester());
73 while (tester->IsLocked() != locker_state ||
74 browser_->window()->IsFullscreen() != fullscreen) {
75 content::RunMessageLoop();
77 // Make sure all pending tasks are executed.
78 content::RunAllPendingInMessageLoop();
84 content::NotificationRegistrar registrar_;
86 // Are we currently running the message loop?
89 DISALLOW_COPY_AND_ASSIGN(Waiter);
96 class ScreenLockerTest : public InProcessBrowserTest {
98 ScreenLockerTest() : fake_session_manager_client_(NULL) {
102 FakeSessionManagerClient* fake_session_manager_client_;
104 void LockScreen(test::ScreenLockerTester* tester) {
105 ScreenLocker::Show();
106 tester->EmulateWindowManagerReady();
107 content::WindowedNotificationObserver lock_state_observer(
108 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
109 content::NotificationService::AllSources());
110 if (!tester->IsLocked())
111 lock_state_observer.Wait();
112 EXPECT_TRUE(tester->IsLocked());
115 // Verifies if LockScreenDismissed() was called once.
116 bool VerifyLockScreenDismissed() {
117 return 1 == fake_session_manager_client_->
118 notify_lock_screen_dismissed_call_count();
122 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
123 fake_session_manager_client_ = new FakeSessionManagerClient;
124 DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient(
125 scoped_ptr<SessionManagerClient>(fake_session_manager_client_));
127 InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
128 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
129 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
132 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
133 command_line->AppendSwitchASCII(switches::kLoginProfile, "user");
136 scoped_ptr<ui::ScopedAnimationDurationScaleMode> zero_duration_mode_;
138 DISALLOW_COPY_AND_ASSIGN(ScreenLockerTest);
141 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestBasic) {
142 ScreenLocker::Show();
143 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
144 tester->EmulateWindowManagerReady();
145 content::WindowedNotificationObserver lock_state_observer(
146 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
147 content::NotificationService::AllSources());
148 if (!tester->IsLocked())
149 lock_state_observer.Wait();
151 // Test to make sure that the widget is actually appearing and is of
152 // reasonable size, preventing a regression of
153 // http://code.google.com/p/chromium-os/issues/detail?id=5987
154 gfx::Rect lock_bounds = tester->GetChildWidget()->GetWindowBoundsInScreen();
155 EXPECT_GT(lock_bounds.width(), 10);
156 EXPECT_GT(lock_bounds.height(), 10);
158 UserContext user_context(chromeos::login::kStubUser);
159 user_context.SetKey(Key("pass"));
160 tester->InjectMockAuthenticator(user_context);
161 EXPECT_TRUE(tester->IsLocked());
162 tester->EnterPassword("fail");
163 content::RunAllPendingInMessageLoop();
164 EXPECT_TRUE(tester->IsLocked());
165 tester->EnterPassword("pass");
166 content::RunAllPendingInMessageLoop();
167 // Successful authentication clears the lock screen and tells the
168 // SessionManager to announce this over DBus.
169 EXPECT_FALSE(tester->IsLocked());
172 fake_session_manager_client_->notify_lock_screen_shown_call_count());
174 EXPECT_TRUE(VerifyLockScreenDismissed());
177 // Test how locking the screen affects an active fullscreen window.
178 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestFullscreenExit) {
179 // 1) If the active browser window is in fullscreen and the fullscreen window
180 // does not have all the pixels (e.g. the shelf is auto hidden instead of
181 // hidden), locking the screen should not exit fullscreen. The shelf is
182 // auto hidden when in immersive fullscreen.
183 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
184 BrowserWindow* browser_window = browser()->window();
185 ash::wm::WindowState* window_state = ash::wm::GetWindowState(
186 browser_window->GetNativeWindow());
188 Waiter waiter(browser());
189 browser()->fullscreen_controller()->ToggleBrowserFullscreenMode();
190 waiter.Wait(false /* not locked */, true /* full screen */);
191 EXPECT_TRUE(browser_window->IsFullscreen());
192 EXPECT_FALSE(window_state->hide_shelf_when_fullscreen());
193 EXPECT_FALSE(tester->IsLocked());
196 Waiter waiter(browser());
197 ScreenLocker::Show();
198 tester->EmulateWindowManagerReady();
199 waiter.Wait(true /* locked */, true /* full screen */);
200 EXPECT_TRUE(browser_window->IsFullscreen());
201 EXPECT_FALSE(window_state->hide_shelf_when_fullscreen());
202 EXPECT_TRUE(tester->IsLocked());
204 UserContext user_context(chromeos::login::kStubUser);
205 user_context.SetKey(Key("pass"));
206 tester->InjectMockAuthenticator(user_context);
207 tester->EnterPassword("pass");
208 content::RunAllPendingInMessageLoop();
209 EXPECT_FALSE(tester->IsLocked());
211 Waiter waiter(browser());
212 browser()->fullscreen_controller()->ToggleBrowserFullscreenMode();
213 waiter.Wait(false /* not locked */, false /* fullscreen */);
214 EXPECT_FALSE(browser_window->IsFullscreen());
217 // 2) If the active browser window is in fullscreen and the fullscreen window
218 // has all of the pixels, locking the screen should exit fullscreen. The
219 // fullscreen window has all of the pixels when in tab fullscreen.
221 Waiter waiter(browser());
222 content::WebContents* web_contents =
223 browser()->tab_strip_model()->GetActiveWebContents();
224 browser()->fullscreen_controller()->ToggleFullscreenModeForTab(
226 waiter.Wait(false /* not locked */, true /* fullscreen */);
227 EXPECT_TRUE(browser_window->IsFullscreen());
228 EXPECT_TRUE(window_state->hide_shelf_when_fullscreen());
229 EXPECT_FALSE(tester->IsLocked());
232 Waiter waiter(browser());
233 ScreenLocker::Show();
234 tester->EmulateWindowManagerReady();
235 waiter.Wait(true /* locked */, false /* full screen */);
236 EXPECT_FALSE(browser_window->IsFullscreen());
237 EXPECT_TRUE(tester->IsLocked());
240 tester->EnterPassword("pass");
241 content::RunAllPendingInMessageLoop();
242 EXPECT_FALSE(tester->IsLocked());
246 fake_session_manager_client_->notify_lock_screen_shown_call_count());
249 fake_session_manager_client_->notify_lock_screen_dismissed_call_count());
252 void SimulateKeyPress(views::Widget* widget, ui::KeyboardCode key_code) {
253 ui_controls::SendKeyPress(widget->GetNativeWindow(),
254 key_code, false, false, false, false);
257 void UnlockKeyPress(views::Widget* widget) {
258 SimulateKeyPress(widget, ui::VKEY_SPACE);
261 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestShowTwice) {
262 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
263 LockScreen(tester.get());
265 // Calling Show again simply send LockCompleted signal.
266 ScreenLocker::Show();
267 EXPECT_TRUE(tester->IsLocked());
270 fake_session_manager_client_->notify_lock_screen_shown_call_count());
273 // Close the locker to match expectations.
274 ScreenLocker::Hide();
275 content::RunAllPendingInMessageLoop();
276 EXPECT_FALSE(tester->IsLocked());
277 EXPECT_TRUE(VerifyLockScreenDismissed());
280 // TODO(flackr): Find out why the RenderView isn't getting the escape press
281 // and re-enable this test (currently this test is flaky).
282 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, DISABLED_TestEscape) {
283 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
284 LockScreen(tester.get());
288 fake_session_manager_client_->notify_lock_screen_shown_call_count());
290 tester->SetPassword("password");
291 EXPECT_EQ("password", tester->GetPassword());
292 // Escape clears the password.
293 SimulateKeyPress(tester->GetWidget(), ui::VKEY_ESCAPE);
294 content::RunAllPendingInMessageLoop();
295 EXPECT_EQ("", tester->GetPassword());
297 // Close the locker to match expectations.
298 ScreenLocker::Hide();
299 content::RunAllPendingInMessageLoop();
300 EXPECT_FALSE(tester->IsLocked());
301 EXPECT_TRUE(VerifyLockScreenDismissed());
304 } // namespace chromeos