Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / screen_orientation / screen_orientation_browsertest.cc
blob4b1d40b307b2d4e7c9f182ba345174d4c2e76dc5
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 <stdlib.h>
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"
24 #if defined(OS_WIN)
25 #include "base/win/windows_version.h"
26 #endif // OS_WIN
28 namespace content {
30 class ScreenOrientationBrowserTest : public ContentBrowserTest {
31 public:
32 ScreenOrientationBrowserTest() {
35 protected:
36 void SendFakeScreenOrientation(unsigned angle, const std::string& strType) {
37 RenderWidgetHost* rwh = shell()->web_contents()->GetRenderWidgetHostView()
38 ->GetRenderWidgetHost();
39 blink::WebScreenInfo screen_info;
40 rwh->GetWebScreenInfo(&screen_info);
41 screen_info.orientationAngle = angle;
43 blink::WebScreenOrientationType type = blink::WebScreenOrientationUndefined;
44 if (strType == "portrait-primary") {
45 type = blink::WebScreenOrientationPortraitPrimary;
46 } else if (strType == "portrait-secondary") {
47 type = blink::WebScreenOrientationPortraitSecondary;
48 } else if (strType == "landscape-primary") {
49 type = blink::WebScreenOrientationLandscapePrimary;
50 } else if (strType == "landscape-secondary") {
51 type = blink::WebScreenOrientationLandscapeSecondary;
53 ASSERT_NE(blink::WebScreenOrientationUndefined, type);
54 screen_info.orientationType = type;
56 ViewMsg_Resize_Params params;
57 params.screen_info = screen_info;
58 params.new_size = gfx::Size(0, 0);
59 params.physical_backing_size = gfx::Size(300, 300);
60 params.top_controls_height = 0.f;
61 params.top_controls_shrink_blink_size = false;
62 params.resizer_rect = gfx::Rect();
63 params.is_fullscreen = false;
64 rwh->Send(new ViewMsg_Resize(rwh->GetRoutingID(), params));
67 int GetOrientationAngle() {
68 int angle;
69 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
70 "screen.orientation.angle")->GetAsInteger(&angle);
71 return angle;
74 std::string GetOrientationType() {
75 std::string type;
76 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
77 "screen.orientation.type")->GetAsString(&type);
78 return type;
81 bool ScreenOrientationSupported() {
82 bool support;
83 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
84 "'orientation' in screen")->GetAsBoolean(&support);
85 return support;
88 bool WindowOrientationSupported() {
89 bool support;
90 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
91 "'orientation' in window")->GetAsBoolean(&support);
92 return support;
95 int GetWindowOrientationAngle() {
96 int angle;
97 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
98 "window.orientation")->GetAsInteger(&angle);
99 return angle;
102 private:
103 DISALLOW_COPY_AND_ASSIGN(ScreenOrientationBrowserTest);
106 // This test doesn't work on MacOS X but the reason is mostly because it is not
107 // used Aura. It could be set as !defined(OS_MACOSX) but the rule below will
108 // actually support MacOS X if and when it switches to Aura.
109 #if defined(USE_AURA) || defined(OS_ANDROID)
110 // Flaky on Chrome OS: http://crbug.com/468259
111 #if defined(OS_CHROMEOS)
112 #define MAYBE_ScreenOrientationChange DISABLED_ScreenOrientationChange
113 #else
114 #define MAYBE_ScreenOrientationChange ScreenOrientationChange
115 #endif
116 IN_PROC_BROWSER_TEST_F(ScreenOrientationBrowserTest,
117 MAYBE_ScreenOrientationChange) {
118 std::string types[] = { "portrait-primary",
119 "portrait-secondary",
120 "landscape-primary",
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();
128 WaitForResizeComplete(shell()->web_contents());
130 #if defined(OS_WIN)
131 // Screen Orientation is currently disabled on Windows 8.
132 // This test will break, requiring an update when the API will be enabled.
133 if (base::win::OSInfo::GetInstance()->version() >= base::win::VERSION_WIN8) {
134 EXPECT_EQ(false, ScreenOrientationSupported());
135 return;
137 #endif // defined(OS_WIN)
139 int angle = GetOrientationAngle();
141 for (int i = 0; i < 4; ++i) {
142 angle = (angle + 90) % 360;
143 SendFakeScreenOrientation(angle, types[i]);
145 TestNavigationObserver navigation_observer(shell()->web_contents());
146 navigation_observer.Wait();
147 EXPECT_EQ(angle, GetOrientationAngle());
148 EXPECT_EQ(types[i], GetOrientationType());
151 #endif // defined(USE_AURA) || defined(OS_ANDROID)
153 // Flaky on Chrome OS: http://crbug.com/468259
154 #if defined(OS_CHROMEOS)
155 #define MAYBE_WindowOrientationChange DISABLED_WindowOrientationChange
156 #else
157 #define MAYBE_WindowOrientationChange WindowOrientationChange
158 #endif
159 IN_PROC_BROWSER_TEST_F(ScreenOrientationBrowserTest,
160 MAYBE_WindowOrientationChange) {
161 GURL test_url = GetTestUrl("screen_orientation",
162 "screen_orientation_windoworientationchange.html");
164 TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
165 shell()->LoadURL(test_url);
166 navigation_observer.Wait();
167 #if USE_AURA || defined(OS_ANDROID)
168 WaitForResizeComplete(shell()->web_contents());
169 #endif // USE_AURA || defined(OS_ANDROID)
171 if (!WindowOrientationSupported())
172 return;
174 int angle = GetWindowOrientationAngle();
176 for (int i = 0; i < 4; ++i) {
177 angle = (angle + 90) % 360;
178 SendFakeScreenOrientation(angle, "portrait-primary");
180 TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
181 navigation_observer.Wait();
182 EXPECT_EQ(angle == 270 ? -90 : angle, GetWindowOrientationAngle());
186 // LockSmoke test seems to have become flaky on all non-ChromeOS platforms.
187 // The cause is unfortunately unknown. See https://crbug.com/448876
188 // Chromium Android does not support fullscreen
189 IN_PROC_BROWSER_TEST_F(ScreenOrientationBrowserTest, DISABLED_LockSmoke) {
190 GURL test_url = GetTestUrl("screen_orientation",
191 "screen_orientation_lock_smoke.html");
193 TestNavigationObserver navigation_observer(shell()->web_contents(), 2);
194 shell()->LoadURL(test_url);
196 #if defined(OS_WIN)
197 // Screen Orientation is currently disabled on Windows 8.
198 // This test will break, requiring an update when the API will be enabled.
199 if (base::win::OSInfo::GetInstance()->version() >= base::win::VERSION_WIN8) {
200 EXPECT_EQ(false, ScreenOrientationSupported());
201 return;
203 #endif // defined(OS_WIN)
205 navigation_observer.Wait();
206 #if USE_AURA || defined(OS_ANDROID)
207 WaitForResizeComplete(shell()->web_contents());
208 #endif // USE_AURA || defined(OS_ANDROID)
210 std::string expected =
211 #if defined(OS_ANDROID)
212 "SecurityError"; // WebContents need to be fullscreen.
213 #else
214 "NotSupportedError"; // Locking isn't supported.
215 #endif
217 EXPECT_EQ(expected, shell()->web_contents()->GetLastCommittedURL().ref());
220 // Check that using screen orientation after a frame is detached doesn't crash
221 // the renderer process.
222 // This could be a LayoutTest if they were not using a mock screen orientation
223 // controller.
224 IN_PROC_BROWSER_TEST_F(ScreenOrientationBrowserTest, CrashTest_UseAfterDetach) {
225 GURL test_url = GetTestUrl("screen_orientation",
226 "screen_orientation_use_after_detach.html");
228 TestNavigationObserver navigation_observer(shell()->web_contents(), 2);
229 shell()->LoadURL(test_url);
231 #if defined(OS_WIN)
232 // Screen Orientation is currently disabled on Windows 8.
233 // When implemented, this test will break, requiring an update.
234 if (base::win::OSInfo::GetInstance()->version() >= base::win::VERSION_WIN8) {
235 EXPECT_EQ(false, ScreenOrientationSupported());
236 return;
238 #endif // defined(OS_WIN)
240 navigation_observer.Wait();
242 // This is a success if the renderer process did not crash, thus, we end up
243 // here.
246 #if defined(OS_ANDROID)
247 class ScreenOrientationLockDisabledBrowserTest : public ContentBrowserTest {
248 public:
249 ScreenOrientationLockDisabledBrowserTest() {}
250 ~ScreenOrientationLockDisabledBrowserTest() override {}
252 void SetUpCommandLine(base::CommandLine* command_line) override {
253 command_line->AppendSwitch(switches::kDisableScreenOrientationLock);
257 // Check that when --disable-screen-orientation-lock is passed to the command
258 // line, screen.orientation.lock() correctly reports to not be supported.
259 IN_PROC_BROWSER_TEST_F(ScreenOrientationLockDisabledBrowserTest, NotSupported) {
260 GURL test_url = GetTestUrl("screen_orientation",
261 "screen_orientation_lock_disabled.html");
263 TestNavigationObserver navigation_observer(shell()->web_contents(), 2);
264 shell()->LoadURL(test_url);
265 navigation_observer.Wait();
267 EXPECT_EQ("NotSupportedError",
268 shell()->web_contents()->GetLastCommittedURL().ref());
270 #endif // defined(OS_ANDROID)
272 } // namespace content