OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/system/chromeos/power/tablet_power_button_controller.h" |
| 6 |
| 7 #include "ash/common/ash_switches.h" |
| 8 #include "ash/common/session/session_state_delegate.h" |
| 9 #include "ash/common/wm/maximize_mode/maximize_mode_controller.h" |
| 10 #include "ash/common/wm_shell.h" |
| 11 #include "ash/shell.h" |
| 12 #include "ash/test/ash_test_base.h" |
| 13 #include "ash/test/lock_state_controller_test_api.h" |
| 14 #include "ash/wm/lock_state_controller.h" |
| 15 #include "base/command_line.h" |
| 16 #include "chromeos/dbus/dbus_thread_manager.h" |
| 17 #include "chromeos/dbus/fake_power_manager_client.h" |
| 18 #include "ui/events/test/event_generator.h" |
| 19 |
| 20 namespace ash { |
| 21 namespace test { |
| 22 |
| 23 namespace { |
| 24 |
| 25 void CheckBoolEquals(bool result, bool expected) { |
| 26 EXPECT_EQ(result, expected); |
| 27 } |
| 28 |
| 29 } // namespace |
| 30 |
| 31 class TabletPowerButtonControllerTest : public AshTestBase { |
| 32 public: |
| 33 TabletPowerButtonControllerTest() {} |
| 34 ~TabletPowerButtonControllerTest() override {} |
| 35 |
| 36 void SetUp() override { |
| 37 // This also initializes DBusThreadManager. |
| 38 std::unique_ptr<chromeos::DBusThreadManagerSetter> dbus_setter = |
| 39 chromeos::DBusThreadManager::GetSetterForTesting(); |
| 40 power_manager_client_ = new chromeos::FakePowerManagerClient(); |
| 41 dbus_setter->SetPowerManagerClient(base::WrapUnique(power_manager_client_)); |
| 42 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 43 switches::kAshEnableTouchViewTesting); |
| 44 AshTestBase::SetUp(); |
| 45 |
| 46 lock_state_controller_ = Shell::GetInstance()->lock_state_controller(); |
| 47 tablet_controller_ = TabletPowerButtonController::GetInstance(); |
| 48 test_api_ = base::MakeUnique<TabletPowerButtonController::TestApi>( |
| 49 tablet_controller_); |
| 50 generator_ = &AshTestBase::GetEventGenerator(); |
| 51 CheckBacklightsForcedOff(false); |
| 52 } |
| 53 |
| 54 void TearDown() override { |
| 55 generator_ = nullptr; |
| 56 AshTestBase::TearDown(); |
| 57 chromeos::DBusThreadManager::Shutdown(); |
| 58 } |
| 59 |
| 60 protected: |
| 61 void PressPowerButton() { |
| 62 tablet_controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); |
| 63 } |
| 64 |
| 65 void ReleasePowerButton() { |
| 66 tablet_controller_->OnPowerButtonEvent(false, base::TimeTicks::Now()); |
| 67 } |
| 68 |
| 69 void SystemUnlocks() { |
| 70 lock_state_controller_->OnLockStateChanged(false); |
| 71 WmShell::Get()->GetSessionStateDelegate()->UnlockScreen(); |
| 72 } |
| 73 |
| 74 void Initialize(LoginStatus status) { |
| 75 lock_state_controller_->OnLoginStateChanged(status); |
| 76 SetUserLoggedIn(status != LoginStatus::NOT_LOGGED_IN); |
| 77 lock_state_controller_->OnLockStateChanged(false); |
| 78 } |
| 79 |
| 80 void EnableMaximizeMode(bool enabled) { |
| 81 WmShell::Get()->maximize_mode_controller()->EnableMaximizeModeWindowManager( |
| 82 enabled); |
| 83 } |
| 84 |
| 85 void CheckLockedState(bool expected_locked) { |
| 86 EXPECT_EQ(WmShell::Get()->GetSessionStateDelegate()->IsScreenLocked(), |
| 87 expected_locked); |
| 88 } |
| 89 |
| 90 void CheckBacklightsForcedOff(bool expected_forced_off) { |
| 91 power_manager_client_->GetBacklightsForcedOff( |
| 92 base::Bind(&CheckBoolEquals, expected_forced_off)); |
| 93 } |
| 94 |
| 95 // Ownership is passed on to chromeos::DBusThreadManager. |
| 96 chromeos::FakePowerManagerClient* power_manager_client_; |
| 97 LockStateController* lock_state_controller_; // Not owned. |
| 98 TabletPowerButtonController* tablet_controller_; // Not owned. |
| 99 std::unique_ptr<TabletPowerButtonController::TestApi> test_api_; |
| 100 ui::test::EventGenerator* generator_ = nullptr; |
| 101 |
| 102 DISALLOW_COPY_AND_ASSIGN(TabletPowerButtonControllerTest); |
| 103 }; |
| 104 |
| 105 TEST_F(TabletPowerButtonControllerTest, LockScreenIfRequired) { |
| 106 Initialize(LoginStatus::USER); |
| 107 SetShouldLockScreenAutomatically(true); |
| 108 CheckLockedState(false); |
| 109 |
| 110 // On User logged in status, power-button-press-release should lock screen if |
| 111 // should lock screen automatically. |
| 112 PressPowerButton(); |
| 113 ReleasePowerButton(); |
| 114 CheckLockedState(true); |
| 115 |
| 116 // On locked state, power-button-press-release should do nothing. |
| 117 PressPowerButton(); |
| 118 ReleasePowerButton(); |
| 119 CheckLockedState(true); |
| 120 |
| 121 // Unlock the sceen. |
| 122 SystemUnlocks(); |
| 123 CheckLockedState(false); |
| 124 |
| 125 // power-button-press-release should not lock the screen if should not lock |
| 126 // screen automatically. |
| 127 SetShouldLockScreenAutomatically(false); |
| 128 PressPowerButton(); |
| 129 ReleasePowerButton(); |
| 130 CheckLockedState(false); |
| 131 } |
| 132 |
| 133 // This test is to test the power button released is done before shutdown |
| 134 // is timeout. |
| 135 TEST_F(TabletPowerButtonControllerTest, |
| 136 ReleasePowerButtonBeforeStartingShutdownTimer) { |
| 137 PressPowerButton(); |
| 138 EXPECT_TRUE(test_api_->ShutdownTimerIsRunning()); |
| 139 CheckBacklightsForcedOff(false); |
| 140 ReleasePowerButton(); |
| 141 EXPECT_FALSE(test_api_->ShutdownTimerIsRunning()); |
| 142 CheckBacklightsForcedOff(true); |
| 143 |
| 144 PressPowerButton(); |
| 145 EXPECT_TRUE(test_api_->ShutdownTimerIsRunning()); |
| 146 CheckBacklightsForcedOff(false); |
| 147 ReleasePowerButton(); |
| 148 EXPECT_FALSE(test_api_->ShutdownTimerIsRunning()); |
| 149 CheckBacklightsForcedOff(false); |
| 150 } |
| 151 |
| 152 // This test is to test the power button released is done after shutdown timer |
| 153 // is timeout, where it will start shutdown animation timer. |
| 154 TEST_F(TabletPowerButtonControllerTest, |
| 155 ReleasePowerButtonDuringShutdownAnimation) { |
| 156 // Initializes LockStateControllerTestApi to observe shutdown animation timer. |
| 157 std::unique_ptr<LockStateControllerTestApi> lock_state_test_api = |
| 158 base::MakeUnique<LockStateControllerTestApi>(lock_state_controller_); |
| 159 |
| 160 PressPowerButton(); |
| 161 test_api_->TriggerShutdownTimeout(); |
| 162 ReleasePowerButton(); |
| 163 CheckBacklightsForcedOff(false); |
| 164 |
| 165 // Set backlights forced off to true for the next test. |
| 166 PressPowerButton(); |
| 167 ReleasePowerButton(); |
| 168 CheckBacklightsForcedOff(true); |
| 169 |
| 170 PressPowerButton(); |
| 171 test_api_->TriggerShutdownTimeout(); |
| 172 EXPECT_TRUE(lock_state_test_api->shutdown_timer_is_running()); |
| 173 ReleasePowerButton(); |
| 174 EXPECT_FALSE(lock_state_test_api->shutdown_timer_is_running()); |
| 175 CheckBacklightsForcedOff(false); |
| 176 } |
| 177 |
| 178 // When screen is turned off, even it is not set to forced off by power button |
| 179 // (can be caused by idle screen off, suspended), we should consider it as |
| 180 // "backlights forced off" to set backlights forced off state when tapping power |
| 181 // button. |
| 182 TEST_F(TabletPowerButtonControllerTest, TappingPowerButtonWhenScreenIsOff) { |
| 183 power_manager_client_->SendBrightnessChanged(0, false); |
| 184 PressPowerButton(); |
| 185 CheckBacklightsForcedOff(false); |
| 186 ReleasePowerButton(); |
| 187 |
| 188 CheckBacklightsForcedOff(false); |
| 189 power_manager_client_->SendBrightnessChanged(10, false); |
| 190 PressPowerButton(); |
| 191 ReleasePowerButton(); |
| 192 CheckBacklightsForcedOff(true); |
| 193 } |
| 194 |
| 195 // For convertible device working on laptop mode, test keyboard/mouse event |
| 196 // when screen is off. |
| 197 TEST_F(TabletPowerButtonControllerTest, ConvertibleOnLaptopMode) { |
| 198 EnableMaximizeMode(false); |
| 199 |
| 200 // KeyEvent should SetBacklightsForcedOff(false). |
| 201 PressPowerButton(); |
| 202 ReleasePowerButton(); |
| 203 CheckBacklightsForcedOff(true); |
| 204 generator_->PressKey(ui::VKEY_L, ui::EF_NONE); |
| 205 CheckBacklightsForcedOff(false); |
| 206 |
| 207 // Regular mouse event should SetBacklightsForcedOff(false). |
| 208 PressPowerButton(); |
| 209 ReleasePowerButton(); |
| 210 CheckBacklightsForcedOff(true); |
| 211 generator_->MoveMouseBy(1, 1); |
| 212 CheckBacklightsForcedOff(false); |
| 213 |
| 214 // Touchpad mouse event should SetBacklightsForcedOff(false). |
| 215 PressPowerButton(); |
| 216 ReleasePowerButton(); |
| 217 CheckBacklightsForcedOff(true); |
| 218 generator_->PressTouch(); |
| 219 CheckBacklightsForcedOff(false); |
| 220 |
| 221 // Stylus mouse event should not SetBacklightsForcedOff(false). |
| 222 PressPowerButton(); |
| 223 ReleasePowerButton(); |
| 224 CheckBacklightsForcedOff(true); |
| 225 generator_->EnterPenPointerMode(); |
| 226 generator_->MoveMouseBy(1, 1); |
| 227 CheckBacklightsForcedOff(true); |
| 228 generator_->ExitPenPointerMode(); |
| 229 } |
| 230 |
| 231 // For convertible device working on tablet mode, keyboard/mouse event should |
| 232 // not SetBacklightsForcedOff(false) when screen is off. |
| 233 TEST_F(TabletPowerButtonControllerTest, ConvertibleOnMaximizeMode) { |
| 234 EnableMaximizeMode(true); |
| 235 |
| 236 PressPowerButton(); |
| 237 ReleasePowerButton(); |
| 238 CheckBacklightsForcedOff(true); |
| 239 generator_->PressKey(ui::VKEY_L, ui::EF_NONE); |
| 240 CheckBacklightsForcedOff(true); |
| 241 |
| 242 generator_->MoveMouseBy(1, 1); |
| 243 CheckBacklightsForcedOff(true); |
| 244 |
| 245 generator_->PressTouch(); |
| 246 CheckBacklightsForcedOff(true); |
| 247 |
| 248 generator_->EnterPenPointerMode(); |
| 249 generator_->MoveMouseBy(1, 1); |
| 250 CheckBacklightsForcedOff(true); |
| 251 generator_->ExitPenPointerMode(); |
| 252 } |
| 253 |
| 254 } // namespace test |
| 255 } // namespace ash |
OLD | NEW |