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/ash_switches.h"
8 #include "ash/session_state_delegate.h"
10 #include "ash/shell_window_ids.h"
11 #include "ash/test/ash_test_base.h"
12 #include "ash/test/test_shell_delegate.h"
13 #include "ash/wm/power_button_controller.h"
14 #include "ash/wm/session_state_animator.h"
15 #include "base/command_line.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/time/time.h"
18 #include "ui/aura/env.h"
19 #include "ui/aura/root_window.h"
20 #include "ui/aura/test/event_generator.h"
21 #include "ui/compositor/layer_animator.h"
22 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
23 #include "ui/compositor/scoped_layer_animation_settings.h"
24 #include "ui/gfx/rect.h"
25 #include "ui/gfx/size.h"
28 #include "base/win/windows_version.h"
33 using internal::SessionStateAnimator
;
39 bool cursor_visible() {
40 return ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible();
43 void CheckCalledCallback(bool* flag
) {
48 aura::Window
* GetContainer(int container
) {
49 aura::Window
* root_window
= Shell::GetPrimaryRootWindow();
50 return Shell::GetContainer(root_window
, container
);
53 bool IsBackgroundHidden() {
54 return !GetContainer(internal::kShellWindowId_DesktopBackgroundContainer
)->
58 void HideBackground() {
59 ui::ScopedLayerAnimationSettings
settings(
60 GetContainer(internal::kShellWindowId_DesktopBackgroundContainer
)->
61 layer()->GetAnimator());
62 settings
.SetTransitionDuration(base::TimeDelta());
63 GetContainer(internal::kShellWindowId_DesktopBackgroundContainer
)->Hide();
68 // Fake implementation of PowerButtonControllerDelegate that just logs requests
69 // to lock the screen and shut down the device.
70 class TestLockStateControllerDelegate
: public LockStateControllerDelegate
{
72 TestLockStateControllerDelegate()
73 : num_lock_requests_(0),
74 num_shutdown_requests_(0) {}
76 int num_lock_requests() const { return num_lock_requests_
; }
77 int num_shutdown_requests() const { return num_shutdown_requests_
; }
79 // LockStateControllerDelegate implementation.
80 virtual void RequestLockScreen() OVERRIDE
{
83 virtual void RequestShutdown() OVERRIDE
{
84 num_shutdown_requests_
++;
88 int num_lock_requests_
;
89 int num_shutdown_requests_
;
91 DISALLOW_COPY_AND_ASSIGN(TestLockStateControllerDelegate
);
94 class LockStateControllerTest
: public AshTestBase
{
96 LockStateControllerTest() : controller_(NULL
), delegate_(NULL
) {}
97 virtual ~LockStateControllerTest() {}
99 virtual void SetUp() OVERRIDE
{
100 AshTestBase::SetUp();
102 // We would control animations in a fine way:
103 animation_duration_mode_
.reset(new ui::ScopedAnimationDurationScaleMode(
104 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION
));
105 // TODO(antrim) : restore
106 // animator_helper_ = ui::test::CreateLayerAnimatorHelperForTest();
108 // Temporary disable animations so that observer is always called, and
109 // no leaks happen during tests.
110 animation_duration_mode_
.reset(new ui::ScopedAnimationDurationScaleMode(
111 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION
));
112 // TODO(antrim): once there is a way to mock time and run animations, make
113 // sure that animations are finished even in simple tests.
115 delegate_
= new TestLockStateControllerDelegate
;
116 controller_
= Shell::GetInstance()->power_button_controller();
117 lock_state_controller_
= static_cast<LockStateController
*>(
118 Shell::GetInstance()->lock_state_controller());
119 lock_state_controller_
->SetDelegate(delegate_
); // transfers ownership
120 test_api_
.reset(new LockStateController::TestApi(lock_state_controller_
));
122 new SessionStateAnimator::TestApi(lock_state_controller_
->
124 shell_delegate_
= reinterpret_cast<TestShellDelegate
*>(
125 ash::Shell::GetInstance()->delegate());
126 session_state_delegate_
= Shell::GetInstance()->session_state_delegate();
129 virtual void TearDown() {
130 // TODO(antrim) : restore
131 // animator_helper_->AdvanceUntilDone();
132 AshTestBase::TearDown();
136 void GenerateMouseMoveEvent() {
137 aura::test::EventGenerator
generator(
138 Shell::GetPrimaryRootWindow());
139 generator
.MoveMouseTo(10, 10);
142 int NumShutdownRequests() {
143 return delegate_
->num_shutdown_requests() +
144 shell_delegate_
->num_exit_requests();
147 void Advance(SessionStateAnimator::AnimationSpeed speed
) {
148 // TODO (antrim) : restore
149 // animator_helper_->Advance(SessionStateAnimator::GetDuration(speed));
152 void AdvancePartially(SessionStateAnimator::AnimationSpeed speed
,
154 // TODO (antrim) : restore
155 // base::TimeDelta duration = SessionStateAnimator::GetDuration(speed);
156 // base::TimeDelta partial_duration =
157 // base::TimeDelta::FromInternalValue(duration.ToInternalValue() * factor);
158 // animator_helper_->Advance(partial_duration);
161 void ExpectPreLockAnimationStarted() {
162 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
164 animator_api_
->ContainersAreAnimated(
165 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS
,
166 SessionStateAnimator::ANIMATION_LIFT
));
168 animator_api_
->ContainersAreAnimated(
169 SessionStateAnimator::LAUNCHER
,
170 SessionStateAnimator::ANIMATION_FADE_OUT
));
172 animator_api_
->ContainersAreAnimated(
173 SessionStateAnimator::LOCK_SCREEN_CONTAINERS
,
174 SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY
));
175 EXPECT_TRUE(test_api_
->is_animating_lock());
178 void ExpectPreLockAnimationCancel() {
180 animator_api_
->ContainersAreAnimated(
181 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS
,
182 SessionStateAnimator::ANIMATION_DROP
));
184 animator_api_
->ContainersAreAnimated(
185 SessionStateAnimator::LAUNCHER
,
186 SessionStateAnimator::ANIMATION_FADE_IN
));
189 void ExpectPreLockAnimationFinished() {
190 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
192 animator_api_
->ContainersAreAnimated(
193 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS
,
194 SessionStateAnimator::ANIMATION_LIFT
));
196 animator_api_
->ContainersAreAnimated(
197 SessionStateAnimator::LAUNCHER
,
198 SessionStateAnimator::ANIMATION_FADE_OUT
));
200 animator_api_
->ContainersAreAnimated(
201 SessionStateAnimator::LOCK_SCREEN_CONTAINERS
,
202 SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY
));
205 void ExpectPostLockAnimationStarted() {
206 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
208 animator_api_
->ContainersAreAnimated(
209 SessionStateAnimator::LOCK_SCREEN_CONTAINERS
,
210 SessionStateAnimator::ANIMATION_RAISE_TO_SCREEN
));
213 void ExpectPastLockAnimationFinished() {
214 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
216 animator_api_
->ContainersAreAnimated(
217 SessionStateAnimator::LOCK_SCREEN_CONTAINERS
,
218 SessionStateAnimator::ANIMATION_RAISE_TO_SCREEN
));
221 void ExpectUnlockBeforeUIDestroyedAnimationStarted() {
222 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
224 animator_api_
->ContainersAreAnimated(
225 SessionStateAnimator::LOCK_SCREEN_CONTAINERS
,
226 SessionStateAnimator::ANIMATION_LIFT
));
229 void ExpectUnlockBeforeUIDestroyedAnimationFinished() {
230 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
232 animator_api_
->ContainersAreAnimated(
233 SessionStateAnimator::LOCK_SCREEN_CONTAINERS
,
234 SessionStateAnimator::ANIMATION_LIFT
));
237 void ExpectUnlockAfterUIDestroyedAnimationStarted() {
238 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
240 animator_api_
->ContainersAreAnimated(
241 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS
,
242 SessionStateAnimator::ANIMATION_DROP
));
244 animator_api_
->ContainersAreAnimated(
245 SessionStateAnimator::LAUNCHER
,
246 SessionStateAnimator::ANIMATION_FADE_IN
));
249 void ExpectUnlockAfterUIDestroyedAnimationFinished() {
250 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
252 animator_api_
->ContainersAreAnimated(
253 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS
,
254 SessionStateAnimator::ANIMATION_DROP
));
256 animator_api_
->ContainersAreAnimated(
257 SessionStateAnimator::LAUNCHER
,
258 SessionStateAnimator::ANIMATION_FADE_IN
));
261 void ExpectShutdownAnimationStarted() {
262 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
264 animator_api_
->RootWindowIsAnimated(
265 SessionStateAnimator::ANIMATION_GRAYSCALE_BRIGHTNESS
));
268 void ExpectShutdownAnimationFinished() {
269 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
271 animator_api_
->RootWindowIsAnimated(
272 SessionStateAnimator::ANIMATION_GRAYSCALE_BRIGHTNESS
));
275 void ExpectShutdownAnimationCancel() {
276 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
278 animator_api_
->RootWindowIsAnimated(
279 SessionStateAnimator::ANIMATION_UNDO_GRAYSCALE_BRIGHTNESS
));
282 void ExpectBackgroundIsShowing() {
283 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
285 animator_api_
->ContainersAreAnimated(
286 SessionStateAnimator::DESKTOP_BACKGROUND
,
287 SessionStateAnimator::ANIMATION_FADE_IN
));
290 void ExpectBackgroundIsHiding() {
291 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
293 animator_api_
->ContainersAreAnimated(
294 SessionStateAnimator::DESKTOP_BACKGROUND
,
295 SessionStateAnimator::ANIMATION_FADE_OUT
));
298 void ExpectUnlockedState() {
299 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
300 EXPECT_FALSE(session_state_delegate_
->IsScreenLocked());
302 aura::Window::Windows containers
;
304 SessionStateAnimator::GetContainers(
305 SessionStateAnimator::LAUNCHER
|
306 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS
,
308 for (aura::Window::Windows::const_iterator it
= containers
.begin();
309 it
!= containers
.end(); ++it
) {
310 aura::Window
* window
= *it
;
311 ui::Layer
* layer
= window
->layer();
312 EXPECT_EQ(1.0f
, layer
->opacity());
313 EXPECT_EQ(0.0f
, layer
->layer_brightness());
314 EXPECT_EQ(0.0f
, layer
->layer_saturation());
315 EXPECT_EQ(gfx::Transform(), layer
->transform());
319 void ExpectLockedState() {
320 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
321 EXPECT_TRUE(session_state_delegate_
->IsScreenLocked());
323 aura::Window::Windows containers
;
325 SessionStateAnimator::GetContainers(
326 SessionStateAnimator::LOCK_SCREEN_RELATED_CONTAINERS
|
327 SessionStateAnimator::LOCK_SCREEN_CONTAINERS
,
329 for (aura::Window::Windows::const_iterator it
= containers
.begin();
330 it
!= containers
.end(); ++it
) {
331 aura::Window
* window
= *it
;
332 ui::Layer
* layer
= window
->layer();
333 EXPECT_EQ(1.0f
, layer
->opacity());
334 EXPECT_EQ(0.0f
, layer
->layer_brightness());
335 EXPECT_EQ(0.0f
, layer
->layer_saturation());
336 EXPECT_EQ(gfx::Transform(), layer
->transform());
340 void PressPowerButton() {
341 controller_
->OnPowerButtonEvent(true, base::TimeTicks::Now());
342 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta());
345 void ReleasePowerButton() {
346 controller_
->OnPowerButtonEvent(false, base::TimeTicks::Now());
347 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta());
350 void PressLockButton() {
351 controller_
->OnLockButtonEvent(true, base::TimeTicks::Now());
354 void ReleaseLockButton() {
355 controller_
->OnLockButtonEvent(false, base::TimeTicks::Now());
359 lock_state_controller_
->OnLockStateChanged(true);
360 session_state_delegate_
->LockScreen();
361 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta());
364 void SuccessfulAuthentication(bool* call_flag
) {
365 base::Closure closure
= base::Bind(&CheckCalledCallback
, call_flag
);
366 lock_state_controller_
->OnLockScreenHide(closure
);
367 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta());
370 void SystemUnlocks() {
371 lock_state_controller_
->OnLockStateChanged(false);
372 session_state_delegate_
->UnlockScreen();
373 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta());
376 void Initialize(bool legacy_button
, user::LoginStatus status
) {
377 controller_
->set_has_legacy_power_button_for_test(legacy_button
);
378 lock_state_controller_
->OnLoginStateChanged(status
);
379 SetUserLoggedIn(status
!= user::LOGGED_IN_NONE
);
380 if (status
== user::LOGGED_IN_GUEST
)
381 SetCanLockScreen(false);
382 lock_state_controller_
->OnLockStateChanged(false);
385 PowerButtonController
* controller_
; // not owned
386 LockStateController
* lock_state_controller_
; // not owned
387 TestLockStateControllerDelegate
* delegate_
; // not owned
388 TestShellDelegate
* shell_delegate_
; // not owned
389 SessionStateDelegate
* session_state_delegate_
; // not owned
391 scoped_ptr
<ui::ScopedAnimationDurationScaleMode
> animation_duration_mode_
;
392 scoped_ptr
<LockStateController::TestApi
> test_api_
;
393 scoped_ptr
<SessionStateAnimator::TestApi
> animator_api_
;
394 // TODO(antrim) : restore
395 // scoped_ptr<ui::test::AnimationContainerTestHelper> animator_helper_;
398 DISALLOW_COPY_AND_ASSIGN(LockStateControllerTest
);
401 // Test the lock-to-shutdown flow for non-Chrome-OS hardware that doesn't
402 // correctly report power button releases. We should lock immediately the first
403 // time the button is pressed and shut down when it's pressed from the locked
405 // TODO(antrim): Reenable this: http://crbug.com/167048
406 TEST_F(LockStateControllerTest
, DISABLED_LegacyLockAndShutDown
) {
407 Initialize(true, user::LOGGED_IN_USER
);
409 ExpectUnlockedState();
411 // We should request that the screen be locked immediately after seeing the
412 // power button get pressed.
415 ExpectPreLockAnimationStarted();
417 EXPECT_FALSE(test_api_
->is_lock_cancellable());
419 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
421 ExpectPreLockAnimationFinished();
422 EXPECT_EQ(1, delegate_
->num_lock_requests());
424 // Notify that we locked successfully.
425 lock_state_controller_
->OnStartingLock();
426 // We had that animation already.
427 //TODO (antrim) : restore
428 // EXPECT_FALSE(animator_helper_->IsAnimating());
432 ExpectPostLockAnimationStarted();
433 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
434 ExpectPastLockAnimationFinished();
436 // We shouldn't progress towards the shutdown state, however.
437 EXPECT_FALSE(test_api_
->lock_to_shutdown_timer_is_running());
438 EXPECT_FALSE(test_api_
->shutdown_timer_is_running());
440 ReleasePowerButton();
442 // Hold the button again and check that we start shutting down.
445 ExpectShutdownAnimationStarted();
447 EXPECT_EQ(0, NumShutdownRequests());
448 // Make sure a mouse move event won't show the cursor.
449 GenerateMouseMoveEvent();
450 EXPECT_FALSE(cursor_visible());
452 EXPECT_TRUE(test_api_
->real_shutdown_timer_is_running());
453 test_api_
->trigger_real_shutdown_timeout();
454 EXPECT_EQ(1, NumShutdownRequests());
457 // Test that we start shutting down immediately if the power button is pressed
458 // while we're not logged in on an unofficial system.
459 TEST_F(LockStateControllerTest
, LegacyNotLoggedIn
) {
460 Initialize(true, user::LOGGED_IN_NONE
);
463 ExpectShutdownAnimationStarted();
465 EXPECT_TRUE(test_api_
->real_shutdown_timer_is_running());
468 // Test that we start shutting down immediately if the power button is pressed
469 // while we're logged in as a guest on an unofficial system.
470 TEST_F(LockStateControllerTest
, LegacyGuest
) {
471 Initialize(true, user::LOGGED_IN_GUEST
);
474 ExpectShutdownAnimationStarted();
476 EXPECT_TRUE(test_api_
->real_shutdown_timer_is_running());
479 // When we hold the power button while the user isn't logged in, we should shut
480 // down the machine directly.
481 TEST_F(LockStateControllerTest
, ShutdownWhenNotLoggedIn
) {
482 Initialize(false, user::LOGGED_IN_NONE
);
484 // Press the power button and check that we start the shutdown timer.
486 EXPECT_FALSE(test_api_
->is_animating_lock());
487 EXPECT_TRUE(test_api_
->shutdown_timer_is_running());
488 ExpectShutdownAnimationStarted();
490 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN
, 0.5f
);
492 // Release the power button before the shutdown timer fires.
493 ReleasePowerButton();
495 EXPECT_FALSE(test_api_
->shutdown_timer_is_running());
496 ExpectShutdownAnimationCancel();
498 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_REVERT
, 0.5f
);
500 // Press the button again and make the shutdown timeout fire this time.
501 // Check that we start the timer for actually requesting the shutdown.
504 EXPECT_TRUE(test_api_
->shutdown_timer_is_running());
506 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN
);
507 ExpectShutdownAnimationFinished();
508 test_api_
->trigger_shutdown_timeout();
510 EXPECT_TRUE(test_api_
->real_shutdown_timer_is_running());
511 EXPECT_EQ(0, NumShutdownRequests());
513 // When the timout fires, we should request a shutdown.
514 test_api_
->trigger_real_shutdown_timeout();
516 EXPECT_EQ(1, NumShutdownRequests());
519 // Test that we lock the screen and deal with unlocking correctly.
520 // TODO(antrim): Reenable this: http://crbug.com/167048
521 TEST_F(LockStateControllerTest
, DISABLED_LockAndUnlock
) {
522 Initialize(false, user::LOGGED_IN_USER
);
524 ExpectUnlockedState();
526 // Press the power button and check that the lock timer is started and that we
527 // start lifting the non-screen-locker containers.
530 ExpectPreLockAnimationStarted();
531 EXPECT_TRUE(test_api_
->is_lock_cancellable());
532 EXPECT_EQ(0, delegate_
->num_lock_requests());
534 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
);
535 ExpectPreLockAnimationFinished();
537 EXPECT_EQ(1, delegate_
->num_lock_requests());
539 // Notify that we locked successfully.
540 lock_state_controller_
->OnStartingLock();
541 // We had that animation already.
542 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
546 ExpectPostLockAnimationStarted();
547 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
548 ExpectPastLockAnimationFinished();
550 // When we release the power button, the lock-to-shutdown timer should be
553 EXPECT_TRUE(test_api_
->lock_to_shutdown_timer_is_running());
554 ReleasePowerButton();
556 EXPECT_FALSE(test_api_
->lock_to_shutdown_timer_is_running());
558 // Notify that the screen has been unlocked. We should show the
559 // non-screen-locker windows.
561 SuccessfulAuthentication(&called
);
563 ExpectUnlockBeforeUIDestroyedAnimationStarted();
564 EXPECT_FALSE(called
);
565 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
566 ExpectUnlockBeforeUIDestroyedAnimationFinished();
572 ExpectUnlockAfterUIDestroyedAnimationStarted();
573 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
574 ExpectUnlockAfterUIDestroyedAnimationFinished();
576 ExpectUnlockedState();
579 // Test that we deal with cancelling lock correctly.
580 // TODO(antrim): Reenable this: http://crbug.com/167048
581 TEST_F(LockStateControllerTest
, DISABLED_LockAndCancel
) {
582 Initialize(false, user::LOGGED_IN_USER
);
584 ExpectUnlockedState();
586 // Press the power button and check that the lock timer is started and that we
587 // start lifting the non-screen-locker containers.
590 ExpectPreLockAnimationStarted();
591 EXPECT_TRUE(test_api_
->is_lock_cancellable());
593 // forward only half way through
594 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
, 0.5f
);
596 gfx::Transform transform_before_button_released
=
597 GetContainer(internal::kShellWindowId_DefaultContainer
)->
598 layer()->transform();
600 // Release the button before the lock timer fires.
601 ReleasePowerButton();
603 ExpectPreLockAnimationCancel();
605 gfx::Transform transform_after_button_released
=
606 GetContainer(internal::kShellWindowId_DefaultContainer
)->
607 layer()->transform();
608 // Expect no flickering, animation should proceed from mid-state.
609 EXPECT_EQ(transform_before_button_released
, transform_after_button_released
);
611 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
612 ExpectUnlockedState();
613 EXPECT_EQ(0, delegate_
->num_lock_requests());
616 // Test that we deal with cancelling lock correctly.
617 // TODO(antrim): Reenable this: http://crbug.com/167048
618 TEST_F(LockStateControllerTest
,
619 DISABLED_LockAndCancelAndLockAgain
) {
620 Initialize(false, user::LOGGED_IN_USER
);
622 ExpectUnlockedState();
624 // Press the power button and check that the lock timer is started and that we
625 // start lifting the non-screen-locker containers.
628 ExpectPreLockAnimationStarted();
629 EXPECT_TRUE(test_api_
->is_lock_cancellable());
631 // forward only half way through
632 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
, 0.5f
);
634 // Release the button before the lock timer fires.
635 ReleasePowerButton();
636 ExpectPreLockAnimationCancel();
638 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
, 0.5f
);
641 ExpectPreLockAnimationStarted();
642 EXPECT_TRUE(test_api_
->is_lock_cancellable());
644 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
, 0.6f
);
646 EXPECT_EQ(0, delegate_
->num_lock_requests());
647 ExpectPreLockAnimationStarted();
649 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
, 0.6f
);
650 ExpectPreLockAnimationFinished();
651 EXPECT_EQ(1, delegate_
->num_lock_requests());
654 // Hold the power button down from the unlocked state to eventual shutdown.
655 // TODO(antrim): Reenable this: http://crbug.com/167048
656 TEST_F(LockStateControllerTest
, DISABLED_LockToShutdown
) {
657 Initialize(false, user::LOGGED_IN_USER
);
659 // Hold the power button and lock the screen.
661 EXPECT_TRUE(test_api_
->is_animating_lock());
663 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
);
665 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
667 // When the lock-to-shutdown timeout fires, we should start the shutdown
669 EXPECT_TRUE(test_api_
->lock_to_shutdown_timer_is_running());
671 test_api_
->trigger_lock_to_shutdown_timeout();
673 ExpectShutdownAnimationStarted();
674 EXPECT_TRUE(test_api_
->shutdown_timer_is_running());
676 // Fire the shutdown timeout and check that we request shutdown.
677 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN
);
678 ExpectShutdownAnimationFinished();
679 test_api_
->trigger_shutdown_timeout();
681 EXPECT_TRUE(test_api_
->real_shutdown_timer_is_running());
682 EXPECT_EQ(0, NumShutdownRequests());
683 test_api_
->trigger_real_shutdown_timeout();
684 EXPECT_EQ(1, NumShutdownRequests());
687 // Hold the power button down from the unlocked state to eventual shutdown,
688 // then release the button while system does locking.
689 TEST_F(LockStateControllerTest
, CancelLockToShutdown
) {
690 Initialize(false, user::LOGGED_IN_USER
);
694 // Hold the power button and lock the screen.
695 EXPECT_TRUE(test_api_
->is_animating_lock());
697 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
);
699 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
, 0.5f
);
701 // Power button is released while system attempts to lock.
702 ReleasePowerButton();
704 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
706 EXPECT_FALSE(lock_state_controller_
->ShutdownRequested());
707 EXPECT_FALSE(test_api_
->lock_to_shutdown_timer_is_running());
708 EXPECT_FALSE(test_api_
->shutdown_timer_is_running());
711 // Test that we handle the case where lock requests are ignored.
712 // TODO(antrim): Reenable this: http://crbug.com/167048
713 TEST_F(LockStateControllerTest
, DISABLED_Lock
) {
714 // We require animations to have a duration for this test.
715 ui::ScopedAnimationDurationScaleMode
normal_duration_mode(
716 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION
);
718 Initialize(false, user::LOGGED_IN_USER
);
720 // Hold the power button and lock the screen.
722 ExpectPreLockAnimationStarted();
724 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
);
726 EXPECT_EQ(1, delegate_
->num_lock_requests());
727 EXPECT_TRUE(test_api_
->lock_fail_timer_is_running());
728 // We shouldn't start the lock-to-shutdown timer until the screen has actually
729 // been locked and this was animated.
730 EXPECT_FALSE(test_api_
->lock_to_shutdown_timer_is_running());
732 // Act as if the request timed out. We should restore the windows.
733 test_api_
->trigger_lock_fail_timeout();
735 ExpectUnlockAfterUIDestroyedAnimationStarted();
736 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
737 ExpectUnlockAfterUIDestroyedAnimationFinished();
738 ExpectUnlockedState();
741 // Test the basic operation of the lock button (not logged in).
742 TEST_F(LockStateControllerTest
, LockButtonBasicNotLoggedIn
) {
743 // The lock button shouldn't do anything if we aren't logged in.
744 Initialize(false, user::LOGGED_IN_NONE
);
747 EXPECT_FALSE(test_api_
->is_animating_lock());
749 EXPECT_EQ(0, delegate_
->num_lock_requests());
752 // Test the basic operation of the lock button (guest).
753 TEST_F(LockStateControllerTest
, LockButtonBasicGuest
) {
754 // The lock button shouldn't do anything when we're logged in as a guest.
755 Initialize(false, user::LOGGED_IN_GUEST
);
758 EXPECT_FALSE(test_api_
->is_animating_lock());
760 EXPECT_EQ(0, delegate_
->num_lock_requests());
763 // Test the basic operation of the lock button.
764 // TODO(antrim): Reenable this: http://crbug.com/167048
765 TEST_F(LockStateControllerTest
, DISABLED_LockButtonBasic
) {
766 // If we're logged in as a regular user, we should start the lock timer and
767 // the pre-lock animation.
768 Initialize(false, user::LOGGED_IN_USER
);
771 ExpectPreLockAnimationStarted();
772 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
, 0.5f
);
774 // If the button is released immediately, we shouldn't lock the screen.
776 ExpectPreLockAnimationCancel();
777 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
779 ExpectUnlockedState();
780 EXPECT_EQ(0, delegate_
->num_lock_requests());
782 // Press the button again and let the lock timeout fire. We should request
783 // that the screen be locked.
785 ExpectPreLockAnimationStarted();
786 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
);
787 EXPECT_EQ(1, delegate_
->num_lock_requests());
789 // Pressing the lock button while we have a pending lock request shouldn't do
793 ExpectPreLockAnimationFinished();
796 // Pressing the button also shouldn't do anything after the screen is locked.
798 ExpectPostLockAnimationStarted();
802 ExpectPostLockAnimationStarted();
804 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
805 ExpectPastLockAnimationFinished();
809 ExpectPastLockAnimationFinished();
812 // Test that the power button takes priority over the lock button.
813 // TODO(antrim): Reenable this: http://crbug.com/167048
814 TEST_F(LockStateControllerTest
,
815 DISABLED_PowerButtonPreemptsLockButton
) {
816 Initialize(false, user::LOGGED_IN_USER
);
818 // While the lock button is down, hold the power button.
820 ExpectPreLockAnimationStarted();
822 ExpectPreLockAnimationStarted();
824 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
, 0.5f
);
826 // The lock timer shouldn't be stopped when the lock button is released.
828 ExpectPreLockAnimationStarted();
829 ReleasePowerButton();
830 ExpectPreLockAnimationCancel();
832 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
833 ExpectUnlockedState();
835 // Now press the power button first and then the lock button.
837 ExpectPreLockAnimationStarted();
839 ExpectPreLockAnimationStarted();
841 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
, 0.5f
);
843 // Releasing the power button should stop the lock timer.
844 ReleasePowerButton();
845 ExpectPreLockAnimationCancel();
847 ExpectPreLockAnimationCancel();
850 // When the screen is locked without going through the usual power-button
851 // slow-close path (e.g. via the wrench menu), test that we still show the
852 // fast-close animation.
853 TEST_F(LockStateControllerTest
, LockWithoutButton
) {
854 Initialize(false, user::LOGGED_IN_USER
);
855 lock_state_controller_
->OnStartingLock();
857 ExpectPreLockAnimationStarted();
858 EXPECT_FALSE(test_api_
->is_lock_cancellable());
860 // TODO(antrim): After time-faking is fixed, let the pre-lock animation
861 // complete here and check that delegate_->num_lock_requests() is 0 to
862 // prevent http://crbug.com/172487 from regressing.
865 // When we hear that the process is exiting but we haven't had a chance to
866 // display an animation, we should just blank the screen.
867 TEST_F(LockStateControllerTest
, ShutdownWithoutButton
) {
868 Initialize(false, user::LOGGED_IN_USER
);
869 lock_state_controller_
->OnAppTerminating();
872 animator_api_
->ContainersAreAnimated(
873 SessionStateAnimator::kAllContainersMask
,
874 SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY
));
875 GenerateMouseMoveEvent();
876 EXPECT_FALSE(cursor_visible());
879 // Test that we display the fast-close animation and shut down when we get an
880 // outside request to shut down (e.g. from the login or lock screen).
881 TEST_F(LockStateControllerTest
, RequestShutdownFromLoginScreen
) {
882 Initialize(false, user::LOGGED_IN_NONE
);
884 lock_state_controller_
->RequestShutdown();
886 ExpectShutdownAnimationStarted();
887 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN
);
889 GenerateMouseMoveEvent();
890 EXPECT_FALSE(cursor_visible());
892 EXPECT_EQ(0, NumShutdownRequests());
893 EXPECT_TRUE(test_api_
->real_shutdown_timer_is_running());
894 test_api_
->trigger_real_shutdown_timeout();
895 EXPECT_EQ(1, NumShutdownRequests());
898 TEST_F(LockStateControllerTest
, RequestShutdownFromLockScreen
) {
899 Initialize(false, user::LOGGED_IN_USER
);
902 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN
);
903 ExpectPastLockAnimationFinished();
905 lock_state_controller_
->RequestShutdown();
907 ExpectShutdownAnimationStarted();
908 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN
);
910 GenerateMouseMoveEvent();
911 EXPECT_FALSE(cursor_visible());
913 EXPECT_EQ(0, NumShutdownRequests());
914 EXPECT_TRUE(test_api_
->real_shutdown_timer_is_running());
915 test_api_
->trigger_real_shutdown_timeout();
916 EXPECT_EQ(1, NumShutdownRequests());
919 // TODO(antrim): Reenable this: http://crbug.com/167048
920 TEST_F(LockStateControllerTest
,
921 DISABLED_RequestAndCancelShutdownFromLockScreen
) {
922 Initialize(false, user::LOGGED_IN_USER
);
925 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN
);
928 // Press the power button and check that we start the shutdown timer.
930 EXPECT_FALSE(test_api_
->is_animating_lock());
931 EXPECT_TRUE(test_api_
->shutdown_timer_is_running());
933 ExpectShutdownAnimationStarted();
935 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN
, 0.5f
);
937 float grayscale_before_button_release
=
938 Shell::GetPrimaryRootWindow()->layer()->layer_grayscale();
940 // Release the power button before the shutdown timer fires.
941 ReleasePowerButton();
943 EXPECT_FALSE(test_api_
->shutdown_timer_is_running());
945 ExpectShutdownAnimationCancel();
947 float grayscale_after_button_release
=
948 Shell::GetPrimaryRootWindow()->layer()->layer_grayscale();
949 // Expect no flickering in undo animation.
950 EXPECT_EQ(grayscale_before_button_release
, grayscale_after_button_release
);
952 Advance(SessionStateAnimator::ANIMATION_SPEED_REVERT
);
956 // Test that we ignore power button presses when the screen is turned off.
957 TEST_F(LockStateControllerTest
, IgnorePowerButtonIfScreenIsOff
) {
958 Initialize(false, user::LOGGED_IN_USER
);
960 // When the screen brightness is at 0%, we shouldn't do anything in response
961 // to power button presses.
962 controller_
->OnScreenBrightnessChanged(0.0);
965 EXPECT_FALSE(test_api_
->is_animating_lock());
966 ReleasePowerButton();
968 // After increasing the brightness to 10%, we should start the timer like
970 controller_
->OnScreenBrightnessChanged(10.0);
973 EXPECT_TRUE(test_api_
->is_animating_lock());
976 // Test that hidden background appears and revers correctly on lock/cancel.
977 // TODO(antrim): Reenable this: http://crbug.com/167048
978 TEST_F(LockStateControllerTest
, DISABLED_TestHiddenBackgroundLockCancel
) {
979 Initialize(false, user::LOGGED_IN_USER
);
982 EXPECT_TRUE(IsBackgroundHidden());
983 ExpectUnlockedState();
986 ExpectPreLockAnimationStarted();
987 EXPECT_FALSE(IsBackgroundHidden());
988 ExpectBackgroundIsShowing();
990 // Forward only half way through.
991 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
, 0.5f
);
993 // Release the button before the lock timer fires.
994 ReleasePowerButton();
995 ExpectPreLockAnimationCancel();
996 ExpectBackgroundIsHiding();
997 EXPECT_FALSE(IsBackgroundHidden());
999 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
1001 ExpectUnlockedState();
1002 EXPECT_TRUE(IsBackgroundHidden());
1005 // Test that hidden background appears and revers correctly on lock/unlock.
1006 // TODO(antrim): Reenable this: http://crbug.com/167048
1007 TEST_F(LockStateControllerTest
, DISABLED_TestHiddenBackgroundLockUnlock
) {
1008 Initialize(false, user::LOGGED_IN_USER
);
1011 EXPECT_TRUE(IsBackgroundHidden());
1012 ExpectUnlockedState();
1014 // Press the power button and check that the lock timer is started and that we
1015 // start lifting the non-screen-locker containers.
1018 ExpectPreLockAnimationStarted();
1019 EXPECT_FALSE(IsBackgroundHidden());
1020 ExpectBackgroundIsShowing();
1022 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE
);
1024 ExpectPreLockAnimationFinished();
1028 ReleasePowerButton();
1030 ExpectPostLockAnimationStarted();
1031 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
1032 ExpectPastLockAnimationFinished();
1034 ExpectLockedState();
1036 SuccessfulAuthentication(NULL
);
1038 ExpectUnlockBeforeUIDestroyedAnimationStarted();
1039 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
1040 ExpectUnlockBeforeUIDestroyedAnimationFinished();
1044 ExpectUnlockAfterUIDestroyedAnimationStarted();
1045 ExpectBackgroundIsHiding();
1046 EXPECT_FALSE(IsBackgroundHidden());
1048 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS
);
1049 ExpectUnlockAfterUIDestroyedAnimationFinished();
1050 EXPECT_TRUE(IsBackgroundHidden());
1052 ExpectUnlockedState();