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 WebCachedURLRequest
;
43 class WebColorChooser
;
44 class WebColorChooserClient
;
47 class WebFileChooserCompletion
;
50 class WebMIDIAccessor
;
51 class WebMIDIAccessorClient
;
55 class WebSerializedScriptValue
;
56 class WebSpeechRecognizer
;
57 class WebSpellCheckClient
;
61 class WebUserMediaClient
;
64 struct WebColorSuggestion
;
65 struct WebConsoleMessage
;
66 struct WebContextMenuData
;
67 struct WebFileChooserParams
;
68 struct WebPluginParams
;
71 struct WebWindowFeatures
;
72 typedef unsigned WebColor
;
75 namespace test_runner
{
77 class MockCredentialManagerClient
;
78 class MockScreenOrientationClient
;
79 class MockWebSpeechRecognizer
;
80 class MockWebUserMediaClient
;
82 class SpellCheckClient
;
84 class WebTestDelegate
;
85 class WebTestInterfaces
;
87 // WebTestProxyBase is the "brain" of WebTestProxy in the sense that
88 // WebTestProxy does the bridge between RenderViewImpl and WebTestProxyBase and
89 // when it requires a behavior to be different from the usual, it will call
90 // WebTestProxyBase that implements the expected behavior.
91 // See WebTestProxy class comments for more information.
92 class TEST_RUNNER_EXPORT WebTestProxyBase
{
94 void SetInterfaces(WebTestInterfaces
* interfaces
);
95 WebTestInterfaces
* GetInterfaces();
96 void SetDelegate(WebTestDelegate
* delegate
);
97 WebTestDelegate
* GetDelegate();
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 blink::WebString
& main_message
,
110 blink::WebTextDirection main_message_hint
,
111 const blink::WebString
& sub_message
,
112 blink::WebTextDirection sub_message_hint
);
113 void HideValidationMessage();
114 void MoveValidationMessage(const blink::WebRect
& anchor_in_root_view
);
116 std::string
CaptureTree(bool debug_render_tree
, bool dump_line_box_trees
);
117 void CapturePixelsForPrinting(
118 const base::Callback
<void(const SkBitmap
&)>& callback
);
119 void CopyImageAtAndCapturePixels(
120 int x
, int y
, const base::Callback
<void(const SkBitmap
&)>& callback
);
121 void CapturePixelsAsync(
122 const base::Callback
<void(const SkBitmap
&)>& callback
);
124 void SetLogConsoleOutput(bool enabled
);
126 void DidOpenChooser();
127 void DidCloseChooser();
128 bool IsChooserShown();
130 void LayoutAndPaintAsyncThen(const base::Closure
& callback
);
132 void GetScreenOrientationForTesting(blink::WebScreenInfo
&);
133 MockScreenOrientationClient
* GetScreenOrientationClientMock();
134 MockWebSpeechRecognizer
* GetSpeechRecognizerMock();
135 MockCredentialManagerClient
* GetCredentialManagerClientMock();
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
);
145 void PostAccessibilityEvent(const blink::WebAXObject
&, blink::WebAXEvent
);
151 void ScheduleAnimation();
152 void StartDragging(blink::WebLocalFrame
* frame
,
153 const blink::WebDragData
& data
,
154 blink::WebDragOperationsMask mask
,
155 const blink::WebImage
& image
,
156 const blink::WebPoint
& point
);
157 void DidChangeSelection(bool isEmptySelection
);
158 void DidChangeContents();
159 void DidEndEditing();
160 bool CreateView(blink::WebLocalFrame
* creator
,
161 const blink::WebURLRequest
& request
,
162 const blink::WebWindowFeatures
& features
,
163 const blink::WebString
& frame_name
,
164 blink::WebNavigationPolicy policy
,
165 bool suppress_opener
);
166 blink::WebPlugin
* CreatePlugin(blink::WebLocalFrame
* frame
,
167 const blink::WebPluginParams
& params
);
168 void SetStatusText(const blink::WebString
& text
);
169 void DidStopLoading();
170 void ShowContextMenu(const blink::WebContextMenuData
& data
);
171 blink::WebUserMediaClient
* GetUserMediaClient();
172 void PrintPage(blink::WebLocalFrame
* frame
);
173 blink::WebSpeechRecognizer
* GetSpeechRecognizer();
174 bool RequestPointerLock();
175 void RequestPointerUnlock();
176 bool IsPointerLocked();
179 void SetToolTipText(const blink::WebString
& text
,
180 blink::WebTextDirection direction
);
181 void DidAddMessageToConsole(const blink::WebConsoleMessage
& text
,
182 const blink::WebString
& source_name
,
183 unsigned source_line
);
184 void LoadURLExternally(blink::WebLocalFrame
* frame
,
185 const blink::WebURLRequest
& request
,
186 blink::WebNavigationPolicy policy
,
187 const blink::WebString
& suggested_name
);
188 void DidStartProvisionalLoad(blink::WebLocalFrame
*);
189 void DidReceiveServerRedirectForProvisionalLoad(blink::WebLocalFrame
* frame
);
190 bool DidFailProvisionalLoad(blink::WebLocalFrame
* frame
,
191 const blink::WebURLError
& error
,
192 blink::WebHistoryCommitType commit_type
);
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 blink::WebHistoryCommitType commit_type
);
206 void DidFinishLoad(blink::WebLocalFrame
* frame
);
207 void DidChangeLocationWithinPage(blink::WebLocalFrame
* frame
);
208 void DidDetectXSS(blink::WebLocalFrame
* frame
,
209 const blink::WebURL
& insecure_url
,
210 bool did_block_entire_page
);
211 void DidDispatchPingLoader(blink::WebLocalFrame
* frame
,
212 const blink::WebURL
& url
);
213 void WillRequestResource(blink::WebLocalFrame
* frame
,
214 const blink::WebCachedURLRequest
& url_request
);
215 void WillSendRequest(blink::WebLocalFrame
* frame
,
217 blink::WebURLRequest
& request
,
218 const blink::WebURLResponse
& redirect_response
);
219 void DidReceiveResponse(blink::WebLocalFrame
* frame
,
221 const blink::WebURLResponse
& response
);
222 void DidChangeResourcePriority(blink::WebLocalFrame
* frame
,
224 const blink::WebURLRequest::Priority
& priority
,
225 int intra_priority_value
);
226 void DidFinishResourceLoad(blink::WebLocalFrame
* frame
, unsigned identifier
);
227 blink::WebNavigationPolicy
DecidePolicyForNavigation(
228 const blink::WebFrameClient::NavigationPolicyInfo
& info
);
229 bool WillCheckAndDispatchMessageEvent(blink::WebLocalFrame
* source_frame
,
230 blink::WebFrame
* target_frame
,
231 blink::WebSecurityOrigin target
,
232 blink::WebDOMMessageEvent event
);
233 void ResetInputMethod();
235 blink::WebString
acceptLanguages();
238 template <class, typename
>
239 friend class WebFrameTestProxy
;
241 enum CheckDoneReason
{
243 MainResourceLoadFailed
,
244 ResourceLoadCompleted
246 void CheckDone(blink::WebLocalFrame
* frame
, CheckDoneReason reason
);
248 void DrawSelectionRect(SkCanvas
* canvas
);
249 void DidCapturePixelsAsync(
250 const base::Callback
<void(const SkBitmap
&)>& callback
,
251 const SkBitmap
& bitmap
);
253 blink::WebWidget
* web_widget() const { return web_widget_
; }
255 WebTestInterfaces
* web_test_interfaces_
;
256 TestInterfaces
* test_interfaces_
;
257 WebTestDelegate
* delegate_
;
258 blink::WebWidget
* web_widget_
;
260 WebTaskList task_list_
;
262 blink::WebImage drag_image_
;
264 scoped_ptr
<SpellCheckClient
> spellcheck_
;
265 scoped_ptr
<MockWebUserMediaClient
> user_media_client_
;
267 bool animate_scheduled_
;
268 std::map
<unsigned, std::string
> resource_identifier_map_
;
270 bool log_console_output_
;
273 scoped_ptr
<MockCredentialManagerClient
> credential_manager_client_
;
274 scoped_ptr
<MockWebSpeechRecognizer
> speech_recognizer_
;
275 scoped_ptr
<MockScreenOrientationClient
> screen_orientation_client_
;
277 std::string accept_languages_
;
280 DISALLOW_COPY_AND_ASSIGN(WebTestProxyBase
);
283 // WebTestProxy is used during LayoutTests and always instantiated, at time of
284 // writing with Base=RenderViewImpl. It does not directly inherit from it for
285 // layering purposes.
286 // The intent of that class is to wrap RenderViewImpl for tests purposes in
287 // order to reduce the amount of test specific code in the production code.
288 // WebTestProxy is only doing the glue between RenderViewImpl and
289 // WebTestProxyBase, that means that there is no logic living in this class
290 // except deciding which base class should be called (could be both).
292 // Examples of usage:
293 // * when a fooClient has a mock implementation, WebTestProxy can override the
294 // fooClient() call and have WebTestProxyBase return the mock implementation.
295 // * when a value needs to be overridden by LayoutTests, WebTestProxy can
296 // override RenderViewImpl's getter and call a getter from WebTestProxyBase
297 // instead. In addition, WebTestProxyBase will have a public setter that
298 // could be called from the TestRunner.
300 // WebTestProxy is a diamond-shaped hierarchy, with WebWidgetClient at the root.
301 // VS warns when we inherit the WebWidgetClient method implementations from
302 // RenderWidget. It's safe to ignore that warning.
303 #pragma warning(disable: 4250)
305 template <class Base
, typename T
>
306 class WebTestProxy
: public Base
, public WebTestProxyBase
{
308 explicit WebTestProxy(T t
) : Base(t
) {}
310 // WebWidgetClient implementation.
311 virtual blink::WebScreenInfo
screenInfo() {
312 blink::WebScreenInfo info
= Base::screenInfo();
313 WebTestProxyBase::GetScreenOrientationForTesting(info
);
317 // WebViewClient implementation.
318 virtual void scheduleAnimation() { WebTestProxyBase::ScheduleAnimation(); }
319 virtual void startDragging(blink::WebLocalFrame
* frame
,
320 const blink::WebDragData
& data
,
321 blink::WebDragOperationsMask mask
,
322 const blink::WebImage
& image
,
323 const blink::WebPoint
& point
) {
324 WebTestProxyBase::StartDragging(frame
, data
, mask
, image
, point
);
325 // Don't forward this call to Base because we don't want to do a real
328 virtual void didChangeContents() {
329 WebTestProxyBase::DidChangeContents();
330 Base::didChangeContents();
332 virtual blink::WebView
* createView(blink::WebLocalFrame
* creator
,
333 const blink::WebURLRequest
& request
,
334 const blink::WebWindowFeatures
& features
,
335 const blink::WebString
& frame_name
,
336 blink::WebNavigationPolicy policy
,
337 bool suppress_opener
) {
338 if (!WebTestProxyBase::CreateView(
339 creator
, request
, features
, frame_name
, policy
, suppress_opener
))
341 return Base::createView(
342 creator
, request
, features
, frame_name
, policy
, suppress_opener
);
344 virtual void setStatusText(const blink::WebString
& text
) {
345 WebTestProxyBase::SetStatusText(text
);
346 Base::setStatusText(text
);
348 virtual void printPage(blink::WebLocalFrame
* frame
) {
349 WebTestProxyBase::PrintPage(frame
);
351 virtual blink::WebSpeechRecognizer
* speechRecognizer() {
352 return WebTestProxyBase::GetSpeechRecognizer();
354 virtual bool requestPointerLock() {
355 return WebTestProxyBase::RequestPointerLock();
357 virtual void requestPointerUnlock() {
358 WebTestProxyBase::RequestPointerUnlock();
360 virtual bool isPointerLocked() { return WebTestProxyBase::IsPointerLocked(); }
361 virtual void didFocus() {
362 WebTestProxyBase::DidFocus();
365 virtual void didBlur() {
366 WebTestProxyBase::DidBlur();
369 virtual void setToolTipText(const blink::WebString
& text
,
370 blink::WebTextDirection hint
) {
371 WebTestProxyBase::SetToolTipText(text
, hint
);
372 Base::setToolTipText(text
, hint
);
374 virtual void resetInputMethod() { WebTestProxyBase::ResetInputMethod(); }
375 virtual bool runFileChooser(const blink::WebFileChooserParams
& params
,
376 blink::WebFileChooserCompletion
* completion
) {
377 return WebTestProxyBase::RunFileChooser(params
, completion
);
379 virtual void showValidationMessage(const blink::WebRect
& anchor_in_root_view
,
380 const blink::WebString
& main_message
,
381 blink::WebTextDirection main_message_hint
,
382 const blink::WebString
& sub_message
,
383 blink::WebTextDirection sub_message_hint
) {
384 WebTestProxyBase::ShowValidationMessage(main_message
, main_message_hint
,
385 sub_message
, sub_message_hint
);
387 virtual void postSpellCheckEvent(const blink::WebString
& event_name
) {
388 WebTestProxyBase::PostSpellCheckEvent(event_name
);
390 virtual blink::WebString
acceptLanguages() {
391 return WebTestProxyBase::acceptLanguages();
395 virtual ~WebTestProxy() {}
397 DISALLOW_COPY_AND_ASSIGN(WebTestProxy
);
400 } // namespace test_runner
402 #endif // COMPONENTS_TEST_RUNNER_WEB_TEST_PROXY_H_