summaryrefslogtreecommitdiffstats
path: root/components/test_runner/test_plugin.h
blob: cc91cbd75f9b4a40e3a9690d1b8e1790a3abf77a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_TEST_RUNNER_TEST_PLUGIN_H_
#define COMPONENTS_TEST_RUNNER_TEST_PLUGIN_H_

#include <string>

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "cc/layers/texture_layer.h"
#include "cc/layers/texture_layer_client.h"
#include "third_party/WebKit/public/platform/WebExternalTextureLayer.h"
#include "third_party/WebKit/public/platform/WebExternalTextureLayerClient.h"
#include "third_party/WebKit/public/platform/WebExternalTextureMailbox.h"
#include "third_party/WebKit/public/platform/WebLayer.h"
#include "third_party/WebKit/public/web/WebPlugin.h"
#include "third_party/WebKit/public/web/WebPluginContainer.h"

namespace blink {
class WebFrame;
class WebGraphicsContext3D;
class WebLayer;
struct WebPluginParams;
}

namespace cc {
class SharedBitmap;
}

namespace gpu {
namespace gles2 {
class GLES2Interface;
}
}

namespace test_runner {

class WebTestDelegate;

// A fake implemention of blink::WebPlugin for testing purposes.
//
// It uses WebGraphicsContext3D to paint a scene consisiting of a primitive
// over a background. The primitive and background can be customized using
// the following plugin parameters:
// primitive: none (default), triangle.
// background-color: black (default), red, green, blue.
// primitive-color: black (default), red, green, blue.
// opacity: [0.0 - 1.0]. Default is 1.0.
//
// Whether the plugin accepts touch events or not can be customized using the
// 'accepts-touch' plugin parameter (defaults to false).
class TestPlugin : public blink::WebPlugin, public cc::TextureLayerClient {
 public:
  static TestPlugin* create(blink::WebFrame* frame,
                            const blink::WebPluginParams& params,
                            WebTestDelegate* delegate);
  ~TestPlugin() override;

  static const blink::WebString& MimeType();
  static const blink::WebString& CanCreateWithoutRendererMimeType();
  static const blink::WebString& PluginPersistsMimeType();
  static bool IsSupportedMimeType(const blink::WebString& mime_type);

  // WebPlugin methods:
  bool initialize(blink::WebPluginContainer* container) override;
  void destroy() override;
  NPObject* scriptableObject() override;
  bool canProcessDrag() const override;
  bool supportsKeyboardFocus() const override;
  void updateAllLifecyclePhases() override {}
  void paint(blink::WebCanvas* canvas, const blink::WebRect& rect) override {}
  void updateGeometry(const blink::WebRect& window_rect,
                      const blink::WebRect& clip_rect,
                      const blink::WebRect& unobscured_rect,
                      const blink::WebVector<blink::WebRect>& cut_outs_rects,
                      bool is_visible) override;
  void updateFocus(bool focus, blink::WebFocusType focus_type) override {}
  void updateVisibility(bool visibility) override {}
  bool acceptsInputEvents() override;
  blink::WebInputEventResult handleInputEvent(
      const blink::WebInputEvent& event,
      blink::WebCursorInfo& info) override;
  bool handleDragStatusUpdate(blink::WebDragStatus drag_status,
                              const blink::WebDragData& data,
                              blink::WebDragOperationsMask mask,
                              const blink::WebPoint& position,
                              const blink::WebPoint& screen_position) override;
  void didReceiveResponse(const blink::WebURLResponse& response) override {}
  void didReceiveData(const char* data, int data_length) override {}
  void didFinishLoading() override {}
  void didFailLoading(const blink::WebURLError& error) override {}
  bool isPlaceholder() override;

  // cc::TextureLayerClient methods:
  bool PrepareTextureMailbox(
      cc::TextureMailbox* mailbox,
      scoped_ptr<cc::SingleReleaseCallback>* release_callback,
      bool use_shared_memory) override;

 private:
  TestPlugin(blink::WebFrame* frame,
             const blink::WebPluginParams& params,
             WebTestDelegate* delegate);

  enum Primitive { PrimitiveNone, PrimitiveTriangle };

  struct Scene {
    Primitive primitive;
    unsigned background_color[3];
    unsigned primitive_color[3];
    float opacity;

    unsigned vbo;
    unsigned program;
    int color_location;
    int position_location;

    Scene()
        : primitive(PrimitiveNone),
          opacity(1.0f)  // Fully opaque.
          ,
          vbo(0),
          program(0),
          color_location(-1),
          position_location(-1) {
      background_color[0] = background_color[1] = background_color[2] = 0;
      primitive_color[0] = primitive_color[1] = primitive_color[2] = 0;
    }
  };

  // Functions for parsing plugin parameters.
  Primitive ParsePrimitive(const blink::WebString& string);
  void ParseColor(const blink::WebString& string, unsigned color[3]);
  float ParseOpacity(const blink::WebString& string);
  bool ParseBoolean(const blink::WebString& string);

  // Functions for loading and drawing scene in GL.
  bool InitScene();
  void DrawSceneGL();
  void DestroyScene();
  bool InitProgram();
  bool InitPrimitive();
  void DrawPrimitive();
  unsigned LoadShader(unsigned type, const std::string& source);
  unsigned LoadProgram(const std::string& vertex_source,
                       const std::string& fragment_source);

  // Functions for drawing scene in Software.
  void DrawSceneSoftware(void* memory);

  blink::WebFrame* frame_;
  WebTestDelegate* delegate_;
  blink::WebPluginContainer* container_;

  blink::WebRect rect_;
  blink::WebGraphicsContext3D* context_;
  gpu::gles2::GLES2Interface* gl_;
  unsigned color_texture_;
  cc::TextureMailbox texture_mailbox_;
  scoped_ptr<cc::SharedBitmap> shared_bitmap_;
  bool mailbox_changed_;
  unsigned framebuffer_;
  Scene scene_;
  scoped_refptr<cc::TextureLayer> layer_;
  scoped_ptr<blink::WebLayer> web_layer_;

  blink::WebPluginContainer::TouchEventRequestType touch_event_request_;
  // Requests touch events from the WebPluginContainerImpl multiple times to
  // tickle webkit.org/b/108381
  bool re_request_touch_events_;
  bool print_event_details_;
  bool print_user_gesture_status_;
  bool can_process_drag_;
  bool supports_keyboard_focus_;

  bool is_persistent_;
  bool can_create_without_renderer_;

  DISALLOW_COPY_AND_ASSIGN(TestPlugin);
};

}  // namespace test_runner

#endif  // COMPONENTS_TEST_RUNNER_TEST_PLUGIN_H_