2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "public/web/WebPluginContainer.h"
34 #include "core/dom/Element.h"
35 #include "core/events/KeyboardEvent.h"
36 #include "core/testing/URLTestHelpers.h"
37 #include "platform/PlatformEvent.h"
38 #include "platform/PlatformKeyboardEvent.h"
39 #include "public/platform/Platform.h"
40 #include "public/platform/WebClipboard.h"
41 #include "public/platform/WebThread.h"
42 #include "public/platform/WebUnitTestSupport.h"
43 #include "public/web/WebDocument.h"
44 #include "public/web/WebElement.h"
45 #include "public/web/WebFrame.h"
46 #include "public/web/WebFrameClient.h"
47 #include "public/web/WebPluginParams.h"
48 #include "public/web/WebSettings.h"
49 #include "public/web/WebView.h"
50 #include "web/WebLocalFrameImpl.h"
51 #include "web/WebPluginContainerImpl.h"
52 #include "web/WebViewImpl.h"
53 #include "web/tests/FakeWebPlugin.h"
54 #include "web/tests/FrameTestHelpers.h"
55 #include <gtest/gtest.h>
57 using namespace blink;
61 class WebPluginContainerTest : public testing::Test {
63 WebPluginContainerTest()
64 : m_baseURL("http://www.test.com/")
68 virtual void TearDown()
70 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
74 std::string m_baseURL;
77 // Subclass of FakeWebPlugin that has a selection of 'x' as plain text and 'y' as markup text.
78 class TestPlugin : public FakeWebPlugin {
80 TestPlugin(WebFrame* frame, const WebPluginParams& params)
81 : FakeWebPlugin(frame, params)
85 virtual bool hasSelection() const { return true; }
86 virtual WebString selectionAsText() const { return WebString("x"); }
87 virtual WebString selectionAsMarkup() const { return WebString("y"); }
90 class TestPluginWebFrameClient : public FrameTestHelpers::TestWebFrameClient {
91 virtual WebPlugin* createPlugin(WebLocalFrame* frame, const WebPluginParams& params) override
93 if (params.mimeType == WebString::fromUTF8("application/x-webkit-test-webplugin"))
94 return new TestPlugin(frame, params);
95 return WebFrameClient::createPlugin(frame, params);
99 WebPluginContainer* getWebPluginContainer(WebView* webView, const WebString& id)
101 WebElement element = webView->mainFrame()->document().getElementById(id);
102 return element.pluginContainer();
105 TEST_F(WebPluginContainerTest, WindowToLocalPointTest)
107 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL.c_str()), WebString::fromUTF8("plugin_container.html"));
108 FrameTestHelpers::WebViewHelper webViewHelper;
109 WebView* webView = webViewHelper.initializeAndLoad(m_baseURL + "plugin_container.html", true, new TestPluginWebFrameClient());
111 webView->settings()->setPluginsEnabled(true);
112 webView->resize(WebSize(300, 300));
114 FrameTestHelpers::runPendingTasks();
116 WebPluginContainer* pluginContainerOne = getWebPluginContainer(webView, WebString::fromUTF8("translated-plugin"));
117 ASSERT(pluginContainerOne);
118 WebPoint point1 = pluginContainerOne->windowToLocalPoint(WebPoint(10, 10));
119 ASSERT_EQ(0, point1.x);
120 ASSERT_EQ(0, point1.y);
121 WebPoint point2 = pluginContainerOne->windowToLocalPoint(WebPoint(100, 100));
122 ASSERT_EQ(90, point2.x);
123 ASSERT_EQ(90, point2.y);
125 WebPluginContainer* pluginContainerTwo = getWebPluginContainer(webView, WebString::fromUTF8("rotated-plugin"));
126 ASSERT(pluginContainerTwo);
127 WebPoint point3 = pluginContainerTwo->windowToLocalPoint(WebPoint(0, 10));
128 ASSERT_EQ(10, point3.x);
129 ASSERT_EQ(0, point3.y);
130 WebPoint point4 = pluginContainerTwo->windowToLocalPoint(WebPoint(-10, 10));
131 ASSERT_EQ(10, point4.x);
132 ASSERT_EQ(10, point4.y);
135 TEST_F(WebPluginContainerTest, LocalToWindowPointTest)
137 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL.c_str()), WebString::fromUTF8("plugin_container.html"));
138 FrameTestHelpers::WebViewHelper webViewHelper;
139 WebView* webView = webViewHelper.initializeAndLoad(m_baseURL + "plugin_container.html", true, new TestPluginWebFrameClient());
141 webView->settings()->setPluginsEnabled(true);
142 webView->resize(WebSize(300, 300));
144 FrameTestHelpers::runPendingTasks();
146 WebPluginContainer* pluginContainerOne = getWebPluginContainer(webView, WebString::fromUTF8("translated-plugin"));
147 ASSERT(pluginContainerOne);
148 WebPoint point1 = pluginContainerOne->localToWindowPoint(WebPoint(0, 0));
149 ASSERT_EQ(10, point1.x);
150 ASSERT_EQ(10, point1.y);
151 WebPoint point2 = pluginContainerOne->localToWindowPoint(WebPoint(90, 90));
152 ASSERT_EQ(100, point2.x);
153 ASSERT_EQ(100, point2.y);
155 WebPluginContainer* pluginContainerTwo = getWebPluginContainer(webView, WebString::fromUTF8("rotated-plugin"));
156 ASSERT(pluginContainerTwo);
157 WebPoint point3 = pluginContainerTwo->localToWindowPoint(WebPoint(10, 0));
158 ASSERT_EQ(0, point3.x);
159 ASSERT_EQ(10, point3.y);
160 WebPoint point4 = pluginContainerTwo->localToWindowPoint(WebPoint(10, 10));
161 ASSERT_EQ(-10, point4.x);
162 ASSERT_EQ(10, point4.y);
165 // Verifies executing the command 'Copy' results in copying to the clipboard.
166 TEST_F(WebPluginContainerTest, Copy)
168 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL.c_str()), WebString::fromUTF8("plugin_container.html"));
169 FrameTestHelpers::WebViewHelper webViewHelper;
170 WebView* webView = webViewHelper.initializeAndLoad(m_baseURL + "plugin_container.html", true, new TestPluginWebFrameClient());
172 webView->settings()->setPluginsEnabled(true);
173 webView->resize(WebSize(300, 300));
175 FrameTestHelpers::runPendingTasks();
177 WebElement pluginContainerOneElement = webView->mainFrame()->document().getElementById(WebString::fromUTF8("translated-plugin"));
178 EXPECT_TRUE(webView->mainFrame()->executeCommand("Copy", pluginContainerOneElement));
179 EXPECT_EQ(WebString("x"), Platform::current()->clipboard()->readPlainText(WebClipboard::Buffer()));
182 // Verifies |Ctrl-C| and |Ctrl-Insert| keyboard events, results in copying to
184 TEST_F(WebPluginContainerTest, CopyInsertKeyboardEventsTest)
186 URLTestHelpers::registerMockedURLFromBaseURL(
187 WebString::fromUTF8(m_baseURL.c_str()),
188 WebString::fromUTF8("plugin_container.html"));
189 FrameTestHelpers::WebViewHelper webViewHelper;
190 WebView* webView = webViewHelper.initializeAndLoad(m_baseURL + "plugin_container.html", true, new TestPluginWebFrameClient());
192 webView->settings()->setPluginsEnabled(true);
193 webView->resize(WebSize(300, 300));
195 FrameTestHelpers::runPendingTasks();
197 WebElement pluginContainerOneElement = webView->mainFrame()->document().getElementById(WebString::fromUTF8("translated-plugin"));
198 PlatformEvent::Modifiers modifierKey = PlatformEvent::CtrlKey;
200 modifierKey = PlatformEvent::MetaKey;
202 PlatformKeyboardEvent platformKeyboardEventC(PlatformEvent::RawKeyDown, "", "", "67", 67, 0, false, false, false, modifierKey, 0.0);
203 RefPtrWillBeRawPtr<KeyboardEvent> keyEventC = KeyboardEvent::create(platformKeyboardEventC, 0);
204 ((WebPluginContainerImpl*)(pluginContainerOneElement.pluginContainer()))->handleEvent(keyEventC.get());
205 EXPECT_EQ(WebString("x"), Platform::current()->clipboard()->readPlainText(WebClipboard::Buffer()));
207 // Clearing |Clipboard::Buffer()|.
208 Platform::current()->clipboard()->writePlainText(WebString(""));
209 EXPECT_EQ(WebString(""), Platform::current()->clipboard()->readPlainText(WebClipboard::Buffer()));
211 PlatformKeyboardEvent platformKeyboardEventInsert(PlatformEvent::RawKeyDown, "", "", "45", 45, 0, false, false, false, modifierKey, 0.0);
212 RefPtrWillBeRawPtr<KeyboardEvent> keyEventInsert = KeyboardEvent::create(platformKeyboardEventInsert, 0);
213 ((WebPluginContainerImpl*)(pluginContainerOneElement.pluginContainer()))->handleEvent(keyEventInsert.get());
214 EXPECT_EQ(WebString("x"), Platform::current()->clipboard()->readPlainText(WebClipboard::Buffer()));