Chromium Code Reviews| 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/test/lock_state_controller_test_api.h" | |
| 14 #include "ash/wm/lock_state_controller.h" | |
| 15 #include "ash/wm/power_button_controller.h" | |
| 16 #include "base/command_line.h" | |
| 17 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 18 #include "chromeos/dbus/fake_power_manager_client.h" | |
| 19 #include "ui/events/test/event_generator.h" | |
| 20 | |
| 21 namespace ash { | |
| 22 namespace test { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 void CheckBoolEquals(bool result, bool expected) { | |
| 27 EXPECT_EQ(result, expected); | |
| 28 } | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 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<TabletPowerButtonController::TestApi>( | |
| 50 power_button_controller_->tablet_controller_.get()); | |
| 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()); | |
|
Daniel Erat
2016/11/07 20:54:42
i'd avoid using PowerButtonController at all in th
Qiang(Joe) Xu
2016/11/10 22:18:45
Done.
| |
| 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<TabletPowerButtonController::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 tablet power | |
| 135 // button 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 tablet power | |
| 153 // button timer 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_->TriggerTabletPowerButtonTimeout(); | |
| 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_->TriggerTabletPowerButtonTimeout(); | |
| 172 EXPECT_TRUE(lock_state_test_api->shutdown_timer_is_running()); | |
| 173 ReleasePowerButton(); | |
| 174 CheckBacklightsForcedOff(false); | |
| 175 } | |
| 176 | |
| 177 // When screen is turned off, even it is not set to forced off by power button | |
| 178 // (can be caused by idle screen off, suspended), we should consider it as | |
| 179 // "backlights forced off" to set backlights forced off state when tapping power | |
| 180 // button. | |
| 181 TEST_F(TabletPowerButtonControllerTest, TappingPowerButtonWhenScreenIsOff) { | |
| 182 power_button_controller_->OnScreenBrightnessChanged(0.0); | |
| 183 PressPowerButton(); | |
| 184 CheckBacklightsForcedOff(false); | |
| 185 ReleasePowerButton(); | |
| 186 | |
| 187 CheckBacklightsForcedOff(false); | |
| 188 power_button_controller_->OnScreenBrightnessChanged(10.0); | |
| 189 PressPowerButton(); | |
| 190 ReleasePowerButton(); | |
| 191 CheckBacklightsForcedOff(true); | |
| 192 } | |
| 193 | |
| 194 // For convertible device working on laptop mode, keyboard/mouse event | |
| 195 // should SetBacklightsForcedOff(false) when screen is off. | |
| 196 TEST_F(TabletPowerButtonControllerTest, ConvertibleOnLaptopMode) { | |
| 197 EnableMaximizeMode(false); | |
| 198 | |
| 199 PressPowerButton(); | |
| 200 ReleasePowerButton(); | |
| 201 CheckBacklightsForcedOff(true); | |
| 202 generator_->PressKey(ui::VKEY_L, ui::EF_NONE); | |
| 203 CheckBacklightsForcedOff(false); | |
| 204 | |
| 205 PressPowerButton(); | |
| 206 ReleasePowerButton(); | |
| 207 CheckBacklightsForcedOff(true); | |
| 208 generator_->MoveMouseBy(1, 1); | |
| 209 CheckBacklightsForcedOff(false); | |
| 210 } | |
| 211 | |
| 212 // For convertible device working on tablet mode, keyboard/mouse event should | |
| 213 // not SetBacklightsForcedOff(false) when screen is off. | |
| 214 TEST_F(TabletPowerButtonControllerTest, ConvertibleOnMaximizeMode) { | |
| 215 EnableMaximizeMode(true); | |
| 216 | |
| 217 PressPowerButton(); | |
| 218 ReleasePowerButton(); | |
| 219 CheckBacklightsForcedOff(true); | |
| 220 generator_->PressKey(ui::VKEY_L, ui::EF_NONE); | |
| 221 CheckBacklightsForcedOff(true); | |
| 222 | |
| 223 generator_->MoveMouseBy(1, 1); | |
| 224 CheckBacklightsForcedOff(true); | |
| 225 } | |
| 226 | |
| 227 } // namespace test | |
| 228 } // namespace ash | |
| OLD | NEW |