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
;
52 class WebMIDIClientMock
;
56 class WebSerializedScriptValue
;
57 class WebSpeechRecognizer
;
58 class WebSpellCheckClient
;
62 class WebUserMediaClient
;
65 struct WebColorSuggestion
;
66 struct WebConsoleMessage
;
67 struct WebContextMenuData
;
68 struct WebFileChooserParams
;
69 struct WebPluginParams
;
72 struct WebWindowFeatures
;
73 typedef unsigned WebColor
;
78 class MockCredentialManagerClient
;
79 class MockPresentationService
;
80 class MockScreenOrientationClient
;
81 class MockWebSpeechRecognizer
;
82 class MockWebUserMediaClient
;
84 class SpellCheckClient
;
86 class WebTestDelegate
;
87 class WebTestInterfaces
;
89 // WebTestProxyBase is the "brain" of WebTestProxy in the sense that
90 // WebTestProxy does the bridge between RenderViewImpl and WebTestProxyBase and
91 // when it requires a behavior to be different from the usual, it will call
92 // WebTestProxyBase that implements the expected behavior.
93 // See WebTestProxy class comments for more information.
94 class WebTestProxyBase
{
96 void SetInterfaces(WebTestInterfaces
* interfaces
);
97 void SetDelegate(WebTestDelegate
* delegate
);
98 void set_widget(blink::WebWidget
* widget
) { web_widget_
= widget
; }
102 blink::WebSpellCheckClient
* GetSpellCheckClient() const;
103 blink::WebColorChooser
* CreateColorChooser(
104 blink::WebColorChooserClient
* client
,
105 const blink::WebColor
& color
,
106 const blink::WebVector
<blink::WebColorSuggestion
>& suggestions
);
107 bool RunFileChooser(const blink::WebFileChooserParams
& params
,
108 blink::WebFileChooserCompletion
* completion
);
109 void ShowValidationMessage(const base::string16
& message
,
110 const base::string16
& sub_message
);
111 void HideValidationMessage();
112 void MoveValidationMessage(const blink::WebRect
& anchor_in_root_view
);
114 std::string
CaptureTree(bool debug_render_tree
);
115 void CapturePixelsForPrinting(
116 const base::Callback
<void(const SkBitmap
&)>& callback
);
117 void CopyImageAtAndCapturePixels(
118 int x
, int y
, const base::Callback
<void(const SkBitmap
&)>& callback
);
119 void CapturePixelsAsync(
120 const base::Callback
<void(const SkBitmap
&)>& callback
);
122 void SetLogConsoleOutput(bool enabled
);
124 void DidOpenChooser();
125 void DidCloseChooser();
126 bool IsChooserShown();
128 void DisplayAsyncThen(const base::Closure
& callback
);
130 void GetScreenOrientationForTesting(blink::WebScreenInfo
&);
131 MockScreenOrientationClient
* GetScreenOrientationClientMock();
132 blink::WebMIDIClientMock
* GetMIDIClientMock();
133 MockWebSpeechRecognizer
* GetSpeechRecognizerMock();
134 MockCredentialManagerClient
* GetCredentialManagerClientMock();
135 MockPresentationService
* GetPresentationServiceMock();
137 WebTaskList
* mutable_task_list() { return &task_list_
; }
139 blink::WebView
* GetWebView() const;
141 void PostSpellCheckEvent(const blink::WebString
& event_name
);
143 void SetAcceptLanguages(const std::string
& accept_languages
);
149 void ScheduleAnimation();
150 void PostAccessibilityEvent(const blink::WebAXObject
&, blink::WebAXEvent
);
151 void StartDragging(blink::WebLocalFrame
* frame
,
152 const blink::WebDragData
& data
,
153 blink::WebDragOperationsMask mask
,
154 const blink::WebImage
& image
,
155 const blink::WebPoint
& point
);
156 void DidChangeSelection(bool isEmptySelection
);
157 void DidChangeContents();
158 void DidEndEditing();
159 bool CreateView(blink::WebLocalFrame
* creator
,
160 const blink::WebURLRequest
& request
,
161 const blink::WebWindowFeatures
& features
,
162 const blink::WebString
& frame_name
,
163 blink::WebNavigationPolicy policy
,
164 bool suppress_opener
);
165 blink::WebPlugin
* CreatePlugin(blink::WebLocalFrame
* frame
,
166 const blink::WebPluginParams
& params
);
167 void SetStatusText(const blink::WebString
& text
);
168 void DidStopLoading();
169 void ShowContextMenu(blink::WebLocalFrame
* frame
,
170 const blink::WebContextMenuData
& data
);
171 blink::WebUserMediaClient
* GetUserMediaClient();
172 void PrintPage(blink::WebLocalFrame
* frame
);
173 blink::WebMIDIClient
* GetWebMIDIClient();
174 blink::WebSpeechRecognizer
* GetSpeechRecognizer();
175 bool RequestPointerLock();
176 void RequestPointerUnlock();
177 bool IsPointerLocked();
180 void SetToolTipText(const blink::WebString
& text
,
181 blink::WebTextDirection direction
);
182 void DidAddMessageToConsole(const blink::WebConsoleMessage
& text
,
183 const blink::WebString
& source_name
,
184 unsigned source_line
);
185 void LoadURLExternally(blink::WebLocalFrame
* frame
,
186 const blink::WebURLRequest
& request
,
187 blink::WebNavigationPolicy policy
,
188 const blink::WebString
& suggested_name
);
189 void DidStartProvisionalLoad(blink::WebLocalFrame
*);
190 void DidReceiveServerRedirectForProvisionalLoad(blink::WebLocalFrame
* frame
);
191 bool DidFailProvisionalLoad(blink::WebLocalFrame
* frame
,
192 const blink::WebURLError
& error
);
193 void DidCommitProvisionalLoad(blink::WebLocalFrame
* frame
,
194 const blink::WebHistoryItem
& history_item
,
195 blink::WebHistoryCommitType history_type
);
196 void DidReceiveTitle(blink::WebLocalFrame
* frame
,
197 const blink::WebString
& title
,
198 blink::WebTextDirection text_direction
);
199 void DidChangeIcon(blink::WebLocalFrame
* frame
,
200 blink::WebIconURL::Type icon_type
);
201 void DidFinishDocumentLoad(blink::WebLocalFrame
* frame
);
202 void DidHandleOnloadEvents(blink::WebLocalFrame
* frame
);
203 void DidFailLoad(blink::WebLocalFrame
* frame
,
204 const blink::WebURLError
& error
);
205 void DidFinishLoad(blink::WebLocalFrame
* frame
);
206 void DidChangeLocationWithinPage(blink::WebLocalFrame
* frame
);
207 void DidDetectXSS(blink::WebLocalFrame
* frame
,
208 const blink::WebURL
& insecure_url
,
209 bool did_block_entire_page
);
210 void DidDispatchPingLoader(blink::WebLocalFrame
* frame
,
211 const blink::WebURL
& url
);
212 void WillRequestResource(blink::WebLocalFrame
* frame
,
213 const blink::WebCachedURLRequest
& url_request
);
214 void WillSendRequest(blink::WebLocalFrame
* frame
,
216 blink::WebURLRequest
& request
,
217 const blink::WebURLResponse
& redirect_response
);
218 void DidReceiveResponse(blink::WebLocalFrame
* frame
,
220 const blink::WebURLResponse
& response
);
221 void DidChangeResourcePriority(blink::WebLocalFrame
* frame
,
223 const blink::WebURLRequest::Priority
& priority
,
224 int intra_priority_value
);
225 void DidFinishResourceLoad(blink::WebLocalFrame
* frame
, unsigned identifier
);
226 blink::WebNavigationPolicy
DecidePolicyForNavigation(
227 const blink::WebFrameClient::NavigationPolicyInfo
& info
);
228 bool WillCheckAndDispatchMessageEvent(blink::WebLocalFrame
* source_frame
,
229 blink::WebFrame
* target_frame
,
230 blink::WebSecurityOrigin target
,
231 blink::WebDOMMessageEvent event
);
232 void ResetInputMethod();
234 blink::WebString
acceptLanguages();
237 template <class, typename
, typename
>
238 friend class WebFrameTestProxy
;
240 enum CheckDoneReason
{
242 MainResourceLoadFailed
,
243 ResourceLoadCompleted
245 void CheckDone(blink::WebLocalFrame
* frame
, CheckDoneReason reason
);
247 void DrawSelectionRect(SkCanvas
* canvas
);
248 void DidCapturePixelsAsync(const base::Callback
<void(const SkBitmap
&)>& callback
, const SkBitmap
& bitmap
);
249 void DidDisplayAsync(const base::Closure
& callback
, const SkBitmap
& bitmap
);
251 blink::WebWidget
* web_widget() const { return web_widget_
; }
253 TestInterfaces
* test_interfaces_
;
254 WebTestDelegate
* delegate_
;
255 blink::WebWidget
* web_widget_
;
257 WebTaskList task_list_
;
259 blink::WebImage drag_image_
;
261 scoped_ptr
<SpellCheckClient
> spellcheck_
;
262 scoped_ptr
<MockWebUserMediaClient
> user_media_client_
;
264 bool animate_scheduled_
;
265 std::map
<unsigned, std::string
> resource_identifier_map_
;
267 bool log_console_output_
;
270 scoped_ptr
<MockCredentialManagerClient
> credential_manager_client_
;
271 scoped_ptr
<blink::WebMIDIClientMock
> midi_client_
;
272 scoped_ptr
<MockWebSpeechRecognizer
> speech_recognizer_
;
273 scoped_ptr
<MockScreenOrientationClient
> screen_orientation_client_
;
274 scoped_ptr
<MockPresentationService
> presentation_service_
;
276 std::string accept_languages_
;
279 DISALLOW_COPY_AND_ASSIGN(WebTestProxyBase
);
282 // WebTestProxy is used during LayoutTests and always instantiated, at time of
283 // writing with Base=RenderViewImpl. It does not directly inherit from it for
284 // layering purposes.
285 // The intent of that class is to wrap RenderViewImpl for tests purposes in
286 // order to reduce the amount of test specific code in the production code.
287 // WebTestProxy is only doing the glue between RenderViewImpl and
288 // WebTestProxyBase, that means that there is no logic living in this class
289 // except deciding which base class should be called (could be both).
291 // Examples of usage:
292 // * when a fooClient has a mock implementation, WebTestProxy can override the
293 // fooClient() call and have WebTestProxyBase return the mock implementation.
294 // * when a value needs to be overridden by LayoutTests, WebTestProxy can
295 // override RenderViewImpl's getter and call a getter from WebTestProxyBase
296 // instead. In addition, WebTestProxyBase will have a public setter that
297 // could be called from the TestRunner.
298 template <class Base
, typename T
>
299 class WebTestProxy
: public Base
, public WebTestProxyBase
{
301 explicit WebTestProxy(T t
) : Base(t
) {}
303 // WebWidgetClient implementation.
304 virtual blink::WebScreenInfo
screenInfo() {
305 blink::WebScreenInfo info
= Base::screenInfo();
306 WebTestProxyBase::GetScreenOrientationForTesting(info
);
310 // WebViewClient implementation.
311 virtual void scheduleAnimation() { WebTestProxyBase::ScheduleAnimation(); }
312 virtual void postAccessibilityEvent(const blink::WebAXObject
& object
,
313 blink::WebAXEvent event
) {
314 WebTestProxyBase::PostAccessibilityEvent(object
, event
);
315 Base::postAccessibilityEvent(object
, event
);
317 virtual void startDragging(blink::WebLocalFrame
* frame
,
318 const blink::WebDragData
& data
,
319 blink::WebDragOperationsMask mask
,
320 const blink::WebImage
& image
,
321 const blink::WebPoint
& point
) {
322 WebTestProxyBase::StartDragging(frame
, data
, mask
, image
, point
);
323 // Don't forward this call to Base because we don't want to do a real
326 virtual void didChangeContents() {
327 WebTestProxyBase::DidChangeContents();
328 Base::didChangeContents();
330 virtual blink::WebView
* createView(blink::WebLocalFrame
* creator
,
331 const blink::WebURLRequest
& request
,
332 const blink::WebWindowFeatures
& features
,
333 const blink::WebString
& frame_name
,
334 blink::WebNavigationPolicy policy
,
335 bool suppress_opener
) {
336 if (!WebTestProxyBase::CreateView(
337 creator
, request
, features
, frame_name
, policy
, suppress_opener
))
339 return Base::createView(
340 creator
, request
, features
, frame_name
, policy
, suppress_opener
);
342 virtual void setStatusText(const blink::WebString
& text
) {
343 WebTestProxyBase::SetStatusText(text
);
344 Base::setStatusText(text
);
346 virtual void printPage(blink::WebLocalFrame
* frame
) {
347 WebTestProxyBase::PrintPage(frame
);
349 virtual blink::WebSpeechRecognizer
* speechRecognizer() {
350 return WebTestProxyBase::GetSpeechRecognizer();
352 virtual bool requestPointerLock() {
353 return WebTestProxyBase::RequestPointerLock();
355 virtual void requestPointerUnlock() {
356 WebTestProxyBase::RequestPointerUnlock();
358 virtual bool isPointerLocked() { return WebTestProxyBase::IsPointerLocked(); }
359 virtual void didFocus() {
360 WebTestProxyBase::DidFocus();
363 virtual void didBlur() {
364 WebTestProxyBase::DidBlur();
367 virtual void setToolTipText(const blink::WebString
& text
,
368 blink::WebTextDirection hint
) {
369 WebTestProxyBase::SetToolTipText(text
, hint
);
370 Base::setToolTipText(text
, hint
);
372 virtual void resetInputMethod() { WebTestProxyBase::ResetInputMethod(); }
373 virtual bool runFileChooser(const blink::WebFileChooserParams
& params
,
374 blink::WebFileChooserCompletion
* completion
) {
375 return WebTestProxyBase::RunFileChooser(params
, completion
);
377 virtual void showValidationMessage(const blink::WebRect
& anchor_in_root_view
,
378 const blink::WebString
& main_message
,
379 blink::WebTextDirection main_message_hint
,
380 const blink::WebString
& sub_message
,
381 blink::WebTextDirection sub_message_hint
) {
382 base::string16 wrapped_main_text
= main_message
;
383 base::string16 wrapped_sub_text
= sub_message
;
385 Base::SetValidationMessageDirection(
386 &wrapped_main_text
, main_message_hint
, &wrapped_sub_text
, sub_message_hint
);
388 WebTestProxyBase::ShowValidationMessage(
389 wrapped_main_text
, wrapped_sub_text
);
391 virtual void postSpellCheckEvent(const blink::WebString
& event_name
) {
392 WebTestProxyBase::PostSpellCheckEvent(event_name
);
394 virtual blink::WebString
acceptLanguages() {
395 return WebTestProxyBase::acceptLanguages();
399 virtual ~WebTestProxy() {}
401 DISALLOW_COPY_AND_ASSIGN(WebTestProxy
);
404 } // namespace content
406 #endif // CONTENT_SHELL_RENDERER_TEST_RUNNER_WEB_TEST_PROXY_H_