web_test_proxy.h revision 116680a4aac90f2aa7413d9095a592090648e557
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.
4
5#ifndef CONTENT_SHELL_RENDERER_TEST_RUNNER_WEB_TEST_PROXY_H_
6#define CONTENT_SHELL_RENDERER_TEST_RUNNER_WEB_TEST_PROXY_H_
7
8#include <deque>
9#include <map>
10#include <string>
11
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/WebHistoryCommitType.h"
27#include "third_party/WebKit/public/web/WebIconURL.h"
28#include "third_party/WebKit/public/web/WebNavigationPolicy.h"
29#include "third_party/WebKit/public/web/WebNavigationType.h"
30#include "third_party/WebKit/public/web/WebSecurityOrigin.h"
31#include "third_party/WebKit/public/web/WebTextAffinity.h"
32#include "third_party/WebKit/public/web/WebTextDirection.h"
33
34class SkCanvas;
35
36namespace blink {
37class WebAXObject;
38class WebAudioDevice;
39class WebCachedURLRequest;
40class WebColorChooser;
41class WebColorChooserClient;
42class WebDataSource;
43class WebDragData;
44class WebFileChooserCompletion;
45class WebFrame;
46class WebImage;
47class WebLocalFrame;
48class WebMIDIAccessor;
49class WebMIDIAccessorClient;
50class WebMIDIClient;
51class WebMIDIClientMock;
52class WebNode;
53class WebNotificationPresenter;
54class WebPlugin;
55class WebPushClient;
56class WebRange;
57class WebSerializedScriptValue;
58class WebSpeechRecognizer;
59class WebSpellCheckClient;
60class WebString;
61class WebURL;
62class WebURLResponse;
63class WebUserMediaClient;
64class WebView;
65class WebWidget;
66struct WebColorSuggestion;
67struct WebConsoleMessage;
68struct WebContextMenuData;
69struct WebFileChooserParams;
70struct WebPluginParams;
71struct WebPoint;
72struct WebSize;
73struct WebWindowFeatures;
74typedef unsigned WebColor;
75}
76
77namespace content {
78
79class MockScreenOrientationClient;
80class MockWebPushClient;
81class MockWebSpeechRecognizer;
82class MockWebUserMediaClient;
83class RenderFrame;
84class SpellCheckClient;
85class TestInterfaces;
86class WebTestDelegate;
87class WebTestInterfaces;
88
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.
94class WebTestProxyBase : public blink::WebCompositeAndReadbackAsyncCallback {
95 public:
96  void SetInterfaces(WebTestInterfaces* interfaces);
97  void SetDelegate(WebTestDelegate* delegate);
98  void set_widget(blink::WebWidget* widget) { web_widget_ = widget; }
99
100  void Reset();
101
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::WebRect& anchor_in_root_view,
110                             const blink::WebString& message,
111                             const blink::WebString& sub_message,
112                             blink::WebTextDirection hint);
113  void HideValidationMessage();
114  void MoveValidationMessage(const blink::WebRect& anchor_in_root_view);
115
116  std::string CaptureTree(bool debug_render_tree);
117  void CapturePixelsForPrinting(
118      const base::Callback<void(const SkBitmap&)>& callback);
119  void CapturePixelsAsync(
120      const base::Callback<void(const SkBitmap&)>& callback);
121
122  void SetLogConsoleOutput(bool enabled);
123
124  void DidOpenChooser();
125  void DidCloseChooser();
126  bool IsChooserShown();
127
128  void DisplayAsyncThen(const base::Closure& callback);
129
130  void GetScreenOrientationForTesting(blink::WebScreenInfo&);
131  MockScreenOrientationClient* GetScreenOrientationClientMock();
132  blink::WebMIDIClientMock* GetMIDIClientMock();
133  MockWebSpeechRecognizer* GetSpeechRecognizerMock();
134
135  WebTaskList* mutable_task_list() { return &task_list_; }
136
137  blink::WebView* GetWebView() const;
138
139  void PostSpellCheckEvent(const blink::WebString& event_name);
140
141  // WebCompositeAndReadbackAsyncCallback implementation.
142  virtual void didCompositeAndReadback(const SkBitmap& bitmap);
143
144  void SetAcceptLanguages(const std::string& accept_languages);
145
146  MockWebPushClient* GetPushClientMock();
147
148 protected:
149  WebTestProxyBase();
150  ~WebTestProxyBase();
151
152  void ScheduleAnimation();
153  void PostAccessibilityEvent(const blink::WebAXObject&, blink::WebAXEvent);
154  void StartDragging(blink::WebLocalFrame* frame,
155                     const blink::WebDragData& data,
156                     blink::WebDragOperationsMask mask,
157                     const blink::WebImage& image,
158                     const blink::WebPoint& point);
159  void DidChangeSelection(bool isEmptySelection);
160  void DidChangeContents();
161  void DidEndEditing();
162  bool CreateView(blink::WebLocalFrame* creator,
163                  const blink::WebURLRequest& request,
164                  const blink::WebWindowFeatures& features,
165                  const blink::WebString& frame_name,
166                  blink::WebNavigationPolicy policy,
167                  bool suppress_opener);
168  blink::WebPlugin* CreatePlugin(blink::WebLocalFrame* frame,
169                                 const blink::WebPluginParams& params);
170  void SetStatusText(const blink::WebString& text);
171  void DidStopLoading();
172  void ShowContextMenu(blink::WebLocalFrame* frame,
173                       const blink::WebContextMenuData& data);
174  blink::WebUserMediaClient* GetUserMediaClient();
175  void PrintPage(blink::WebLocalFrame* frame);
176  blink::WebNotificationPresenter* GetNotificationPresenter();
177  blink::WebMIDIClient* GetWebMIDIClient();
178  blink::WebSpeechRecognizer* GetSpeechRecognizer();
179  bool RequestPointerLock();
180  void RequestPointerUnlock();
181  bool IsPointerLocked();
182  void DidFocus();
183  void DidBlur();
184  void SetToolTipText(const blink::WebString& text,
185                      blink::WebTextDirection direction);
186  void DidAddMessageToConsole(const blink::WebConsoleMessage& text,
187                              const blink::WebString& source_name,
188                              unsigned source_line);
189  void LoadURLExternally(blink::WebLocalFrame* frame,
190                         const blink::WebURLRequest& request,
191                         blink::WebNavigationPolicy policy,
192                         const blink::WebString& suggested_name);
193  void DidStartProvisionalLoad(blink::WebLocalFrame*);
194  void DidReceiveServerRedirectForProvisionalLoad(blink::WebLocalFrame* frame);
195  bool DidFailProvisionalLoad(blink::WebLocalFrame* frame,
196                              const blink::WebURLError& error);
197  void DidCommitProvisionalLoad(blink::WebLocalFrame* frame,
198                                const blink::WebHistoryItem& history_item,
199                                blink::WebHistoryCommitType history_type);
200  void DidReceiveTitle(blink::WebLocalFrame* frame,
201                       const blink::WebString& title,
202                       blink::WebTextDirection text_direction);
203  void DidChangeIcon(blink::WebLocalFrame* frame,
204                     blink::WebIconURL::Type icon_type);
205  void DidFinishDocumentLoad(blink::WebLocalFrame* frame);
206  void DidHandleOnloadEvents(blink::WebLocalFrame* frame);
207  void DidFailLoad(blink::WebLocalFrame* frame,
208                   const blink::WebURLError& error);
209  void DidFinishLoad(blink::WebLocalFrame* frame);
210  void DidChangeLocationWithinPage(blink::WebLocalFrame* frame);
211  void DidDetectXSS(blink::WebLocalFrame* frame,
212                    const blink::WebURL& insecure_url,
213                    bool did_block_entire_page);
214  void DidDispatchPingLoader(blink::WebLocalFrame* frame,
215                             const blink::WebURL& url);
216  void WillRequestResource(blink::WebLocalFrame* frame,
217                           const blink::WebCachedURLRequest& url_request);
218  void WillSendRequest(blink::WebLocalFrame* frame,
219                       unsigned identifier,
220                       blink::WebURLRequest& request,
221                       const blink::WebURLResponse& redirect_response);
222  void DidReceiveResponse(blink::WebLocalFrame* frame,
223                          unsigned identifier,
224                          const blink::WebURLResponse& response);
225  void DidChangeResourcePriority(blink::WebLocalFrame* frame,
226                                 unsigned identifier,
227                                 const blink::WebURLRequest::Priority& priority,
228                                 int intra_priority_value);
229  void DidFinishResourceLoad(blink::WebLocalFrame* frame, unsigned identifier);
230  blink::WebNavigationPolicy DecidePolicyForNavigation(
231      blink::WebLocalFrame* frame,
232      blink::WebDataSource::ExtraData* data,
233      const blink::WebURLRequest& request,
234      blink::WebNavigationType navigation_type,
235      blink::WebNavigationPolicy default_policy,
236      bool is_redirect);
237  bool WillCheckAndDispatchMessageEvent(blink::WebLocalFrame* source_frame,
238                                        blink::WebFrame* target_frame,
239                                        blink::WebSecurityOrigin target,
240                                        blink::WebDOMMessageEvent event);
241  void ResetInputMethod();
242
243  blink::WebString acceptLanguages();
244  blink::WebPushClient* GetWebPushClient();
245
246 private:
247  template <class, typename, typename>
248  friend class WebFrameTestProxy;
249  void LocationChangeDone(blink::WebFrame* frame);
250  void AnimateNow();
251  void DrawSelectionRect(SkCanvas* canvas);
252  void DidDisplayAsync(const base::Closure& callback, const SkBitmap& bitmap);
253
254  blink::WebWidget* web_widget() const { return web_widget_; }
255
256  TestInterfaces* test_interfaces_;
257  WebTestDelegate* delegate_;
258  blink::WebWidget* web_widget_;
259
260  WebTaskList task_list_;
261
262  scoped_ptr<SpellCheckClient> spellcheck_;
263  scoped_ptr<MockWebUserMediaClient> user_media_client_;
264
265  bool animate_scheduled_;
266  std::map<unsigned, std::string> resource_identifier_map_;
267  std::deque<base::Callback<void(const SkBitmap&)> >
268      composite_and_readback_callbacks_;
269
270  bool log_console_output_;
271  int chooser_count_;
272
273  scoped_ptr<blink::WebMIDIClientMock> midi_client_;
274  scoped_ptr<MockWebSpeechRecognizer> speech_recognizer_;
275  scoped_ptr<MockWebPushClient> push_client_;
276  scoped_ptr<MockScreenOrientationClient> screen_orientation_client_;
277
278  std::string accept_languages_;
279
280 private:
281  DISALLOW_COPY_AND_ASSIGN(WebTestProxyBase);
282};
283
284// WebTestProxy is used during LayoutTests and always instantiated, at time of
285// writing with Base=RenderViewImpl. It does not directly inherit from it for
286// layering purposes.
287// The intent of that class is to wrap RenderViewImpl for tests purposes in
288// order to reduce the amount of test specific code in the production code.
289// WebTestProxy is only doing the glue between RenderViewImpl and
290// WebTestProxyBase, that means that there is no logic living in this class
291// except deciding which base class should be called (could be both).
292//
293// Examples of usage:
294//  * when a fooClient has a mock implementation, WebTestProxy can override the
295//    fooClient() call and have WebTestProxyBase return the mock implementation.
296//  * when a value needs to be overridden by LayoutTests, WebTestProxy can
297//    override RenderViewImpl's getter and call a getter from WebTestProxyBase
298//    instead. In addition, WebTestProxyBase will have a public setter that
299//    could be called from the TestRunner.
300template <class Base, typename T>
301class WebTestProxy : public Base, public WebTestProxyBase {
302 public:
303  explicit WebTestProxy(T t) : Base(t) {}
304
305  virtual ~WebTestProxy() {}
306
307  // WebWidgetClient implementation.
308  virtual blink::WebScreenInfo screenInfo() {
309    blink::WebScreenInfo info = Base::screenInfo();
310    WebTestProxyBase::GetScreenOrientationForTesting(info);
311    return info;
312  }
313
314  // WebViewClient implementation.
315  virtual void scheduleAnimation() { WebTestProxyBase::ScheduleAnimation(); }
316  virtual void postAccessibilityEvent(const blink::WebAXObject& object,
317                                      blink::WebAXEvent event) {
318    WebTestProxyBase::PostAccessibilityEvent(object, event);
319    Base::postAccessibilityEvent(object, event);
320  }
321  virtual void startDragging(blink::WebLocalFrame* frame,
322                             const blink::WebDragData& data,
323                             blink::WebDragOperationsMask mask,
324                             const blink::WebImage& image,
325                             const blink::WebPoint& point) {
326    WebTestProxyBase::StartDragging(frame, data, mask, image, point);
327    // Don't forward this call to Base because we don't want to do a real
328    // drag-and-drop.
329  }
330  virtual void didChangeContents() {
331    WebTestProxyBase::DidChangeContents();
332    Base::didChangeContents();
333  }
334  virtual blink::WebView* createView(blink::WebLocalFrame* creator,
335                                     const blink::WebURLRequest& request,
336                                     const blink::WebWindowFeatures& features,
337                                     const blink::WebString& frame_name,
338                                     blink::WebNavigationPolicy policy,
339                                     bool suppress_opener) {
340    if (!WebTestProxyBase::CreateView(
341            creator, request, features, frame_name, policy, suppress_opener))
342      return 0;
343    return Base::createView(
344        creator, request, features, frame_name, policy, suppress_opener);
345  }
346  virtual void setStatusText(const blink::WebString& text) {
347    WebTestProxyBase::SetStatusText(text);
348    Base::setStatusText(text);
349  }
350  virtual void printPage(blink::WebLocalFrame* frame) {
351    WebTestProxyBase::PrintPage(frame);
352  }
353  virtual blink::WebSpeechRecognizer* speechRecognizer() {
354    return WebTestProxyBase::GetSpeechRecognizer();
355  }
356  virtual bool requestPointerLock() {
357    return WebTestProxyBase::RequestPointerLock();
358  }
359  virtual void requestPointerUnlock() {
360    WebTestProxyBase::RequestPointerUnlock();
361  }
362  virtual bool isPointerLocked() { return WebTestProxyBase::IsPointerLocked(); }
363  virtual void didFocus() {
364    WebTestProxyBase::DidFocus();
365    Base::didFocus();
366  }
367  virtual void didBlur() {
368    WebTestProxyBase::DidBlur();
369    Base::didBlur();
370  }
371  virtual void setToolTipText(const blink::WebString& text,
372                              blink::WebTextDirection hint) {
373    WebTestProxyBase::SetToolTipText(text, hint);
374    Base::setToolTipText(text, hint);
375  }
376  virtual void resetInputMethod() { WebTestProxyBase::ResetInputMethod(); }
377  virtual bool runFileChooser(const blink::WebFileChooserParams& params,
378                              blink::WebFileChooserCompletion* completion) {
379    return WebTestProxyBase::RunFileChooser(params, completion);
380  }
381  virtual void showValidationMessage(const blink::WebRect& anchor_in_root_view,
382                                     const blink::WebString& message,
383                                     const blink::WebString& sub_message,
384                                     blink::WebTextDirection hint) {
385    WebTestProxyBase::ShowValidationMessage(
386        anchor_in_root_view, message, sub_message, hint);
387  }
388  virtual void postSpellCheckEvent(const blink::WebString& event_name) {
389    WebTestProxyBase::PostSpellCheckEvent(event_name);
390  }
391  virtual blink::WebString acceptLanguages() {
392    return WebTestProxyBase::acceptLanguages();
393  }
394  virtual blink::WebPushClient* webPushClient() {
395    return WebTestProxyBase::GetWebPushClient();
396  }
397
398 private:
399  DISALLOW_COPY_AND_ASSIGN(WebTestProxy);
400};
401
402}  // namespace content
403
404#endif  // CONTENT_SHELL_RENDERER_TEST_RUNNER_WEB_TEST_PROXY_H_
405