Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ash / wm / lock_state_controller_unittest.cc
blob80db34923ae149c837685486d42d91d2691f77b8
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/session_state_delegate.h"
9 #include "ash/shell.h"
10 #include "ash/shell_window_ids.h"
11 #include "ash/test/ash_test_base.h"
12 #include "ash/test/test_lock_state_controller_delegate.h"
13 #include "ash/test/test_shell_delegate.h"
14 #include "ash/wm/power_button_controller.h"
15 #include "ash/wm/session_state_animator.h"
16 #include "base/command_line.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/time/time.h"
19 #include "ui/aura/env.h"
20 #include "ui/aura/test/test_window_delegate.h"
21 #include "ui/aura/window_event_dispatcher.h"
22 #include "ui/compositor/layer_animator.h"
23 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
24 #include "ui/compositor/scoped_layer_animation_settings.h"
25 #include "ui/events/test/event_generator.h"
26 #include "ui/gfx/rect.h"
27 #include "ui/gfx/size.h"
29 #if defined(OS_CHROMEOS)
30 #include "ui/display/chromeos/display_configurator.h"
31 #include "ui/display/chromeos/test/test_display_snapshot.h"
32 #include "ui/display/types/display_constants.h"
33 #endif
35 #if defined(OS_WIN)
36 #include "base/win/windows_version.h"
37 #endif
39 namespace ash {
40 namespace test {
41 namespace {
43 bool cursor_visible() {
44 return ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible();
47 void CheckCalledCallback(bool* flag) {
48 if (flag)
49 (*flag) = true;
52 aura::Window* GetContainer(int container ) {
53 aura::Window* root_window = Shell::GetPrimaryRootWindow();
54 return Shell::GetContainer(root_window, container);
57 bool IsBackgroundHidden() {
58 return !GetContainer(kShellWindowId_DesktopBackgroundContainer)->IsVisible();
61 void HideBackground() {
62 ui::ScopedLayerAnimationSettings settings(
63 GetContainer(kShellWindowId_DesktopBackgroundContainer)
64 ->layer()
65 ->GetAnimator());
66 settings.SetTransitionDuration(base::TimeDelta());
67 GetContainer(kShellWindowId_DesktopBackgroundContainer)->Hide();
70 } // namespace
72 class LockStateControllerTest : public AshTestBase {
73 public:
74 LockStateControllerTest() : controller_(NULL), delegate_(NULL) {}
75 virtual ~LockStateControllerTest() {}
77 virtual void SetUp() OVERRIDE {
78 AshTestBase::SetUp();
80 // We would control animations in a fine way:
81 animation_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
82 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION));
83 // TODO(antrim) : restore
84 // animator_helper_ = ui::test::CreateLayerAnimatorHelperForTest();
86 // Temporary disable animations so that observer is always called, and
87 // no leaks happen during tests.
88 animation_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
89 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
90 // TODO(antrim): once there is a way to mock time and run animations, make
91 // sure that animations are finished even in simple tests.
93 delegate_ = new TestLockStateControllerDelegate;
94 controller_ = Shell::GetInstance()->power_button_controller();
95 lock_state_controller_ = static_cast<LockStateController*>(
96 Shell::GetInstance()->lock_state_controller());
97 lock_state_controller_->SetDelegate(delegate_); // transfers ownership
98 test_api_.reset(new LockStateController::TestApi(lock_state_controller_));
99 animator_api_.reset(
100 new SessionStateAnimator::TestApi(lock_state_controller_->
101 animator_.get()));
102 shell_delegate_ = reinterpret_cast<TestShellDelegate*>(
103 ash::Shell::GetInstance()->delegate());
104 session_state_delegate_ = Shell::GetInstance()->session_state_delegate();
107 virtual void TearDown() {
108 // TODO(antrim) : restore
109 // animator_helper_->AdvanceUntilDone();
110 window_.reset();
111 AshTestBase::TearDown();
114 protected:
115 void GenerateMouseMoveEvent() {
116 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
117 generator.MoveMouseTo(10, 10);
120 int NumShutdownRequests() {
121 return delegate_->num_shutdown_requests() +
122 shell_delegate_->num_exit_requests();
125 void Advance(SessionStateAnimator::AnimationSpeed speed) {
126 // TODO (antrim) : restore
127 // animator_helper_->Advance(SessionStateAnimator::GetDuration(speed));
130 void AdvancePartially(SessionStateAnimator::AnimationSpeed speed,
131 float factor) {
132 // TODO (antrim) : restore
133 // base::TimeDelta duration = SessionStateAnimator::GetDuration(speed);
134 // base::TimeDelta partial_duration =
135 // base::TimeDelta::FromInternalValue(duration.ToInternalValue() * factor);
136 // animator_helper_->Advance(partial_duration);
139 void ExpectPreLockAnimationStarted() {
140 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
141 EXPECT_TRUE(
142 animator_api_->ContainersAreAnimated(
143 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
144 SessionStateAnimator::ANIMATION_LIFT));
145 EXPECT_TRUE(
146 animator_api_->ContainersAreAnimated(
147 SessionStateAnimator::LAUNCHER,
148 SessionStateAnimator::ANIMATION_FADE_OUT));
149 EXPECT_TRUE(
150 animator_api_->ContainersAreAnimated(
151 SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
152 SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY));
153 EXPECT_TRUE(test_api_->is_animating_lock());
156 void ExpectPreLockAnimationCancel() {
157 EXPECT_TRUE(
158 animator_api_->ContainersAreAnimated(
159 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
160 SessionStateAnimator::ANIMATION_DROP));
161 EXPECT_TRUE(
162 animator_api_->ContainersAreAnimated(
163 SessionStateAnimator::LAUNCHER,
164 SessionStateAnimator::ANIMATION_FADE_IN));
167 void ExpectPreLockAnimationFinished() {
168 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
169 EXPECT_TRUE(
170 animator_api_->ContainersAreAnimated(
171 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
172 SessionStateAnimator::ANIMATION_LIFT));
173 EXPECT_TRUE(
174 animator_api_->ContainersAreAnimated(
175 SessionStateAnimator::LAUNCHER,
176 SessionStateAnimator::ANIMATION_FADE_OUT));
177 EXPECT_TRUE(
178 animator_api_->ContainersAreAnimated(
179 SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
180 SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY));
183 void ExpectPostLockAnimationStarted() {
184 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
185 EXPECT_TRUE(
186 animator_api_->ContainersAreAnimated(
187 SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
188 SessionStateAnimator::ANIMATION_RAISE_TO_SCREEN));
191 void ExpectPastLockAnimationFinished() {
192 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
193 EXPECT_TRUE(
194 animator_api_->ContainersAreAnimated(
195 SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
196 SessionStateAnimator::ANIMATION_RAISE_TO_SCREEN));
199 void ExpectUnlockBeforeUIDestroyedAnimationStarted() {
200 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
201 EXPECT_TRUE(
202 animator_api_->ContainersAreAnimated(
203 SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
204 SessionStateAnimator::ANIMATION_LIFT));
207 void ExpectUnlockBeforeUIDestroyedAnimationFinished() {
208 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
209 EXPECT_TRUE(
210 animator_api_->ContainersAreAnimated(
211 SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
212 SessionStateAnimator::ANIMATION_LIFT));
215 void ExpectUnlockAfterUIDestroyedAnimationStarted() {
216 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
217 EXPECT_TRUE(
218 animator_api_->ContainersAreAnimated(
219 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
220 SessionStateAnimator::ANIMATION_DROP));
221 EXPECT_TRUE(
222 animator_api_->ContainersAreAnimated(
223 SessionStateAnimator::LAUNCHER,
224 SessionStateAnimator::ANIMATION_FADE_IN));
227 void ExpectUnlockAfterUIDestroyedAnimationFinished() {
228 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
229 EXPECT_TRUE(
230 animator_api_->ContainersAreAnimated(
231 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
232 SessionStateAnimator::ANIMATION_DROP));
233 EXPECT_TRUE(
234 animator_api_->ContainersAreAnimated(
235 SessionStateAnimator::LAUNCHER,
236 SessionStateAnimator::ANIMATION_FADE_IN));
239 void ExpectShutdownAnimationStarted() {
240 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
241 EXPECT_TRUE(
242 animator_api_->RootWindowIsAnimated(
243 SessionStateAnimator::ANIMATION_GRAYSCALE_BRIGHTNESS));
246 void ExpectShutdownAnimationFinished() {
247 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
248 EXPECT_TRUE(
249 animator_api_->RootWindowIsAnimated(
250 SessionStateAnimator::ANIMATION_GRAYSCALE_BRIGHTNESS));
253 void ExpectShutdownAnimationCancel() {
254 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
255 EXPECT_TRUE(
256 animator_api_->RootWindowIsAnimated(
257 SessionStateAnimator::ANIMATION_UNDO_GRAYSCALE_BRIGHTNESS));
260 void ExpectBackgroundIsShowing() {
261 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
262 EXPECT_TRUE(
263 animator_api_->ContainersAreAnimated(
264 SessionStateAnimator::DESKTOP_BACKGROUND,
265 SessionStateAnimator::ANIMATION_FADE_IN));
268 void ExpectBackgroundIsHiding() {
269 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
270 EXPECT_TRUE(
271 animator_api_->ContainersAreAnimated(
272 SessionStateAnimator::DESKTOP_BACKGROUND,
273 SessionStateAnimator::ANIMATION_FADE_OUT));
276 void ExpectUnlockedState() {
277 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
278 EXPECT_FALSE(session_state_delegate_->IsScreenLocked());
280 aura::Window::Windows containers;
282 SessionStateAnimator::GetContainers(
283 SessionStateAnimator::LAUNCHER |
284 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
285 &containers);
286 for (aura::Window::Windows::const_iterator it = containers.begin();
287 it != containers.end(); ++it) {
288 aura::Window* window = *it;
289 ui::Layer* layer = window->layer();
290 EXPECT_EQ(1.0f, layer->opacity());
291 EXPECT_EQ(0.0f, layer->layer_brightness());
292 EXPECT_EQ(0.0f, layer->layer_saturation());
293 EXPECT_EQ(gfx::Transform(), layer->transform());
297 void ExpectLockedState() {
298 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
299 EXPECT_TRUE(session_state_delegate_->IsScreenLocked());
301 aura::Window::Windows containers;
303 SessionStateAnimator::GetContainers(
304 SessionStateAnimator::LOCK_SCREEN_RELATED_CONTAINERS |
305 SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
306 &containers);
307 for (aura::Window::Windows::const_iterator it = containers.begin();
308 it != containers.end(); ++it) {
309 aura::Window* window = *it;
310 ui::Layer* layer = window->layer();
311 EXPECT_EQ(1.0f, layer->opacity());
312 EXPECT_EQ(0.0f, layer->layer_brightness());
313 EXPECT_EQ(0.0f, layer->layer_saturation());
314 EXPECT_EQ(gfx::Transform(), layer->transform());
318 void PressPowerButton() {
319 controller_->OnPowerButtonEvent(true, base::TimeTicks::Now());
320 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta());
323 void ReleasePowerButton() {
324 controller_->OnPowerButtonEvent(false, base::TimeTicks::Now());
325 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta());
328 void PressLockButton() {
329 controller_->OnLockButtonEvent(true, base::TimeTicks::Now());
332 void ReleaseLockButton() {
333 controller_->OnLockButtonEvent(false, base::TimeTicks::Now());
336 void SystemLocks() {
337 lock_state_controller_->OnLockStateChanged(true);
338 session_state_delegate_->LockScreen();
339 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta());
342 void SuccessfulAuthentication(bool* call_flag) {
343 base::Closure closure = base::Bind(&CheckCalledCallback, call_flag);
344 lock_state_controller_->OnLockScreenHide(closure);
345 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta());
348 void SystemUnlocks() {
349 lock_state_controller_->OnLockStateChanged(false);
350 session_state_delegate_->UnlockScreen();
351 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta());
354 void Initialize(bool legacy_button, user::LoginStatus status) {
355 controller_->set_has_legacy_power_button_for_test(legacy_button);
356 lock_state_controller_->OnLoginStateChanged(status);
357 SetUserLoggedIn(status != user::LOGGED_IN_NONE);
358 if (status == user::LOGGED_IN_GUEST)
359 SetCanLockScreen(false);
360 lock_state_controller_->OnLockStateChanged(false);
363 void CreateWindowForLockscreen() {
364 window_.reset(new aura::Window(&window_delegate_));
365 window_->SetBounds(gfx::Rect(0, 0, 100, 100));
366 window_->SetType(ui::wm::WINDOW_TYPE_NORMAL);
367 window_->Init(aura::WINDOW_LAYER_TEXTURED);
368 window_->SetName("WINDOW");
369 aura::Window* container = Shell::GetContainer(
370 Shell::GetPrimaryRootWindow(), kShellWindowId_LockScreenContainer);
371 ASSERT_TRUE(container);
372 container->AddChild(window_.get());
373 window_->Show();
376 PowerButtonController* controller_; // not owned
377 LockStateController* lock_state_controller_; // not owned
378 TestLockStateControllerDelegate* delegate_; // not owned
379 TestShellDelegate* shell_delegate_; // not owned
380 SessionStateDelegate* session_state_delegate_; // not owned
382 aura::test::TestWindowDelegate window_delegate_;
383 scoped_ptr<aura::Window> window_;
384 scoped_ptr<ui::ScopedAnimationDurationScaleMode> animation_duration_mode_;
385 scoped_ptr<LockStateController::TestApi> test_api_;
386 scoped_ptr<SessionStateAnimator::TestApi> animator_api_;
387 // TODO(antrim) : restore
388 // scoped_ptr<ui::test::AnimationContainerTestHelper> animator_helper_;
390 private:
391 DISALLOW_COPY_AND_ASSIGN(LockStateControllerTest);
394 // Test the lock-to-shutdown flow for non-Chrome-OS hardware that doesn't
395 // correctly report power button releases. We should lock immediately the first
396 // time the button is pressed and shut down when it's pressed from the locked
397 // state.
398 // TODO(antrim): Reenable this: http://crbug.com/167048
399 TEST_F(LockStateControllerTest, DISABLED_LegacyLockAndShutDown) {
400 Initialize(true, user::LOGGED_IN_USER);
402 ExpectUnlockedState();
404 // We should request that the screen be locked immediately after seeing the
405 // power button get pressed.
406 PressPowerButton();
408 ExpectPreLockAnimationStarted();
410 EXPECT_FALSE(test_api_->is_lock_cancellable());
412 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
414 ExpectPreLockAnimationFinished();
415 EXPECT_EQ(1, delegate_->num_lock_requests());
417 // Notify that we locked successfully.
418 lock_state_controller_->OnStartingLock();
419 // We had that animation already.
420 //TODO (antrim) : restore
421 // EXPECT_FALSE(animator_helper_->IsAnimating());
423 SystemLocks();
425 ExpectPostLockAnimationStarted();
426 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
427 ExpectPastLockAnimationFinished();
429 // We shouldn't progress towards the shutdown state, however.
430 EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running());
431 EXPECT_FALSE(test_api_->shutdown_timer_is_running());
433 ReleasePowerButton();
435 // Hold the button again and check that we start shutting down.
436 PressPowerButton();
438 ExpectShutdownAnimationStarted();
440 EXPECT_EQ(0, NumShutdownRequests());
441 // Make sure a mouse move event won't show the cursor.
442 GenerateMouseMoveEvent();
443 EXPECT_FALSE(cursor_visible());
445 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
446 test_api_->trigger_real_shutdown_timeout();
447 EXPECT_EQ(1, NumShutdownRequests());
450 // Test that we start shutting down immediately if the power button is pressed
451 // while we're not logged in on an unofficial system.
452 TEST_F(LockStateControllerTest, LegacyNotLoggedIn) {
453 Initialize(true, user::LOGGED_IN_NONE);
455 PressPowerButton();
456 ExpectShutdownAnimationStarted();
458 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
461 // Test that we start shutting down immediately if the power button is pressed
462 // while we're logged in as a guest on an unofficial system.
463 TEST_F(LockStateControllerTest, LegacyGuest) {
464 Initialize(true, user::LOGGED_IN_GUEST);
466 PressPowerButton();
467 ExpectShutdownAnimationStarted();
469 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
472 // When we hold the power button while the user isn't logged in, we should shut
473 // down the machine directly.
474 TEST_F(LockStateControllerTest, ShutdownWhenNotLoggedIn) {
475 Initialize(false, user::LOGGED_IN_NONE);
477 // Press the power button and check that we start the shutdown timer.
478 PressPowerButton();
479 EXPECT_FALSE(test_api_->is_animating_lock());
480 EXPECT_TRUE(test_api_->shutdown_timer_is_running());
481 ExpectShutdownAnimationStarted();
483 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN, 0.5f);
485 // Release the power button before the shutdown timer fires.
486 ReleasePowerButton();
488 EXPECT_FALSE(test_api_->shutdown_timer_is_running());
489 ExpectShutdownAnimationCancel();
491 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_REVERT, 0.5f);
493 // Press the button again and make the shutdown timeout fire this time.
494 // Check that we start the timer for actually requesting the shutdown.
495 PressPowerButton();
497 EXPECT_TRUE(test_api_->shutdown_timer_is_running());
499 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
500 ExpectShutdownAnimationFinished();
501 test_api_->trigger_shutdown_timeout();
503 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
504 EXPECT_EQ(0, NumShutdownRequests());
506 // When the timout fires, we should request a shutdown.
507 test_api_->trigger_real_shutdown_timeout();
509 EXPECT_EQ(1, NumShutdownRequests());
512 // Test that we lock the screen and deal with unlocking correctly.
513 // TODO(antrim): Reenable this: http://crbug.com/167048
514 TEST_F(LockStateControllerTest, DISABLED_LockAndUnlock) {
515 Initialize(false, user::LOGGED_IN_USER);
517 ExpectUnlockedState();
519 // Press the power button and check that the lock timer is started and that we
520 // start lifting the non-screen-locker containers.
521 PressPowerButton();
523 ExpectPreLockAnimationStarted();
524 EXPECT_TRUE(test_api_->is_lock_cancellable());
525 EXPECT_EQ(0, delegate_->num_lock_requests());
527 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
528 ExpectPreLockAnimationFinished();
530 EXPECT_EQ(1, delegate_->num_lock_requests());
532 // Notify that we locked successfully.
533 lock_state_controller_->OnStartingLock();
534 // We had that animation already.
535 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
537 SystemLocks();
539 ExpectPostLockAnimationStarted();
540 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
541 ExpectPastLockAnimationFinished();
543 // When we release the power button, the lock-to-shutdown timer should be
544 // stopped.
545 ExpectLockedState();
546 EXPECT_TRUE(test_api_->lock_to_shutdown_timer_is_running());
547 ReleasePowerButton();
548 ExpectLockedState();
549 EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running());
551 // Notify that the screen has been unlocked. We should show the
552 // non-screen-locker windows.
553 bool called = false;
554 SuccessfulAuthentication(&called);
556 ExpectUnlockBeforeUIDestroyedAnimationStarted();
557 EXPECT_FALSE(called);
558 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
559 ExpectUnlockBeforeUIDestroyedAnimationFinished();
561 EXPECT_TRUE(called);
563 SystemUnlocks();
565 ExpectUnlockAfterUIDestroyedAnimationStarted();
566 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
567 ExpectUnlockAfterUIDestroyedAnimationFinished();
569 ExpectUnlockedState();
572 // Test that we deal with cancelling lock correctly.
573 // TODO(antrim): Reenable this: http://crbug.com/167048
574 TEST_F(LockStateControllerTest, DISABLED_LockAndCancel) {
575 Initialize(false, user::LOGGED_IN_USER);
577 ExpectUnlockedState();
579 // Press the power button and check that the lock timer is started and that we
580 // start lifting the non-screen-locker containers.
581 PressPowerButton();
583 ExpectPreLockAnimationStarted();
584 EXPECT_TRUE(test_api_->is_lock_cancellable());
586 // forward only half way through
587 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
589 gfx::Transform transform_before_button_released =
590 GetContainer(kShellWindowId_DefaultContainer)->layer()->transform();
592 // Release the button before the lock timer fires.
593 ReleasePowerButton();
595 ExpectPreLockAnimationCancel();
597 gfx::Transform transform_after_button_released =
598 GetContainer(kShellWindowId_DefaultContainer)->layer()->transform();
599 // Expect no flickering, animation should proceed from mid-state.
600 EXPECT_EQ(transform_before_button_released, transform_after_button_released);
602 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
603 ExpectUnlockedState();
604 EXPECT_EQ(0, delegate_->num_lock_requests());
607 // Test that we deal with cancelling lock correctly.
608 // TODO(antrim): Reenable this: http://crbug.com/167048
609 TEST_F(LockStateControllerTest,
610 DISABLED_LockAndCancelAndLockAgain) {
611 Initialize(false, user::LOGGED_IN_USER);
613 ExpectUnlockedState();
615 // Press the power button and check that the lock timer is started and that we
616 // start lifting the non-screen-locker containers.
617 PressPowerButton();
619 ExpectPreLockAnimationStarted();
620 EXPECT_TRUE(test_api_->is_lock_cancellable());
622 // forward only half way through
623 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
625 // Release the button before the lock timer fires.
626 ReleasePowerButton();
627 ExpectPreLockAnimationCancel();
629 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS, 0.5f);
631 PressPowerButton();
632 ExpectPreLockAnimationStarted();
633 EXPECT_TRUE(test_api_->is_lock_cancellable());
635 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.6f);
637 EXPECT_EQ(0, delegate_->num_lock_requests());
638 ExpectPreLockAnimationStarted();
640 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.6f);
641 ExpectPreLockAnimationFinished();
642 EXPECT_EQ(1, delegate_->num_lock_requests());
645 // Hold the power button down from the unlocked state to eventual shutdown.
646 // TODO(antrim): Reenable this: http://crbug.com/167048
647 TEST_F(LockStateControllerTest, DISABLED_LockToShutdown) {
648 Initialize(false, user::LOGGED_IN_USER);
650 // Hold the power button and lock the screen.
651 PressPowerButton();
652 EXPECT_TRUE(test_api_->is_animating_lock());
654 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
655 SystemLocks();
656 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
658 // When the lock-to-shutdown timeout fires, we should start the shutdown
659 // timer.
660 EXPECT_TRUE(test_api_->lock_to_shutdown_timer_is_running());
662 test_api_->trigger_lock_to_shutdown_timeout();
664 ExpectShutdownAnimationStarted();
665 EXPECT_TRUE(test_api_->shutdown_timer_is_running());
667 // Fire the shutdown timeout and check that we request shutdown.
668 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
669 ExpectShutdownAnimationFinished();
670 test_api_->trigger_shutdown_timeout();
672 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
673 EXPECT_EQ(0, NumShutdownRequests());
674 test_api_->trigger_real_shutdown_timeout();
675 EXPECT_EQ(1, NumShutdownRequests());
678 // Hold the power button down from the unlocked state to eventual shutdown,
679 // then release the button while system does locking.
680 TEST_F(LockStateControllerTest, CancelLockToShutdown) {
681 Initialize(false, user::LOGGED_IN_USER);
683 PressPowerButton();
685 // Hold the power button and lock the screen.
686 EXPECT_TRUE(test_api_->is_animating_lock());
688 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
689 SystemLocks();
690 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS, 0.5f);
692 // Power button is released while system attempts to lock.
693 ReleasePowerButton();
695 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
697 EXPECT_FALSE(lock_state_controller_->ShutdownRequested());
698 EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running());
699 EXPECT_FALSE(test_api_->shutdown_timer_is_running());
702 // Test that we handle the case where lock requests are ignored.
703 // TODO(antrim): Reenable this: http://crbug.com/167048
704 TEST_F(LockStateControllerTest, DISABLED_Lock) {
705 // We require animations to have a duration for this test.
706 ui::ScopedAnimationDurationScaleMode normal_duration_mode(
707 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
709 Initialize(false, user::LOGGED_IN_USER);
711 // Hold the power button and lock the screen.
712 PressPowerButton();
713 ExpectPreLockAnimationStarted();
715 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
717 EXPECT_EQ(1, delegate_->num_lock_requests());
718 EXPECT_TRUE(test_api_->lock_fail_timer_is_running());
719 // We shouldn't start the lock-to-shutdown timer until the screen has actually
720 // been locked and this was animated.
721 EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running());
723 // Act as if the request timed out. We should restore the windows.
724 test_api_->trigger_lock_fail_timeout();
726 ExpectUnlockAfterUIDestroyedAnimationStarted();
727 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
728 ExpectUnlockAfterUIDestroyedAnimationFinished();
729 ExpectUnlockedState();
732 // Test the basic operation of the lock button (not logged in).
733 TEST_F(LockStateControllerTest, LockButtonBasicNotLoggedIn) {
734 // The lock button shouldn't do anything if we aren't logged in.
735 Initialize(false, user::LOGGED_IN_NONE);
737 PressLockButton();
738 EXPECT_FALSE(test_api_->is_animating_lock());
739 ReleaseLockButton();
740 EXPECT_EQ(0, delegate_->num_lock_requests());
743 // Test the basic operation of the lock button (guest).
744 TEST_F(LockStateControllerTest, LockButtonBasicGuest) {
745 // The lock button shouldn't do anything when we're logged in as a guest.
746 Initialize(false, user::LOGGED_IN_GUEST);
748 PressLockButton();
749 EXPECT_FALSE(test_api_->is_animating_lock());
750 ReleaseLockButton();
751 EXPECT_EQ(0, delegate_->num_lock_requests());
754 // Test the basic operation of the lock button.
755 // TODO(antrim): Reenable this: http://crbug.com/167048
756 TEST_F(LockStateControllerTest, DISABLED_LockButtonBasic) {
757 // If we're logged in as a regular user, we should start the lock timer and
758 // the pre-lock animation.
759 Initialize(false, user::LOGGED_IN_USER);
761 PressLockButton();
762 ExpectPreLockAnimationStarted();
763 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
765 // If the button is released immediately, we shouldn't lock the screen.
766 ReleaseLockButton();
767 ExpectPreLockAnimationCancel();
768 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
770 ExpectUnlockedState();
771 EXPECT_EQ(0, delegate_->num_lock_requests());
773 // Press the button again and let the lock timeout fire. We should request
774 // that the screen be locked.
775 PressLockButton();
776 ExpectPreLockAnimationStarted();
777 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
778 EXPECT_EQ(1, delegate_->num_lock_requests());
780 // Pressing the lock button while we have a pending lock request shouldn't do
781 // anything.
782 ReleaseLockButton();
783 PressLockButton();
784 ExpectPreLockAnimationFinished();
785 ReleaseLockButton();
787 // Pressing the button also shouldn't do anything after the screen is locked.
788 SystemLocks();
789 ExpectPostLockAnimationStarted();
791 PressLockButton();
792 ReleaseLockButton();
793 ExpectPostLockAnimationStarted();
795 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
796 ExpectPastLockAnimationFinished();
798 PressLockButton();
799 ReleaseLockButton();
800 ExpectPastLockAnimationFinished();
803 // Test that the power button takes priority over the lock button.
804 // TODO(antrim): Reenable this: http://crbug.com/167048
805 TEST_F(LockStateControllerTest,
806 DISABLED_PowerButtonPreemptsLockButton) {
807 Initialize(false, user::LOGGED_IN_USER);
809 // While the lock button is down, hold the power button.
810 PressLockButton();
811 ExpectPreLockAnimationStarted();
812 PressPowerButton();
813 ExpectPreLockAnimationStarted();
815 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
817 // The lock timer shouldn't be stopped when the lock button is released.
818 ReleaseLockButton();
819 ExpectPreLockAnimationStarted();
820 ReleasePowerButton();
821 ExpectPreLockAnimationCancel();
823 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
824 ExpectUnlockedState();
826 // Now press the power button first and then the lock button.
827 PressPowerButton();
828 ExpectPreLockAnimationStarted();
829 PressLockButton();
830 ExpectPreLockAnimationStarted();
832 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
834 // Releasing the power button should stop the lock timer.
835 ReleasePowerButton();
836 ExpectPreLockAnimationCancel();
837 ReleaseLockButton();
838 ExpectPreLockAnimationCancel();
841 // When the screen is locked without going through the usual power-button
842 // slow-close path (e.g. via the wrench menu), test that we still show the
843 // fast-close animation.
844 TEST_F(LockStateControllerTest, LockWithoutButton) {
845 Initialize(false, user::LOGGED_IN_USER);
846 lock_state_controller_->OnStartingLock();
848 ExpectPreLockAnimationStarted();
849 EXPECT_FALSE(test_api_->is_lock_cancellable());
851 // TODO(antrim): After time-faking is fixed, let the pre-lock animation
852 // complete here and check that delegate_->num_lock_requests() is 0 to
853 // prevent http://crbug.com/172487 from regressing.
856 // When we hear that the process is exiting but we haven't had a chance to
857 // display an animation, we should just blank the screen.
858 TEST_F(LockStateControllerTest, ShutdownWithoutButton) {
859 Initialize(false, user::LOGGED_IN_USER);
860 lock_state_controller_->OnAppTerminating();
862 EXPECT_TRUE(
863 animator_api_->ContainersAreAnimated(
864 SessionStateAnimator::kAllContainersMask,
865 SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY));
866 GenerateMouseMoveEvent();
867 EXPECT_FALSE(cursor_visible());
870 // Test that we display the fast-close animation and shut down when we get an
871 // outside request to shut down (e.g. from the login or lock screen).
872 TEST_F(LockStateControllerTest, RequestShutdownFromLoginScreen) {
873 Initialize(false, user::LOGGED_IN_NONE);
874 CreateWindowForLockscreen();
876 lock_state_controller_->RequestShutdown();
878 ExpectShutdownAnimationStarted();
879 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
881 GenerateMouseMoveEvent();
882 EXPECT_FALSE(cursor_visible());
884 EXPECT_EQ(0, NumShutdownRequests());
885 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
886 test_api_->trigger_real_shutdown_timeout();
887 EXPECT_EQ(1, NumShutdownRequests());
890 TEST_F(LockStateControllerTest, RequestShutdownFromLockScreen) {
891 Initialize(false, user::LOGGED_IN_USER);
893 SystemLocks();
894 CreateWindowForLockscreen();
895 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
896 ExpectPastLockAnimationFinished();
898 lock_state_controller_->RequestShutdown();
900 ExpectShutdownAnimationStarted();
901 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
903 GenerateMouseMoveEvent();
904 EXPECT_FALSE(cursor_visible());
906 EXPECT_EQ(0, NumShutdownRequests());
907 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
908 test_api_->trigger_real_shutdown_timeout();
909 EXPECT_EQ(1, NumShutdownRequests());
912 // TODO(antrim): Reenable this: http://crbug.com/167048
913 TEST_F(LockStateControllerTest,
914 DISABLED_RequestAndCancelShutdownFromLockScreen) {
915 Initialize(false, user::LOGGED_IN_USER);
917 SystemLocks();
918 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
919 ExpectLockedState();
921 // Press the power button and check that we start the shutdown timer.
922 PressPowerButton();
923 EXPECT_FALSE(test_api_->is_animating_lock());
924 EXPECT_TRUE(test_api_->shutdown_timer_is_running());
926 ExpectShutdownAnimationStarted();
928 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN, 0.5f);
930 float grayscale_before_button_release =
931 Shell::GetPrimaryRootWindow()->layer()->layer_grayscale();
933 // Release the power button before the shutdown timer fires.
934 ReleasePowerButton();
936 EXPECT_FALSE(test_api_->shutdown_timer_is_running());
938 ExpectShutdownAnimationCancel();
940 float grayscale_after_button_release =
941 Shell::GetPrimaryRootWindow()->layer()->layer_grayscale();
942 // Expect no flickering in undo animation.
943 EXPECT_EQ(grayscale_before_button_release, grayscale_after_button_release);
945 Advance(SessionStateAnimator::ANIMATION_SPEED_REVERT);
946 ExpectLockedState();
949 // Test that we ignore power button presses when the screen is turned off.
950 TEST_F(LockStateControllerTest, IgnorePowerButtonIfScreenIsOff) {
951 Initialize(false, user::LOGGED_IN_USER);
953 // When the screen brightness is at 0%, we shouldn't do anything in response
954 // to power button presses.
955 controller_->OnScreenBrightnessChanged(0.0);
956 PressPowerButton();
957 EXPECT_FALSE(test_api_->is_animating_lock());
958 ReleasePowerButton();
960 // After increasing the brightness to 10%, we should start the timer like
961 // usual.
962 controller_->OnScreenBrightnessChanged(10.0);
963 PressPowerButton();
964 EXPECT_TRUE(test_api_->is_animating_lock());
965 ReleasePowerButton();
968 #if defined(OS_CHROMEOS)
969 TEST_F(LockStateControllerTest, HonorPowerButtonInDockedMode) {
970 ScopedVector<const ui::DisplayMode> modes;
971 modes.push_back(new ui::DisplayMode(gfx::Size(1, 1), false, 60.0f));
973 // Create two outputs, the first internal and the second external.
974 ui::DisplayConfigurator::DisplayStateList outputs;
975 ui::DisplayConfigurator::DisplayState internal_output;
976 ui::TestDisplaySnapshot internal_display;
977 internal_display.set_type(ui::DISPLAY_CONNECTION_TYPE_INTERNAL);
978 internal_display.set_modes(modes.get());
979 internal_output.display = &internal_display;
980 outputs.push_back(internal_output);
982 ui::DisplayConfigurator::DisplayState external_output;
983 ui::TestDisplaySnapshot external_display;
984 external_display.set_type(ui::DISPLAY_CONNECTION_TYPE_HDMI);
985 external_display.set_modes(modes.get());
986 external_output.display = &external_display;
987 outputs.push_back(external_output);
989 // When all of the displays are turned off (e.g. due to user inactivity), the
990 // power button should be ignored.
991 controller_->OnScreenBrightnessChanged(0.0);
992 static_cast<ui::TestDisplaySnapshot*>(outputs[0].display)
993 ->set_current_mode(NULL);
994 static_cast<ui::TestDisplaySnapshot*>(outputs[1].display)
995 ->set_current_mode(NULL);
996 controller_->OnDisplayModeChanged(outputs);
997 PressPowerButton();
998 EXPECT_FALSE(test_api_->is_animating_lock());
999 ReleasePowerButton();
1001 // When the screen brightness is 0% but the external display is still turned
1002 // on (indicating either docked mode or the user having manually decreased the
1003 // brightness to 0%), the power button should still be handled.
1004 static_cast<ui::TestDisplaySnapshot*>(outputs[1].display)
1005 ->set_current_mode(modes[0]);
1006 controller_->OnDisplayModeChanged(outputs);
1007 PressPowerButton();
1008 EXPECT_TRUE(test_api_->is_animating_lock());
1009 ReleasePowerButton();
1011 #endif
1013 // Test that hidden background appears and revers correctly on lock/cancel.
1014 // TODO(antrim): Reenable this: http://crbug.com/167048
1015 TEST_F(LockStateControllerTest, DISABLED_TestHiddenBackgroundLockCancel) {
1016 Initialize(false, user::LOGGED_IN_USER);
1017 HideBackground();
1019 EXPECT_TRUE(IsBackgroundHidden());
1020 ExpectUnlockedState();
1021 PressPowerButton();
1023 ExpectPreLockAnimationStarted();
1024 EXPECT_FALSE(IsBackgroundHidden());
1025 ExpectBackgroundIsShowing();
1027 // Forward only half way through.
1028 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
1030 // Release the button before the lock timer fires.
1031 ReleasePowerButton();
1032 ExpectPreLockAnimationCancel();
1033 ExpectBackgroundIsHiding();
1034 EXPECT_FALSE(IsBackgroundHidden());
1036 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
1038 ExpectUnlockedState();
1039 EXPECT_TRUE(IsBackgroundHidden());
1042 // Test that hidden background appears and revers correctly on lock/unlock.
1043 // TODO(antrim): Reenable this: http://crbug.com/167048
1044 TEST_F(LockStateControllerTest, DISABLED_TestHiddenBackgroundLockUnlock) {
1045 Initialize(false, user::LOGGED_IN_USER);
1046 HideBackground();
1048 EXPECT_TRUE(IsBackgroundHidden());
1049 ExpectUnlockedState();
1051 // Press the power button and check that the lock timer is started and that we
1052 // start lifting the non-screen-locker containers.
1053 PressPowerButton();
1055 ExpectPreLockAnimationStarted();
1056 EXPECT_FALSE(IsBackgroundHidden());
1057 ExpectBackgroundIsShowing();
1059 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
1061 ExpectPreLockAnimationFinished();
1063 SystemLocks();
1065 ReleasePowerButton();
1067 ExpectPostLockAnimationStarted();
1068 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
1069 ExpectPastLockAnimationFinished();
1071 ExpectLockedState();
1073 SuccessfulAuthentication(NULL);
1075 ExpectUnlockBeforeUIDestroyedAnimationStarted();
1076 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
1077 ExpectUnlockBeforeUIDestroyedAnimationFinished();
1079 SystemUnlocks();
1081 ExpectUnlockAfterUIDestroyedAnimationStarted();
1082 ExpectBackgroundIsHiding();
1083 EXPECT_FALSE(IsBackgroundHidden());
1085 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
1086 ExpectUnlockAfterUIDestroyedAnimationFinished();
1087 EXPECT_TRUE(IsBackgroundHidden());
1089 ExpectUnlockedState();
1092 } // namespace test
1093 } // namespace ash