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 #ifndef CONTENT_SHELL_RENDERER_TEST_RUNNER_WEB_TEST_PROXY_H_
6 #define CONTENT_SHELL_RENDERER_TEST_RUNNER_WEB_TEST_PROXY_H_
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "content/shell/renderer/test_runner/web_task.h"
16 #include "third_party/WebKit/public/platform/WebImage.h"
17 #include "third_party/WebKit/public/platform/WebRect.h"
18 #include "third_party/WebKit/public/platform/WebScreenInfo.h"
19 #include "third_party/WebKit/public/platform/WebURLError.h"
20 #include "third_party/WebKit/public/platform/WebURLRequest.h"
21 #include "third_party/WebKit/public/web/WebAXEnums.h"
22 #include "third_party/WebKit/public/web/WebDOMMessageEvent.h"
23 #include "third_party/WebKit/public/web/WebDataSource.h"
24 #include "third_party/WebKit/public/web/WebDragOperation.h"
25 #include "third_party/WebKit/public/web/WebFrame.h"
26 #include "third_party/WebKit/public/web/WebFrameClient.h"
27 #include "third_party/WebKit/public/web/WebHistoryCommitType.h"
28 #include "third_party/WebKit/public/web/WebIconURL.h"
29 #include "third_party/WebKit/public/web/WebNavigationPolicy.h"
30 #include "third_party/WebKit/public/web/WebNavigationType.h"
31 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
32 #include "third_party/WebKit/public/web/WebTextAffinity.h"
33 #include "third_party/WebKit/public/web/WebTextDirection.h"
41 class WebCachedURLRequest
;
42 class WebColorChooser
;
43 class WebColorChooserClient
;
46 class WebFileChooserCompletion
;
49 class WebMIDIAccessor
;
50 class WebMIDIAccessorClient
;
54 class WebSerializedScriptValue
;
55 class WebSpeechRecognizer
;
56 class WebSpellCheckClient
;
60 class WebUserMediaClient
;
63 struct WebColorSuggestion
;
64 struct WebConsoleMessage
;
65 struct WebContextMenuData
;
66 struct WebFileChooserParams
;
67 struct WebPluginParams
;
70 struct WebWindowFeatures
;
71 typedef unsigned WebColor
;
76 class MockCredentialManagerClient
;
77 class MockScreenOrientationClient
;
78 class MockWebSpeechRecognizer
;
79 class MockWebUserMediaClient
;
81 class SpellCheckClient
;
83 class WebTestDelegate
;
84 class WebTestInterfaces
;
86 // WebTestProxyBase is the "brain" of WebTestProxy in the sense that
87 // WebTestProxy does the bridge between RenderViewImpl and WebTestProxyBase and
88 // when it requires a behavior to be different from the usual, it will call
89 // WebTestProxyBase that implements the expected behavior.
90 // See WebTestProxy class comments for more information.
91 class WebTestProxyBase
{
93 void SetInterfaces(WebTestInterfaces
* interfaces
);
94 void SetDelegate(WebTestDelegate
* delegate
);
95 void set_widget(blink::WebWidget
* widget
) { web_widget_
= widget
; }
99 blink::WebSpellCheckClient
* GetSpellCheckClient() const;
100 blink::WebColorChooser
* CreateColorChooser(
101 blink::WebColorChooserClient
* client
,
102 const blink::WebColor
& color
,
103 const blink::WebVector
<blink::WebColorSuggestion
>& suggestions
);
104 bool RunFileChooser(const blink::WebFileChooserParams
& params
,
105 blink::WebFileChooserCompletion
* completion
);
106 void ShowValidationMessage(const base::string16
& message
,
107 const base::string16
& sub_message
);
108 void HideValidationMessage();
109 void MoveValidationMessage(const blink::WebRect
& anchor_in_root_view
);
111 std::string
CaptureTree(bool debug_render_tree
);
112 void CapturePixelsForPrinting(
113 const base::Callback
<void(const SkBitmap
&)>& callback
);
114 void CopyImageAtAndCapturePixels(
115 int x
, int y
, const base::Callback
<void(const SkBitmap
&)>& callback
);
116 void CapturePixelsAsync(
117 const base::Callback
<void(const SkBitmap
&)>& callback
);
119 void SetLogConsoleOutput(bool enabled
);
121 void DidOpenChooser();
122 void DidCloseChooser();
123 bool IsChooserShown();
125 void DisplayAsyncThen(const base::Closure
& callback
);
127 void GetScreenOrientationForTesting(blink::WebScreenInfo
&);
128 MockScreenOrientationClient
* GetScreenOrientationClientMock();
129 MockWebSpeechRecognizer
* GetSpeechRecognizerMock();
130 MockCredentialManagerClient
* GetCredentialManagerClientMock();
132 WebTaskList
* mutable_task_list() { return &task_list_
; }
134 blink::WebView
* GetWebView() const;
136 void PostSpellCheckEvent(const blink::WebString
& event_name
);
138 void SetAcceptLanguages(const std::string
& accept_languages
);
144 void ScheduleAnimation();
145 void PostAccessibilityEvent(const blink::WebAXObject
&, blink::WebAXEvent
);
146 void StartDragging(blink::WebLocalFrame
* frame
,
147 const blink::WebDragData
& data
,
148 blink::WebDragOperationsMask mask
,
149 const blink::WebImage
& image
,
150 const blink::WebPoint
& point
);
151 void DidChangeSelection(bool isEmptySelection
);
152 void DidChangeContents();
153 void DidEndEditing();
154 bool CreateView(blink::WebLocalFrame
* creator
,
155 const blink::WebURLRequest
& request
,
156 const blink::WebWindowFeatures
& features
,
157 const blink::WebString
& frame_name
,
158 blink::WebNavigationPolicy policy
,
159 bool suppress_opener
);
160 blink::WebPlugin
* CreatePlugin(blink::WebLocalFrame
* frame
,
161 const blink::WebPluginParams
& params
);
162 void SetStatusText(const blink::WebString
& text
);
163 void DidStopLoading();
164 void ShowContextMenu(blink::WebLocalFrame
* frame
,
165 const blink::WebContextMenuData
& data
);
166 blink::WebUserMediaClient
* GetUserMediaClient();
167 void PrintPage(blink::WebLocalFrame
* frame
);
168 blink::WebSpeechRecognizer
* GetSpeechRecognizer();
169 bool RequestPointerLock();
170 void RequestPointerUnlock();
171 bool IsPointerLocked();
174 void SetToolTipText(const blink::WebString
& text
,
175 blink::WebTextDirection direction
);
176 void DidAddMessageToConsole(const blink::WebConsoleMessage
& text
,
177 const blink::WebString
& source_name
,
178 unsigned source_line
);
179 void LoadURLExternally(blink::WebLocalFrame
* frame
,
180 const blink::WebURLRequest
& request
,
181 blink::WebNavigationPolicy policy
,
182 const blink::WebString
& suggested_name
);
183 void DidStartProvisionalLoad(blink::WebLocalFrame
*);
184 void DidReceiveServerRedirectForProvisionalLoad(blink::WebLocalFrame
* frame
);
185 bool DidFailProvisionalLoad(blink::WebLocalFrame
* frame
,
186 const blink::WebURLError
& error
,
187 blink::WebHistoryCommitType commit_type
);
188 void DidCommitProvisionalLoad(blink::WebLocalFrame
* frame
,
189 const blink::WebHistoryItem
& history_item
,
190 blink::WebHistoryCommitType history_type
);
191 void DidReceiveTitle(blink::WebLocalFrame
* frame
,
192 const blink::WebString
& title
,
193 blink::WebTextDirection text_direction
);
194 void DidChangeIcon(blink::WebLocalFrame
* frame
,
195 blink::WebIconURL::Type icon_type
);
196 void DidFinishDocumentLoad(blink::WebLocalFrame
* frame
);
197 void DidHandleOnloadEvents(blink::WebLocalFrame
* frame
);
198 void DidFailLoad(blink::WebLocalFrame
* frame
,
199 const blink::WebURLError
& error
,
200 blink::WebHistoryCommitType commit_type
);
201 void DidFinishLoad(blink::WebLocalFrame
* frame
);
202 void DidChangeLocationWithinPage(blink::WebLocalFrame
* frame
);
203 void DidDetectXSS(blink::WebLocalFrame
* frame
,
204 const blink::WebURL
& insecure_url
,
205 bool did_block_entire_page
);
206 void DidDispatchPingLoader(blink::WebLocalFrame
* frame
,
207 const blink::WebURL
& url
);
208 void WillRequestResource(blink::WebLocalFrame
* frame
,
209 const blink::WebCachedURLRequest
& url_request
);
210 void WillSendRequest(blink::WebLocalFrame
* frame
,
212 blink::WebURLRequest
& request
,
213 const blink::WebURLResponse
& redirect_response
);
214 void DidReceiveResponse(blink::WebLocalFrame
* frame
,
216 const blink::WebURLResponse
& response
);
217 void DidChangeResourcePriority(blink::WebLocalFrame
* frame
,
219 const blink::WebURLRequest::Priority
& priority
,
220 int intra_priority_value
);
221 void DidFinishResourceLoad(blink::WebLocalFrame
* frame
, unsigned identifier
);
222 blink::WebNavigationPolicy
DecidePolicyForNavigation(
223 const blink::WebFrameClient::NavigationPolicyInfo
& info
);
224 bool WillCheckAndDispatchMessageEvent(blink::WebLocalFrame
* source_frame
,
225 blink::WebFrame
* target_frame
,
226 blink::WebSecurityOrigin target
,
227 blink::WebDOMMessageEvent event
);
228 void ResetInputMethod();
230 blink::WebString
acceptLanguages();
233 template <class, typename
, typename
>
234 friend class WebFrameTestProxy
;
236 enum CheckDoneReason
{
238 MainResourceLoadFailed
,
239 ResourceLoadCompleted
241 void CheckDone(blink::WebLocalFrame
* frame
, CheckDoneReason reason
);
243 void DrawSelectionRect(SkCanvas
* canvas
);
244 void DidCapturePixelsAsync(const base::Callback
<void(const SkBitmap
&)>& callback
, const SkBitmap
& bitmap
);
245 void DidDisplayAsync(const base::Closure
& callback
, const SkBitmap
& bitmap
);
247 blink::WebWidget
* web_widget() const { return web_widget_
; }
249 TestInterfaces
* test_interfaces_
;
250 WebTestDelegate
* delegate_
;
251 blink::WebWidget
* web_widget_
;
253 WebTaskList task_list_
;
255 blink::WebImage drag_image_
;
257 scoped_ptr
<SpellCheckClient
> spellcheck_
;
258 scoped_ptr
<MockWebUserMediaClient
> user_media_client_
;
260 bool animate_scheduled_
;
261 std::map
<unsigned, std::string
> resource_identifier_map_
;
263 bool log_console_output_
;
266 scoped_ptr
<MockCredentialManagerClient
> credential_manager_client_
;
267 scoped_ptr
<MockWebSpeechRecognizer
> speech_recognizer_
;
268 scoped_ptr
<MockScreenOrientationClient
> screen_orientation_client_
;
270 std::string accept_languages_
;
273 DISALLOW_COPY_AND_ASSIGN(WebTestProxyBase
);
276 // WebTestProxy is used during LayoutTests and always instantiated, at time of
277 // writing with Base=RenderViewImpl. It does not directly inherit from it for
278 // layering purposes.
279 // The intent of that class is to wrap RenderViewImpl for tests purposes in
280 // order to reduce the amount of test specific code in the production code.
281 // WebTestProxy is only doing the glue between RenderViewImpl and
282 // WebTestProxyBase, that means that there is no logic living in this class
283 // except deciding which base class should be called (could be both).
285 // Examples of usage:
286 // * when a fooClient has a mock implementation, WebTestProxy can override the
287 // fooClient() call and have WebTestProxyBase return the mock implementation.
288 // * when a value needs to be overridden by LayoutTests, WebTestProxy can
289 // override RenderViewImpl's getter and call a getter from WebTestProxyBase
290 // instead. In addition, WebTestProxyBase will have a public setter that
291 // could be called from the TestRunner.
292 template <class Base
, typename T
>
293 class WebTestProxy
: public Base
, public WebTestProxyBase
{
295 explicit WebTestProxy(T t
) : Base(t
) {}
297 // WebWidgetClient implementation.
298 virtual blink::WebScreenInfo
screenInfo() {
299 blink::WebScreenInfo info
= Base::screenInfo();
300 WebTestProxyBase::GetScreenOrientationForTesting(info
);
304 // WebViewClient implementation.
305 virtual void scheduleAnimation() { WebTestProxyBase::ScheduleAnimation(); }
306 virtual void postAccessibilityEvent(const blink::WebAXObject
& object
,
307 blink::WebAXEvent event
) {
308 WebTestProxyBase::PostAccessibilityEvent(object
, event
);
309 Base::postAccessibilityEvent(object
, event
);
311 virtual void startDragging(blink::WebLocalFrame
* frame
,
312 const blink::WebDragData
& data
,
313 blink::WebDragOperationsMask mask
,
314 const blink::WebImage
& image
,
315 const blink::WebPoint
& point
) {
316 WebTestProxyBase::StartDragging(frame
, data
, mask
, image
, point
);
317 // Don't forward this call to Base because we don't want to do a real
320 virtual void didChangeContents() {
321 WebTestProxyBase::DidChangeContents();
322 Base::didChangeContents();
324 virtual blink::WebView
* createView(blink::WebLocalFrame
* creator
,
325 const blink::WebURLRequest
& request
,
326 const blink::WebWindowFeatures
& features
,
327 const blink::WebString
& frame_name
,
328 blink::WebNavigationPolicy policy
,
329 bool suppress_opener
) {
330 if (!WebTestProxyBase::CreateView(
331 creator
, request
, features
, frame_name
, policy
, suppress_opener
))
333 return Base::createView(
334 creator
, request
, features
, frame_name
, policy
, suppress_opener
);
336 virtual void setStatusText(const blink::WebString
& text
) {
337 WebTestProxyBase::SetStatusText(text
);
338 Base::setStatusText(text
);
340 virtual void printPage(blink::WebLocalFrame
* frame
) {
341 WebTestProxyBase::PrintPage(frame
);
343 virtual blink::WebSpeechRecognizer
* speechRecognizer() {
344 return WebTestProxyBase::GetSpeechRecognizer();
346 virtual bool requestPointerLock() {
347 return WebTestProxyBase::RequestPointerLock();
349 virtual void requestPointerUnlock() {
350 WebTestProxyBase::RequestPointerUnlock();
352 virtual bool isPointerLocked() { return WebTestProxyBase::IsPointerLocked(); }
353 virtual void didFocus() {
354 WebTestProxyBase::DidFocus();
357 virtual void didBlur() {
358 WebTestProxyBase::DidBlur();
361 virtual void setToolTipText(const blink::WebString
& text
,
362 blink::WebTextDirection hint
) {
363 WebTestProxyBase::SetToolTipText(text
, hint
);
364 Base::setToolTipText(text
, hint
);
366 virtual void resetInputMethod() { WebTestProxyBase::ResetInputMethod(); }
367 virtual bool runFileChooser(const blink::WebFileChooserParams
& params
,
368 blink::WebFileChooserCompletion
* completion
) {
369 return WebTestProxyBase::RunFileChooser(params
, completion
);
371 virtual void showValidationMessage(const blink::WebRect
& anchor_in_root_view
,
372 const blink::WebString
& main_message
,
373 blink::WebTextDirection main_message_hint
,
374 const blink::WebString
& sub_message
,
375 blink::WebTextDirection sub_message_hint
) {
376 base::string16 wrapped_main_text
= main_message
;
377 base::string16 wrapped_sub_text
= sub_message
;
379 Base::SetValidationMessageDirection(
380 &wrapped_main_text
, main_message_hint
, &wrapped_sub_text
, sub_message_hint
);
382 WebTestProxyBase::ShowValidationMessage(
383 wrapped_main_text
, wrapped_sub_text
);
385 virtual void postSpellCheckEvent(const blink::WebString
& event_name
) {
386 WebTestProxyBase::PostSpellCheckEvent(event_name
);
388 virtual blink::WebString
acceptLanguages() {
389 return WebTestProxyBase::acceptLanguages();
393 virtual ~WebTestProxy() {}
395 DISALLOW_COPY_AND_ASSIGN(WebTestProxy
);
398 } // namespace content
400 #endif // CONTENT_SHELL_RENDERER_TEST_RUNNER_WEB_TEST_PROXY_H_