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/fake_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 FakeDBusThreadManager* fake_dbus_thread_manager = new FakeDBusThreadManager;
124 fake_dbus_thread_manager->SetFakeClients();
125 fake_session_manager_client_ = new FakeSessionManagerClient;
126 fake_dbus_thread_manager->SetSessionManagerClient(
127 scoped_ptr<SessionManagerClient>(fake_session_manager_client_));
128 DBusThreadManager::SetInstanceForTesting(fake_dbus_thread_manager);
130 InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
131 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
132 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
135 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
136 command_line->AppendSwitchASCII(switches::kLoginProfile, "user");
139 scoped_ptr<ui::ScopedAnimationDurationScaleMode> zero_duration_mode_;
141 DISALLOW_COPY_AND_ASSIGN(ScreenLockerTest);
144 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestBasic) {
145 ScreenLocker::Show();
146 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
147 tester->EmulateWindowManagerReady();
148 content::WindowedNotificationObserver lock_state_observer(
149 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
150 content::NotificationService::AllSources());
151 if (!tester->IsLocked())
152 lock_state_observer.Wait();
154 // Test to make sure that the widget is actually appearing and is of
155 // reasonable size, preventing a regression of
156 // http://code.google.com/p/chromium-os/issues/detail?id=5987
157 gfx::Rect lock_bounds = tester->GetChildWidget()->GetWindowBoundsInScreen();
158 EXPECT_GT(lock_bounds.width(), 10);
159 EXPECT_GT(lock_bounds.height(), 10);
161 UserContext user_context(chromeos::login::kStubUser);
162 user_context.SetKey(Key("pass"));
163 tester->InjectMockAuthenticator(user_context);
164 EXPECT_TRUE(tester->IsLocked());
165 tester->EnterPassword("fail");
166 content::RunAllPendingInMessageLoop();
167 EXPECT_TRUE(tester->IsLocked());
168 tester->EnterPassword("pass");
169 content::RunAllPendingInMessageLoop();
170 // Successful authentication clears the lock screen and tells the
171 // SessionManager to announce this over DBus.
172 EXPECT_FALSE(tester->IsLocked());
175 fake_session_manager_client_->notify_lock_screen_shown_call_count());
177 EXPECT_TRUE(VerifyLockScreenDismissed());
180 // Test how locking the screen affects an active fullscreen window.
181 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestFullscreenExit) {
182 // 1) If the active browser window is in fullscreen and the fullscreen window
183 // does not have all the pixels (e.g. the shelf is auto hidden instead of
184 // hidden), locking the screen should not exit fullscreen. The shelf is
185 // auto hidden when in immersive fullscreen.
186 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
187 BrowserWindow* browser_window = browser()->window();
188 ash::wm::WindowState* window_state = ash::wm::GetWindowState(
189 browser_window->GetNativeWindow());
191 Waiter waiter(browser());
192 browser()->fullscreen_controller()->ToggleBrowserFullscreenMode();
193 waiter.Wait(false /* not locked */, true /* full screen */);
194 EXPECT_TRUE(browser_window->IsFullscreen());
195 EXPECT_FALSE(window_state->hide_shelf_when_fullscreen());
196 EXPECT_FALSE(tester->IsLocked());
199 Waiter waiter(browser());
200 ScreenLocker::Show();
201 tester->EmulateWindowManagerReady();
202 waiter.Wait(true /* locked */, true /* full screen */);
203 EXPECT_TRUE(browser_window->IsFullscreen());
204 EXPECT_FALSE(window_state->hide_shelf_when_fullscreen());
205 EXPECT_TRUE(tester->IsLocked());
207 UserContext user_context(chromeos::login::kStubUser);
208 user_context.SetKey(Key("pass"));
209 tester->InjectMockAuthenticator(user_context);
210 tester->EnterPassword("pass");
211 content::RunAllPendingInMessageLoop();
212 EXPECT_FALSE(tester->IsLocked());
214 Waiter waiter(browser());
215 browser()->fullscreen_controller()->ToggleBrowserFullscreenMode();
216 waiter.Wait(false /* not locked */, false /* fullscreen */);
217 EXPECT_FALSE(browser_window->IsFullscreen());
220 // 2) If the active browser window is in fullscreen and the fullscreen window
221 // has all of the pixels, locking the screen should exit fullscreen. The
222 // fullscreen window has all of the pixels when in tab fullscreen.
224 Waiter waiter(browser());
225 content::WebContents* web_contents =
226 browser()->tab_strip_model()->GetActiveWebContents();
227 browser()->fullscreen_controller()->ToggleFullscreenModeForTab(
229 waiter.Wait(false /* not locked */, true /* fullscreen */);
230 EXPECT_TRUE(browser_window->IsFullscreen());
231 EXPECT_TRUE(window_state->hide_shelf_when_fullscreen());
232 EXPECT_FALSE(tester->IsLocked());
235 Waiter waiter(browser());
236 ScreenLocker::Show();
237 tester->EmulateWindowManagerReady();
238 waiter.Wait(true /* locked */, false /* full screen */);
239 EXPECT_FALSE(browser_window->IsFullscreen());
240 EXPECT_TRUE(tester->IsLocked());
243 tester->EnterPassword("pass");
244 content::RunAllPendingInMessageLoop();
245 EXPECT_FALSE(tester->IsLocked());
249 fake_session_manager_client_->notify_lock_screen_shown_call_count());
252 fake_session_manager_client_->notify_lock_screen_dismissed_call_count());
255 void SimulateKeyPress(views::Widget* widget, ui::KeyboardCode key_code) {
256 ui_controls::SendKeyPress(widget->GetNativeWindow(),
257 key_code, false, false, false, false);
260 void UnlockKeyPress(views::Widget* widget) {
261 SimulateKeyPress(widget, ui::VKEY_SPACE);
264 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, TestShowTwice) {
265 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
266 LockScreen(tester.get());
268 // Calling Show again simply send LockCompleted signal.
269 ScreenLocker::Show();
270 EXPECT_TRUE(tester->IsLocked());
273 fake_session_manager_client_->notify_lock_screen_shown_call_count());
276 // Close the locker to match expectations.
277 ScreenLocker::Hide();
278 content::RunAllPendingInMessageLoop();
279 EXPECT_FALSE(tester->IsLocked());
280 EXPECT_TRUE(VerifyLockScreenDismissed());
283 // TODO(flackr): Find out why the RenderView isn't getting the escape press
284 // and re-enable this test (currently this test is flaky).
285 IN_PROC_BROWSER_TEST_F(ScreenLockerTest, DISABLED_TestEscape) {
286 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
287 LockScreen(tester.get());
291 fake_session_manager_client_->notify_lock_screen_shown_call_count());
293 tester->SetPassword("password");
294 EXPECT_EQ("password", tester->GetPassword());
295 // Escape clears the password.
296 SimulateKeyPress(tester->GetWidget(), ui::VKEY_ESCAPE);
297 content::RunAllPendingInMessageLoop();
298 EXPECT_EQ("", tester->GetPassword());
300 // Close the locker to match expectations.
301 ScreenLocker::Hide();
302 content::RunAllPendingInMessageLoop();
303 EXPECT_FALSE(tester->IsLocked());
304 EXPECT_TRUE(VerifyLockScreenDismissed());
307 } // namespace chromeos