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 "public/web/WebPluginContainer.h"
33
34#include "core/dom/Element.h"
35#include "core/testing/URLTestHelpers.h"
36#include "public/platform/Platform.h"
37#include "public/platform/WebClipboard.h"
38#include "public/platform/WebThread.h"
39#include "public/platform/WebUnitTestSupport.h"
40#include "public/web/WebDocument.h"
41#include "public/web/WebElement.h"
42#include "public/web/WebFrame.h"
43#include "public/web/WebFrameClient.h"
44#include "public/web/WebPluginParams.h"
45#include "public/web/WebSettings.h"
46#include "public/web/WebView.h"
47#include "web/WebLocalFrameImpl.h"
48#include "web/WebPluginContainerImpl.h"
49#include "web/WebViewImpl.h"
50#include "web/tests/FakeWebPlugin.h"
51#include "web/tests/FrameTestHelpers.h"
52#include <gtest/gtest.h>
53
54using namespace blink;
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 FrameTestHelpers::TestWebFrameClient {
88    virtual WebPlugin* createPlugin(WebLocalFrame* 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    FrameTestHelpers::WebViewHelper webViewHelper;
106    WebView* webView = webViewHelper.initializeAndLoad(m_baseURL + "plugin_container.html", true, new TestPluginWebFrameClient());
107    ASSERT(webView);
108    webView->settings()->setPluginsEnabled(true);
109    webView->resize(WebSize(300, 300));
110    webView->layout();
111    FrameTestHelpers::runPendingTasks();
112
113    WebPluginContainer* pluginContainerOne = getWebPluginContainer(webView, WebString::fromUTF8("translated-plugin"));
114    ASSERT(pluginContainerOne);
115    WebPoint point1 = pluginContainerOne->windowToLocalPoint(WebPoint(10, 10));
116    ASSERT_EQ(0, point1.x);
117    ASSERT_EQ(0, point1.y);
118    WebPoint point2 = pluginContainerOne->windowToLocalPoint(WebPoint(100, 100));
119    ASSERT_EQ(90, point2.x);
120    ASSERT_EQ(90, point2.y);
121
122    WebPluginContainer* pluginContainerTwo = getWebPluginContainer(webView, WebString::fromUTF8("rotated-plugin"));
123    ASSERT(pluginContainerTwo);
124    WebPoint point3 = pluginContainerTwo->windowToLocalPoint(WebPoint(0, 10));
125    ASSERT_EQ(10, point3.x);
126    ASSERT_EQ(0, point3.y);
127    WebPoint point4 = pluginContainerTwo->windowToLocalPoint(WebPoint(-10, 10));
128    ASSERT_EQ(10, point4.x);
129    ASSERT_EQ(10, point4.y);
130}
131
132TEST_F(WebPluginContainerTest, LocalToWindowPointTest)
133{
134    URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL.c_str()), WebString::fromUTF8("plugin_container.html"));
135    FrameTestHelpers::WebViewHelper webViewHelper;
136    WebView* webView = webViewHelper.initializeAndLoad(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
162// Verifies executing the command 'Copy' results in copying to the clipboard.
163TEST_F(WebPluginContainerTest, Copy)
164{
165    URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL.c_str()), WebString::fromUTF8("plugin_container.html"));
166    FrameTestHelpers::WebViewHelper webViewHelper;
167    WebView* webView = webViewHelper.initializeAndLoad(m_baseURL + "plugin_container.html", true, new TestPluginWebFrameClient());
168    ASSERT(webView);
169    webView->settings()->setPluginsEnabled(true);
170    webView->resize(WebSize(300, 300));
171    webView->layout();
172    FrameTestHelpers::runPendingTasks();
173
174    WebElement pluginContainerOneElement = webView->mainFrame()->document().getElementById(WebString::fromUTF8("translated-plugin"));
175    EXPECT_TRUE(webView->mainFrame()->executeCommand("Copy",  pluginContainerOneElement));
176    EXPECT_EQ(WebString("x"), Platform::current()->clipboard()->readPlainText(WebClipboard::Buffer()));
177}
178
179}
180