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