Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: ash/system/chromeos/power/tablet_power_button_controller_unittest.cc

Issue 2474913004: Tablet-like power button behavior on Convertible/Tablet ChromeOS devices (Closed)
Patch Set: cr based on comments Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 SystemUnlocks() {
Daniel Erat 2016/11/14 16:15:31 nit: UnlockScreen?
Qiang(Joe) Xu 2016/11/14 18:47:56 Done.
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 void CheckLockedState(bool expected_locked) {
Daniel Erat 2016/11/14 16:15:31 how about: bool GetLockedState() so that you c
Qiang(Joe) Xu 2016/11/14 18:47:57 Done.
98 EXPECT_EQ(WmShell::Get()->GetSessionStateDelegate()->IsScreenLocked(),
99 expected_locked);
100 }
101
102 bool GetBacklightsForcedOff() WARN_UNUSED_RESULT {
103 bool forced_off = false;
104 power_manager_client_->GetBacklightsForcedOff(
105 base::Bind(&CopyResult, base::Unretained(&forced_off)));
106 base::RunLoop().RunUntilIdle();
107 return forced_off;
108 }
109
110 // Ownership is passed on to chromeos::DBusThreadManager.
111 chromeos::FakePowerManagerClient* power_manager_client_;
112
113 LockStateController* lock_state_controller_; // Not owned.
114 TabletPowerButtonController* tablet_controller_; // Not owned.
115 std::unique_ptr<TabletPowerButtonController::TestApi> test_api_;
116 ui::test::EventGenerator* generator_ = nullptr;
117
118 DISALLOW_COPY_AND_ASSIGN(TabletPowerButtonControllerTest);
119 };
120
121 TEST_F(TabletPowerButtonControllerTest, LockScreenIfRequired) {
122 Initialize(LoginStatus::USER);
123 SetShouldLockScreenAutomatically(true);
124 CheckLockedState(false);
125
126 // On User logged in status, power-button-press-release should lock screen if
127 // should lock screen automatically.
Daniel Erat 2016/11/14 16:15:31 nit: "... if automatic screen-locking was requeste
Qiang(Joe) Xu 2016/11/14 18:47:57 Done.
128 PressPowerButton();
129 ReleasePowerButton();
130 CheckLockedState(true);
131
132 // On locked state, power-button-press-release should do nothing.
133 PressPowerButton();
134 ReleasePowerButton();
135 CheckLockedState(true);
136
137 // Unlock the sceen.
138 SystemUnlocks();
139 CheckLockedState(false);
Daniel Erat 2016/11/14 16:15:31 if you change to GetLockedState as requested above
Qiang(Joe) Xu 2016/11/14 18:47:57 Done.
140
141 // power-button-press-release should not lock the screen if should not lock
142 // screen automatically.
Daniel Erat 2016/11/14 16:15:31 nit: "... if automatic screen-locking wasn't reque
Qiang(Joe) Xu 2016/11/14 18:47:56 Done.
143 SetShouldLockScreenAutomatically(false);
144 PressPowerButton();
145 ReleasePowerButton();
146 CheckLockedState(false);
147 }
148
149 // Tests that shutdown animation is not started if the power button is released
150 // quickly.
151 TEST_F(TabletPowerButtonControllerTest,
152 ReleasePowerButtonBeforeStartingShutdownAnimation) {
153 PressPowerButton();
154 EXPECT_TRUE(test_api_->ShutdownTimerIsRunning());
155 EXPECT_FALSE(GetBacklightsForcedOff());
156 ReleasePowerButton();
157 power_manager_client_->SendBrightnessChanged(0, true);
158 EXPECT_FALSE(test_api_->ShutdownTimerIsRunning());
159 EXPECT_TRUE(GetBacklightsForcedOff());
160
161 PressPowerButton();
162 power_manager_client_->SendBrightnessChanged(kNonZeroBrightness, true);
163 EXPECT_TRUE(test_api_->ShutdownTimerIsRunning());
164 EXPECT_FALSE(GetBacklightsForcedOff());
165 ReleasePowerButton();
166 EXPECT_FALSE(test_api_->ShutdownTimerIsRunning());
167 EXPECT_FALSE(GetBacklightsForcedOff());
168 }
169
170 // Tests the power button released is done after shutdown timer is timeout,
Daniel Erat 2016/11/14 16:15:31 // Tests that the shutdown animation is started wh
Qiang(Joe) Xu 2016/11/14 18:47:57 Done.
171 // where it will start shutdown animation timer.
172 TEST_F(TabletPowerButtonControllerTest,
173 ReleasePowerButtonDuringShutdownAnimation) {
174 // Initializes LockStateControllerTestApi to observe shutdown animation timer.
175 std::unique_ptr<LockStateControllerTestApi> lock_state_test_api =
176 base::MakeUnique<LockStateControllerTestApi>(lock_state_controller_);
177
178 PressPowerButton();
179 test_api_->TriggerShutdownTimeout();
180 EXPECT_TRUE(lock_state_test_api->shutdown_timer_is_running());
181 ReleasePowerButton();
182 EXPECT_FALSE(lock_state_test_api->shutdown_timer_is_running());
183 EXPECT_FALSE(GetBacklightsForcedOff());
184
185 // Test again when backlights is forced off.
186 PressPowerButton();
187 ReleasePowerButton();
188 power_manager_client_->SendBrightnessChanged(0, true);
189 EXPECT_TRUE(GetBacklightsForcedOff());
190
191 PressPowerButton();
192 power_manager_client_->SendBrightnessChanged(kNonZeroBrightness, true);
193 EXPECT_FALSE(GetBacklightsForcedOff());
194 test_api_->TriggerShutdownTimeout();
195 EXPECT_TRUE(lock_state_test_api->shutdown_timer_is_running());
196 ReleasePowerButton();
197 EXPECT_FALSE(lock_state_test_api->shutdown_timer_is_running());
198 EXPECT_FALSE(GetBacklightsForcedOff());
199 }
200
201 // Tests tapping power button when screen is idle off.
202 TEST_F(TabletPowerButtonControllerTest, TappingPowerButtonWhenScreenIsIdleOff) {
203 power_manager_client_->SendBrightnessChanged(0, false);
204 PressPowerButton();
205 power_manager_client_->SendBrightnessChanged(kNonZeroBrightness, true);
206 EXPECT_FALSE(GetBacklightsForcedOff());
Daniel Erat 2016/11/14 16:15:31 nit: check this before sending the non-zero bright
Qiang(Joe) Xu 2016/11/14 18:47:57 Done.
207 ReleasePowerButton();
208 EXPECT_FALSE(GetBacklightsForcedOff());
209 }
210
211 // Tests tapping power button when device is suspended.
212 TEST_F(TabletPowerButtonControllerTest, TappingPowerButtonWhenSuspended) {
213 base::SimpleTestTickClock* tick_clock = new base::SimpleTestTickClock;
214 // |tick_clock| owned by |tablet_controller_|.
215 tablet_controller_->SetTickClockForTesting(
216 std::unique_ptr<base::TickClock>(tick_clock));
217
218 power_manager_client_->SendSuspendImminent();
219 power_manager_client_->SendBrightnessChanged(0, false);
220 // There is a power button pressed here, but PowerButtonEvent is sent later.
221 power_manager_client_->SendSuspendDone();
222 power_manager_client_->SendBrightnessChanged(kNonZeroBrightness, false);
223
224 // Test PowerButtonEvent is sent with a delay.
Daniel Erat 2016/11/14 16:15:31 nit: // Send the power button event after a short
Qiang(Joe) Xu 2016/11/14 18:47:56 Done.
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());
Daniel Erat 2016/11/14 16:15:31 please send another event after another 500-600 ms
Qiang(Joe) Xu 2016/11/14 18:47:57 Since the time duration to wait is 2s, I think the
Daniel Erat 2016/11/14 19:22:50 whoops. yes, sorry -- got the delays confused
229 }
230
231 // For convertible device working on laptop mode, tests keyboard/mouse event
232 // when screen is off.
233 TEST_F(TabletPowerButtonControllerTest, ConvertibleOnLaptopMode) {
234 EnableMaximizeMode(false);
235
236 // KeyEvent should SetBacklightsForcedOff(false).
237 PressPowerButton();
238 ReleasePowerButton();
239 power_manager_client_->SendBrightnessChanged(0, true);
240 EXPECT_TRUE(GetBacklightsForcedOff());
241 generator_->PressKey(ui::VKEY_L, ui::EF_NONE);
242 power_manager_client_->SendBrightnessChanged(kNonZeroBrightness, true);
243 EXPECT_FALSE(GetBacklightsForcedOff());
244
245 // Regular mouse event should SetBacklightsForcedOff(false).
246 PressPowerButton();
247 ReleasePowerButton();
248 power_manager_client_->SendBrightnessChanged(0, true);
249 EXPECT_TRUE(GetBacklightsForcedOff());
250 generator_->MoveMouseBy(1, 1);
251 power_manager_client_->SendBrightnessChanged(kNonZeroBrightness, true);
252 EXPECT_FALSE(GetBacklightsForcedOff());
253
254 // Touchpad mouse event should SetBacklightsForcedOff(false).
255 PressPowerButton();
256 ReleasePowerButton();
257 power_manager_client_->SendBrightnessChanged(0, true);
258 EXPECT_TRUE(GetBacklightsForcedOff());
259 generator_->PressTouch();
260 power_manager_client_->SendBrightnessChanged(kNonZeroBrightness, true);
261 EXPECT_FALSE(GetBacklightsForcedOff());
262
263 // Stylus mouse event should not SetBacklightsForcedOff(false).
264 PressPowerButton();
265 ReleasePowerButton();
266 power_manager_client_->SendBrightnessChanged(0, true);
267 EXPECT_TRUE(GetBacklightsForcedOff());
268 generator_->EnterPenPointerMode();
269 generator_->MoveMouseBy(1, 1);
270 EXPECT_TRUE(GetBacklightsForcedOff());
271 generator_->ExitPenPointerMode();
272 }
273
274 // For convertible device working on tablet mode, keyboard/mouse event should
275 // not SetBacklightsForcedOff(false) when screen is off.
276 TEST_F(TabletPowerButtonControllerTest, ConvertibleOnMaximizeMode) {
277 EnableMaximizeMode(true);
278
279 PressPowerButton();
280 ReleasePowerButton();
281 power_manager_client_->SendBrightnessChanged(0, true);
282 EXPECT_TRUE(GetBacklightsForcedOff());
283 generator_->PressKey(ui::VKEY_L, ui::EF_NONE);
284 EXPECT_TRUE(GetBacklightsForcedOff());
285
286 generator_->MoveMouseBy(1, 1);
287 EXPECT_TRUE(GetBacklightsForcedOff());
288
289 generator_->PressTouch();
290 EXPECT_TRUE(GetBacklightsForcedOff());
291
292 generator_->EnterPenPointerMode();
293 generator_->MoveMouseBy(1, 1);
294 EXPECT_TRUE(GetBacklightsForcedOff());
295 generator_->ExitPenPointerMode();
296 }
297
298 } // namespace test
299 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698