Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / shell / renderer / test_runner / web_test_proxy.h
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/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"
34
35 class SkCanvas;
36
37 namespace blink {
38 class WebAXObject;
39 class WebAudioDevice;
40 class WebCachedURLRequest;
41 class WebColorChooser;
42 class WebColorChooserClient;
43 class WebDataSource;
44 class WebDragData;
45 class WebFileChooserCompletion;
46 class WebFrame;
47 class WebImage;
48 class WebLocalFrame;
49 class WebMIDIAccessor;
50 class WebMIDIAccessorClient;
51 class WebMIDIClient;
52 class WebMIDIClientMock;
53 class WebNode;
54 class WebNotificationPresenter;
55 class WebPlugin;
56 class WebPushClient;
57 class WebRange;
58 class WebSerializedScriptValue;
59 class WebSpeechRecognizer;
60 class WebSpellCheckClient;
61 class WebString;
62 class WebURL;
63 class WebURLResponse;
64 class WebUserMediaClient;
65 class WebView;
66 class WebWidget;
67 struct WebColorSuggestion;
68 struct WebConsoleMessage;
69 struct WebContextMenuData;
70 struct WebFileChooserParams;
71 struct WebPluginParams;
72 struct WebPoint;
73 struct WebSize;
74 struct WebWindowFeatures;
75 typedef unsigned WebColor;
76 }
77
78 namespace content {
79
80 class MockScreenOrientationClient;
81 class MockWebPushClient;
82 class MockWebSpeechRecognizer;
83 class MockWebUserMediaClient;
84 class RenderFrame;
85 class SpellCheckClient;
86 class TestInterfaces;
87 class WebTestDelegate;
88 class WebTestInterfaces;
89
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 {
96  public:
97   void SetInterfaces(WebTestInterfaces* interfaces);
98   void SetDelegate(WebTestDelegate* delegate);
99   void set_widget(blink::WebWidget* widget) { web_widget_ = widget; }
100
101   void Reset();
102
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);
116
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);
124
125   void SetLogConsoleOutput(bool enabled);
126
127   void DidOpenChooser();
128   void DidCloseChooser();
129   bool IsChooserShown();
130
131   void DisplayAsyncThen(const base::Closure& callback);
132
133   void GetScreenOrientationForTesting(blink::WebScreenInfo&);
134   MockScreenOrientationClient* GetScreenOrientationClientMock();
135   blink::WebMIDIClientMock* GetMIDIClientMock();
136   MockWebSpeechRecognizer* GetSpeechRecognizerMock();
137
138   WebTaskList* mutable_task_list() { return &task_list_; }
139
140   blink::WebView* GetWebView() const;
141
142   void PostSpellCheckEvent(const blink::WebString& event_name);
143
144   // WebCompositeAndReadbackAsyncCallback implementation.
145   virtual void didCompositeAndReadback(const SkBitmap& bitmap);
146
147   void SetAcceptLanguages(const std::string& accept_languages);
148
149   MockWebPushClient* GetPushClientMock();
150
151  protected:
152   WebTestProxyBase();
153   ~WebTestProxyBase();
154
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();
185   void DidFocus();
186   void DidBlur();
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,
222                        unsigned identifier,
223                        blink::WebURLRequest& request,
224                        const blink::WebURLResponse& redirect_response);
225   void DidReceiveResponse(blink::WebLocalFrame* frame,
226                           unsigned identifier,
227                           const blink::WebURLResponse& response);
228   void DidChangeResourcePriority(blink::WebLocalFrame* frame,
229                                  unsigned identifier,
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();
240
241   blink::WebString acceptLanguages();
242   blink::WebPushClient* GetWebPushClient();
243
244  private:
245   template <class, typename, typename>
246   friend class WebFrameTestProxy;
247   void LocationChangeDone(blink::WebFrame* frame);
248   void AnimateNow();
249   void DrawSelectionRect(SkCanvas* canvas);
250   void DidDisplayAsync(const base::Closure& callback, const SkBitmap& bitmap);
251
252   blink::WebWidget* web_widget() const { return web_widget_; }
253
254   TestInterfaces* test_interfaces_;
255   WebTestDelegate* delegate_;
256   blink::WebWidget* web_widget_;
257
258   WebTaskList task_list_;
259
260   scoped_ptr<SpellCheckClient> spellcheck_;
261   scoped_ptr<MockWebUserMediaClient> user_media_client_;
262
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_;
267
268   bool log_console_output_;
269   int chooser_count_;
270
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_;
275
276   std::string accept_languages_;
277
278  private:
279   DISALLOW_COPY_AND_ASSIGN(WebTestProxyBase);
280 };
281
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).
290 //
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 {
300  public:
301   explicit WebTestProxy(T t) : Base(t) {}
302
303   virtual ~WebTestProxy() {}
304
305   // WebWidgetClient implementation.
306   virtual blink::WebScreenInfo screenInfo() {
307     blink::WebScreenInfo info = Base::screenInfo();
308     WebTestProxyBase::GetScreenOrientationForTesting(info);
309     return info;
310   }
311
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);
318   }
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
326     // drag-and-drop.
327   }
328   virtual void didChangeContents() {
329     WebTestProxyBase::DidChangeContents();
330     Base::didChangeContents();
331   }
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))
340       return 0;
341     return Base::createView(
342         creator, request, features, frame_name, policy, suppress_opener);
343   }
344   virtual void setStatusText(const blink::WebString& text) {
345     WebTestProxyBase::SetStatusText(text);
346     Base::setStatusText(text);
347   }
348   virtual void printPage(blink::WebLocalFrame* frame) {
349     WebTestProxyBase::PrintPage(frame);
350   }
351   virtual blink::WebSpeechRecognizer* speechRecognizer() {
352     return WebTestProxyBase::GetSpeechRecognizer();
353   }
354   virtual bool requestPointerLock() {
355     return WebTestProxyBase::RequestPointerLock();
356   }
357   virtual void requestPointerUnlock() {
358     WebTestProxyBase::RequestPointerUnlock();
359   }
360   virtual bool isPointerLocked() { return WebTestProxyBase::IsPointerLocked(); }
361   virtual void didFocus() {
362     WebTestProxyBase::DidFocus();
363     Base::didFocus();
364   }
365   virtual void didBlur() {
366     WebTestProxyBase::DidBlur();
367     Base::didBlur();
368   }
369   virtual void setToolTipText(const blink::WebString& text,
370                               blink::WebTextDirection hint) {
371     WebTestProxyBase::SetToolTipText(text, hint);
372     Base::setToolTipText(text, hint);
373   }
374   virtual void resetInputMethod() { WebTestProxyBase::ResetInputMethod(); }
375   virtual bool runFileChooser(const blink::WebFileChooserParams& params,
376                               blink::WebFileChooserCompletion* completion) {
377     return WebTestProxyBase::RunFileChooser(params, completion);
378   }
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);
385   }
386   virtual void postSpellCheckEvent(const blink::WebString& event_name) {
387     WebTestProxyBase::PostSpellCheckEvent(event_name);
388   }
389   virtual blink::WebString acceptLanguages() {
390     return WebTestProxyBase::acceptLanguages();
391   }
392   virtual blink::WebPushClient* webPushClient() {
393     return WebTestProxyBase::GetWebPushClient();
394   }
395
396  private:
397   DISALLOW_COPY_AND_ASSIGN(WebTestProxy);
398 };
399
400 }  // namespace content
401
402 #endif  // CONTENT_SHELL_RENDERER_TEST_RUNNER_WEB_TEST_PROXY_H_