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 "content/browser/renderer_host/render_widget_host_view_base.h"
6 
7 #include "content/browser/renderer_host/display_util.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "third_party/blink/public/common/screen_orientation/web_screen_orientation_type.h"
10 #include "ui/display/display.h"
11 
12 namespace content {
13 
14 namespace {
15 
CreateDisplay(int width,int height,display::Display::Rotation rotation)16 display::Display CreateDisplay(int width,
17                                int height,
18                                display::Display::Rotation rotation) {
19   display::Display display;
20   display.set_panel_rotation(rotation);
21   display.set_bounds(gfx::Rect(width, height));
22 
23   return display;
24 }
25 
26 } // anonymous namespace
27 
TEST(RenderWidgetHostViewBaseTest,OrientationTypeForMobile)28 TEST(RenderWidgetHostViewBaseTest, OrientationTypeForMobile) {
29   // Square display (width == height).
30   {
31     display::Display display =
32         CreateDisplay(100, 100, display::Display::ROTATE_0);
33     EXPECT_EQ(SCREEN_ORIENTATION_VALUES_PORTRAIT_PRIMARY,
34               DisplayUtil::GetOrientationTypeForMobile(display));
35 
36     display = CreateDisplay(200, 200, display::Display::ROTATE_90);
37     EXPECT_EQ(SCREEN_ORIENTATION_VALUES_LANDSCAPE_PRIMARY,
38               DisplayUtil::GetOrientationTypeForMobile(display));
39 
40     display = CreateDisplay(0, 0, display::Display::ROTATE_180);
41     EXPECT_EQ(SCREEN_ORIENTATION_VALUES_PORTRAIT_SECONDARY,
42               DisplayUtil::GetOrientationTypeForMobile(display));
43 
44     display = CreateDisplay(10000, 10000, display::Display::ROTATE_270);
45     EXPECT_EQ(SCREEN_ORIENTATION_VALUES_LANDSCAPE_SECONDARY,
46               DisplayUtil::GetOrientationTypeForMobile(display));
47   }
48 
49   // natural width > natural height.
50   {
51     display::Display display = CreateDisplay(1, 0, display::Display::ROTATE_0);
52     EXPECT_EQ(SCREEN_ORIENTATION_VALUES_LANDSCAPE_PRIMARY,
53               DisplayUtil::GetOrientationTypeForMobile(display));
54 
55     display = CreateDisplay(19999, 20000, display::Display::ROTATE_90);
56     EXPECT_EQ(SCREEN_ORIENTATION_VALUES_PORTRAIT_SECONDARY,
57               DisplayUtil::GetOrientationTypeForMobile(display));
58 
59     display = CreateDisplay(200, 100, display::Display::ROTATE_180);
60     EXPECT_EQ(SCREEN_ORIENTATION_VALUES_LANDSCAPE_SECONDARY,
61               DisplayUtil::GetOrientationTypeForMobile(display));
62 
63     display = CreateDisplay(1, 10000, display::Display::ROTATE_270);
64     EXPECT_EQ(SCREEN_ORIENTATION_VALUES_PORTRAIT_PRIMARY,
65               DisplayUtil::GetOrientationTypeForMobile(display));
66   }
67 
68   // natural width < natural height.
69   {
70     display::Display display = CreateDisplay(0, 1, display::Display::ROTATE_0);
71     EXPECT_EQ(SCREEN_ORIENTATION_VALUES_PORTRAIT_PRIMARY,
72               DisplayUtil::GetOrientationTypeForMobile(display));
73 
74     display = CreateDisplay(20000, 19999, display::Display::ROTATE_90);
75     EXPECT_EQ(SCREEN_ORIENTATION_VALUES_LANDSCAPE_PRIMARY,
76               DisplayUtil::GetOrientationTypeForMobile(display));
77 
78     display = CreateDisplay(100, 200, display::Display::ROTATE_180);
79     EXPECT_EQ(SCREEN_ORIENTATION_VALUES_PORTRAIT_SECONDARY,
80               DisplayUtil::GetOrientationTypeForMobile(display));
81 
82     display = CreateDisplay(10000, 1, display::Display::ROTATE_270);
83     EXPECT_EQ(SCREEN_ORIENTATION_VALUES_LANDSCAPE_SECONDARY,
84               DisplayUtil::GetOrientationTypeForMobile(display));
85   }
86 }
87 
TEST(RenderWidgetHostViewBaseTest,OrientationTypeForDesktop)88 TEST(RenderWidgetHostViewBaseTest, OrientationTypeForDesktop) {
89   // On Desktop, the primary orientation is the first computed one so a test
90   // similar to OrientationTypeForMobile is not possible.
91   // Instead this test will only check one configuration and verify that the
92   // method reports two landscape and two portrait orientations with one primary
93   // and one secondary for each.
94 
95   // natural width > natural height.
96   {
97     display::Display display = CreateDisplay(1, 0, display::Display::ROTATE_0);
98     ScreenOrientationValues landscape_1 =
99         DisplayUtil::GetOrientationTypeForDesktop(display);
100     EXPECT_TRUE(landscape_1 == SCREEN_ORIENTATION_VALUES_LANDSCAPE_PRIMARY ||
101                 landscape_1 == SCREEN_ORIENTATION_VALUES_LANDSCAPE_SECONDARY);
102 
103     display = CreateDisplay(200, 100, display::Display::ROTATE_180);
104     ScreenOrientationValues landscape_2 =
105         DisplayUtil::GetOrientationTypeForDesktop(display);
106     EXPECT_TRUE(landscape_2 == SCREEN_ORIENTATION_VALUES_LANDSCAPE_PRIMARY ||
107                 landscape_2 == SCREEN_ORIENTATION_VALUES_LANDSCAPE_SECONDARY);
108 
109     EXPECT_NE(landscape_1, landscape_2);
110 
111     display = CreateDisplay(19999, 20000, display::Display::ROTATE_90);
112     ScreenOrientationValues portrait_1 =
113         DisplayUtil::GetOrientationTypeForDesktop(display);
114     EXPECT_TRUE(portrait_1 == SCREEN_ORIENTATION_VALUES_PORTRAIT_PRIMARY ||
115                 portrait_1 == SCREEN_ORIENTATION_VALUES_PORTRAIT_SECONDARY);
116 
117     display = CreateDisplay(1, 10000, display::Display::ROTATE_270);
118     ScreenOrientationValues portrait_2 =
119         DisplayUtil::GetOrientationTypeForDesktop(display);
120     EXPECT_TRUE(portrait_2 == SCREEN_ORIENTATION_VALUES_PORTRAIT_PRIMARY ||
121                 portrait_2 == SCREEN_ORIENTATION_VALUES_PORTRAIT_SECONDARY);
122 
123     EXPECT_NE(portrait_1, portrait_2);
124 
125   }
126 }
127 
128 } // namespace content
129