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_layout_height = 0.f;
69 params.resizer_rect = gfx::Rect();
70 params.is_fullscreen = false;
71 rwh->Send(new ViewMsg_Resize(rwh->GetRoutingID(), params));
74 int GetOrientationAngle() {
76 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
77 "screen.orientation.angle")->GetAsInteger(&angle);
81 std::string GetOrientationType() {
83 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
84 "screen.orientation.type")->GetAsString(&type);
88 bool ScreenOrientationSupported() {
90 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
91 "'orientation' in screen")->GetAsBoolean(&support);
95 bool WindowOrientationSupported() {
97 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
98 "'orientation' in window")->GetAsBoolean(&support);
102 int GetWindowOrientationAngle() {
104 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
105 "window.orientation")->GetAsInteger(&angle);
110 DISALLOW_COPY_AND_ASSIGN(ScreenOrientationBrowserTest);
113 // This test doesn't work on MacOS X but the reason is mostly because it is not
114 // used Aura. It could be set as !defined(OS_MACOSX) but the rule below will
115 // actually support MacOS X if and when it switches to Aura.
116 #if defined(USE_AURA) || defined(OS_ANDROID)
117 IN_PROC_BROWSER_TEST_F(ScreenOrientationBrowserTest, ScreenOrientationChange) {
118 std::string types[] = { "portrait-primary",
119 "portrait-secondary",
121 "landscape-secondary" };
122 GURL test_url = GetTestUrl("screen_orientation",
123 "screen_orientation_screenorientationchange.html");
125 TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
126 shell()->LoadURL(test_url);
127 navigation_observer.Wait();
129 WaitForResizeComplete(shell()->web_contents());
133 // Screen Orientation is currently disabled on Windows 8.
134 // This test will break, requiring an update when the API will be enabled.
135 if (base::win::OSInfo::GetInstance()->version() >= base::win::VERSION_WIN8) {
136 EXPECT_EQ(false, ScreenOrientationSupported());
139 #endif // defined(OS_WIN)
141 int angle = GetOrientationAngle();
143 for (int i = 0; i < 4; ++i) {
144 angle = (angle + 90) % 360;
145 SendFakeScreenOrientation(angle, types[i]);
147 TestNavigationObserver navigation_observer(shell()->web_contents());
148 navigation_observer.Wait();
149 EXPECT_EQ(angle, GetOrientationAngle());
150 EXPECT_EQ(types[i], GetOrientationType());
153 #endif // defined(USE_AURA) || defined(OS_ANDROID)
155 IN_PROC_BROWSER_TEST_F(ScreenOrientationBrowserTest, WindowOrientationChange) {
156 GURL test_url = GetTestUrl("screen_orientation",
157 "screen_orientation_windoworientationchange.html");
159 TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
160 shell()->LoadURL(test_url);
161 navigation_observer.Wait();
163 WaitForResizeComplete(shell()->web_contents());
166 if (!WindowOrientationSupported())
169 int angle = GetWindowOrientationAngle();
171 for (int i = 0; i < 4; ++i) {
172 angle = (angle + 90) % 360;
173 SendFakeScreenOrientation(angle, "portrait-primary");
175 TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
176 navigation_observer.Wait();
177 EXPECT_EQ(angle == 270 ? -90 : angle, GetWindowOrientationAngle());
181 // Chromium Android does not support fullscreen
182 IN_PROC_BROWSER_TEST_F(ScreenOrientationBrowserTest, LockSmoke) {
183 GURL test_url = GetTestUrl("screen_orientation",
184 "screen_orientation_lock_smoke.html");
186 TestNavigationObserver navigation_observer(shell()->web_contents(), 2);
187 shell()->LoadURL(test_url);
190 // Screen Orientation is currently disabled on Windows 8.
191 // This test will break, requiring an update when the API will be enabled.
192 if (base::win::OSInfo::GetInstance()->version() >= base::win::VERSION_WIN8) {
193 EXPECT_EQ(false, ScreenOrientationSupported());
196 #endif // defined(OS_WIN)
198 navigation_observer.Wait();
200 WaitForResizeComplete(shell()->web_contents());
203 std::string expected =
204 #if defined(OS_ANDROID)
205 "SecurityError"; // WebContents need to be fullscreen.
207 "NotSupportedError"; // Locking isn't supported.
210 EXPECT_EQ(expected, shell()->web_contents()->GetLastCommittedURL().ref());
213 // Check that using screen orientation after a frame is detached doesn't crash
214 // the renderer process.
215 // This could be a LayoutTest if they were not using a mock screen orientation
217 IN_PROC_BROWSER_TEST_F(ScreenOrientationBrowserTest, CrashTest_UseAfterDetach) {
218 GURL test_url = GetTestUrl("screen_orientation",
219 "screen_orientation_use_after_detach.html");
221 TestNavigationObserver navigation_observer(shell()->web_contents(), 2);
222 shell()->LoadURL(test_url);
225 // Screen Orientation is currently disabled on Windows 8.
226 // When implemented, this test will break, requiring an update.
227 if (base::win::OSInfo::GetInstance()->version() >= base::win::VERSION_WIN8) {
228 EXPECT_EQ(false, ScreenOrientationSupported());
231 #endif // defined(OS_WIN)
233 navigation_observer.Wait();
235 // This is a success if the renderer process did not crash, thus, we end up
239 } // namespace content