1 // Copyright 2013 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 "ash/wm/lock_state_controller.h"
7 #include "ash/session/session_state_delegate.h"
9 #include "ash/test/ash_test_base.h"
10 #include "ash/test/test_lock_state_controller_delegate.h"
11 #include "ash/test/test_screenshot_delegate.h"
12 #include "ash/test/test_session_state_animator.h"
13 #include "ash/test/test_shell_delegate.h"
14 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
15 #include "ash/wm/power_button_controller.h"
16 #include "ash/wm/session_state_animator.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/scoped_vector.h"
19 #include "base/time/time.h"
20 #include "ui/events/test/event_generator.h"
21 #include "ui/gfx/geometry/size.h"
23 #if defined(OS_CHROMEOS)
24 #include "ui/display/chromeos/display_configurator.h"
25 #include "ui/display/chromeos/test/test_display_snapshot.h"
26 #include "ui/display/types/display_constants.h"
33 bool cursor_visible() {
34 return ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible();
37 void CheckCalledCallback(bool* flag
) {
44 class LockStateControllerTest
: public AshTestBase
{
46 LockStateControllerTest() : power_button_controller_(NULL
),
47 lock_state_controller_(NULL
),
48 lock_state_controller_delegate_(NULL
),
49 test_animator_(NULL
) {
51 ~LockStateControllerTest() override
{}
53 void SetUp() override
{
56 scoped_ptr
<LockStateControllerDelegate
> lock_state_controller_delegate(
57 lock_state_controller_delegate_
= new TestLockStateControllerDelegate
);
58 test_animator_
= new TestSessionStateAnimator
;
60 lock_state_controller_
= Shell::GetInstance()->lock_state_controller();
61 lock_state_controller_
->SetDelegate(lock_state_controller_delegate
.Pass());
62 lock_state_controller_
->set_animator_for_test(test_animator_
);
64 test_api_
.reset(new LockStateController::TestApi(lock_state_controller_
));
66 power_button_controller_
= Shell::GetInstance()->power_button_controller();
67 session_state_delegate_
= Shell::GetInstance()->session_state_delegate();
69 shell_delegate_
= reinterpret_cast<TestShellDelegate
*>(
70 ash::Shell::GetInstance()->delegate());
74 void GenerateMouseMoveEvent() {
75 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
76 generator
.MoveMouseTo(10, 10);
79 int NumShutdownRequests() {
80 return lock_state_controller_delegate_
->num_shutdown_requests() +
81 shell_delegate_
->num_exit_requests();
84 void Advance(SessionStateAnimator::AnimationSpeed speed
) {
85 test_animator_
->Advance(test_animator_
->GetDuration(speed
));
88 void AdvancePartially(SessionStateAnimator::AnimationSpeed speed
,
90 base::TimeDelta duration
= test_animator_
->GetDuration(speed
);
91 base::TimeDelta partial_duration
=
92 base::TimeDelta::FromInternalValue(duration
.ToInternalValue() * factor
);
93 test_animator_
->Advance(partial_duration
);
96 void ExpectPreLockAnimationStarted() {
97 SCOPED_TRACE("Failure in ExpectPreLockAnimationStarted");
98 EXPECT_LT(0u, test_animator_
->GetAnimationCount());
100 test_animator_
->AreContainersAnimated(
101 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS
,
102 SessionStateAnimator::ANIMATION_LIFT
));
104 test_animator_
->AreContainersAnimated(
105 SessionStateAnimator::LAUNCHER
,
106 SessionStateAnimator::ANIMATION_FADE_OUT
));
108 test_animator_
->AreContainersAnimated(
109 SessionStateAnimator::LOCK_SCREEN_CONTAINERS
,
110 SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY
));
111 EXPECT_TRUE(test_api_
->is_animating_lock());
114 void ExpectPreLockAnimationRunning() {
115 SCOPED_TRACE("Failure in ExpectPreLockAnimationRunning");
116 EXPECT_LT(0u, test_animator_
->GetAnimationCount());
118 test_animator_
->AreContainersAnimated(
119 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS
,
120 SessionStateAnimator::ANIMATION_LIFT
));
122 test_animator_
->AreContainersAnimated(
123 SessionStateAnimator::LAUNCHER
,
124 SessionStateAnimator::ANIMATION_FADE_OUT
));
125 EXPECT_TRUE(test_api_
->is_animating_lock());
128 void ExpectPreLockAnimationCancel() {
129 SCOPED_TRACE("Failure in ExpectPreLockAnimationCancel");
130 EXPECT_LT(0u, test_animator_
->GetAnimationCount());
132 test_animator_
->AreContainersAnimated(
133 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS
,
134 SessionStateAnimator::ANIMATION_UNDO_LIFT
));
136 test_animator_
->AreContainersAnimated(
137 SessionStateAnimator::LAUNCHER
,
138 SessionStateAnimator::ANIMATION_FADE_IN
));
141 void ExpectPreLockAnimationFinished() {
142 SCOPED_TRACE("Failure in ExpectPreLockAnimationFinished");
144 test_animator_
->AreContainersAnimated(
145 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS
,
146 SessionStateAnimator::ANIMATION_LIFT
));
148 test_animator_
->AreContainersAnimated(
149 SessionStateAnimator::LAUNCHER
,
150 SessionStateAnimator::ANIMATION_FADE_OUT
));
152 test_animator_
->AreContainersAnimated(
153 SessionStateAnimator::LOCK_SCREEN_CONTAINERS
,
154 SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY
));
157 void ExpectPostLockAnimationStarted() {
158 SCOPED_TRACE("Failure in ExpectPostLockAnimationStarted");
159 EXPECT_LT(0u, test_animator_
->GetAnimationCount());
161 test_animator_
->AreContainersAnimated(
162 SessionStateAnimator::LOCK_SCREEN_CONTAINERS
,
163 SessionStateAnimator::ANIMATION_RAISE_TO_SCREEN
));
166 void ExpectPostLockAnimationFinished() {
167 SCOPED_TRACE("Failure in ExpectPostLockAnimationFinished");
169 test_animator_
->AreContainersAnimated(
170 SessionStateAnimator::LOCK_SCREEN_CONTAINERS
,
171 SessionStateAnimator::ANIMATION_RAISE_TO_SCREEN
));
174 void ExpectUnlockBeforeUIDestroyedAnimationStarted() {
175 SCOPED_TRACE("Failure in ExpectUnlockBeforeUIDestroyedAnimationStarted");
176 EXPECT_LT(0u, test_animator_
->GetAnimationCount());
178 test_animator_
->AreContainersAnimated(
179 SessionStateAnimator::LOCK_SCREEN_CONTAINERS
,
180 SessionStateAnimator::ANIMATION_LIFT
));
183 void ExpectUnlockBeforeUIDestroyedAnimationFinished() {
184 SCOPED_TRACE("Failure in ExpectUnlockBeforeUIDestroyedAnimationFinished");
186 test_animator_
->AreContainersAnimated(
187 SessionStateAnimator::LOCK_SCREEN_CONTAINERS
,
188 SessionStateAnimator::ANIMATION_LIFT
));
191 void ExpectUnlockAfterUIDestroyedAnimationStarted() {
192 SCOPED_TRACE("Failure in ExpectUnlockAfterUIDestroyedAnimationStarted");
193 EXPECT_LT(0u, test_animator_
->GetAnimationCount());
195 test_animator_
->AreContainersAnimated(
196 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS
,
197 SessionStateAnimator::ANIMATION_DROP
));
199 test_animator_
->AreContainersAnimated(
200 SessionStateAnimator::LAUNCHER
,
201 SessionStateAnimator::ANIMATION_FADE_IN
));
204 void ExpectUnlockAfterUIDestroyedAnimationFinished() {
205 SCOPED_TRACE("Failure in ExpectUnlockAfterUIDestroyedAnimationFinished");
206 EXPECT_EQ(0u, test_animator_
->GetAnimationCount());
208 test_animator_
->AreContainersAnimated(
209 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS
,
210 SessionStateAnimator::ANIMATION_DROP
));
212 test_animator_
->AreContainersAnimated(
213 SessionStateAnimator::LAUNCHER
,
214 SessionStateAnimator::ANIMATION_FADE_IN
));
217 void ExpectShutdownAnimationStarted() {
218 SCOPED_TRACE("Failure in ExpectShutdownAnimationStarted");
219 EXPECT_LT(0u, test_animator_
->GetAnimationCount());
221 test_animator_
->AreContainersAnimated(
222 SessionStateAnimator::ROOT_CONTAINER
,
223 SessionStateAnimator::ANIMATION_GRAYSCALE_BRIGHTNESS
));
226 void ExpectShutdownAnimationFinished() {
227 SCOPED_TRACE("Failure in ExpectShutdownAnimationFinished");
228 EXPECT_EQ(0u, test_animator_
->GetAnimationCount());
230 test_animator_
->AreContainersAnimated(
231 SessionStateAnimator::ROOT_CONTAINER
,
232 SessionStateAnimator::ANIMATION_GRAYSCALE_BRIGHTNESS
));
235 void ExpectShutdownAnimationCancel() {
236 SCOPED_TRACE("Failure in ExpectShutdownAnimationCancel");
237 EXPECT_LT(0u, test_animator_
->GetAnimationCount());
239 test_animator_
->AreContainersAnimated(
240 SessionStateAnimator::ROOT_CONTAINER
,
241 SessionStateAnimator::ANIMATION_UNDO_GRAYSCALE_BRIGHTNESS
));
244 void ExpectBackgroundIsShowing() {
245 SCOPED_TRACE("Failure in ExpectBackgroundIsShowing");
246 EXPECT_LT(0u, test_animator_
->GetAnimationCount());
248 test_animator_
->AreContainersAnimated(
249 SessionStateAnimator::DESKTOP_BACKGROUND
,
250 SessionStateAnimator::ANIMATION_FADE_IN
));
253 void ExpectBackgroundIsHiding() {
254 SCOPED_TRACE("Failure in ExpectBackgroundIsHiding");
255 EXPECT_LT(0u, test_animator_
->GetAnimationCount());
257 test_animator_
->AreContainersAnimated(
258 SessionStateAnimator::DESKTOP_BACKGROUND
,
259 SessionStateAnimator::ANIMATION_FADE_OUT
));
262 void ExpectRestoringBackgroundVisibility() {
263 SCOPED_TRACE("Failure in ExpectRestoringBackgroundVisibility");
264 EXPECT_LT(0u, test_animator_
->GetAnimationCount());
266 test_animator_
->AreContainersAnimated(
267 SessionStateAnimator::DESKTOP_BACKGROUND
,
268 SessionStateAnimator::ANIMATION_FADE_IN
));
271 void ExpectUnlockedState() {
272 SCOPED_TRACE("Failure in ExpectUnlockedState");
273 EXPECT_EQ(0u, test_animator_
->GetAnimationCount());
274 EXPECT_FALSE(session_state_delegate_
->IsScreenLocked());
277 void ExpectLockedState() {
278 SCOPED_TRACE("Failure in ExpectLockedState");
279 EXPECT_EQ(0u, test_animator_
->GetAnimationCount());
280 EXPECT_TRUE(session_state_delegate_
->IsScreenLocked());
283 void HideBackground() {
284 test_animator_
->HideBackground();
287 void PressPowerButton() {
288 power_button_controller_
->OnPowerButtonEvent(true, base::TimeTicks::Now());
291 void ReleasePowerButton() {
292 power_button_controller_
->OnPowerButtonEvent(false, base::TimeTicks::Now());
295 void PressLockButton() {
296 power_button_controller_
->OnLockButtonEvent(true, base::TimeTicks::Now());
299 void ReleaseLockButton() {
300 power_button_controller_
->OnLockButtonEvent(false, base::TimeTicks::Now());
303 void PressVolumeDown() {
304 GetEventGenerator().PressKey(ui::VKEY_VOLUME_DOWN
, ui::EF_NONE
);
307 void ReleaseVolumeDown() {
308 GetEventGenerator().ReleaseKey(ui::VKEY_VOLUME_DOWN
, ui::EF_NONE
);
312 lock_state_controller_
->OnLockStateChanged(true);
313 session_state_delegate_
->LockScreen();
316 void SuccessfulAuthentication(bool* call_flag
) {
317 base::Closure closure
= base::Bind(&CheckCalledCallback
, call_flag
);
318 lock_state_controller_
->OnLockScreenHide(closure
);
321 void SystemUnlocks() {
322 lock_state_controller_
->OnLockStateChanged(false);
323 session_state_delegate_
->UnlockScreen();
326 void EnableMaximizeMode(bool enable
) {
327 Shell::GetInstance()->maximize_mode_controller()->
328 EnableMaximizeModeWindowManager(enable
);
331 void Initialize(bool legacy_button
, user::LoginStatus status
) {
332 power_button_controller_
->set_has_legacy_power_button_for_test(
334 lock_state_controller_
->OnLoginStateChanged(status
);
335 SetUserLoggedIn(status
!= user::LOGGED_IN_NONE
);
336 if (status
== user::LOGGED_IN_GUEST
)
337 SetCanLockScreen(false);
338 lock_state_controller_
->OnLockStateChanged(false);
341 PowerButtonController
* power_button_controller_
; // not owned
342 LockStateController
* lock_state_controller_
; // not owned
343 TestLockStateControllerDelegate
*
344 lock_state_controller_delegate_
; // not owned
345 TestSessionStateAnimator
* test_animator_
; // not owned
346 SessionStateDelegate
* session_state_delegate_
; // not owned
347 scoped_ptr
<LockStateController::TestApi
> test_api_
;
348 TestShellDelegate
* shell_delegate_
; // not owned
351 DISALLOW_COPY_AND_ASSIGN(LockStateControllerTest
);
354 // Test the lock-to-shutdown flow for non-Chrome-OS hardware that doesn't
355 // correctly report power button releases. We should lock immediately the first
356 // time the button is pressed and shut down when it's pressed from the locked
358 TEST_F(LockStateControllerTest
, LegacyLockAndShutDown
) {
359 Initialize(true, user::LOGGED_IN_USER
);
361 ExpectUnlockedState();
363 // We should request that the screen be locked immediately after seeing the
364 // power button get pressed.
367 EXPECT_FALSE(test_api_
->is_lock_cancellable());
369 ExpectPreLockAnimationStarted();
370 test_animator_
->CompleteAllAnimations(true);
371 ExpectPreLockAnimationFinished();
373 EXPECT_EQ(1, lock_state_controller_delegate_
->num_lock_requests());
375 // Notify that we locked successfully.
376 lock_state_controller_
->OnStartingLock();
377 EXPECT_EQ(0u, test_animator_
->GetAnimationCount());
381 ExpectPostLockAnimationStarted();
382 test_animator_
->CompleteAllAnimations(true);
383 ExpectPostLockAnimationFinished();
385 // We shouldn't progress towards the shutdown state, however.
386 EXPECT_FALSE(test_api_
->lock_to_shutdown_timer_is_running());
387 EXPECT_FALSE(test_api_
->shutdown_timer_is_running());
389 ReleasePowerButton();
391 // Hold the button again and check that we start shutting down.
394 ExpectShutdownAnimationStarted();
396 EXPECT_EQ(0, NumShutdownRequests());
397 // Make sure a mouse move event won't show the cursor.
398 GenerateMouseMoveEvent();
399 EXPECT_FALSE(cursor_visible());
401 EXPECT_TRUE(test_api_
->real_shutdown_timer_is_running());
402 test_api_
->trigger_real_shutdown_timeout();
403 EXPECT_EQ(1, NumShutdownRequests());
406 // Test that we start shutting down immediately if the power button is pressed
407 // while we're not logged in on an unofficial system.
408 TEST_F(LockStateControllerTest
, LegacyNotLoggedIn
) {
409 Initialize(true, user::LOGGED_IN_NONE
);
412 ExpectShutdownAnimationStarted();
414 EXPECT_TRUE(test_api_
->real_shutdown_timer_is_running());
417 // Test that we start shutting down immediately if the power button is pressed
418 // while we're logged in as a guest on an unofficial system.
419 TEST_F(LockStateControllerTest
, LegacyGuest
) {
420 Initialize(true, user::LOGGED_IN_GUEST
);
423 ExpectShutdownAnimationStarted();
425 EXPECT_TRUE(test_api_
->real_shutdown_timer_is_running());
428 // When we hold the power button while the user isn't logged in, we should shut
429 // down the machine directly.
430 TEST_F(LockStateControllerTest
, ShutdownWhenNotLoggedIn
) {
431 Initialize(false, user::LOGGED_IN_NONE
);
433 // Press the power button and check that we start the shutdown timer.
435 EXPECT_FALSE(test_api_
->is_animating_lock());
436 EXPECT_TRUE(test_api_
->shutdown_timer_is_running());
437 ExpectShutdownAnimationStarted();
439 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN
, 0.5f
);
441 // Release the power button before the shutdown timer fires.
442 ReleasePowerButton();
444 EXPECT_FALSE(test_api_
->shutdown_timer_is_running());
445 ExpectShutdownAnimationCancel();
447 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_REVERT
, 0.5f
);
449 // Press the button again and make the shutdown timeout fire this time.
450 // Check that we start the timer for actually requesting the shutdown.
453 EXPECT_TRUE(test_api_
->shutdown_timer_is_running());
455 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN
);
456 ExpectShutdownAnimationFinished();
457 test_api_
->trigger_shutdown_timeout();
459 EXPECT_TRUE(test_api_
->real_shutdown_timer_is_running());
460 EXPECT_EQ(0, NumShutdownRequests());
462 // When the timout fires, we should request a shutdown.
463 test_api_
->trigger_real_shutdown_timeout();
465 EXPECT_EQ(1, NumShutdownRequests());
468 // Test that we lock the screen and deal with unlocking correctly.
469 TEST_F(LockStateControllerTest
, LockAndUnlock
) {
470 Initialize(false, user::LOGGED_IN_USER
);
472 ExpectUnlockedState();
474 // Press the power button and check that the lock timer is started and that we
475 // start lifting the non-screen-locker containers.
478 ExpectPreLockAnimationStarted();
479 EXPECT_TRUE(test_api_
->is_lock_cancellable());
480 EXPECT_EQ(0, lock_state_controller_delegate_
->num_lock_requests());
482 test_animator_
->CompleteAllAnimations(true);
483 ExpectPreLockAnimationFinished();
485 EXPECT_EQ(1, lock_state_controller_delegate_
->num_lock_requests());
487 // Notify that we locked successfully.
488 lock_state_controller_
->OnStartingLock();
489 // We had that animation already.
490 EXPECT_EQ(0u, test_animator_
->GetAnimationCount());
494 ExpectPostLockAnimationStarted();
495 test_animator_
->CompleteAllAnimations(true);
496 ExpectPostLockAnimationFinished();
498 // When we release the power button, the lock-to-shutdown timer should be
501 EXPECT_TRUE(test_api_
->lock_to_shutdown_timer_is_running());
502 ReleasePowerButton();
504 EXPECT_FALSE(test_api_
->lock_to_shutdown_timer_is_running());
506 // Notify that the screen has been unlocked. We should show the
507 // non-screen-locker windows.
509 SuccessfulAuthentication(&called
);
511 ExpectUnlockBeforeUIDestroyedAnimationStarted();
512 EXPECT_FALSE(called
);
513 test_animator_
->CompleteAllAnimations(true);
514 ExpectUnlockBeforeUIDestroyedAnimationFinished();
520 ExpectUnlockAfterUIDestroyedAnimationStarted();
521 test_animator_
->CompleteAllAnimations(true);
522 ExpectUnlockAfterUIDestroyedAnimationFinished();
524 ExpectUnlockedState();
527 // Test that we deal with cancelling lock correctly.
528 TEST_F(LockStateControllerTest
, LockAndCancel
) {
529 Initialize(false, user::LOGGED_IN_USER
);
531 ExpectUnlockedState();
533 // Press the power button and check that the lock timer is started and that we
534 // start lifting the non-screen-locker containers.
537 ExpectPreLockAnimationStarted();
538 EXPECT_TRUE(test_api_
->is_lock_cancellable());
540 // forward only half way through
541 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
, 0.5f
);
543 // Release the button before the lock timer fires.
544 ReleasePowerButton();
546 ExpectPreLockAnimationCancel();
548 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
549 ExpectUnlockedState();
550 EXPECT_EQ(0, lock_state_controller_delegate_
->num_lock_requests());
553 // Test that we deal with cancelling lock correctly.
554 TEST_F(LockStateControllerTest
, LockAndCancelAndLockAgain
) {
555 Initialize(false, user::LOGGED_IN_USER
);
557 ExpectUnlockedState();
559 // Press the power button and check that the lock timer is started and that we
560 // start lifting the non-screen-locker containers.
563 ExpectPreLockAnimationStarted();
564 EXPECT_TRUE(test_api_
->is_lock_cancellable());
566 // forward only half way through
567 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
, 0.5f
);
569 // Release the button before the lock timer fires.
570 ReleasePowerButton();
571 ExpectPreLockAnimationCancel();
573 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDO_MOVE_WINDOWS
,
577 ExpectPreLockAnimationStarted();
578 EXPECT_TRUE(test_api_
->is_lock_cancellable());
580 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
, 0.6f
);
582 EXPECT_EQ(0, lock_state_controller_delegate_
->num_lock_requests());
584 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
, 0.6f
);
585 ExpectPreLockAnimationFinished();
586 EXPECT_EQ(1, lock_state_controller_delegate_
->num_lock_requests());
589 // Hold the power button down from the unlocked state to eventual shutdown.
590 TEST_F(LockStateControllerTest
, LockToShutdown
) {
591 Initialize(false, user::LOGGED_IN_USER
);
593 // Hold the power button and lock the screen.
595 EXPECT_TRUE(test_api_
->is_animating_lock());
597 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
);
599 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
601 // When the lock-to-shutdown timeout fires, we should start the shutdown
603 EXPECT_TRUE(test_api_
->lock_to_shutdown_timer_is_running());
605 test_api_
->trigger_lock_to_shutdown_timeout();
607 ExpectShutdownAnimationStarted();
608 EXPECT_TRUE(test_api_
->shutdown_timer_is_running());
610 // Fire the shutdown timeout and check that we request shutdown.
611 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN
);
612 ExpectShutdownAnimationFinished();
613 test_api_
->trigger_shutdown_timeout();
615 EXPECT_TRUE(test_api_
->real_shutdown_timer_is_running());
616 EXPECT_EQ(0, NumShutdownRequests());
617 test_api_
->trigger_real_shutdown_timeout();
618 EXPECT_EQ(1, NumShutdownRequests());
621 // Hold the power button down from the unlocked state to eventual shutdown,
622 // then release the button while system does locking.
623 TEST_F(LockStateControllerTest
, CancelLockToShutdown
) {
624 Initialize(false, user::LOGGED_IN_USER
);
628 // Hold the power button and lock the screen.
629 EXPECT_TRUE(test_api_
->is_animating_lock());
631 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
);
633 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
, 0.5f
);
635 // Power button is released while system attempts to lock.
636 ReleasePowerButton();
638 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
640 EXPECT_FALSE(lock_state_controller_
->ShutdownRequested());
641 EXPECT_FALSE(test_api_
->lock_to_shutdown_timer_is_running());
642 EXPECT_FALSE(test_api_
->shutdown_timer_is_running());
645 // TODO(bruthig): Investigate why this hangs on Windows 8 and whether it can be
646 // safely enabled on OS_WIN.
648 // Test that we handle the case where lock requests are ignored.
649 TEST_F(LockStateControllerTest
, Lock
) {
650 Initialize(false, user::LOGGED_IN_USER
);
652 // Hold the power button and lock the screen.
654 ExpectPreLockAnimationStarted();
656 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
);
658 EXPECT_EQ(1, lock_state_controller_delegate_
->num_lock_requests());
659 EXPECT_TRUE(test_api_
->lock_fail_timer_is_running());
660 // We shouldn't start the lock-to-shutdown timer until the screen has actually
661 // been locked and this was animated.
662 EXPECT_FALSE(test_api_
->lock_to_shutdown_timer_is_running());
664 // Act as if the request timed out.
665 EXPECT_DEATH(test_api_
->trigger_lock_fail_timeout(), "");
669 // Test the basic operation of the lock button (not logged in).
670 TEST_F(LockStateControllerTest
, LockButtonBasicNotLoggedIn
) {
671 // The lock button shouldn't do anything if we aren't logged in.
672 Initialize(false, user::LOGGED_IN_NONE
);
675 EXPECT_FALSE(test_api_
->is_animating_lock());
677 EXPECT_EQ(0, lock_state_controller_delegate_
->num_lock_requests());
680 // Test the basic operation of the lock button (guest).
681 TEST_F(LockStateControllerTest
, LockButtonBasicGuest
) {
682 // The lock button shouldn't do anything when we're logged in as a guest.
683 Initialize(false, user::LOGGED_IN_GUEST
);
686 EXPECT_FALSE(test_api_
->is_animating_lock());
688 EXPECT_EQ(0, lock_state_controller_delegate_
->num_lock_requests());
691 // Test the basic operation of the lock button.
692 TEST_F(LockStateControllerTest
, LockButtonBasic
) {
693 // If we're logged in as a regular user, we should start the lock timer and
694 // the pre-lock animation.
695 Initialize(false, user::LOGGED_IN_USER
);
698 ExpectPreLockAnimationStarted();
699 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
, 0.5f
);
701 // If the button is released immediately, we shouldn't lock the screen.
703 ExpectPreLockAnimationCancel();
704 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
706 ExpectUnlockedState();
707 EXPECT_EQ(0, lock_state_controller_delegate_
->num_lock_requests());
709 // Press the button again and let the lock timeout fire. We should request
710 // that the screen be locked.
712 ExpectPreLockAnimationStarted();
713 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
);
714 EXPECT_EQ(1, lock_state_controller_delegate_
->num_lock_requests());
716 // Pressing the lock button while we have a pending lock request shouldn't do
720 ExpectPreLockAnimationFinished();
723 // Pressing the button also shouldn't do anything after the screen is locked.
725 ExpectPostLockAnimationStarted();
729 ExpectPostLockAnimationStarted();
731 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
732 ExpectPostLockAnimationFinished();
736 ExpectPostLockAnimationFinished();
739 // Test that the power button takes priority over the lock button.
740 TEST_F(LockStateControllerTest
, PowerButtonPreemptsLockButton
) {
741 Initialize(false, user::LOGGED_IN_USER
);
743 // While the lock button is down, hold the power button.
745 ExpectPreLockAnimationStarted();
747 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
, 0.1f
);
748 ExpectPreLockAnimationRunning();
751 ExpectPreLockAnimationRunning();
753 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
, 0.1f
);
754 ExpectPreLockAnimationRunning();
756 // The lock timer shouldn't be stopped when the lock button is released.
758 ExpectPreLockAnimationRunning();
760 ReleasePowerButton();
761 ExpectPreLockAnimationCancel();
763 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
764 ExpectUnlockedState();
766 // Now press the power button first and then the lock button.
768 ExpectPreLockAnimationStarted();
770 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
, 0.1f
);
773 ExpectPreLockAnimationRunning();
775 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
, 0.1f
);
777 // Releasing the power button should stop the lock timer.
778 ReleasePowerButton();
779 ExpectPreLockAnimationCancel();
781 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
, 0.1f
);
784 ExpectPreLockAnimationCancel();
787 // When the screen is locked without going through the usual power-button
788 // slow-close path (e.g. via the wrench menu), test that we still show the
789 // fast-close animation.
790 TEST_F(LockStateControllerTest
, LockWithoutButton
) {
791 Initialize(false, user::LOGGED_IN_USER
);
792 lock_state_controller_
->OnStartingLock();
794 ExpectPreLockAnimationStarted();
795 EXPECT_FALSE(test_api_
->is_lock_cancellable());
796 EXPECT_LT(0u, test_animator_
->GetAnimationCount());
798 test_animator_
->CompleteAllAnimations(true);
799 EXPECT_EQ(0, lock_state_controller_delegate_
->num_lock_requests());
802 // When we hear that the process is exiting but we haven't had a chance to
803 // display an animation, we should just blank the screen.
804 TEST_F(LockStateControllerTest
, ShutdownWithoutButton
) {
805 Initialize(false, user::LOGGED_IN_USER
);
806 lock_state_controller_
->OnAppTerminating();
809 test_animator_
->AreContainersAnimated(
810 SessionStateAnimator::kAllNonRootContainersMask
,
811 SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY
));
812 GenerateMouseMoveEvent();
813 EXPECT_FALSE(cursor_visible());
816 // Test that we display the fast-close animation and shut down when we get an
817 // outside request to shut down (e.g. from the login or lock screen).
818 TEST_F(LockStateControllerTest
, RequestShutdownFromLoginScreen
) {
819 Initialize(false, user::LOGGED_IN_NONE
);
821 lock_state_controller_
->RequestShutdown();
823 ExpectShutdownAnimationStarted();
824 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN
);
826 GenerateMouseMoveEvent();
827 EXPECT_FALSE(cursor_visible());
829 EXPECT_EQ(0, NumShutdownRequests());
830 EXPECT_TRUE(test_api_
->real_shutdown_timer_is_running());
831 test_api_
->trigger_real_shutdown_timeout();
832 EXPECT_EQ(1, NumShutdownRequests());
835 TEST_F(LockStateControllerTest
, RequestShutdownFromLockScreen
) {
836 Initialize(false, user::LOGGED_IN_USER
);
840 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN
);
841 ExpectPostLockAnimationFinished();
843 lock_state_controller_
->RequestShutdown();
845 ExpectShutdownAnimationStarted();
846 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN
);
848 GenerateMouseMoveEvent();
849 EXPECT_FALSE(cursor_visible());
851 EXPECT_EQ(0, NumShutdownRequests());
852 EXPECT_TRUE(test_api_
->real_shutdown_timer_is_running());
853 test_api_
->trigger_real_shutdown_timeout();
854 EXPECT_EQ(1, NumShutdownRequests());
857 TEST_F(LockStateControllerTest
, RequestAndCancelShutdownFromLockScreen
) {
858 Initialize(false, user::LOGGED_IN_USER
);
861 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN
);
864 // Press the power button and check that we start the shutdown timer.
866 EXPECT_FALSE(test_api_
->is_animating_lock());
867 EXPECT_TRUE(test_api_
->shutdown_timer_is_running());
869 ExpectShutdownAnimationStarted();
871 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN
, 0.5f
);
873 // Release the power button before the shutdown timer fires.
874 ReleasePowerButton();
876 EXPECT_FALSE(test_api_
->shutdown_timer_is_running());
878 ExpectShutdownAnimationCancel();
880 Advance(SessionStateAnimator::ANIMATION_SPEED_REVERT_SHUTDOWN
);
884 // Test that we ignore power button presses when the screen is turned off.
885 TEST_F(LockStateControllerTest
, IgnorePowerButtonIfScreenIsOff
) {
886 Initialize(false, user::LOGGED_IN_USER
);
888 // When the screen brightness is at 0%, we shouldn't do anything in response
889 // to power button presses.
890 power_button_controller_
->OnScreenBrightnessChanged(0.0);
892 EXPECT_FALSE(test_api_
->is_animating_lock());
893 ReleasePowerButton();
895 // After increasing the brightness to 10%, we should start the timer like
897 power_button_controller_
->OnScreenBrightnessChanged(10.0);
899 EXPECT_TRUE(test_api_
->is_animating_lock());
900 ReleasePowerButton();
903 #if defined(OS_CHROMEOS)
904 TEST_F(LockStateControllerTest
, HonorPowerButtonInDockedMode
) {
905 ScopedVector
<const ui::DisplayMode
> modes
;
906 modes
.push_back(new ui::DisplayMode(gfx::Size(1, 1), false, 60.0f
));
908 // Create two outputs, the first internal and the second external.
909 ui::DisplayConfigurator::DisplayStateList outputs
;
910 ui::DisplayConfigurator::DisplayState internal_output
;
911 ui::TestDisplaySnapshot internal_display
;
912 internal_display
.set_type(ui::DISPLAY_CONNECTION_TYPE_INTERNAL
);
913 internal_display
.set_modes(modes
.get());
914 internal_output
.display
= &internal_display
;
915 outputs
.push_back(internal_output
);
917 ui::DisplayConfigurator::DisplayState external_output
;
918 ui::TestDisplaySnapshot external_display
;
919 external_display
.set_type(ui::DISPLAY_CONNECTION_TYPE_HDMI
);
920 external_display
.set_modes(modes
.get());
921 external_output
.display
= &external_display
;
922 outputs
.push_back(external_output
);
924 // When all of the displays are turned off (e.g. due to user inactivity), the
925 // power button should be ignored.
926 power_button_controller_
->OnScreenBrightnessChanged(0.0);
927 static_cast<ui::TestDisplaySnapshot
*>(outputs
[0].display
)
928 ->set_current_mode(NULL
);
929 static_cast<ui::TestDisplaySnapshot
*>(outputs
[1].display
)
930 ->set_current_mode(NULL
);
931 power_button_controller_
->OnDisplayModeChanged(outputs
);
933 EXPECT_FALSE(test_api_
->is_animating_lock());
934 ReleasePowerButton();
936 // When the screen brightness is 0% but the external display is still turned
937 // on (indicating either docked mode or the user having manually decreased the
938 // brightness to 0%), the power button should still be handled.
939 static_cast<ui::TestDisplaySnapshot
*>(outputs
[1].display
)
940 ->set_current_mode(modes
[0]);
941 power_button_controller_
->OnDisplayModeChanged(outputs
);
943 EXPECT_TRUE(test_api_
->is_animating_lock());
944 ReleasePowerButton();
948 // Test that hidden background appears and revers correctly on lock/cancel.
949 TEST_F(LockStateControllerTest
, TestHiddenBackgroundLockCancel
) {
950 Initialize(false, user::LOGGED_IN_USER
);
953 ExpectUnlockedState();
956 ExpectPreLockAnimationStarted();
957 ExpectBackgroundIsShowing();
959 // Forward only half way through.
960 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
, 0.5f
);
962 // Release the button before the lock timer fires.
963 ReleasePowerButton();
964 ExpectPreLockAnimationCancel();
965 ExpectBackgroundIsHiding();
967 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDO_MOVE_WINDOWS
);
969 // When the CancelPrelockAnimation sequence finishes it queues up a
970 // restore background visibilty sequence when the background is hidden.
971 ExpectRestoringBackgroundVisibility();
973 Advance(SessionStateAnimator::ANIMATION_SPEED_IMMEDIATE
);
975 ExpectUnlockedState();
978 // Test that hidden background appears and revers correctly on lock/unlock.
979 TEST_F(LockStateControllerTest
, TestHiddenBackgroundLockUnlock
) {
980 Initialize(false, user::LOGGED_IN_USER
);
983 ExpectUnlockedState();
985 // Press the power button and check that the lock timer is started and that we
986 // start lifting the non-screen-locker containers.
989 ExpectPreLockAnimationStarted();
990 ExpectBackgroundIsShowing();
992 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
);
994 ExpectPreLockAnimationFinished();
998 ReleasePowerButton();
1000 ExpectPostLockAnimationStarted();
1001 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
1002 ExpectPostLockAnimationFinished();
1004 ExpectLockedState();
1006 SuccessfulAuthentication(NULL
);
1008 ExpectUnlockBeforeUIDestroyedAnimationStarted();
1009 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
1010 ExpectUnlockBeforeUIDestroyedAnimationFinished();
1014 ExpectUnlockAfterUIDestroyedAnimationStarted();
1015 ExpectBackgroundIsHiding();
1017 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
1019 // When the StartUnlockAnimationAfterUIDestroyed sequence finishes it queues
1020 // up a restore background visibilty sequence when the background is hidden.
1021 ExpectRestoringBackgroundVisibility();
1023 Advance(SessionStateAnimator::ANIMATION_SPEED_IMMEDIATE
);
1025 ExpectUnlockAfterUIDestroyedAnimationFinished();
1027 ExpectUnlockedState();
1030 TEST_F(LockStateControllerTest
, Screenshot
) {
1031 test::TestScreenshotDelegate
* delegate
= GetScreenshotDelegate();
1032 delegate
->set_can_take_screenshot(true);
1034 EnableMaximizeMode(false);
1036 // Screenshot handling should not be active when not in maximize mode.
1037 ASSERT_EQ(0, delegate
->handle_take_screenshot_count());
1040 ReleasePowerButton();
1041 ReleaseVolumeDown();
1042 EXPECT_EQ(0, delegate
->handle_take_screenshot_count());
1044 EnableMaximizeMode(true);
1046 // Pressing power alone does not take a screenshot.
1048 ReleasePowerButton();
1049 EXPECT_EQ(0, delegate
->handle_take_screenshot_count());
1051 // Press & release volume then pressing power does not take a screenshot.
1052 ASSERT_EQ(0, delegate
->handle_take_screenshot_count());
1054 ReleaseVolumeDown();
1056 ReleasePowerButton();
1057 EXPECT_EQ(0, delegate
->handle_take_screenshot_count());
1059 // Pressing power and then volume does not take a screenshot.
1060 ASSERT_EQ(0, delegate
->handle_take_screenshot_count());
1062 ReleasePowerButton();
1064 ReleaseVolumeDown();
1065 EXPECT_EQ(0, delegate
->handle_take_screenshot_count());
1067 // Holding volume down and pressing power takes a screenshot.
1068 ASSERT_EQ(0, delegate
->handle_take_screenshot_count());
1071 ReleasePowerButton();
1072 ReleaseVolumeDown();
1073 EXPECT_EQ(1, delegate
->handle_take_screenshot_count());
1076 // Tests that a lock action is cancellable when quick lock is turned on and
1077 // maximize mode is not active.
1078 TEST_F(LockStateControllerTest
, QuickLockWhileNotInMaximizeMode
) {
1079 Initialize(false, user::LOGGED_IN_USER
);
1080 power_button_controller_
->set_enable_quick_lock_for_test(true);
1081 EnableMaximizeMode(false);
1085 ExpectPreLockAnimationStarted();
1086 EXPECT_TRUE(test_api_
->is_animating_lock());
1087 EXPECT_TRUE(lock_state_controller_
->CanCancelLockAnimation());
1089 ReleasePowerButton();
1091 EXPECT_EQ(0, lock_state_controller_delegate_
->num_lock_requests());
1094 // Tests that a lock action is not cancellable when quick lock is turned on and
1095 // maximize mode is active.
1096 TEST_F(LockStateControllerTest
, QuickLockWhileInMaximizeMode
) {
1097 Initialize(false, user::LOGGED_IN_USER
);
1098 power_button_controller_
->set_enable_quick_lock_for_test(true);
1099 EnableMaximizeMode(true);
1103 ExpectPreLockAnimationStarted();
1104 EXPECT_TRUE(test_api_
->is_animating_lock());
1105 EXPECT_FALSE(lock_state_controller_
->CanCancelLockAnimation());
1107 ReleasePowerButton();
1109 ExpectPreLockAnimationStarted();
1111 test_animator_
->CompleteAllAnimations(true);
1112 EXPECT_EQ(1, lock_state_controller_delegate_
->num_lock_requests());