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 <memory> | |
8 | |
9 #include "ash/common/ash_switches.h" | |
10 #include "ash/common/session/session_state_delegate.h" | |
11 #include "ash/common/wm/maximize_mode/maximize_mode_controller.h" | |
12 #include "ash/common/wm_shell.h" | |
13 #include "ash/shell.h" | |
14 #include "ash/test/ash_test_base.h" | |
15 #include "ash/test/lock_state_controller_test_api.h" | |
16 #include "ash/wm/lock_state_controller.h" | |
17 #include "ash/wm/power_button_controller.h" | |
18 #include "base/command_line.h" | |
19 #include "base/compiler_specific.h" | |
20 #include "base/run_loop.h" | |
21 #include "base/test/simple_test_tick_clock.h" | |
22 #include "chromeos/dbus/dbus_thread_manager.h" | |
23 #include "chromeos/dbus/fake_power_manager_client.h" | |
24 #include "ui/events/test/event_generator.h" | |
25 | |
26 namespace ash { | |
27 namespace test { | |
28 | |
29 namespace { | |
30 | |
31 // A non-zero brightness used for test. | |
32 constexpr int kNonZeroBrightness = 10; | |
33 | |
34 void CopyResult(bool* dest, bool src) { | |
35 *dest = src; | |
36 } | |
37 | |
38 } // namespace | |
39 | |
40 class TabletPowerButtonControllerTest : public AshTestBase { | |
41 public: | |
42 TabletPowerButtonControllerTest() {} | |
43 ~TabletPowerButtonControllerTest() override {} | |
44 | |
45 void SetUp() override { | |
46 // This also initializes DBusThreadManager. | |
47 std::unique_ptr<chromeos::DBusThreadManagerSetter> dbus_setter = | |
48 chromeos::DBusThreadManager::GetSetterForTesting(); | |
49 power_manager_client_ = new chromeos::FakePowerManagerClient(); | |
50 dbus_setter->SetPowerManagerClient(base::WrapUnique(power_manager_client_)); | |
51 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
52 switches::kAshEnableTouchViewTesting); | |
53 AshTestBase::SetUp(); | |
54 | |
55 lock_state_controller_ = Shell::GetInstance()->lock_state_controller(); | |
56 tablet_controller_ = Shell::GetInstance() | |
57 ->power_button_controller() | |
58 ->tablet_power_button_controller_for_test(); | |
59 test_api_ = base::MakeUnique<TabletPowerButtonController::TestApi>( | |
60 tablet_controller_); | |
61 generator_ = &AshTestBase::GetEventGenerator(); | |
62 power_manager_client_->SendBrightnessChanged(kNonZeroBrightness, false); | |
63 EXPECT_FALSE(GetBacklightsForcedOff()); | |
64 } | |
65 | |
66 void TearDown() override { | |
67 generator_ = nullptr; | |
68 AshTestBase::TearDown(); | |
69 chromeos::DBusThreadManager::Shutdown(); | |
70 } | |
71 | |
72 protected: | |
73 void PressPowerButton() { | |
74 tablet_controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); | |
75 } | |
76 | |
77 void ReleasePowerButton() { | |
78 tablet_controller_->OnPowerButtonEvent(false, base::TimeTicks::Now()); | |
79 } | |
80 | |
81 void UnlockScreen() { | |
82 lock_state_controller_->OnLockStateChanged(false); | |
83 WmShell::Get()->GetSessionStateDelegate()->UnlockScreen(); | |
84 } | |
85 | |
86 void Initialize(LoginStatus status) { | |
87 lock_state_controller_->OnLoginStateChanged(status); | |
88 SetUserLoggedIn(status != LoginStatus::NOT_LOGGED_IN); | |
89 lock_state_controller_->OnLockStateChanged(false); | |
90 } | |
91 | |
92 void EnableMaximizeMode(bool enabled) { | |
93 WmShell::Get()->maximize_mode_controller()->EnableMaximizeModeWindowManager( | |
94 enabled); | |
95 } | |
96 | |
97 bool GetLockedState() { | |
98 return WmShell::Get()->GetSessionStateDelegate()->IsScreenLocked(); | |
99 } | |
100 | |
101 bool GetBacklightsForcedOff() WARN_UNUSED_RESULT { | |
102 bool forced_off = false; | |
103 power_manager_client_->GetBacklightsForcedOff( | |
104 base::Bind(&CopyResult, base::Unretained(&forced_off))); | |
105 base::RunLoop().RunUntilIdle(); | |
106 return forced_off; | |
107 } | |
108 | |
109 // Ownership is passed on to chromeos::DBusThreadManager. | |
110 chromeos::FakePowerManagerClient* power_manager_client_; | |
111 | |
112 LockStateController* lock_state_controller_; // Not owned. | |
113 TabletPowerButtonController* tablet_controller_; // Not owned. | |
114 std::unique_ptr<TabletPowerButtonController::TestApi> test_api_; | |
115 ui::test::EventGenerator* generator_ = nullptr; | |
116 | |
117 DISALLOW_COPY_AND_ASSIGN(TabletPowerButtonControllerTest); | |
118 }; | |
119 | |
120 TEST_F(TabletPowerButtonControllerTest, LockScreenIfRequired) { | |
121 Initialize(LoginStatus::USER); | |
122 SetShouldLockScreenAutomatically(true); | |
123 EXPECT_FALSE(GetLockedState()); | |
124 | |
125 // On User logged in status, power-button-press-release should lock screen if | |
126 // automatic screen-locking was requested. | |
127 PressPowerButton(); | |
128 ReleasePowerButton(); | |
129 EXPECT_TRUE(GetLockedState()); | |
130 | |
131 // On locked state, power-button-press-release should do nothing. | |
132 PressPowerButton(); | |
133 ReleasePowerButton(); | |
134 EXPECT_TRUE(GetLockedState()); | |
135 | |
136 // Unlock the sceen. | |
137 UnlockScreen(); | |
138 EXPECT_FALSE(GetLockedState()); | |
Daniel Erat
2016/11/14 19:22:50
nit: ASSERT_FALSE since something is seriously wro
| |
139 | |
140 // power-button-press-release should not lock the screen if automatic | |
141 // screen-locking wasn't requested. | |
142 SetShouldLockScreenAutomatically(false); | |
143 PressPowerButton(); | |
144 ReleasePowerButton(); | |
145 EXPECT_FALSE(GetLockedState()); | |
146 } | |
147 | |
148 // Tests that shutdown animation is not started if the power button is released | |
149 // quickly. | |
150 TEST_F(TabletPowerButtonControllerTest, | |
151 ReleasePowerButtonBeforeStartingShutdownAnimation) { | |
152 PressPowerButton(); | |
153 EXPECT_TRUE(test_api_->ShutdownTimerIsRunning()); | |
154 EXPECT_FALSE(GetBacklightsForcedOff()); | |
155 ReleasePowerButton(); | |
156 power_manager_client_->SendBrightnessChanged(0, true); | |
157 EXPECT_FALSE(test_api_->ShutdownTimerIsRunning()); | |
158 EXPECT_TRUE(GetBacklightsForcedOff()); | |
159 | |
160 PressPowerButton(); | |
161 power_manager_client_->SendBrightnessChanged(kNonZeroBrightness, true); | |
162 EXPECT_TRUE(test_api_->ShutdownTimerIsRunning()); | |
163 EXPECT_FALSE(GetBacklightsForcedOff()); | |
164 ReleasePowerButton(); | |
165 EXPECT_FALSE(test_api_->ShutdownTimerIsRunning()); | |
166 EXPECT_FALSE(GetBacklightsForcedOff()); | |
167 } | |
168 | |
169 // Tests that the shutdown animation is started when the power button is | |
170 // released after the timer fires. | |
171 TEST_F(TabletPowerButtonControllerTest, | |
172 ReleasePowerButtonDuringShutdownAnimation) { | |
173 // Initializes LockStateControllerTestApi to observe shutdown animation timer. | |
174 std::unique_ptr<LockStateControllerTestApi> lock_state_test_api = | |
175 base::MakeUnique<LockStateControllerTestApi>(lock_state_controller_); | |
176 | |
177 PressPowerButton(); | |
178 test_api_->TriggerShutdownTimeout(); | |
179 EXPECT_TRUE(lock_state_test_api->shutdown_timer_is_running()); | |
180 ReleasePowerButton(); | |
181 EXPECT_FALSE(lock_state_test_api->shutdown_timer_is_running()); | |
182 EXPECT_FALSE(GetBacklightsForcedOff()); | |
183 | |
184 // Test again when backlights is forced off. | |
185 PressPowerButton(); | |
186 ReleasePowerButton(); | |
187 power_manager_client_->SendBrightnessChanged(0, true); | |
188 EXPECT_TRUE(GetBacklightsForcedOff()); | |
189 | |
190 PressPowerButton(); | |
191 power_manager_client_->SendBrightnessChanged(kNonZeroBrightness, true); | |
192 EXPECT_FALSE(GetBacklightsForcedOff()); | |
193 test_api_->TriggerShutdownTimeout(); | |
194 EXPECT_TRUE(lock_state_test_api->shutdown_timer_is_running()); | |
195 ReleasePowerButton(); | |
196 EXPECT_FALSE(lock_state_test_api->shutdown_timer_is_running()); | |
197 EXPECT_FALSE(GetBacklightsForcedOff()); | |
198 } | |
199 | |
200 // Tests tapping power button when screen is idle off. | |
201 TEST_F(TabletPowerButtonControllerTest, TappingPowerButtonWhenScreenIsIdleOff) { | |
202 power_manager_client_->SendBrightnessChanged(0, false); | |
203 PressPowerButton(); | |
204 EXPECT_FALSE(GetBacklightsForcedOff()); | |
205 power_manager_client_->SendBrightnessChanged(kNonZeroBrightness, true); | |
206 ReleasePowerButton(); | |
207 EXPECT_FALSE(GetBacklightsForcedOff()); | |
208 } | |
209 | |
210 // Tests tapping power button when device is suspended. | |
211 TEST_F(TabletPowerButtonControllerTest, TappingPowerButtonWhenSuspended) { | |
212 base::SimpleTestTickClock* tick_clock = new base::SimpleTestTickClock; | |
213 // |tick_clock| owned by |tablet_controller_|. | |
214 tablet_controller_->SetTickClockForTesting( | |
215 std::unique_ptr<base::TickClock>(tick_clock)); | |
216 | |
217 power_manager_client_->SendSuspendImminent(); | |
218 power_manager_client_->SendBrightnessChanged(0, false); | |
219 // There is a power button pressed here, but PowerButtonEvent is sent later. | |
220 power_manager_client_->SendSuspendDone(); | |
221 power_manager_client_->SendBrightnessChanged(kNonZeroBrightness, false); | |
222 | |
223 // Send the power button event after a short delay and check that it is | |
224 // ignored. | |
225 tick_clock->Advance(base::TimeDelta::FromMilliseconds(500)); | |
226 power_manager_client_->SendPowerButtonEvent(true, tick_clock->NowTicks()); | |
227 power_manager_client_->SendPowerButtonEvent(false, tick_clock->NowTicks()); | |
228 EXPECT_FALSE(GetBacklightsForcedOff()); | |
229 | |
230 // Send the power button event after a longer delay and check that it is not | |
231 // ignored. | |
232 tick_clock->Advance(base::TimeDelta::FromMilliseconds(1600)); | |
233 power_manager_client_->SendPowerButtonEvent(true, tick_clock->NowTicks()); | |
234 power_manager_client_->SendPowerButtonEvent(false, tick_clock->NowTicks()); | |
235 EXPECT_TRUE(GetBacklightsForcedOff()); | |
236 } | |
237 | |
238 // For convertible device working on laptop mode, tests keyboard/mouse event | |
239 // when screen is off. | |
240 TEST_F(TabletPowerButtonControllerTest, ConvertibleOnLaptopMode) { | |
241 EnableMaximizeMode(false); | |
242 | |
243 // KeyEvent should SetBacklightsForcedOff(false). | |
244 PressPowerButton(); | |
245 ReleasePowerButton(); | |
246 power_manager_client_->SendBrightnessChanged(0, true); | |
247 EXPECT_TRUE(GetBacklightsForcedOff()); | |
248 generator_->PressKey(ui::VKEY_L, ui::EF_NONE); | |
249 power_manager_client_->SendBrightnessChanged(kNonZeroBrightness, true); | |
250 EXPECT_FALSE(GetBacklightsForcedOff()); | |
251 | |
252 // Regular mouse event should SetBacklightsForcedOff(false). | |
253 PressPowerButton(); | |
254 ReleasePowerButton(); | |
255 power_manager_client_->SendBrightnessChanged(0, true); | |
256 EXPECT_TRUE(GetBacklightsForcedOff()); | |
257 generator_->MoveMouseBy(1, 1); | |
258 power_manager_client_->SendBrightnessChanged(kNonZeroBrightness, true); | |
259 EXPECT_FALSE(GetBacklightsForcedOff()); | |
260 | |
261 // Touchpad mouse event should SetBacklightsForcedOff(false). | |
262 PressPowerButton(); | |
263 ReleasePowerButton(); | |
264 power_manager_client_->SendBrightnessChanged(0, true); | |
265 EXPECT_TRUE(GetBacklightsForcedOff()); | |
266 generator_->PressTouch(); | |
267 power_manager_client_->SendBrightnessChanged(kNonZeroBrightness, true); | |
268 EXPECT_FALSE(GetBacklightsForcedOff()); | |
269 | |
270 // Stylus mouse event should not SetBacklightsForcedOff(false). | |
271 PressPowerButton(); | |
272 ReleasePowerButton(); | |
273 power_manager_client_->SendBrightnessChanged(0, true); | |
274 EXPECT_TRUE(GetBacklightsForcedOff()); | |
275 generator_->EnterPenPointerMode(); | |
276 generator_->MoveMouseBy(1, 1); | |
277 EXPECT_TRUE(GetBacklightsForcedOff()); | |
278 generator_->ExitPenPointerMode(); | |
279 } | |
280 | |
281 // For convertible device working on tablet mode, keyboard/mouse event should | |
282 // not SetBacklightsForcedOff(false) when screen is off. | |
283 TEST_F(TabletPowerButtonControllerTest, ConvertibleOnMaximizeMode) { | |
284 EnableMaximizeMode(true); | |
285 | |
286 PressPowerButton(); | |
287 ReleasePowerButton(); | |
288 power_manager_client_->SendBrightnessChanged(0, true); | |
289 EXPECT_TRUE(GetBacklightsForcedOff()); | |
290 generator_->PressKey(ui::VKEY_L, ui::EF_NONE); | |
291 EXPECT_TRUE(GetBacklightsForcedOff()); | |
292 | |
293 generator_->MoveMouseBy(1, 1); | |
294 EXPECT_TRUE(GetBacklightsForcedOff()); | |
295 | |
296 generator_->PressTouch(); | |
297 EXPECT_TRUE(GetBacklightsForcedOff()); | |
298 | |
299 generator_->EnterPenPointerMode(); | |
300 generator_->MoveMouseBy(1, 1); | |
301 EXPECT_TRUE(GetBacklightsForcedOff()); | |
302 generator_->ExitPenPointerMode(); | |
303 } | |
304 | |
305 } // namespace test | |
306 } // namespace ash | |
OLD | NEW |