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 COMPONENTS_TEST_RUNNER_WEB_TEST_PROXY_H_
6 #define COMPONENTS_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 "components/test_runner/test_runner_export.h"
16 #include "components/test_runner/web_task.h"
17 #include "third_party/WebKit/public/platform/WebImage.h"
18 #include "third_party/WebKit/public/platform/WebRect.h"
19 #include "third_party/WebKit/public/platform/WebScreenInfo.h"
20 #include "third_party/WebKit/public/platform/WebURLError.h"
21 #include "third_party/WebKit/public/platform/WebURLRequest.h"
22 #include "third_party/WebKit/public/web/WebAXEnums.h"
23 #include "third_party/WebKit/public/web/WebDOMMessageEvent.h"
24 #include "third_party/WebKit/public/web/WebDataSource.h"
25 #include "third_party/WebKit/public/web/WebDragOperation.h"
26 #include "third_party/WebKit/public/web/WebFrame.h"
27 #include "third_party/WebKit/public/web/WebFrameClient.h"
28 #include "third_party/WebKit/public/web/WebHistoryCommitType.h"
29 #include "third_party/WebKit/public/web/WebIconURL.h"
30 #include "third_party/WebKit/public/web/WebNavigationPolicy.h"
31 #include "third_party/WebKit/public/web/WebNavigationType.h"
32 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
33 #include "third_party/WebKit/public/web/WebTextAffinity.h"
34 #include "third_party/WebKit/public/web/WebTextDirection.h"
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
;
74 namespace test_runner
{
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 TEST_RUNNER_EXPORT WebTestProxyBase
{
93 void SetInterfaces(WebTestInterfaces
* interfaces
);
94 WebTestInterfaces
* GetInterfaces();
95 void SetDelegate(WebTestDelegate
* delegate
);
96 WebTestDelegate
* GetDelegate();
97 void set_widget(blink::WebWidget
* widget
) { web_widget_
= widget
; }
101 blink::WebSpellCheckClient
* GetSpellCheckClient() const;
102 blink::WebColorChooser
* CreateColorChooser(
103 blink::WebColorChooserClient
* client
,
104 const blink::WebColor
& color
,
105 const blink::WebVector
<blink::WebColorSuggestion
>& suggestions
);
106 bool RunFileChooser(const blink::WebFileChooserParams
& params
,
107 blink::WebFileChooserCompletion
* completion
);
108 void ShowValidationMessage(const blink::WebString
& main_message
,
109 blink::WebTextDirection main_message_hint
,
110 const blink::WebString
& sub_message
,
111 blink::WebTextDirection sub_message_hint
);
112 void HideValidationMessage();
113 void MoveValidationMessage(const blink::WebRect
& anchor_in_root_view
);
115 std::string
CaptureTree(bool debug_render_tree
, bool dump_line_box_trees
);
116 void CapturePixelsForPrinting(
117 const base::Callback
<void(const SkBitmap
&)>& callback
);
118 void CopyImageAtAndCapturePixels(
119 int x
, int y
, const base::Callback
<void(const SkBitmap
&)>& callback
);
120 void CapturePixelsAsync(
121 const base::Callback
<void(const SkBitmap
&)>& callback
);
123 void SetLogConsoleOutput(bool enabled
);
125 void DidOpenChooser();
126 void DidCloseChooser();
127 bool IsChooserShown();
129 void LayoutAndPaintAsyncThen(const base::Closure
& callback
);
131 void GetScreenOrientationForTesting(blink::WebScreenInfo
&);
132 MockScreenOrientationClient
* GetScreenOrientationClientMock();
133 MockWebSpeechRecognizer
* GetSpeechRecognizerMock();
134 MockCredentialManagerClient
* GetCredentialManagerClientMock();
136 WebTaskList
* mutable_task_list() { return &task_list_
; }
138 blink::WebView
* GetWebView() const;
140 void PostSpellCheckEvent(const blink::WebString
& event_name
);
142 void SetAcceptLanguages(const std::string
& accept_languages
);
144 void PostAccessibilityEvent(const blink::WebAXObject
&, blink::WebAXEvent
);
150 void ScheduleAnimation();
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(const blink::WebContextMenuData
& data
);
170 blink::WebUserMediaClient
* GetUserMediaClient();
171 void PrintPage(blink::WebLocalFrame
* frame
);
172 blink::WebSpeechRecognizer
* GetSpeechRecognizer();
173 bool RequestPointerLock();
174 void RequestPointerUnlock();
175 bool IsPointerLocked();
178 void SetToolTipText(const blink::WebString
& text
,
179 blink::WebTextDirection direction
);
180 void DidAddMessageToConsole(const blink::WebConsoleMessage
& text
,
181 const blink::WebString
& source_name
,
182 unsigned source_line
);
183 void LoadURLExternally(blink::WebLocalFrame
* frame
,
184 const blink::WebURLRequest
& request
,
185 blink::WebNavigationPolicy policy
,
186 const blink::WebString
& suggested_name
);
187 void DidStartProvisionalLoad(blink::WebLocalFrame
*);
188 void DidReceiveServerRedirectForProvisionalLoad(blink::WebLocalFrame
* frame
);
189 bool DidFailProvisionalLoad(blink::WebLocalFrame
* frame
,
190 const blink::WebURLError
& error
,
191 blink::WebHistoryCommitType commit_type
);
192 void DidCommitProvisionalLoad(blink::WebLocalFrame
* frame
,
193 const blink::WebHistoryItem
& history_item
,
194 blink::WebHistoryCommitType history_type
);
195 void DidReceiveTitle(blink::WebLocalFrame
* frame
,
196 const blink::WebString
& title
,
197 blink::WebTextDirection text_direction
);
198 void DidChangeIcon(blink::WebLocalFrame
* frame
,
199 blink::WebIconURL::Type icon_type
);
200 void DidFinishDocumentLoad(blink::WebLocalFrame
* frame
);
201 void DidHandleOnloadEvents(blink::WebLocalFrame
* frame
);
202 void DidFailLoad(blink::WebLocalFrame
* frame
,
203 const blink::WebURLError
& error
,
204 blink::WebHistoryCommitType commit_type
);
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 WillSendRequest(blink::WebLocalFrame
* frame
,
214 blink::WebURLRequest
& request
,
215 const blink::WebURLResponse
& redirect_response
);
216 void DidReceiveResponse(blink::WebLocalFrame
* frame
,
218 const blink::WebURLResponse
& response
);
219 void DidChangeResourcePriority(blink::WebLocalFrame
* frame
,
221 const blink::WebURLRequest::Priority
& priority
,
222 int intra_priority_value
);
223 void DidFinishResourceLoad(blink::WebLocalFrame
* frame
, unsigned identifier
);
224 blink::WebNavigationPolicy
DecidePolicyForNavigation(
225 const blink::WebFrameClient::NavigationPolicyInfo
& info
);
226 bool WillCheckAndDispatchMessageEvent(blink::WebLocalFrame
* source_frame
,
227 blink::WebFrame
* target_frame
,
228 blink::WebSecurityOrigin target
,
229 blink::WebDOMMessageEvent event
);
230 void ResetInputMethod();
232 blink::WebString
acceptLanguages();
235 template <class, typename
>
236 friend class WebFrameTestProxy
;
238 enum CheckDoneReason
{
240 MainResourceLoadFailed
,
241 ResourceLoadCompleted
243 void CheckDone(blink::WebLocalFrame
* frame
, CheckDoneReason reason
);
245 void DrawSelectionRect(SkCanvas
* canvas
);
246 void DidCapturePixelsAsync(
247 const base::Callback
<void(const SkBitmap
&)>& callback
,
248 const SkBitmap
& bitmap
);
250 blink::WebWidget
* web_widget() const { return web_widget_
; }
252 WebTestInterfaces
* web_test_interfaces_
;
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
<MockWebSpeechRecognizer
> speech_recognizer_
;
272 scoped_ptr
<MockScreenOrientationClient
> screen_orientation_client_
;
274 std::string accept_languages_
;
277 DISALLOW_COPY_AND_ASSIGN(WebTestProxyBase
);
280 // WebTestProxy is used during LayoutTests and always instantiated, at time of
281 // writing with Base=RenderViewImpl. It does not directly inherit from it for
282 // layering purposes.
283 // The intent of that class is to wrap RenderViewImpl for tests purposes in
284 // order to reduce the amount of test specific code in the production code.
285 // WebTestProxy is only doing the glue between RenderViewImpl and
286 // WebTestProxyBase, that means that there is no logic living in this class
287 // except deciding which base class should be called (could be both).
289 // Examples of usage:
290 // * when a fooClient has a mock implementation, WebTestProxy can override the
291 // fooClient() call and have WebTestProxyBase return the mock implementation.
292 // * when a value needs to be overridden by LayoutTests, WebTestProxy can
293 // override RenderViewImpl's getter and call a getter from WebTestProxyBase
294 // instead. In addition, WebTestProxyBase will have a public setter that
295 // could be called from the TestRunner.
297 // WebTestProxy is a diamond-shaped hierarchy, with WebWidgetClient at the root.
298 // VS warns when we inherit the WebWidgetClient method implementations from
299 // RenderWidget. It's safe to ignore that warning.
300 #pragma warning(disable: 4250)
302 template <class Base
, typename
... Args
>
303 class WebTestProxy
: public Base
, public WebTestProxyBase
{
305 explicit WebTestProxy(Args
... args
) : Base(args
...) {}
307 // WebWidgetClient implementation.
308 virtual blink::WebScreenInfo
screenInfo() {
309 blink::WebScreenInfo info
= Base::screenInfo();
310 WebTestProxyBase::GetScreenOrientationForTesting(info
);
314 // WebViewClient implementation.
315 virtual void scheduleAnimation() { WebTestProxyBase::ScheduleAnimation(); }
316 virtual void startDragging(blink::WebLocalFrame
* frame
,
317 const blink::WebDragData
& data
,
318 blink::WebDragOperationsMask mask
,
319 const blink::WebImage
& image
,
320 const blink::WebPoint
& point
) {
321 WebTestProxyBase::StartDragging(frame
, data
, mask
, image
, point
);
322 // Don't forward this call to Base because we don't want to do a real
325 virtual void didChangeContents() {
326 WebTestProxyBase::DidChangeContents();
327 Base::didChangeContents();
329 virtual blink::WebView
* createView(blink::WebLocalFrame
* creator
,
330 const blink::WebURLRequest
& request
,
331 const blink::WebWindowFeatures
& features
,
332 const blink::WebString
& frame_name
,
333 blink::WebNavigationPolicy policy
,
334 bool suppress_opener
) {
335 if (!WebTestProxyBase::CreateView(
336 creator
, request
, features
, frame_name
, policy
, suppress_opener
))
338 return Base::createView(
339 creator
, request
, features
, frame_name
, policy
, suppress_opener
);
341 virtual void setStatusText(const blink::WebString
& text
) {
342 WebTestProxyBase::SetStatusText(text
);
343 Base::setStatusText(text
);
345 virtual void printPage(blink::WebLocalFrame
* frame
) {
346 WebTestProxyBase::PrintPage(frame
);
348 virtual blink::WebSpeechRecognizer
* speechRecognizer() {
349 return WebTestProxyBase::GetSpeechRecognizer();
351 virtual bool requestPointerLock() {
352 return WebTestProxyBase::RequestPointerLock();
354 virtual void requestPointerUnlock() {
355 WebTestProxyBase::RequestPointerUnlock();
357 virtual bool isPointerLocked() { return WebTestProxyBase::IsPointerLocked(); }
358 virtual void didFocus() {
359 WebTestProxyBase::DidFocus();
362 virtual void didBlur() {
363 WebTestProxyBase::DidBlur();
366 virtual void setToolTipText(const blink::WebString
& text
,
367 blink::WebTextDirection hint
) {
368 WebTestProxyBase::SetToolTipText(text
, hint
);
369 Base::setToolTipText(text
, hint
);
371 virtual void resetInputMethod() { WebTestProxyBase::ResetInputMethod(); }
372 virtual bool runFileChooser(const blink::WebFileChooserParams
& params
,
373 blink::WebFileChooserCompletion
* completion
) {
374 return WebTestProxyBase::RunFileChooser(params
, completion
);
376 virtual void showValidationMessage(const blink::WebRect
& anchor_in_root_view
,
377 const blink::WebString
& main_message
,
378 blink::WebTextDirection main_message_hint
,
379 const blink::WebString
& sub_message
,
380 blink::WebTextDirection sub_message_hint
) {
381 WebTestProxyBase::ShowValidationMessage(main_message
, main_message_hint
,
382 sub_message
, sub_message_hint
);
384 virtual void postSpellCheckEvent(const blink::WebString
& event_name
) {
385 WebTestProxyBase::PostSpellCheckEvent(event_name
);
387 virtual blink::WebString
acceptLanguages() {
388 return WebTestProxyBase::acceptLanguages();
392 virtual ~WebTestProxy() {}
394 DISALLOW_COPY_AND_ASSIGN(WebTestProxy
);
397 } // namespace test_runner
399 #endif // COMPONENTS_TEST_RUNNER_WEB_TEST_PROXY_H_