Update V8 to version 4.7.47.
[chromium-blink-merge.git] / ash / wm / lock_state_controller_unittest.cc
blobfa2485654b8c6dda55612186c2252b7e24ee9d94
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"
8 #include "ash/shell.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"
27 #endif
29 namespace ash {
30 namespace test {
31 namespace {
33 bool cursor_visible() {
34 return ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible();
37 void CheckCalledCallback(bool* flag) {
38 if (flag)
39 (*flag) = true;
42 } // namespace
44 class LockStateControllerTest : public AshTestBase {
45 public:
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 {
54 AshTestBase::SetUp();
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());
73 protected:
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,
89 float factor) {
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());
99 EXPECT_TRUE(
100 test_animator_->AreContainersAnimated(
101 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
102 SessionStateAnimator::ANIMATION_LIFT));
103 EXPECT_TRUE(
104 test_animator_->AreContainersAnimated(
105 SessionStateAnimator::LAUNCHER,
106 SessionStateAnimator::ANIMATION_FADE_OUT));
107 EXPECT_TRUE(
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());
117 EXPECT_TRUE(
118 test_animator_->AreContainersAnimated(
119 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
120 SessionStateAnimator::ANIMATION_LIFT));
121 EXPECT_TRUE(
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());
131 EXPECT_TRUE(
132 test_animator_->AreContainersAnimated(
133 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
134 SessionStateAnimator::ANIMATION_UNDO_LIFT));
135 EXPECT_TRUE(
136 test_animator_->AreContainersAnimated(
137 SessionStateAnimator::LAUNCHER,
138 SessionStateAnimator::ANIMATION_FADE_IN));
141 void ExpectPreLockAnimationFinished() {
142 SCOPED_TRACE("Failure in ExpectPreLockAnimationFinished");
143 EXPECT_FALSE(
144 test_animator_->AreContainersAnimated(
145 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
146 SessionStateAnimator::ANIMATION_LIFT));
147 EXPECT_FALSE(
148 test_animator_->AreContainersAnimated(
149 SessionStateAnimator::LAUNCHER,
150 SessionStateAnimator::ANIMATION_FADE_OUT));
151 EXPECT_FALSE(
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());
160 EXPECT_TRUE(
161 test_animator_->AreContainersAnimated(
162 SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
163 SessionStateAnimator::ANIMATION_RAISE_TO_SCREEN));
166 void ExpectPostLockAnimationFinished() {
167 SCOPED_TRACE("Failure in ExpectPostLockAnimationFinished");
168 EXPECT_FALSE(
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());
177 EXPECT_TRUE(
178 test_animator_->AreContainersAnimated(
179 SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
180 SessionStateAnimator::ANIMATION_LIFT));
183 void ExpectUnlockBeforeUIDestroyedAnimationFinished() {
184 SCOPED_TRACE("Failure in ExpectUnlockBeforeUIDestroyedAnimationFinished");
185 EXPECT_FALSE(
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());
194 EXPECT_TRUE(
195 test_animator_->AreContainersAnimated(
196 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
197 SessionStateAnimator::ANIMATION_DROP));
198 EXPECT_TRUE(
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());
207 EXPECT_FALSE(
208 test_animator_->AreContainersAnimated(
209 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
210 SessionStateAnimator::ANIMATION_DROP));
211 EXPECT_FALSE(
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());
220 EXPECT_TRUE(
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());
229 EXPECT_FALSE(
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());
238 EXPECT_TRUE(
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());
247 EXPECT_TRUE(
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());
256 EXPECT_TRUE(
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());
265 EXPECT_TRUE(
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);
311 void SystemLocks() {
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(
333 legacy_button);
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
350 private:
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
357 // state.
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.
365 PressPowerButton();
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());
379 SystemLocks();
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.
392 PressPowerButton();
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);
411 PressPowerButton();
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);
422 PressPowerButton();
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.
434 PressPowerButton();
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.
451 PressPowerButton();
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.
476 PressPowerButton();
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());
492 SystemLocks();
494 ExpectPostLockAnimationStarted();
495 test_animator_->CompleteAllAnimations(true);
496 ExpectPostLockAnimationFinished();
498 // When we release the power button, the lock-to-shutdown timer should be
499 // stopped.
500 ExpectLockedState();
501 EXPECT_TRUE(test_api_->lock_to_shutdown_timer_is_running());
502 ReleasePowerButton();
503 ExpectLockedState();
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.
508 bool called = false;
509 SuccessfulAuthentication(&called);
511 ExpectUnlockBeforeUIDestroyedAnimationStarted();
512 EXPECT_FALSE(called);
513 test_animator_->CompleteAllAnimations(true);
514 ExpectUnlockBeforeUIDestroyedAnimationFinished();
516 EXPECT_TRUE(called);
518 SystemUnlocks();
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.
535 PressPowerButton();
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.
561 PressPowerButton();
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,
574 0.5f);
576 PressPowerButton();
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.
594 PressPowerButton();
595 EXPECT_TRUE(test_api_->is_animating_lock());
597 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
598 SystemLocks();
599 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
601 // When the lock-to-shutdown timeout fires, we should start the shutdown
602 // timer.
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);
626 PressPowerButton();
628 // Hold the power button and lock the screen.
629 EXPECT_TRUE(test_api_->is_animating_lock());
631 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
632 SystemLocks();
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.
647 #ifndef 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.
653 PressPowerButton();
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(), "");
667 #endif
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);
674 PressLockButton();
675 EXPECT_FALSE(test_api_->is_animating_lock());
676 ReleaseLockButton();
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);
685 PressLockButton();
686 EXPECT_FALSE(test_api_->is_animating_lock());
687 ReleaseLockButton();
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);
697 PressLockButton();
698 ExpectPreLockAnimationStarted();
699 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
701 // If the button is released immediately, we shouldn't lock the screen.
702 ReleaseLockButton();
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.
711 PressLockButton();
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
717 // anything.
718 ReleaseLockButton();
719 PressLockButton();
720 ExpectPreLockAnimationFinished();
721 ReleaseLockButton();
723 // Pressing the button also shouldn't do anything after the screen is locked.
724 SystemLocks();
725 ExpectPostLockAnimationStarted();
727 PressLockButton();
728 ReleaseLockButton();
729 ExpectPostLockAnimationStarted();
731 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
732 ExpectPostLockAnimationFinished();
734 PressLockButton();
735 ReleaseLockButton();
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.
744 PressLockButton();
745 ExpectPreLockAnimationStarted();
747 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.1f);
748 ExpectPreLockAnimationRunning();
750 PressPowerButton();
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.
757 ReleaseLockButton();
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.
767 PressPowerButton();
768 ExpectPreLockAnimationStarted();
770 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.1f);
772 PressLockButton();
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);
783 ReleaseLockButton();
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();
808 EXPECT_TRUE(
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);
838 SystemLocks();
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);
860 SystemLocks();
861 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
862 ExpectLockedState();
864 // Press the power button and check that we start the shutdown timer.
865 PressPowerButton();
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);
881 ExpectLockedState();
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);
891 PressPowerButton();
892 EXPECT_FALSE(test_api_->is_animating_lock());
893 ReleasePowerButton();
895 // After increasing the brightness to 10%, we should start the timer like
896 // usual.
897 power_button_controller_->OnScreenBrightnessChanged(10.0);
898 PressPowerButton();
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::TestDisplaySnapshot internal_display;
911 internal_display.set_type(ui::DISPLAY_CONNECTION_TYPE_INTERNAL);
912 internal_display.set_modes(modes.get());
913 outputs.push_back(&internal_display);
915 ui::TestDisplaySnapshot external_display;
916 external_display.set_type(ui::DISPLAY_CONNECTION_TYPE_HDMI);
917 external_display.set_modes(modes.get());
918 outputs.push_back(&external_display);
920 // When all of the displays are turned off (e.g. due to user inactivity), the
921 // power button should be ignored.
922 power_button_controller_->OnScreenBrightnessChanged(0.0);
923 internal_display.set_current_mode(nullptr);
924 external_display.set_current_mode(nullptr);
925 power_button_controller_->OnDisplayModeChanged(outputs);
926 PressPowerButton();
927 EXPECT_FALSE(test_api_->is_animating_lock());
928 ReleasePowerButton();
930 // When the screen brightness is 0% but the external display is still turned
931 // on (indicating either docked mode or the user having manually decreased the
932 // brightness to 0%), the power button should still be handled.
933 external_display.set_current_mode(modes[0]);
934 power_button_controller_->OnDisplayModeChanged(outputs);
935 PressPowerButton();
936 EXPECT_TRUE(test_api_->is_animating_lock());
937 ReleasePowerButton();
939 #endif
941 // Test that hidden background appears and revers correctly on lock/cancel.
942 TEST_F(LockStateControllerTest, TestHiddenBackgroundLockCancel) {
943 Initialize(false, user::LOGGED_IN_USER);
944 HideBackground();
946 ExpectUnlockedState();
947 PressPowerButton();
949 ExpectPreLockAnimationStarted();
950 ExpectBackgroundIsShowing();
952 // Forward only half way through.
953 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
955 // Release the button before the lock timer fires.
956 ReleasePowerButton();
957 ExpectPreLockAnimationCancel();
958 ExpectBackgroundIsHiding();
960 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDO_MOVE_WINDOWS);
962 // When the CancelPrelockAnimation sequence finishes it queues up a
963 // restore background visibilty sequence when the background is hidden.
964 ExpectRestoringBackgroundVisibility();
966 Advance(SessionStateAnimator::ANIMATION_SPEED_IMMEDIATE);
968 ExpectUnlockedState();
971 // Test that hidden background appears and revers correctly on lock/unlock.
972 TEST_F(LockStateControllerTest, TestHiddenBackgroundLockUnlock) {
973 Initialize(false, user::LOGGED_IN_USER);
974 HideBackground();
976 ExpectUnlockedState();
978 // Press the power button and check that the lock timer is started and that we
979 // start lifting the non-screen-locker containers.
980 PressPowerButton();
982 ExpectPreLockAnimationStarted();
983 ExpectBackgroundIsShowing();
985 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
987 ExpectPreLockAnimationFinished();
989 SystemLocks();
991 ReleasePowerButton();
993 ExpectPostLockAnimationStarted();
994 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
995 ExpectPostLockAnimationFinished();
997 ExpectLockedState();
999 SuccessfulAuthentication(NULL);
1001 ExpectUnlockBeforeUIDestroyedAnimationStarted();
1002 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
1003 ExpectUnlockBeforeUIDestroyedAnimationFinished();
1005 SystemUnlocks();
1007 ExpectUnlockAfterUIDestroyedAnimationStarted();
1008 ExpectBackgroundIsHiding();
1010 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
1012 // When the StartUnlockAnimationAfterUIDestroyed sequence finishes it queues
1013 // up a restore background visibilty sequence when the background is hidden.
1014 ExpectRestoringBackgroundVisibility();
1016 Advance(SessionStateAnimator::ANIMATION_SPEED_IMMEDIATE);
1018 ExpectUnlockAfterUIDestroyedAnimationFinished();
1020 ExpectUnlockedState();
1023 TEST_F(LockStateControllerTest, Screenshot) {
1024 test::TestScreenshotDelegate* delegate = GetScreenshotDelegate();
1025 delegate->set_can_take_screenshot(true);
1027 EnableMaximizeMode(false);
1029 // Screenshot handling should not be active when not in maximize mode.
1030 ASSERT_EQ(0, delegate->handle_take_screenshot_count());
1031 PressVolumeDown();
1032 PressPowerButton();
1033 ReleasePowerButton();
1034 ReleaseVolumeDown();
1035 EXPECT_EQ(0, delegate->handle_take_screenshot_count());
1037 EnableMaximizeMode(true);
1039 // Pressing power alone does not take a screenshot.
1040 PressPowerButton();
1041 ReleasePowerButton();
1042 EXPECT_EQ(0, delegate->handle_take_screenshot_count());
1044 // Press & release volume then pressing power does not take a screenshot.
1045 ASSERT_EQ(0, delegate->handle_take_screenshot_count());
1046 PressVolumeDown();
1047 ReleaseVolumeDown();
1048 PressPowerButton();
1049 ReleasePowerButton();
1050 EXPECT_EQ(0, delegate->handle_take_screenshot_count());
1052 // Pressing power and then volume does not take a screenshot.
1053 ASSERT_EQ(0, delegate->handle_take_screenshot_count());
1054 PressPowerButton();
1055 ReleasePowerButton();
1056 PressVolumeDown();
1057 ReleaseVolumeDown();
1058 EXPECT_EQ(0, delegate->handle_take_screenshot_count());
1060 // Holding volume down and pressing power takes a screenshot.
1061 ASSERT_EQ(0, delegate->handle_take_screenshot_count());
1062 PressVolumeDown();
1063 PressPowerButton();
1064 ReleasePowerButton();
1065 ReleaseVolumeDown();
1066 EXPECT_EQ(1, delegate->handle_take_screenshot_count());
1069 // Tests that a lock action is cancellable when quick lock is turned on and
1070 // maximize mode is not active.
1071 TEST_F(LockStateControllerTest, QuickLockWhileNotInMaximizeMode) {
1072 Initialize(false, user::LOGGED_IN_USER);
1073 power_button_controller_->set_enable_quick_lock_for_test(true);
1074 EnableMaximizeMode(false);
1076 PressPowerButton();
1078 ExpectPreLockAnimationStarted();
1079 EXPECT_TRUE(test_api_->is_animating_lock());
1080 EXPECT_TRUE(lock_state_controller_->CanCancelLockAnimation());
1082 ReleasePowerButton();
1084 EXPECT_EQ(0, lock_state_controller_delegate_->num_lock_requests());
1087 // Tests that a lock action is not cancellable when quick lock is turned on and
1088 // maximize mode is active.
1089 TEST_F(LockStateControllerTest, QuickLockWhileInMaximizeMode) {
1090 Initialize(false, user::LOGGED_IN_USER);
1091 power_button_controller_->set_enable_quick_lock_for_test(true);
1092 EnableMaximizeMode(true);
1094 PressPowerButton();
1096 ExpectPreLockAnimationStarted();
1097 EXPECT_TRUE(test_api_->is_animating_lock());
1098 EXPECT_FALSE(lock_state_controller_->CanCancelLockAnimation());
1100 ReleasePowerButton();
1102 ExpectPreLockAnimationStarted();
1104 test_animator_->CompleteAllAnimations(true);
1105 EXPECT_EQ(1, lock_state_controller_delegate_->num_lock_requests());
1108 } // namespace test
1109 } // namespace ash