Home | History | Annotate | Download | only in test_runner
      1 // Copyright 2013 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_TEST_PLUGIN_H_
      6 #define CONTENT_SHELL_RENDERER_TEST_RUNNER_TEST_PLUGIN_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "cc/layers/texture_layer.h"
     13 #include "cc/layers/texture_layer_client.h"
     14 #include "content/public/test/layouttest_support.h"
     15 #include "third_party/WebKit/public/platform/WebExternalTextureLayer.h"
     16 #include "third_party/WebKit/public/platform/WebExternalTextureLayerClient.h"
     17 #include "third_party/WebKit/public/platform/WebExternalTextureMailbox.h"
     18 #include "third_party/WebKit/public/platform/WebLayer.h"
     19 #include "third_party/WebKit/public/web/WebPlugin.h"
     20 #include "third_party/WebKit/public/web/WebPluginContainer.h"
     21 
     22 namespace blink {
     23 class WebFrame;
     24 class WebLayer;
     25 }
     26 
     27 namespace content {
     28 
     29 class WebTestDelegate;
     30 
     31 // A fake implemention of blink::WebPlugin for testing purposes.
     32 //
     33 // It uses WebGraphicsContext3D to paint a scene consisiting of a primitive
     34 // over a background. The primitive and background can be customized using
     35 // the following plugin parameters:
     36 // primitive: none (default), triangle.
     37 // background-color: black (default), red, green, blue.
     38 // primitive-color: black (default), red, green, blue.
     39 // opacity: [0.0 - 1.0]. Default is 1.0.
     40 //
     41 // Whether the plugin accepts touch events or not can be customized using the
     42 // 'accepts-touch' plugin parameter (defaults to false).
     43 class TestPlugin : public blink::WebPlugin, public cc::TextureLayerClient {
     44  public:
     45   static TestPlugin* create(blink::WebFrame* frame,
     46                             const blink::WebPluginParams& params,
     47                             WebTestDelegate* delegate);
     48   virtual ~TestPlugin();
     49 
     50   static const blink::WebString& MimeType();
     51   static const blink::WebString& CanCreateWithoutRendererMimeType();
     52   static const blink::WebString& PluginPersistsMimeType();
     53   static bool IsSupportedMimeType(const blink::WebString& mime_type);
     54 
     55   // WebPlugin methods:
     56   virtual bool initialize(blink::WebPluginContainer* container);
     57   virtual void destroy();
     58   virtual NPObject* scriptableObject();
     59   virtual bool canProcessDrag() const;
     60   virtual void paint(blink::WebCanvas* canvas, const blink::WebRect& rect) {}
     61   virtual void updateGeometry(
     62       const blink::WebRect& frame_rect,
     63       const blink::WebRect& clip_rect,
     64       const blink::WebVector<blink::WebRect>& cut_outs_rects,
     65       bool is_visible);
     66   virtual void updateFocus(bool focus) {}
     67   virtual void updateVisibility(bool visibility) {}
     68   virtual bool acceptsInputEvents();
     69   virtual bool handleInputEvent(const blink::WebInputEvent& event,
     70                                 blink::WebCursorInfo& info);
     71   virtual bool handleDragStatusUpdate(blink::WebDragStatus drag_status,
     72                                       const blink::WebDragData& data,
     73                                       blink::WebDragOperationsMask mask,
     74                                       const blink::WebPoint& position,
     75                                       const blink::WebPoint& screen_position);
     76   virtual void didReceiveResponse(const blink::WebURLResponse& response) {}
     77   virtual void didReceiveData(const char* data, int data_length) {}
     78   virtual void didFinishLoading() {}
     79   virtual void didFailLoading(const blink::WebURLError& error) {}
     80   virtual void didFinishLoadingFrameRequest(const blink::WebURL& url,
     81                                             void* notify_data) {}
     82   virtual void didFailLoadingFrameRequest(const blink::WebURL& url,
     83                                           void* notify_data,
     84                                           const blink::WebURLError& error) {}
     85   virtual bool isPlaceholder();
     86 
     87   // cc::TextureLayerClient methods:
     88   virtual bool PrepareTextureMailbox(
     89       cc::TextureMailbox* mailbox,
     90       scoped_ptr<cc::SingleReleaseCallback>* release_callback,
     91       bool use_shared_memory) OVERRIDE;
     92 
     93  private:
     94   TestPlugin(blink::WebFrame* frame,
     95              const blink::WebPluginParams& params,
     96              WebTestDelegate* delegate);
     97 
     98   enum Primitive { PrimitiveNone, PrimitiveTriangle };
     99 
    100   struct Scene {
    101     Primitive primitive;
    102     unsigned background_color[3];
    103     unsigned primitive_color[3];
    104     float opacity;
    105 
    106     unsigned vbo;
    107     unsigned program;
    108     int color_location;
    109     int position_location;
    110 
    111     Scene()
    112         : primitive(PrimitiveNone),
    113           opacity(1.0f)  // Fully opaque.
    114           ,
    115           vbo(0),
    116           program(0),
    117           color_location(-1),
    118           position_location(-1) {
    119       background_color[0] = background_color[1] = background_color[2] = 0;
    120       primitive_color[0] = primitive_color[1] = primitive_color[2] = 0;
    121     }
    122   };
    123 
    124   // Functions for parsing plugin parameters.
    125   Primitive ParsePrimitive(const blink::WebString& string);
    126   void ParseColor(const blink::WebString& string, unsigned color[3]);
    127   float ParseOpacity(const blink::WebString& string);
    128   bool ParseBoolean(const blink::WebString& string);
    129 
    130   // Functions for loading and drawing scene in GL.
    131   bool InitScene();
    132   void DrawSceneGL();
    133   void DestroyScene();
    134   bool InitProgram();
    135   bool InitPrimitive();
    136   void DrawPrimitive();
    137   unsigned LoadShader(unsigned type, const std::string& source);
    138   unsigned LoadProgram(const std::string& vertex_source,
    139                        const std::string& fragment_source);
    140 
    141   // Functions for drawing scene in Software.
    142   void DrawSceneSoftware(void* memory, size_t bytes);
    143 
    144   blink::WebFrame* frame_;
    145   WebTestDelegate* delegate_;
    146   blink::WebPluginContainer* container_;
    147 
    148   blink::WebRect rect_;
    149   blink::WebGraphicsContext3D* context_;
    150   unsigned color_texture_;
    151   cc::TextureMailbox texture_mailbox_;
    152   scoped_ptr<base::SharedMemory> shared_bitmap_;
    153   bool mailbox_changed_;
    154   unsigned framebuffer_;
    155   Scene scene_;
    156   scoped_refptr<cc::TextureLayer> layer_;
    157   scoped_ptr<blink::WebLayer> web_layer_;
    158 
    159   blink::WebPluginContainer::TouchEventRequestType touch_event_request_;
    160   // Requests touch events from the WebPluginContainerImpl multiple times to
    161   // tickle webkit.org/b/108381
    162   bool re_request_touch_events_;
    163   bool print_event_details_;
    164   bool print_user_gesture_status_;
    165   bool can_process_drag_;
    166 
    167   bool is_persistent_;
    168   bool can_create_without_renderer_;
    169 
    170   DISALLOW_COPY_AND_ASSIGN(TestPlugin);
    171 };
    172 
    173 }  // namespace content
    174 
    175 #endif  // CONTENT_SHELL_RENDERER_TEST_RUNNER_TEST_PLUGIN_H_
    176