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. 4 5 #include "ash/wm/lock_state_controller.h" 6 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/event_generator.h" 21 #include "ui/aura/test/test_window_delegate.h" 22 #include "ui/aura/window_event_dispatcher.h" 23 #include "ui/compositor/layer_animator.h" 24 #include "ui/compositor/scoped_animation_duration_scale_mode.h" 25 #include "ui/compositor/scoped_layer_animation_settings.h" 26 #include "ui/gfx/rect.h" 27 #include "ui/gfx/size.h" 28 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 34 35 #if defined(OS_WIN) 36 #include "base/win/windows_version.h" 37 #endif 38 39 namespace ash { 40 namespace test { 41 namespace { 42 43 bool cursor_visible() { 44 return ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible(); 45 } 46 47 void CheckCalledCallback(bool* flag) { 48 if (flag) 49 (*flag) = true; 50 } 51 52 aura::Window* GetContainer(int container ) { 53 aura::Window* root_window = Shell::GetPrimaryRootWindow(); 54 return Shell::GetContainer(root_window, container); 55 } 56 57 bool IsBackgroundHidden() { 58 return !GetContainer(kShellWindowId_DesktopBackgroundContainer)->IsVisible(); 59 } 60 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(); 68 } 69 70 } // namespace 71 72 class LockStateControllerTest : public AshTestBase { 73 public: 74 LockStateControllerTest() : controller_(NULL), delegate_(NULL) {} 75 virtual ~LockStateControllerTest() {} 76 77 virtual void SetUp() OVERRIDE { 78 AshTestBase::SetUp(); 79 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(); 85 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. 92 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(); 105 } 106 107 virtual void TearDown() { 108 // TODO(antrim) : restore 109 // animator_helper_->AdvanceUntilDone(); 110 window_.reset(); 111 AshTestBase::TearDown(); 112 } 113 114 protected: 115 void GenerateMouseMoveEvent() { 116 aura::test::EventGenerator generator( 117 Shell::GetPrimaryRootWindow()); 118 generator.MoveMouseTo(10, 10); 119 } 120 121 int NumShutdownRequests() { 122 return delegate_->num_shutdown_requests() + 123 shell_delegate_->num_exit_requests(); 124 } 125 126 void Advance(SessionStateAnimator::AnimationSpeed speed) { 127 // TODO (antrim) : restore 128 // animator_helper_->Advance(SessionStateAnimator::GetDuration(speed)); 129 } 130 131 void AdvancePartially(SessionStateAnimator::AnimationSpeed speed, 132 float factor) { 133 // TODO (antrim) : restore 134 // base::TimeDelta duration = SessionStateAnimator::GetDuration(speed); 135 // base::TimeDelta partial_duration = 136 // base::TimeDelta::FromInternalValue(duration.ToInternalValue() * factor); 137 // animator_helper_->Advance(partial_duration); 138 } 139 140 void ExpectPreLockAnimationStarted() { 141 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating()); 142 EXPECT_TRUE( 143 animator_api_->ContainersAreAnimated( 144 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS, 145 SessionStateAnimator::ANIMATION_LIFT)); 146 EXPECT_TRUE( 147 animator_api_->ContainersAreAnimated( 148 SessionStateAnimator::LAUNCHER, 149 SessionStateAnimator::ANIMATION_FADE_OUT)); 150 EXPECT_TRUE( 151 animator_api_->ContainersAreAnimated( 152 SessionStateAnimator::LOCK_SCREEN_CONTAINERS, 153 SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY)); 154 EXPECT_TRUE(test_api_->is_animating_lock()); 155 } 156 157 void ExpectPreLockAnimationCancel() { 158 EXPECT_TRUE( 159 animator_api_->ContainersAreAnimated( 160 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS, 161 SessionStateAnimator::ANIMATION_DROP)); 162 EXPECT_TRUE( 163 animator_api_->ContainersAreAnimated( 164 SessionStateAnimator::LAUNCHER, 165 SessionStateAnimator::ANIMATION_FADE_IN)); 166 } 167 168 void ExpectPreLockAnimationFinished() { 169 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating()); 170 EXPECT_TRUE( 171 animator_api_->ContainersAreAnimated( 172 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS, 173 SessionStateAnimator::ANIMATION_LIFT)); 174 EXPECT_TRUE( 175 animator_api_->ContainersAreAnimated( 176 SessionStateAnimator::LAUNCHER, 177 SessionStateAnimator::ANIMATION_FADE_OUT)); 178 EXPECT_TRUE( 179 animator_api_->ContainersAreAnimated( 180 SessionStateAnimator::LOCK_SCREEN_CONTAINERS, 181 SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY)); 182 } 183 184 void ExpectPostLockAnimationStarted() { 185 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating()); 186 EXPECT_TRUE( 187 animator_api_->ContainersAreAnimated( 188 SessionStateAnimator::LOCK_SCREEN_CONTAINERS, 189 SessionStateAnimator::ANIMATION_RAISE_TO_SCREEN)); 190 } 191 192 void ExpectPastLockAnimationFinished() { 193 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating()); 194 EXPECT_TRUE( 195 animator_api_->ContainersAreAnimated( 196 SessionStateAnimator::LOCK_SCREEN_CONTAINERS, 197 SessionStateAnimator::ANIMATION_RAISE_TO_SCREEN)); 198 } 199 200 void ExpectUnlockBeforeUIDestroyedAnimationStarted() { 201 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating()); 202 EXPECT_TRUE( 203 animator_api_->ContainersAreAnimated( 204 SessionStateAnimator::LOCK_SCREEN_CONTAINERS, 205 SessionStateAnimator::ANIMATION_LIFT)); 206 } 207 208 void ExpectUnlockBeforeUIDestroyedAnimationFinished() { 209 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating()); 210 EXPECT_TRUE( 211 animator_api_->ContainersAreAnimated( 212 SessionStateAnimator::LOCK_SCREEN_CONTAINERS, 213 SessionStateAnimator::ANIMATION_LIFT)); 214 } 215 216 void ExpectUnlockAfterUIDestroyedAnimationStarted() { 217 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating()); 218 EXPECT_TRUE( 219 animator_api_->ContainersAreAnimated( 220 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS, 221 SessionStateAnimator::ANIMATION_DROP)); 222 EXPECT_TRUE( 223 animator_api_->ContainersAreAnimated( 224 SessionStateAnimator::LAUNCHER, 225 SessionStateAnimator::ANIMATION_FADE_IN)); 226 } 227 228 void ExpectUnlockAfterUIDestroyedAnimationFinished() { 229 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating()); 230 EXPECT_TRUE( 231 animator_api_->ContainersAreAnimated( 232 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS, 233 SessionStateAnimator::ANIMATION_DROP)); 234 EXPECT_TRUE( 235 animator_api_->ContainersAreAnimated( 236 SessionStateAnimator::LAUNCHER, 237 SessionStateAnimator::ANIMATION_FADE_IN)); 238 } 239 240 void ExpectShutdownAnimationStarted() { 241 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating()); 242 EXPECT_TRUE( 243 animator_api_->RootWindowIsAnimated( 244 SessionStateAnimator::ANIMATION_GRAYSCALE_BRIGHTNESS)); 245 } 246 247 void ExpectShutdownAnimationFinished() { 248 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating()); 249 EXPECT_TRUE( 250 animator_api_->RootWindowIsAnimated( 251 SessionStateAnimator::ANIMATION_GRAYSCALE_BRIGHTNESS)); 252 } 253 254 void ExpectShutdownAnimationCancel() { 255 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating()); 256 EXPECT_TRUE( 257 animator_api_->RootWindowIsAnimated( 258 SessionStateAnimator::ANIMATION_UNDO_GRAYSCALE_BRIGHTNESS)); 259 } 260 261 void ExpectBackgroundIsShowing() { 262 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating()); 263 EXPECT_TRUE( 264 animator_api_->ContainersAreAnimated( 265 SessionStateAnimator::DESKTOP_BACKGROUND, 266 SessionStateAnimator::ANIMATION_FADE_IN)); 267 } 268 269 void ExpectBackgroundIsHiding() { 270 //TODO (antrim) : restore EXPECT_TRUE(animator_helper_->IsAnimating()); 271 EXPECT_TRUE( 272 animator_api_->ContainersAreAnimated( 273 SessionStateAnimator::DESKTOP_BACKGROUND, 274 SessionStateAnimator::ANIMATION_FADE_OUT)); 275 } 276 277 void ExpectUnlockedState() { 278 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating()); 279 EXPECT_FALSE(session_state_delegate_->IsScreenLocked()); 280 281 aura::Window::Windows containers; 282 283 SessionStateAnimator::GetContainers( 284 SessionStateAnimator::LAUNCHER | 285 SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS, 286 &containers); 287 for (aura::Window::Windows::const_iterator it = containers.begin(); 288 it != containers.end(); ++it) { 289 aura::Window* window = *it; 290 ui::Layer* layer = window->layer(); 291 EXPECT_EQ(1.0f, layer->opacity()); 292 EXPECT_EQ(0.0f, layer->layer_brightness()); 293 EXPECT_EQ(0.0f, layer->layer_saturation()); 294 EXPECT_EQ(gfx::Transform(), layer->transform()); 295 } 296 } 297 298 void ExpectLockedState() { 299 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating()); 300 EXPECT_TRUE(session_state_delegate_->IsScreenLocked()); 301 302 aura::Window::Windows containers; 303 304 SessionStateAnimator::GetContainers( 305 SessionStateAnimator::LOCK_SCREEN_RELATED_CONTAINERS | 306 SessionStateAnimator::LOCK_SCREEN_CONTAINERS, 307 &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()); 316 } 317 } 318 319 void PressPowerButton() { 320 controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); 321 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta()); 322 } 323 324 void ReleasePowerButton() { 325 controller_->OnPowerButtonEvent(false, base::TimeTicks::Now()); 326 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta()); 327 } 328 329 void PressLockButton() { 330 controller_->OnLockButtonEvent(true, base::TimeTicks::Now()); 331 } 332 333 void ReleaseLockButton() { 334 controller_->OnLockButtonEvent(false, base::TimeTicks::Now()); 335 } 336 337 void SystemLocks() { 338 lock_state_controller_->OnLockStateChanged(true); 339 session_state_delegate_->LockScreen(); 340 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta()); 341 } 342 343 void SuccessfulAuthentication(bool* call_flag) { 344 base::Closure closure = base::Bind(&CheckCalledCallback, call_flag); 345 lock_state_controller_->OnLockScreenHide(closure); 346 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta()); 347 } 348 349 void SystemUnlocks() { 350 lock_state_controller_->OnLockStateChanged(false); 351 session_state_delegate_->UnlockScreen(); 352 //TODO (antrim) : restore animator_helper_->Advance(base::TimeDelta()); 353 } 354 355 void Initialize(bool legacy_button, user::LoginStatus status) { 356 controller_->set_has_legacy_power_button_for_test(legacy_button); 357 lock_state_controller_->OnLoginStateChanged(status); 358 SetUserLoggedIn(status != user::LOGGED_IN_NONE); 359 if (status == user::LOGGED_IN_GUEST) 360 SetCanLockScreen(false); 361 lock_state_controller_->OnLockStateChanged(false); 362 } 363 364 void CreateWindowForLockscreen() { 365 window_.reset(new aura::Window(&window_delegate_)); 366 window_->SetBounds(gfx::Rect(0, 0, 100, 100)); 367 window_->SetType(ui::wm::WINDOW_TYPE_NORMAL); 368 window_->Init(aura::WINDOW_LAYER_TEXTURED); 369 window_->SetName("WINDOW"); 370 aura::Window* container = Shell::GetContainer( 371 Shell::GetPrimaryRootWindow(), kShellWindowId_LockScreenContainer); 372 ASSERT_TRUE(container); 373 container->AddChild(window_.get()); 374 window_->Show(); 375 } 376 377 PowerButtonController* controller_; // not owned 378 LockStateController* lock_state_controller_; // not owned 379 TestLockStateControllerDelegate* delegate_; // not owned 380 TestShellDelegate* shell_delegate_; // not owned 381 SessionStateDelegate* session_state_delegate_; // not owned 382 383 aura::test::TestWindowDelegate window_delegate_; 384 scoped_ptr<aura::Window> window_; 385 scoped_ptr<ui::ScopedAnimationDurationScaleMode> animation_duration_mode_; 386 scoped_ptr<LockStateController::TestApi> test_api_; 387 scoped_ptr<SessionStateAnimator::TestApi> animator_api_; 388 // TODO(antrim) : restore 389 // scoped_ptr<ui::test::AnimationContainerTestHelper> animator_helper_; 390 391 private: 392 DISALLOW_COPY_AND_ASSIGN(LockStateControllerTest); 393 }; 394 395 // Test the lock-to-shutdown flow for non-Chrome-OS hardware that doesn't 396 // correctly report power button releases. We should lock immediately the first 397 // time the button is pressed and shut down when it's pressed from the locked 398 // state. 399 // TODO(antrim): Reenable this: http://crbug.com/167048 400 TEST_F(LockStateControllerTest, DISABLED_LegacyLockAndShutDown) { 401 Initialize(true, user::LOGGED_IN_USER); 402 403 ExpectUnlockedState(); 404 405 // We should request that the screen be locked immediately after seeing the 406 // power button get pressed. 407 PressPowerButton(); 408 409 ExpectPreLockAnimationStarted(); 410 411 EXPECT_FALSE(test_api_->is_lock_cancellable()); 412 413 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS); 414 415 ExpectPreLockAnimationFinished(); 416 EXPECT_EQ(1, delegate_->num_lock_requests()); 417 418 // Notify that we locked successfully. 419 lock_state_controller_->OnStartingLock(); 420 // We had that animation already. 421 //TODO (antrim) : restore 422 // EXPECT_FALSE(animator_helper_->IsAnimating()); 423 424 SystemLocks(); 425 426 ExpectPostLockAnimationStarted(); 427 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS); 428 ExpectPastLockAnimationFinished(); 429 430 // We shouldn't progress towards the shutdown state, however. 431 EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running()); 432 EXPECT_FALSE(test_api_->shutdown_timer_is_running()); 433 434 ReleasePowerButton(); 435 436 // Hold the button again and check that we start shutting down. 437 PressPowerButton(); 438 439 ExpectShutdownAnimationStarted(); 440 441 EXPECT_EQ(0, NumShutdownRequests()); 442 // Make sure a mouse move event won't show the cursor. 443 GenerateMouseMoveEvent(); 444 EXPECT_FALSE(cursor_visible()); 445 446 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running()); 447 test_api_->trigger_real_shutdown_timeout(); 448 EXPECT_EQ(1, NumShutdownRequests()); 449 } 450 451 // Test that we start shutting down immediately if the power button is pressed 452 // while we're not logged in on an unofficial system. 453 TEST_F(LockStateControllerTest, LegacyNotLoggedIn) { 454 Initialize(true, user::LOGGED_IN_NONE); 455 456 PressPowerButton(); 457 ExpectShutdownAnimationStarted(); 458 459 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running()); 460 } 461 462 // Test that we start shutting down immediately if the power button is pressed 463 // while we're logged in as a guest on an unofficial system. 464 TEST_F(LockStateControllerTest, LegacyGuest) { 465 Initialize(true, user::LOGGED_IN_GUEST); 466 467 PressPowerButton(); 468 ExpectShutdownAnimationStarted(); 469 470 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running()); 471 } 472 473 // When we hold the power button while the user isn't logged in, we should shut 474 // down the machine directly. 475 TEST_F(LockStateControllerTest, ShutdownWhenNotLoggedIn) { 476 Initialize(false, user::LOGGED_IN_NONE); 477 478 // Press the power button and check that we start the shutdown timer. 479 PressPowerButton(); 480 EXPECT_FALSE(test_api_->is_animating_lock()); 481 EXPECT_TRUE(test_api_->shutdown_timer_is_running()); 482 ExpectShutdownAnimationStarted(); 483 484 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN, 0.5f); 485 486 // Release the power button before the shutdown timer fires. 487 ReleasePowerButton(); 488 489 EXPECT_FALSE(test_api_->shutdown_timer_is_running()); 490 ExpectShutdownAnimationCancel(); 491 492 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_REVERT, 0.5f); 493 494 // Press the button again and make the shutdown timeout fire this time. 495 // Check that we start the timer for actually requesting the shutdown. 496 PressPowerButton(); 497 498 EXPECT_TRUE(test_api_->shutdown_timer_is_running()); 499 500 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN); 501 ExpectShutdownAnimationFinished(); 502 test_api_->trigger_shutdown_timeout(); 503 504 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running()); 505 EXPECT_EQ(0, NumShutdownRequests()); 506 507 // When the timout fires, we should request a shutdown. 508 test_api_->trigger_real_shutdown_timeout(); 509 510 EXPECT_EQ(1, NumShutdownRequests()); 511 } 512 513 // Test that we lock the screen and deal with unlocking correctly. 514 // TODO(antrim): Reenable this: http://crbug.com/167048 515 TEST_F(LockStateControllerTest, DISABLED_LockAndUnlock) { 516 Initialize(false, user::LOGGED_IN_USER); 517 518 ExpectUnlockedState(); 519 520 // Press the power button and check that the lock timer is started and that we 521 // start lifting the non-screen-locker containers. 522 PressPowerButton(); 523 524 ExpectPreLockAnimationStarted(); 525 EXPECT_TRUE(test_api_->is_lock_cancellable()); 526 EXPECT_EQ(0, delegate_->num_lock_requests()); 527 528 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE); 529 ExpectPreLockAnimationFinished(); 530 531 EXPECT_EQ(1, delegate_->num_lock_requests()); 532 533 // Notify that we locked successfully. 534 lock_state_controller_->OnStartingLock(); 535 // We had that animation already. 536 //TODO (antrim) : restore EXPECT_FALSE(animator_helper_->IsAnimating()); 537 538 SystemLocks(); 539 540 ExpectPostLockAnimationStarted(); 541 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS); 542 ExpectPastLockAnimationFinished(); 543 544 // When we release the power button, the lock-to-shutdown timer should be 545 // stopped. 546 ExpectLockedState(); 547 EXPECT_TRUE(test_api_->lock_to_shutdown_timer_is_running()); 548 ReleasePowerButton(); 549 ExpectLockedState(); 550 EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running()); 551 552 // Notify that the screen has been unlocked. We should show the 553 // non-screen-locker windows. 554 bool called = false; 555 SuccessfulAuthentication(&called); 556 557 ExpectUnlockBeforeUIDestroyedAnimationStarted(); 558 EXPECT_FALSE(called); 559 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS); 560 ExpectUnlockBeforeUIDestroyedAnimationFinished(); 561 562 EXPECT_TRUE(called); 563 564 SystemUnlocks(); 565 566 ExpectUnlockAfterUIDestroyedAnimationStarted(); 567 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS); 568 ExpectUnlockAfterUIDestroyedAnimationFinished(); 569 570 ExpectUnlockedState(); 571 } 572 573 // Test that we deal with cancelling lock correctly. 574 // TODO(antrim): Reenable this: http://crbug.com/167048 575 TEST_F(LockStateControllerTest, DISABLED_LockAndCancel) { 576 Initialize(false, user::LOGGED_IN_USER); 577 578 ExpectUnlockedState(); 579 580 // Press the power button and check that the lock timer is started and that we 581 // start lifting the non-screen-locker containers. 582 PressPowerButton(); 583 584 ExpectPreLockAnimationStarted(); 585 EXPECT_TRUE(test_api_->is_lock_cancellable()); 586 587 // forward only half way through 588 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f); 589 590 gfx::Transform transform_before_button_released = 591 GetContainer(kShellWindowId_DefaultContainer)->layer()->transform(); 592 593 // Release the button before the lock timer fires. 594 ReleasePowerButton(); 595 596 ExpectPreLockAnimationCancel(); 597 598 gfx::Transform transform_after_button_released = 599 GetContainer(kShellWindowId_DefaultContainer)->layer()->transform(); 600 // Expect no flickering, animation should proceed from mid-state. 601 EXPECT_EQ(transform_before_button_released, transform_after_button_released); 602 603 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS); 604 ExpectUnlockedState(); 605 EXPECT_EQ(0, delegate_->num_lock_requests()); 606 } 607 608 // Test that we deal with cancelling lock correctly. 609 // TODO(antrim): Reenable this: http://crbug.com/167048 610 TEST_F(LockStateControllerTest, 611 DISABLED_LockAndCancelAndLockAgain) { 612 Initialize(false, user::LOGGED_IN_USER); 613 614 ExpectUnlockedState(); 615 616 // Press the power button and check that the lock timer is started and that we 617 // start lifting the non-screen-locker containers. 618 PressPowerButton(); 619 620 ExpectPreLockAnimationStarted(); 621 EXPECT_TRUE(test_api_->is_lock_cancellable()); 622 623 // forward only half way through 624 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f); 625 626 // Release the button before the lock timer fires. 627 ReleasePowerButton(); 628 ExpectPreLockAnimationCancel(); 629 630 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS, 0.5f); 631 632 PressPowerButton(); 633 ExpectPreLockAnimationStarted(); 634 EXPECT_TRUE(test_api_->is_lock_cancellable()); 635 636 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.6f); 637 638 EXPECT_EQ(0, delegate_->num_lock_requests()); 639 ExpectPreLockAnimationStarted(); 640 641 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.6f); 642 ExpectPreLockAnimationFinished(); 643 EXPECT_EQ(1, delegate_->num_lock_requests()); 644 } 645 646 // Hold the power button down from the unlocked state to eventual shutdown. 647 // TODO(antrim): Reenable this: http://crbug.com/167048 648 TEST_F(LockStateControllerTest, DISABLED_LockToShutdown) { 649 Initialize(false, user::LOGGED_IN_USER); 650 651 // Hold the power button and lock the screen. 652 PressPowerButton(); 653 EXPECT_TRUE(test_api_->is_animating_lock()); 654 655 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE); 656 SystemLocks(); 657 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS); 658 659 // When the lock-to-shutdown timeout fires, we should start the shutdown 660 // timer. 661 EXPECT_TRUE(test_api_->lock_to_shutdown_timer_is_running()); 662 663 test_api_->trigger_lock_to_shutdown_timeout(); 664 665 ExpectShutdownAnimationStarted(); 666 EXPECT_TRUE(test_api_->shutdown_timer_is_running()); 667 668 // Fire the shutdown timeout and check that we request shutdown. 669 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN); 670 ExpectShutdownAnimationFinished(); 671 test_api_->trigger_shutdown_timeout(); 672 673 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running()); 674 EXPECT_EQ(0, NumShutdownRequests()); 675 test_api_->trigger_real_shutdown_timeout(); 676 EXPECT_EQ(1, NumShutdownRequests()); 677 } 678 679 // Hold the power button down from the unlocked state to eventual shutdown, 680 // then release the button while system does locking. 681 TEST_F(LockStateControllerTest, CancelLockToShutdown) { 682 Initialize(false, user::LOGGED_IN_USER); 683 684 PressPowerButton(); 685 686 // Hold the power button and lock the screen. 687 EXPECT_TRUE(test_api_->is_animating_lock()); 688 689 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE); 690 SystemLocks(); 691 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS, 0.5f); 692 693 // Power button is released while system attempts to lock. 694 ReleasePowerButton(); 695 696 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS); 697 698 EXPECT_FALSE(lock_state_controller_->ShutdownRequested()); 699 EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running()); 700 EXPECT_FALSE(test_api_->shutdown_timer_is_running()); 701 } 702 703 // Test that we handle the case where lock requests are ignored. 704 // TODO(antrim): Reenable this: http://crbug.com/167048 705 TEST_F(LockStateControllerTest, DISABLED_Lock) { 706 // We require animations to have a duration for this test. 707 ui::ScopedAnimationDurationScaleMode normal_duration_mode( 708 ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION); 709 710 Initialize(false, user::LOGGED_IN_USER); 711 712 // Hold the power button and lock the screen. 713 PressPowerButton(); 714 ExpectPreLockAnimationStarted(); 715 716 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE); 717 718 EXPECT_EQ(1, delegate_->num_lock_requests()); 719 EXPECT_TRUE(test_api_->lock_fail_timer_is_running()); 720 // We shouldn't start the lock-to-shutdown timer until the screen has actually 721 // been locked and this was animated. 722 EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running()); 723 724 // Act as if the request timed out. We should restore the windows. 725 test_api_->trigger_lock_fail_timeout(); 726 727 ExpectUnlockAfterUIDestroyedAnimationStarted(); 728 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS); 729 ExpectUnlockAfterUIDestroyedAnimationFinished(); 730 ExpectUnlockedState(); 731 } 732 733 // Test the basic operation of the lock button (not logged in). 734 TEST_F(LockStateControllerTest, LockButtonBasicNotLoggedIn) { 735 // The lock button shouldn't do anything if we aren't logged in. 736 Initialize(false, user::LOGGED_IN_NONE); 737 738 PressLockButton(); 739 EXPECT_FALSE(test_api_->is_animating_lock()); 740 ReleaseLockButton(); 741 EXPECT_EQ(0, delegate_->num_lock_requests()); 742 } 743 744 // Test the basic operation of the lock button (guest). 745 TEST_F(LockStateControllerTest, LockButtonBasicGuest) { 746 // The lock button shouldn't do anything when we're logged in as a guest. 747 Initialize(false, user::LOGGED_IN_GUEST); 748 749 PressLockButton(); 750 EXPECT_FALSE(test_api_->is_animating_lock()); 751 ReleaseLockButton(); 752 EXPECT_EQ(0, delegate_->num_lock_requests()); 753 } 754 755 // Test the basic operation of the lock button. 756 // TODO(antrim): Reenable this: http://crbug.com/167048 757 TEST_F(LockStateControllerTest, DISABLED_LockButtonBasic) { 758 // If we're logged in as a regular user, we should start the lock timer and 759 // the pre-lock animation. 760 Initialize(false, user::LOGGED_IN_USER); 761 762 PressLockButton(); 763 ExpectPreLockAnimationStarted(); 764 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f); 765 766 // If the button is released immediately, we shouldn't lock the screen. 767 ReleaseLockButton(); 768 ExpectPreLockAnimationCancel(); 769 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS); 770 771 ExpectUnlockedState(); 772 EXPECT_EQ(0, delegate_->num_lock_requests()); 773 774 // Press the button again and let the lock timeout fire. We should request 775 // that the screen be locked. 776 PressLockButton(); 777 ExpectPreLockAnimationStarted(); 778 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE); 779 EXPECT_EQ(1, delegate_->num_lock_requests()); 780 781 // Pressing the lock button while we have a pending lock request shouldn't do 782 // anything. 783 ReleaseLockButton(); 784 PressLockButton(); 785 ExpectPreLockAnimationFinished(); 786 ReleaseLockButton(); 787 788 // Pressing the button also shouldn't do anything after the screen is locked. 789 SystemLocks(); 790 ExpectPostLockAnimationStarted(); 791 792 PressLockButton(); 793 ReleaseLockButton(); 794 ExpectPostLockAnimationStarted(); 795 796 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS); 797 ExpectPastLockAnimationFinished(); 798 799 PressLockButton(); 800 ReleaseLockButton(); 801 ExpectPastLockAnimationFinished(); 802 } 803 804 // Test that the power button takes priority over the lock button. 805 // TODO(antrim): Reenable this: http://crbug.com/167048 806 TEST_F(LockStateControllerTest, 807 DISABLED_PowerButtonPreemptsLockButton) { 808 Initialize(false, user::LOGGED_IN_USER); 809 810 // While the lock button is down, hold the power button. 811 PressLockButton(); 812 ExpectPreLockAnimationStarted(); 813 PressPowerButton(); 814 ExpectPreLockAnimationStarted(); 815 816 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f); 817 818 // The lock timer shouldn't be stopped when the lock button is released. 819 ReleaseLockButton(); 820 ExpectPreLockAnimationStarted(); 821 ReleasePowerButton(); 822 ExpectPreLockAnimationCancel(); 823 824 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS); 825 ExpectUnlockedState(); 826 827 // Now press the power button first and then the lock button. 828 PressPowerButton(); 829 ExpectPreLockAnimationStarted(); 830 PressLockButton(); 831 ExpectPreLockAnimationStarted(); 832 833 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f); 834 835 // Releasing the power button should stop the lock timer. 836 ReleasePowerButton(); 837 ExpectPreLockAnimationCancel(); 838 ReleaseLockButton(); 839 ExpectPreLockAnimationCancel(); 840 } 841 842 // When the screen is locked without going through the usual power-button 843 // slow-close path (e.g. via the wrench menu), test that we still show the 844 // fast-close animation. 845 TEST_F(LockStateControllerTest, LockWithoutButton) { 846 Initialize(false, user::LOGGED_IN_USER); 847 lock_state_controller_->OnStartingLock(); 848 849 ExpectPreLockAnimationStarted(); 850 EXPECT_FALSE(test_api_->is_lock_cancellable()); 851 852 // TODO(antrim): After time-faking is fixed, let the pre-lock animation 853 // complete here and check that delegate_->num_lock_requests() is 0 to 854 // prevent http://crbug.com/172487 from regressing. 855 } 856 857 // When we hear that the process is exiting but we haven't had a chance to 858 // display an animation, we should just blank the screen. 859 TEST_F(LockStateControllerTest, ShutdownWithoutButton) { 860 Initialize(false, user::LOGGED_IN_USER); 861 lock_state_controller_->OnAppTerminating(); 862 863 EXPECT_TRUE( 864 animator_api_->ContainersAreAnimated( 865 SessionStateAnimator::kAllContainersMask, 866 SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY)); 867 GenerateMouseMoveEvent(); 868 EXPECT_FALSE(cursor_visible()); 869 } 870 871 // Test that we display the fast-close animation and shut down when we get an 872 // outside request to shut down (e.g. from the login or lock screen). 873 TEST_F(LockStateControllerTest, RequestShutdownFromLoginScreen) { 874 Initialize(false, user::LOGGED_IN_NONE); 875 CreateWindowForLockscreen(); 876 877 lock_state_controller_->RequestShutdown(); 878 879 ExpectShutdownAnimationStarted(); 880 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN); 881 882 GenerateMouseMoveEvent(); 883 EXPECT_FALSE(cursor_visible()); 884 885 EXPECT_EQ(0, NumShutdownRequests()); 886 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running()); 887 test_api_->trigger_real_shutdown_timeout(); 888 EXPECT_EQ(1, NumShutdownRequests()); 889 } 890 891 TEST_F(LockStateControllerTest, RequestShutdownFromLockScreen) { 892 Initialize(false, user::LOGGED_IN_USER); 893 894 SystemLocks(); 895 CreateWindowForLockscreen(); 896 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN); 897 ExpectPastLockAnimationFinished(); 898 899 lock_state_controller_->RequestShutdown(); 900 901 ExpectShutdownAnimationStarted(); 902 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN); 903 904 GenerateMouseMoveEvent(); 905 EXPECT_FALSE(cursor_visible()); 906 907 EXPECT_EQ(0, NumShutdownRequests()); 908 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running()); 909 test_api_->trigger_real_shutdown_timeout(); 910 EXPECT_EQ(1, NumShutdownRequests()); 911 } 912 913 // TODO(antrim): Reenable this: http://crbug.com/167048 914 TEST_F(LockStateControllerTest, 915 DISABLED_RequestAndCancelShutdownFromLockScreen) { 916 Initialize(false, user::LOGGED_IN_USER); 917 918 SystemLocks(); 919 Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN); 920 ExpectLockedState(); 921 922 // Press the power button and check that we start the shutdown timer. 923 PressPowerButton(); 924 EXPECT_FALSE(test_api_->is_animating_lock()); 925 EXPECT_TRUE(test_api_->shutdown_timer_is_running()); 926 927 ExpectShutdownAnimationStarted(); 928 929 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN, 0.5f); 930 931 float grayscale_before_button_release = 932 Shell::GetPrimaryRootWindow()->layer()->layer_grayscale(); 933 934 // Release the power button before the shutdown timer fires. 935 ReleasePowerButton(); 936 937 EXPECT_FALSE(test_api_->shutdown_timer_is_running()); 938 939 ExpectShutdownAnimationCancel(); 940 941 float grayscale_after_button_release = 942 Shell::GetPrimaryRootWindow()->layer()->layer_grayscale(); 943 // Expect no flickering in undo animation. 944 EXPECT_EQ(grayscale_before_button_release, grayscale_after_button_release); 945 946 Advance(SessionStateAnimator::ANIMATION_SPEED_REVERT); 947 ExpectLockedState(); 948 } 949 950 // Test that we ignore power button presses when the screen is turned off. 951 TEST_F(LockStateControllerTest, IgnorePowerButtonIfScreenIsOff) { 952 Initialize(false, user::LOGGED_IN_USER); 953 954 // When the screen brightness is at 0%, we shouldn't do anything in response 955 // to power button presses. 956 controller_->OnScreenBrightnessChanged(0.0); 957 PressPowerButton(); 958 EXPECT_FALSE(test_api_->is_animating_lock()); 959 ReleasePowerButton(); 960 961 // After increasing the brightness to 10%, we should start the timer like 962 // usual. 963 controller_->OnScreenBrightnessChanged(10.0); 964 PressPowerButton(); 965 EXPECT_TRUE(test_api_->is_animating_lock()); 966 ReleasePowerButton(); 967 } 968 969 #if defined(OS_CHROMEOS) && defined(USE_X11) 970 TEST_F(LockStateControllerTest, HonorPowerButtonInDockedMode) { 971 ScopedVector<const ui::DisplayMode> modes; 972 modes.push_back(new ui::DisplayMode(gfx::Size(1, 1), false, 60.0f)); 973 974 // Create two outputs, the first internal and the second external. 975 ui::DisplayConfigurator::DisplayStateList outputs; 976 ui::DisplayConfigurator::DisplayState internal_output; 977 ui::TestDisplaySnapshot internal_display; 978 internal_display.set_type(ui::DISPLAY_CONNECTION_TYPE_INTERNAL); 979 internal_display.set_modes(modes.get()); 980 internal_output.display = &internal_display; 981 outputs.push_back(internal_output); 982 983 ui::DisplayConfigurator::DisplayState external_output; 984 ui::TestDisplaySnapshot external_display; 985 external_display.set_type(ui::DISPLAY_CONNECTION_TYPE_HDMI); 986 external_display.set_modes(modes.get()); 987 external_output.display = &external_display; 988 outputs.push_back(external_output); 989 990 // When all of the displays are turned off (e.g. due to user inactivity), the 991 // power button should be ignored. 992 controller_->OnScreenBrightnessChanged(0.0); 993 static_cast<ui::TestDisplaySnapshot*>(outputs[0].display) 994 ->set_current_mode(NULL); 995 static_cast<ui::TestDisplaySnapshot*>(outputs[1].display) 996 ->set_current_mode(NULL); 997 controller_->OnDisplayModeChanged(outputs); 998 PressPowerButton(); 999 EXPECT_FALSE(test_api_->is_animating_lock()); 1000 ReleasePowerButton(); 1001 1002 // When the screen brightness is 0% but the external display is still turned 1003 // on (indicating either docked mode or the user having manually decreased the 1004 // brightness to 0%), the power button should still be handled. 1005 static_cast<ui::TestDisplaySnapshot*>(outputs[1].display) 1006 ->set_current_mode(modes[0]); 1007 controller_->OnDisplayModeChanged(outputs); 1008 PressPowerButton(); 1009 EXPECT_TRUE(test_api_->is_animating_lock()); 1010 ReleasePowerButton(); 1011 } 1012 #endif 1013 1014 // Test that hidden background appears and revers correctly on lock/cancel. 1015 // TODO(antrim): Reenable this: http://crbug.com/167048 1016 TEST_F(LockStateControllerTest, DISABLED_TestHiddenBackgroundLockCancel) { 1017 Initialize(false, user::LOGGED_IN_USER); 1018 HideBackground(); 1019 1020 EXPECT_TRUE(IsBackgroundHidden()); 1021 ExpectUnlockedState(); 1022 PressPowerButton(); 1023 1024 ExpectPreLockAnimationStarted(); 1025 EXPECT_FALSE(IsBackgroundHidden()); 1026 ExpectBackgroundIsShowing(); 1027 1028 // Forward only half way through. 1029 AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f); 1030 1031 // Release the button before the lock timer fires. 1032 ReleasePowerButton(); 1033 ExpectPreLockAnimationCancel(); 1034 ExpectBackgroundIsHiding(); 1035 EXPECT_FALSE(IsBackgroundHidden()); 1036 1037 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS); 1038 1039 ExpectUnlockedState(); 1040 EXPECT_TRUE(IsBackgroundHidden()); 1041 } 1042 1043 // Test that hidden background appears and revers correctly on lock/unlock. 1044 // TODO(antrim): Reenable this: http://crbug.com/167048 1045 TEST_F(LockStateControllerTest, DISABLED_TestHiddenBackgroundLockUnlock) { 1046 Initialize(false, user::LOGGED_IN_USER); 1047 HideBackground(); 1048 1049 EXPECT_TRUE(IsBackgroundHidden()); 1050 ExpectUnlockedState(); 1051 1052 // Press the power button and check that the lock timer is started and that we 1053 // start lifting the non-screen-locker containers. 1054 PressPowerButton(); 1055 1056 ExpectPreLockAnimationStarted(); 1057 EXPECT_FALSE(IsBackgroundHidden()); 1058 ExpectBackgroundIsShowing(); 1059 1060 Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE); 1061 1062 ExpectPreLockAnimationFinished(); 1063 1064 SystemLocks(); 1065 1066 ReleasePowerButton(); 1067 1068 ExpectPostLockAnimationStarted(); 1069 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS); 1070 ExpectPastLockAnimationFinished(); 1071 1072 ExpectLockedState(); 1073 1074 SuccessfulAuthentication(NULL); 1075 1076 ExpectUnlockBeforeUIDestroyedAnimationStarted(); 1077 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS); 1078 ExpectUnlockBeforeUIDestroyedAnimationFinished(); 1079 1080 SystemUnlocks(); 1081 1082 ExpectUnlockAfterUIDestroyedAnimationStarted(); 1083 ExpectBackgroundIsHiding(); 1084 EXPECT_FALSE(IsBackgroundHidden()); 1085 1086 Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS); 1087 ExpectUnlockAfterUIDestroyedAnimationFinished(); 1088 EXPECT_TRUE(IsBackgroundHidden()); 1089 1090 ExpectUnlockedState(); 1091 } 1092 1093 } // namespace test 1094 } // namespace ash 1095