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/WebTask.h"
16 #include "third_party/WebKit/public/platform/WebCompositeAndReadbackAsyncCallback.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"
40 class WebCachedURLRequest;
41 class WebColorChooser;
42 class WebColorChooserClient;
45 class WebFileChooserCompletion;
49 class WebMIDIAccessor;
50 class WebMIDIAccessorClient;
52 class WebMIDIClientMock;
54 class WebNotificationPresenter;
58 class WebSerializedScriptValue;
59 class WebSpeechRecognizer;
60 class WebSpellCheckClient;
64 class WebUserMediaClient;
67 struct WebColorSuggestion;
68 struct WebConsoleMessage;
69 struct WebContextMenuData;
70 struct WebFileChooserParams;
71 struct WebPluginParams;
74 struct WebWindowFeatures;
75 typedef unsigned WebColor;
80 class MockScreenOrientationClient;
81 class MockWebPushClient;
82 class MockWebSpeechRecognizer;
83 class MockWebUserMediaClient;
85 class SpellCheckClient;
87 class WebTestDelegate;
88 class WebTestInterfaces;
90 // WebTestProxyBase is the "brain" of WebTestProxy in the sense that
91 // WebTestProxy does the bridge between RenderViewImpl and WebTestProxyBase and
92 // when it requires a behavior to be different from the usual, it will call
93 // WebTestProxyBase that implements the expected behavior.
94 // See WebTestProxy class comments for more information.
95 class WebTestProxyBase : public blink::WebCompositeAndReadbackAsyncCallback {
97 void SetInterfaces(WebTestInterfaces* interfaces);
98 void SetDelegate(WebTestDelegate* delegate);
99 void set_widget(blink::WebWidget* widget) { web_widget_ = widget; }
103 blink::WebSpellCheckClient* GetSpellCheckClient() const;
104 blink::WebColorChooser* CreateColorChooser(
105 blink::WebColorChooserClient* client,
106 const blink::WebColor& color,
107 const blink::WebVector<blink::WebColorSuggestion>& suggestions);
108 bool RunFileChooser(const blink::WebFileChooserParams& params,
109 blink::WebFileChooserCompletion* completion);
110 void ShowValidationMessage(const blink::WebRect& anchor_in_root_view,
111 const blink::WebString& message,
112 const blink::WebString& sub_message,
113 blink::WebTextDirection hint);
114 void HideValidationMessage();
115 void MoveValidationMessage(const blink::WebRect& anchor_in_root_view);
117 std::string CaptureTree(bool debug_render_tree);
118 void CapturePixelsForPrinting(
119 const base::Callback<void(const SkBitmap&)>& callback);
120 void CopyImageAtAndCapturePixels(
121 int x, int y, const base::Callback<void(const SkBitmap&)>& callback);
122 void CapturePixelsAsync(
123 const base::Callback<void(const SkBitmap&)>& callback);
125 void SetLogConsoleOutput(bool enabled);
127 void DidOpenChooser();
128 void DidCloseChooser();
129 bool IsChooserShown();
131 void DisplayAsyncThen(const base::Closure& callback);
133 void GetScreenOrientationForTesting(blink::WebScreenInfo&);
134 MockScreenOrientationClient* GetScreenOrientationClientMock();
135 blink::WebMIDIClientMock* GetMIDIClientMock();
136 MockWebSpeechRecognizer* GetSpeechRecognizerMock();
138 WebTaskList* mutable_task_list() { return &task_list_; }
140 blink::WebView* GetWebView() const;
142 void PostSpellCheckEvent(const blink::WebString& event_name);
144 // WebCompositeAndReadbackAsyncCallback implementation.
145 virtual void didCompositeAndReadback(const SkBitmap& bitmap);
147 void SetAcceptLanguages(const std::string& accept_languages);
149 MockWebPushClient* GetPushClientMock();
155 void ScheduleAnimation();
156 void PostAccessibilityEvent(const blink::WebAXObject&, blink::WebAXEvent);
157 void StartDragging(blink::WebLocalFrame* frame,
158 const blink::WebDragData& data,
159 blink::WebDragOperationsMask mask,
160 const blink::WebImage& image,
161 const blink::WebPoint& point);
162 void DidChangeSelection(bool isEmptySelection);
163 void DidChangeContents();
164 void DidEndEditing();
165 bool CreateView(blink::WebLocalFrame* creator,
166 const blink::WebURLRequest& request,
167 const blink::WebWindowFeatures& features,
168 const blink::WebString& frame_name,
169 blink::WebNavigationPolicy policy,
170 bool suppress_opener);
171 blink::WebPlugin* CreatePlugin(blink::WebLocalFrame* frame,
172 const blink::WebPluginParams& params);
173 void SetStatusText(const blink::WebString& text);
174 void DidStopLoading();
175 void ShowContextMenu(blink::WebLocalFrame* frame,
176 const blink::WebContextMenuData& data);
177 blink::WebUserMediaClient* GetUserMediaClient();
178 void PrintPage(blink::WebLocalFrame* frame);
179 blink::WebNotificationPresenter* GetNotificationPresenter();
180 blink::WebMIDIClient* GetWebMIDIClient();
181 blink::WebSpeechRecognizer* GetSpeechRecognizer();
182 bool RequestPointerLock();
183 void RequestPointerUnlock();
184 bool IsPointerLocked();
187 void SetToolTipText(const blink::WebString& text,
188 blink::WebTextDirection direction);
189 void DidAddMessageToConsole(const blink::WebConsoleMessage& text,
190 const blink::WebString& source_name,
191 unsigned source_line);
192 void LoadURLExternally(blink::WebLocalFrame* frame,
193 const blink::WebURLRequest& request,
194 blink::WebNavigationPolicy policy,
195 const blink::WebString& suggested_name);
196 void DidStartProvisionalLoad(blink::WebLocalFrame*);
197 void DidReceiveServerRedirectForProvisionalLoad(blink::WebLocalFrame* frame);
198 bool DidFailProvisionalLoad(blink::WebLocalFrame* frame,
199 const blink::WebURLError& error);
200 void DidCommitProvisionalLoad(blink::WebLocalFrame* frame,
201 const blink::WebHistoryItem& history_item,
202 blink::WebHistoryCommitType history_type);
203 void DidReceiveTitle(blink::WebLocalFrame* frame,
204 const blink::WebString& title,
205 blink::WebTextDirection text_direction);
206 void DidChangeIcon(blink::WebLocalFrame* frame,
207 blink::WebIconURL::Type icon_type);
208 void DidFinishDocumentLoad(blink::WebLocalFrame* frame);
209 void DidHandleOnloadEvents(blink::WebLocalFrame* frame);
210 void DidFailLoad(blink::WebLocalFrame* frame,
211 const blink::WebURLError& error);
212 void DidFinishLoad(blink::WebLocalFrame* frame);
213 void DidChangeLocationWithinPage(blink::WebLocalFrame* frame);
214 void DidDetectXSS(blink::WebLocalFrame* frame,
215 const blink::WebURL& insecure_url,
216 bool did_block_entire_page);
217 void DidDispatchPingLoader(blink::WebLocalFrame* frame,
218 const blink::WebURL& url);
219 void WillRequestResource(blink::WebLocalFrame* frame,
220 const blink::WebCachedURLRequest& url_request);
221 void WillSendRequest(blink::WebLocalFrame* frame,
223 blink::WebURLRequest& request,
224 const blink::WebURLResponse& redirect_response);
225 void DidReceiveResponse(blink::WebLocalFrame* frame,
227 const blink::WebURLResponse& response);
228 void DidChangeResourcePriority(blink::WebLocalFrame* frame,
230 const blink::WebURLRequest::Priority& priority,
231 int intra_priority_value);
232 void DidFinishResourceLoad(blink::WebLocalFrame* frame, unsigned identifier);
233 blink::WebNavigationPolicy DecidePolicyForNavigation(
234 const blink::WebFrameClient::NavigationPolicyInfo& info);
235 bool WillCheckAndDispatchMessageEvent(blink::WebLocalFrame* source_frame,
236 blink::WebFrame* target_frame,
237 blink::WebSecurityOrigin target,
238 blink::WebDOMMessageEvent event);
239 void ResetInputMethod();
241 blink::WebString acceptLanguages();
242 blink::WebPushClient* GetWebPushClient();
245 template <class, typename, typename>
246 friend class WebFrameTestProxy;
247 void LocationChangeDone(blink::WebFrame* frame);
249 void DrawSelectionRect(SkCanvas* canvas);
250 void DidDisplayAsync(const base::Closure& callback, const SkBitmap& bitmap);
252 blink::WebWidget* web_widget() const { return web_widget_; }
254 TestInterfaces* test_interfaces_;
255 WebTestDelegate* delegate_;
256 blink::WebWidget* web_widget_;
258 WebTaskList task_list_;
260 scoped_ptr<SpellCheckClient> spellcheck_;
261 scoped_ptr<MockWebUserMediaClient> user_media_client_;
263 bool animate_scheduled_;
264 std::map<unsigned, std::string> resource_identifier_map_;
265 std::deque<base::Callback<void(const SkBitmap&)> >
266 composite_and_readback_callbacks_;
268 bool log_console_output_;
271 scoped_ptr<blink::WebMIDIClientMock> midi_client_;
272 scoped_ptr<MockWebSpeechRecognizer> speech_recognizer_;
273 scoped_ptr<MockWebPushClient> push_client_;
274 scoped_ptr<MockScreenOrientationClient> screen_orientation_client_;
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 virtual ~WebTestProxy() {}
305 // WebWidgetClient implementation.
306 virtual blink::WebScreenInfo screenInfo() {
307 blink::WebScreenInfo info = Base::screenInfo();
308 WebTestProxyBase::GetScreenOrientationForTesting(info);
312 // WebViewClient implementation.
313 virtual void scheduleAnimation() { WebTestProxyBase::ScheduleAnimation(); }
314 virtual void postAccessibilityEvent(const blink::WebAXObject& object,
315 blink::WebAXEvent event) {
316 WebTestProxyBase::PostAccessibilityEvent(object, event);
317 Base::postAccessibilityEvent(object, event);
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& message,
381 const blink::WebString& sub_message,
382 blink::WebTextDirection hint) {
383 WebTestProxyBase::ShowValidationMessage(
384 anchor_in_root_view, message, sub_message, hint);
386 virtual void postSpellCheckEvent(const blink::WebString& event_name) {
387 WebTestProxyBase::PostSpellCheckEvent(event_name);
389 virtual blink::WebString acceptLanguages() {
390 return WebTestProxyBase::acceptLanguages();
392 virtual blink::WebPushClient* webPushClient() {
393 return WebTestProxyBase::GetWebPushClient();
397 DISALLOW_COPY_AND_ASSIGN(WebTestProxy);
400 } // namespace content
402 #endif // CONTENT_SHELL_RENDERER_TEST_RUNNER_WEB_TEST_PROXY_H_