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.
5 #include "content/shell/renderer/test_runner/TestPlugin.h"
7 #include "base/basictypes.h"
8 #include "content/shell/renderer/test_runner/TestCommon.h"
9 #include "content/shell/renderer/test_runner/WebTestDelegate.h"
10 #include "third_party/WebKit/public/platform/Platform.h"
11 #include "third_party/WebKit/public/platform/WebCompositorSupport.h"
12 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
13 #include "third_party/WebKit/public/web/WebFrame.h"
14 #include "third_party/WebKit/public/web/WebInputEvent.h"
15 #include "third_party/WebKit/public/web/WebKit.h"
16 #include "third_party/WebKit/public/web/WebPluginParams.h"
17 #include "third_party/WebKit/public/web/WebTouchPoint.h"
18 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
20 using namespace blink;
23 namespace WebTestRunner {
27 // GLenum values copied from gl2.h.
31 #define GL_TRIANGLES 0x0004
32 #define GL_ONE_MINUS_SRC_ALPHA 0x0303
33 #define GL_DEPTH_TEST 0x0B71
34 #define GL_BLEND 0x0BE2
35 #define GL_SCISSOR_TEST 0x0B90
36 #define GL_TEXTURE_2D 0x0DE1
37 #define GL_FLOAT 0x1406
38 #define GL_RGBA 0x1908
39 #define GL_UNSIGNED_BYTE 0x1401
40 #define GL_TEXTURE_MAG_FILTER 0x2800
41 #define GL_TEXTURE_MIN_FILTER 0x2801
42 #define GL_TEXTURE_WRAP_S 0x2802
43 #define GL_TEXTURE_WRAP_T 0x2803
44 #define GL_NEAREST 0x2600
45 #define GL_COLOR_BUFFER_BIT 0x4000
46 #define GL_CLAMP_TO_EDGE 0x812F
47 #define GL_ARRAY_BUFFER 0x8892
48 #define GL_STATIC_DRAW 0x88E4
49 #define GL_FRAGMENT_SHADER 0x8B30
50 #define GL_VERTEX_SHADER 0x8B31
51 #define GL_COMPILE_STATUS 0x8B81
52 #define GL_LINK_STATUS 0x8B82
53 #define GL_COLOR_ATTACHMENT0 0x8CE0
54 #define GL_FRAMEBUFFER_COMPLETE 0x8CD5
55 #define GL_FRAMEBUFFER 0x8D40
57 void premultiplyAlpha(const unsigned colorIn[3], float alpha, float colorOut[4])
59 for (int i = 0; i < 3; ++i)
60 colorOut[i] = (colorIn[i] / 255.0f) * alpha;
65 const char* pointState(WebTouchPoint::State state)
68 case WebTouchPoint::StateReleased:
70 case WebTouchPoint::StatePressed:
72 case WebTouchPoint::StateMoved:
74 case WebTouchPoint::StateCancelled:
80 BLINK_ASSERT_NOT_REACHED();
84 void printTouchList(WebTestDelegate* delegate, const WebTouchPoint* points, int length)
86 for (int i = 0; i < length; ++i) {
88 snprintf(buffer, sizeof(buffer), "* %d, %d: %s\n", points[i].position.x, points[i].position.y, pointState(points[i].state));
89 delegate->printMessage(buffer);
93 void printEventDetails(WebTestDelegate* delegate, const WebInputEvent& event)
95 if (WebInputEvent::isTouchEventType(event.type)) {
96 const WebTouchEvent& touch = static_cast<const WebTouchEvent&>(event);
97 printTouchList(delegate, touch.touches, touch.touchesLength);
98 printTouchList(delegate, touch.changedTouches, touch.changedTouchesLength);
99 printTouchList(delegate, touch.targetTouches, touch.targetTouchesLength);
100 } else if (WebInputEvent::isMouseEventType(event.type) || event.type == WebInputEvent::MouseWheel) {
101 const WebMouseEvent& mouse = static_cast<const WebMouseEvent&>(event);
103 snprintf(buffer, sizeof(buffer), "* %d, %d\n", mouse.x, mouse.y);
104 delegate->printMessage(buffer);
105 } else if (WebInputEvent::isGestureEventType(event.type)) {
106 const WebGestureEvent& gesture = static_cast<const WebGestureEvent&>(event);
108 snprintf(buffer, sizeof(buffer), "* %d, %d\n", gesture.x, gesture.y);
109 delegate->printMessage(buffer);
113 WebPluginContainer::TouchEventRequestType parseTouchEventRequestType(const WebString& string)
115 if (string == WebString::fromUTF8("raw"))
116 return WebPluginContainer::TouchEventRequestTypeRaw;
117 if (string == WebString::fromUTF8("synthetic"))
118 return WebPluginContainer::TouchEventRequestTypeSynthesizedMouse;
119 return WebPluginContainer::TouchEventRequestTypeNone;
122 void deferredDelete(void* context)
124 TestPlugin* plugin = static_cast<TestPlugin*>(context);
131 TestPlugin::TestPlugin(WebFrame* frame, const WebPluginParams& params, WebTestDelegate* delegate)
133 , m_delegate(delegate)
137 , m_mailboxChanged(false)
139 , m_touchEventRequest(WebPluginContainer::TouchEventRequestTypeNone)
140 , m_reRequestTouchEvents(false)
141 , m_printEventDetails(false)
142 , m_printUserGestureStatus(false)
143 , m_canProcessDrag(false)
145 const CR_DEFINE_STATIC_LOCAL(WebString, kAttributePrimitive, ("primitive"));
146 const CR_DEFINE_STATIC_LOCAL(WebString, kAttributeBackgroundColor, ("background-color"));
147 const CR_DEFINE_STATIC_LOCAL(WebString, kAttributePrimitiveColor, ("primitive-color"));
148 const CR_DEFINE_STATIC_LOCAL(WebString, kAttributeOpacity, ("opacity"));
149 const CR_DEFINE_STATIC_LOCAL(WebString, kAttributeAcceptsTouch, ("accepts-touch"));
150 const CR_DEFINE_STATIC_LOCAL(WebString, kAttributeReRequestTouchEvents, ("re-request-touch"));
151 const CR_DEFINE_STATIC_LOCAL(WebString, kAttributePrintEventDetails, ("print-event-details"));
152 const CR_DEFINE_STATIC_LOCAL(WebString, kAttributeCanProcessDrag, ("can-process-drag"));
153 const CR_DEFINE_STATIC_LOCAL(WebString, kAttributePrintUserGestureStatus, ("print-user-gesture-status"));
155 BLINK_ASSERT(params.attributeNames.size() == params.attributeValues.size());
156 size_t size = params.attributeNames.size();
157 for (size_t i = 0; i < size; ++i) {
158 const WebString& attributeName = params.attributeNames[i];
159 const WebString& attributeValue = params.attributeValues[i];
161 if (attributeName == kAttributePrimitive)
162 m_scene.primitive = parsePrimitive(attributeValue);
163 else if (attributeName == kAttributeBackgroundColor)
164 parseColor(attributeValue, m_scene.backgroundColor);
165 else if (attributeName == kAttributePrimitiveColor)
166 parseColor(attributeValue, m_scene.primitiveColor);
167 else if (attributeName == kAttributeOpacity)
168 m_scene.opacity = parseOpacity(attributeValue);
169 else if (attributeName == kAttributeAcceptsTouch)
170 m_touchEventRequest = parseTouchEventRequestType(attributeValue);
171 else if (attributeName == kAttributeReRequestTouchEvents)
172 m_reRequestTouchEvents = parseBoolean(attributeValue);
173 else if (attributeName == kAttributePrintEventDetails)
174 m_printEventDetails = parseBoolean(attributeValue);
175 else if (attributeName == kAttributeCanProcessDrag)
176 m_canProcessDrag = parseBoolean(attributeValue);
177 else if (attributeName == kAttributePrintUserGestureStatus)
178 m_printUserGestureStatus = parseBoolean(attributeValue);
182 TestPlugin::~TestPlugin()
186 bool TestPlugin::initialize(WebPluginContainer* container)
188 WebGraphicsContext3D::Attributes attrs;
189 m_context = Platform::current()->createOffscreenGraphicsContext3D(attrs);
193 if (!m_context->makeContextCurrent())
199 m_layer = scoped_ptr<WebExternalTextureLayer>(Platform::current()->compositorSupport()->createExternalTextureLayer(this));
200 m_container = container;
201 m_container->setWebLayer(m_layer->layer());
202 if (m_reRequestTouchEvents) {
203 m_container->requestTouchEventType(WebPluginContainer::TouchEventRequestTypeSynthesizedMouse);
204 m_container->requestTouchEventType(WebPluginContainer::TouchEventRequestTypeRaw);
206 m_container->requestTouchEventType(m_touchEventRequest);
207 m_container->setWantsWheelEvents(true);
211 void TestPlugin::destroy()
214 m_layer->clearTexture();
216 m_container->setWebLayer(0);
226 Platform::current()->callOnMainThread(deferredDelete, this);
229 NPObject* TestPlugin::scriptableObject()
234 bool TestPlugin::canProcessDrag() const
236 return m_canProcessDrag;
239 void TestPlugin::updateGeometry(const WebRect& frameRect, const WebRect& clipRect, const WebVector<WebRect>& cutOutsRects, bool isVisible)
241 if (clipRect == m_rect)
244 if (m_rect.isEmpty())
247 m_context->viewport(0, 0, m_rect.width, m_rect.height);
249 m_context->bindTexture(GL_TEXTURE_2D, m_colorTexture);
250 m_context->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
251 m_context->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
252 m_context->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
253 m_context->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
254 m_context->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_rect.width, m_rect.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
255 m_context->bindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
256 m_context->framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_colorTexture, 0);
260 m_context->genMailboxCHROMIUM(m_mailbox.name);
261 m_context->produceTextureCHROMIUM(GL_TEXTURE_2D, m_mailbox.name);
264 m_layer->layer()->invalidate();
265 m_mailboxChanged = true;
268 bool TestPlugin::acceptsInputEvents()
273 bool TestPlugin::isPlaceholder()
278 blink::WebGraphicsContext3D* TestPlugin::context()
283 bool TestPlugin::prepareMailbox(blink::WebExternalTextureMailbox* mailbox, blink::WebExternalBitmap*)
285 if (!m_mailboxChanged)
287 *mailbox = m_mailbox;
288 m_mailboxChanged = false;
292 void TestPlugin::mailboxReleased(const blink::WebExternalTextureMailbox&)
296 TestPlugin::Primitive TestPlugin::parsePrimitive(const WebString& string)
298 const CR_DEFINE_STATIC_LOCAL(WebString, kPrimitiveNone, ("none"));
299 const CR_DEFINE_STATIC_LOCAL(WebString, kPrimitiveTriangle, ("triangle"));
301 Primitive primitive = PrimitiveNone;
302 if (string == kPrimitiveNone)
303 primitive = PrimitiveNone;
304 else if (string == kPrimitiveTriangle)
305 primitive = PrimitiveTriangle;
307 BLINK_ASSERT_NOT_REACHED();
311 // FIXME: This method should already exist. Use it.
312 // For now just parse primary colors.
313 void TestPlugin::parseColor(const WebString& string, unsigned color[3])
315 color[0] = color[1] = color[2] = 0;
316 if (string == "black")
321 else if (string == "green")
323 else if (string == "blue")
326 BLINK_ASSERT_NOT_REACHED();
329 float TestPlugin::parseOpacity(const WebString& string)
331 return static_cast<float>(atof(string.utf8().data()));
334 bool TestPlugin::parseBoolean(const WebString& string)
336 const CR_DEFINE_STATIC_LOCAL(WebString, kPrimitiveTrue, ("true"));
337 return string == kPrimitiveTrue;
340 bool TestPlugin::initScene()
343 premultiplyAlpha(m_scene.backgroundColor, m_scene.opacity, color);
345 m_colorTexture = m_context->createTexture();
346 m_framebuffer = m_context->createFramebuffer();
348 m_context->viewport(0, 0, m_rect.width, m_rect.height);
349 m_context->disable(GL_DEPTH_TEST);
350 m_context->disable(GL_SCISSOR_TEST);
352 m_context->clearColor(color[0], color[1], color[2], color[3]);
354 m_context->enable(GL_BLEND);
355 m_context->blendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
357 return m_scene.primitive != PrimitiveNone ? initProgram() && initPrimitive() : true;
360 void TestPlugin::drawScene()
362 m_context->viewport(0, 0, m_rect.width, m_rect.height);
363 m_context->clear(GL_COLOR_BUFFER_BIT);
365 if (m_scene.primitive != PrimitiveNone)
369 void TestPlugin::destroyScene()
371 if (m_scene.program) {
372 m_context->deleteProgram(m_scene.program);
376 m_context->deleteBuffer(m_scene.vbo);
381 m_context->deleteFramebuffer(m_framebuffer);
385 if (m_colorTexture) {
386 m_context->deleteTexture(m_colorTexture);
391 bool TestPlugin::initProgram()
393 const string vertexSource(
394 "attribute vec4 position; \n"
396 " gl_Position = position; \n"
400 const string fragmentSource(
401 "precision mediump float; \n"
402 "uniform vec4 color; \n"
404 " gl_FragColor = color; \n"
408 m_scene.program = loadProgram(vertexSource, fragmentSource);
409 if (!m_scene.program)
412 m_scene.colorLocation = m_context->getUniformLocation(m_scene.program, "color");
413 m_scene.positionLocation = m_context->getAttribLocation(m_scene.program, "position");
417 bool TestPlugin::initPrimitive()
419 BLINK_ASSERT(m_scene.primitive == PrimitiveTriangle);
421 m_scene.vbo = m_context->createBuffer();
425 const float vertices[] = {
429 m_context->bindBuffer(GL_ARRAY_BUFFER, m_scene.vbo);
430 m_context->bufferData(GL_ARRAY_BUFFER, sizeof(vertices), 0, GL_STATIC_DRAW);
431 m_context->bufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
435 void TestPlugin::drawPrimitive()
437 BLINK_ASSERT(m_scene.primitive == PrimitiveTriangle);
438 BLINK_ASSERT(m_scene.vbo);
439 BLINK_ASSERT(m_scene.program);
441 m_context->useProgram(m_scene.program);
443 // Bind primitive color.
445 premultiplyAlpha(m_scene.primitiveColor, m_scene.opacity, color);
446 m_context->uniform4f(m_scene.colorLocation, color[0], color[1], color[2], color[3]);
448 // Bind primitive vertices.
449 m_context->bindBuffer(GL_ARRAY_BUFFER, m_scene.vbo);
450 m_context->enableVertexAttribArray(m_scene.positionLocation);
451 m_context->vertexAttribPointer(m_scene.positionLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
452 m_context->drawArrays(GL_TRIANGLES, 0, 3);
455 unsigned TestPlugin::loadShader(unsigned type, const string& source)
457 unsigned shader = m_context->createShader(type);
459 m_context->shaderSource(shader, source.data());
460 m_context->compileShader(shader);
463 m_context->getShaderiv(shader, GL_COMPILE_STATUS, &compiled);
465 m_context->deleteShader(shader);
472 unsigned TestPlugin::loadProgram(const string& vertexSource, const string& fragmentSource)
474 unsigned vertexShader = loadShader(GL_VERTEX_SHADER, vertexSource);
475 unsigned fragmentShader = loadShader(GL_FRAGMENT_SHADER, fragmentSource);
476 unsigned program = m_context->createProgram();
477 if (vertexShader && fragmentShader && program) {
478 m_context->attachShader(program, vertexShader);
479 m_context->attachShader(program, fragmentShader);
480 m_context->linkProgram(program);
483 m_context->getProgramiv(program, GL_LINK_STATUS, &linked);
485 m_context->deleteProgram(program);
490 m_context->deleteShader(vertexShader);
492 m_context->deleteShader(fragmentShader);
497 bool TestPlugin::handleInputEvent(const WebInputEvent& event, WebCursorInfo& info)
499 const char* eventName = 0;
500 switch (event.type) {
501 case WebInputEvent::Undefined: eventName = "unknown"; break;
503 case WebInputEvent::MouseDown: eventName = "MouseDown"; break;
504 case WebInputEvent::MouseUp: eventName = "MouseUp"; break;
505 case WebInputEvent::MouseMove: eventName = "MouseMove"; break;
506 case WebInputEvent::MouseEnter: eventName = "MouseEnter"; break;
507 case WebInputEvent::MouseLeave: eventName = "MouseLeave"; break;
508 case WebInputEvent::ContextMenu: eventName = "ContextMenu"; break;
510 case WebInputEvent::MouseWheel: eventName = "MouseWheel"; break;
512 case WebInputEvent::RawKeyDown: eventName = "RawKeyDown"; break;
513 case WebInputEvent::KeyDown: eventName = "KeyDown"; break;
514 case WebInputEvent::KeyUp: eventName = "KeyUp"; break;
515 case WebInputEvent::Char: eventName = "Char"; break;
517 case WebInputEvent::GestureScrollBegin: eventName = "GestureScrollBegin"; break;
518 case WebInputEvent::GestureScrollEnd: eventName = "GestureScrollEnd"; break;
519 case WebInputEvent::GestureScrollUpdateWithoutPropagation:
520 case WebInputEvent::GestureScrollUpdate: eventName = "GestureScrollUpdate"; break;
521 case WebInputEvent::GestureFlingStart: eventName = "GestureFlingStart"; break;
522 case WebInputEvent::GestureFlingCancel: eventName = "GestureFlingCancel"; break;
523 case WebInputEvent::GestureTap: eventName = "GestureTap"; break;
524 case WebInputEvent::GestureTapUnconfirmed:
525 eventName = "GestureTapUnconfirmed"; break;
526 case WebInputEvent::GestureTapDown: eventName = "GestureTapDown"; break;
527 case WebInputEvent::GestureShowPress: eventName = "GestureShowPress"; break;
528 case WebInputEvent::GestureTapCancel: eventName = "GestureTapCancel"; break;
529 case WebInputEvent::GestureDoubleTap: eventName = "GestureDoubleTap"; break;
530 case WebInputEvent::GestureTwoFingerTap: eventName = "GestureTwoFingerTap"; break;
531 case WebInputEvent::GestureLongPress: eventName = "GestureLongPress"; break;
532 case WebInputEvent::GestureLongTap: eventName = "GestureLongTap"; break;
533 case WebInputEvent::GesturePinchBegin: eventName = "GesturePinchBegin"; break;
534 case WebInputEvent::GesturePinchEnd: eventName = "GesturePinchEnd"; break;
535 case WebInputEvent::GesturePinchUpdate: eventName = "GesturePinchUpdate"; break;
537 case WebInputEvent::TouchStart: eventName = "TouchStart"; break;
538 case WebInputEvent::TouchMove: eventName = "TouchMove"; break;
539 case WebInputEvent::TouchEnd: eventName = "TouchEnd"; break;
540 case WebInputEvent::TouchCancel: eventName = "TouchCancel"; break;
543 m_delegate->printMessage(std::string("Plugin received event: ") + (eventName ? eventName : "unknown") + "\n");
544 if (m_printEventDetails)
545 printEventDetails(m_delegate, event);
546 if (m_printUserGestureStatus)
547 m_delegate->printMessage(std::string("* ") + (WebUserGestureIndicator::isProcessingUserGesture() ? "" : "not ") + "handling user gesture\n");
551 bool TestPlugin::handleDragStatusUpdate(WebDragStatus dragStatus, const WebDragData&, WebDragOperationsMask, const WebPoint& position, const WebPoint& screenPosition)
553 const char* dragStatusName = 0;
554 switch (dragStatus) {
555 case WebDragStatusEnter:
556 dragStatusName = "DragEnter";
558 case WebDragStatusOver:
559 dragStatusName = "DragOver";
561 case WebDragStatusLeave:
562 dragStatusName = "DragLeave";
564 case WebDragStatusDrop:
565 dragStatusName = "DragDrop";
567 case WebDragStatusUnknown:
568 BLINK_ASSERT_NOT_REACHED();
570 m_delegate->printMessage(std::string("Plugin received event: ") + dragStatusName + "\n");
574 TestPlugin* TestPlugin::create(WebFrame* frame, const WebPluginParams& params, WebTestDelegate* delegate)
576 return new TestPlugin(frame, params, delegate);
579 const WebString& TestPlugin::mimeType()
581 const CR_DEFINE_STATIC_LOCAL(WebString, kMimeType, ("application/x-webkit-test-webplugin"));