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/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/wm/lock_state_controller.h" |
| 14 #include "ash/wm/power_button_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 power_button_controller_ = Shell::GetInstance()->power_button_controller(); |
| 48 test_api_ = base::MakeUnique<TabletPowerButtonController::TestApi>( |
| 49 power_button_controller_->tablet_controller_.get()); |
| 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 power_button_controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); |
| 63 } |
| 64 |
| 65 void ReleasePowerButton() { |
| 66 power_button_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 PowerButtonController* power_button_controller_; // Not owned. |
| 98 LockStateController* lock_state_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 |
| 134 // |tablet_pre_start_shutdown_animation_timer_| is timeout. |
| 135 TEST_F(TabletPowerButtonControllerTest, QuickTapTest) { |
| 136 PressPowerButton(); |
| 137 EXPECT_TRUE(test_api_->TabletPowerButtonTimerIsRunning()); |
| 138 CheckBacklightsForcedOff(false); |
| 139 ReleasePowerButton(); |
| 140 EXPECT_FALSE(test_api_->TabletPowerButtonTimerIsRunning()); |
| 141 CheckBacklightsForcedOff(true); |
| 142 |
| 143 PressPowerButton(); |
| 144 EXPECT_TRUE(test_api_->TabletPowerButtonTimerIsRunning()); |
| 145 CheckBacklightsForcedOff(false); |
| 146 ReleasePowerButton(); |
| 147 EXPECT_FALSE(test_api_->TabletPowerButtonTimerIsRunning()); |
| 148 CheckBacklightsForcedOff(false); |
| 149 } |
| 150 |
| 151 // This test is to test the power button released is done after |
| 152 // |tablet_pre_start_shutdown_animation_timer_| is timeout. |
| 153 TEST_F(TabletPowerButtonControllerTest, |
| 154 ReleasePowerButtonDuringShutdownAnimation) { |
| 155 PressPowerButton(); |
| 156 test_api_->TriggerTabletPowerButtonTimeout(); |
| 157 ReleasePowerButton(); |
| 158 CheckBacklightsForcedOff(false); |
| 159 |
| 160 // Set backlights forced off to true for the next test. |
| 161 PressPowerButton(); |
| 162 ReleasePowerButton(); |
| 163 CheckBacklightsForcedOff(true); |
| 164 |
| 165 PressPowerButton(); |
| 166 test_api_->TriggerTabletPowerButtonTimeout(); |
| 167 ReleasePowerButton(); |
| 168 CheckBacklightsForcedOff(false); |
| 169 } |
| 170 |
| 171 // When screen is turned off, even it is not set to forced off by power button |
| 172 // (can be caused by idle screen off, suspended), we should consider it as |
| 173 // "backlights forced off" to set backlights forced off state when tapping power |
| 174 // button. |
| 175 TEST_F(TabletPowerButtonControllerTest, TappingPowerButtonWhenScreenIsOff) { |
| 176 power_button_controller_->OnScreenBrightnessChanged(0.0); |
| 177 PressPowerButton(); |
| 178 CheckBacklightsForcedOff(false); |
| 179 ReleasePowerButton(); |
| 180 |
| 181 CheckBacklightsForcedOff(false); |
| 182 power_button_controller_->OnScreenBrightnessChanged(10.0); |
| 183 PressPowerButton(); |
| 184 ReleasePowerButton(); |
| 185 CheckBacklightsForcedOff(true); |
| 186 } |
| 187 |
| 188 // For convertible device working on laptop mode, keyboard/mouse event |
| 189 // should SetBacklightsForcedOff(false) when screen is off. |
| 190 TEST_F(TabletPowerButtonControllerTest, ConvertibleOnLaptopMode) { |
| 191 EnableMaximizeMode(false); |
| 192 |
| 193 PressPowerButton(); |
| 194 ReleasePowerButton(); |
| 195 CheckBacklightsForcedOff(true); |
| 196 generator_->PressKey(ui::VKEY_L, ui::EF_NONE); |
| 197 CheckBacklightsForcedOff(false); |
| 198 |
| 199 PressPowerButton(); |
| 200 ReleasePowerButton(); |
| 201 CheckBacklightsForcedOff(true); |
| 202 generator_->MoveMouseBy(1, 1); |
| 203 CheckBacklightsForcedOff(false); |
| 204 } |
| 205 |
| 206 // For convertible device working on tablet mode, keyboard/mouse event should |
| 207 // not SetBacklightsForcedOff(false) when screen is off. |
| 208 TEST_F(TabletPowerButtonControllerTest, ConvertibleOnMaximizeMode) { |
| 209 EnableMaximizeMode(true); |
| 210 |
| 211 PressPowerButton(); |
| 212 ReleasePowerButton(); |
| 213 CheckBacklightsForcedOff(true); |
| 214 generator_->PressKey(ui::VKEY_L, ui::EF_NONE); |
| 215 CheckBacklightsForcedOff(true); |
| 216 |
| 217 generator_->MoveMouseBy(1, 1); |
| 218 CheckBacklightsForcedOff(true); |
| 219 } |
| 220 |
| 221 } // namespace test |
| 222 } // namespace ash |
OLD | NEW |