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.
7 #include "base/command_line.h"
8 #include "content/browser/renderer_host/render_widget_host_impl.h"
9 #include "content/common/view_messages.h"
10 #include "content/public/browser/render_widget_host.h"
11 #include "content/public/browser/render_widget_host_view.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/common/content_switches.h"
14 #include "content/public/test/browser_test_utils.h"
15 #include "content/public/test/content_browser_test.h"
16 #include "content/public/test/content_browser_test_utils.h"
17 #include "content/public/test/test_navigation_observer.h"
18 #include "content/public/test/test_utils.h"
19 #include "content/shell/browser/shell.h"
20 #include "content/shell/common/shell_switches.h"
21 #include "third_party/WebKit/public/platform/WebScreenInfo.h"
22 #include "ui/compositor/compositor_switches.h"
25 #include "base/win/windows_version.h"
30 class ScreenOrientationBrowserTest
: public ContentBrowserTest
{
32 ScreenOrientationBrowserTest() {
35 void SetUp() override
{
36 // Painting has to happen otherwise the Resize messages will be added on top
37 // of each other without properly ack-painting which will fail and crash.
38 UseSoftwareCompositing();
40 ContentBrowserTest::SetUp();
44 void SendFakeScreenOrientation(unsigned angle
, const std::string
& strType
) {
45 RenderWidgetHost
* rwh
= shell()->web_contents()->GetRenderWidgetHostView()
46 ->GetRenderWidgetHost();
47 blink::WebScreenInfo screen_info
;
48 rwh
->GetWebScreenInfo(&screen_info
);
49 screen_info
.orientationAngle
= angle
;
51 blink::WebScreenOrientationType type
= blink::WebScreenOrientationUndefined
;
52 if (strType
== "portrait-primary") {
53 type
= blink::WebScreenOrientationPortraitPrimary
;
54 } else if (strType
== "portrait-secondary") {
55 type
= blink::WebScreenOrientationPortraitSecondary
;
56 } else if (strType
== "landscape-primary") {
57 type
= blink::WebScreenOrientationLandscapePrimary
;
58 } else if (strType
== "landscape-secondary") {
59 type
= blink::WebScreenOrientationLandscapeSecondary
;
61 ASSERT_NE(blink::WebScreenOrientationUndefined
, type
);
62 screen_info
.orientationType
= type
;
64 ViewMsg_Resize_Params params
;
65 params
.screen_info
= screen_info
;
66 params
.new_size
= gfx::Size(0, 0);
67 params
.physical_backing_size
= gfx::Size(300, 300);
68 params
.top_controls_height
= 0.f
;
69 params
.top_controls_shrink_blink_size
= false;
70 params
.resizer_rect
= gfx::Rect();
71 params
.is_fullscreen
= false;
72 rwh
->Send(new ViewMsg_Resize(rwh
->GetRoutingID(), params
));
75 int GetOrientationAngle() {
77 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
78 "screen.orientation.angle")->GetAsInteger(&angle
);
82 std::string
GetOrientationType() {
84 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
85 "screen.orientation.type")->GetAsString(&type
);
89 bool ScreenOrientationSupported() {
91 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
92 "'orientation' in screen")->GetAsBoolean(&support
);
96 bool WindowOrientationSupported() {
98 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
99 "'orientation' in window")->GetAsBoolean(&support
);
103 int GetWindowOrientationAngle() {
105 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
106 "window.orientation")->GetAsInteger(&angle
);
111 DISALLOW_COPY_AND_ASSIGN(ScreenOrientationBrowserTest
);
114 // This test doesn't work on MacOS X but the reason is mostly because it is not
115 // used Aura. It could be set as !defined(OS_MACOSX) but the rule below will
116 // actually support MacOS X if and when it switches to Aura.
117 #if defined(USE_AURA) || defined(OS_ANDROID)
118 IN_PROC_BROWSER_TEST_F(ScreenOrientationBrowserTest
, ScreenOrientationChange
) {
119 std::string types
[] = { "portrait-primary",
120 "portrait-secondary",
122 "landscape-secondary" };
123 GURL test_url
= GetTestUrl("screen_orientation",
124 "screen_orientation_screenorientationchange.html");
126 TestNavigationObserver
navigation_observer(shell()->web_contents(), 1);
127 shell()->LoadURL(test_url
);
128 navigation_observer
.Wait();
130 WaitForResizeComplete(shell()->web_contents());
134 // Screen Orientation is currently disabled on Windows 8.
135 // This test will break, requiring an update when the API will be enabled.
136 if (base::win::OSInfo::GetInstance()->version() >= base::win::VERSION_WIN8
) {
137 EXPECT_EQ(false, ScreenOrientationSupported());
140 #endif // defined(OS_WIN)
142 int angle
= GetOrientationAngle();
144 for (int i
= 0; i
< 4; ++i
) {
145 angle
= (angle
+ 90) % 360;
146 SendFakeScreenOrientation(angle
, types
[i
]);
148 TestNavigationObserver
navigation_observer(shell()->web_contents());
149 navigation_observer
.Wait();
150 EXPECT_EQ(angle
, GetOrientationAngle());
151 EXPECT_EQ(types
[i
], GetOrientationType());
154 #endif // defined(USE_AURA) || defined(OS_ANDROID)
156 IN_PROC_BROWSER_TEST_F(ScreenOrientationBrowserTest
, WindowOrientationChange
) {
157 GURL test_url
= GetTestUrl("screen_orientation",
158 "screen_orientation_windoworientationchange.html");
160 TestNavigationObserver
navigation_observer(shell()->web_contents(), 1);
161 shell()->LoadURL(test_url
);
162 navigation_observer
.Wait();
164 WaitForResizeComplete(shell()->web_contents());
167 if (!WindowOrientationSupported())
170 int angle
= GetWindowOrientationAngle();
172 for (int i
= 0; i
< 4; ++i
) {
173 angle
= (angle
+ 90) % 360;
174 SendFakeScreenOrientation(angle
, "portrait-primary");
176 TestNavigationObserver
navigation_observer(shell()->web_contents(), 1);
177 navigation_observer
.Wait();
178 EXPECT_EQ(angle
== 270 ? -90 : angle
, GetWindowOrientationAngle());
182 // Chromium Android does not support fullscreen
183 IN_PROC_BROWSER_TEST_F(ScreenOrientationBrowserTest
, LockSmoke
) {
184 GURL test_url
= GetTestUrl("screen_orientation",
185 "screen_orientation_lock_smoke.html");
187 TestNavigationObserver
navigation_observer(shell()->web_contents(), 2);
188 shell()->LoadURL(test_url
);
191 // Screen Orientation is currently disabled on Windows 8.
192 // This test will break, requiring an update when the API will be enabled.
193 if (base::win::OSInfo::GetInstance()->version() >= base::win::VERSION_WIN8
) {
194 EXPECT_EQ(false, ScreenOrientationSupported());
197 #endif // defined(OS_WIN)
199 navigation_observer
.Wait();
201 WaitForResizeComplete(shell()->web_contents());
204 std::string expected
=
205 #if defined(OS_ANDROID)
206 "SecurityError"; // WebContents need to be fullscreen.
208 "NotSupportedError"; // Locking isn't supported.
211 EXPECT_EQ(expected
, shell()->web_contents()->GetLastCommittedURL().ref());
214 // Check that using screen orientation after a frame is detached doesn't crash
215 // the renderer process.
216 // This could be a LayoutTest if they were not using a mock screen orientation
218 IN_PROC_BROWSER_TEST_F(ScreenOrientationBrowserTest
, CrashTest_UseAfterDetach
) {
219 GURL test_url
= GetTestUrl("screen_orientation",
220 "screen_orientation_use_after_detach.html");
222 TestNavigationObserver
navigation_observer(shell()->web_contents(), 2);
223 shell()->LoadURL(test_url
);
226 // Screen Orientation is currently disabled on Windows 8.
227 // When implemented, this test will break, requiring an update.
228 if (base::win::OSInfo::GetInstance()->version() >= base::win::VERSION_WIN8
) {
229 EXPECT_EQ(false, ScreenOrientationSupported());
232 #endif // defined(OS_WIN)
234 navigation_observer
.Wait();
236 // This is a success if the renderer process did not crash, thus, we end up
240 } // namespace content