1/* 2 * Copyright (C) 2012 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 are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#include "config.h" 32#include "WebPluginContainer.h" 33 34#include <gtest/gtest.h> 35#include "FakeWebPlugin.h" 36#include "FrameTestHelpers.h" 37#include "URLTestHelpers.h" 38#include "WebDocument.h" 39#include "WebElement.h" 40#include "WebFrame.h" 41#include "WebFrameClient.h" 42#include "WebFrameImpl.h" 43#include "WebPluginContainerImpl.h" 44#include "WebPluginParams.h" 45#include "WebSettings.h" 46#include "WebView.h" 47#include "WebViewImpl.h" 48#include "core/dom/Element.h" 49#include "public/platform/Platform.h" 50#include "public/platform/WebClipboard.h" 51#include "public/platform/WebThread.h" 52#include "public/platform/WebUnitTestSupport.h" 53 54using namespace WebKit; 55 56namespace { 57 58class WebPluginContainerTest : public testing::Test { 59public: 60 WebPluginContainerTest() 61 : m_baseURL("http://www.test.com/") 62 { 63 } 64 65 virtual void TearDown() 66 { 67 Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); 68 } 69 70protected: 71 std::string m_baseURL; 72}; 73 74// Subclass of FakeWebPlugin that has a selection of 'x' as plain text and 'y' as markup text. 75class TestPlugin : public FakeWebPlugin { 76public: 77 TestPlugin(WebFrame* frame, const WebPluginParams& params) 78 : FakeWebPlugin(frame, params) 79 { 80 } 81 82 virtual bool hasSelection() const { return true; } 83 virtual WebString selectionAsText() const { return WebString("x"); } 84 virtual WebString selectionAsMarkup() const { return WebString("y"); } 85}; 86 87class TestPluginWebFrameClient : public WebFrameClient { 88 virtual WebPlugin* createPlugin(WebFrame* frame, const WebPluginParams& params) OVERRIDE 89 { 90 if (params.mimeType == WebString::fromUTF8("application/x-webkit-test-webplugin")) 91 return new TestPlugin(frame, params); 92 return WebFrameClient::createPlugin(frame, params); 93 } 94}; 95 96WebPluginContainer* getWebPluginContainer(WebView* webView, const WebString& id) 97{ 98 WebElement element = webView->mainFrame()->document().getElementById(id); 99 return element.pluginContainer(); 100} 101 102TEST_F(WebPluginContainerTest, WindowToLocalPointTest) 103{ 104 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL.c_str()), WebString::fromUTF8("plugin_container.html")); 105 WebView* webView = FrameTestHelpers::createWebViewAndLoad(m_baseURL + "plugin_container.html", true, new TestPluginWebFrameClient()); 106 ASSERT(webView); 107 webView->settings()->setPluginsEnabled(true); 108 webView->resize(WebSize(300, 300)); 109 webView->layout(); 110 FrameTestHelpers::runPendingTasks(); 111 112 WebPluginContainer* pluginContainerOne = getWebPluginContainer(webView, WebString::fromUTF8("translated-plugin")); 113 ASSERT(pluginContainerOne); 114 WebPoint point1 = pluginContainerOne->windowToLocalPoint(WebPoint(10, 10)); 115 ASSERT_EQ(0, point1.x); 116 ASSERT_EQ(0, point1.y); 117 WebPoint point2 = pluginContainerOne->windowToLocalPoint(WebPoint(100, 100)); 118 ASSERT_EQ(90, point2.x); 119 ASSERT_EQ(90, point2.y); 120 121 WebPluginContainer* pluginContainerTwo = getWebPluginContainer(webView, WebString::fromUTF8("rotated-plugin")); 122 ASSERT(pluginContainerTwo); 123 WebPoint point3 = pluginContainerTwo->windowToLocalPoint(WebPoint(0, 10)); 124 ASSERT_EQ(10, point3.x); 125 ASSERT_EQ(0, point3.y); 126 WebPoint point4 = pluginContainerTwo->windowToLocalPoint(WebPoint(-10, 10)); 127 ASSERT_EQ(10, point4.x); 128 ASSERT_EQ(10, point4.y); 129 130 webView->close(); 131} 132 133TEST_F(WebPluginContainerTest, LocalToWindowPointTest) 134{ 135 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL.c_str()), WebString::fromUTF8("plugin_container.html")); 136 WebView* webView = FrameTestHelpers::createWebViewAndLoad(m_baseURL + "plugin_container.html", true, new TestPluginWebFrameClient()); 137 ASSERT(webView); 138 webView->settings()->setPluginsEnabled(true); 139 webView->resize(WebSize(300, 300)); 140 webView->layout(); 141 FrameTestHelpers::runPendingTasks(); 142 143 WebPluginContainer* pluginContainerOne = getWebPluginContainer(webView, WebString::fromUTF8("translated-plugin")); 144 ASSERT(pluginContainerOne); 145 WebPoint point1 = pluginContainerOne->localToWindowPoint(WebPoint(0, 0)); 146 ASSERT_EQ(10, point1.x); 147 ASSERT_EQ(10, point1.y); 148 WebPoint point2 = pluginContainerOne->localToWindowPoint(WebPoint(90, 90)); 149 ASSERT_EQ(100, point2.x); 150 ASSERT_EQ(100, point2.y); 151 152 WebPluginContainer* pluginContainerTwo = getWebPluginContainer(webView, WebString::fromUTF8("rotated-plugin")); 153 ASSERT(pluginContainerTwo); 154 WebPoint point3 = pluginContainerTwo->localToWindowPoint(WebPoint(10, 0)); 155 ASSERT_EQ(0, point3.x); 156 ASSERT_EQ(10, point3.y); 157 WebPoint point4 = pluginContainerTwo->localToWindowPoint(WebPoint(10, 10)); 158 ASSERT_EQ(-10, point4.x); 159 ASSERT_EQ(10, point4.y); 160 161 webView->close(); 162} 163 164// Verifies executing the command 'Copy' results in copying to the clipboard. 165TEST_F(WebPluginContainerTest, Copy) 166{ 167 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL.c_str()), WebString::fromUTF8("plugin_container.html")); 168 WebView* webView = FrameTestHelpers::createWebViewAndLoad(m_baseURL + "plugin_container.html", true, new TestPluginWebFrameClient()); 169 ASSERT(webView); 170 webView->settings()->setPluginsEnabled(true); 171 webView->resize(WebSize(300, 300)); 172 webView->layout(); 173 FrameTestHelpers::runPendingTasks(); 174 175 WebElement pluginContainerOneElement = webView->mainFrame()->document().getElementById(WebString::fromUTF8("translated-plugin")); 176 EXPECT_TRUE(webView->mainFrame()->executeCommand("Copy", pluginContainerOneElement)); 177 EXPECT_EQ(WebString("x"), Platform::current()->clipboard()->readPlainText(WebClipboard::Buffer())); 178 179 webView->close(); 180} 181 182} 183