Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / screen_orientation / screen_orientation_browsertest.cc
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 <stdlib.h>
6
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"
23
24 #if defined(OS_WIN)
25 #include "base/win/windows_version.h"
26 #endif // OS_WIN
27
28 namespace content {
29
30 class ScreenOrientationBrowserTest : public ContentBrowserTest  {
31  public:
32   ScreenOrientationBrowserTest() {
33   }
34
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();
39
40     ContentBrowserTest::SetUp();
41   }
42
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;
50
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;
60     }
61     ASSERT_NE(blink::WebScreenOrientationUndefined, type);
62     screen_info.orientationType = type;
63
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));
72   }
73
74   int GetOrientationAngle() {
75     int angle;
76     ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
77                              "screen.orientation.angle")->GetAsInteger(&angle);
78     return angle;
79   }
80
81   std::string GetOrientationType() {
82     std::string type;
83     ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
84                              "screen.orientation.type")->GetAsString(&type);
85     return type;
86   }
87
88   bool ScreenOrientationSupported() {
89     bool support;
90     ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
91                              "'orientation' in screen")->GetAsBoolean(&support);
92     return support;
93   }
94
95   bool WindowOrientationSupported() {
96     bool support;
97     ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
98                              "'orientation' in window")->GetAsBoolean(&support);
99     return support;
100   }
101
102   int GetWindowOrientationAngle() {
103     int angle;
104     ExecuteScriptAndGetValue(shell()->web_contents()->GetMainFrame(),
105                              "window.orientation")->GetAsInteger(&angle);
106     return angle;
107   }
108
109  private:
110   DISALLOW_COPY_AND_ASSIGN(ScreenOrientationBrowserTest);
111 };
112
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",
120                           "landscape-primary",
121                           "landscape-secondary" };
122   GURL test_url = GetTestUrl("screen_orientation",
123                              "screen_orientation_screenorientationchange.html");
124
125   TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
126   shell()->LoadURL(test_url);
127   navigation_observer.Wait();
128 #if USE_AURA
129   WaitForResizeComplete(shell()->web_contents());
130 #endif // USE_AURA
131
132 #if defined(OS_WIN)
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());
137     return;
138   }
139 #endif // defined(OS_WIN)
140
141   int angle = GetOrientationAngle();
142
143   for (int i = 0; i < 4; ++i) {
144     angle = (angle + 90) % 360;
145     SendFakeScreenOrientation(angle, types[i]);
146
147     TestNavigationObserver navigation_observer(shell()->web_contents());
148     navigation_observer.Wait();
149     EXPECT_EQ(angle, GetOrientationAngle());
150     EXPECT_EQ(types[i], GetOrientationType());
151   }
152 }
153 #endif // defined(USE_AURA) || defined(OS_ANDROID)
154
155 IN_PROC_BROWSER_TEST_F(ScreenOrientationBrowserTest, WindowOrientationChange) {
156   GURL test_url = GetTestUrl("screen_orientation",
157                              "screen_orientation_windoworientationchange.html");
158
159   TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
160   shell()->LoadURL(test_url);
161   navigation_observer.Wait();
162 #if USE_AURA
163   WaitForResizeComplete(shell()->web_contents());
164 #endif // USE_AURA
165
166   if (!WindowOrientationSupported())
167     return;
168
169   int angle = GetWindowOrientationAngle();
170
171   for (int i = 0; i < 4; ++i) {
172     angle = (angle + 90) % 360;
173     SendFakeScreenOrientation(angle, "portrait-primary");
174
175     TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
176     navigation_observer.Wait();
177     EXPECT_EQ(angle == 270 ? -90 : angle, GetWindowOrientationAngle());
178   }
179 }
180
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");
185
186   TestNavigationObserver navigation_observer(shell()->web_contents(), 2);
187   shell()->LoadURL(test_url);
188
189 #if defined(OS_WIN)
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());
194     return;
195   }
196 #endif // defined(OS_WIN)
197
198   navigation_observer.Wait();
199 #if USE_AURA
200   WaitForResizeComplete(shell()->web_contents());
201 #endif // USE_AURA
202
203   std::string expected =
204 #if defined(OS_ANDROID)
205       "SecurityError"; // WebContents need to be fullscreen.
206 #else
207       "NotSupportedError"; // Locking isn't supported.
208 #endif
209
210   EXPECT_EQ(expected, shell()->web_contents()->GetLastCommittedURL().ref());
211 }
212
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
216 // controller.
217 IN_PROC_BROWSER_TEST_F(ScreenOrientationBrowserTest, CrashTest_UseAfterDetach) {
218   GURL test_url = GetTestUrl("screen_orientation",
219                              "screen_orientation_use_after_detach.html");
220
221   TestNavigationObserver navigation_observer(shell()->web_contents(), 2);
222   shell()->LoadURL(test_url);
223
224 #if defined(OS_WIN)
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());
229     return;
230   }
231 #endif // defined(OS_WIN)
232
233   navigation_observer.Wait();
234
235   // This is a success if the renderer process did not crash, thus, we end up
236   // here.
237 }
238
239 } // namespace content