1 // Copyright (c) 2012 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 "content/public/test/layouttest_support.h"
7 #include "base/callback.h"
8 #include "base/lazy_instance.h"
9 #include "content/browser/renderer_host/render_widget_host_impl.h"
10 #include "content/common/gpu/image_transport_surface.h"
11 #include "content/public/common/page_state.h"
12 #include "content/renderer/history_entry.h"
13 #include "content/renderer/history_serialization.h"
14 #include "content/renderer/render_frame_impl.h"
15 #include "content/renderer/render_thread_impl.h"
16 #include "content/renderer/render_view_impl.h"
17 #include "content/renderer/renderer_webkitplatformsupport_impl.h"
18 #include "content/shell/renderer/test_runner/TestCommon.h"
19 #include "content/shell/renderer/test_runner/WebFrameTestProxy.h"
20 #include "content/shell/renderer/test_runner/WebTestProxy.h"
21 #include "content/test/test_media_stream_client.h"
22 #include "third_party/WebKit/public/platform/WebDeviceMotionData.h"
23 #include "third_party/WebKit/public/platform/WebDeviceOrientationData.h"
24 #include "third_party/WebKit/public/platform/WebGamepads.h"
25 #include "third_party/WebKit/public/web/WebHistoryItem.h"
27 #if defined(OS_MACOSX)
28 #include "content/browser/renderer_host/popup_menu_helper_mac.h"
31 using blink::WebDeviceMotionData
;
32 using blink::WebDeviceOrientationData
;
33 using blink::WebGamepad
;
34 using blink::WebGamepads
;
42 base::LazyInstance
<base::Callback
<void(RenderView
*, WebTestProxyBase
*)> >::Leaky
43 g_callback
= LAZY_INSTANCE_INITIALIZER
;
45 RenderViewImpl
* CreateWebTestProxy(RenderViewImplParams
* params
) {
46 typedef WebTestProxy
<RenderViewImpl
, RenderViewImplParams
*> ProxyType
;
47 ProxyType
* render_view_proxy
= new ProxyType(params
);
49 return render_view_proxy
;
50 g_callback
.Get().Run(render_view_proxy
, render_view_proxy
);
51 return render_view_proxy
;
54 WebTestProxyBase
* GetWebTestProxyBase(RenderViewImpl
* render_view
) {
55 typedef WebTestProxy
<RenderViewImpl
, RenderViewImplParams
*> ViewProxy
;
57 ViewProxy
* render_view_proxy
= static_cast<ViewProxy
*>(render_view
);
58 return static_cast<WebTestProxyBase
*>(render_view_proxy
);
61 RenderFrameImpl
* CreateWebFrameTestProxy(
62 RenderViewImpl
* render_view
,
64 typedef WebFrameTestProxy
<RenderFrameImpl
, RenderViewImpl
*, int32
> FrameProxy
;
66 FrameProxy
* render_frame_proxy
= new FrameProxy(render_view
, routing_id
);
67 render_frame_proxy
->setBaseProxy(GetWebTestProxyBase(render_view
));
69 UseMockMediaStreams(render_frame_proxy
);
71 return render_frame_proxy
;
77 void EnableWebTestProxyCreation(
78 const base::Callback
<void(RenderView
*, WebTestProxyBase
*)>& callback
) {
79 g_callback
.Get() = callback
;
80 RenderViewImpl::InstallCreateHook(CreateWebTestProxy
);
81 RenderFrameImpl::InstallCreateHook(CreateWebFrameTestProxy
);
84 void SetMockGamepads(const WebGamepads
& pads
) {
85 RendererWebKitPlatformSupportImpl::SetMockGamepadsForTesting(pads
);
88 void MockGamepadConnected(int index
, const WebGamepad
& pad
) {
89 RendererWebKitPlatformSupportImpl::MockGamepadConnected(index
, pad
);
92 void MockGamepadDisconnected(int index
, const WebGamepad
& pad
) {
93 RendererWebKitPlatformSupportImpl::MockGamepadDisconnected(index
, pad
);
96 void SetMockDeviceMotionData(const WebDeviceMotionData
& data
) {
97 RendererWebKitPlatformSupportImpl::SetMockDeviceMotionDataForTesting(data
);
100 void SetMockDeviceOrientationData(const WebDeviceOrientationData
& data
) {
101 RendererWebKitPlatformSupportImpl::
102 SetMockDeviceOrientationDataForTesting(data
);
105 void SetMockScreenOrientation(
106 const blink::WebScreenOrientationType
& orientation
) {
107 RendererWebKitPlatformSupportImpl::
108 SetMockScreenOrientationForTesting(orientation
);
111 void EnableRendererLayoutTestMode() {
112 RenderThreadImpl::current()->set_layout_test_mode(true);
115 void EnableBrowserLayoutTestMode() {
116 #if defined(OS_MACOSX)
117 ImageTransportSurface::SetAllowOSMesaForTesting(true);
118 PopupMenuHelper::DontShowPopupMenuForTesting();
120 RenderWidgetHostImpl::DisableResizeAckCheckForTesting();
123 int GetLocalSessionHistoryLength(RenderView
* render_view
) {
124 return static_cast<RenderViewImpl
*>(render_view
)->
125 GetLocalSessionHistoryLengthForTesting();
128 void SyncNavigationState(RenderView
* render_view
) {
129 static_cast<RenderViewImpl
*>(render_view
)->SyncNavigationState();
132 void SetFocusAndActivate(RenderView
* render_view
, bool enable
) {
133 static_cast<RenderViewImpl
*>(render_view
)->
134 SetFocusAndActivateForTesting(enable
);
137 void ForceResizeRenderView(RenderView
* render_view
,
138 const WebSize
& new_size
) {
139 RenderViewImpl
* render_view_impl
= static_cast<RenderViewImpl
*>(render_view
);
140 render_view_impl
->ForceResizeForTesting(new_size
);
141 GetWebTestProxyBase(render_view_impl
)->didForceResize();
144 void SetDeviceScaleFactor(RenderView
* render_view
, float factor
) {
145 static_cast<RenderViewImpl
*>(render_view
)->
146 SetDeviceScaleFactorForTesting(factor
);
149 void UseSynchronousResizeMode(RenderView
* render_view
, bool enable
) {
150 static_cast<RenderViewImpl
*>(render_view
)->
151 UseSynchronousResizeModeForTesting(enable
);
154 void EnableAutoResizeMode(RenderView
* render_view
,
155 const WebSize
& min_size
,
156 const WebSize
& max_size
) {
157 static_cast<RenderViewImpl
*>(render_view
)->
158 EnableAutoResizeForTesting(min_size
, max_size
);
161 void DisableAutoResizeMode(RenderView
* render_view
, const WebSize
& new_size
) {
162 static_cast<RenderViewImpl
*>(render_view
)->
163 DisableAutoResizeForTesting(new_size
);
166 void UseMockMediaStreams(RenderFrame
* render_frame
) {
167 RenderFrameImpl
* render_frame_impl
= static_cast<RenderFrameImpl
*>(
169 render_frame_impl
->SetMediaStreamClientForTesting(
170 new TestMediaStreamClient(render_frame_impl
));
174 base::char16
operator()(base::char16 c
) { return tolower(c
); }
177 // Returns True if node1 < node2.
178 bool HistoryEntryCompareLess(HistoryEntry::HistoryNode
* node1
,
179 HistoryEntry::HistoryNode
* node2
) {
180 base::string16 target1
= node1
->item().target();
181 base::string16 target2
= node2
->item().target();
182 std::transform(target1
.begin(), target1
.end(), target1
.begin(), ToLower());
183 std::transform(target2
.begin(), target2
.end(), target2
.begin(), ToLower());
184 return target1
< target2
;
187 std::string
DumpHistoryItem(HistoryEntry::HistoryNode
* node
,
189 bool is_current_index
) {
192 const blink::WebHistoryItem
& item
= node
->item();
193 if (is_current_index
) {
194 result
.append("curr->");
195 result
.append(indent
- 6, ' '); // 6 == "curr->".length()
197 result
.append(indent
, ' ');
200 std::string url
= normalizeLayoutTestURL(item
.urlString().utf8());
202 if (!item
.target().isEmpty()) {
203 result
.append(" (in frame \"");
204 result
.append(item
.target().utf8());
205 result
.append("\")");
209 std::vector
<HistoryEntry::HistoryNode
*> children
= node
->children();
210 if (!children
.empty()) {
211 std::sort(children
.begin(), children
.end(), HistoryEntryCompareLess
);
212 for (size_t i
= 0; i
< children
.size(); ++i
)
213 result
+= DumpHistoryItem(children
[i
], indent
+ 4, false);
219 std::string
DumpBackForwardList(std::vector
<PageState
>& page_state
,
220 size_t current_index
) {
222 result
.append("\n============== Back Forward List ==============\n");
223 for (size_t index
= 0; index
< page_state
.size(); ++index
) {
224 scoped_ptr
<HistoryEntry
> entry(
225 PageStateToHistoryEntry(page_state
[index
]));
227 DumpHistoryItem(entry
->root_history_node(),
229 index
== current_index
));
231 result
.append("===============================================\n");
235 } // namespace content