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