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

Side by Side Diff: ash/content/display/screen_orientation_controller_chromeos_unittest.cc

Issue 759063002: Move Screen Rotation from MaximizeModeController to ScreenOrientationController (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactor Rotation from MaximizeModeController to ScreenOrientationController Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 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
5 #include "ash/accelerometer/accelerometer_controller.h"
6 #include "ash/ash_switches.h"
7 #include "ash/content/display/screen_orientation_controller_chromeos.h"
8 #include "ash/display/display_info.h"
9 #include "ash/display/display_manager.h"
10 #include "ash/shell.h"
11 #include "ash/test/ash_test_base.h"
12 #include "ash/test/ash_test_helper.h"
13 #include "ash/test/test_shell_delegate.h"
14 #include "ash/test/test_system_tray_delegate.h"
15 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
16 #include "base/command_line.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "content/public/browser/browser_context.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/test/test_browser_context.h"
21 #include "third_party/WebKit/public/platform/WebScreenOrientationLockType.h"
22 #include "ui/gfx/display.h"
23 #include "ui/message_center/message_center.h"
24 #include "ui/views/test/webview_test_helper.h"
25 #include "ui/views/view.h"
26 #include "ui/views/views_delegate.h"
27
28 namespace ash {
29
30 namespace {
31
32 const float kDegreesToRadians = 3.1415926f / 180.0f;
33 const float kMeanGravity = 9.8066f;
34
35 void EnableMaximizeMode(bool enable) {
36 Shell::GetInstance()
37 ->maximize_mode_controller()
38 ->EnableMaximizeModeWindowManager(enable);
39 }
40
41 gfx::Display::Rotation GetInternalDisplayRotation() {
42 return Shell::GetInstance()
43 ->display_manager()
44 ->GetDisplayInfo(gfx::Display::InternalDisplayId())
45 .rotation();
46 }
47
48 gfx::Display::Rotation Rotation() {
49 return Shell::GetInstance()
50 ->display_manager()
51 ->GetDisplayInfo(gfx::Display::InternalDisplayId())
52 .rotation();
53 }
54
55 bool RotationLocked() {
56 return Shell::GetInstance()
57 ->screen_orientation_controller()
58 ->rotation_locked();
59 }
60
61 void SetInternalDisplayRotation(gfx::Display::Rotation rotation) {
62 Shell::GetInstance()->display_manager()->SetDisplayRotation(
63 gfx::Display::InternalDisplayId(), rotation);
64 }
65
66 void SetRotationLocked(bool rotation_locked) {
67 Shell::GetInstance()->screen_orientation_controller()->SetRotationLocked(
68 rotation_locked);
69 }
70
71 void TriggerLidUpdate(const gfx::Vector3dF& lid) {
72 ui::AccelerometerUpdate update;
73 update.Set(ui::ACCELEROMETER_SOURCE_SCREEN, lid.x(), lid.y(), lid.z());
74 Shell::GetInstance()->screen_orientation_controller()->OnAccelerometerUpdated(
75 update);
76 }
77
78 } // namespace
79
80 class ScreenOrientationControllerTest : public test::AshTestBase {
81 public:
82 ScreenOrientationControllerTest();
83 virtual ~ScreenOrientationControllerTest();
84
85 ScreenOrientationController* delegate() {
86 return screen_orientation_controller_;
87 }
88
89 // Creates and initializes and empty content::WebContents that is backed by a
90 // content::BrowserContext and that has an aura::Window.
91 content::WebContents* CreateWebContents();
92
93 // Creates a secondary content::WebContents, with a separate
94 // content::BrowserContext.
95 content::WebContents* CreateSecondaryWebContents();
96
97 // test::AshTestBase:
98 void SetUp() override;
99
100 private:
101 ScreenOrientationController* screen_orientation_controller_;
102
103 // Optional content::BrowserContext used for two window tests.
104 scoped_ptr<content::BrowserContext> secondary_browser_context_;
105
106 // Setups underlying content layer so that content::WebContents can be
107 // generated.
108 scoped_ptr<views::WebViewTestHelper> webview_test_helper_;
109
110 DISALLOW_COPY_AND_ASSIGN(ScreenOrientationControllerTest);
111 };
112
113 ScreenOrientationControllerTest::ScreenOrientationControllerTest() {
114 webview_test_helper_.reset(new views::WebViewTestHelper());
115 }
116
117 ScreenOrientationControllerTest::~ScreenOrientationControllerTest() {
118 }
119
120 content::WebContents* ScreenOrientationControllerTest::CreateWebContents() {
121 return views::ViewsDelegate::views_delegate->CreateWebContents(
122 ash_test_helper()->test_shell_delegate()->GetActiveBrowserContext(),
123 nullptr);
124 }
125
126 content::WebContents*
127 ScreenOrientationControllerTest::CreateSecondaryWebContents() {
128 secondary_browser_context_.reset(new content::TestBrowserContext());
129 return views::ViewsDelegate::views_delegate->CreateWebContents(
130 secondary_browser_context_.get(), nullptr);
131 }
132
133 void ScreenOrientationControllerTest::SetUp() {
134 base::CommandLine::ForCurrentProcess()->AppendSwitch(
135 switches::kAshUseFirstDisplayAsInternal);
136 base::CommandLine::ForCurrentProcess()->AppendSwitch(
137 switches::kAshEnableTouchViewTesting);
138 test::AshTestBase::SetUp();
139 screen_orientation_controller_ =
140 Shell::GetInstance()->screen_orientation_controller();
141 }
142
143 // Tests that a content::WebContents can lock rotation.
144 TEST_F(ScreenOrientationControllerTest, LockOrientation) {
145 scoped_ptr<content::WebContents> content(CreateWebContents());
146 ASSERT_NE(nullptr, content->GetNativeView());
147 ASSERT_EQ(gfx::Display::ROTATE_0, Rotation());
148 ASSERT_FALSE(RotationLocked());
149
150 delegate()->Lock(content.get(), blink::WebScreenOrientationLockLandscape);
151 EXPECT_EQ(gfx::Display::ROTATE_0, Rotation());
152 EXPECT_TRUE(RotationLocked());
153 }
154
155 // Tests that a content::WebContents can unlock rotation.
156 TEST_F(ScreenOrientationControllerTest, Unlock) {
157 scoped_ptr<content::WebContents> content(CreateWebContents());
158 ASSERT_NE(nullptr, content->GetNativeView());
159 ASSERT_EQ(gfx::Display::ROTATE_0, Rotation());
160 ASSERT_FALSE(RotationLocked());
161
162 delegate()->Lock(content.get(), blink::WebScreenOrientationLockLandscape);
163 EXPECT_EQ(gfx::Display::ROTATE_0, Rotation());
164 EXPECT_TRUE(RotationLocked());
165
166 delegate()->Unlock(content.get());
167 EXPECT_FALSE(RotationLocked());
168 }
169
170 // Tests that a content::WebContents is able to change the orientation of the
171 // display after having locked rotation.
172 TEST_F(ScreenOrientationControllerTest, OrientationChanges) {
173 scoped_ptr<content::WebContents> content(CreateWebContents());
174 ASSERT_NE(nullptr, content->GetNativeView());
175 ASSERT_EQ(gfx::Display::ROTATE_0, Rotation());
176 ASSERT_FALSE(RotationLocked());
177
178 delegate()->Lock(content.get(), blink::WebScreenOrientationLockPortrait);
179 EXPECT_EQ(gfx::Display::ROTATE_90, Rotation());
180 EXPECT_TRUE(RotationLocked());
181
182 delegate()->Lock(content.get(), blink::WebScreenOrientationLockLandscape);
183 EXPECT_EQ(gfx::Display::ROTATE_0, Rotation());
184 }
185
186 // Tests that a user initiated rotation lock cannot be unlocked by a
187 // content::WebContents.
188 TEST_F(ScreenOrientationControllerTest, UserLockRejectsUnlock) {
189 delegate()->SetRotationLocked(true);
190
191 scoped_ptr<content::WebContents> content(CreateWebContents());
192 delegate()->Unlock(content.get());
193 EXPECT_TRUE(RotationLocked());
194 }
195
196 // Tests that orientation can only be set by the first content::WebContents that
197 // has set a rotation lock.
198 TEST_F(ScreenOrientationControllerTest, SecondContentCannotChangeOrientation) {
199 scoped_ptr<content::WebContents> content1(CreateWebContents());
200 scoped_ptr<content::WebContents> content2(CreateSecondaryWebContents());
201 ASSERT_NE(content1->GetNativeView(), content2->GetNativeView());
202
203 delegate()->Lock(content1.get(), blink::WebScreenOrientationLockLandscape);
204 delegate()->Lock(content2.get(), blink::WebScreenOrientationLockPortrait);
205 EXPECT_EQ(gfx::Display::ROTATE_0, Rotation());
206 }
207
208 // Tests that only the content::WebContents that set a rotation lock can perform
209 // an unlock.
210 TEST_F(ScreenOrientationControllerTest, SecondContentCannotUnlock) {
211 scoped_ptr<content::WebContents> content1(CreateWebContents());
212 scoped_ptr<content::WebContents> content2(CreateSecondaryWebContents());
213 ASSERT_NE(content1->GetNativeView(), content2->GetNativeView());
214
215 delegate()->Lock(content1.get(), blink::WebScreenOrientationLockLandscape);
216 delegate()->Unlock(content2.get());
217 EXPECT_TRUE(RotationLocked());
218 }
219
220 // Tests that alternate content::WebContents can set a rotation lock after a
221 // preexisting lock has been released.
222 TEST_F(ScreenOrientationControllerTest, AfterUnlockSecondContentCanLock) {
223 scoped_ptr<content::WebContents> content1(CreateWebContents());
224 scoped_ptr<content::WebContents> content2(CreateSecondaryWebContents());
225 ASSERT_NE(content1->GetNativeView(), content2->GetNativeView());
226
227 delegate()->Lock(content1.get(), blink::WebScreenOrientationLockLandscape);
228 delegate()->Unlock(content1.get());
229 delegate()->Lock(content2.get(), blink::WebScreenOrientationLockPortrait);
230 EXPECT_EQ(gfx::Display::ROTATE_90, Rotation());
231 EXPECT_TRUE(RotationLocked());
232 }
233
234 // Tests that accelerometer readings in each of the screen angles will trigger a
235 // rotation of the internal display.
236 TEST_F(ScreenOrientationControllerTest, DisplayRotation) {
237 EnableMaximizeMode(true);
238 // Now test rotating in all directions.
239 TriggerLidUpdate(gfx::Vector3dF(-kMeanGravity, 0.0f, 0.0f));
240 EXPECT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation());
241 TriggerLidUpdate(gfx::Vector3dF(0.0f, kMeanGravity, 0.0f));
242 EXPECT_EQ(gfx::Display::ROTATE_180, GetInternalDisplayRotation());
243 TriggerLidUpdate(gfx::Vector3dF(kMeanGravity, 0.0f, 0.0f));
244 EXPECT_EQ(gfx::Display::ROTATE_270, GetInternalDisplayRotation());
245 TriggerLidUpdate(gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
246 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
247 }
248
249 // Tests that low angles are ignored by the accelerometer (i.e. when the device
250 // is almost laying flat).
251 TEST_F(ScreenOrientationControllerTest, RotationIgnoresLowAngles) {
252 EnableMaximizeMode(true);
253 TriggerLidUpdate(gfx::Vector3dF(0.0f, -kMeanGravity, -kMeanGravity));
254 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
255 TriggerLidUpdate(gfx::Vector3dF(-2.0f, 0.0f, -kMeanGravity));
256 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
257 TriggerLidUpdate(gfx::Vector3dF(0.0f, 2.0f, -kMeanGravity));
258 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
259 TriggerLidUpdate(gfx::Vector3dF(2.0f, 0.0f, -kMeanGravity));
260 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
261 TriggerLidUpdate(gfx::Vector3dF(0.0f, -2.0f, -kMeanGravity));
262 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
263 }
264
265 // Tests that the display will stick to the current orientation beyond the
266 // halfway point, preventing frequent updates back and forth.
267 TEST_F(ScreenOrientationControllerTest, RotationSticky) {
268 EnableMaximizeMode(true);
269 gfx::Vector3dF gravity(0.0f, -kMeanGravity, 0.0f);
270 TriggerLidUpdate(gravity);
271 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
272
273 // Turn past half-way point to next direction and rotation should remain
274 // the same.
275 float degrees = 50.0;
276 gravity.set_x(-sin(degrees * kDegreesToRadians) * kMeanGravity);
277 gravity.set_y(-cos(degrees * kDegreesToRadians) * kMeanGravity);
278 TriggerLidUpdate(gravity);
279 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
280
281 // Turn more and the screen should rotate.
282 degrees = 70.0;
283 gravity.set_x(-sin(degrees * kDegreesToRadians) * kMeanGravity);
284 gravity.set_y(-cos(degrees * kDegreesToRadians) * kMeanGravity);
285 TriggerLidUpdate(gravity);
286 EXPECT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation());
287
288 // Turn back just beyond the half-way point and the new rotation should
289 // still be in effect.
290 degrees = 40.0;
291 gravity.set_x(-sin(degrees * kDegreesToRadians) * kMeanGravity);
292 gravity.set_y(-cos(degrees * kDegreesToRadians) * kMeanGravity);
293 TriggerLidUpdate(gravity);
294 EXPECT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation());
295 }
296
297 // Tests that the display will stick to its current orientation when the
298 // rotation lock has been set.
299 TEST_F(ScreenOrientationControllerTest, RotationLockPreventsRotation) {
300 EnableMaximizeMode(true);
301 SetRotationLocked(true);
302
303 // Turn past the threshold for rotation.
304 float degrees = 90.0;
305 gfx::Vector3dF gravity(-sin(degrees * kDegreesToRadians) * kMeanGravity,
306 -cos(degrees * kDegreesToRadians) * kMeanGravity,
307 0.0f);
308 TriggerLidUpdate(gravity);
309 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
310
311 SetRotationLocked(false);
312 TriggerLidUpdate(gravity);
313 EXPECT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation());
314 }
315
316 // The TrayDisplay class that is responsible for adding/updating MessageCenter
317 // notifications is only added to the SystemTray on ChromeOS.
318 // Tests that the screen rotation notifications are suppressed when
319 // triggered by the accelerometer.
320 TEST_F(ScreenOrientationControllerTest, BlockRotationNotifications) {
321 EnableMaximizeMode(true);
322 test::TestSystemTrayDelegate* tray_delegate =
323 static_cast<test::TestSystemTrayDelegate*>(
324 Shell::GetInstance()->system_tray_delegate());
325 tray_delegate->set_should_show_display_notification(true);
326
327 message_center::MessageCenter* message_center =
328 message_center::MessageCenter::Get();
329
330 EXPECT_EQ(0u, message_center->NotificationCount());
331 EXPECT_FALSE(message_center->HasPopupNotifications());
332
333 // Make sure notifications are still displayed when
334 // adjusting the screen rotation directly when in maximize mode
335 ASSERT_NE(gfx::Display::ROTATE_270, GetInternalDisplayRotation());
336 SetInternalDisplayRotation(gfx::Display::ROTATE_270);
337 SetRotationLocked(false);
338 EXPECT_EQ(gfx::Display::ROTATE_270, GetInternalDisplayRotation());
339 EXPECT_EQ(1u, message_center->NotificationCount());
340 EXPECT_TRUE(message_center->HasPopupNotifications());
341
342 // Clear all notifications
343 message_center->RemoveAllNotifications(false);
344 EXPECT_EQ(0u, message_center->NotificationCount());
345 EXPECT_FALSE(message_center->HasPopupNotifications());
346
347 // Make sure notifications are blocked when adjusting the screen rotation
348 // via the accelerometer while in maximize mode
349 // Rotate the screen 90 degrees
350 ASSERT_NE(gfx::Display::ROTATE_90, GetInternalDisplayRotation());
351 TriggerLidUpdate(gfx::Vector3dF(-kMeanGravity, 0.0f, 0.0f));
352 ASSERT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation());
353 EXPECT_EQ(0u, message_center->NotificationCount());
354 EXPECT_FALSE(message_center->HasPopupNotifications());
355
356 // Make sure notifications are still displayed when
357 // adjusting the screen rotation directly when not in maximize mode
358 EnableMaximizeMode(false);
359 // Reset the screen rotation.
360 SetInternalDisplayRotation(gfx::Display::ROTATE_0);
361 // Clear all notifications
362 message_center->RemoveAllNotifications(false);
363 ASSERT_NE(gfx::Display::ROTATE_180, GetInternalDisplayRotation());
364 ASSERT_EQ(0u, message_center->NotificationCount());
365 ASSERT_FALSE(message_center->HasPopupNotifications());
366 SetInternalDisplayRotation(gfx::Display::ROTATE_180);
367 EXPECT_EQ(gfx::Display::ROTATE_180, GetInternalDisplayRotation());
368 EXPECT_EQ(1u, message_center->NotificationCount());
369 EXPECT_TRUE(message_center->HasPopupNotifications());
370 }
371
372 // Tests that if a user has set a display rotation that it is restored upon
373 // exiting maximize mode.
374 TEST_F(ScreenOrientationControllerTest, ResetUserRotationUponExit) {
375 SetInternalDisplayRotation(gfx::Display::ROTATE_90);
376 EnableMaximizeMode(true);
377
378 TriggerLidUpdate(gfx::Vector3dF(0.0f, kMeanGravity, 0.0f));
379 EXPECT_EQ(gfx::Display::ROTATE_180, GetInternalDisplayRotation());
380
381 EnableMaximizeMode(false);
382 EXPECT_EQ(gfx::Display::ROTATE_90, GetInternalDisplayRotation());
383 }
384
385 // Tests that if a user sets a display rotation that accelerometer rotation
386 // becomes locked.
387 TEST_F(ScreenOrientationControllerTest,
388 NonAccelerometerRotationChangesLockRotation) {
389 EnableMaximizeMode(true);
390 ASSERT_FALSE(RotationLocked());
391 SetInternalDisplayRotation(gfx::Display::ROTATE_270);
392 EXPECT_TRUE(RotationLocked());
393 }
394
395 // Tests that if a user changes the display rotation, while rotation is locked,
396 // that the updates are recorded. Upon exiting maximize mode the latest user
397 // rotation should be applied.
398 TEST_F(ScreenOrientationControllerTest, UpdateUserRotationWhileRotationLocked) {
399 EnableMaximizeMode(true);
400 SetInternalDisplayRotation(gfx::Display::ROTATE_270);
401 // User sets rotation to the same rotation that the display was at when
402 // maximize mode was activated.
403 SetInternalDisplayRotation(gfx::Display::ROTATE_0);
404 EnableMaximizeMode(false);
405 EXPECT_EQ(gfx::Display::ROTATE_0, GetInternalDisplayRotation());
406 }
407
408 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698