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

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: 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 "base/command_line.h"
18 #include "base/test/simple_test_tick_clock.h"
19 #include "chromeos/dbus/dbus_thread_manager.h"
20 #include "chromeos/dbus/fake_power_manager_client.h"
21 #include "ui/events/test/event_generator.h"
22
23 namespace ash {
24 namespace test {
25
26 namespace {
27
28 // A non-zero brightness used for test.
29 constexpr int kNonZeroBrightnessForTest = 10;
Daniel Erat 2016/11/11 05:40:50 nit: drop the "ForTest" here?
Qiang(Joe) Xu 2016/11/11 18:19:30 Done.
30
31 void CheckBoolEquals(bool result, bool expected) {
32 EXPECT_EQ(result, expected);
33 }
34
35 } // namespace
36
37 class TabletPowerButtonControllerTest : public AshTestBase {
38 public:
39 TabletPowerButtonControllerTest() {}
40 ~TabletPowerButtonControllerTest() override {}
41
42 void SetUp() override {
43 // This also initializes DBusThreadManager.
44 std::unique_ptr<chromeos::DBusThreadManagerSetter> dbus_setter =
45 chromeos::DBusThreadManager::GetSetterForTesting();
46 power_manager_client_ = new chromeos::FakePowerManagerClient();
47 dbus_setter->SetPowerManagerClient(base::WrapUnique(power_manager_client_));
48 base::CommandLine::ForCurrentProcess()->AppendSwitch(
49 switches::kAshEnableTouchViewTesting);
50 AshTestBase::SetUp();
51
52 lock_state_controller_ = Shell::GetInstance()->lock_state_controller();
53 tablet_controller_ = Shell::GetInstance()->tablet_power_button_controller();
54 test_api_ = base::MakeUnique<TabletPowerButtonController::TestApi>(
55 tablet_controller_);
56 generator_ = &AshTestBase::GetEventGenerator();
57 power_manager_client_->SendBrightnessChanged(kNonZeroBrightnessForTest,
58 false);
59 CheckBacklightsForcedOff(false);
60 }
61
62 void TearDown() override {
63 generator_ = nullptr;
64 AshTestBase::TearDown();
65 chromeos::DBusThreadManager::Shutdown();
66 }
67
68 protected:
69 void PressPowerButton() {
70 tablet_controller_->OnPowerButtonEvent(true, base::TimeTicks::Now());
71 }
72
73 void ReleasePowerButton() {
74 tablet_controller_->OnPowerButtonEvent(false, base::TimeTicks::Now());
75 }
76
77 void SystemUnlocks() {
78 lock_state_controller_->OnLockStateChanged(false);
79 WmShell::Get()->GetSessionStateDelegate()->UnlockScreen();
80 }
81
82 void Initialize(LoginStatus status) {
83 lock_state_controller_->OnLoginStateChanged(status);
84 SetUserLoggedIn(status != LoginStatus::NOT_LOGGED_IN);
85 lock_state_controller_->OnLockStateChanged(false);
86 }
87
88 void EnableMaximizeMode(bool enabled) {
89 WmShell::Get()->maximize_mode_controller()->EnableMaximizeModeWindowManager(
90 enabled);
91 }
92
93 void CheckLockedState(bool expected_locked) {
94 EXPECT_EQ(WmShell::Get()->GetSessionStateDelegate()->IsScreenLocked(),
95 expected_locked);
96 }
97
98 void CheckBacklightsForcedOff(bool expected_forced_off) {
99 power_manager_client_->GetBacklightsForcedOff(
100 base::Bind(&CheckBoolEquals, expected_forced_off));
Daniel Erat 2016/11/11 05:40:51 does this callback ever run? i don't see the messa
Qiang(Joe) Xu 2016/11/11 18:19:31 Done.
101 }
102
103 // Ownership is passed on to chromeos::DBusThreadManager.
104 chromeos::FakePowerManagerClient* power_manager_client_;
Daniel Erat 2016/11/11 05:40:50 nit: add a blank line after this line so it's clea
Qiang(Joe) Xu 2016/11/11 18:19:30 Done.
105 LockStateController* lock_state_controller_; // Not owned.
106 TabletPowerButtonController* tablet_controller_; // Not owned.
107 std::unique_ptr<TabletPowerButtonController::TestApi> test_api_;
108 ui::test::EventGenerator* generator_ = nullptr;
109
110 DISALLOW_COPY_AND_ASSIGN(TabletPowerButtonControllerTest);
111 };
112
113 TEST_F(TabletPowerButtonControllerTest, LockScreenIfRequired) {
114 Initialize(LoginStatus::USER);
115 SetShouldLockScreenAutomatically(true);
116 CheckLockedState(false);
117
118 // On User logged in status, power-button-press-release should lock screen if
119 // should lock screen automatically.
120 PressPowerButton();
121 ReleasePowerButton();
122 CheckLockedState(true);
123
124 // On locked state, power-button-press-release should do nothing.
125 PressPowerButton();
126 ReleasePowerButton();
127 CheckLockedState(true);
128
129 // Unlock the sceen.
130 SystemUnlocks();
131 CheckLockedState(false);
132
133 // power-button-press-release should not lock the screen if should not lock
134 // screen automatically.
135 SetShouldLockScreenAutomatically(false);
136 PressPowerButton();
137 ReleasePowerButton();
138 CheckLockedState(false);
139 }
140
141 // This test is to test the power button released is done before shutdown
142 // is timeout.
Daniel Erat 2016/11/11 05:40:50 nit: "Tests that shutdown is cancelled if the powe
Qiang(Joe) Xu 2016/11/11 18:19:31 I changed to "Tests that shutdown animation is not
143 TEST_F(TabletPowerButtonControllerTest,
144 ReleasePowerButtonBeforeStartingShutdownTimer) {
Daniel Erat 2016/11/11 05:40:51 this should be "BeforeStartingShutdownAnimation",
Qiang(Joe) Xu 2016/11/11 18:19:30 yes, it should be "BeforeStartingShutdownAnimation
145 PressPowerButton();
146 EXPECT_TRUE(test_api_->ShutdownTimerIsRunning());
147 CheckBacklightsForcedOff(false);
148 ReleasePowerButton();
149 power_manager_client_->SendBrightnessChanged(0, true);
150 EXPECT_FALSE(test_api_->ShutdownTimerIsRunning());
151 CheckBacklightsForcedOff(true);
152
153 PressPowerButton();
154 power_manager_client_->SendBrightnessChanged(kNonZeroBrightnessForTest, true);
155 EXPECT_TRUE(test_api_->ShutdownTimerIsRunning());
156 CheckBacklightsForcedOff(false);
157 ReleasePowerButton();
158 EXPECT_FALSE(test_api_->ShutdownTimerIsRunning());
159 CheckBacklightsForcedOff(false);
160 }
161
162 // This test is to test the power button released is done after shutdown timer
163 // is timeout, where it will start shutdown animation timer.
164 TEST_F(TabletPowerButtonControllerTest,
165 ReleasePowerButtonDuringShutdownAnimation) {
166 // Initializes LockStateControllerTestApi to observe shutdown animation timer.
167 std::unique_ptr<LockStateControllerTestApi> lock_state_test_api =
168 base::MakeUnique<LockStateControllerTestApi>(lock_state_controller_);
169
170 PressPowerButton();
171 test_api_->TriggerShutdownTimeout();
172 ReleasePowerButton();
173 CheckBacklightsForcedOff(false);
174
175 // Set backlights forced off to true for the next test.
176 PressPowerButton();
Daniel Erat 2016/11/11 05:40:50 i don't understand what this part of the test is d
Qiang(Joe) Xu 2016/11/11 18:19:30 I missed two lines in the earlier tests. I think
177 ReleasePowerButton();
178 power_manager_client_->SendBrightnessChanged(0, true);
179 CheckBacklightsForcedOff(true);
180
181 PressPowerButton();
182 power_manager_client_->SendBrightnessChanged(kNonZeroBrightnessForTest, true);
183 CheckBacklightsForcedOff(false);
184 test_api_->TriggerShutdownTimeout();
185 EXPECT_TRUE(lock_state_test_api->shutdown_timer_is_running());
186 ReleasePowerButton();
187 EXPECT_FALSE(lock_state_test_api->shutdown_timer_is_running());
188 CheckBacklightsForcedOff(false);
189 }
190
191 // Test tapping power button when screen is idle off.
192 TEST_F(TabletPowerButtonControllerTest, TappingPowerButtonWhenScreenIsIdleOff) {
193 power_manager_client_->SendBrightnessChanged(0, false);
194 PressPowerButton();
195 power_manager_client_->SendBrightnessChanged(kNonZeroBrightnessForTest, true);
196 CheckBacklightsForcedOff(false);
197 ReleasePowerButton();
Daniel Erat 2016/11/11 05:40:50 you should also check that the backlights are stil
Qiang(Joe) Xu 2016/11/11 18:19:31 Done.
198 }
199
200 // Test tapping power button when device is suspended.
201 TEST_F(TabletPowerButtonControllerTest, TappingPowerButtonWhenSuspended) {
202 base::SimpleTestTickClock* tick_clock = new base::SimpleTestTickClock;
203 // |tick_clock| owned by |tablet_controller_|.
204 tablet_controller_->SetTickClockForTesting(
205 std::unique_ptr<base::TickClock>(tick_clock));
206
207 power_manager_client_->SendSuspendImminent();
208 power_manager_client_->SendBrightnessChanged(0, false);
209 // There is a power button pressed here, but PowerButtonEvent is sent later.
210 power_manager_client_->SendSuspendDone();
211 power_manager_client_->SendBrightnessChanged(kNonZeroBrightnessForTest,
212 false);
213
214 // Test PowerButtonEvent is sent with a delay.
215 tick_clock->Advance(base::TimeDelta::FromMilliseconds(500));
216 power_manager_client_->SendPowerButtonEvent(true, tick_clock->NowTicks());
217 power_manager_client_->SendPowerButtonEvent(false, tick_clock->NowTicks());
218 CheckBacklightsForcedOff(false);
219 }
220
221 // For convertible device working on laptop mode, test keyboard/mouse event
222 // when screen is off.
223 TEST_F(TabletPowerButtonControllerTest, ConvertibleOnLaptopMode) {
224 EnableMaximizeMode(false);
225
226 // KeyEvent should SetBacklightsForcedOff(false).
227 PressPowerButton();
228 ReleasePowerButton();
229 power_manager_client_->SendBrightnessChanged(0, true);
230 CheckBacklightsForcedOff(true);
231 generator_->PressKey(ui::VKEY_L, ui::EF_NONE);
232 power_manager_client_->SendBrightnessChanged(kNonZeroBrightnessForTest, true);
233 CheckBacklightsForcedOff(false);
234
235 // Regular mouse event should SetBacklightsForcedOff(false).
236 PressPowerButton();
237 ReleasePowerButton();
238 power_manager_client_->SendBrightnessChanged(0, true);
239 CheckBacklightsForcedOff(true);
240 generator_->MoveMouseBy(1, 1);
241 power_manager_client_->SendBrightnessChanged(kNonZeroBrightnessForTest, true);
242 CheckBacklightsForcedOff(false);
243
244 // Touchpad mouse event should SetBacklightsForcedOff(false).
245 PressPowerButton();
246 ReleasePowerButton();
247 power_manager_client_->SendBrightnessChanged(0, true);
248 CheckBacklightsForcedOff(true);
249 generator_->PressTouch();
250 power_manager_client_->SendBrightnessChanged(kNonZeroBrightnessForTest, true);
251 CheckBacklightsForcedOff(false);
252
253 // Stylus mouse event should not SetBacklightsForcedOff(false).
254 PressPowerButton();
255 ReleasePowerButton();
256 power_manager_client_->SendBrightnessChanged(0, true);
257 CheckBacklightsForcedOff(true);
258 generator_->EnterPenPointerMode();
259 generator_->MoveMouseBy(1, 1);
260 CheckBacklightsForcedOff(true);
261 generator_->ExitPenPointerMode();
262 }
263
264 // For convertible device working on tablet mode, keyboard/mouse event should
265 // not SetBacklightsForcedOff(false) when screen is off.
266 TEST_F(TabletPowerButtonControllerTest, ConvertibleOnMaximizeMode) {
267 EnableMaximizeMode(true);
268
269 PressPowerButton();
270 ReleasePowerButton();
271 power_manager_client_->SendBrightnessChanged(0, true);
272 CheckBacklightsForcedOff(true);
273 generator_->PressKey(ui::VKEY_L, ui::EF_NONE);
274 CheckBacklightsForcedOff(true);
275
276 generator_->MoveMouseBy(1, 1);
277 CheckBacklightsForcedOff(true);
278
279 generator_->PressTouch();
280 CheckBacklightsForcedOff(true);
281
282 generator_->EnterPenPointerMode();
283 generator_->MoveMouseBy(1, 1);
284 CheckBacklightsForcedOff(true);
285 generator_->ExitPenPointerMode();
286 }
287
288 } // namespace test
289 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698