Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / ash / content / display / screen_orientation_controller_chromeos_unittest.cc
blob57a7485e387d2cbcd448082d8e129f895de4b0a4
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 <vector>
7 #include "ash/ash_switches.h"
8 #include "ash/content/display/screen_orientation_controller_chromeos.h"
9 #include "ash/display/display_info.h"
10 #include "ash/display/display_manager.h"
11 #include "ash/shell.h"
12 #include "ash/test/ash_test_base.h"
13 #include "ash/test/ash_test_helper.h"
14 #include "ash/test/display_manager_test_api.h"
15 #include "ash/test/test_shell_delegate.h"
16 #include "ash/test/test_system_tray_delegate.h"
17 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
18 #include "base/command_line.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "chromeos/accelerometer/accelerometer_reader.h"
21 #include "chromeos/accelerometer/accelerometer_types.h"
22 #include "content/public/browser/browser_context.h"
23 #include "content/public/browser/web_contents.h"
24 #include "content/public/test/test_browser_context.h"
25 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebScreenOrientationLockType.h"
26 #include "ui/aura/window.h"
27 #include "ui/gfx/display.h"
28 #include "ui/message_center/message_center.h"
29 #include "ui/views/test/webview_test_helper.h"
30 #include "ui/views/view.h"
31 #include "ui/views/views_delegate.h"
32 #include "ui/wm/public/activation_client.h"
34 namespace ash {
36 namespace {
38 const float kDegreesToRadians = 3.1415926f / 180.0f;
39 const float kMeanGravity = 9.8066f;
41 DisplayInfo CreateDisplayInfo(int64 id, const gfx::Rect& bounds) {
42 DisplayInfo info(id, "dummy", false);
43 info.SetBounds(bounds);
44 return info;
47 void EnableMaximizeMode(bool enable) {
48 Shell::GetInstance()
49 ->maximize_mode_controller()
50 ->EnableMaximizeModeWindowManager(enable);
53 bool RotationLocked() {
54 return Shell::GetInstance()
55 ->screen_orientation_controller()
56 ->rotation_locked();
59 void SetDisplayRotationById(int64 display_id, gfx::Display::Rotation rotation) {
60 Shell::GetInstance()->display_manager()->SetDisplayRotation(
61 display_id, rotation, gfx::Display::ROTATION_SOURCE_USER);
64 void SetInternalDisplayRotation(gfx::Display::Rotation rotation) {
65 SetDisplayRotationById(gfx::Display::InternalDisplayId(), rotation);
68 void SetRotationLocked(bool rotation_locked) {
69 Shell::GetInstance()->screen_orientation_controller()->SetRotationLocked(
70 rotation_locked);
73 void TriggerLidUpdate(const gfx::Vector3dF& lid) {
74 scoped_refptr<chromeos::AccelerometerUpdate> update(
75 new chromeos::AccelerometerUpdate());
76 update->Set(chromeos::ACCELEROMETER_SOURCE_SCREEN, lid.x(), lid.y(), lid.z());
77 Shell::GetInstance()->screen_orientation_controller()->OnAccelerometerUpdated(
78 update);
81 // Attaches the NativeView of |web_contents| to |parent| without changing the
82 // currently active window.
83 void AttachWebContents(content::WebContents* web_contents,
84 aura::Window* parent) {
85 aura::Window* window = web_contents->GetNativeView();
86 window->Show();
87 parent->AddChild(window);
90 // Attaches the NativeView of |web_contents| to |parent|, ensures that it is
91 // visible, and activates the parent window.
92 void AttachAndActivateWebContents(content::WebContents* web_contents,
93 aura::Window* parent) {
94 AttachWebContents(web_contents, parent);
95 Shell::GetInstance()->activation_client()->ActivateWindow(parent);
98 } // namespace
100 class ScreenOrientationControllerTest : public test::AshTestBase {
101 public:
102 ScreenOrientationControllerTest();
103 ~ScreenOrientationControllerTest() override;
105 ScreenOrientationController* delegate() {
106 return screen_orientation_controller_;
109 // Creates and initializes and empty content::WebContents that is backed by a
110 // content::BrowserContext and that has an aura::Window.
111 content::WebContents* CreateWebContents();
113 // Creates a secondary content::WebContents, with a separate
114 // content::BrowserContext.
115 content::WebContents* CreateSecondaryWebContents();
117 // test::AshTestBase:
118 void SetUp() override;
120 private:
121 ScreenOrientationController* screen_orientation_controller_;
123 // Optional content::BrowserContext used for two window tests.
124 scoped_ptr<content::BrowserContext> secondary_browser_context_;
126 // Setups underlying content layer so that content::WebContents can be
127 // generated.
128 scoped_ptr<views::WebViewTestHelper> webview_test_helper_;
130 DISALLOW_COPY_AND_ASSIGN(ScreenOrientationControllerTest);
133 ScreenOrientationControllerTest::ScreenOrientationControllerTest() {
134 webview_test_helper_.reset(new views::WebViewTestHelper());
137 ScreenOrientationControllerTest::~ScreenOrientationControllerTest() {
140 content::WebContents* ScreenOrientationControllerTest::CreateWebContents() {
141 return views::ViewsDelegate::GetInstance()->CreateWebContents(
142 ash_test_helper()->test_shell_delegate()->GetActiveBrowserContext(),
143 nullptr);
146 content::WebContents*
147 ScreenOrientationControllerTest::CreateSecondaryWebContents() {
148 secondary_browser_context_.reset(new content::TestBrowserContext());
149 return views::ViewsDelegate::GetInstance()->CreateWebContents(
150 secondary_browser_context_.get(), nullptr);
153 void ScreenOrientationControllerTest::SetUp() {
154 base::CommandLine::ForCurrentProcess()->AppendSwitch(
155 switches::kAshUseFirstDisplayAsInternal);
156 base::CommandLine::ForCurrentProcess()->AppendSwitch(
157 switches::kAshEnableTouchViewTesting);
158 test::AshTestBase::SetUp();
159 screen_orientation_controller_ =
160 Shell::GetInstance()->screen_orientation_controller();
163 // Tests that a content::WebContents can lock rotation.
164 TEST_F(ScreenOrientationControllerTest, LockOrientation) {
165 scoped_ptr<content::WebContents> content(CreateWebContents());
166 scoped_ptr<aura::Window> focus_window(CreateTestWindowInShellWithId(0));
167 ASSERT_NE(nullptr, content->GetNativeView());
168 ASSERT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
169 ASSERT_FALSE(RotationLocked());
171 AttachAndActivateWebContents(content.get(), focus_window.get());
172 delegate()->Lock(content.get(), blink::WebScreenOrientationLockLandscape);
173 EXPECT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
174 EXPECT_TRUE(RotationLocked());
177 // Tests that a content::WebContents can unlock rotation.
178 TEST_F(ScreenOrientationControllerTest, Unlock) {
179 scoped_ptr<content::WebContents> content(CreateWebContents());
180 scoped_ptr<aura::Window> focus_window(CreateTestWindowInShellWithId(0));
181 ASSERT_NE(nullptr, content->GetNativeView());
182 ASSERT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
183 ASSERT_FALSE(RotationLocked());
185 AttachAndActivateWebContents(content.get(), focus_window.get());
186 delegate()->Lock(content.get(), blink::WebScreenOrientationLockLandscape);
187 EXPECT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
188 EXPECT_TRUE(RotationLocked());
190 delegate()->Unlock(content.get());
191 EXPECT_FALSE(RotationLocked());
194 // Tests that a content::WebContents is able to change the orientation of the
195 // display after having locked rotation.
196 TEST_F(ScreenOrientationControllerTest, OrientationChanges) {
197 scoped_ptr<content::WebContents> content(CreateWebContents());
198 scoped_ptr<aura::Window> focus_window(CreateTestWindowInShellWithId(0));
199 ASSERT_NE(nullptr, content->GetNativeView());
200 ASSERT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
201 ASSERT_FALSE(RotationLocked());
203 AttachAndActivateWebContents(content.get(), focus_window.get());
204 delegate()->Lock(content.get(), blink::WebScreenOrientationLockPortrait);
205 EXPECT_EQ(gfx::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
206 EXPECT_TRUE(RotationLocked());
208 delegate()->Lock(content.get(), blink::WebScreenOrientationLockLandscape);
209 EXPECT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
212 // Tests that orientation can only be set by the first content::WebContents that
213 // has set a rotation lock.
214 TEST_F(ScreenOrientationControllerTest, SecondContentCannotChangeOrientation) {
215 scoped_ptr<content::WebContents> content1(CreateWebContents());
216 scoped_ptr<content::WebContents> content2(CreateSecondaryWebContents());
217 scoped_ptr<aura::Window> focus_window1(CreateTestWindowInShellWithId(0));
218 scoped_ptr<aura::Window> focus_window2(CreateTestWindowInShellWithId(1));
219 ASSERT_NE(content1->GetNativeView(), content2->GetNativeView());
221 AttachAndActivateWebContents(content1.get(), focus_window1.get());
222 AttachWebContents(content2.get(), focus_window2.get());
223 delegate()->Lock(content1.get(), blink::WebScreenOrientationLockLandscape);
224 delegate()->Lock(content2.get(), blink::WebScreenOrientationLockPortrait);
225 EXPECT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
228 // Tests that only the content::WebContents that set a rotation lock can perform
229 // an unlock.
230 TEST_F(ScreenOrientationControllerTest, SecondContentCannotUnlock) {
231 scoped_ptr<content::WebContents> content1(CreateWebContents());
232 scoped_ptr<content::WebContents> content2(CreateSecondaryWebContents());
233 scoped_ptr<aura::Window> focus_window1(CreateTestWindowInShellWithId(0));
234 scoped_ptr<aura::Window> focus_window2(CreateTestWindowInShellWithId(1));
235 ASSERT_NE(content1->GetNativeView(), content2->GetNativeView());
237 AttachAndActivateWebContents(content1.get(), focus_window1.get());
238 AttachWebContents(content2.get(), focus_window2.get());
239 delegate()->Lock(content1.get(), blink::WebScreenOrientationLockLandscape);
240 delegate()->Unlock(content2.get());
241 EXPECT_TRUE(RotationLocked());
244 // Tests that a rotation lock is applied only while the content::WebContents are
245 // a part of the active window.
246 TEST_F(ScreenOrientationControllerTest, ActiveWindowChangesUpdateLock) {
247 scoped_ptr<content::WebContents> content(CreateWebContents());
248 scoped_ptr<aura::Window> focus_window1(CreateTestWindowInShellWithId(0));
249 scoped_ptr<aura::Window> focus_window2(CreateTestWindowInShellWithId(1));
251 AttachAndActivateWebContents(content.get(), focus_window1.get());
252 delegate()->Lock(content.get(), blink::WebScreenOrientationLockLandscape);
253 ASSERT_TRUE(RotationLocked());
255 aura::client::ActivationClient* activation_client =
256 Shell::GetInstance()->activation_client();
257 activation_client->ActivateWindow(focus_window2.get());
258 EXPECT_FALSE(RotationLocked());
260 activation_client->ActivateWindow(focus_window1.get());
261 EXPECT_TRUE(RotationLocked());
264 // Tests that switching between windows with different orientation locks change
265 // the orientation.
266 TEST_F(ScreenOrientationControllerTest, ActiveWindowChangesUpdateOrientation) {
267 scoped_ptr<content::WebContents> content1(CreateWebContents());
268 scoped_ptr<content::WebContents> content2(CreateSecondaryWebContents());
269 scoped_ptr<aura::Window> focus_window1(CreateTestWindowInShellWithId(0));
270 scoped_ptr<aura::Window> focus_window2(CreateTestWindowInShellWithId(1));
271 AttachAndActivateWebContents(content1.get(), focus_window1.get());
272 AttachWebContents(content2.get(), focus_window2.get());
274 delegate()->Lock(content1.get(), blink::WebScreenOrientationLockLandscape);
275 delegate()->Lock(content2.get(), blink::WebScreenOrientationLockPortrait);
276 EXPECT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
278 aura::client::ActivationClient* activation_client =
279 Shell::GetInstance()->activation_client();
280 activation_client->ActivateWindow(focus_window2.get());
281 EXPECT_TRUE(RotationLocked());
282 EXPECT_EQ(gfx::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
284 activation_client->ActivateWindow(focus_window1.get());
285 EXPECT_TRUE(RotationLocked());
286 EXPECT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
289 // Tests that a rotation lock is removed when the setting window is hidden, and
290 // that it is reapplied when the window becomes visible.
291 TEST_F(ScreenOrientationControllerTest, VisibilityChangesLock) {
292 scoped_ptr<content::WebContents> content(CreateWebContents());
293 scoped_ptr<aura::Window> focus_window(CreateTestWindowInShellWithId(0));
294 AttachAndActivateWebContents(content.get(), focus_window.get());
295 delegate()->Lock(content.get(), blink::WebScreenOrientationLockLandscape);
296 EXPECT_TRUE(RotationLocked());
298 aura::Window* window = content->GetNativeView();
299 window->Hide();
300 EXPECT_FALSE(RotationLocked());
302 window->Show();
303 EXPECT_TRUE(RotationLocked());
306 // Tests that when a window is destroyed that its rotation lock is removed, and
307 // window activations no longer change the lock
308 TEST_F(ScreenOrientationControllerTest, WindowDestructionRemovesLock) {
309 scoped_ptr<content::WebContents> content(CreateWebContents());
310 scoped_ptr<aura::Window> focus_window1(CreateTestWindowInShellWithId(0));
311 scoped_ptr<aura::Window> focus_window2(CreateTestWindowInShellWithId(1));
313 AttachAndActivateWebContents(content.get(), focus_window1.get());
314 delegate()->Lock(content.get(), blink::WebScreenOrientationLockLandscape);
315 ASSERT_TRUE(RotationLocked());
317 focus_window1->RemoveChild(content->GetNativeView());
318 content.reset();
319 EXPECT_FALSE(RotationLocked());
321 aura::client::ActivationClient* activation_client =
322 Shell::GetInstance()->activation_client();
323 activation_client->ActivateWindow(focus_window2.get());
324 EXPECT_FALSE(RotationLocked());
326 activation_client->ActivateWindow(focus_window1.get());
327 EXPECT_FALSE(RotationLocked());
330 // Tests that accelerometer readings in each of the screen angles will trigger a
331 // rotation of the internal display.
332 TEST_F(ScreenOrientationControllerTest, DisplayRotation) {
333 EnableMaximizeMode(true);
334 // Now test rotating in all directions.
335 TriggerLidUpdate(gfx::Vector3dF(-kMeanGravity, 0.0f, 0.0f));
336 EXPECT_EQ(gfx::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
337 TriggerLidUpdate(gfx::Vector3dF(0.0f, kMeanGravity, 0.0f));
338 EXPECT_EQ(gfx::Display::ROTATE_180, GetCurrentInternalDisplayRotation());
339 TriggerLidUpdate(gfx::Vector3dF(kMeanGravity, 0.0f, 0.0f));
340 EXPECT_EQ(gfx::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
341 TriggerLidUpdate(gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
342 EXPECT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
345 // Tests that low angles are ignored by the accelerometer (i.e. when the device
346 // is almost laying flat).
347 TEST_F(ScreenOrientationControllerTest, RotationIgnoresLowAngles) {
348 EnableMaximizeMode(true);
349 TriggerLidUpdate(gfx::Vector3dF(0.0f, -kMeanGravity, -kMeanGravity));
350 EXPECT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
351 TriggerLidUpdate(gfx::Vector3dF(-2.0f, 0.0f, -kMeanGravity));
352 EXPECT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
353 TriggerLidUpdate(gfx::Vector3dF(0.0f, 2.0f, -kMeanGravity));
354 EXPECT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
355 TriggerLidUpdate(gfx::Vector3dF(2.0f, 0.0f, -kMeanGravity));
356 EXPECT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
357 TriggerLidUpdate(gfx::Vector3dF(0.0f, -2.0f, -kMeanGravity));
358 EXPECT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
361 // Tests that the display will stick to the current orientation beyond the
362 // halfway point, preventing frequent updates back and forth.
363 TEST_F(ScreenOrientationControllerTest, RotationSticky) {
364 EnableMaximizeMode(true);
365 gfx::Vector3dF gravity(0.0f, -kMeanGravity, 0.0f);
366 TriggerLidUpdate(gravity);
367 EXPECT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
369 // Turn past half-way point to next direction and rotation should remain
370 // the same.
371 float degrees = 50.0;
372 gravity.set_x(-sin(degrees * kDegreesToRadians) * kMeanGravity);
373 gravity.set_y(-cos(degrees * kDegreesToRadians) * kMeanGravity);
374 TriggerLidUpdate(gravity);
375 EXPECT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
377 // Turn more and the screen should rotate.
378 degrees = 70.0;
379 gravity.set_x(-sin(degrees * kDegreesToRadians) * kMeanGravity);
380 gravity.set_y(-cos(degrees * kDegreesToRadians) * kMeanGravity);
381 TriggerLidUpdate(gravity);
382 EXPECT_EQ(gfx::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
384 // Turn back just beyond the half-way point and the new rotation should
385 // still be in effect.
386 degrees = 40.0;
387 gravity.set_x(-sin(degrees * kDegreesToRadians) * kMeanGravity);
388 gravity.set_y(-cos(degrees * kDegreesToRadians) * kMeanGravity);
389 TriggerLidUpdate(gravity);
390 EXPECT_EQ(gfx::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
393 // Tests that the display will stick to its current orientation when the
394 // rotation lock has been set.
395 TEST_F(ScreenOrientationControllerTest, RotationLockPreventsRotation) {
396 EnableMaximizeMode(true);
397 SetRotationLocked(true);
399 // Turn past the threshold for rotation.
400 float degrees = 90.0;
401 gfx::Vector3dF gravity(-sin(degrees * kDegreesToRadians) * kMeanGravity,
402 -cos(degrees * kDegreesToRadians) * kMeanGravity,
403 0.0f);
404 TriggerLidUpdate(gravity);
405 EXPECT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
407 SetRotationLocked(false);
408 TriggerLidUpdate(gravity);
409 EXPECT_EQ(gfx::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
412 // The TrayDisplay class that is responsible for adding/updating MessageCenter
413 // notifications is only added to the SystemTray on ChromeOS.
414 // Tests that the screen rotation notifications are suppressed when
415 // triggered by the accelerometer.
416 TEST_F(ScreenOrientationControllerTest, BlockRotationNotifications) {
417 EnableMaximizeMode(true);
418 test::TestSystemTrayDelegate* tray_delegate =
419 static_cast<test::TestSystemTrayDelegate*>(
420 Shell::GetInstance()->system_tray_delegate());
421 tray_delegate->set_should_show_display_notification(true);
422 test::DisplayManagerTestApi().SetFirstDisplayAsInternalDisplay();
424 message_center::MessageCenter* message_center =
425 message_center::MessageCenter::Get();
427 EXPECT_EQ(0u, message_center->NotificationCount());
428 EXPECT_FALSE(message_center->HasPopupNotifications());
430 // Make sure notifications are still displayed when
431 // adjusting the screen rotation directly when in maximize mode
432 ASSERT_NE(gfx::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
433 SetInternalDisplayRotation(gfx::Display::ROTATE_270);
434 SetRotationLocked(false);
435 EXPECT_EQ(gfx::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
436 EXPECT_EQ(1u, message_center->NotificationCount());
437 EXPECT_TRUE(message_center->HasPopupNotifications());
439 // Clear all notifications
440 message_center->RemoveAllNotifications(false);
441 EXPECT_EQ(0u, message_center->NotificationCount());
442 EXPECT_FALSE(message_center->HasPopupNotifications());
444 // Make sure notifications are blocked when adjusting the screen rotation
445 // via the accelerometer while in maximize mode
446 // Rotate the screen 90 degrees
447 ASSERT_NE(gfx::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
448 TriggerLidUpdate(gfx::Vector3dF(-kMeanGravity, 0.0f, 0.0f));
449 ASSERT_EQ(gfx::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
450 EXPECT_EQ(0u, message_center->NotificationCount());
451 EXPECT_FALSE(message_center->HasPopupNotifications());
453 // Make sure notifications are still displayed when
454 // adjusting the screen rotation directly when not in maximize mode
455 EnableMaximizeMode(false);
456 // Reset the screen rotation.
457 SetInternalDisplayRotation(gfx::Display::ROTATE_0);
458 // Clear all notifications
459 message_center->RemoveAllNotifications(false);
460 ASSERT_NE(gfx::Display::ROTATE_180, GetCurrentInternalDisplayRotation());
461 ASSERT_EQ(0u, message_center->NotificationCount());
462 ASSERT_FALSE(message_center->HasPopupNotifications());
463 SetInternalDisplayRotation(gfx::Display::ROTATE_180);
464 EXPECT_EQ(gfx::Display::ROTATE_180, GetCurrentInternalDisplayRotation());
465 EXPECT_EQ(1u, message_center->NotificationCount());
466 EXPECT_TRUE(message_center->HasPopupNotifications());
469 // Tests that if a user has set a display rotation that it is restored upon
470 // exiting maximize mode.
471 TEST_F(ScreenOrientationControllerTest, ResetUserRotationUponExit) {
472 test::DisplayManagerTestApi().SetFirstDisplayAsInternalDisplay();
474 SetInternalDisplayRotation(gfx::Display::ROTATE_90);
475 EnableMaximizeMode(true);
477 TriggerLidUpdate(gfx::Vector3dF(0.0f, kMeanGravity, 0.0f));
478 EXPECT_EQ(gfx::Display::ROTATE_180, GetCurrentInternalDisplayRotation());
480 EnableMaximizeMode(false);
481 EXPECT_EQ(gfx::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
484 // Tests that if a user sets a display rotation that accelerometer rotation
485 // becomes locked.
486 TEST_F(ScreenOrientationControllerTest,
487 NonAccelerometerRotationChangesLockRotation) {
488 EnableMaximizeMode(true);
489 ASSERT_FALSE(RotationLocked());
490 SetInternalDisplayRotation(gfx::Display::ROTATE_270);
491 EXPECT_TRUE(RotationLocked());
494 // Tests that if a user changes the display rotation, while rotation is locked,
495 // that the updates are recorded. Upon exiting maximize mode the latest user
496 // rotation should be applied.
497 TEST_F(ScreenOrientationControllerTest, UpdateUserRotationWhileRotationLocked) {
498 EnableMaximizeMode(true);
499 SetInternalDisplayRotation(gfx::Display::ROTATE_270);
500 // User sets rotation to the same rotation that the display was at when
501 // maximize mode was activated.
502 SetInternalDisplayRotation(gfx::Display::ROTATE_0);
503 EnableMaximizeMode(false);
504 EXPECT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
507 // Tests that when the orientation lock is set to Landscape, that rotation can
508 // be done between the two angles of the orientation.
509 TEST_F(ScreenOrientationControllerTest, LandscapeOrientationAllowsRotation) {
510 scoped_ptr<content::WebContents> content(CreateWebContents());
511 scoped_ptr<aura::Window> focus_window(CreateTestWindowInShellWithId(0));
512 EnableMaximizeMode(true);
514 AttachAndActivateWebContents(content.get(), focus_window.get());
515 delegate()->Lock(content.get(), blink::WebScreenOrientationLockLandscape);
516 EXPECT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
517 EXPECT_TRUE(RotationLocked());
519 // Inverse of orientation is allowed
520 TriggerLidUpdate(gfx::Vector3dF(0.0f, kMeanGravity, 0.0f));
521 EXPECT_EQ(gfx::Display::ROTATE_180, GetCurrentInternalDisplayRotation());
523 // Display rotations between are not allowed
524 TriggerLidUpdate(gfx::Vector3dF(kMeanGravity, 0.0f, 0.0f));
525 EXPECT_EQ(gfx::Display::ROTATE_180, GetCurrentInternalDisplayRotation());
526 TriggerLidUpdate(gfx::Vector3dF(-kMeanGravity, 0.0f, 0.0f));
527 EXPECT_EQ(gfx::Display::ROTATE_180, GetCurrentInternalDisplayRotation());
530 // Tests that when the orientation lock is set to Portrait, that rotaiton can be
531 // done between the two angles of the orientation.
532 TEST_F(ScreenOrientationControllerTest, PortraitOrientationAllowsRotation) {
533 scoped_ptr<content::WebContents> content(CreateWebContents());
534 scoped_ptr<aura::Window> focus_window(CreateTestWindowInShellWithId(0));
535 EnableMaximizeMode(true);
537 AttachAndActivateWebContents(content.get(), focus_window.get());
538 delegate()->Lock(content.get(), blink::WebScreenOrientationLockPortrait);
539 EXPECT_EQ(gfx::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
540 EXPECT_TRUE(RotationLocked());
542 // Inverse of orientation is allowed
543 TriggerLidUpdate(gfx::Vector3dF(kMeanGravity, 0.0f, 0.0f));
544 EXPECT_EQ(gfx::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
546 // Display rotations between are not allowed
547 TriggerLidUpdate(gfx::Vector3dF(0.0f, kMeanGravity, 0.0f));
548 EXPECT_EQ(gfx::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
549 TriggerLidUpdate(gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
550 EXPECT_EQ(gfx::Display::ROTATE_270, GetCurrentInternalDisplayRotation());
553 // Tests that for an orientation lock which does not allow rotation, that the
554 // display rotation remains constant.
555 TEST_F(ScreenOrientationControllerTest, OrientationLockDisallowsRotation) {
556 scoped_ptr<content::WebContents> content(CreateWebContents());
557 scoped_ptr<aura::Window> focus_window(CreateTestWindowInShellWithId(0));
558 EnableMaximizeMode(true);
560 AttachAndActivateWebContents(content.get(), focus_window.get());
561 delegate()->Lock(content.get(),
562 blink::WebScreenOrientationLockPortraitPrimary);
563 EXPECT_EQ(gfx::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
564 EXPECT_TRUE(RotationLocked());
566 // Rotation does not change.
567 TriggerLidUpdate(gfx::Vector3dF(kMeanGravity, 0.0f, 0.0f));
568 EXPECT_EQ(gfx::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
569 TriggerLidUpdate(gfx::Vector3dF(0.0f, kMeanGravity, 0.0f));
570 EXPECT_EQ(gfx::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
571 TriggerLidUpdate(gfx::Vector3dF(0.0f, -kMeanGravity, 0.0f));
572 EXPECT_EQ(gfx::Display::ROTATE_90, GetCurrentInternalDisplayRotation());
575 // Tests that after a content::WebContents has applied an orientation lock which
576 // supports rotation, that a user rotation lock does not allow rotation.
577 TEST_F(ScreenOrientationControllerTest, UserRotationLockDisallowsRotation) {
578 scoped_ptr<content::WebContents> content(CreateWebContents());
579 scoped_ptr<aura::Window> focus_window(CreateTestWindowInShellWithId(0));
580 EnableMaximizeMode(true);
582 AttachAndActivateWebContents(content.get(), focus_window.get());
583 delegate()->Lock(content.get(), blink::WebScreenOrientationLockLandscape);
584 delegate()->Unlock(content.get());
586 SetRotationLocked(true);
587 EXPECT_TRUE(RotationLocked());
588 EXPECT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
590 TriggerLidUpdate(gfx::Vector3dF(0.0f, kMeanGravity, 0.0f));
591 EXPECT_EQ(gfx::Display::ROTATE_0, GetCurrentInternalDisplayRotation());
594 // Tests that when MaximizeMode is triggered before the internal display is
595 // ready, that ScreenOrientationController still begins listening to events,
596 // which require an internal display to be acted upon.
597 TEST_F(ScreenOrientationControllerTest, InternalDisplayNotAvailableAtStartup) {
598 test::DisplayManagerTestApi().SetFirstDisplayAsInternalDisplay();
600 int64 internal_display_id = gfx::Display::InternalDisplayId();
601 gfx::Display::SetInternalDisplayId(gfx::Display::kInvalidDisplayID);
603 EnableMaximizeMode(true);
605 // Should not crash, even though there is no internal display.
606 SetDisplayRotationById(internal_display_id, gfx::Display::ROTATE_180);
607 EXPECT_FALSE(RotationLocked());
609 // Should not crash, even though the invalid display id is requested.
610 SetDisplayRotationById(gfx::Display::kInvalidDisplayID,
611 gfx::Display::ROTATE_180);
612 EXPECT_FALSE(RotationLocked());
614 // With an internal display now available, functionality should resume.
615 gfx::Display::SetInternalDisplayId(internal_display_id);
616 SetInternalDisplayRotation(gfx::Display::ROTATE_90);
617 EXPECT_TRUE(RotationLocked());
620 // Verifies rotating an inactive Display is sucessful.
621 TEST_F(ScreenOrientationControllerTest, RotateInactiveDisplay) {
622 const int64 kInternalDisplayId = 9;
623 const int64 kExternalDisplayId = 10;
624 const gfx::Display::Rotation kNewRotation = gfx::Display::ROTATE_180;
626 const DisplayInfo internal_display_info =
627 CreateDisplayInfo(kInternalDisplayId, gfx::Rect(0, 0, 500, 500));
628 const DisplayInfo external_display_info =
629 CreateDisplayInfo(kExternalDisplayId, gfx::Rect(1, 1, 500, 500));
631 std::vector<DisplayInfo> display_info_list_two_active;
632 display_info_list_two_active.push_back(internal_display_info);
633 display_info_list_two_active.push_back(external_display_info);
635 std::vector<DisplayInfo> display_info_list_one_active;
636 display_info_list_one_active.push_back(external_display_info);
638 // The DisplayInfo list with two active displays needs to be added first so
639 // that the DisplayManager can track the |internal_display_info| as inactive
640 // instead of non-existent.
641 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
642 display_manager->UpdateDisplays(display_info_list_two_active);
643 display_manager->UpdateDisplays(display_info_list_one_active);
645 test::ScopedSetInternalDisplayId set_internal(kInternalDisplayId);
647 ASSERT_NE(kNewRotation, display_manager->GetDisplayInfo(kInternalDisplayId)
648 .GetActiveRotation());
650 delegate()->SetDisplayRotation(kNewRotation,
651 gfx::Display::ROTATION_SOURCE_ACTIVE);
653 EXPECT_EQ(kNewRotation, display_manager->GetDisplayInfo(kInternalDisplayId)
654 .GetActiveRotation());
657 } // namespace ash