Allow MaximizeModeController to ignore hinge angles.
[chromium-blink-merge.git] / ash / wm / maximize_mode / maximize_mode_controller_unittest.cc
blob449e9ac38eef39f5e9b1b87e855807677d12ff11
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
3 // found in the LICENSE file.
5 #include <math.h>
7 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
9 #include "ash/accelerometer/accelerometer_controller.h"
10 #include "ash/ash_switches.h"
11 #include "ash/display/display_manager.h"
12 #include "ash/shell.h"
13 #include "ash/system/tray/system_tray_delegate.h"
14 #include "ash/test/ash_test_base.h"
15 #include "ash/test/display_manager_test_api.h"
16 #include "ash/test/test_system_tray_delegate.h"
17 #include "ash/test/test_volume_control_delegate.h"
18 #include "base/command_line.h"
19 #include "base/test/simple_test_tick_clock.h"
20 #include "ui/accelerometer/accelerometer_types.h"
21 #include "ui/events/event_handler.h"
22 #include "ui/events/test/event_generator.h"
23 #include "ui/gfx/vector3d_f.h"
24 #include "ui/message_center/message_center.h"
26 #if defined(USE_X11)
27 #include "ui/events/test/events_test_utils_x11.h"
28 #endif
30 namespace ash {
32 namespace {
34 const float kDegreesToRadians = 3.1415926f / 180.0f;
35 const float kMeanGravity = 9.8066f;
37 } // namespace
39 // Test accelerometer data taken with the lid at less than 180 degrees while
40 // shaking the device around. The data is to be interpreted in groups of 6 where
41 // each 6 values corresponds to the X, Y, and Z readings from the base and lid
42 // accelerometers in this order.
43 extern const float kAccelerometerLaptopModeTestData[];
44 extern const size_t kAccelerometerLaptopModeTestDataLength;
46 // Test accelerometer data taken with the lid open 360 degrees while
47 // shaking the device around. The data is to be interpreted in groups of 6 where
48 // each 6 values corresponds to the X, Y, and Z readings from the base and lid
49 // accelerometers in this order.
50 extern const float kAccelerometerFullyOpenTestData[];
51 extern const size_t kAccelerometerFullyOpenTestDataLength;
53 class MaximizeModeControllerTest : public test::AshTestBase {
54 public:
55 MaximizeModeControllerTest() {}
56 virtual ~MaximizeModeControllerTest() {}
58 virtual void SetUp() override {
59 test::AshTestBase::SetUp();
60 Shell::GetInstance()->accelerometer_controller()->RemoveObserver(
61 maximize_mode_controller());
63 // Set the first display to be the internal display for the accelerometer
64 // screen rotation tests.
65 test::DisplayManagerTestApi(Shell::GetInstance()->display_manager()).
66 SetFirstDisplayAsInternalDisplay();
69 virtual void TearDown() override {
70 Shell::GetInstance()->accelerometer_controller()->AddObserver(
71 maximize_mode_controller());
72 test::AshTestBase::TearDown();
75 MaximizeModeController* maximize_mode_controller() {
76 return Shell::GetInstance()->maximize_mode_controller();
79 void TriggerAccelerometerUpdate(const gfx::Vector3dF& base,
80 const gfx::Vector3dF& lid) {
81 ui::AccelerometerUpdate update;
82 update.Set(ui::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD,
83 base.x(), base.y(), base.z());
84 update.Set(ui::ACCELEROMETER_SOURCE_SCREEN,
85 lid.x(), lid.y(), lid.z());
86 maximize_mode_controller()->OnAccelerometerUpdated(update);
89 bool IsMaximizeModeStarted() {
90 return maximize_mode_controller()->IsMaximizeModeWindowManagerEnabled();
93 gfx::Display::Rotation GetInternalDisplayRotation() const {
94 return Shell::GetInstance()->display_manager()->GetDisplayInfo(
95 gfx::Display::InternalDisplayId()).rotation();
98 void SetInternalDisplayRotation(gfx::Display::Rotation rotation) const {
99 Shell::GetInstance()->display_manager()->
100 SetDisplayRotation(gfx::Display::InternalDisplayId(), rotation);
103 // Attaches a SimpleTestTickClock to the MaximizeModeController with a non
104 // null value initial value.
105 void AttachTickClockForTest() {
106 scoped_ptr<base::TickClock> tick_clock(
107 test_tick_clock_ = new base::SimpleTestTickClock());
108 test_tick_clock_->Advance(base::TimeDelta::FromSeconds(1));
109 maximize_mode_controller()->SetTickClockForTest(tick_clock.Pass());
112 void AdvanceTickClock(const base::TimeDelta& delta) {
113 DCHECK(test_tick_clock_);
114 test_tick_clock_->Advance(delta);
117 void OpenLidToAngle(float degrees) {
118 DCHECK(degrees >= 0.0f);
119 DCHECK(degrees <= 360.0f);
121 float radians = degrees * kDegreesToRadians;
122 gfx::Vector3dF base_vector(0.0f, -kMeanGravity, 0.0f);
123 gfx::Vector3dF lid_vector(0.0f,
124 kMeanGravity * cos(radians),
125 kMeanGravity * sin(radians));
126 TriggerAccelerometerUpdate(base_vector, lid_vector);
129 #if defined(OS_CHROMEOS)
130 void OpenLid() {
131 maximize_mode_controller()->LidEventReceived(true /* open */,
132 maximize_mode_controller()->tick_clock_->NowTicks());
135 void CloseLid() {
136 maximize_mode_controller()->LidEventReceived(false /* open */,
137 maximize_mode_controller()->tick_clock_->NowTicks());
139 #endif // OS_CHROMEOS
141 bool WasLidOpenedRecently() {
142 return maximize_mode_controller()->WasLidOpenedRecently();
145 private:
146 base::SimpleTestTickClock* test_tick_clock_;
148 DISALLOW_COPY_AND_ASSIGN(MaximizeModeControllerTest);
151 #if defined(OS_CHROMEOS)
153 // Verify that closing the lid will exit maximize mode.
154 TEST_F(MaximizeModeControllerTest, CloseLidWhileInMaximizeMode) {
155 OpenLidToAngle(315.0f);
156 ASSERT_TRUE(IsMaximizeModeStarted());
158 CloseLid();
159 EXPECT_FALSE(IsMaximizeModeStarted());
162 // Verify that maximize mode will not be entered when the lid is closed.
163 TEST_F(MaximizeModeControllerTest,
164 HingeAnglesWithLidClosed) {
165 AttachTickClockForTest();
167 CloseLid();
169 OpenLidToAngle(270.0f);
170 EXPECT_FALSE(IsMaximizeModeStarted());
172 OpenLidToAngle(315.0f);
173 EXPECT_FALSE(IsMaximizeModeStarted());
175 OpenLidToAngle(355.0f);
176 EXPECT_FALSE(IsMaximizeModeStarted());
179 // Verify the maximize mode state for unstable hinge angles when the lid was
180 // recently open.
181 TEST_F(MaximizeModeControllerTest,
182 UnstableHingeAnglesWhenLidRecentlyOpened) {
183 AttachTickClockForTest();
185 OpenLid();
186 ASSERT_TRUE(WasLidOpenedRecently());
188 OpenLidToAngle(5.0f);
189 EXPECT_FALSE(IsMaximizeModeStarted());
191 OpenLidToAngle(355.0f);
192 EXPECT_FALSE(IsMaximizeModeStarted());
194 // This is a stable reading and should clear the last lid opened time.
195 OpenLidToAngle(45.0f);
196 EXPECT_FALSE(IsMaximizeModeStarted());
197 EXPECT_FALSE(WasLidOpenedRecently());
199 OpenLidToAngle(355.0f);
200 EXPECT_TRUE(IsMaximizeModeStarted());
203 #endif // OS_CHROMEOS
205 // Verify the WasLidOpenedRecently signal with respect to time.
206 TEST_F(MaximizeModeControllerTest, WasLidOpenedRecentlyOverTime) {
207 #if defined(OS_CHROMEOS)
209 AttachTickClockForTest();
211 // No lid open time initially.
212 ASSERT_FALSE(WasLidOpenedRecently());
214 CloseLid();
215 EXPECT_FALSE(WasLidOpenedRecently());
217 OpenLid();
218 EXPECT_TRUE(WasLidOpenedRecently());
220 // 1 second after lid open.
221 AdvanceTickClock(base::TimeDelta::FromSeconds(1));
222 EXPECT_TRUE(WasLidOpenedRecently());
224 // 3 seconds after lid open.
225 AdvanceTickClock(base::TimeDelta::FromSeconds(2));
226 EXPECT_FALSE(WasLidOpenedRecently());
228 #else
230 EXPECT_FALSE(WasLidOpenedRecently());
232 #endif // OS_CHROMEOS
235 // Verify the maximize mode enter/exit thresholds for stable angles.
236 TEST_F(MaximizeModeControllerTest, StableHingeAnglesWithLidOpened) {
237 ASSERT_FALSE(IsMaximizeModeStarted());
238 ASSERT_FALSE(WasLidOpenedRecently());
240 OpenLidToAngle(180.0f);
241 EXPECT_FALSE(IsMaximizeModeStarted());
243 OpenLidToAngle(315.0f);
244 EXPECT_TRUE(IsMaximizeModeStarted());
246 OpenLidToAngle(180.0f);
247 EXPECT_TRUE(IsMaximizeModeStarted());
249 OpenLidToAngle(45.0f);
250 EXPECT_FALSE(IsMaximizeModeStarted());
252 OpenLidToAngle(270.0f);
253 EXPECT_TRUE(IsMaximizeModeStarted());
255 OpenLidToAngle(90.0f);
256 EXPECT_FALSE(IsMaximizeModeStarted());
259 // Verify the maximize mode state for unstable hinge angles when the lid is open
260 // but not recently.
261 TEST_F(MaximizeModeControllerTest, UnstableHingeAnglesWithLidOpened) {
262 AttachTickClockForTest();
264 ASSERT_FALSE(WasLidOpenedRecently());
265 ASSERT_FALSE(IsMaximizeModeStarted());
267 OpenLidToAngle(5.0f);
268 EXPECT_FALSE(IsMaximizeModeStarted());
270 OpenLidToAngle(355.0f);
271 EXPECT_TRUE(IsMaximizeModeStarted());
273 OpenLidToAngle(5.0f);
274 EXPECT_TRUE(IsMaximizeModeStarted());
277 // Tests that when the hinge is nearly vertically aligned, the current state
278 // persists as the computed angle is highly inaccurate in this orientation.
279 TEST_F(MaximizeModeControllerTest, HingeAligned) {
280 // Laptop in normal orientation lid open 90 degrees.
281 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, -kMeanGravity),
282 gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
283 EXPECT_FALSE(IsMaximizeModeStarted());
285 // Completely vertical.
286 TriggerAccelerometerUpdate(gfx::Vector3dF(kMeanGravity, 0.0f, 0.0f),
287 gfx::Vector3dF(kMeanGravity, 0.0f, 0.0f));
288 EXPECT_FALSE(IsMaximizeModeStarted());
290 // Close to vertical but with hinge appearing to be open 270 degrees.
291 TriggerAccelerometerUpdate(gfx::Vector3dF(kMeanGravity, 0.0f, -0.1f),
292 gfx::Vector3dF(kMeanGravity, 0.1f, 0.0f));
293 EXPECT_FALSE(IsMaximizeModeStarted());
295 // Flat and open 270 degrees should start maximize mode.
296 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, -kMeanGravity),
297 gfx::Vector3dF(0.0f, kMeanGravity, 0.0f));
298 EXPECT_TRUE(IsMaximizeModeStarted());
300 // Normal 90 degree orientation but near vertical should stay in maximize
301 // mode.
302 TriggerAccelerometerUpdate(gfx::Vector3dF(kMeanGravity, 0.0f, -0.1f),
303 gfx::Vector3dF(kMeanGravity, -0.1f, 0.0f));
304 EXPECT_TRUE(IsMaximizeModeStarted());
307 // Tests that accelerometer readings in each of the screen angles will trigger a
308 // rotation of the internal display.
309 TEST_F(MaximizeModeControllerTest, DisplayRotation) {
310 // Trigger maximize mode by opening to 270.
311 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, kMeanGravity),
312 gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
313 ASSERT_TRUE(IsMaximizeModeStarted());
315 // Now test rotating in all directions.
316 TriggerAccelerometerUpdate(gfx::Vector3dF(-kMeanGravity, 0.0f, 0.0f),
317 gfx::Vector3dF(-kMeanGravity, 0.0f, 0.0f));
318 EXPECT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation());
319 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f),
320 gfx::Vector3dF(0.0f, kMeanGravity, 0.0f));
321 EXPECT_EQ(gfx::Display::ROTATE_180, GetInternalDisplayRotation());
322 TriggerAccelerometerUpdate(gfx::Vector3dF(kMeanGravity, 0.0f, 0.0f),
323 gfx::Vector3dF(kMeanGravity, 0.0f, 0.0f));
324 EXPECT_EQ(gfx::Display::ROTATE_270, GetInternalDisplayRotation());
325 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, kMeanGravity, 0.0f),
326 gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
327 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
330 // Tests that low angles are ignored by the accelerometer (i.e. when the device
331 // is almost laying flat).
332 TEST_F(MaximizeModeControllerTest, RotationIgnoresLowAngles) {
333 // Trigger maximize mode by opening to 270.
334 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, kMeanGravity),
335 gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
336 ASSERT_TRUE(IsMaximizeModeStarted());
338 TriggerAccelerometerUpdate(
339 gfx::Vector3dF(0.0f, kMeanGravity, kMeanGravity),
340 gfx::Vector3dF(0.0f, -kMeanGravity, -kMeanGravity));
341 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
342 TriggerAccelerometerUpdate(gfx::Vector3dF(-2.0f, 0.0f, kMeanGravity),
343 gfx::Vector3dF(-2.0f, 0.0f, -kMeanGravity));
344 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
345 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, -2.0f, kMeanGravity),
346 gfx::Vector3dF(0.0f, 2.0f, -kMeanGravity));
347 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
348 TriggerAccelerometerUpdate(gfx::Vector3dF(2.0f, 0.0f, kMeanGravity),
349 gfx::Vector3dF(2.0f, 0.0f, -kMeanGravity));
350 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
351 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 2.0f, kMeanGravity),
352 gfx::Vector3dF(0.0f, -2.0f, -kMeanGravity));
353 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
356 // Tests that the display will stick to the current orientation beyond the
357 // halfway point, preventing frequent updates back and forth.
358 TEST_F(MaximizeModeControllerTest, RotationSticky) {
359 // Trigger maximize mode by opening to 270.
360 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, kMeanGravity),
361 gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
362 ASSERT_TRUE(IsMaximizeModeStarted());
364 gfx::Vector3dF gravity(0.0f, -kMeanGravity, 0.0f);
365 TriggerAccelerometerUpdate(gravity, gravity);
366 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
368 // Turn past half-way point to next direction and rotation should remain
369 // the same.
370 float degrees = 50.0;
371 gravity.set_x(-sin(degrees * kDegreesToRadians) * kMeanGravity);
372 gravity.set_y(-cos(degrees * kDegreesToRadians) * kMeanGravity);
373 TriggerAccelerometerUpdate(gravity, gravity);
374 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
376 // Turn more and the screen should rotate.
377 degrees = 70.0;
378 gravity.set_x(-sin(degrees * kDegreesToRadians) * kMeanGravity);
379 gravity.set_y(-cos(degrees * kDegreesToRadians) * kMeanGravity);
380 TriggerAccelerometerUpdate(gravity, gravity);
381 EXPECT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation());
383 // Turn back just beyond the half-way point and the new rotation should
384 // still be in effect.
385 degrees = 40.0;
386 gravity.set_x(-sin(degrees * kDegreesToRadians) * kMeanGravity);
387 gravity.set_y(-cos(degrees * kDegreesToRadians) * kMeanGravity);
388 TriggerAccelerometerUpdate(gravity, gravity);
389 EXPECT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation());
392 // Tests that the screen only rotates when maximize mode is engaged, and will
393 // return to the standard orientation on exiting maximize mode.
394 TEST_F(MaximizeModeControllerTest, RotationOnlyInMaximizeMode) {
395 // Rotate on side with lid only open 90 degrees.
396 TriggerAccelerometerUpdate(gfx::Vector3dF(-9.5f, 0.0f, -3.5f),
397 gfx::Vector3dF(-9.5f, -3.5f, 0.0f));
398 ASSERT_FALSE(IsMaximizeModeStarted());
399 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
401 // Open lid, screen should now rotate to match orientation.
402 TriggerAccelerometerUpdate(gfx::Vector3dF(-9.5f, 0.0f, 3.5f),
403 gfx::Vector3dF(-9.5f, -3.5f, 0.0f));
404 ASSERT_TRUE(IsMaximizeModeStarted());
405 EXPECT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation());
407 // Close lid back to 90, screen should rotate back.
408 TriggerAccelerometerUpdate(gfx::Vector3dF(-9.5f, 0.0f, -3.5f),
409 gfx::Vector3dF(-9.5f, -3.5f, 0.0f));
410 ASSERT_FALSE(IsMaximizeModeStarted());
411 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
415 TEST_F(MaximizeModeControllerTest, LaptopTest) {
416 // Feeds in sample accelerometer data and verifies that there are no
417 // transitions into touchview / maximize mode while shaking the device around
418 // with the hinge at less than 180 degrees. Note the conversion from device
419 // data to accelerometer updates consistent with accelerometer_reader.cc.
420 ASSERT_EQ(0u, kAccelerometerLaptopModeTestDataLength % 6);
421 for (size_t i = 0; i < kAccelerometerLaptopModeTestDataLength / 6; ++i) {
422 gfx::Vector3dF base(-kAccelerometerLaptopModeTestData[i * 6 + 1],
423 -kAccelerometerLaptopModeTestData[i * 6],
424 -kAccelerometerLaptopModeTestData[i * 6 + 2]);
425 base.Scale(kMeanGravity);
426 gfx::Vector3dF lid(-kAccelerometerLaptopModeTestData[i * 6 + 4],
427 kAccelerometerLaptopModeTestData[i * 6 + 3],
428 kAccelerometerLaptopModeTestData[i * 6 + 5]);
429 lid.Scale(kMeanGravity);
430 TriggerAccelerometerUpdate(base, lid);
431 // There are a lot of samples, so ASSERT rather than EXPECT to only generate
432 // one failure rather than potentially hundreds.
433 ASSERT_FALSE(IsMaximizeModeStarted());
437 TEST_F(MaximizeModeControllerTest, MaximizeModeTest) {
438 // Trigger maximize mode by opening to 270 to begin the test in maximize mode.
439 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, kMeanGravity),
440 gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
441 ASSERT_TRUE(IsMaximizeModeStarted());
443 // Feeds in sample accelerometer data and verifies that there are no
444 // transitions out of touchview / maximize mode while shaking the device
445 // around. Note the conversion from device data to accelerometer updates
446 // consistent with accelerometer_reader.cc.
447 ASSERT_EQ(0u, kAccelerometerFullyOpenTestDataLength % 6);
448 for (size_t i = 0; i < kAccelerometerFullyOpenTestDataLength / 6; ++i) {
449 gfx::Vector3dF base(-kAccelerometerFullyOpenTestData[i * 6 + 1],
450 -kAccelerometerFullyOpenTestData[i * 6],
451 -kAccelerometerFullyOpenTestData[i * 6 + 2]);
452 base.Scale(kMeanGravity);
453 gfx::Vector3dF lid(-kAccelerometerFullyOpenTestData[i * 6 + 4],
454 kAccelerometerFullyOpenTestData[i * 6 + 3],
455 kAccelerometerFullyOpenTestData[i * 6 + 5]);
456 lid.Scale(kMeanGravity);
457 TriggerAccelerometerUpdate(base, lid);
458 // There are a lot of samples, so ASSERT rather than EXPECT to only generate
459 // one failure rather than potentially hundreds.
460 ASSERT_TRUE(IsMaximizeModeStarted());
464 // Tests that the display will stick to its current orientation when the
465 // rotation lock has been set.
466 TEST_F(MaximizeModeControllerTest, RotationLockPreventsRotation) {
467 // Trigger maximize mode by opening to 270.
468 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, kMeanGravity),
469 gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
470 ASSERT_TRUE(IsMaximizeModeStarted());
472 gfx::Vector3dF gravity(-kMeanGravity, 0.0f, 0.0f);
474 maximize_mode_controller()->SetRotationLocked(true);
476 // Turn past the threshold for rotation.
477 float degrees = 90.0;
478 gravity.set_x(-sin(degrees * kDegreesToRadians) * kMeanGravity);
479 gravity.set_y(-cos(degrees * kDegreesToRadians) * kMeanGravity);
480 TriggerAccelerometerUpdate(gravity, gravity);
481 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
483 maximize_mode_controller()->SetRotationLocked(false);
484 TriggerAccelerometerUpdate(gravity, gravity);
485 EXPECT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation());
488 // Tests that when MaximizeModeController turns off MaximizeMode that on the
489 // next accelerometer update the rotation lock is cleared.
490 TEST_F(MaximizeModeControllerTest, ExitingMaximizeModeClearRotationLock) {
491 // Trigger maximize mode by opening to 270.
492 OpenLidToAngle(270.0f);
493 ASSERT_TRUE(IsMaximizeModeStarted());
495 maximize_mode_controller()->SetRotationLocked(true);
497 OpenLidToAngle(90.0f);
498 EXPECT_FALSE(IsMaximizeModeStarted());
500 // Send an update that would not relaunch MaximizeMode.
501 OpenLidToAngle(90.0f);
502 EXPECT_FALSE(maximize_mode_controller()->rotation_locked());
505 // The TrayDisplay class that is responsible for adding/updating MessageCenter
506 // notifications is only added to the SystemTray on ChromeOS.
507 #if defined(OS_CHROMEOS)
508 // Tests that the screen rotation notifications are suppressed when
509 // triggered by the accelerometer.
510 TEST_F(MaximizeModeControllerTest, BlockRotationNotifications) {
511 test::TestSystemTrayDelegate* tray_delegate =
512 static_cast<test::TestSystemTrayDelegate*>(
513 Shell::GetInstance()->system_tray_delegate());
514 tray_delegate->set_should_show_display_notification(true);
516 message_center::MessageCenter* message_center =
517 message_center::MessageCenter::Get();
519 // Make sure notifications are still displayed when
520 // adjusting the screen rotation directly when not in maximize mode
521 ASSERT_FALSE(IsMaximizeModeStarted());
522 ASSERT_NE(gfx::Display::ROTATE_180, GetInternalDisplayRotation());
523 ASSERT_EQ(0u, message_center->NotificationCount());
524 ASSERT_FALSE(message_center->HasPopupNotifications());
525 SetInternalDisplayRotation(gfx::Display::ROTATE_180);
526 EXPECT_EQ(gfx::Display::ROTATE_180, GetInternalDisplayRotation());
527 EXPECT_EQ(1u, message_center->NotificationCount());
528 EXPECT_TRUE(message_center->HasPopupNotifications());
530 // Reset the screen rotation.
531 SetInternalDisplayRotation(gfx::Display::ROTATE_0);
532 // Clear all notifications
533 message_center->RemoveAllNotifications(false);
534 // Trigger maximize mode by opening to 270.
535 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, kMeanGravity),
536 gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
537 EXPECT_TRUE(IsMaximizeModeStarted());
538 EXPECT_EQ(0u, message_center->NotificationCount());
539 EXPECT_FALSE(message_center->HasPopupNotifications());
541 // Make sure notifications are still displayed when
542 // adjusting the screen rotation directly when in maximize mode
543 ASSERT_NE(gfx::Display::ROTATE_270, GetInternalDisplayRotation());
544 SetInternalDisplayRotation(gfx::Display::ROTATE_270);
545 maximize_mode_controller()->SetRotationLocked(false);
546 EXPECT_EQ(gfx::Display::ROTATE_270, GetInternalDisplayRotation());
547 EXPECT_EQ(1u, message_center->NotificationCount());
548 EXPECT_TRUE(message_center->HasPopupNotifications());
550 // Clear all notifications
551 message_center->RemoveAllNotifications(false);
552 EXPECT_EQ(0u, message_center->NotificationCount());
553 EXPECT_FALSE(message_center->HasPopupNotifications());
555 // Make sure notifications are blocked when adjusting the screen rotation
556 // via the accelerometer while in maximize mode
557 // Rotate the screen 90 degrees
558 ASSERT_NE(gfx::Display::ROTATE_90, GetInternalDisplayRotation());
559 TriggerAccelerometerUpdate(gfx::Vector3dF(-kMeanGravity, 0.0f, 0.0f),
560 gfx::Vector3dF(-kMeanGravity, 0.0f, 0.0f));
561 ASSERT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation());
562 EXPECT_EQ(0u, message_center->NotificationCount());
563 EXPECT_FALSE(message_center->HasPopupNotifications());
565 #endif
567 // Tests that if a user has set a display rotation that it is restored upon
568 // exiting maximize mode.
569 TEST_F(MaximizeModeControllerTest, ResetUserRotationUponExit) {
570 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
571 display_manager->SetDisplayRotation(gfx::Display::InternalDisplayId(),
572 gfx::Display::ROTATE_90);
574 // Trigger maximize mode
575 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, kMeanGravity),
576 gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
577 ASSERT_TRUE(IsMaximizeModeStarted());
579 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f),
580 gfx::Vector3dF(0.0f, kMeanGravity, 0.0f));
581 EXPECT_EQ(gfx::Display::ROTATE_180, GetInternalDisplayRotation());
583 // Exit maximize mode
584 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, -kMeanGravity),
585 gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
586 EXPECT_FALSE(IsMaximizeModeStarted());
587 EXPECT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation());
590 // Tests that if a user sets a display rotation that accelerometer rotation
591 // becomes locked.
592 TEST_F(MaximizeModeControllerTest,
593 NonAccelerometerRotationChangesLockRotation) {
594 // Trigger maximize mode by opening to 270.
595 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, kMeanGravity),
596 gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
597 ASSERT_FALSE(maximize_mode_controller()->rotation_locked());
598 SetInternalDisplayRotation(gfx::Display::ROTATE_270);
599 EXPECT_TRUE(maximize_mode_controller()->rotation_locked());
602 // Tests that if a user changes the display rotation, while rotation is locked,
603 // that the updates are recorded. Upon exiting maximize mode the latest user
604 // rotation should be applied.
605 TEST_F(MaximizeModeControllerTest, UpdateUserRotationWhileRotationLocked) {
606 // Trigger maximize mode by opening to 270.
607 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, kMeanGravity),
608 gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
609 SetInternalDisplayRotation(gfx::Display::ROTATE_270);
610 // User sets rotation to the same rotation that the display was at when
611 // maximize mode was activated.
612 SetInternalDisplayRotation(gfx::Display::ROTATE_0);
613 // Exit maximize mode
614 TriggerAccelerometerUpdate(gfx::Vector3dF(0.0f, 0.0f, -kMeanGravity),
615 gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
616 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
619 class MaximizeModeControllerSwitchesTest : public MaximizeModeControllerTest {
620 public:
621 MaximizeModeControllerSwitchesTest() {}
622 virtual ~MaximizeModeControllerSwitchesTest(){}
624 virtual void SetUp() override {
625 CommandLine::ForCurrentProcess()->AppendSwitch(
626 switches::kAshEnableTouchViewTesting);
627 MaximizeModeControllerTest::SetUp();
629 private:
630 DISALLOW_COPY_AND_ASSIGN(MaximizeModeControllerSwitchesTest);
633 // Tests that when the command line switch for testing maximize mode is on, that
634 // accelerometer updates which would normally cause it to exit do not, and that
635 // screen rotations still occur.
636 TEST_F(MaximizeModeControllerSwitchesTest, IgnoreHingeAngles) {
637 maximize_mode_controller()->EnableMaximizeModeWindowManager(true);
639 // Would normally trigger an exit from maximize mode.
640 OpenLidToAngle(90.0f);
641 EXPECT_TRUE(IsMaximizeModeStarted());
643 TriggerAccelerometerUpdate(gfx::Vector3dF(-kMeanGravity, 0.0f, 0.0f),
644 gfx::Vector3dF(-kMeanGravity, 0.0f, 0.0f));
645 EXPECT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation());
648 } // namespace ash