diff options
author | derat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-04 00:46:57 +0000 |
---|---|---|
committer | derat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-04 00:46:57 +0000 |
commit | e0d22e884725a6fb153e405083d61d79639349ec (patch) | |
tree | e0bcde44b7dcd888566c6f8418bb20f9167909ca /ash | |
parent | cdcb1dee07b6c8e7fe968b2a5f4199c9c8fb2323 (diff) | |
download | chromium_src-e0d22e884725a6fb153e405083d61d79639349ec.zip chromium_src-e0d22e884725a6fb153e405083d61d79639349ec.tar.gz chromium_src-e0d22e884725a6fb153e405083d61d79639349ec.tar.bz2 |
chromeos: Implement power button animations for Aura.
This adds animated transitions first to the lock screen and
then to shutdown similar to those currently implemented in
the Chrome OS power manager and X window manager. I'll add
support for the lock key in a later change.
BUG=98328
TEST=added tests; also did a lot of manual testing
TBR=evan@chromium.org
Review URL: http://codereview.chromium.org/8976012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@116245 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ash')
-rw-r--r-- | ash/ash.gyp | 3 | ||||
-rw-r--r-- | ash/shell.cc | 11 | ||||
-rw-r--r-- | ash/shell.h | 8 | ||||
-rw-r--r-- | ash/wm/power_button_controller.cc | 495 | ||||
-rw-r--r-- | ash/wm/power_button_controller.h | 224 | ||||
-rw-r--r-- | ash/wm/power_button_controller_unittest.cc | 276 | ||||
-rw-r--r-- | ash/wm/shadow_controller.h | 8 |
7 files changed, 1013 insertions, 12 deletions
diff --git a/ash/ash.gyp b/ash/ash.gyp index 6fab5dd..6943a98 100644 --- a/ash/ash.gyp +++ b/ash/ash.gyp @@ -114,6 +114,8 @@ 'wm/modality_event_filter.cc', 'wm/modality_event_filter.h', 'wm/modality_event_filter_delegate.h', + 'wm/power_button_controller.cc', + 'wm/power_button_controller.h', 'wm/property_util.cc', 'wm/property_util.h', 'wm/root_window_event_filter.cc', @@ -202,6 +204,7 @@ 'wm/default_container_layout_manager_unittest.cc', 'wm/image_grid_unittest.cc', 'wm/modal_container_layout_manager_unittest.cc', + 'wm/power_button_controller_unittest.cc', 'wm/root_window_event_filter_unittest.cc', 'wm/shadow_controller_unittest.cc', 'wm/shelf_layout_manager_unittest.cc', diff --git a/ash/shell.cc b/ash/shell.cc index 935b1e3c..77e1c0c 100644 --- a/ash/shell.cc +++ b/ash/shell.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -23,6 +23,7 @@ #include "ash/wm/default_container_event_filter.h" #include "ash/wm/default_container_layout_manager.h" #include "ash/wm/modal_container_layout_manager.h" +#include "ash/wm/power_button_controller.h" #include "ash/wm/root_window_event_filter.h" #include "ash/wm/root_window_layout_manager.h" #include "ash/wm/shadow_controller.h" @@ -224,23 +225,21 @@ void Shell::Init() { // Force a layout. root_window->layout_manager()->OnWindowResized(); - // Initialize InputMethodEventFilter. The filter must be added first since it - // has the highest priority. + // InputMethodEventFilter must be added first since it has the highest + // priority. DCHECK(!GetRootWindowEventFilterCount()); input_method_filter_.reset(new internal::InputMethodEventFilter); AddRootWindowEventFilter(input_method_filter_.get()); - // Initialize AcceleratorFilter. accelerator_filter_.reset(new internal::AcceleratorFilter); AddRootWindowEventFilter(accelerator_filter_.get()); - // Initialize TooltipController. tooltip_controller_.reset(new internal::TooltipController); AddRootWindowEventFilter(tooltip_controller_.get()); aura::client::SetTooltipClient(tooltip_controller_.get()); - // Initialize drag drop controller. drag_drop_controller_.reset(new internal::DragDropController); + power_button_controller_.reset(new PowerButtonController); } bool Shell::DefaultToCompactWindowMode(const gfx::Size& monitor_size, diff --git a/ash/shell.h b/ash/shell.h index b146754..8fb6ffb 100644 --- a/ash/shell.h +++ b/ash/shell.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -33,6 +33,7 @@ namespace ash { class AcceleratorController; class Launcher; +class PowerButtonController; class ShellDelegate; namespace internal { @@ -83,10 +84,12 @@ class ASH_EXPORT Shell { AcceleratorController* accelerator_controller() { return accelerator_controller_.get(); } - internal::TooltipController* tooltip_controller() { return tooltip_controller_.get(); } + PowerButtonController* power_button_controller() { + return power_button_controller_.get(); + } ShellDelegate* delegate() { return delegate_.get(); } @@ -138,6 +141,7 @@ class ASH_EXPORT Shell { scoped_ptr<internal::WorkspaceController> workspace_controller_; scoped_ptr<internal::ShadowController> shadow_controller_; scoped_ptr<internal::TooltipController> tooltip_controller_; + scoped_ptr<PowerButtonController> power_button_controller_; // An event filter that pre-handles all key events to send them to an IME. scoped_ptr<internal::InputMethodEventFilter> input_method_filter_; diff --git a/ash/wm/power_button_controller.cc b/ash/wm/power_button_controller.cc new file mode 100644 index 0000000..330ef6d --- /dev/null +++ b/ash/wm/power_button_controller.cc @@ -0,0 +1,495 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ash/wm/power_button_controller.h" + +#include "ash/shell.h" +#include "ash/shell_window_ids.h" +#include "base/logging.h" +#include "base/time.h" +#include "third_party/skia/include/core/SkColor.h" +#include "ui/aura/root_window.h" +#include "ui/aura/window.h" +#include "ui/gfx/canvas.h" +#include "ui/gfx/compositor/layer.h" +#include "ui/gfx/compositor/layer_animation_element.h" +#include "ui/gfx/compositor/layer_animation_sequence.h" +#include "ui/gfx/compositor/layer_animator.h" +#include "ui/gfx/rect.h" +#include "ui/gfx/size.h" +#include "ui/gfx/transform.h" + +namespace ash { + +namespace { + +// Amount of time that the power button needs to be held before we lock the +// screen. +const int kLockTimeoutMs = 400; + +// Amount of time that the power button needs to be held before we shut down. +const int kShutdownTimeoutMs = 400; + +// Amount of time to wait for our lock requests to be honored before giving up. +const int kLockFailTimeoutMs = 4000; + +// When the button has been held continuously from the unlocked state, amount of +// time that we wait after the screen locker window is shown before starting the +// pre-shutdown animation. +const int kLockToShutdownTimeoutMs = 150; + +// Amount of time taken to scale the snapshot of the screen down to a +// slightly-smaller size once the user starts holding the power button. Used +// for both the pre-lock and pre-shutdown animations. +const int kSlowCloseAnimMs = 400; + +// Amount of time taken to scale the snapshot of the screen back to its original +// size when the button is released. +const int kUndoSlowCloseAnimMs = 100; + +// Amount of time taken to scale the snapshot down to a point in the center of +// the screen once the screen has been locked or we've been notified that the +// system is shutting down. +const int kFastCloseAnimMs = 150; + +// Amount of time taken to make the lock window fade in when the screen is +// locked. +const int kLockFadeInAnimMs = 500; + +// Slightly-smaller size that we scale the screen down to for the pre-lock and +// pre-shutdown states. +const float kSlowCloseSizeRatio = 0.95f; + +// Containers holding screen locker windows. +const int kScreenLockerContainerIds[] = { + internal::kShellWindowId_LockScreenContainer, + internal::kShellWindowId_LockModalContainer, +}; + +// Containers holding additional windows that should be shown while the screen +// is locked. +const int kRelatedContainerIds[] = { + internal::kShellWindowId_StatusContainer, + internal::kShellWindowId_MenusAndTooltipsContainer, +}; + +// Is |window| a container that holds screen locker windows? +bool IsScreenLockerContainer(aura::Window* window) { + for (size_t i = 0; i < arraysize(kScreenLockerContainerIds); ++i) + if (window->id() == kScreenLockerContainerIds[i]) + return true; + return false; +} + +// Is |window| a container that holds other windows that should be shown while +// the screen is locked? +bool IsRelatedContainer(aura::Window* window) { + for (size_t i = 0; i < arraysize(kRelatedContainerIds); ++i) + if (window->id() == kRelatedContainerIds[i]) + return true; + return false; +} + +// Returns the transform that should be applied to containers for the slow-close +// animation. +ui::Transform GetSlowCloseTransform() { + gfx::Size root_size = aura::RootWindow::GetInstance()->bounds().size(); + ui::Transform transform; + transform.SetScale(kSlowCloseSizeRatio, kSlowCloseSizeRatio); + transform.ConcatTranslate( + floor(0.5 * (1.0 - kSlowCloseSizeRatio) * root_size.width() + 0.5), + floor(0.5 * (1.0 - kSlowCloseSizeRatio) * root_size.height() + 0.5)); + return transform; +} + +// Returns the transform that should be applied to containers for the fast-close +// animation. +ui::Transform GetFastCloseTransform() { + gfx::Size root_size = aura::RootWindow::GetInstance()->bounds().size(); + ui::Transform transform; + transform.SetScale(0.0, 0.0); + transform.ConcatTranslate(floor(0.5 * root_size.width() + 0.5), + floor(0.5 * root_size.height() + 0.5)); + return transform; +} + +// Slowly shrinks |window| to a slightly-smaller size. +void StartSlowCloseAnimationForWindow(aura::Window* window) { + ui::LayerAnimator* animator = window->layer()->GetAnimator(); + animator->set_preemption_strategy( + ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); + animator->StartAnimation( + new ui::LayerAnimationSequence( + ui::LayerAnimationElement::CreateTransformElement( + GetSlowCloseTransform(), + base::TimeDelta::FromMilliseconds(kSlowCloseAnimMs)))); +} + +// Quickly undoes the effects of the slow-close animation on |window|. +void StartUndoSlowCloseAnimationForWindow(aura::Window* window) { + ui::LayerAnimator* animator = window->layer()->GetAnimator(); + animator->set_preemption_strategy( + ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); + animator->StartAnimation( + new ui::LayerAnimationSequence( + ui::LayerAnimationElement::CreateTransformElement( + ui::Transform(), + base::TimeDelta::FromMilliseconds(kUndoSlowCloseAnimMs)))); +} + +// Quickly shrinks |window| down to a point in the center of the screen and +// fades it out to 0 opacity. +void StartFastCloseAnimationForWindow(aura::Window* window) { + ui::LayerAnimator* animator = window->layer()->GetAnimator(); + animator->set_preemption_strategy( + ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); + animator->StartAnimation( + new ui::LayerAnimationSequence( + ui::LayerAnimationElement::CreateTransformElement( + GetFastCloseTransform(), + base::TimeDelta::FromMilliseconds(kFastCloseAnimMs)))); + animator->StartAnimation( + new ui::LayerAnimationSequence( + ui::LayerAnimationElement::CreateOpacityElement( + 0.0, base::TimeDelta::FromMilliseconds(kFastCloseAnimMs)))); +} + +// Fades |window| in to full opacity. +void FadeInWindow(aura::Window* window) { + ui::LayerAnimator* animator = window->layer()->GetAnimator(); + animator->set_preemption_strategy( + ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); + animator->StartAnimation( + new ui::LayerAnimationSequence( + ui::LayerAnimationElement::CreateOpacityElement( + 1.0, base::TimeDelta::FromMilliseconds(kLockFadeInAnimMs)))); +} + +// Makes |window| fully transparent instantaneously. +void HideWindow(aura::Window* window) { + window->layer()->SetOpacity(0.0); +} + +// Restores |window| to its original position and scale and full opacity +// instantaneously. +void RestoreWindow(aura::Window* window) { + window->layer()->SetTransform(ui::Transform()); + window->layer()->SetOpacity(1.0); +} + +// Fills |containers| with the containers described by |group|. +void GetContainers(PowerButtonController::ContainerGroup group, + aura::Window::Windows* containers) { + containers->clear(); + + aura::Window* root = aura::RootWindow::GetInstance(); + for (aura::Window::Windows::const_iterator it = root->children().begin(); + it != root->children().end(); ++it) { + aura::Window* window = *it; + + bool matched = true; + if (group != PowerButtonController::ALL_CONTAINERS) { + bool is_screen_locker = IsScreenLockerContainer(window); + bool is_related = IsRelatedContainer(window); + + switch (group) { + case PowerButtonController::SCREEN_LOCKER_CONTAINERS: + matched = is_screen_locker; + break; + case PowerButtonController::SCREEN_LOCKER_AND_RELATED_CONTAINERS: + matched = is_screen_locker || is_related; + break; + case PowerButtonController:: + ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS: + matched = !is_screen_locker && !is_related; + break; + default: + NOTREACHED() << "Unhandled container group " << group; + } + } + + if (matched) + containers->push_back(window); + } +} + +// Apply animation |type| to all containers described by |group|. +void StartAnimation(PowerButtonController::ContainerGroup group, + PowerButtonController::AnimationType type) { + aura::Window::Windows containers; + GetContainers(group, &containers); + + for (aura::Window::Windows::const_iterator it = containers.begin(); + it != containers.end(); ++it) { + aura::Window* window = *it; + switch (type) { + case PowerButtonController::ANIMATION_SLOW_CLOSE: + StartSlowCloseAnimationForWindow(window); + break; + case PowerButtonController::ANIMATION_UNDO_SLOW_CLOSE: + StartUndoSlowCloseAnimationForWindow(window); + break; + case PowerButtonController::ANIMATION_FAST_CLOSE: + StartFastCloseAnimationForWindow(window); + break; + case PowerButtonController::ANIMATION_FADE_IN: + FadeInWindow(window); + break; + case PowerButtonController::ANIMATION_HIDE: + HideWindow(window); + break; + case PowerButtonController::ANIMATION_RESTORE: + RestoreWindow(window); + break; + default: + NOTREACHED() << "Unhandled animation type " << type; + } + } +} + +} // namespace + +bool PowerButtonController::TestApi::ContainerGroupIsAnimated( + ContainerGroup group, AnimationType type) const { + aura::Window::Windows containers; + GetContainers(group, &containers); + for (aura::Window::Windows::const_iterator it = containers.begin(); + it != containers.end(); ++it) { + aura::Window* window = *it; + ui::Layer* layer = window->layer(); + + switch (type) { + case PowerButtonController::ANIMATION_SLOW_CLOSE: + if (layer->GetTargetTransform() != GetSlowCloseTransform()) + return false; + break; + case PowerButtonController::ANIMATION_UNDO_SLOW_CLOSE: + if (layer->GetTargetTransform() != ui::Transform()) + return false; + break; + case PowerButtonController::ANIMATION_FAST_CLOSE: + if (layer->GetTargetTransform() != GetFastCloseTransform() || + layer->GetTargetOpacity() > 0.0001) + return false; + break; + case PowerButtonController::ANIMATION_FADE_IN: + if (layer->GetTargetOpacity() < 0.9999) + return false; + break; + case PowerButtonController::ANIMATION_HIDE: + if (layer->GetTargetOpacity() > 0.0001) + return false; + break; + case PowerButtonController::ANIMATION_RESTORE: + if (layer->opacity() < 0.9999 || layer->transform() != ui::Transform()) + return false; + break; + default: + NOTREACHED() << "Unhandled animation type " << type; + return false; + } + } + return true; +} + +bool PowerButtonController::TestApi::BackgroundLayerIsVisible() const { + return controller_->background_layer_.get() && + controller_->background_layer_->visible(); +} + +// Simple class that fills |background_layer_| with black. +class PowerButtonController::BackgroundLayerDelegate + : public ui::LayerDelegate { + public: + BackgroundLayerDelegate() {} + virtual ~BackgroundLayerDelegate() {} + + // ui::LayerDelegate implementation: + virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE { + canvas->FillRect(SK_ColorBLACK, aura::RootWindow::GetInstance()->bounds()); + } + + private: + DISALLOW_COPY_AND_ASSIGN(BackgroundLayerDelegate); +}; + +PowerButtonController::PowerButtonController() + : logged_in_as_non_guest_(false), + locked_(false), + power_button_down_(false), + lock_button_down_(false), + shutting_down_(false) { +} + +PowerButtonController::~PowerButtonController() { +} + +void PowerButtonController::OnLoginStateChange(bool logged_in, bool is_guest) { + logged_in_as_non_guest_ = logged_in && !is_guest; +} + +void PowerButtonController::OnLockStateChange(bool locked) { + if (shutting_down_ || locked_ == locked) + return; + + locked_ = locked; + if (locked) { + StartAnimation(SCREEN_LOCKER_CONTAINERS, ANIMATION_FADE_IN); + lock_timer_.Stop(); + lock_fail_timer_.Stop(); + + if (power_button_down_) { + lock_to_shutdown_timer_.Stop(); + lock_to_shutdown_timer_.Start( + FROM_HERE, + base::TimeDelta::FromMilliseconds(kLockToShutdownTimeoutMs), + this, &PowerButtonController::OnLockToShutdownTimeout); + } + } else { + StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, + ANIMATION_RESTORE); + HideBackgroundLayer(); + } +} + +void PowerButtonController::OnStartingLock() { + if (shutting_down_ || locked_) + return; + + StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, + ANIMATION_FAST_CLOSE); + + // Hide the screen locker containers so we can make them fade in later. + StartAnimation(SCREEN_LOCKER_CONTAINERS, ANIMATION_HIDE); +} + +void PowerButtonController::OnPowerButtonEvent( + bool down, const base::TimeTicks& timestamp) { + power_button_down_ = down; + + if (shutting_down_) + return; + + if (down) { + // If we already have a pending request to lock the screen, wait. + if (lock_fail_timer_.IsRunning()) + return; + + if (logged_in_as_non_guest_ && !locked_) { + ShowBackgroundLayer(); + StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, + ANIMATION_SLOW_CLOSE); + lock_timer_.Stop(); + lock_timer_.Start(FROM_HERE, + base::TimeDelta::FromMilliseconds(kSlowCloseAnimMs), + this, &PowerButtonController::OnLockTimeout); + } else { + StartShutdownTimer(); + } + } else { // Button is up. + if (lock_timer_.IsRunning() || shutdown_timer_.IsRunning()) + StartAnimation( + locked_ ? SCREEN_LOCKER_AND_RELATED_CONTAINERS : ALL_CONTAINERS, + ANIMATION_UNDO_SLOW_CLOSE); + + // Drop the background layer after the undo animation finishes. + if (lock_timer_.IsRunning() || + (shutdown_timer_.IsRunning() && !logged_in_as_non_guest_)) { + hide_background_layer_timer_.Stop(); + hide_background_layer_timer_.Start( + FROM_HERE, + base::TimeDelta::FromMilliseconds(kUndoSlowCloseAnimMs), + this, &PowerButtonController::HideBackgroundLayer); + } + + lock_timer_.Stop(); + shutdown_timer_.Stop(); + lock_to_shutdown_timer_.Stop(); + } +} + +void PowerButtonController::OnLockButtonEvent( + bool down, const base::TimeTicks& timestamp) { + lock_button_down_ = down; + + if (shutting_down_) + return; + + NOTIMPLEMENTED(); +} + +void PowerButtonController::OnRootWindowResized(const gfx::Size& new_size) { + if (background_layer_.get()) + background_layer_->SetBounds(gfx::Rect(new_size)); +} + +void PowerButtonController::OnLockTimeout() { + delegate_->RequestLockScreen(); + lock_fail_timer_.Start( + FROM_HERE, + base::TimeDelta::FromMilliseconds(kLockFailTimeoutMs), + this, &PowerButtonController::OnLockFailTimeout); +} + +void PowerButtonController::OnLockFailTimeout() { + DCHECK(!locked_); + LOG(ERROR) << "Screen lock request timed out"; + StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, + ANIMATION_RESTORE); + HideBackgroundLayer(); +} + +void PowerButtonController::OnLockToShutdownTimeout() { + DCHECK(locked_); + StartShutdownTimer(); +} + +void PowerButtonController::OnShutdownTimeout() { + DCHECK(!shutting_down_); + shutting_down_ = true; + aura::RootWindow::GetInstance()->ShowCursor(false); + StartAnimation(ALL_CONTAINERS, ANIMATION_FAST_CLOSE); + real_shutdown_timer_.Start( + FROM_HERE, + base::TimeDelta::FromMilliseconds(kFastCloseAnimMs), + this, &PowerButtonController::OnRealShutdownTimeout); +} + +void PowerButtonController::OnRealShutdownTimeout() { + DCHECK(shutting_down_); + delegate_->RequestShutdown(); +} + +void PowerButtonController::StartShutdownTimer() { + ShowBackgroundLayer(); + StartAnimation(ALL_CONTAINERS, ANIMATION_SLOW_CLOSE); + shutdown_timer_.Stop(); + shutdown_timer_.Start( + FROM_HERE, + base::TimeDelta::FromMilliseconds(kShutdownTimeoutMs), + this, &PowerButtonController::OnShutdownTimeout); +} + +void PowerButtonController::ShowBackgroundLayer() { + if (hide_background_layer_timer_.IsRunning()) + hide_background_layer_timer_.Stop(); + + if (!background_layer_.get()) { + background_layer_delegate_.reset(new BackgroundLayerDelegate); + background_layer_.reset(new ui::Layer(ui::Layer::LAYER_HAS_TEXTURE)); + background_layer_->set_delegate(background_layer_delegate_.get()); + + ui::Layer* root_layer = aura::RootWindow::GetInstance()->layer(); + background_layer_->SetBounds(root_layer->bounds()); + root_layer->Add(background_layer_.get()); + root_layer->StackAtBottom(background_layer_.get()); + } + background_layer_->SetVisible(true); +} + +void PowerButtonController::HideBackgroundLayer() { + background_layer_.reset(); +} + +} // namespace ash diff --git a/ash/wm/power_button_controller.h b/ash/wm/power_button_controller.h new file mode 100644 index 0000000..98ba1f8 --- /dev/null +++ b/ash/wm/power_button_controller.h @@ -0,0 +1,224 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef ASH_WM_POWER_BUTTON_CONTROLLER_H_ +#define ASH_WM_POWER_BUTTON_CONTROLLER_H_ +#pragma once + +#include "ash/ash_export.h" +#include "base/basictypes.h" +#include "base/memory/scoped_ptr.h" +#include "base/time.h" +#include "base/timer.h" +#include "ui/aura/root_window_observer.h" + +namespace gfx { +class Size; +} + +namespace ui { +class Layer; +class LayerDelegate; +} + +namespace ash { + +// Performs system-related functions on behalf of PowerButtonController. +class ASH_EXPORT PowerButtonControllerDelegate { + public: + PowerButtonControllerDelegate() {} + virtual ~PowerButtonControllerDelegate() {} + + virtual void RequestLockScreen() = 0; + virtual void RequestShutdown() = 0; + + private: + DISALLOW_COPY_AND_ASSIGN(PowerButtonControllerDelegate); +}; + +// Displays onscreen animations and locks or suspends the system in response to +// the power button being pressed or released. +class ASH_EXPORT PowerButtonController : public aura::RootWindowObserver { + public: + // Animations that can be applied to groups of containers. + // Exposed here for TestApi::ContainerGroupIsAnimated(). + enum AnimationType { + ANIMATION_SLOW_CLOSE = 0, + ANIMATION_UNDO_SLOW_CLOSE, + ANIMATION_FAST_CLOSE, + ANIMATION_FADE_IN, + ANIMATION_HIDE, + ANIMATION_RESTORE, + }; + + // Groups of containers that can be animated. + // Exposed here for TestApi::ContainerGroupIsAnimated(). + enum ContainerGroup { + ALL_CONTAINERS = 0, + SCREEN_LOCKER_CONTAINERS, + SCREEN_LOCKER_AND_RELATED_CONTAINERS, + ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, + }; + + // Helper class used by tests to access internal state. + class TestApi { + public: + TestApi(PowerButtonController* controller) : controller_(controller) {} + + bool lock_timer_is_running() const { + return controller_->lock_timer_.IsRunning(); + } + bool lock_fail_timer_is_running() const { + return controller_->lock_fail_timer_.IsRunning(); + } + bool lock_to_shutdown_timer_is_running() const { + return controller_->lock_to_shutdown_timer_.IsRunning(); + } + bool shutdown_timer_is_running() const { + return controller_->shutdown_timer_.IsRunning(); + } + bool real_shutdown_timer_is_running() const { + return controller_->real_shutdown_timer_.IsRunning(); + } + bool hide_background_layer_timer_is_running() const { + return controller_->hide_background_layer_timer_.IsRunning(); + } + + void trigger_lock_timeout() { controller_->OnLockTimeout(); } + void trigger_lock_fail_timeout() { controller_->OnLockFailTimeout(); } + void trigger_lock_to_shutdown_timeout() { + controller_->OnLockToShutdownTimeout(); + } + void trigger_shutdown_timeout() { controller_->OnShutdownTimeout(); } + void trigger_real_shutdown_timeout() { + controller_->OnRealShutdownTimeout(); + } + void trigger_hide_background_layer_timeout() { + controller_->HideBackgroundLayer(); + } + + // Returns true if the given set of containers was last animated with + // |type| (probably; the analysis is fairly ad-hoc). + bool ContainerGroupIsAnimated(ContainerGroup group, + AnimationType type) const; + + // Returns true if |background_layer_| is non-NULL and visible. + bool BackgroundLayerIsVisible() const; + + private: + PowerButtonController* controller_; // not owned + + DISALLOW_COPY_AND_ASSIGN(TestApi); + }; + + PowerButtonController(); + ~PowerButtonController(); + + void set_delegate(PowerButtonControllerDelegate* delegate) { + delegate_.reset(delegate); + } + + // Called when the user logs in. + void OnLoginStateChange(bool logged_in, bool is_guest); + + // Called when the screen is locked (after the lock window is visible) or + // unlocked. + void OnLockStateChange(bool locked); + + // Called when Chrome gets a request to display the lock screen. + void OnStartingLock(); + + // Called when the power or lock buttons are pressed or released. + void OnPowerButtonEvent(bool down, const base::TimeTicks& timestamp); + void OnLockButtonEvent(bool down, const base::TimeTicks& timestamp); + + // aura::RootWindowObserver overrides: + virtual void OnRootWindowResized(const gfx::Size& new_size) OVERRIDE; + + private: + class BackgroundLayerDelegate; + + // Requests that the screen be locked and starts |lock_fail_timer_|. + void OnLockTimeout(); + + // Aborts the pre-lock animation. + void OnLockFailTimeout(); + + // Displays the pre-shutdown animation and starts |shutdown_timer_|. + void OnLockToShutdownTimeout(); + + // Displays the shutdown animation and starts |real_shutdown_timer_|. + void OnShutdownTimeout(); + + // Requests that the machine be shut down. + void OnRealShutdownTimeout(); + + // Puts us into the pre-shutdown state. + void StartShutdownTimer(); + + // Shows or hides |background_layer_|. The show method creates and + // initializes the layer if it doesn't already exist. + void ShowBackgroundLayer(); + void HideBackgroundLayer(); + + scoped_ptr<PowerButtonControllerDelegate> delegate_; + + // True if a non-guest user is currently logged in. + bool logged_in_as_non_guest_; + + // True if the screen is currently locked. + bool locked_; + + // Are the power or lock buttons currently held? + bool power_button_down_; + bool lock_button_down_; + + // Are we in the process of shutting the machine down? + bool shutting_down_; + + // Should we start |shutdown_timer_| when we receive notification that the + // screen has been locked? + bool should_start_shutdown_timer_after_lock_; + + // Responsible for painting |background_layer_|. + scoped_ptr<BackgroundLayerDelegate> background_layer_delegate_; + + // Layer that's stacked under all of the root window's children to provide a + // black background when we're scaling all of the other windows down. + scoped_ptr<ui::Layer> background_layer_; + + // Started when the user first presses the power button while in a + // logged-in-as-a-non-guest-user, unlocked state. When it fires, we lock the + // screen. + base::OneShotTimer<PowerButtonController> lock_timer_; + + // Started when we request that the screen be locked. When it fires, we + // assume that our request got dropped. + base::OneShotTimer<PowerButtonController> lock_fail_timer_; + + // Started when the screen is locked while the power button is held. Adds a + // delay between the appearance of the lock screen and the beginning of the + // pre-shutdown animation. + base::OneShotTimer<PowerButtonController> lock_to_shutdown_timer_; + + // Started when we begin displaying the pre-shutdown animation. When it + // fires, we start the shutdown animation and get ready to request shutdown. + base::OneShotTimer<PowerButtonController> shutdown_timer_; + + // Started when we display the shutdown animation. When it fires, we actually + // request shutdown. Gives the animation time to complete before Chrome, X, + // etc. are shut down. + base::OneShotTimer<PowerButtonController> real_shutdown_timer_; + + // Started when we abort the pre-lock state. When it fires, we hide + // |background_layer_|, as the desktop background is now covering the whole + // screen. + base::OneShotTimer<PowerButtonController> hide_background_layer_timer_; + + DISALLOW_COPY_AND_ASSIGN(PowerButtonController); +}; + +} // namespace ash + +#endif // ASH_WM_POWER_BUTTON_CONTROLLER_H_ diff --git a/ash/wm/power_button_controller_unittest.cc b/ash/wm/power_button_controller_unittest.cc new file mode 100644 index 0000000..85ff5aa --- /dev/null +++ b/ash/wm/power_button_controller_unittest.cc @@ -0,0 +1,276 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ash/wm/power_button_controller.h" + +#include "ash/shell.h" +#include "ash/test/aura_shell_test_base.h" +#include "base/memory/scoped_ptr.h" +#include "base/time.h" +#include "ui/aura/root_window.h" + +namespace ash { +namespace test { + +// Fake implementation of PowerButtonControllerDelegate that just logs requests +// to lock the screen and shut down the device. +class TestPowerButtonControllerDelegate : public PowerButtonControllerDelegate { + public: + TestPowerButtonControllerDelegate() + : num_lock_requests_(0), + num_shutdown_requests_(0) {} + + int num_lock_requests() const { return num_lock_requests_; } + int num_shutdown_requests() const { return num_shutdown_requests_; } + + // PowerButtonControllerDelegate implementation. + virtual void RequestLockScreen() OVERRIDE { + num_lock_requests_++; + } + virtual void RequestShutdown() OVERRIDE { + num_shutdown_requests_++; + } + + private: + int num_lock_requests_; + int num_shutdown_requests_; + + DISALLOW_COPY_AND_ASSIGN(TestPowerButtonControllerDelegate); +}; + +class PowerButtonControllerTest : public AuraShellTestBase { + public: + PowerButtonControllerTest() : controller_(NULL), delegate_(NULL) {} + virtual ~PowerButtonControllerTest() {} + + void SetUp() OVERRIDE { + AuraShellTestBase::SetUp(); + delegate_ = new TestPowerButtonControllerDelegate; + controller_ = Shell::GetInstance()->power_button_controller(); + controller_->set_delegate(delegate_); // transfers ownership + test_api_.reset(new PowerButtonController::TestApi(controller_)); + } + + protected: + PowerButtonController* controller_; // not owned + TestPowerButtonControllerDelegate* delegate_; // not owned + scoped_ptr<PowerButtonController::TestApi> test_api_; + + private: + DISALLOW_COPY_AND_ASSIGN(PowerButtonControllerTest); +}; + +// When we hold the power button while the user isn't logged in, we should shut +// down the machine directly. +TEST_F(PowerButtonControllerTest, ShutdownWhenNotLoggedIn) { + controller_->OnLoginStateChange(false /*logged_in*/, false /*is_guest*/); + controller_->OnLockStateChange(false); + EXPECT_FALSE(test_api_->BackgroundLayerIsVisible()); + + // Press the power button and check that we start the shutdown timer. + controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); + EXPECT_FALSE(test_api_->lock_timer_is_running()); + EXPECT_TRUE(test_api_->shutdown_timer_is_running()); + EXPECT_TRUE( + test_api_->ContainerGroupIsAnimated( + PowerButtonController::ALL_CONTAINERS, + PowerButtonController::ANIMATION_SLOW_CLOSE)); + EXPECT_TRUE(test_api_->BackgroundLayerIsVisible()); + + // Release the power button before the shutdown timer fires. + controller_->OnPowerButtonEvent(false, base::TimeTicks::Now()); + EXPECT_FALSE(test_api_->shutdown_timer_is_running()); + EXPECT_TRUE( + test_api_->ContainerGroupIsAnimated( + PowerButtonController::ALL_CONTAINERS, + PowerButtonController::ANIMATION_UNDO_SLOW_CLOSE)); + EXPECT_TRUE(test_api_->BackgroundLayerIsVisible()); + + // We should re-hide the black background layer after waiting long enough for + // the animation to finish. + EXPECT_TRUE(test_api_->hide_background_layer_timer_is_running()); + test_api_->trigger_hide_background_layer_timeout(); + EXPECT_FALSE(test_api_->BackgroundLayerIsVisible()); + + // Press the button again and make the shutdown timeout fire this time. + // Check that we start the timer for actually requesting the shutdown. + controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); + EXPECT_TRUE(test_api_->shutdown_timer_is_running()); + test_api_->trigger_shutdown_timeout(); + EXPECT_TRUE(test_api_->real_shutdown_timer_is_running()); + EXPECT_EQ(0, delegate_->num_shutdown_requests()); + EXPECT_TRUE( + test_api_->ContainerGroupIsAnimated( + PowerButtonController::ALL_CONTAINERS, + PowerButtonController::ANIMATION_FAST_CLOSE)); + + // When the timout fires, we should request a shutdown. + test_api_->trigger_real_shutdown_timeout(); + EXPECT_EQ(1, delegate_->num_shutdown_requests()); +} + +// Test that we lock the screen and deal with unlocking correctly. +TEST_F(PowerButtonControllerTest, LockAndUnlock) { + controller_->OnLoginStateChange(true /*logged_in*/, false /*is_guest*/); + controller_->OnLockStateChange(false); + EXPECT_FALSE(test_api_->BackgroundLayerIsVisible()); + + // We should initially be showing the screen locker containers, since they + // also contain login-related windows that we want to show during the + // logging-in animation. + EXPECT_TRUE( + test_api_->ContainerGroupIsAnimated( + PowerButtonController::SCREEN_LOCKER_CONTAINERS, + PowerButtonController::ANIMATION_RESTORE)); + + // Press the power button and check that the lock timer is started and that we + // start scaling the non-screen-locker containers. + controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); + EXPECT_TRUE(test_api_->lock_timer_is_running()); + EXPECT_FALSE(test_api_->shutdown_timer_is_running()); + EXPECT_TRUE( + test_api_->ContainerGroupIsAnimated( + PowerButtonController::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, + PowerButtonController::ANIMATION_SLOW_CLOSE)); + EXPECT_TRUE(test_api_->BackgroundLayerIsVisible()); + + // Release the button before the lock timer fires. + controller_->OnPowerButtonEvent(false, base::TimeTicks::Now()); + EXPECT_FALSE(test_api_->lock_timer_is_running()); + EXPECT_TRUE( + test_api_->ContainerGroupIsAnimated( + PowerButtonController::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, + PowerButtonController::ANIMATION_UNDO_SLOW_CLOSE)); + EXPECT_TRUE(test_api_->BackgroundLayerIsVisible()); + EXPECT_TRUE(test_api_->hide_background_layer_timer_is_running()); + test_api_->trigger_hide_background_layer_timeout(); + EXPECT_FALSE(test_api_->BackgroundLayerIsVisible()); + + // Press the button and fire the lock timer. We should request that the + // screen be locked, but we should still be in the slow-close animation. + controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); + EXPECT_TRUE(test_api_->lock_timer_is_running()); + EXPECT_EQ(0, delegate_->num_lock_requests()); + test_api_->trigger_lock_timeout(); + EXPECT_EQ(1, delegate_->num_lock_requests()); + EXPECT_TRUE( + test_api_->ContainerGroupIsAnimated( + PowerButtonController::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, + PowerButtonController::ANIMATION_SLOW_CLOSE)); + EXPECT_TRUE(test_api_->BackgroundLayerIsVisible()); + + // Notify that we locked successfully. + controller_->OnStartingLock(); + EXPECT_TRUE( + test_api_->ContainerGroupIsAnimated( + PowerButtonController::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, + PowerButtonController::ANIMATION_FAST_CLOSE)); + EXPECT_TRUE( + test_api_->ContainerGroupIsAnimated( + PowerButtonController::SCREEN_LOCKER_CONTAINERS, + PowerButtonController::ANIMATION_HIDE)); + + // Notify that the lock window is visible. We should make it fade in. + controller_->OnLockStateChange(true); + EXPECT_TRUE( + test_api_->ContainerGroupIsAnimated( + PowerButtonController::SCREEN_LOCKER_AND_RELATED_CONTAINERS, + PowerButtonController::ANIMATION_FADE_IN)); + + // When we release the power button, the lock-to-shutdown timer should be + // stopped. + EXPECT_TRUE(test_api_->lock_to_shutdown_timer_is_running()); + controller_->OnPowerButtonEvent(false, base::TimeTicks::Now()); + EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running()); + + // Notify that the screen has been unlocked. We should show the + // non-screen-locker windows and hide the background layer. + controller_->OnLockStateChange(false); + EXPECT_TRUE( + test_api_->ContainerGroupIsAnimated( + PowerButtonController::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, + PowerButtonController::ANIMATION_RESTORE)); + EXPECT_FALSE(test_api_->BackgroundLayerIsVisible()); +} + +// Hold the power button down from the unlocked state to eventual shutdown. +TEST_F(PowerButtonControllerTest, LockToShutdown) { + controller_->OnLoginStateChange(true /*logged_in*/, false /*is_guest*/); + controller_->OnLockStateChange(false); + + // Hold the power button and lock the screen. + controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); + EXPECT_TRUE(test_api_->lock_timer_is_running()); + test_api_->trigger_lock_timeout(); + controller_->OnStartingLock(); + controller_->OnLockStateChange(true); + EXPECT_TRUE(test_api_->BackgroundLayerIsVisible()); + + // When the lock-to-shutdown timeout fires, we should start the shutdown + // timer. + EXPECT_TRUE(test_api_->lock_to_shutdown_timer_is_running()); + test_api_->trigger_lock_to_shutdown_timeout(); + EXPECT_TRUE(test_api_->shutdown_timer_is_running()); + EXPECT_TRUE( + test_api_->ContainerGroupIsAnimated( + PowerButtonController::ALL_CONTAINERS, + PowerButtonController::ANIMATION_SLOW_CLOSE)); + + // Fire the shutdown timeout and check that we request shutdown. + test_api_->trigger_shutdown_timeout(); + EXPECT_TRUE(test_api_->real_shutdown_timer_is_running()); + EXPECT_EQ(0, delegate_->num_shutdown_requests()); + test_api_->trigger_real_shutdown_timeout(); + EXPECT_EQ(1, delegate_->num_shutdown_requests()); + EXPECT_TRUE(test_api_->BackgroundLayerIsVisible()); +} + +// Test that we handle the case where lock requests are ignored. +TEST_F(PowerButtonControllerTest, LockFail) { + controller_->OnLoginStateChange(true /*logged_in*/, false /*is_guest*/); + controller_->OnLockStateChange(false); + + // Hold the power button and lock the screen. + controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); + EXPECT_TRUE(test_api_->lock_timer_is_running()); + EXPECT_TRUE( + test_api_->ContainerGroupIsAnimated( + PowerButtonController::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, + PowerButtonController::ANIMATION_RESTORE)); + EXPECT_TRUE(test_api_->BackgroundLayerIsVisible()); + test_api_->trigger_lock_timeout(); + EXPECT_EQ(1, delegate_->num_lock_requests()); + EXPECT_TRUE(test_api_->lock_fail_timer_is_running()); + + // We shouldn't start the lock-to-shutdown timer until the screen has actually + // been locked. + EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running()); + + // Act as if the request timed out. We should restore the windows. + test_api_->trigger_lock_fail_timeout(); + EXPECT_TRUE( + test_api_->ContainerGroupIsAnimated( + PowerButtonController::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, + PowerButtonController::ANIMATION_RESTORE)); + EXPECT_FALSE(test_api_->BackgroundLayerIsVisible()); +} + +// Test that we start the timer to hide the background layer when the power +// button is released, but that we cancel the timer if the button is pressed +// again before the timer has fired. +TEST_F(PowerButtonControllerTest, CancelHideBackground) { + controller_->OnLoginStateChange(false /*logged_in*/, false /*is_guest*/); + controller_->OnLockStateChange(false); + + controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); + controller_->OnPowerButtonEvent(false, base::TimeTicks::Now()); + EXPECT_TRUE(test_api_->hide_background_layer_timer_is_running()); + + // We should cancel the timer if we get another button-down event. + controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); + EXPECT_FALSE(test_api_->hide_background_layer_timer_is_running()); +} + +} // namespace test +} // namespace ash diff --git a/ash/wm/shadow_controller.h b/ash/wm/shadow_controller.h index 0613dd7..7a75a91 100644 --- a/ash/wm/shadow_controller.h +++ b/ash/wm/shadow_controller.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -31,7 +31,7 @@ class Shadow; // shadows as needed. class ASH_EXPORT ShadowController : public aura::RootWindowObserver, public aura::WindowObserver { -public: + public: class TestApi { public: explicit TestApi(ShadowController* controller) : controller_(controller) {} @@ -83,7 +83,7 @@ public: DISALLOW_COPY_AND_ASSIGN(ShadowController); }; -} // namepsace internal -} // namepsace ash +} // namespace internal +} // namespace ash #endif // ASH_WM_SHADOW_CONTROLLER_H_ |