OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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/power_button_controller.h" |
| 6 |
| 7 #include "ash/shell.h" |
| 8 #include "ash/test/aura_shell_test_base.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/time.h" |
| 11 #include "ui/aura/root_window.h" |
| 12 |
| 13 namespace ash { |
| 14 namespace test { |
| 15 |
| 16 // Fake implementation of PowerButtonControllerDelegate that just logs requests |
| 17 // to lock the screen and shut down the device. |
| 18 class TestPowerButtonControllerDelegate : public PowerButtonControllerDelegate { |
| 19 public: |
| 20 TestPowerButtonControllerDelegate() |
| 21 : num_lock_requests_(0), |
| 22 num_shutdown_requests_(0) {} |
| 23 |
| 24 int num_lock_requests() const { return num_lock_requests_; } |
| 25 int num_shutdown_requests() const { return num_shutdown_requests_; } |
| 26 |
| 27 // PowerButtonControllerDelegate implementation. |
| 28 virtual void RequestLockScreen() OVERRIDE { |
| 29 num_lock_requests_++; |
| 30 } |
| 31 virtual void RequestShutdown() OVERRIDE { |
| 32 num_shutdown_requests_++; |
| 33 } |
| 34 |
| 35 private: |
| 36 int num_lock_requests_; |
| 37 int num_shutdown_requests_; |
| 38 |
| 39 DISALLOW_COPY_AND_ASSIGN(TestPowerButtonControllerDelegate); |
| 40 }; |
| 41 |
| 42 class PowerButtonControllerTest : public AuraShellTestBase { |
| 43 public: |
| 44 PowerButtonControllerTest() : controller_(NULL), delegate_(NULL) {} |
| 45 virtual ~PowerButtonControllerTest() {} |
| 46 |
| 47 void SetUp() OVERRIDE { |
| 48 AuraShellTestBase::SetUp(); |
| 49 delegate_ = new TestPowerButtonControllerDelegate; |
| 50 controller_ = Shell::GetInstance()->power_button_controller(); |
| 51 controller_->set_delegate(delegate_); // transfers ownership |
| 52 test_api_.reset(new PowerButtonController::TestApi(controller_)); |
| 53 } |
| 54 |
| 55 protected: |
| 56 PowerButtonController* controller_; // not owned |
| 57 TestPowerButtonControllerDelegate* delegate_; // not owned |
| 58 scoped_ptr<PowerButtonController::TestApi> test_api_; |
| 59 |
| 60 private: |
| 61 DISALLOW_COPY_AND_ASSIGN(PowerButtonControllerTest); |
| 62 }; |
| 63 |
| 64 // When we hold the power button while the user isn't logged in, we should shut |
| 65 // down the machine directly. |
| 66 TEST_F(PowerButtonControllerTest, ShutdownWhenNotLoggedIn) { |
| 67 controller_->OnLoginStateChange(false /*logged_in*/, false /*is_guest*/); |
| 68 controller_->OnLockStateChange(false); |
| 69 EXPECT_FALSE(test_api_->BackgroundLayerIsVisible()); |
| 70 |
| 71 // Press the power button and check that we start the shutdown timer. |
| 72 controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); |
| 73 EXPECT_FALSE(test_api_->lock_timer_is_running()); |
| 74 EXPECT_TRUE(test_api_->shutdown_timer_is_running()); |
| 75 EXPECT_TRUE( |
| 76 test_api_->ContainerGroupIsAnimated( |
| 77 PowerButtonController::ALL_CONTAINERS, |
| 78 PowerButtonController::ANIMATION_SLOW_CLOSE)); |
| 79 EXPECT_TRUE(test_api_->BackgroundLayerIsVisible()); |
| 80 |
| 81 // Release the power button before the shutdown timer fires. |
| 82 controller_->OnPowerButtonEvent(false, base::TimeTicks::Now()); |
| 83 EXPECT_FALSE(test_api_->shutdown_timer_is_running()); |
| 84 EXPECT_TRUE( |
| 85 test_api_->ContainerGroupIsAnimated( |
| 86 PowerButtonController::ALL_CONTAINERS, |
| 87 PowerButtonController::ANIMATION_UNDO_SLOW_CLOSE)); |
| 88 EXPECT_TRUE(test_api_->BackgroundLayerIsVisible()); |
| 89 |
| 90 // We should re-hide the black background layer after waiting long enough for |
| 91 // the animation to finish. |
| 92 EXPECT_TRUE(test_api_->hide_background_layer_timer_is_running()); |
| 93 test_api_->trigger_hide_background_layer_timeout(); |
| 94 EXPECT_FALSE(test_api_->BackgroundLayerIsVisible()); |
| 95 |
| 96 // Press the button again and make the shutdown timeout fire this time. |
| 97 // Check that we start the timer for actually requesting the shutdown. |
| 98 controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); |
| 99 EXPECT_TRUE(test_api_->shutdown_timer_is_running()); |
| 100 test_api_->trigger_shutdown_timeout(); |
| 101 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running()); |
| 102 EXPECT_EQ(0, delegate_->num_shutdown_requests()); |
| 103 EXPECT_TRUE( |
| 104 test_api_->ContainerGroupIsAnimated( |
| 105 PowerButtonController::ALL_CONTAINERS, |
| 106 PowerButtonController::ANIMATION_FAST_CLOSE)); |
| 107 |
| 108 // When the timout fires, we should request a shutdown. |
| 109 test_api_->trigger_real_shutdown_timeout(); |
| 110 EXPECT_EQ(1, delegate_->num_shutdown_requests()); |
| 111 } |
| 112 |
| 113 // Test that we lock the screen and deal with unlocking correctly. |
| 114 TEST_F(PowerButtonControllerTest, LockAndUnlock) { |
| 115 controller_->OnLoginStateChange(true /*logged_in*/, false /*is_guest*/); |
| 116 controller_->OnLockStateChange(false); |
| 117 EXPECT_FALSE(test_api_->BackgroundLayerIsVisible()); |
| 118 |
| 119 // We should initially be showing the screen locker containers, since they |
| 120 // also contain login-related windows that we want to show during the |
| 121 // logging-in animation. |
| 122 EXPECT_TRUE( |
| 123 test_api_->ContainerGroupIsAnimated( |
| 124 PowerButtonController::SCREEN_LOCKER_CONTAINERS, |
| 125 PowerButtonController::ANIMATION_RESTORE)); |
| 126 |
| 127 // Press the power button and check that the lock timer is started and that we |
| 128 // start scaling the non-screen-locker containers. |
| 129 controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); |
| 130 EXPECT_TRUE(test_api_->lock_timer_is_running()); |
| 131 EXPECT_FALSE(test_api_->shutdown_timer_is_running()); |
| 132 EXPECT_TRUE( |
| 133 test_api_->ContainerGroupIsAnimated( |
| 134 PowerButtonController::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, |
| 135 PowerButtonController::ANIMATION_SLOW_CLOSE)); |
| 136 EXPECT_TRUE(test_api_->BackgroundLayerIsVisible()); |
| 137 |
| 138 // Release the button before the lock timer fires. |
| 139 controller_->OnPowerButtonEvent(false, base::TimeTicks::Now()); |
| 140 EXPECT_FALSE(test_api_->lock_timer_is_running()); |
| 141 EXPECT_TRUE( |
| 142 test_api_->ContainerGroupIsAnimated( |
| 143 PowerButtonController::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, |
| 144 PowerButtonController::ANIMATION_UNDO_SLOW_CLOSE)); |
| 145 EXPECT_TRUE(test_api_->BackgroundLayerIsVisible()); |
| 146 EXPECT_TRUE(test_api_->hide_background_layer_timer_is_running()); |
| 147 test_api_->trigger_hide_background_layer_timeout(); |
| 148 EXPECT_FALSE(test_api_->BackgroundLayerIsVisible()); |
| 149 |
| 150 // Press the button and fire the lock timer. We should request that the |
| 151 // screen be locked, but we should still be in the slow-close animation. |
| 152 controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); |
| 153 EXPECT_TRUE(test_api_->lock_timer_is_running()); |
| 154 EXPECT_EQ(0, delegate_->num_lock_requests()); |
| 155 test_api_->trigger_lock_timeout(); |
| 156 EXPECT_EQ(1, delegate_->num_lock_requests()); |
| 157 EXPECT_TRUE( |
| 158 test_api_->ContainerGroupIsAnimated( |
| 159 PowerButtonController::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, |
| 160 PowerButtonController::ANIMATION_SLOW_CLOSE)); |
| 161 EXPECT_TRUE(test_api_->BackgroundLayerIsVisible()); |
| 162 |
| 163 // Notify that we locked successfully. |
| 164 controller_->OnStartingLock(); |
| 165 EXPECT_TRUE( |
| 166 test_api_->ContainerGroupIsAnimated( |
| 167 PowerButtonController::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, |
| 168 PowerButtonController::ANIMATION_FAST_CLOSE)); |
| 169 EXPECT_TRUE( |
| 170 test_api_->ContainerGroupIsAnimated( |
| 171 PowerButtonController::SCREEN_LOCKER_CONTAINERS, |
| 172 PowerButtonController::ANIMATION_HIDE)); |
| 173 |
| 174 // Notify that the lock window is visible. We should make it fade in. |
| 175 controller_->OnLockStateChange(true); |
| 176 EXPECT_TRUE( |
| 177 test_api_->ContainerGroupIsAnimated( |
| 178 PowerButtonController::SCREEN_LOCKER_AND_RELATED_CONTAINERS, |
| 179 PowerButtonController::ANIMATION_FADE_IN)); |
| 180 |
| 181 // When we release the power button, the lock-to-shutdown timer should be |
| 182 // stopped. |
| 183 EXPECT_TRUE(test_api_->lock_to_shutdown_timer_is_running()); |
| 184 controller_->OnPowerButtonEvent(false, base::TimeTicks::Now()); |
| 185 EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running()); |
| 186 |
| 187 // Notify that the screen has been unlocked. We should show the |
| 188 // non-screen-locker windows and hide the background layer. |
| 189 controller_->OnLockStateChange(false); |
| 190 EXPECT_TRUE( |
| 191 test_api_->ContainerGroupIsAnimated( |
| 192 PowerButtonController::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, |
| 193 PowerButtonController::ANIMATION_RESTORE)); |
| 194 EXPECT_FALSE(test_api_->BackgroundLayerIsVisible()); |
| 195 } |
| 196 |
| 197 // Hold the power button down from the unlocked state to eventual shutdown. |
| 198 TEST_F(PowerButtonControllerTest, LockToShutdown) { |
| 199 controller_->OnLoginStateChange(true /*logged_in*/, false /*is_guest*/); |
| 200 controller_->OnLockStateChange(false); |
| 201 |
| 202 // Hold the power button and lock the screen. |
| 203 controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); |
| 204 EXPECT_TRUE(test_api_->lock_timer_is_running()); |
| 205 test_api_->trigger_lock_timeout(); |
| 206 controller_->OnStartingLock(); |
| 207 controller_->OnLockStateChange(true); |
| 208 EXPECT_TRUE(test_api_->BackgroundLayerIsVisible()); |
| 209 |
| 210 // When the lock-to-shutdown timeout fires, we should start the shutdown |
| 211 // timer. |
| 212 EXPECT_TRUE(test_api_->lock_to_shutdown_timer_is_running()); |
| 213 test_api_->trigger_lock_to_shutdown_timeout(); |
| 214 EXPECT_TRUE(test_api_->shutdown_timer_is_running()); |
| 215 EXPECT_TRUE( |
| 216 test_api_->ContainerGroupIsAnimated( |
| 217 PowerButtonController::ALL_CONTAINERS, |
| 218 PowerButtonController::ANIMATION_SLOW_CLOSE)); |
| 219 |
| 220 // Fire the shutdown timeout and check that we request shutdown. |
| 221 test_api_->trigger_shutdown_timeout(); |
| 222 EXPECT_TRUE(test_api_->real_shutdown_timer_is_running()); |
| 223 EXPECT_EQ(0, delegate_->num_shutdown_requests()); |
| 224 test_api_->trigger_real_shutdown_timeout(); |
| 225 EXPECT_EQ(1, delegate_->num_shutdown_requests()); |
| 226 EXPECT_TRUE(test_api_->BackgroundLayerIsVisible()); |
| 227 } |
| 228 |
| 229 // Test that we handle the case where lock requests are ignored. |
| 230 TEST_F(PowerButtonControllerTest, LockFail) { |
| 231 controller_->OnLoginStateChange(true /*logged_in*/, false /*is_guest*/); |
| 232 controller_->OnLockStateChange(false); |
| 233 |
| 234 // Hold the power button and lock the screen. |
| 235 controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); |
| 236 EXPECT_TRUE(test_api_->lock_timer_is_running()); |
| 237 EXPECT_TRUE( |
| 238 test_api_->ContainerGroupIsAnimated( |
| 239 PowerButtonController::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, |
| 240 PowerButtonController::ANIMATION_RESTORE)); |
| 241 EXPECT_TRUE(test_api_->BackgroundLayerIsVisible()); |
| 242 test_api_->trigger_lock_timeout(); |
| 243 EXPECT_EQ(1, delegate_->num_lock_requests()); |
| 244 EXPECT_TRUE(test_api_->lock_fail_timer_is_running()); |
| 245 |
| 246 // We shouldn't start the lock-to-shutdown timer until the screen has actually |
| 247 // been locked. |
| 248 EXPECT_FALSE(test_api_->lock_to_shutdown_timer_is_running()); |
| 249 |
| 250 // Act as if the request timed out. We should restore the windows. |
| 251 test_api_->trigger_lock_fail_timeout(); |
| 252 EXPECT_TRUE( |
| 253 test_api_->ContainerGroupIsAnimated( |
| 254 PowerButtonController::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, |
| 255 PowerButtonController::ANIMATION_RESTORE)); |
| 256 EXPECT_FALSE(test_api_->BackgroundLayerIsVisible()); |
| 257 } |
| 258 |
| 259 // Test that we start the timer to hide the background layer when the power |
| 260 // button is released, but that we cancel the timer if the button is pressed |
| 261 // again before the timer has fired. |
| 262 TEST_F(PowerButtonControllerTest, CancelHideBackground) { |
| 263 controller_->OnLoginStateChange(false /*logged_in*/, false /*is_guest*/); |
| 264 controller_->OnLockStateChange(false); |
| 265 |
| 266 controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); |
| 267 controller_->OnPowerButtonEvent(false, base::TimeTicks::Now()); |
| 268 EXPECT_TRUE(test_api_->hide_background_layer_timer_is_running()); |
| 269 |
| 270 // We should cancel the timer if we get another button-down event. |
| 271 controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); |
| 272 EXPECT_FALSE(test_api_->hide_background_layer_timer_is_running()); |
| 273 } |
| 274 |
| 275 } // namespace test |
| 276 } // namespace ash |
OLD | NEW |