platformKeys: Add per-extension sign permissions.
[chromium-blink-merge.git] / ash / wm / maximize_mode / maximize_mode_controller_unittest.cc
blob5a4a018029206e4a0123390320c310168b5fa6d4
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/ash_switches.h"
10 #include "ash/display/display_manager.h"
11 #include "ash/shell.h"
12 #include "ash/system/tray/system_tray_delegate.h"
13 #include "ash/test/ash_test_base.h"
14 #include "ash/test/display_manager_test_api.h"
15 #include "ash/test/test_system_tray_delegate.h"
16 #include "ash/test/test_volume_control_delegate.h"
17 #include "base/command_line.h"
18 #include "base/test/simple_test_tick_clock.h"
19 #include "chromeos/accelerometer/accelerometer_reader.h"
20 #include "chromeos/accelerometer/accelerometer_types.h"
21 #include "ui/events/event_handler.h"
22 #include "ui/events/test/event_generator.h"
23 #include "ui/gfx/geometry/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 ~MaximizeModeControllerTest() override {}
58 void SetUp() override {
59 test::AshTestBase::SetUp();
60 chromeos::AccelerometerReader::GetInstance()->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 void TearDown() override {
70 chromeos::AccelerometerReader::GetInstance()->AddObserver(
71 maximize_mode_controller());
72 test::AshTestBase::TearDown();
75 MaximizeModeController* maximize_mode_controller() {
76 return Shell::GetInstance()->maximize_mode_controller();
79 void TriggerLidUpdate(const gfx::Vector3dF& lid) {
80 chromeos::AccelerometerUpdate update;
81 update.Set(chromeos::ACCELEROMETER_SOURCE_SCREEN, lid.x(), lid.y(),
82 lid.z());
83 maximize_mode_controller()->OnAccelerometerUpdated(update);
86 void TriggerBaseAndLidUpdate(const gfx::Vector3dF& base,
87 const gfx::Vector3dF& lid) {
88 chromeos::AccelerometerUpdate update;
89 update.Set(chromeos::ACCELEROMETER_SOURCE_ATTACHED_KEYBOARD, base.x(),
90 base.y(), base.z());
91 update.Set(chromeos::ACCELEROMETER_SOURCE_SCREEN, lid.x(), lid.y(),
92 lid.z());
93 maximize_mode_controller()->OnAccelerometerUpdated(update);
96 bool IsMaximizeModeStarted() {
97 return maximize_mode_controller()->IsMaximizeModeWindowManagerEnabled();
100 gfx::Display::Rotation GetInternalDisplayRotation() const {
101 return Shell::GetInstance()->display_manager()->GetDisplayInfo(
102 gfx::Display::InternalDisplayId()).rotation();
105 void SetInternalDisplayRotation(gfx::Display::Rotation rotation) const {
106 Shell::GetInstance()->display_manager()->
107 SetDisplayRotation(gfx::Display::InternalDisplayId(), rotation);
110 // Attaches a SimpleTestTickClock to the MaximizeModeController with a non
111 // null value initial value.
112 void AttachTickClockForTest() {
113 scoped_ptr<base::TickClock> tick_clock(
114 test_tick_clock_ = new base::SimpleTestTickClock());
115 test_tick_clock_->Advance(base::TimeDelta::FromSeconds(1));
116 maximize_mode_controller()->SetTickClockForTest(tick_clock.Pass());
119 void AdvanceTickClock(const base::TimeDelta& delta) {
120 DCHECK(test_tick_clock_);
121 test_tick_clock_->Advance(delta);
124 void OpenLidToAngle(float degrees) {
125 DCHECK(degrees >= 0.0f);
126 DCHECK(degrees <= 360.0f);
128 float radians = degrees * kDegreesToRadians;
129 gfx::Vector3dF base_vector(0.0f, -kMeanGravity, 0.0f);
130 gfx::Vector3dF lid_vector(0.0f,
131 kMeanGravity * cos(radians),
132 kMeanGravity * sin(radians));
133 TriggerBaseAndLidUpdate(base_vector, lid_vector);
136 void OpenLid() {
137 maximize_mode_controller()->LidEventReceived(true /* open */,
138 maximize_mode_controller()->tick_clock_->NowTicks());
141 void CloseLid() {
142 maximize_mode_controller()->LidEventReceived(false /* open */,
143 maximize_mode_controller()->tick_clock_->NowTicks());
146 bool WasLidOpenedRecently() {
147 return maximize_mode_controller()->WasLidOpenedRecently();
150 private:
151 base::SimpleTestTickClock* test_tick_clock_;
153 DISALLOW_COPY_AND_ASSIGN(MaximizeModeControllerTest);
156 // Verify that closing the lid will exit maximize mode.
157 TEST_F(MaximizeModeControllerTest, CloseLidWhileInMaximizeMode) {
158 OpenLidToAngle(315.0f);
159 ASSERT_TRUE(IsMaximizeModeStarted());
161 CloseLid();
162 EXPECT_FALSE(IsMaximizeModeStarted());
165 // Verify that maximize mode will not be entered when the lid is closed.
166 TEST_F(MaximizeModeControllerTest,
167 HingeAnglesWithLidClosed) {
168 AttachTickClockForTest();
170 CloseLid();
172 OpenLidToAngle(270.0f);
173 EXPECT_FALSE(IsMaximizeModeStarted());
175 OpenLidToAngle(315.0f);
176 EXPECT_FALSE(IsMaximizeModeStarted());
178 OpenLidToAngle(355.0f);
179 EXPECT_FALSE(IsMaximizeModeStarted());
182 // Verify the maximize mode state for unstable hinge angles when the lid was
183 // recently open.
184 TEST_F(MaximizeModeControllerTest,
185 UnstableHingeAnglesWhenLidRecentlyOpened) {
186 AttachTickClockForTest();
188 OpenLid();
189 ASSERT_TRUE(WasLidOpenedRecently());
191 OpenLidToAngle(5.0f);
192 EXPECT_FALSE(IsMaximizeModeStarted());
194 OpenLidToAngle(355.0f);
195 EXPECT_FALSE(IsMaximizeModeStarted());
197 // This is a stable reading and should clear the last lid opened time.
198 OpenLidToAngle(45.0f);
199 EXPECT_FALSE(IsMaximizeModeStarted());
200 EXPECT_FALSE(WasLidOpenedRecently());
202 OpenLidToAngle(355.0f);
203 EXPECT_TRUE(IsMaximizeModeStarted());
206 // Verify the WasLidOpenedRecently signal with respect to time.
207 TEST_F(MaximizeModeControllerTest, WasLidOpenedRecentlyOverTime) {
208 AttachTickClockForTest();
210 // No lid open time initially.
211 ASSERT_FALSE(WasLidOpenedRecently());
213 CloseLid();
214 EXPECT_FALSE(WasLidOpenedRecently());
216 OpenLid();
217 EXPECT_TRUE(WasLidOpenedRecently());
219 // 1 second after lid open.
220 AdvanceTickClock(base::TimeDelta::FromSeconds(1));
221 EXPECT_TRUE(WasLidOpenedRecently());
223 // 3 seconds after lid open.
224 AdvanceTickClock(base::TimeDelta::FromSeconds(2));
225 EXPECT_FALSE(WasLidOpenedRecently());
228 // Verify the maximize mode enter/exit thresholds for stable angles.
229 TEST_F(MaximizeModeControllerTest, StableHingeAnglesWithLidOpened) {
230 ASSERT_FALSE(IsMaximizeModeStarted());
231 ASSERT_FALSE(WasLidOpenedRecently());
233 OpenLidToAngle(180.0f);
234 EXPECT_FALSE(IsMaximizeModeStarted());
236 OpenLidToAngle(315.0f);
237 EXPECT_TRUE(IsMaximizeModeStarted());
239 OpenLidToAngle(180.0f);
240 EXPECT_TRUE(IsMaximizeModeStarted());
242 OpenLidToAngle(45.0f);
243 EXPECT_FALSE(IsMaximizeModeStarted());
245 OpenLidToAngle(270.0f);
246 EXPECT_TRUE(IsMaximizeModeStarted());
248 OpenLidToAngle(90.0f);
249 EXPECT_FALSE(IsMaximizeModeStarted());
252 // Verify the maximize mode state for unstable hinge angles when the lid is open
253 // but not recently.
254 TEST_F(MaximizeModeControllerTest, UnstableHingeAnglesWithLidOpened) {
255 AttachTickClockForTest();
257 ASSERT_FALSE(WasLidOpenedRecently());
258 ASSERT_FALSE(IsMaximizeModeStarted());
260 OpenLidToAngle(5.0f);
261 EXPECT_FALSE(IsMaximizeModeStarted());
263 OpenLidToAngle(355.0f);
264 EXPECT_TRUE(IsMaximizeModeStarted());
266 OpenLidToAngle(5.0f);
267 EXPECT_TRUE(IsMaximizeModeStarted());
270 // Tests that when the hinge is nearly vertically aligned, the current state
271 // persists as the computed angle is highly inaccurate in this orientation.
272 TEST_F(MaximizeModeControllerTest, HingeAligned) {
273 // Laptop in normal orientation lid open 90 degrees.
274 TriggerBaseAndLidUpdate(gfx::Vector3dF(0.0f, 0.0f, -kMeanGravity),
275 gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
276 EXPECT_FALSE(IsMaximizeModeStarted());
278 // Completely vertical.
279 TriggerBaseAndLidUpdate(gfx::Vector3dF(kMeanGravity, 0.0f, 0.0f),
280 gfx::Vector3dF(kMeanGravity, 0.0f, 0.0f));
281 EXPECT_FALSE(IsMaximizeModeStarted());
283 // Close to vertical but with hinge appearing to be open 270 degrees.
284 TriggerBaseAndLidUpdate(gfx::Vector3dF(kMeanGravity, 0.0f, -0.1f),
285 gfx::Vector3dF(kMeanGravity, 0.1f, 0.0f));
286 EXPECT_FALSE(IsMaximizeModeStarted());
288 // Flat and open 270 degrees should start maximize mode.
289 TriggerBaseAndLidUpdate(gfx::Vector3dF(0.0f, 0.0f, -kMeanGravity),
290 gfx::Vector3dF(0.0f, kMeanGravity, 0.0f));
291 EXPECT_TRUE(IsMaximizeModeStarted());
293 // Normal 90 degree orientation but near vertical should stay in maximize
294 // mode.
295 TriggerBaseAndLidUpdate(gfx::Vector3dF(kMeanGravity, 0.0f, -0.1f),
296 gfx::Vector3dF(kMeanGravity, -0.1f, 0.0f));
297 EXPECT_TRUE(IsMaximizeModeStarted());
300 TEST_F(MaximizeModeControllerTest, LaptopTest) {
301 // Feeds in sample accelerometer data and verifies that there are no
302 // transitions into touchview / maximize mode while shaking the device around
303 // with the hinge at less than 180 degrees. Note the conversion from device
304 // data to accelerometer updates consistent with accelerometer_reader.cc.
305 ASSERT_EQ(0u, kAccelerometerLaptopModeTestDataLength % 6);
306 for (size_t i = 0; i < kAccelerometerLaptopModeTestDataLength / 6; ++i) {
307 gfx::Vector3dF base(-kAccelerometerLaptopModeTestData[i * 6 + 1],
308 -kAccelerometerLaptopModeTestData[i * 6],
309 -kAccelerometerLaptopModeTestData[i * 6 + 2]);
310 base.Scale(kMeanGravity);
311 gfx::Vector3dF lid(-kAccelerometerLaptopModeTestData[i * 6 + 4],
312 kAccelerometerLaptopModeTestData[i * 6 + 3],
313 kAccelerometerLaptopModeTestData[i * 6 + 5]);
314 lid.Scale(kMeanGravity);
315 TriggerBaseAndLidUpdate(base, lid);
316 // There are a lot of samples, so ASSERT rather than EXPECT to only generate
317 // one failure rather than potentially hundreds.
318 ASSERT_FALSE(IsMaximizeModeStarted());
322 TEST_F(MaximizeModeControllerTest, MaximizeModeTest) {
323 // Trigger maximize mode by opening to 270 to begin the test in maximize mode.
324 TriggerBaseAndLidUpdate(gfx::Vector3dF(0.0f, 0.0f, kMeanGravity),
325 gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
326 ASSERT_TRUE(IsMaximizeModeStarted());
328 // Feeds in sample accelerometer data and verifies that there are no
329 // transitions out of touchview / maximize mode while shaking the device
330 // around. Note the conversion from device data to accelerometer updates
331 // consistent with accelerometer_reader.cc.
332 ASSERT_EQ(0u, kAccelerometerFullyOpenTestDataLength % 6);
333 for (size_t i = 0; i < kAccelerometerFullyOpenTestDataLength / 6; ++i) {
334 gfx::Vector3dF base(-kAccelerometerFullyOpenTestData[i * 6 + 1],
335 -kAccelerometerFullyOpenTestData[i * 6],
336 -kAccelerometerFullyOpenTestData[i * 6 + 2]);
337 base.Scale(kMeanGravity);
338 gfx::Vector3dF lid(-kAccelerometerFullyOpenTestData[i * 6 + 4],
339 kAccelerometerFullyOpenTestData[i * 6 + 3],
340 kAccelerometerFullyOpenTestData[i * 6 + 5]);
341 lid.Scale(kMeanGravity);
342 TriggerBaseAndLidUpdate(base, lid);
343 // There are a lot of samples, so ASSERT rather than EXPECT to only generate
344 // one failure rather than potentially hundreds.
345 ASSERT_TRUE(IsMaximizeModeStarted());
349 // Tests that CanEnterMaximizeMode returns false until a valid accelerometer
350 // event has been received, and that it returns true afterwards.
351 TEST_F(MaximizeModeControllerTest,
352 CanEnterMaximizeModeRequiresValidAccelerometerUpdate) {
353 // Should be false until an accelerometer event is sent.
354 ASSERT_FALSE(maximize_mode_controller()->CanEnterMaximizeMode());
355 OpenLidToAngle(90.0f);
356 EXPECT_TRUE(maximize_mode_controller()->CanEnterMaximizeMode());
359 // Tests that when an accelerometer event is received which has no keyboard that
360 // we enter maximize mode.
361 TEST_F(MaximizeModeControllerTest,
362 NoKeyboardAccelerometerTriggersMaximizeMode) {
363 ASSERT_FALSE(IsMaximizeModeStarted());
364 TriggerLidUpdate(gfx::Vector3dF(0.0f, 0.0f, kMeanGravity));
365 ASSERT_TRUE(IsMaximizeModeStarted());
368 class MaximizeModeControllerSwitchesTest : public MaximizeModeControllerTest {
369 public:
370 MaximizeModeControllerSwitchesTest() {}
371 ~MaximizeModeControllerSwitchesTest() override {}
373 void SetUp() override {
374 base::CommandLine::ForCurrentProcess()->AppendSwitch(
375 switches::kAshEnableTouchViewTesting);
376 MaximizeModeControllerTest::SetUp();
378 private:
379 DISALLOW_COPY_AND_ASSIGN(MaximizeModeControllerSwitchesTest);
382 // Tests that when the command line switch for testing maximize mode is on, that
383 // accelerometer updates which would normally cause it to exit do not.
384 TEST_F(MaximizeModeControllerSwitchesTest, IgnoreHingeAngles) {
385 maximize_mode_controller()->EnableMaximizeModeWindowManager(true);
387 // Would normally trigger an exit from maximize mode.
388 OpenLidToAngle(90.0f);
389 EXPECT_TRUE(IsMaximizeModeStarted());
392 } // namespace ash