blob: c1009dbc9caf4c6957ff89c606f9ed5ec3aff02e [file] [log] [blame]
jonross0af45212015-01-13 18:55:461// 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.
4
Mitsuru Oshimaddfde3552018-03-22 09:12:135#include "ash/display/screen_orientation_controller.h"
dchenga94547472016-04-08 08:41:116
7#include <memory>
bruthigf57a7bba2015-04-24 21:19:318#include <vector>
9
Steven Bennetts368a1c82019-01-11 20:45:2210#include "ash/accelerometer/accelerometer_reader.h"
11#include "ash/accelerometer/accelerometer_types.h"
Mitsuru Oshimaddfde3552018-03-22 09:12:1312#include "ash/display/screen_orientation_controller.h"
James Cook346c8c62017-07-18 19:38:3713#include "ash/display/screen_orientation_controller_test_api.h"
Scott Violet1be03062017-06-26 18:22:1714#include "ash/public/cpp/app_types.h"
Evan Stade7d0e8232017-10-04 02:01:3615#include "ash/public/cpp/ash_switches.h"
jonross0af45212015-01-13 18:55:4616#include "ash/shell.h"
James Cooka35a1e22017-04-08 02:33:0817#include "ash/system/screen_layout_observer.h"
jonross0af45212015-01-13 18:55:4618#include "ash/test/ash_test_base.h"
19#include "ash/test/ash_test_helper.h"
James Cook346c8c62017-07-18 19:38:3720#include "ash/test_shell_delegate.h"
Scott Violet8d5bc4e2018-12-18 01:48:0121#include "ash/window_factory.h"
Mitsuru Oshima0e9b7a62017-07-19 18:23:0322#include "ash/wm/tablet_mode/tablet_mode_controller.h"
James Cookb0bf8e82017-04-09 17:01:4423#include "ash/wm/window_state.h"
jonross0af45212015-01-13 18:55:4624#include "base/command_line.h"
varkhac8621dd2017-05-31 19:09:0125#include "ui/aura/client/aura_constants.h"
jonross31c4a4822015-01-23 15:56:3326#include "ui/aura/window.h"
Scott Violet8d5bc4e2018-12-18 01:48:0127#include "ui/compositor/layer_type.h"
oshimaf84b0da722016-04-27 19:47:1928#include "ui/display/display.h"
rjkroege03f3d6b2016-09-09 21:27:5929#include "ui/display/display_switches.h"
rjkroege72f8154f2016-10-29 00:49:0230#include "ui/display/manager/display_manager.h"
rjkroege259c01882016-08-30 19:29:5031#include "ui/display/manager/managed_display_info.h"
rjkroege72f8154f2016-10-29 00:49:0232#include "ui/display/test/display_manager_test_api.h"
jonross0af45212015-01-13 18:55:4633#include "ui/message_center/message_center.h"
jonross31c4a4822015-01-23 15:56:3334#include "ui/wm/public/activation_client.h"
jonross0af45212015-01-13 18:55:4635
36namespace ash {
jonross0af45212015-01-13 18:55:4637namespace {
38
39const float kDegreesToRadians = 3.1415926f / 180.0f;
jonross01d453e2015-08-28 15:06:3040const float kMeanGravity = -9.8066f;
jonross0af45212015-01-13 18:55:4641
rjkroege259c01882016-08-30 19:29:5042display::ManagedDisplayInfo CreateDisplayInfo(int64_t id,
43 const gfx::Rect& bounds) {
44 display::ManagedDisplayInfo info(id, "dummy", false);
bruthigf57a7bba2015-04-24 21:19:3145 info.SetBounds(bounds);
46 return info;
47}
48
Mitsuru Oshima0e9b7a62017-07-19 18:23:0349void EnableTabletMode(bool enable) {
50 Shell::Get()->tablet_mode_controller()->EnableTabletModeWindowManager(enable);
jonross0af45212015-01-13 18:55:4651}
52
jonross0af45212015-01-13 18:55:4653bool RotationLocked() {
skycb4be5b2017-04-06 17:52:4554 return Shell::Get()->screen_orientation_controller()->rotation_locked();
jonross0af45212015-01-13 18:55:4655}
56
oshimaa5176fd72017-03-24 00:09:0557bool UserRotationLocked() {
skycb4be5b2017-04-06 17:52:4558 return Shell::Get()->screen_orientation_controller()->user_rotation_locked();
oshimaa5176fd72017-03-24 00:09:0559}
60
avidb567a8a2015-12-20 17:07:2461void SetDisplayRotationById(int64_t display_id,
oshimaf84b0da722016-04-27 19:47:1962 display::Display::Rotation rotation) {
skycb4be5b2017-04-06 17:52:4563 Shell::Get()->display_manager()->SetDisplayRotation(
Miguel Casas8f12ad62018-02-20 15:19:5964 display_id, rotation, display::Display::RotationSource::USER);
oshima8130b67e2015-07-23 16:39:1265}
66
oshimaf84b0da722016-04-27 19:47:1967void SetInternalDisplayRotation(display::Display::Rotation rotation) {
68 SetDisplayRotationById(display::Display::InternalDisplayId(), rotation);
jonross0af45212015-01-13 18:55:4669}
70
jonross0af45212015-01-13 18:55:4671void TriggerLidUpdate(const gfx::Vector3dF& lid) {
Steven Bennetts368a1c82019-01-11 20:45:2272 scoped_refptr<AccelerometerUpdate> update(new AccelerometerUpdate());
73 update->Set(ACCELEROMETER_SOURCE_SCREEN, lid.x(), lid.y(), lid.z());
skycb4be5b2017-04-06 17:52:4574 Shell::Get()->screen_orientation_controller()->OnAccelerometerUpdated(update);
jonross0af45212015-01-13 18:55:4675}
76
Scott Violet8d5bc4e2018-12-18 01:48:0177// Shows |child| and adds |child| to |parent|.
78void AddWindowAndShow(aura::Window* child, aura::Window* parent) {
79 child->Show();
80 parent->AddChild(child);
jonross31c4a4822015-01-23 15:56:3381}
82
Scott Violet8d5bc4e2018-12-18 01:48:0183// Adds |child| to |parent| and activates |parent|.
84void AddWindowAndActivateParent(aura::Window* child, aura::Window* parent) {
85 AddWindowAndShow(child, parent);
skycb4be5b2017-04-06 17:52:4586 Shell::Get()->activation_client()->ActivateWindow(parent);
jonross31c4a4822015-01-23 15:56:3387}
88
Scott Violet8d5bc4e2018-12-18 01:48:0189void Lock(aura::Window* window, OrientationLockType orientation_lock) {
Evan Stadef553d082018-10-01 20:02:1890 Shell::Get()->screen_orientation_controller()->LockOrientationForWindow(
Scott Violet8d5bc4e2018-12-18 01:48:0191 window, orientation_lock);
Evan Stadef553d082018-10-01 20:02:1892}
93
Scott Violet8d5bc4e2018-12-18 01:48:0194void Unlock(aura::Window* window) {
Evan Stadef553d082018-10-01 20:02:1895 Shell::Get()->screen_orientation_controller()->UnlockOrientationForWindow(
Scott Violet8d5bc4e2018-12-18 01:48:0196 window);
97}
98
99// Creates a window of type WINDOW_TYPE_CONTROL.
100std::unique_ptr<aura::Window> CreateControlWindow() {
101 std::unique_ptr<aura::Window> window = window_factory::NewWindow(
102 nullptr, aura::client::WindowType::WINDOW_TYPE_CONTROL);
103 window->Init(ui::LAYER_NOT_DRAWN);
104 window->set_owned_by_parent(false);
105 return window;
Evan Stadef553d082018-10-01 20:02:18106}
107
jonross0af45212015-01-13 18:55:46108} // namespace
109
James Cook317781a2017-07-18 02:08:06110class ScreenOrientationControllerTest : public AshTestBase {
jonross0af45212015-01-13 18:55:46111 public:
Scott Violet8d5bc4e2018-12-18 01:48:01112 ScreenOrientationControllerTest() = default;
113 ~ScreenOrientationControllerTest() override = default;
jonross0af45212015-01-13 18:55:46114
James Cook317781a2017-07-18 02:08:06115 // AshTestBase:
Scott Violet8d5bc4e2018-12-18 01:48:01116 void SetUp() override {
117 base::CommandLine::ForCurrentProcess()->AppendSwitch(
118 ::switches::kUseFirstDisplayAsInternal);
119 AshTestBase::SetUp();
120 }
jonross0af45212015-01-13 18:55:46121
oshimaa5176fd72017-03-24 00:09:05122 protected:
123 aura::Window* CreateAppWindowInShellWithId(int id) {
124 aura::Window* window = CreateTestWindowInShellWithId(id);
varkhac8621dd2017-05-31 19:09:01125 window->SetProperty(aura::client::kAppType,
126 static_cast<int>(AppType::CHROME_APP));
oshimaa5176fd72017-03-24 00:09:05127 return window;
128 }
129
130 void SetSystemRotationLocked(bool rotation_locked) {
James Cook317781a2017-07-18 02:08:06131 ScreenOrientationControllerTestApi(
skycb4be5b2017-04-06 17:52:45132 Shell::Get()->screen_orientation_controller())
oshimaa5176fd72017-03-24 00:09:05133 .SetRotationLocked(rotation_locked);
134 }
135
136 void SetUserRotationLocked(bool rotation_locked) {
skycb4be5b2017-04-06 17:52:45137 if (Shell::Get()->screen_orientation_controller()->user_rotation_locked() !=
138 rotation_locked) {
139 Shell::Get()->screen_orientation_controller()->ToggleUserRotationLock();
oshimaa5176fd72017-03-24 00:09:05140 }
141 }
142
Mitsuru Oshimac6b51112018-03-19 21:05:29143 OrientationLockType UserLockedOrientation() const {
James Cook317781a2017-07-18 02:08:06144 ScreenOrientationControllerTestApi test_api(
skycb4be5b2017-04-06 17:52:45145 Shell::Get()->screen_orientation_controller());
oshimaa5176fd72017-03-24 00:09:05146 return test_api.UserLockedOrientation();
147 }
148
jonross0af45212015-01-13 18:55:46149 private:
jonross0af45212015-01-13 18:55:46150 DISALLOW_COPY_AND_ASSIGN(ScreenOrientationControllerTest);
151};
152
Scott Violet8d5bc4e2018-12-18 01:48:01153// Tests that a Window can lock rotation.
jonross0af45212015-01-13 18:55:46154TEST_F(ScreenOrientationControllerTest, LockOrientation) {
Mitsuru Oshima850d9722018-03-21 21:24:20155 EnableTabletMode(true);
156
Scott Violet8d5bc4e2018-12-18 01:48:01157 std::unique_ptr<aura::Window> child_window = CreateControlWindow();
oshimaa5176fd72017-03-24 00:09:05158 std::unique_ptr<aura::Window> focus_window(CreateAppWindowInShellWithId(0));
oshimaf84b0da722016-04-27 19:47:19159 ASSERT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46160 ASSERT_FALSE(RotationLocked());
161
Scott Violet8d5bc4e2018-12-18 01:48:01162 AddWindowAndActivateParent(child_window.get(), focus_window.get());
163 Lock(child_window.get(), OrientationLockType::kLandscape);
oshimaf84b0da722016-04-27 19:47:19164 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46165 EXPECT_TRUE(RotationLocked());
166}
167
Scott Violet8d5bc4e2018-12-18 01:48:01168// Tests that a Window can unlock rotation.
jonross0af45212015-01-13 18:55:46169TEST_F(ScreenOrientationControllerTest, Unlock) {
Mitsuru Oshima850d9722018-03-21 21:24:20170 EnableTabletMode(true);
171
Scott Violet8d5bc4e2018-12-18 01:48:01172 std::unique_ptr<aura::Window> child_window = CreateControlWindow();
oshimaa5176fd72017-03-24 00:09:05173 std::unique_ptr<aura::Window> focus_window(CreateAppWindowInShellWithId(0));
oshimaf84b0da722016-04-27 19:47:19174 ASSERT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46175 ASSERT_FALSE(RotationLocked());
176
Scott Violet8d5bc4e2018-12-18 01:48:01177 AddWindowAndActivateParent(child_window.get(), focus_window.get());
178 Lock(child_window.get(), OrientationLockType::kLandscape);
oshimaf84b0da722016-04-27 19:47:19179 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46180 EXPECT_TRUE(RotationLocked());
181
Scott Violet8d5bc4e2018-12-18 01:48:01182 Unlock(child_window.get());
jonross0af45212015-01-13 18:55:46183 EXPECT_FALSE(RotationLocked());
184}
185
Scott Violet8d5bc4e2018-12-18 01:48:01186// Tests that a Window is able to change the orientation of the display after
187// having locked rotation.
jonross0af45212015-01-13 18:55:46188TEST_F(ScreenOrientationControllerTest, OrientationChanges) {
Mitsuru Oshima850d9722018-03-21 21:24:20189 EnableTabletMode(true);
190
Scott Violet8d5bc4e2018-12-18 01:48:01191 std::unique_ptr<aura::Window> child_window = CreateControlWindow();
oshimaa5176fd72017-03-24 00:09:05192 std::unique_ptr<aura::Window> focus_window(CreateAppWindowInShellWithId(0));
oshimaf84b0da722016-04-27 19:47:19193 ASSERT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46194 ASSERT_FALSE(RotationLocked());
195
Scott Violet8d5bc4e2018-12-18 01:48:01196 AddWindowAndActivateParent(child_window.get(), focus_window.get());
197 Lock(child_window.get(), OrientationLockType::kPortrait);
Mitsuru Oshima162e929b2018-01-23 16:56:27198 EXPECT_EQ(display::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46199 EXPECT_TRUE(RotationLocked());
200
Scott Violet8d5bc4e2018-12-18 01:48:01201 Lock(child_window.get(), OrientationLockType::kLandscape);
oshimaf84b0da722016-04-27 19:47:19202 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46203}
204
Scott Violet8d5bc4e2018-12-18 01:48:01205// Tests that orientation can only be set by the first Window that has set a
206// rotation lock.
jonross0af45212015-01-13 18:55:46207TEST_F(ScreenOrientationControllerTest, SecondContentCannotChangeOrientation) {
Mitsuru Oshima850d9722018-03-21 21:24:20208 EnableTabletMode(true);
209
Scott Violet8d5bc4e2018-12-18 01:48:01210 std::unique_ptr<aura::Window> child_window1 = CreateControlWindow();
211 std::unique_ptr<aura::Window> child_window2 = CreateControlWindow();
oshimaa5176fd72017-03-24 00:09:05212 std::unique_ptr<aura::Window> focus_window1(CreateAppWindowInShellWithId(0));
213 std::unique_ptr<aura::Window> focus_window2(CreateAppWindowInShellWithId(1));
jonross0af45212015-01-13 18:55:46214
Scott Violet8d5bc4e2018-12-18 01:48:01215 AddWindowAndActivateParent(child_window1.get(), focus_window1.get());
216 AddWindowAndShow(child_window2.get(), focus_window2.get());
217 Lock(child_window1.get(), OrientationLockType::kLandscape);
218 Lock(child_window2.get(), OrientationLockType::kPortrait);
oshimaf84b0da722016-04-27 19:47:19219 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46220}
221
Scott Violet8d5bc4e2018-12-18 01:48:01222// Tests that only the Window that set a rotation lock can perform an unlock.
jonross0af45212015-01-13 18:55:46223TEST_F(ScreenOrientationControllerTest, SecondContentCannotUnlock) {
Mitsuru Oshima850d9722018-03-21 21:24:20224 EnableTabletMode(true);
225
Scott Violet8d5bc4e2018-12-18 01:48:01226 std::unique_ptr<aura::Window> child_window1 = CreateControlWindow();
227 std::unique_ptr<aura::Window> child_window2 = CreateControlWindow();
oshimaa5176fd72017-03-24 00:09:05228 std::unique_ptr<aura::Window> focus_window1(CreateAppWindowInShellWithId(0));
229 std::unique_ptr<aura::Window> focus_window2(CreateAppWindowInShellWithId(1));
jonross0af45212015-01-13 18:55:46230
Scott Violet8d5bc4e2018-12-18 01:48:01231 AddWindowAndActivateParent(child_window1.get(), focus_window1.get());
232 AddWindowAndShow(child_window2.get(), focus_window2.get());
233 Lock(child_window1.get(), OrientationLockType::kLandscape);
234 Unlock(child_window2.get());
jonross0af45212015-01-13 18:55:46235 EXPECT_TRUE(RotationLocked());
236}
237
Scott Violet8d5bc4e2018-12-18 01:48:01238// Tests that a rotation lock is applied only while the Window are a part of the
239// active window.
jonross31c4a4822015-01-23 15:56:33240TEST_F(ScreenOrientationControllerTest, ActiveWindowChangesUpdateLock) {
Mitsuru Oshima850d9722018-03-21 21:24:20241 EnableTabletMode(true);
242
Scott Violet8d5bc4e2018-12-18 01:48:01243 std::unique_ptr<aura::Window> child_window = CreateControlWindow();
oshimaa5176fd72017-03-24 00:09:05244 std::unique_ptr<aura::Window> focus_window1(CreateAppWindowInShellWithId(0));
245 std::unique_ptr<aura::Window> focus_window2(CreateAppWindowInShellWithId(1));
jonross31c4a4822015-01-23 15:56:33246
Scott Violet8d5bc4e2018-12-18 01:48:01247 AddWindowAndActivateParent(child_window.get(), focus_window1.get());
248 Lock(child_window.get(), OrientationLockType::kLandscape);
jonross31c4a4822015-01-23 15:56:33249 ASSERT_TRUE(RotationLocked());
250
Thiago Farina3b086a02017-05-30 22:32:50251 ::wm::ActivationClient* activation_client = Shell::Get()->activation_client();
jonross31c4a4822015-01-23 15:56:33252 activation_client->ActivateWindow(focus_window2.get());
253 EXPECT_FALSE(RotationLocked());
254
255 activation_client->ActivateWindow(focus_window1.get());
256 EXPECT_TRUE(RotationLocked());
257}
258
259// Tests that switching between windows with different orientation locks change
260// the orientation.
261TEST_F(ScreenOrientationControllerTest, ActiveWindowChangesUpdateOrientation) {
Mitsuru Oshima850d9722018-03-21 21:24:20262 EnableTabletMode(true);
263
Scott Violet8d5bc4e2018-12-18 01:48:01264 std::unique_ptr<aura::Window> child_window1 = CreateControlWindow();
265 std::unique_ptr<aura::Window> child_window2 = CreateControlWindow();
oshimaa5176fd72017-03-24 00:09:05266 std::unique_ptr<aura::Window> focus_window1(CreateAppWindowInShellWithId(0));
267 std::unique_ptr<aura::Window> focus_window2(CreateAppWindowInShellWithId(1));
Scott Violet8d5bc4e2018-12-18 01:48:01268 AddWindowAndActivateParent(child_window1.get(), focus_window1.get());
269 AddWindowAndShow(child_window2.get(), focus_window2.get());
jonross0af45212015-01-13 18:55:46270
Scott Violet8d5bc4e2018-12-18 01:48:01271 Lock(child_window1.get(), OrientationLockType::kLandscape);
272 Lock(child_window2.get(), OrientationLockType::kPortrait);
oshimaf84b0da722016-04-27 19:47:19273 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross31c4a4822015-01-23 15:56:33274
Thiago Farina3b086a02017-05-30 22:32:50275 ::wm::ActivationClient* activation_client = Shell::Get()->activation_client();
jonross31c4a4822015-01-23 15:56:33276 activation_client->ActivateWindow(focus_window2.get());
jonross0af45212015-01-13 18:55:46277 EXPECT_TRUE(RotationLocked());
Mitsuru Oshima162e929b2018-01-23 16:56:27278 EXPECT_EQ(display::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
jonross31c4a4822015-01-23 15:56:33279
280 activation_client->ActivateWindow(focus_window1.get());
281 EXPECT_TRUE(RotationLocked());
oshimaf84b0da722016-04-27 19:47:19282 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross31c4a4822015-01-23 15:56:33283}
284
285// Tests that a rotation lock is removed when the setting window is hidden, and
286// that it is reapplied when the window becomes visible.
287TEST_F(ScreenOrientationControllerTest, VisibilityChangesLock) {
Mitsuru Oshima850d9722018-03-21 21:24:20288 EnableTabletMode(true);
289
Scott Violet8d5bc4e2018-12-18 01:48:01290 std::unique_ptr<aura::Window> child_window = CreateControlWindow();
oshimaa5176fd72017-03-24 00:09:05291 std::unique_ptr<aura::Window> focus_window(CreateAppWindowInShellWithId(0));
Scott Violet8d5bc4e2018-12-18 01:48:01292 AddWindowAndActivateParent(child_window.get(), focus_window.get());
293 Lock(child_window.get(), OrientationLockType::kLandscape);
jonross31c4a4822015-01-23 15:56:33294 EXPECT_TRUE(RotationLocked());
295
Scott Violet8d5bc4e2018-12-18 01:48:01296 child_window->Hide();
jonross31c4a4822015-01-23 15:56:33297 EXPECT_FALSE(RotationLocked());
298
Scott Violet8d5bc4e2018-12-18 01:48:01299 child_window->Show();
jonross31c4a4822015-01-23 15:56:33300 EXPECT_TRUE(RotationLocked());
301}
302
303// Tests that when a window is destroyed that its rotation lock is removed, and
304// window activations no longer change the lock
305TEST_F(ScreenOrientationControllerTest, WindowDestructionRemovesLock) {
Mitsuru Oshima850d9722018-03-21 21:24:20306 EnableTabletMode(true);
307
Scott Violet8d5bc4e2018-12-18 01:48:01308 std::unique_ptr<aura::Window> child_window = CreateControlWindow();
oshimaa5176fd72017-03-24 00:09:05309 std::unique_ptr<aura::Window> focus_window1(CreateAppWindowInShellWithId(0));
310 std::unique_ptr<aura::Window> focus_window2(CreateAppWindowInShellWithId(1));
jonross31c4a4822015-01-23 15:56:33311
Scott Violet8d5bc4e2018-12-18 01:48:01312 AddWindowAndActivateParent(child_window.get(), focus_window1.get());
313 Lock(child_window.get(), OrientationLockType::kLandscape);
jonross31c4a4822015-01-23 15:56:33314 ASSERT_TRUE(RotationLocked());
315
Scott Violet8d5bc4e2018-12-18 01:48:01316 focus_window1->RemoveChild(child_window.get());
317 child_window.reset();
jonross31c4a4822015-01-23 15:56:33318 EXPECT_FALSE(RotationLocked());
319
Thiago Farina3b086a02017-05-30 22:32:50320 ::wm::ActivationClient* activation_client = Shell::Get()->activation_client();
jonross31c4a4822015-01-23 15:56:33321 activation_client->ActivateWindow(focus_window2.get());
322 EXPECT_FALSE(RotationLocked());
323
324 activation_client->ActivateWindow(focus_window1.get());
325 EXPECT_FALSE(RotationLocked());
jonross0af45212015-01-13 18:55:46326}
327
328// Tests that accelerometer readings in each of the screen angles will trigger a
329// rotation of the internal display.
330TEST_F(ScreenOrientationControllerTest, DisplayRotation) {
Mitsuru Oshima0e9b7a62017-07-19 18:23:03331 EnableTabletMode(true);
Mitsuru Oshima850d9722018-03-21 21:24:20332
jonross0af45212015-01-13 18:55:46333 // Now test rotating in all directions.
334 TriggerLidUpdate(gfx::Vector3dF(-kMeanGravity, 0.0f, 0.0f));
oshimaf84b0da722016-04-27 19:47:19335 EXPECT_EQ(display::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46336 TriggerLidUpdate(gfx::Vector3dF(0.0f, kMeanGravity, 0.0f));
oshimaf84b0da722016-04-27 19:47:19337 EXPECT_EQ(display::Display::ROTATE_180, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46338 TriggerLidUpdate(gfx::Vector3dF(kMeanGravity, 0.0f, 0.0f));
oshimaf84b0da722016-04-27 19:47:19339 EXPECT_EQ(display::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46340 TriggerLidUpdate(gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
oshimaf84b0da722016-04-27 19:47:19341 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46342}
343
344// Tests that low angles are ignored by the accelerometer (i.e. when the device
345// is almost laying flat).
346TEST_F(ScreenOrientationControllerTest, RotationIgnoresLowAngles) {
Mitsuru Oshima0e9b7a62017-07-19 18:23:03347 EnableTabletMode(true);
Mitsuru Oshima850d9722018-03-21 21:24:20348
jonross0af45212015-01-13 18:55:46349 TriggerLidUpdate(gfx::Vector3dF(0.0f, -kMeanGravity, -kMeanGravity));
oshimaf84b0da722016-04-27 19:47:19350 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46351 TriggerLidUpdate(gfx::Vector3dF(-2.0f, 0.0f, -kMeanGravity));
oshimaf84b0da722016-04-27 19:47:19352 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46353 TriggerLidUpdate(gfx::Vector3dF(0.0f, 2.0f, -kMeanGravity));
oshimaf84b0da722016-04-27 19:47:19354 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46355 TriggerLidUpdate(gfx::Vector3dF(2.0f, 0.0f, -kMeanGravity));
oshimaf84b0da722016-04-27 19:47:19356 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46357 TriggerLidUpdate(gfx::Vector3dF(0.0f, -2.0f, -kMeanGravity));
oshimaf84b0da722016-04-27 19:47:19358 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46359}
360
361// Tests that the display will stick to the current orientation beyond the
362// halfway point, preventing frequent updates back and forth.
363TEST_F(ScreenOrientationControllerTest, RotationSticky) {
Mitsuru Oshima0e9b7a62017-07-19 18:23:03364 EnableTabletMode(true);
Mitsuru Oshima850d9722018-03-21 21:24:20365
jonross0af45212015-01-13 18:55:46366 gfx::Vector3dF gravity(0.0f, -kMeanGravity, 0.0f);
367 TriggerLidUpdate(gravity);
oshimaf84b0da722016-04-27 19:47:19368 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46369
370 // Turn past half-way point to next direction and rotation should remain
371 // the same.
372 float degrees = 50.0;
373 gravity.set_x(-sin(degrees * kDegreesToRadians) * kMeanGravity);
374 gravity.set_y(-cos(degrees * kDegreesToRadians) * kMeanGravity);
375 TriggerLidUpdate(gravity);
oshimaf84b0da722016-04-27 19:47:19376 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46377
378 // Turn more and the screen should rotate.
379 degrees = 70.0;
380 gravity.set_x(-sin(degrees * kDegreesToRadians) * kMeanGravity);
381 gravity.set_y(-cos(degrees * kDegreesToRadians) * kMeanGravity);
382 TriggerLidUpdate(gravity);
oshimaf84b0da722016-04-27 19:47:19383 EXPECT_EQ(display::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46384
385 // Turn back just beyond the half-way point and the new rotation should
386 // still be in effect.
387 degrees = 40.0;
388 gravity.set_x(-sin(degrees * kDegreesToRadians) * kMeanGravity);
389 gravity.set_y(-cos(degrees * kDegreesToRadians) * kMeanGravity);
390 TriggerLidUpdate(gravity);
oshimaf84b0da722016-04-27 19:47:19391 EXPECT_EQ(display::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46392}
393
394// Tests that the display will stick to its current orientation when the
395// rotation lock has been set.
396TEST_F(ScreenOrientationControllerTest, RotationLockPreventsRotation) {
Mitsuru Oshima0e9b7a62017-07-19 18:23:03397 EnableTabletMode(true);
oshimaa5176fd72017-03-24 00:09:05398 SetUserRotationLocked(true);
jonross0af45212015-01-13 18:55:46399
400 // Turn past the threshold for rotation.
401 float degrees = 90.0;
402 gfx::Vector3dF gravity(-sin(degrees * kDegreesToRadians) * kMeanGravity,
403 -cos(degrees * kDegreesToRadians) * kMeanGravity,
404 0.0f);
405 TriggerLidUpdate(gravity);
oshimaf84b0da722016-04-27 19:47:19406 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46407
oshimaa5176fd72017-03-24 00:09:05408 SetUserRotationLocked(false);
jonross0af45212015-01-13 18:55:46409 TriggerLidUpdate(gravity);
oshimaf84b0da722016-04-27 19:47:19410 EXPECT_EQ(display::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46411}
412
yiyixa68ade512016-09-01 17:38:13413// The ScreenLayoutObserver class that is responsible for adding/updating
414// MessageCenter notifications is only added to the SystemTray on ChromeOS.
jonross0af45212015-01-13 18:55:46415// Tests that the screen rotation notifications are suppressed when
416// triggered by the accelerometer.
417TEST_F(ScreenOrientationControllerTest, BlockRotationNotifications) {
Mitsuru Oshima0e9b7a62017-07-19 18:23:03418 EnableTabletMode(true);
skycb4be5b2017-04-06 17:52:45419 Shell::Get()->screen_layout_observer()->set_show_notifications_for_testing(
420 true);
rjkroege72f8154f2016-10-29 00:49:02421 display::test::DisplayManagerTestApi(display_manager())
rjkroege980ccfd2016-10-06 18:00:24422 .SetFirstDisplayAsInternalDisplay();
jonross0af45212015-01-13 18:55:46423
424 message_center::MessageCenter* message_center =
425 message_center::MessageCenter::Get();
426
427 EXPECT_EQ(0u, message_center->NotificationCount());
428 EXPECT_FALSE(message_center->HasPopupNotifications());
429
430 // Make sure notifications are still displayed when
Mitsuru Oshima0e9b7a62017-07-19 18:23:03431 // adjusting the screen rotation directly when in tablet mode
oshimaf84b0da722016-04-27 19:47:19432 ASSERT_NE(display::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
433 SetInternalDisplayRotation(display::Display::ROTATE_270);
oshimaa5176fd72017-03-24 00:09:05434 SetSystemRotationLocked(false);
oshimaf84b0da722016-04-27 19:47:19435 EXPECT_EQ(display::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46436 EXPECT_EQ(1u, message_center->NotificationCount());
437 EXPECT_TRUE(message_center->HasPopupNotifications());
438
439 // Clear all notifications
yoshiki35c9a3b2016-02-25 05:52:13440 message_center->RemoveAllNotifications(
441 false /* by_user */, message_center::MessageCenter::RemoveType::ALL);
jonross0af45212015-01-13 18:55:46442 EXPECT_EQ(0u, message_center->NotificationCount());
443 EXPECT_FALSE(message_center->HasPopupNotifications());
444
445 // Make sure notifications are blocked when adjusting the screen rotation
Mitsuru Oshima0e9b7a62017-07-19 18:23:03446 // via the accelerometer while in tablet mode
jonross0af45212015-01-13 18:55:46447 // Rotate the screen 90 degrees
oshimaa5176fd72017-03-24 00:09:05448 ASSERT_EQ(display::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46449 TriggerLidUpdate(gfx::Vector3dF(-kMeanGravity, 0.0f, 0.0f));
oshimaf84b0da722016-04-27 19:47:19450 ASSERT_EQ(display::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46451 EXPECT_EQ(0u, message_center->NotificationCount());
452 EXPECT_FALSE(message_center->HasPopupNotifications());
453
454 // Make sure notifications are still displayed when
Mitsuru Oshima0e9b7a62017-07-19 18:23:03455 // adjusting the screen rotation directly when not in tablet mode
456 EnableTabletMode(false);
jonross0af45212015-01-13 18:55:46457 // Reset the screen rotation.
oshimaf84b0da722016-04-27 19:47:19458 SetInternalDisplayRotation(display::Display::ROTATE_0);
jonross0af45212015-01-13 18:55:46459 // Clear all notifications
yoshiki35c9a3b2016-02-25 05:52:13460 message_center->RemoveAllNotifications(
461 false /* by_user */, message_center::MessageCenter::RemoveType::ALL);
oshimaf84b0da722016-04-27 19:47:19462 ASSERT_NE(display::Display::ROTATE_180, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46463 ASSERT_EQ(0u, message_center->NotificationCount());
464 ASSERT_FALSE(message_center->HasPopupNotifications());
oshimaf84b0da722016-04-27 19:47:19465 SetInternalDisplayRotation(display::Display::ROTATE_180);
466 EXPECT_EQ(display::Display::ROTATE_180, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46467 EXPECT_EQ(1u, message_center->NotificationCount());
468 EXPECT_TRUE(message_center->HasPopupNotifications());
469}
470
471// Tests that if a user has set a display rotation that it is restored upon
Mitsuru Oshima0e9b7a62017-07-19 18:23:03472// exiting tablet mode.
jonross0af45212015-01-13 18:55:46473TEST_F(ScreenOrientationControllerTest, ResetUserRotationUponExit) {
rjkroege72f8154f2016-10-29 00:49:02474 display::test::DisplayManagerTestApi(display_manager())
rjkroege980ccfd2016-10-06 18:00:24475 .SetFirstDisplayAsInternalDisplay();
oshima8130b67e2015-07-23 16:39:12476
oshimaf84b0da722016-04-27 19:47:19477 SetInternalDisplayRotation(display::Display::ROTATE_90);
Mitsuru Oshima0e9b7a62017-07-19 18:23:03478 EnableTabletMode(true);
jonross0af45212015-01-13 18:55:46479
480 TriggerLidUpdate(gfx::Vector3dF(0.0f, kMeanGravity, 0.0f));
oshimaf84b0da722016-04-27 19:47:19481 EXPECT_EQ(display::Display::ROTATE_180, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46482
Mitsuru Oshima0e9b7a62017-07-19 18:23:03483 EnableTabletMode(false);
oshimaf84b0da722016-04-27 19:47:19484 EXPECT_EQ(display::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46485}
486
jonross0af45212015-01-13 18:55:46487// Tests that if a user changes the display rotation, while rotation is locked,
Mitsuru Oshima0e9b7a62017-07-19 18:23:03488// that the updates are recorded. Upon exiting tablet mode the latest user
jonross0af45212015-01-13 18:55:46489// rotation should be applied.
490TEST_F(ScreenOrientationControllerTest, UpdateUserRotationWhileRotationLocked) {
Mitsuru Oshima0e9b7a62017-07-19 18:23:03491 EnableTabletMode(true);
oshimaf84b0da722016-04-27 19:47:19492 SetInternalDisplayRotation(display::Display::ROTATE_270);
jonross0af45212015-01-13 18:55:46493 // User sets rotation to the same rotation that the display was at when
Mitsuru Oshima0e9b7a62017-07-19 18:23:03494 // tablet mode was activated.
oshimaf84b0da722016-04-27 19:47:19495 SetInternalDisplayRotation(display::Display::ROTATE_0);
Mitsuru Oshima0e9b7a62017-07-19 18:23:03496 EnableTabletMode(false);
oshimaf84b0da722016-04-27 19:47:19497 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross0af45212015-01-13 18:55:46498}
499
jonross2d2c2fd62015-02-02 20:58:23500// Tests that when the orientation lock is set to Landscape, that rotation can
501// be done between the two angles of the orientation.
502TEST_F(ScreenOrientationControllerTest, LandscapeOrientationAllowsRotation) {
Scott Violet8d5bc4e2018-12-18 01:48:01503 std::unique_ptr<aura::Window> child_window = CreateControlWindow();
oshimaa5176fd72017-03-24 00:09:05504 std::unique_ptr<aura::Window> focus_window(CreateAppWindowInShellWithId(0));
Mitsuru Oshima0e9b7a62017-07-19 18:23:03505 EnableTabletMode(true);
jonross2d2c2fd62015-02-02 20:58:23506
Scott Violet8d5bc4e2018-12-18 01:48:01507 AddWindowAndActivateParent(child_window.get(), focus_window.get());
508 Lock(child_window.get(), OrientationLockType::kLandscape);
oshimaf84b0da722016-04-27 19:47:19509 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross2d2c2fd62015-02-02 20:58:23510 EXPECT_TRUE(RotationLocked());
511
512 // Inverse of orientation is allowed
513 TriggerLidUpdate(gfx::Vector3dF(0.0f, kMeanGravity, 0.0f));
oshimaf84b0da722016-04-27 19:47:19514 EXPECT_EQ(display::Display::ROTATE_180, GetCurrentInternalDisplayRotation());
jonross2d2c2fd62015-02-02 20:58:23515
516 // Display rotations between are not allowed
517 TriggerLidUpdate(gfx::Vector3dF(kMeanGravity, 0.0f, 0.0f));
oshimaf84b0da722016-04-27 19:47:19518 EXPECT_EQ(display::Display::ROTATE_180, GetCurrentInternalDisplayRotation());
jonross2d2c2fd62015-02-02 20:58:23519 TriggerLidUpdate(gfx::Vector3dF(-kMeanGravity, 0.0f, 0.0f));
oshimaf84b0da722016-04-27 19:47:19520 EXPECT_EQ(display::Display::ROTATE_180, GetCurrentInternalDisplayRotation());
jonross2d2c2fd62015-02-02 20:58:23521}
522
Evan Stadef553d082018-10-01 20:02:18523// Tests that when the orientation lock is set to Portrait, that rotation can be
jonross2d2c2fd62015-02-02 20:58:23524// done between the two angles of the orientation.
525TEST_F(ScreenOrientationControllerTest, PortraitOrientationAllowsRotation) {
Scott Violet8d5bc4e2018-12-18 01:48:01526 std::unique_ptr<aura::Window> child_window = CreateControlWindow();
oshimaa5176fd72017-03-24 00:09:05527 std::unique_ptr<aura::Window> focus_window(CreateAppWindowInShellWithId(0));
Mitsuru Oshima0e9b7a62017-07-19 18:23:03528 EnableTabletMode(true);
jonross2d2c2fd62015-02-02 20:58:23529
Scott Violet8d5bc4e2018-12-18 01:48:01530 AddWindowAndActivateParent(child_window.get(), focus_window.get());
531 Lock(child_window.get(), OrientationLockType::kPortrait);
Mitsuru Oshima162e929b2018-01-23 16:56:27532 EXPECT_EQ(display::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
jonross2d2c2fd62015-02-02 20:58:23533 EXPECT_TRUE(RotationLocked());
534
535 // Inverse of orientation is allowed
Mitsuru Oshima162e929b2018-01-23 16:56:27536 TriggerLidUpdate(gfx::Vector3dF(-kMeanGravity, 0.0f, 0.0f));
537 EXPECT_EQ(display::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
jonross2d2c2fd62015-02-02 20:58:23538
539 // Display rotations between are not allowed
540 TriggerLidUpdate(gfx::Vector3dF(0.0f, kMeanGravity, 0.0f));
Mitsuru Oshima162e929b2018-01-23 16:56:27541 EXPECT_EQ(display::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
jonross2d2c2fd62015-02-02 20:58:23542 TriggerLidUpdate(gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
Mitsuru Oshima162e929b2018-01-23 16:56:27543 EXPECT_EQ(display::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
jonross2d2c2fd62015-02-02 20:58:23544}
545
546// Tests that for an orientation lock which does not allow rotation, that the
547// display rotation remains constant.
548TEST_F(ScreenOrientationControllerTest, OrientationLockDisallowsRotation) {
Scott Violet8d5bc4e2018-12-18 01:48:01549 std::unique_ptr<aura::Window> child_window = CreateControlWindow();
oshimaa5176fd72017-03-24 00:09:05550 std::unique_ptr<aura::Window> focus_window(CreateAppWindowInShellWithId(0));
Mitsuru Oshima0e9b7a62017-07-19 18:23:03551 EnableTabletMode(true);
jonross2d2c2fd62015-02-02 20:58:23552
Scott Violet8d5bc4e2018-12-18 01:48:01553 AddWindowAndActivateParent(child_window.get(), focus_window.get());
554 Lock(child_window.get(), OrientationLockType::kPortraitPrimary);
Mitsuru Oshima162e929b2018-01-23 16:56:27555 EXPECT_EQ(display::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
jonross2d2c2fd62015-02-02 20:58:23556 EXPECT_TRUE(RotationLocked());
557
558 // Rotation does not change.
559 TriggerLidUpdate(gfx::Vector3dF(kMeanGravity, 0.0f, 0.0f));
Mitsuru Oshima162e929b2018-01-23 16:56:27560 EXPECT_EQ(display::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
jonross2d2c2fd62015-02-02 20:58:23561 TriggerLidUpdate(gfx::Vector3dF(0.0f, kMeanGravity, 0.0f));
Mitsuru Oshima162e929b2018-01-23 16:56:27562 EXPECT_EQ(display::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
jonross2d2c2fd62015-02-02 20:58:23563 TriggerLidUpdate(gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
Mitsuru Oshima162e929b2018-01-23 16:56:27564 EXPECT_EQ(display::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
jonross2d2c2fd62015-02-02 20:58:23565}
566
Scott Violet8d5bc4e2018-12-18 01:48:01567// Tests that after a Window has applied an orientation lock which supports
568// rotation, that a user rotation lock does not allow rotation.
jonross2d2c2fd62015-02-02 20:58:23569TEST_F(ScreenOrientationControllerTest, UserRotationLockDisallowsRotation) {
Scott Violet8d5bc4e2018-12-18 01:48:01570 std::unique_ptr<aura::Window> child_window = CreateControlWindow();
oshimaa5176fd72017-03-24 00:09:05571 std::unique_ptr<aura::Window> focus_window(CreateAppWindowInShellWithId(0));
Mitsuru Oshima0e9b7a62017-07-19 18:23:03572 EnableTabletMode(true);
jonross2d2c2fd62015-02-02 20:58:23573
Scott Violet8d5bc4e2018-12-18 01:48:01574 AddWindowAndActivateParent(child_window.get(), focus_window.get());
575 Lock(child_window.get(), OrientationLockType::kLandscape);
576 Unlock(child_window.get());
jonross2d2c2fd62015-02-02 20:58:23577
oshimaa5176fd72017-03-24 00:09:05578 SetUserRotationLocked(true);
jonross2d2c2fd62015-02-02 20:58:23579 EXPECT_TRUE(RotationLocked());
oshimaa5176fd72017-03-24 00:09:05580 EXPECT_TRUE(UserRotationLocked());
jonross2d2c2fd62015-02-02 20:58:23581
oshimaa5176fd72017-03-24 00:09:05582 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross2d2c2fd62015-02-02 20:58:23583 TriggerLidUpdate(gfx::Vector3dF(0.0f, kMeanGravity, 0.0f));
oshimaf84b0da722016-04-27 19:47:19584 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
jonross2d2c2fd62015-02-02 20:58:23585}
586
thestig259626c2015-11-23 20:18:11587// Verifies rotating an inactive Display is successful.
bruthigf57a7bba2015-04-24 21:19:31588TEST_F(ScreenOrientationControllerTest, RotateInactiveDisplay) {
avidb567a8a2015-12-20 17:07:24589 const int64_t kInternalDisplayId = 9;
590 const int64_t kExternalDisplayId = 10;
oshimaf84b0da722016-04-27 19:47:19591 const display::Display::Rotation kNewRotation = display::Display::ROTATE_180;
bruthigf57a7bba2015-04-24 21:19:31592
rjkroege259c01882016-08-30 19:29:50593 const display::ManagedDisplayInfo internal_display_info =
bruthigf57a7bba2015-04-24 21:19:31594 CreateDisplayInfo(kInternalDisplayId, gfx::Rect(0, 0, 500, 500));
rjkroege259c01882016-08-30 19:29:50595 const display::ManagedDisplayInfo external_display_info =
bruthigf57a7bba2015-04-24 21:19:31596 CreateDisplayInfo(kExternalDisplayId, gfx::Rect(1, 1, 500, 500));
597
rjkroege259c01882016-08-30 19:29:50598 std::vector<display::ManagedDisplayInfo> display_info_list_two_active;
bruthigf57a7bba2015-04-24 21:19:31599 display_info_list_two_active.push_back(internal_display_info);
600 display_info_list_two_active.push_back(external_display_info);
601
rjkroege259c01882016-08-30 19:29:50602 std::vector<display::ManagedDisplayInfo> display_info_list_one_active;
bruthigf57a7bba2015-04-24 21:19:31603 display_info_list_one_active.push_back(external_display_info);
604
rjkroege259c01882016-08-30 19:29:50605 // The display::ManagedDisplayInfo list with two active displays needs to be
606 // added first so that the DisplayManager can track the
607 // |internal_display_info| as inactive instead of non-existent.
rjkroege980ccfd2016-10-06 18:00:24608 display_manager()->UpdateDisplaysWith(display_info_list_two_active);
609 display_manager()->UpdateDisplaysWith(display_info_list_one_active);
bruthigf57a7bba2015-04-24 21:19:31610
rjkroege72f8154f2016-10-29 00:49:02611 display::test::ScopedSetInternalDisplayId set_internal(display_manager(),
612 kInternalDisplayId);
bruthigf57a7bba2015-04-24 21:19:31613
rjkroege980ccfd2016-10-06 18:00:24614 ASSERT_NE(kNewRotation, display_manager()
615 ->GetDisplayInfo(kInternalDisplayId)
616 .GetActiveRotation());
James Cook317781a2017-07-18 02:08:06617 ScreenOrientationControllerTestApi(
skycb4be5b2017-04-06 17:52:45618 Shell::Get()->screen_orientation_controller())
oshimaa5176fd72017-03-24 00:09:05619 .SetDisplayRotation(kNewRotation,
Miguel Casas8f12ad62018-02-20 15:19:59620 display::Display::RotationSource::ACTIVE);
bruthigf57a7bba2015-04-24 21:19:31621
rjkroege980ccfd2016-10-06 18:00:24622 EXPECT_EQ(kNewRotation, display_manager()
623 ->GetDisplayInfo(kInternalDisplayId)
624 .GetActiveRotation());
bruthigf57a7bba2015-04-24 21:19:31625}
626
oshimaa5176fd72017-03-24 00:09:05627TEST_F(ScreenOrientationControllerTest, UserRotationLockedOrientation) {
628 ScreenOrientationController* orientation_controller =
skycb4be5b2017-04-06 17:52:45629 Shell::Get()->screen_orientation_controller();
oshimaa5176fd72017-03-24 00:09:05630 orientation_controller->ToggleUserRotationLock();
631 EXPECT_TRUE(orientation_controller->user_rotation_locked());
Mitsuru Oshimac6b51112018-03-19 21:05:29632 EXPECT_EQ(OrientationLockType::kLandscapePrimary, UserLockedOrientation());
oshimaa5176fd72017-03-24 00:09:05633
634 orientation_controller->ToggleUserRotationLock();
Mitsuru Oshima162e929b2018-01-23 16:56:27635 SetInternalDisplayRotation(display::Display::ROTATE_270);
oshimaa5176fd72017-03-24 00:09:05636 orientation_controller->ToggleUserRotationLock();
Mitsuru Oshimac6b51112018-03-19 21:05:29637 EXPECT_EQ(OrientationLockType::kPortraitPrimary, UserLockedOrientation());
oshimaa5176fd72017-03-24 00:09:05638
639 orientation_controller->ToggleUserRotationLock();
640 SetInternalDisplayRotation(display::Display::ROTATE_180);
641 orientation_controller->ToggleUserRotationLock();
Mitsuru Oshimac6b51112018-03-19 21:05:29642 EXPECT_EQ(OrientationLockType::kLandscapeSecondary, UserLockedOrientation());
oshimaa5176fd72017-03-24 00:09:05643
644 orientation_controller->ToggleUserRotationLock();
Mitsuru Oshima162e929b2018-01-23 16:56:27645 SetInternalDisplayRotation(display::Display::ROTATE_90);
oshimaa5176fd72017-03-24 00:09:05646 orientation_controller->ToggleUserRotationLock();
Mitsuru Oshimac6b51112018-03-19 21:05:29647 EXPECT_EQ(OrientationLockType::kPortraitSecondary, UserLockedOrientation());
oshimaa5176fd72017-03-24 00:09:05648 orientation_controller->ToggleUserRotationLock();
649
650 SetInternalDisplayRotation(display::Display::ROTATE_270);
651
652 UpdateDisplay("800x1280");
653 orientation_controller->ToggleUserRotationLock();
654 EXPECT_TRUE(orientation_controller->user_rotation_locked());
Mitsuru Oshimac6b51112018-03-19 21:05:29655 EXPECT_EQ(OrientationLockType::kPortraitPrimary, UserLockedOrientation());
oshimaa5176fd72017-03-24 00:09:05656
657 orientation_controller->ToggleUserRotationLock();
658 SetInternalDisplayRotation(display::Display::ROTATE_90);
659 orientation_controller->ToggleUserRotationLock();
Mitsuru Oshimac6b51112018-03-19 21:05:29660 EXPECT_EQ(OrientationLockType::kLandscapePrimary, UserLockedOrientation());
oshimaa5176fd72017-03-24 00:09:05661
662 orientation_controller->ToggleUserRotationLock();
663 SetInternalDisplayRotation(display::Display::ROTATE_180);
664 orientation_controller->ToggleUserRotationLock();
Mitsuru Oshimac6b51112018-03-19 21:05:29665 EXPECT_EQ(OrientationLockType::kPortraitSecondary, UserLockedOrientation());
oshimaa5176fd72017-03-24 00:09:05666
667 orientation_controller->ToggleUserRotationLock();
668 SetInternalDisplayRotation(display::Display::ROTATE_270);
669 orientation_controller->ToggleUserRotationLock();
Mitsuru Oshimac6b51112018-03-19 21:05:29670 EXPECT_EQ(OrientationLockType::kLandscapeSecondary, UserLockedOrientation());
oshimaa5176fd72017-03-24 00:09:05671 orientation_controller->ToggleUserRotationLock();
672}
673
674TEST_F(ScreenOrientationControllerTest, UserRotationLock) {
Mitsuru Oshima850d9722018-03-21 21:24:20675 EnableTabletMode(true);
676
Scott Violet8d5bc4e2018-12-18 01:48:01677 std::unique_ptr<aura::Window> child_window1 = CreateControlWindow();
678 std::unique_ptr<aura::Window> child_window2 = CreateControlWindow();
oshimaa5176fd72017-03-24 00:09:05679 std::unique_ptr<aura::Window> focus_window1(CreateAppWindowInShellWithId(0));
680 std::unique_ptr<aura::Window> focus_window2(CreateAppWindowInShellWithId(1));
oshimaa5176fd72017-03-24 00:09:05681
Scott Violet8d5bc4e2018-12-18 01:48:01682 AddWindowAndActivateParent(child_window2.get(), focus_window2.get());
683 AddWindowAndActivateParent(child_window1.get(), focus_window1.get());
oshimaa5176fd72017-03-24 00:09:05684
685 ASSERT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
686 ASSERT_FALSE(RotationLocked());
687 ASSERT_FALSE(UserRotationLocked());
688
689 ScreenOrientationController* orientation_controller =
skycb4be5b2017-04-06 17:52:45690 Shell::Get()->screen_orientation_controller();
oshimaa5176fd72017-03-24 00:09:05691 ASSERT_FALSE(orientation_controller->user_rotation_locked());
692 orientation_controller->ToggleUserRotationLock();
693 ASSERT_TRUE(orientation_controller->user_rotation_locked());
694
Scott Violet8d5bc4e2018-12-18 01:48:01695 Lock(child_window1.get(), OrientationLockType::kPortrait);
oshimaa5176fd72017-03-24 00:09:05696
Mitsuru Oshima162e929b2018-01-23 16:56:27697 EXPECT_EQ(display::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
oshimaa5176fd72017-03-24 00:09:05698
Thiago Farina3b086a02017-05-30 22:32:50699 ::wm::ActivationClient* activation_client = Shell::Get()->activation_client();
oshimaa5176fd72017-03-24 00:09:05700 // Activating any will switch to the natural orientation.
701 activation_client->ActivateWindow(focus_window2.get());
702 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
703
704 // Activating the portrait window will rotate to the portrait.
705 activation_client->ActivateWindow(focus_window1.get());
Mitsuru Oshima162e929b2018-01-23 16:56:27706 EXPECT_EQ(display::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
oshimaa5176fd72017-03-24 00:09:05707
708 // User locked to the 90 dig.
709 orientation_controller->ToggleUserRotationLock();
710 orientation_controller->ToggleUserRotationLock();
711
712 // Switching to Any orientation will stay to the user locked orientation.
713 activation_client->ActivateWindow(focus_window2.get());
Mitsuru Oshima162e929b2018-01-23 16:56:27714 EXPECT_EQ(display::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
oshimaa5176fd72017-03-24 00:09:05715
716 // Application forced to be landscape.
Scott Violet8d5bc4e2018-12-18 01:48:01717 Lock(child_window2.get(), OrientationLockType::kLandscape);
oshimaa5176fd72017-03-24 00:09:05718 EXPECT_EQ(display::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
719
Scott Violet8d5bc4e2018-12-18 01:48:01720 Lock(child_window1.get(), OrientationLockType::kAny);
oshimaa5176fd72017-03-24 00:09:05721 activation_client->ActivateWindow(focus_window1.get());
722 // Switching back to any will rotate to user rotation.
Mitsuru Oshima162e929b2018-01-23 16:56:27723 EXPECT_EQ(display::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
oshimaa5176fd72017-03-24 00:09:05724}
725
jonross0af45212015-01-13 18:55:46726} // namespace ash