1 /* 2 * Copyright (C) 2011 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #ifndef TestPlugin_h 27 #define TestPlugin_h 28 29 #include "public/platform/WebExternalTextureLayer.h" 30 #include "public/platform/WebExternalTextureLayerClient.h" 31 #include "public/platform/WebExternalTextureMailbox.h" 32 #include "public/web/WebPlugin.h" 33 #include "public/web/WebPluginContainer.h" 34 #include <memory> 35 #include <string> 36 37 namespace WebTestRunner { 38 39 class WebTestDelegate; 40 41 // A fake implemention of WebKit::WebPlugin for testing purposes. 42 // 43 // It uses WebGraphicsContext3D to paint a scene consisiting of a primitive 44 // over a background. The primitive and background can be customized using 45 // the following plugin parameters: 46 // primitive: none (default), triangle. 47 // background-color: black (default), red, green, blue. 48 // primitive-color: black (default), red, green, blue. 49 // opacity: [0.0 - 1.0]. Default is 1.0. 50 // 51 // Whether the plugin accepts touch events or not can be customized using the 52 // 'accepts-touch' plugin parameter (defaults to false). 53 class TestPlugin : public WebKit::WebPlugin, public WebKit::WebExternalTextureLayerClient { 54 public: 55 static TestPlugin* create(WebKit::WebFrame*, const WebKit::WebPluginParams&, WebTestDelegate*); 56 virtual ~TestPlugin(); 57 58 static const WebKit::WebString& mimeType(); 59 60 // WebPlugin methods: 61 virtual bool initialize(WebKit::WebPluginContainer*); 62 virtual void destroy(); 63 virtual NPObject* scriptableObject() { return 0; } 64 virtual bool canProcessDrag() const { return m_canProcessDrag; } 65 virtual void paint(WebKit::WebCanvas*, const WebKit::WebRect&) { } 66 virtual void updateGeometry(const WebKit::WebRect& frameRect, const WebKit::WebRect& clipRect, const WebKit::WebVector<WebKit::WebRect>& cutOutsRects, bool isVisible); 67 virtual void updateFocus(bool) { } 68 virtual void updateVisibility(bool) { } 69 virtual bool acceptsInputEvents() { return true; } 70 virtual bool handleInputEvent(const WebKit::WebInputEvent&, WebKit::WebCursorInfo&); 71 virtual bool handleDragStatusUpdate(WebKit::WebDragStatus, const WebKit::WebDragData&, WebKit::WebDragOperationsMask, const WebKit::WebPoint& position, const WebKit::WebPoint& screenPosition); 72 virtual void didReceiveResponse(const WebKit::WebURLResponse&) { } 73 virtual void didReceiveData(const char* data, int dataLength) { } 74 virtual void didFinishLoading() { } 75 virtual void didFailLoading(const WebKit::WebURLError&) { } 76 virtual void didFinishLoadingFrameRequest(const WebKit::WebURL&, void* notifyData) { } 77 virtual void didFailLoadingFrameRequest(const WebKit::WebURL&, void* notifyData, const WebKit::WebURLError&) { } 78 virtual bool isPlaceholder() { return false; } 79 80 // WebExternalTextureLayerClient methods: 81 virtual WebKit::WebGraphicsContext3D* context() { return 0; } 82 virtual bool prepareMailbox(WebKit::WebExternalTextureMailbox*, WebKit::WebExternalBitmap*); 83 virtual void mailboxReleased(const WebKit::WebExternalTextureMailbox&); 84 85 private: 86 TestPlugin(WebKit::WebFrame*, const WebKit::WebPluginParams&, WebTestDelegate*); 87 88 enum Primitive { 89 PrimitiveNone, 90 PrimitiveTriangle 91 }; 92 93 struct Scene { 94 Primitive primitive; 95 unsigned backgroundColor[3]; 96 unsigned primitiveColor[3]; 97 float opacity; 98 99 unsigned vbo; 100 unsigned program; 101 int colorLocation; 102 int positionLocation; 103 104 Scene() 105 : primitive(PrimitiveNone) 106 , opacity(1.0f) // Fully opaque. 107 , vbo(0) 108 , program(0) 109 , colorLocation(-1) 110 , positionLocation(-1) 111 { 112 backgroundColor[0] = backgroundColor[1] = backgroundColor[2] = 0; 113 primitiveColor[0] = primitiveColor[1] = primitiveColor[2] = 0; 114 } 115 }; 116 117 // Functions for parsing plugin parameters. 118 Primitive parsePrimitive(const WebKit::WebString&); 119 void parseColor(const WebKit::WebString&, unsigned color[3]); 120 float parseOpacity(const WebKit::WebString&); 121 bool parseBoolean(const WebKit::WebString&); 122 123 // Functions for loading and drawing scene. 124 bool initScene(); 125 void drawScene(); 126 void destroyScene(); 127 bool initProgram(); 128 bool initPrimitive(); 129 void drawPrimitive(); 130 unsigned loadShader(unsigned type, const std::string& source); 131 unsigned loadProgram(const std::string& vertexSource, const std::string& fragmentSource); 132 133 WebKit::WebFrame* m_frame; 134 WebTestDelegate* m_delegate; 135 WebKit::WebPluginContainer* m_container; 136 137 WebKit::WebRect m_rect; 138 WebKit::WebGraphicsContext3D* m_context; 139 unsigned m_colorTexture; 140 WebKit::WebExternalTextureMailbox m_mailbox; 141 bool m_mailboxChanged; 142 unsigned m_framebuffer; 143 Scene m_scene; 144 std::auto_ptr<WebKit::WebExternalTextureLayer> m_layer; 145 146 WebKit::WebPluginContainer::TouchEventRequestType m_touchEventRequest; 147 // Requests touch events from the WebPluginContainerImpl multiple times to tickle webkit.org/b/108381 148 bool m_reRequestTouchEvents; 149 bool m_printEventDetails; 150 bool m_printUserGestureStatus; 151 bool m_canProcessDrag; 152 }; 153 154 } 155 156 #endif // TestPlugin_h 157