Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / ash / wm / lock_state_controller_unittest.cc
blobf9c7dd5457d87275c569f6fc30ac872e79b3e5d4
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_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/test/event_generator.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/gfx/rect.h"
26 #include "ui/gfx/size.h"
28 #if defined(OS_CHROMEOS)
29 #include "ui/display/chromeos/display_configurator.h"
30 #include "ui/display/chromeos/test/test_display_snapshot.h"
31 #include "ui/display/types/display_constants.h"
32 #endif
34 #if defined(OS_WIN)
35 #include "base/win/windows_version.h"
36 #endif
38 namespace ash {
39 namespace test {
40 namespace {
42 bool cursor_visible() {
43 return ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible();
46 void CheckCalledCallback(bool* flag) {
47 if (flag)
48 (*flag) = true;
51 aura::Window* GetContainer(int container ) {
52 aura::Window* root_window = Shell::GetPrimaryRootWindow();
53 return Shell::GetContainer(root_window, container);
56 bool IsBackgroundHidden() {
57 return !GetContainer(kShellWindowId_DesktopBackgroundContainer)->IsVisible();
60 void HideBackground() {
61 ui::ScopedLayerAnimationSettings settings(
62 GetContainer(kShellWindowId_DesktopBackgroundContainer)
63 ->layer()
64 ->GetAnimator());
65 settings.SetTransitionDuration(base::TimeDelta());
66 GetContainer(kShellWindowId_DesktopBackgroundContainer)->Hide();
69 } // namespace
71 // Fake implementation of PowerButtonControllerDelegate that just logs requests
72 // to lock the screen and shut down the device.
73 class TestLockStateControllerDelegate : public LockStateControllerDelegate {
74 public:
75 TestLockStateControllerDelegate()
76 : num_lock_requests_(0),
77 num_shutdown_requests_(0) {}
79 int num_lock_requests() const { return num_lock_requests_; }
80 int num_shutdown_requests() const { return num_shutdown_requests_; }
82 // LockStateControllerDelegate implementation.
83 virtual void RequestLockScreen() OVERRIDE {
84 num_lock_requests_++;
86 virtual void RequestShutdown() OVERRIDE {
87 num_shutdown_requests_++;
90 private:
91 int num_lock_requests_;
92 int num_shutdown_requests_;
94 DISALLOW_COPY_AND_ASSIGN(TestLockStateControllerDelegate);
97 class LockStateControllerTest : public AshTestBase {
98 public:
99 LockStateControllerTest() : controller_(NULL), delegate_(NULL) {}
100 virtual ~LockStateControllerTest() {}
102 virtual void SetUp() OVERRIDE {
103 AshTestBase::SetUp();
105 // We would control animations in a fine way:
106 animation_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
107 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION));
108 // TODO(antrim) : restore
109 // animator_helper_ = ui::test::CreateLayerAnimatorHelperForTest();
111 // Temporary disable animations so that observer is always called, and
112 // no leaks happen during tests.
113 animation_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
114 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
115 // TODO(antrim): once there is a way to mock time and run animations, make
116 // sure that animations are finished even in simple tests.
118 delegate_ = new TestLockStateControllerDelegate;
119 controller_ = Shell::GetInstance()->power_button_controller();
120 lock_state_controller_ = static_cast<LockStateController*>(
121 Shell::GetInstance()->lock_state_controller());
122 lock_state_controller_->SetDelegate(delegate_); // transfers ownership
123 test_api_.reset(new LockStateController::TestApi(lock_state_controller_));
124 animator_api_.reset(
125 new SessionStateAnimator::TestApi(lock_state_controller_->
126 animator_.get()));
127 shell_delegate_ = reinterpret_cast<TestShellDelegate*>(
128 ash::Shell::GetInstance()->delegate());
129 session_state_delegate_ = Shell::GetInstance()->session_state_delegate();
132 virtual void TearDown() {
133 // TODO(antrim) : restore
134 // animator_helper_->AdvanceUntilDone();
135 window_.reset();
136 AshTestBase::TearDown();
139 protected:
140 void GenerateMouseMoveEvent() {
141 aura::test::EventGenerator generator(
142 Shell::GetPrimaryRootWindow());
143 generator.MoveMouseTo(10, 10);
146 int NumShutdownRequests() {
147 return delegate_->num_shutdown_requests() +
148 shell_delegate_->num_exit_requests();
151 void Advance(SessionStateAnimator::AnimationSpeed speed) {
152 // TODO (antrim) : restore
153 // animator_helper_->Advance(SessionStateAnimator::GetDuration(speed));
156 void AdvancePartially(SessionStateAnimator::AnimationSpeed speed,
157 float factor) {
158 // TODO (antrim) : restore
159 // base::TimeDelta duration = SessionStateAnimator::GetDuration(speed);
160 // base::TimeDelta partial_duration =
161 // base::TimeDelta::FromInternalValue(duration.ToInternalValue() * factor);
162 // animator_helper_->Advance(partial_duration);
165 void ExpectPreLockAnimationStarted() {
166 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
167 EXPECT_TRUE(
168 animator_api_->ContainersAreAnimated(
169 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
170 SessionStateAnimator::ANIMATION_LIFT));
171 EXPECT_TRUE(
172 animator_api_->ContainersAreAnimated(
173 SessionStateAnimator::LAUNCHER,
174 SessionStateAnimator::ANIMATION_FADE_OUT));
175 EXPECT_TRUE(
176 animator_api_->ContainersAreAnimated(
177 SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
178 SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY));
179 EXPECT_TRUE(test_api_->is_animating_lock());
182 void ExpectPreLockAnimationCancel() {
183 EXPECT_TRUE(
184 animator_api_->ContainersAreAnimated(
185 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
186 SessionStateAnimator::ANIMATION_DROP));
187 EXPECT_TRUE(
188 animator_api_->ContainersAreAnimated(
189 SessionStateAnimator::LAUNCHER,
190 SessionStateAnimator::ANIMATION_FADE_IN));
193 void ExpectPreLockAnimationFinished() {
194 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
195 EXPECT_TRUE(
196 animator_api_->ContainersAreAnimated(
197 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
198 SessionStateAnimator::ANIMATION_LIFT));
199 EXPECT_TRUE(
200 animator_api_->ContainersAreAnimated(
201 SessionStateAnimator::LAUNCHER,
202 SessionStateAnimator::ANIMATION_FADE_OUT));
203 EXPECT_TRUE(
204 animator_api_->ContainersAreAnimated(
205 SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
206 SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY));
209 void ExpectPostLockAnimationStarted() {
210 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
211 EXPECT_TRUE(
212 animator_api_->ContainersAreAnimated(
213 SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
214 SessionStateAnimator::ANIMATION_RAISE_TO_SCREEN));
217 void ExpectPastLockAnimationFinished() {
218 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
219 EXPECT_TRUE(
220 animator_api_->ContainersAreAnimated(
221 SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
222 SessionStateAnimator::ANIMATION_RAISE_TO_SCREEN));
225 void ExpectUnlockBeforeUIDestroyedAnimationStarted() {
226 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
227 EXPECT_TRUE(
228 animator_api_->ContainersAreAnimated(
229 SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
230 SessionStateAnimator::ANIMATION_LIFT));
233 void ExpectUnlockBeforeUIDestroyedAnimationFinished() {
234 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
235 EXPECT_TRUE(
236 animator_api_->ContainersAreAnimated(
237 SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
238 SessionStateAnimator::ANIMATION_LIFT));
241 void ExpectUnlockAfterUIDestroyedAnimationStarted() {
242 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
243 EXPECT_TRUE(
244 animator_api_->ContainersAreAnimated(
245 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
246 SessionStateAnimator::ANIMATION_DROP));
247 EXPECT_TRUE(
248 animator_api_->ContainersAreAnimated(
249 SessionStateAnimator::LAUNCHER,
250 SessionStateAnimator::ANIMATION_FADE_IN));
253 void ExpectUnlockAfterUIDestroyedAnimationFinished() {
254 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
255 EXPECT_TRUE(
256 animator_api_->ContainersAreAnimated(
257 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
258 SessionStateAnimator::ANIMATION_DROP));
259 EXPECT_TRUE(
260 animator_api_->ContainersAreAnimated(
261 SessionStateAnimator::LAUNCHER,
262 SessionStateAnimator::ANIMATION_FADE_IN));
265 void ExpectShutdownAnimationStarted() {
266 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
267 EXPECT_TRUE(
268 animator_api_->RootWindowIsAnimated(
269 SessionStateAnimator::ANIMATION_GRAYSCALE_BRIGHTNESS));
272 void ExpectShutdownAnimationFinished() {
273 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
274 EXPECT_TRUE(
275 animator_api_->RootWindowIsAnimated(
276 SessionStateAnimator::ANIMATION_GRAYSCALE_BRIGHTNESS));
279 void ExpectShutdownAnimationCancel() {
280 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
281 EXPECT_TRUE(
282 animator_api_->RootWindowIsAnimated(
283 SessionStateAnimator::ANIMATION_UNDO_GRAYSCALE_BRIGHTNESS));
286 void ExpectBackgroundIsShowing() {
287 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
288 EXPECT_TRUE(
289 animator_api_->ContainersAreAnimated(
290 SessionStateAnimator::DESKTOP_BACKGROUND,
291 SessionStateAnimator::ANIMATION_FADE_IN));
294 void ExpectBackgroundIsHiding() {
295 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating());
296 EXPECT_TRUE(
297 animator_api_->ContainersAreAnimated(
298 SessionStateAnimator::DESKTOP_BACKGROUND,
299 SessionStateAnimator::ANIMATION_FADE_OUT));
302 void ExpectUnlockedState() {
303 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
304 EXPECT_FALSE(session_state_delegate_->IsScreenLocked());
306 aura::Window::Windows containers;
308 SessionStateAnimator::GetContainers(
309 SessionStateAnimator::LAUNCHER |
310 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
311 &containers);
312 for (aura::Window::Windows::const_iterator it = containers.begin();
313 it != containers.end(); ++it) {
314 aura::Window* window = *it;
315 ui::Layer* layer = window->layer();
316 EXPECT_EQ(1.0f, layer->opacity());
317 EXPECT_EQ(0.0f, layer->layer_brightness());
318 EXPECT_EQ(0.0f, layer->layer_saturation());
319 EXPECT_EQ(gfx::Transform(), layer->transform());
323 void ExpectLockedState() {
324 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
325 EXPECT_TRUE(session_state_delegate_->IsScreenLocked());
327 aura::Window::Windows containers;
329 SessionStateAnimator::GetContainers(
330 SessionStateAnimator::LOCK_SCREEN_RELATED_CONTAINERS |
331 SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
332 &containers);
333 for (aura::Window::Windows::const_iterator it = containers.begin();
334 it != containers.end(); ++it) {
335 aura::Window* window = *it;
336 ui::Layer* layer = window->layer();
337 EXPECT_EQ(1.0f, layer->opacity());
338 EXPECT_EQ(0.0f, layer->layer_brightness());
339 EXPECT_EQ(0.0f, layer->layer_saturation());
340 EXPECT_EQ(gfx::Transform(), layer->transform());
344 void PressPowerButton() {
345 controller_->OnPowerButtonEvent(true, base::TimeTicks::Now());
346 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta());
349 void ReleasePowerButton() {
350 controller_->OnPowerButtonEvent(false, base::TimeTicks::Now());
351 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta());
354 void PressLockButton() {
355 controller_->OnLockButtonEvent(true, base::TimeTicks::Now());
358 void ReleaseLockButton() {
359 controller_->OnLockButtonEvent(false, base::TimeTicks::Now());
362 void SystemLocks() {
363 lock_state_controller_->OnLockStateChanged(true);
364 session_state_delegate_->LockScreen();
365 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta());
368 void SuccessfulAuthentication(bool* call_flag) {
369 base::Closure closure = base::Bind(&CheckCalledCallback, call_flag);
370 lock_state_controller_->OnLockScreenHide(closure);
371 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta());
374 void SystemUnlocks() {
375 lock_state_controller_->OnLockStateChanged(false);
376 session_state_delegate_->UnlockScreen();
377 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta());
380 void Initialize(bool legacy_button, user::LoginStatus status) {
381 controller_->set_has_legacy_power_button_for_test(legacy_button);
382 lock_state_controller_->OnLoginStateChanged(status);
383 SetUserLoggedIn(status != user::LOGGED_IN_NONE);
384 if (status == user::LOGGED_IN_GUEST)
385 SetCanLockScreen(false);
386 lock_state_controller_->OnLockStateChanged(false);
389 void CreateWindowForLockscreen() {
390 window_.reset(new aura::Window(&window_delegate_));
391 window_->SetBounds(gfx::Rect(0, 0, 100, 100));
392 window_->SetType(ui::wm::WINDOW_TYPE_NORMAL);
393 window_->Init(aura::WINDOW_LAYER_TEXTURED);
394 window_->SetName("WINDOW");
395 aura::Window* container = Shell::GetContainer(
396 Shell::GetPrimaryRootWindow(), kShellWindowId_LockScreenContainer);
397 ASSERT_TRUE(container);
398 container->AddChild(window_.get());
399 window_->Show();
402 PowerButtonController* controller_; // not owned
403 LockStateController* lock_state_controller_; // not owned
404 TestLockStateControllerDelegate* delegate_; // not owned
405 TestShellDelegate* shell_delegate_; // not owned
406 SessionStateDelegate* session_state_delegate_; // not owned
408 aura::test::TestWindowDelegate window_delegate_;
409 scoped_ptr<aura::Window> window_;
410 scoped_ptr<ui::ScopedAnimationDurationScaleMode> animation_duration_mode_;
411 scoped_ptr<LockStateController::TestApi> test_api_;
412 scoped_ptr<SessionStateAnimator::TestApi> animator_api_;
413 // TODO(antrim) : restore
414 // scoped_ptr<ui::test::AnimationContainerTestHelper> animator_helper_;
416 private:
417 DISALLOW_COPY_AND_ASSIGN(LockStateControllerTest);
420 // Test the lock-to-shutdown flow for non-Chrome-OS hardware that doesn't
421 // correctly report power button releases. We should lock immediately the first
422 // time the button is pressed and shut down when it's pressed from the locked
423 // state.
424 // TODO(antrim): Reenable this: http://crbug.com/167048
425 TEST_F(LockStateControllerTest, DISABLED_LegacyLockAndShutDown) {
426 Initialize(true, user::LOGGED_IN_USER);
428 ExpectUnlockedState();
430 // We should request that the screen be locked immediately after seeing the
431 // power button get pressed.
432 PressPowerButton();
434 ExpectPreLockAnimationStarted();
436 EXPECT_FALSE(test_api_->is_lock_cancellable());
438 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
440 ExpectPreLockAnimationFinished();
441 EXPECT_EQ(1, delegate_->num_lock_requests());
443 // Notify that we locked successfully.
444 lock_state_controller_->OnStartingLock();
445 // We had that animation already.
446 //TODO (antrim) : restore
447 // EXPECT_FALSE(animator_helper_->IsAnimating());
449 SystemLocks();
451 ExpectPostLockAnimationStarted();
452 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
453 ExpectPastLockAnimationFinished();
455 // We shouldn't progress towards the shutdown state, however.
456 EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running());
457 EXPECT_FALSE(test_api_->shutdown_timer_is_running());
459 ReleasePowerButton();
461 // Hold the button again and check that we start shutting down.
462 PressPowerButton();
464 ExpectShutdownAnimationStarted();
466 EXPECT_EQ(0, NumShutdownRequests());
467 // Make sure a mouse move event won't show the cursor.
468 GenerateMouseMoveEvent();
469 EXPECT_FALSE(cursor_visible());
471 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
472 test_api_->trigger_real_shutdown_timeout();
473 EXPECT_EQ(1, NumShutdownRequests());
476 // Test that we start shutting down immediately if the power button is pressed
477 // while we're not logged in on an unofficial system.
478 TEST_F(LockStateControllerTest, LegacyNotLoggedIn) {
479 Initialize(true, user::LOGGED_IN_NONE);
481 PressPowerButton();
482 ExpectShutdownAnimationStarted();
484 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
487 // Test that we start shutting down immediately if the power button is pressed
488 // while we're logged in as a guest on an unofficial system.
489 TEST_F(LockStateControllerTest, LegacyGuest) {
490 Initialize(true, user::LOGGED_IN_GUEST);
492 PressPowerButton();
493 ExpectShutdownAnimationStarted();
495 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
498 // When we hold the power button while the user isn't logged in, we should shut
499 // down the machine directly.
500 TEST_F(LockStateControllerTest, ShutdownWhenNotLoggedIn) {
501 Initialize(false, user::LOGGED_IN_NONE);
503 // Press the power button and check that we start the shutdown timer.
504 PressPowerButton();
505 EXPECT_FALSE(test_api_->is_animating_lock());
506 EXPECT_TRUE(test_api_->shutdown_timer_is_running());
507 ExpectShutdownAnimationStarted();
509 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN, 0.5f);
511 // Release the power button before the shutdown timer fires.
512 ReleasePowerButton();
514 EXPECT_FALSE(test_api_->shutdown_timer_is_running());
515 ExpectShutdownAnimationCancel();
517 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_REVERT, 0.5f);
519 // Press the button again and make the shutdown timeout fire this time.
520 // Check that we start the timer for actually requesting the shutdown.
521 PressPowerButton();
523 EXPECT_TRUE(test_api_->shutdown_timer_is_running());
525 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
526 ExpectShutdownAnimationFinished();
527 test_api_->trigger_shutdown_timeout();
529 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
530 EXPECT_EQ(0, NumShutdownRequests());
532 // When the timout fires, we should request a shutdown.
533 test_api_->trigger_real_shutdown_timeout();
535 EXPECT_EQ(1, NumShutdownRequests());
538 // Test that we lock the screen and deal with unlocking correctly.
539 // TODO(antrim): Reenable this: http://crbug.com/167048
540 TEST_F(LockStateControllerTest, DISABLED_LockAndUnlock) {
541 Initialize(false, user::LOGGED_IN_USER);
543 ExpectUnlockedState();
545 // Press the power button and check that the lock timer is started and that we
546 // start lifting the non-screen-locker containers.
547 PressPowerButton();
549 ExpectPreLockAnimationStarted();
550 EXPECT_TRUE(test_api_->is_lock_cancellable());
551 EXPECT_EQ(0, delegate_->num_lock_requests());
553 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
554 ExpectPreLockAnimationFinished();
556 EXPECT_EQ(1, delegate_->num_lock_requests());
558 // Notify that we locked successfully.
559 lock_state_controller_->OnStartingLock();
560 // We had that animation already.
561 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating());
563 SystemLocks();
565 ExpectPostLockAnimationStarted();
566 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
567 ExpectPastLockAnimationFinished();
569 // When we release the power button, the lock-to-shutdown timer should be
570 // stopped.
571 ExpectLockedState();
572 EXPECT_TRUE(test_api_->lock_to_shutdown_timer_is_running());
573 ReleasePowerButton();
574 ExpectLockedState();
575 EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running());
577 // Notify that the screen has been unlocked. We should show the
578 // non-screen-locker windows.
579 bool called = false;
580 SuccessfulAuthentication(&called);
582 ExpectUnlockBeforeUIDestroyedAnimationStarted();
583 EXPECT_FALSE(called);
584 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
585 ExpectUnlockBeforeUIDestroyedAnimationFinished();
587 EXPECT_TRUE(called);
589 SystemUnlocks();
591 ExpectUnlockAfterUIDestroyedAnimationStarted();
592 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
593 ExpectUnlockAfterUIDestroyedAnimationFinished();
595 ExpectUnlockedState();
598 // Test that we deal with cancelling lock correctly.
599 // TODO(antrim): Reenable this: http://crbug.com/167048
600 TEST_F(LockStateControllerTest, DISABLED_LockAndCancel) {
601 Initialize(false, user::LOGGED_IN_USER);
603 ExpectUnlockedState();
605 // Press the power button and check that the lock timer is started and that we
606 // start lifting the non-screen-locker containers.
607 PressPowerButton();
609 ExpectPreLockAnimationStarted();
610 EXPECT_TRUE(test_api_->is_lock_cancellable());
612 // forward only half way through
613 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
615 gfx::Transform transform_before_button_released =
616 GetContainer(kShellWindowId_DefaultContainer)->layer()->transform();
618 // Release the button before the lock timer fires.
619 ReleasePowerButton();
621 ExpectPreLockAnimationCancel();
623 gfx::Transform transform_after_button_released =
624 GetContainer(kShellWindowId_DefaultContainer)->layer()->transform();
625 // Expect no flickering, animation should proceed from mid-state.
626 EXPECT_EQ(transform_before_button_released, transform_after_button_released);
628 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
629 ExpectUnlockedState();
630 EXPECT_EQ(0, delegate_->num_lock_requests());
633 // Test that we deal with cancelling lock correctly.
634 // TODO(antrim): Reenable this: http://crbug.com/167048
635 TEST_F(LockStateControllerTest,
636 DISABLED_LockAndCancelAndLockAgain) {
637 Initialize(false, user::LOGGED_IN_USER);
639 ExpectUnlockedState();
641 // Press the power button and check that the lock timer is started and that we
642 // start lifting the non-screen-locker containers.
643 PressPowerButton();
645 ExpectPreLockAnimationStarted();
646 EXPECT_TRUE(test_api_->is_lock_cancellable());
648 // forward only half way through
649 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
651 // Release the button before the lock timer fires.
652 ReleasePowerButton();
653 ExpectPreLockAnimationCancel();
655 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS, 0.5f);
657 PressPowerButton();
658 ExpectPreLockAnimationStarted();
659 EXPECT_TRUE(test_api_->is_lock_cancellable());
661 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.6f);
663 EXPECT_EQ(0, delegate_->num_lock_requests());
664 ExpectPreLockAnimationStarted();
666 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.6f);
667 ExpectPreLockAnimationFinished();
668 EXPECT_EQ(1, delegate_->num_lock_requests());
671 // Hold the power button down from the unlocked state to eventual shutdown.
672 // TODO(antrim): Reenable this: http://crbug.com/167048
673 TEST_F(LockStateControllerTest, DISABLED_LockToShutdown) {
674 Initialize(false, user::LOGGED_IN_USER);
676 // Hold the power button and lock the screen.
677 PressPowerButton();
678 EXPECT_TRUE(test_api_->is_animating_lock());
680 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
681 SystemLocks();
682 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
684 // When the lock-to-shutdown timeout fires, we should start the shutdown
685 // timer.
686 EXPECT_TRUE(test_api_->lock_to_shutdown_timer_is_running());
688 test_api_->trigger_lock_to_shutdown_timeout();
690 ExpectShutdownAnimationStarted();
691 EXPECT_TRUE(test_api_->shutdown_timer_is_running());
693 // Fire the shutdown timeout and check that we request shutdown.
694 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
695 ExpectShutdownAnimationFinished();
696 test_api_->trigger_shutdown_timeout();
698 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
699 EXPECT_EQ(0, NumShutdownRequests());
700 test_api_->trigger_real_shutdown_timeout();
701 EXPECT_EQ(1, NumShutdownRequests());
704 // Hold the power button down from the unlocked state to eventual shutdown,
705 // then release the button while system does locking.
706 TEST_F(LockStateControllerTest, CancelLockToShutdown) {
707 Initialize(false, user::LOGGED_IN_USER);
709 PressPowerButton();
711 // Hold the power button and lock the screen.
712 EXPECT_TRUE(test_api_->is_animating_lock());
714 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
715 SystemLocks();
716 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS, 0.5f);
718 // Power button is released while system attempts to lock.
719 ReleasePowerButton();
721 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
723 EXPECT_FALSE(lock_state_controller_->ShutdownRequested());
724 EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running());
725 EXPECT_FALSE(test_api_->shutdown_timer_is_running());
728 // Test that we handle the case where lock requests are ignored.
729 // TODO(antrim): Reenable this: http://crbug.com/167048
730 TEST_F(LockStateControllerTest, DISABLED_Lock) {
731 // We require animations to have a duration for this test.
732 ui::ScopedAnimationDurationScaleMode normal_duration_mode(
733 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
735 Initialize(false, user::LOGGED_IN_USER);
737 // Hold the power button and lock the screen.
738 PressPowerButton();
739 ExpectPreLockAnimationStarted();
741 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
743 EXPECT_EQ(1, delegate_->num_lock_requests());
744 EXPECT_TRUE(test_api_->lock_fail_timer_is_running());
745 // We shouldn't start the lock-to-shutdown timer until the screen has actually
746 // been locked and this was animated.
747 EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running());
749 // Act as if the request timed out. We should restore the windows.
750 test_api_->trigger_lock_fail_timeout();
752 ExpectUnlockAfterUIDestroyedAnimationStarted();
753 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
754 ExpectUnlockAfterUIDestroyedAnimationFinished();
755 ExpectUnlockedState();
758 // Test the basic operation of the lock button (not logged in).
759 TEST_F(LockStateControllerTest, LockButtonBasicNotLoggedIn) {
760 // The lock button shouldn't do anything if we aren't logged in.
761 Initialize(false, user::LOGGED_IN_NONE);
763 PressLockButton();
764 EXPECT_FALSE(test_api_->is_animating_lock());
765 ReleaseLockButton();
766 EXPECT_EQ(0, delegate_->num_lock_requests());
769 // Test the basic operation of the lock button (guest).
770 TEST_F(LockStateControllerTest, LockButtonBasicGuest) {
771 // The lock button shouldn't do anything when we're logged in as a guest.
772 Initialize(false, user::LOGGED_IN_GUEST);
774 PressLockButton();
775 EXPECT_FALSE(test_api_->is_animating_lock());
776 ReleaseLockButton();
777 EXPECT_EQ(0, delegate_->num_lock_requests());
780 // Test the basic operation of the lock button.
781 // TODO(antrim): Reenable this: http://crbug.com/167048
782 TEST_F(LockStateControllerTest, DISABLED_LockButtonBasic) {
783 // If we're logged in as a regular user, we should start the lock timer and
784 // the pre-lock animation.
785 Initialize(false, user::LOGGED_IN_USER);
787 PressLockButton();
788 ExpectPreLockAnimationStarted();
789 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
791 // If the button is released immediately, we shouldn't lock the screen.
792 ReleaseLockButton();
793 ExpectPreLockAnimationCancel();
794 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
796 ExpectUnlockedState();
797 EXPECT_EQ(0, delegate_->num_lock_requests());
799 // Press the button again and let the lock timeout fire. We should request
800 // that the screen be locked.
801 PressLockButton();
802 ExpectPreLockAnimationStarted();
803 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
804 EXPECT_EQ(1, delegate_->num_lock_requests());
806 // Pressing the lock button while we have a pending lock request shouldn't do
807 // anything.
808 ReleaseLockButton();
809 PressLockButton();
810 ExpectPreLockAnimationFinished();
811 ReleaseLockButton();
813 // Pressing the button also shouldn't do anything after the screen is locked.
814 SystemLocks();
815 ExpectPostLockAnimationStarted();
817 PressLockButton();
818 ReleaseLockButton();
819 ExpectPostLockAnimationStarted();
821 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
822 ExpectPastLockAnimationFinished();
824 PressLockButton();
825 ReleaseLockButton();
826 ExpectPastLockAnimationFinished();
829 // Test that the power button takes priority over the lock button.
830 // TODO(antrim): Reenable this: http://crbug.com/167048
831 TEST_F(LockStateControllerTest,
832 DISABLED_PowerButtonPreemptsLockButton) {
833 Initialize(false, user::LOGGED_IN_USER);
835 // While the lock button is down, hold the power button.
836 PressLockButton();
837 ExpectPreLockAnimationStarted();
838 PressPowerButton();
839 ExpectPreLockAnimationStarted();
841 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
843 // The lock timer shouldn't be stopped when the lock button is released.
844 ReleaseLockButton();
845 ExpectPreLockAnimationStarted();
846 ReleasePowerButton();
847 ExpectPreLockAnimationCancel();
849 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
850 ExpectUnlockedState();
852 // Now press the power button first and then the lock button.
853 PressPowerButton();
854 ExpectPreLockAnimationStarted();
855 PressLockButton();
856 ExpectPreLockAnimationStarted();
858 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
860 // Releasing the power button should stop the lock timer.
861 ReleasePowerButton();
862 ExpectPreLockAnimationCancel();
863 ReleaseLockButton();
864 ExpectPreLockAnimationCancel();
867 // When the screen is locked without going through the usual power-button
868 // slow-close path (e.g. via the wrench menu), test that we still show the
869 // fast-close animation.
870 TEST_F(LockStateControllerTest, LockWithoutButton) {
871 Initialize(false, user::LOGGED_IN_USER);
872 lock_state_controller_->OnStartingLock();
874 ExpectPreLockAnimationStarted();
875 EXPECT_FALSE(test_api_->is_lock_cancellable());
877 // TODO(antrim): After time-faking is fixed, let the pre-lock animation
878 // complete here and check that delegate_->num_lock_requests() is 0 to
879 // prevent http://crbug.com/172487 from regressing.
882 // When we hear that the process is exiting but we haven't had a chance to
883 // display an animation, we should just blank the screen.
884 TEST_F(LockStateControllerTest, ShutdownWithoutButton) {
885 Initialize(false, user::LOGGED_IN_USER);
886 lock_state_controller_->OnAppTerminating();
888 EXPECT_TRUE(
889 animator_api_->ContainersAreAnimated(
890 SessionStateAnimator::kAllContainersMask,
891 SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY));
892 GenerateMouseMoveEvent();
893 EXPECT_FALSE(cursor_visible());
896 // Test that we display the fast-close animation and shut down when we get an
897 // outside request to shut down (e.g. from the login or lock screen).
898 TEST_F(LockStateControllerTest, RequestShutdownFromLoginScreen) {
899 Initialize(false, user::LOGGED_IN_NONE);
900 CreateWindowForLockscreen();
902 lock_state_controller_->RequestShutdown();
904 ExpectShutdownAnimationStarted();
905 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
907 GenerateMouseMoveEvent();
908 EXPECT_FALSE(cursor_visible());
910 EXPECT_EQ(0, NumShutdownRequests());
911 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
912 test_api_->trigger_real_shutdown_timeout();
913 EXPECT_EQ(1, NumShutdownRequests());
916 TEST_F(LockStateControllerTest, RequestShutdownFromLockScreen) {
917 Initialize(false, user::LOGGED_IN_USER);
919 SystemLocks();
920 CreateWindowForLockscreen();
921 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
922 ExpectPastLockAnimationFinished();
924 lock_state_controller_->RequestShutdown();
926 ExpectShutdownAnimationStarted();
927 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
929 GenerateMouseMoveEvent();
930 EXPECT_FALSE(cursor_visible());
932 EXPECT_EQ(0, NumShutdownRequests());
933 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running());
934 test_api_->trigger_real_shutdown_timeout();
935 EXPECT_EQ(1, NumShutdownRequests());
938 // TODO(antrim): Reenable this: http://crbug.com/167048
939 TEST_F(LockStateControllerTest,
940 DISABLED_RequestAndCancelShutdownFromLockScreen) {
941 Initialize(false, user::LOGGED_IN_USER);
943 SystemLocks();
944 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
945 ExpectLockedState();
947 // Press the power button and check that we start the shutdown timer.
948 PressPowerButton();
949 EXPECT_FALSE(test_api_->is_animating_lock());
950 EXPECT_TRUE(test_api_->shutdown_timer_is_running());
952 ExpectShutdownAnimationStarted();
954 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN, 0.5f);
956 float grayscale_before_button_release =
957 Shell::GetPrimaryRootWindow()->layer()->layer_grayscale();
959 // Release the power button before the shutdown timer fires.
960 ReleasePowerButton();
962 EXPECT_FALSE(test_api_->shutdown_timer_is_running());
964 ExpectShutdownAnimationCancel();
966 float grayscale_after_button_release =
967 Shell::GetPrimaryRootWindow()->layer()->layer_grayscale();
968 // Expect no flickering in undo animation.
969 EXPECT_EQ(grayscale_before_button_release, grayscale_after_button_release);
971 Advance(SessionStateAnimator::ANIMATION_SPEED_REVERT);
972 ExpectLockedState();
975 // Test that we ignore power button presses when the screen is turned off.
976 TEST_F(LockStateControllerTest, IgnorePowerButtonIfScreenIsOff) {
977 Initialize(false, user::LOGGED_IN_USER);
979 // When the screen brightness is at 0%, we shouldn't do anything in response
980 // to power button presses.
981 controller_->OnScreenBrightnessChanged(0.0);
982 PressPowerButton();
983 EXPECT_FALSE(test_api_->is_animating_lock());
984 ReleasePowerButton();
986 // After increasing the brightness to 10%, we should start the timer like
987 // usual.
988 controller_->OnScreenBrightnessChanged(10.0);
989 PressPowerButton();
990 EXPECT_TRUE(test_api_->is_animating_lock());
991 ReleasePowerButton();
994 #if defined(OS_CHROMEOS) && defined(USE_X11)
995 TEST_F(LockStateControllerTest, HonorPowerButtonInDockedMode) {
996 ScopedVector<const ui::DisplayMode> modes;
997 modes.push_back(new ui::DisplayMode(gfx::Size(1, 1), false, 60.0f));
999 // Create two outputs, the first internal and the second external.
1000 ui::DisplayConfigurator::DisplayStateList outputs;
1001 ui::DisplayConfigurator::DisplayState internal_output;
1002 ui::TestDisplaySnapshot internal_display;
1003 internal_display.set_type(ui::DISPLAY_CONNECTION_TYPE_INTERNAL);
1004 internal_display.set_modes(modes.get());
1005 internal_output.display = &internal_display;
1006 outputs.push_back(internal_output);
1008 ui::DisplayConfigurator::DisplayState external_output;
1009 ui::TestDisplaySnapshot external_display;
1010 external_display.set_type(ui::DISPLAY_CONNECTION_TYPE_HDMI);
1011 external_display.set_modes(modes.get());
1012 external_output.display = &external_display;
1013 outputs.push_back(external_output);
1015 // When all of the displays are turned off (e.g. due to user inactivity), the
1016 // power button should be ignored.
1017 controller_->OnScreenBrightnessChanged(0.0);
1018 static_cast<ui::TestDisplaySnapshot*>(outputs[0].display)
1019 ->set_current_mode(NULL);
1020 static_cast<ui::TestDisplaySnapshot*>(outputs[1].display)
1021 ->set_current_mode(NULL);
1022 controller_->OnDisplayModeChanged(outputs);
1023 PressPowerButton();
1024 EXPECT_FALSE(test_api_->is_animating_lock());
1025 ReleasePowerButton();
1027 // When the screen brightness is 0% but the external display is still turned
1028 // on (indicating either docked mode or the user having manually decreased the
1029 // brightness to 0%), the power button should still be handled.
1030 static_cast<ui::TestDisplaySnapshot*>(outputs[1].display)
1031 ->set_current_mode(modes[0]);
1032 controller_->OnDisplayModeChanged(outputs);
1033 PressPowerButton();
1034 EXPECT_TRUE(test_api_->is_animating_lock());
1035 ReleasePowerButton();
1037 #endif
1039 // Test that hidden background appears and revers correctly on lock/cancel.
1040 // TODO(antrim): Reenable this: http://crbug.com/167048
1041 TEST_F(LockStateControllerTest, DISABLED_TestHiddenBackgroundLockCancel) {
1042 Initialize(false, user::LOGGED_IN_USER);
1043 HideBackground();
1045 EXPECT_TRUE(IsBackgroundHidden());
1046 ExpectUnlockedState();
1047 PressPowerButton();
1049 ExpectPreLockAnimationStarted();
1050 EXPECT_FALSE(IsBackgroundHidden());
1051 ExpectBackgroundIsShowing();
1053 // Forward only half way through.
1054 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
1056 // Release the button before the lock timer fires.
1057 ReleasePowerButton();
1058 ExpectPreLockAnimationCancel();
1059 ExpectBackgroundIsHiding();
1060 EXPECT_FALSE(IsBackgroundHidden());
1062 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
1064 ExpectUnlockedState();
1065 EXPECT_TRUE(IsBackgroundHidden());
1068 // Test that hidden background appears and revers correctly on lock/unlock.
1069 // TODO(antrim): Reenable this: http://crbug.com/167048
1070 TEST_F(LockStateControllerTest, DISABLED_TestHiddenBackgroundLockUnlock) {
1071 Initialize(false, user::LOGGED_IN_USER);
1072 HideBackground();
1074 EXPECT_TRUE(IsBackgroundHidden());
1075 ExpectUnlockedState();
1077 // Press the power button and check that the lock timer is started and that we
1078 // start lifting the non-screen-locker containers.
1079 PressPowerButton();
1081 ExpectPreLockAnimationStarted();
1082 EXPECT_FALSE(IsBackgroundHidden());
1083 ExpectBackgroundIsShowing();
1085 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
1087 ExpectPreLockAnimationFinished();
1089 SystemLocks();
1091 ReleasePowerButton();
1093 ExpectPostLockAnimationStarted();
1094 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
1095 ExpectPastLockAnimationFinished();
1097 ExpectLockedState();
1099 SuccessfulAuthentication(NULL);
1101 ExpectUnlockBeforeUIDestroyedAnimationStarted();
1102 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
1103 ExpectUnlockBeforeUIDestroyedAnimationFinished();
1105 SystemUnlocks();
1107 ExpectUnlockAfterUIDestroyedAnimationStarted();
1108 ExpectBackgroundIsHiding();
1109 EXPECT_FALSE(IsBackgroundHidden());
1111 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
1112 ExpectUnlockAfterUIDestroyedAnimationFinished();
1113 EXPECT_TRUE(IsBackgroundHidden());
1115 ExpectUnlockedState();
1118 } // namespace test
1119 } // namespace ash