Roll src/third_party/skia 0a24297:ceadfef
[chromium-blink-merge.git] / components / test_runner / web_test_proxy.h
blob81d736ad8e4e76b38daac68d1c710dd7f4318460
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_
8 #include <deque>
9 #include <map>
10 #include <string>
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"
36 class SkBitmap;
37 class SkCanvas;
39 namespace blink {
40 class WebAXObject;
41 class WebAudioDevice;
42 class WebCachedURLRequest;
43 class WebColorChooser;
44 class WebColorChooserClient;
45 class WebDataSource;
46 class WebDragData;
47 class WebFileChooserCompletion;
48 class WebFrame;
49 class WebLocalFrame;
50 class WebMIDIAccessor;
51 class WebMIDIAccessorClient;
52 class WebNode;
53 class WebPlugin;
54 class WebRange;
55 class WebSerializedScriptValue;
56 class WebSpeechRecognizer;
57 class WebSpellCheckClient;
58 class WebString;
59 class WebURL;
60 class WebURLResponse;
61 class WebUserMediaClient;
62 class WebView;
63 class WebWidget;
64 struct WebColorSuggestion;
65 struct WebConsoleMessage;
66 struct WebContextMenuData;
67 struct WebFileChooserParams;
68 struct WebPluginParams;
69 struct WebPoint;
70 struct WebSize;
71 struct WebWindowFeatures;
72 typedef unsigned WebColor;
75 namespace test_runner {
77 class MockCredentialManagerClient;
78 class MockScreenOrientationClient;
79 class MockWebSpeechRecognizer;
80 class MockWebUserMediaClient;
81 class RenderFrame;
82 class SpellCheckClient;
83 class TestInterfaces;
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 {
93 public:
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; }
100 void Reset();
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 LayoutAndPaintAsyncThen(const base::Closure& callback);
130 void GetScreenOrientationForTesting(blink::WebScreenInfo&);
131 MockScreenOrientationClient* GetScreenOrientationClientMock();
132 MockWebSpeechRecognizer* GetSpeechRecognizerMock();
133 MockCredentialManagerClient* GetCredentialManagerClientMock();
135 WebTaskList* mutable_task_list() { return &task_list_; }
137 blink::WebView* GetWebView() const;
139 void PostSpellCheckEvent(const blink::WebString& event_name);
141 void SetAcceptLanguages(const std::string& accept_languages);
143 protected:
144 WebTestProxyBase();
145 ~WebTestProxyBase();
147 void ScheduleAnimation();
148 void PostAccessibilityEvent(const blink::WebAXObject&, blink::WebAXEvent);
149 void StartDragging(blink::WebLocalFrame* frame,
150 const blink::WebDragData& data,
151 blink::WebDragOperationsMask mask,
152 const blink::WebImage& image,
153 const blink::WebPoint& point);
154 void DidChangeSelection(bool isEmptySelection);
155 void DidChangeContents();
156 void DidEndEditing();
157 bool CreateView(blink::WebLocalFrame* creator,
158 const blink::WebURLRequest& request,
159 const blink::WebWindowFeatures& features,
160 const blink::WebString& frame_name,
161 blink::WebNavigationPolicy policy,
162 bool suppress_opener);
163 blink::WebPlugin* CreatePlugin(blink::WebLocalFrame* frame,
164 const blink::WebPluginParams& params);
165 void SetStatusText(const blink::WebString& text);
166 void DidStopLoading();
167 void ShowContextMenu(blink::WebLocalFrame* frame,
168 const blink::WebContextMenuData& data);
169 blink::WebUserMediaClient* GetUserMediaClient();
170 void PrintPage(blink::WebLocalFrame* frame);
171 blink::WebSpeechRecognizer* GetSpeechRecognizer();
172 bool RequestPointerLock();
173 void RequestPointerUnlock();
174 bool IsPointerLocked();
175 void DidFocus();
176 void DidBlur();
177 void SetToolTipText(const blink::WebString& text,
178 blink::WebTextDirection direction);
179 void DidAddMessageToConsole(const blink::WebConsoleMessage& text,
180 const blink::WebString& source_name,
181 unsigned source_line);
182 void LoadURLExternally(blink::WebLocalFrame* frame,
183 const blink::WebURLRequest& request,
184 blink::WebNavigationPolicy policy,
185 const blink::WebString& suggested_name);
186 void DidStartProvisionalLoad(blink::WebLocalFrame*);
187 void DidReceiveServerRedirectForProvisionalLoad(blink::WebLocalFrame* frame);
188 bool DidFailProvisionalLoad(blink::WebLocalFrame* frame,
189 const blink::WebURLError& error,
190 blink::WebHistoryCommitType commit_type);
191 void DidCommitProvisionalLoad(blink::WebLocalFrame* frame,
192 const blink::WebHistoryItem& history_item,
193 blink::WebHistoryCommitType history_type);
194 void DidReceiveTitle(blink::WebLocalFrame* frame,
195 const blink::WebString& title,
196 blink::WebTextDirection text_direction);
197 void DidChangeIcon(blink::WebLocalFrame* frame,
198 blink::WebIconURL::Type icon_type);
199 void DidFinishDocumentLoad(blink::WebLocalFrame* frame);
200 void DidHandleOnloadEvents(blink::WebLocalFrame* frame);
201 void DidFailLoad(blink::WebLocalFrame* frame,
202 const blink::WebURLError& error,
203 blink::WebHistoryCommitType commit_type);
204 void DidFinishLoad(blink::WebLocalFrame* frame);
205 void DidChangeLocationWithinPage(blink::WebLocalFrame* frame);
206 void DidDetectXSS(blink::WebLocalFrame* frame,
207 const blink::WebURL& insecure_url,
208 bool did_block_entire_page);
209 void DidDispatchPingLoader(blink::WebLocalFrame* frame,
210 const blink::WebURL& url);
211 void WillRequestResource(blink::WebLocalFrame* frame,
212 const blink::WebCachedURLRequest& url_request);
213 void WillSendRequest(blink::WebLocalFrame* frame,
214 unsigned identifier,
215 blink::WebURLRequest& request,
216 const blink::WebURLResponse& redirect_response);
217 void DidReceiveResponse(blink::WebLocalFrame* frame,
218 unsigned identifier,
219 const blink::WebURLResponse& response);
220 void DidChangeResourcePriority(blink::WebLocalFrame* frame,
221 unsigned identifier,
222 const blink::WebURLRequest::Priority& priority,
223 int intra_priority_value);
224 void DidFinishResourceLoad(blink::WebLocalFrame* frame, unsigned identifier);
225 blink::WebNavigationPolicy DecidePolicyForNavigation(
226 const blink::WebFrameClient::NavigationPolicyInfo& info);
227 bool WillCheckAndDispatchMessageEvent(blink::WebLocalFrame* source_frame,
228 blink::WebFrame* target_frame,
229 blink::WebSecurityOrigin target,
230 blink::WebDOMMessageEvent event);
231 void ResetInputMethod();
233 blink::WebString acceptLanguages();
235 private:
236 template <class, typename, typename>
237 friend class WebFrameTestProxy;
239 enum CheckDoneReason {
240 LoadFinished,
241 MainResourceLoadFailed,
242 ResourceLoadCompleted
244 void CheckDone(blink::WebLocalFrame* frame, CheckDoneReason reason);
245 void AnimateNow();
246 void DrawSelectionRect(SkCanvas* canvas);
247 void DidCapturePixelsAsync(
248 const base::Callback<void(const SkBitmap&)>& callback,
249 const SkBitmap& bitmap);
251 blink::WebWidget* web_widget() const { return web_widget_; }
253 WebTestInterfaces* web_test_interfaces_;
254 TestInterfaces* test_interfaces_;
255 WebTestDelegate* delegate_;
256 blink::WebWidget* web_widget_;
258 WebTaskList task_list_;
260 blink::WebImage drag_image_;
262 scoped_ptr<SpellCheckClient> spellcheck_;
263 scoped_ptr<MockWebUserMediaClient> user_media_client_;
265 bool animate_scheduled_;
266 std::map<unsigned, std::string> resource_identifier_map_;
268 bool log_console_output_;
269 int chooser_count_;
271 scoped_ptr<MockCredentialManagerClient> credential_manager_client_;
272 scoped_ptr<MockWebSpeechRecognizer> speech_recognizer_;
273 scoped_ptr<MockScreenOrientationClient> screen_orientation_client_;
275 std::string accept_languages_;
277 private:
278 DISALLOW_COPY_AND_ASSIGN(WebTestProxyBase);
281 // WebTestProxy is used during LayoutTests and always instantiated, at time of
282 // writing with Base=RenderViewImpl. It does not directly inherit from it for
283 // layering purposes.
284 // The intent of that class is to wrap RenderViewImpl for tests purposes in
285 // order to reduce the amount of test specific code in the production code.
286 // WebTestProxy is only doing the glue between RenderViewImpl and
287 // WebTestProxyBase, that means that there is no logic living in this class
288 // except deciding which base class should be called (could be both).
290 // Examples of usage:
291 // * when a fooClient has a mock implementation, WebTestProxy can override the
292 // fooClient() call and have WebTestProxyBase return the mock implementation.
293 // * when a value needs to be overridden by LayoutTests, WebTestProxy can
294 // override RenderViewImpl's getter and call a getter from WebTestProxyBase
295 // instead. In addition, WebTestProxyBase will have a public setter that
296 // could be called from the TestRunner.
297 #if defined(OS_WIN)
298 // WebTestProxy is a diamond-shaped hierarchy, with WebWidgetClient at the root.
299 // VS warns when we inherit the WebWidgetClient method implementations from
300 // RenderWidget. It's safe to ignore that warning.
301 #pragma warning(disable: 4250)
302 #endif
303 template <class Base, typename T>
304 class WebTestProxy : public Base, public WebTestProxyBase {
305 public:
306 explicit WebTestProxy(T t) : Base(t) {}
308 // WebWidgetClient implementation.
309 virtual blink::WebScreenInfo screenInfo() {
310 blink::WebScreenInfo info = Base::screenInfo();
311 WebTestProxyBase::GetScreenOrientationForTesting(info);
312 return info;
315 // WebViewClient implementation.
316 virtual void scheduleAnimation() { WebTestProxyBase::ScheduleAnimation(); }
317 virtual void postAccessibilityEvent(const blink::WebAXObject& object,
318 blink::WebAXEvent event) {
319 WebTestProxyBase::PostAccessibilityEvent(object, event);
320 Base::postAccessibilityEvent(object, event);
322 virtual void startDragging(blink::WebLocalFrame* frame,
323 const blink::WebDragData& data,
324 blink::WebDragOperationsMask mask,
325 const blink::WebImage& image,
326 const blink::WebPoint& point) {
327 WebTestProxyBase::StartDragging(frame, data, mask, image, point);
328 // Don't forward this call to Base because we don't want to do a real
329 // drag-and-drop.
331 virtual void didChangeContents() {
332 WebTestProxyBase::DidChangeContents();
333 Base::didChangeContents();
335 virtual blink::WebView* createView(blink::WebLocalFrame* creator,
336 const blink::WebURLRequest& request,
337 const blink::WebWindowFeatures& features,
338 const blink::WebString& frame_name,
339 blink::WebNavigationPolicy policy,
340 bool suppress_opener) {
341 if (!WebTestProxyBase::CreateView(
342 creator, request, features, frame_name, policy, suppress_opener))
343 return 0;
344 return Base::createView(
345 creator, request, features, frame_name, policy, suppress_opener);
347 virtual void setStatusText(const blink::WebString& text) {
348 WebTestProxyBase::SetStatusText(text);
349 Base::setStatusText(text);
351 virtual void printPage(blink::WebLocalFrame* frame) {
352 WebTestProxyBase::PrintPage(frame);
354 virtual blink::WebSpeechRecognizer* speechRecognizer() {
355 return WebTestProxyBase::GetSpeechRecognizer();
357 virtual bool requestPointerLock() {
358 return WebTestProxyBase::RequestPointerLock();
360 virtual void requestPointerUnlock() {
361 WebTestProxyBase::RequestPointerUnlock();
363 virtual bool isPointerLocked() { return WebTestProxyBase::IsPointerLocked(); }
364 virtual void didFocus() {
365 WebTestProxyBase::DidFocus();
366 Base::didFocus();
368 virtual void didBlur() {
369 WebTestProxyBase::DidBlur();
370 Base::didBlur();
372 virtual void setToolTipText(const blink::WebString& text,
373 blink::WebTextDirection hint) {
374 WebTestProxyBase::SetToolTipText(text, hint);
375 Base::setToolTipText(text, hint);
377 virtual void resetInputMethod() { WebTestProxyBase::ResetInputMethod(); }
378 virtual bool runFileChooser(const blink::WebFileChooserParams& params,
379 blink::WebFileChooserCompletion* completion) {
380 return WebTestProxyBase::RunFileChooser(params, completion);
382 virtual void showValidationMessage(const blink::WebRect& anchor_in_root_view,
383 const blink::WebString& main_message,
384 blink::WebTextDirection main_message_hint,
385 const blink::WebString& sub_message,
386 blink::WebTextDirection sub_message_hint) {
387 base::string16 wrapped_main_text = main_message;
388 base::string16 wrapped_sub_text = sub_message;
390 Base::SetValidationMessageDirection(&wrapped_main_text, main_message_hint,
391 &wrapped_sub_text, sub_message_hint);
393 WebTestProxyBase::ShowValidationMessage(
394 wrapped_main_text, wrapped_sub_text);
396 virtual void postSpellCheckEvent(const blink::WebString& event_name) {
397 WebTestProxyBase::PostSpellCheckEvent(event_name);
399 virtual blink::WebString acceptLanguages() {
400 return WebTestProxyBase::acceptLanguages();
403 private:
404 virtual ~WebTestProxy() {}
406 DISALLOW_COPY_AND_ASSIGN(WebTestProxy);
409 } // namespace test_runner
411 #endif // COMPONENTS_TEST_RUNNER_WEB_TEST_PROXY_H_