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