Change next_proto member type.
[chromium-blink-merge.git] / content / browser / screen_orientation / screen_orientation_browsertest.cc
blob4ef1bccc4093914cbb4e6f7e76d71180655b2bd1
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 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();
43 protected:
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() {
76 int angle;
77 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
78 "screen.orientation.angle")->GetAsInteger(&angle);
79 return angle;
82 std::string GetOrientationType() {
83 std::string type;
84 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
85 "screen.orientation.type")->GetAsString(&type);
86 return type;
89 bool ScreenOrientationSupported() {
90 bool support;
91 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
92 "'orientation' in screen")->GetAsBoolean(&support);
93 return support;
96 bool WindowOrientationSupported() {
97 bool support;
98 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
99 "'orientation' in window")->GetAsBoolean(&support);
100 return support;
103 int GetWindowOrientationAngle() {
104 int angle;
105 ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
106 "window.orientation")->GetAsInteger(&angle);
107 return angle;
110 private:
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",
121 "landscape-primary",
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();
129 #if USE_AURA
130 WaitForResizeComplete(shell()->web_contents());
131 #endif // USE_AURA
133 #if defined(OS_WIN)
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());
138 return;
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();
163 #if USE_AURA
164 WaitForResizeComplete(shell()->web_contents());
165 #endif // USE_AURA
167 if (!WindowOrientationSupported())
168 return;
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);
190 #if defined(OS_WIN)
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());
195 return;
197 #endif // defined(OS_WIN)
199 navigation_observer.Wait();
200 #if USE_AURA
201 WaitForResizeComplete(shell()->web_contents());
202 #endif // USE_AURA
204 std::string expected =
205 #if defined(OS_ANDROID)
206 "SecurityError"; // WebContents need to be fullscreen.
207 #else
208 "NotSupportedError"; // Locking isn't supported.
209 #endif
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
217 // controller.
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);
225 #if defined(OS_WIN)
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());
230 return;
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
237 // here.
240 } // namespace content