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

Side by Side Diff: ash/wm/maximize_mode/maximize_mode_controller_unittest.cc

Issue 412013002: Prevent entering maximize mode when the lid is closed or has recently opened. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleaned up MaximizeModeControllerTest fixture by removing static class variables. Created 6 years, 4 months 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <math.h>
6
5 #include "ash/wm/maximize_mode/maximize_mode_controller.h" 7 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
6 8
7 #include "ash/accelerometer/accelerometer_controller.h" 9 #include "ash/accelerometer/accelerometer_controller.h"
8 #include "ash/display/display_manager.h" 10 #include "ash/display/display_manager.h"
9 #include "ash/shell.h" 11 #include "ash/shell.h"
10 #include "ash/system/tray/system_tray_delegate.h" 12 #include "ash/system/tray/system_tray_delegate.h"
11 #include "ash/test/ash_test_base.h" 13 #include "ash/test/ash_test_base.h"
12 #include "ash/test/display_manager_test_api.h" 14 #include "ash/test/display_manager_test_api.h"
13 #include "ash/test/test_lock_state_controller_delegate.h" 15 #include "ash/test/test_lock_state_controller_delegate.h"
14 #include "ash/test/test_screenshot_delegate.h" 16 #include "ash/test/test_screenshot_delegate.h"
15 #include "ash/test/test_system_tray_delegate.h" 17 #include "ash/test/test_system_tray_delegate.h"
16 #include "ash/test/test_volume_control_delegate.h" 18 #include "ash/test/test_volume_control_delegate.h"
19 #include "base/test/simple_test_tick_clock.h"
17 #include "ui/events/event_handler.h" 20 #include "ui/events/event_handler.h"
18 #include "ui/events/test/event_generator.h" 21 #include "ui/events/test/event_generator.h"
19 #include "ui/gfx/vector3d_f.h" 22 #include "ui/gfx/vector3d_f.h"
20 #include "ui/message_center/message_center.h" 23 #include "ui/message_center/message_center.h"
21 24
22 #if defined(USE_X11) 25 #if defined(USE_X11)
23 #include "ui/events/test/events_test_utils_x11.h" 26 #include "ui/events/test/events_test_utils_x11.h"
24 #endif 27 #endif
25 28
26 namespace ash { 29 namespace ash {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 gfx::Display::Rotation GetInternalDisplayRotation() const { 86 gfx::Display::Rotation GetInternalDisplayRotation() const {
84 return Shell::GetInstance()->display_manager()->GetDisplayInfo( 87 return Shell::GetInstance()->display_manager()->GetDisplayInfo(
85 gfx::Display::InternalDisplayId()).rotation(); 88 gfx::Display::InternalDisplayId()).rotation();
86 } 89 }
87 90
88 void SetInternalDisplayRotation(gfx::Display::Rotation rotation) const { 91 void SetInternalDisplayRotation(gfx::Display::Rotation rotation) const {
89 Shell::GetInstance()->display_manager()-> 92 Shell::GetInstance()->display_manager()->
90 SetDisplayRotation(gfx::Display::InternalDisplayId(), rotation); 93 SetDisplayRotation(gfx::Display::InternalDisplayId(), rotation);
91 } 94 }
92 95
96 // Attaches a SimpleTestTickClock to the MaximizeModeController with a non
97 // null value initial value.
98 void AttachTickClockForTest() {
99 scoped_ptr<base::TickClock> tick_clock(
100 test_tick_clock_ = new base::SimpleTestTickClock());
101 test_tick_clock_->Advance(base::TimeDelta::FromSeconds(1));
102 maximize_mode_controller()->SetTickClockForTest(tick_clock.Pass());
103 }
104
105 void AdvanceTickClock(const base::TimeDelta& delta) {
106 DCHECK(test_tick_clock_);
107 test_tick_clock_->Advance(delta);
108 }
109
110 void OpenLidToAngle(float degrees) {
111 DCHECK(degrees >= 0.0f);
112 DCHECK(degrees <= 360.0f);
113
114 float radians = degrees * kDegreesToRadians;
115 gfx::Vector3dF base_vector(1.0f, 0.0f, 0.0f);
116 gfx::Vector3dF lid_vector(cos(radians), 0.0f, sin(radians));
117 TriggerAccelerometerUpdate(base_vector, lid_vector);
118 }
119
120 #if defined(OS_CHROMEOS)
121 void OpenLid() {
122 maximize_mode_controller()->LidEventReceived(true /* open */,
123 maximize_mode_controller()->tick_clock_->NowTicks());
124 }
125
126 void CloseLid() {
127 maximize_mode_controller()->LidEventReceived(false /* open */,
128 maximize_mode_controller()->tick_clock_->NowTicks());
129 }
130 #endif // OS_CHROMEOS
131
132 bool WasLidOpenedRecently() {
133 return maximize_mode_controller()->WasLidOpenedRecently();
134 }
135
93 private: 136 private:
137 base::SimpleTestTickClock* test_tick_clock_;
138
94 DISALLOW_COPY_AND_ASSIGN(MaximizeModeControllerTest); 139 DISALLOW_COPY_AND_ASSIGN(MaximizeModeControllerTest);
95 }; 140 };
96 141
97 // Tests that opening the lid beyond 180 will enter touchview, and that it will 142 #if defined(OS_CHROMEOS)
98 // exit when the lid comes back from 180. Also tests the thresholds, i.e. it
99 // will stick to the current mode.
100 TEST_F(MaximizeModeControllerTest, EnterExitThresholds) {
101 // For the simple test the base remains steady.
102 gfx::Vector3dF base(0.0f, 0.0f, 1.0f);
103 143
104 // Lid open 90 degrees. 144 // Verify that closing the lid will exit maximize mode.
105 TriggerAccelerometerUpdate(base, gfx::Vector3dF(-1.0f, 0.0f, 0.0f)); 145 TEST_F(MaximizeModeControllerTest, CloseLidWhileInMaximizeMode) {
146 OpenLidToAngle(315.0f);
147 ASSERT_TRUE(IsMaximizeModeStarted());
148
149 CloseLid();
150 EXPECT_FALSE(IsMaximizeModeStarted());
151 }
152
153 // Verify that maximize mode will not be entered when the lid is closed.
154 TEST_F(MaximizeModeControllerTest,
155 HingeAnglesWithLidClosed) {
156 AttachTickClockForTest();
157
158 CloseLid();
159
160 OpenLidToAngle(270.0f);
106 EXPECT_FALSE(IsMaximizeModeStarted()); 161 EXPECT_FALSE(IsMaximizeModeStarted());
107 162
108 // Open just past 180. 163 OpenLidToAngle(315.0f);
109 TriggerAccelerometerUpdate(base, gfx::Vector3dF(0.05f, 0.0f, -1.0f));
110 EXPECT_FALSE(IsMaximizeModeStarted()); 164 EXPECT_FALSE(IsMaximizeModeStarted());
111 165
112 // Open up 270 degrees. 166 OpenLidToAngle(355.0f);
113 TriggerAccelerometerUpdate(base, gfx::Vector3dF(1.0f, 0.0f, 0.0f)); 167 EXPECT_FALSE(IsMaximizeModeStarted());
168 }
169
170 // Verify the maximize mode state for unstable hinge angles when the lid was
171 // recently open.
172 TEST_F(MaximizeModeControllerTest,
173 UnstableHingeAnglesWhenLidRecentlyOpened) {
174 AttachTickClockForTest();
175
176 OpenLid();
177 ASSERT_TRUE(WasLidOpenedRecently());
178
179 OpenLidToAngle(5.0f);
180 EXPECT_FALSE(IsMaximizeModeStarted());
181
182 OpenLidToAngle(355.0f);
183 EXPECT_FALSE(IsMaximizeModeStarted());
184
185 // This is a stable reading and should clear the last lid opened time.
186 OpenLidToAngle(45.0f);
187 EXPECT_FALSE(IsMaximizeModeStarted());
188 EXPECT_FALSE(WasLidOpenedRecently());
189
190 OpenLidToAngle(355.0f);
191 EXPECT_TRUE(IsMaximizeModeStarted());
192 }
193
194 #endif // OS_CHROMEOS
195
196 // Verify the WasLidOpenedRecently signal with respect to time.
197 TEST_F(MaximizeModeControllerTest, WasLidOpenedRecentlyOverTime) {
198 #if defined(OS_CHROMEOS)
199
200 AttachTickClockForTest();
201
202 // No lid open time initially.
203 ASSERT_FALSE(WasLidOpenedRecently());
204
205 CloseLid();
206 EXPECT_FALSE(WasLidOpenedRecently());
207
208 OpenLid();
209 EXPECT_TRUE(WasLidOpenedRecently());
210
211 // 1 second after lid open.
212 AdvanceTickClock(base::TimeDelta::FromSeconds(1));
213 EXPECT_TRUE(WasLidOpenedRecently());
214
215 // 3 seconds after lid open.
216 AdvanceTickClock(base::TimeDelta::FromSeconds(2));
217 EXPECT_FALSE(WasLidOpenedRecently());
218
219 #else
220
221 EXPECT_FALSE(WasLidOpenedRecently());
222
223 #endif // OS_CHROMEOS
224 }
225
226 // Verify the maximize mode enter/exit thresholds for stable angles.
227 TEST_F(MaximizeModeControllerTest, StableHingeAnglesWithLidOpened) {
228 ASSERT_FALSE(IsMaximizeModeStarted());
229 ASSERT_FALSE(WasLidOpenedRecently());
230
231 OpenLidToAngle(180.0f);
232 EXPECT_FALSE(IsMaximizeModeStarted());
233
234 OpenLidToAngle(315.0f);
114 EXPECT_TRUE(IsMaximizeModeStarted()); 235 EXPECT_TRUE(IsMaximizeModeStarted());
115 236
116 // Open up 360 degrees and appearing to be slightly past it (i.e. as if almost 237 OpenLidToAngle(180.0f);
117 // closed).
118 TriggerAccelerometerUpdate(base, gfx::Vector3dF(-0.05f, 0.0f, 1.0f));
119 EXPECT_TRUE(IsMaximizeModeStarted()); 238 EXPECT_TRUE(IsMaximizeModeStarted());
120 239
121 // Open just before 180. 240 OpenLidToAngle(45.0f);
122 TriggerAccelerometerUpdate(base, gfx::Vector3dF(-0.05f, 0.0f, -1.0f)); 241 EXPECT_FALSE(IsMaximizeModeStarted());
242
243 OpenLidToAngle(270.0f);
123 EXPECT_TRUE(IsMaximizeModeStarted()); 244 EXPECT_TRUE(IsMaximizeModeStarted());
124 245
125 // Open 90 degrees. 246 OpenLidToAngle(90.0f);
126 TriggerAccelerometerUpdate(base, gfx::Vector3dF(-1.0f, 0.0f, 0.0f));
127 EXPECT_FALSE(IsMaximizeModeStarted()); 247 EXPECT_FALSE(IsMaximizeModeStarted());
128 } 248 }
129 249
250 // Verify the maximize mode state for unstable hinge angles when the lid is open
251 // but not recently.
252 TEST_F(MaximizeModeControllerTest, UnstableHingeAnglesWithLidOpened) {
253 AttachTickClockForTest();
254
255 ASSERT_FALSE(WasLidOpenedRecently());
256 ASSERT_FALSE(IsMaximizeModeStarted());
257
258 OpenLidToAngle(5.0f);
259 EXPECT_FALSE(IsMaximizeModeStarted());
260
261 OpenLidToAngle(355.0f);
262 EXPECT_TRUE(IsMaximizeModeStarted());
263
264 OpenLidToAngle(5.0f);
265 EXPECT_TRUE(IsMaximizeModeStarted());
266 }
267
130 // Tests that when the hinge is nearly vertically aligned, the current state 268 // Tests that when the hinge is nearly vertically aligned, the current state
131 // persists as the computed angle is highly inaccurate in this orientation. 269 // persists as the computed angle is highly inaccurate in this orientation.
132 TEST_F(MaximizeModeControllerTest, HingeAligned) { 270 TEST_F(MaximizeModeControllerTest, HingeAligned) {
133 // Laptop in normal orientation lid open 90 degrees. 271 // Laptop in normal orientation lid open 90 degrees.
134 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, 1.0f), 272 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, 1.0f),
135 gfx::Vector3dF(-1.0f, 0.0f, 0.0f)); 273 gfx::Vector3dF(-1.0f, 0.0f, 0.0f));
136 EXPECT_FALSE(IsMaximizeModeStarted()); 274 EXPECT_FALSE(IsMaximizeModeStarted());
137 275
138 // Completely vertical. 276 // Completely vertical.
139 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, -1.0f, 0.0f), 277 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, -1.0f, 0.0f),
(...skipping 10 matching lines...) Expand all
150 gfx::Vector3dF(1.0f, 0.0f, 0.0f)); 288 gfx::Vector3dF(1.0f, 0.0f, 0.0f));
151 EXPECT_TRUE(IsMaximizeModeStarted()); 289 EXPECT_TRUE(IsMaximizeModeStarted());
152 290
153 // Normal 90 degree orientation but near vertical should stay in maximize 291 // Normal 90 degree orientation but near vertical should stay in maximize
154 // mode. 292 // mode.
155 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, -1.0f, 0.01f), 293 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, -1.0f, 0.01f),
156 gfx::Vector3dF(-0.01f, -1.0f, 0.0f)); 294 gfx::Vector3dF(-0.01f, -1.0f, 0.0f));
157 EXPECT_TRUE(IsMaximizeModeStarted()); 295 EXPECT_TRUE(IsMaximizeModeStarted());
158 } 296 }
159 297
160 // Tests that accelerometer readings in each of the screen angles will trigger 298 // Tests that accelerometer readings in each of the screen angles will trigger a
161 // a rotation of the internal display. 299 // rotation of the internal display.
162 TEST_F(MaximizeModeControllerTest, DisplayRotation) { 300 TEST_F(MaximizeModeControllerTest, DisplayRotation) {
163 // Trigger maximize mode by opening to 270. 301 // Trigger maximize mode by opening to 270.
164 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, -1.0f), 302 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, -1.0f),
165 gfx::Vector3dF(-1.0f, 0.0f, 0.0f)); 303 gfx::Vector3dF(-1.0f, 0.0f, 0.0f));
166 ASSERT_TRUE(IsMaximizeModeStarted()); 304 ASSERT_TRUE(IsMaximizeModeStarted());
167 305
168 // Now test rotating in all directions. 306 // Now test rotating in all directions.
169 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 1.0f, 0.0f), 307 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 1.0f, 0.0f),
170 gfx::Vector3dF(0.0f, 1.0f, 0.0f)); 308 gfx::Vector3dF(0.0f, 1.0f, 0.0f));
171 EXPECT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation()); 309 EXPECT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation());
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 // and pressing power. 406 // and pressing power.
269 TEST_F(MaximizeModeControllerTest, Screenshot) { 407 TEST_F(MaximizeModeControllerTest, Screenshot) {
270 Shell::GetInstance()->lock_state_controller()->SetDelegate( 408 Shell::GetInstance()->lock_state_controller()->SetDelegate(
271 new test::TestLockStateControllerDelegate); 409 new test::TestLockStateControllerDelegate);
272 aura::Window* root = Shell::GetPrimaryRootWindow(); 410 aura::Window* root = Shell::GetPrimaryRootWindow();
273 ui::test::EventGenerator event_generator(root, root); 411 ui::test::EventGenerator event_generator(root, root);
274 test::TestScreenshotDelegate* delegate = GetScreenshotDelegate(); 412 test::TestScreenshotDelegate* delegate = GetScreenshotDelegate();
275 delegate->set_can_take_screenshot(true); 413 delegate->set_can_take_screenshot(true);
276 414
277 // Open up 270 degrees. 415 // Open up 270 degrees.
278 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, 1.0f), 416 OpenLidToAngle(270.0f);
279 gfx::Vector3dF(1.0f, 0.0f, 0.0f));
280 ASSERT_TRUE(IsMaximizeModeStarted()); 417 ASSERT_TRUE(IsMaximizeModeStarted());
281 418
282 // Pressing power alone does not take a screenshot. 419 // Pressing power alone does not take a screenshot.
283 event_generator.PressKey(ui::VKEY_POWER, 0); 420 event_generator.PressKey(ui::VKEY_POWER, 0);
284 event_generator.ReleaseKey(ui::VKEY_POWER, 0); 421 event_generator.ReleaseKey(ui::VKEY_POWER, 0);
285 EXPECT_EQ(0, delegate->handle_take_screenshot_count()); 422 EXPECT_EQ(0, delegate->handle_take_screenshot_count());
286 423
287 // Holding volume down and pressing power takes a screenshot. 424 // Holding volume down and pressing power takes a screenshot.
288 event_generator.PressKey(ui::VKEY_VOLUME_DOWN, 0); 425 event_generator.PressKey(ui::VKEY_VOLUME_DOWN, 0);
289 event_generator.PressKey(ui::VKEY_POWER, 0); 426 event_generator.PressKey(ui::VKEY_POWER, 0);
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation()); 493 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
357 494
358 maximize_mode_controller()->SetRotationLocked(false); 495 maximize_mode_controller()->SetRotationLocked(false);
359 TriggerAccelerometerUpdate(gravity, gravity); 496 TriggerAccelerometerUpdate(gravity, gravity);
360 EXPECT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation()); 497 EXPECT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation());
361 } 498 }
362 499
363 // Tests that when MaximizeModeController turns off MaximizeMode that on the 500 // Tests that when MaximizeModeController turns off MaximizeMode that on the
364 // next accelerometer update the rotation lock is cleared. 501 // next accelerometer update the rotation lock is cleared.
365 TEST_F(MaximizeModeControllerTest, ExitingMaximizeModeClearRotationLock) { 502 TEST_F(MaximizeModeControllerTest, ExitingMaximizeModeClearRotationLock) {
366 // The base remains steady.
367 gfx::Vector3dF base(0.0f, 0.0f, 1.0f);
368
369 // Trigger maximize mode by opening to 270. 503 // Trigger maximize mode by opening to 270.
370 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, -1.0f), 504 OpenLidToAngle(270.0f);
371 gfx::Vector3dF(-1.0f, 0.0f, 0.0f));
372 ASSERT_TRUE(IsMaximizeModeStarted()); 505 ASSERT_TRUE(IsMaximizeModeStarted());
373 506
374 maximize_mode_controller()->SetRotationLocked(true); 507 maximize_mode_controller()->SetRotationLocked(true);
375 508
376 // Open 90 degrees. 509 OpenLidToAngle(90.0f);
377 TriggerAccelerometerUpdate(base, gfx::Vector3dF(-1.0f, 0.0f, 0.0f));
378 EXPECT_FALSE(IsMaximizeModeStarted()); 510 EXPECT_FALSE(IsMaximizeModeStarted());
379 511
380 // Send an update that would not relaunch MaximizeMode. 90 degrees. 512 // Send an update that would not relaunch MaximizeMode.
381 TriggerAccelerometerUpdate(base, gfx::Vector3dF(-1.0f, 0.0f, 0.0f)); 513 OpenLidToAngle(90.0f);
382 EXPECT_FALSE(maximize_mode_controller()->rotation_locked()); 514 EXPECT_FALSE(maximize_mode_controller()->rotation_locked());
383 } 515 }
384 516
385 // The TrayDisplay class that is responsible for adding/updating MessageCenter 517 // The TrayDisplay class that is responsible for adding/updating MessageCenter
386 // notifications is only added to the SystemTray on ChromeOS. 518 // notifications is only added to the SystemTray on ChromeOS.
387 #if defined(OS_CHROMEOS) 519 #if defined(OS_CHROMEOS)
388 // Tests that the screen rotation notifications are suppressed when 520 // Tests that the screen rotation notifications are suppressed when
389 // triggered by the accelerometer. 521 // triggered by the accelerometer.
390 TEST_F(MaximizeModeControllerTest, BlockRotationNotifications) { 522 TEST_F(MaximizeModeControllerTest, BlockRotationNotifications) {
391 test::TestSystemTrayDelegate* tray_delegate = 523 test::TestSystemTrayDelegate* tray_delegate =
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 // User sets rotation to the same rotation that the display was at when 622 // User sets rotation to the same rotation that the display was at when
491 // maximize mode was activated. 623 // maximize mode was activated.
492 SetInternalDisplayRotation(gfx::Display::ROTATE_0); 624 SetInternalDisplayRotation(gfx::Display::ROTATE_0);
493 // Exit maximize mode 625 // Exit maximize mode
494 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, 1.0f), 626 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, 1.0f),
495 gfx::Vector3dF(-1.0f, 0.0f, 0.0f)); 627 gfx::Vector3dF(-1.0f, 0.0f, 0.0f));
496 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation()); 628 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
497 } 629 }
498 630
499 } // namespace ash 631 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698