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 "platform/PlatformEvent.h"
37 #include "platform/PlatformKeyboardEvent.h"
38 #include "platform/testing/URLTestHelpers.h"
39 #include "platform/testing/UnitTestHelpers.h"
40 #include "public/platform/Platform.h"
41 #include "public/platform/WebClipboard.h"
42 #include "public/platform/WebThread.h"
43 #include "public/platform/WebUnitTestSupport.h"
44 #include "public/web/WebDocument.h"
45 #include "public/web/WebElement.h"
46 #include "public/web/WebFrame.h"
47 #include "public/web/WebFrameClient.h"
48 #include "public/web/WebPluginParams.h"
49 #include "public/web/WebPrintParams.h"
50 #include "public/web/WebSettings.h"
51 #include "public/web/WebView.h"
52 #include "third_party/skia/include/core/SkPictureRecorder.h"
53 #include "web/WebLocalFrameImpl.h"
54 #include "web/WebPluginContainerImpl.h"
55 #include "web/WebViewImpl.h"
56 #include "web/tests/FakeWebPlugin.h"
57 #include "web/tests/FrameTestHelpers.h"
58 #include <gtest/gtest.h>
60 using blink::testing::runPendingTasks
;
64 class WebPluginContainerTest
: public ::testing::Test
{
66 WebPluginContainerTest()
67 : m_baseURL("http://www.test.com/")
71 void TearDown() override
73 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
77 std::string m_baseURL
;
80 class TestPluginWebFrameClient
;
82 // Subclass of FakeWebPlugin that has a selection of 'x' as plain text and 'y' as markup text.
83 class TestPlugin
: public FakeWebPlugin
{
85 TestPlugin(WebFrame
* frame
, const WebPluginParams
& params
, TestPluginWebFrameClient
* testClient
)
86 : FakeWebPlugin(frame
, params
)
88 m_testClient
= testClient
;
91 bool hasSelection() const override
{ return true; }
92 WebString
selectionAsText() const override
{ return WebString("x"); }
93 WebString
selectionAsMarkup() const override
{ return WebString("y"); }
94 bool supportsPaginatedPrint() override
{ return true; }
95 int printBegin(const WebPrintParams
& printParams
) override
{ return 1; }
96 void printPage(int pageNumber
, WebCanvas
*) override
;
98 TestPluginWebFrameClient
* m_testClient
;
101 class TestPluginWebFrameClient
: public FrameTestHelpers::TestWebFrameClient
{
102 WebPlugin
* createPlugin(WebLocalFrame
* frame
, const WebPluginParams
& params
) override
104 if (params
.mimeType
== WebString::fromUTF8("application/x-webkit-test-webplugin"))
105 return new TestPlugin(frame
, params
, this);
106 if (params
.mimeType
== WebString::fromUTF8("application/pdf"))
107 return new TestPlugin(frame
, params
, this);
108 return WebFrameClient::createPlugin(frame
, params
);
112 void onPrintPage() { m_printedPage
= true; }
113 bool printedAtLeastOnePage() { return m_printedPage
; }
116 bool m_printedPage
= false;
119 void TestPlugin::printPage(int pageNumber
, WebCanvas
* canvas
)
121 ASSERT(m_testClient
);
122 m_testClient
->onPrintPage();
125 WebPluginContainer
* getWebPluginContainer(WebView
* webView
, const WebString
& id
)
127 WebElement element
= webView
->mainFrame()->document().getElementById(id
);
128 return element
.pluginContainer();
131 TEST_F(WebPluginContainerTest
, WindowToLocalPointTest
)
133 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL
.c_str()), WebString::fromUTF8("plugin_container.html"));
134 TestPluginWebFrameClient pluginWebFrameClient
; // Must outlive webViewHelper.
135 FrameTestHelpers::WebViewHelper webViewHelper
;
136 WebView
* webView
= webViewHelper
.initializeAndLoad(m_baseURL
+ "plugin_container.html", true, &pluginWebFrameClient
);
138 webView
->settings()->setPluginsEnabled(true);
139 webView
->resize(WebSize(300, 300));
143 WebPluginContainer
* pluginContainerOne
= getWebPluginContainer(webView
, WebString::fromUTF8("translated-plugin"));
144 ASSERT(pluginContainerOne
);
145 WebPoint point1
= pluginContainerOne
->rootFrameToLocalPoint(WebPoint(10, 10));
146 ASSERT_EQ(0, point1
.x
);
147 ASSERT_EQ(0, point1
.y
);
148 WebPoint point2
= pluginContainerOne
->rootFrameToLocalPoint(WebPoint(100, 100));
149 ASSERT_EQ(90, point2
.x
);
150 ASSERT_EQ(90, point2
.y
);
152 WebPluginContainer
* pluginContainerTwo
= getWebPluginContainer(webView
, WebString::fromUTF8("rotated-plugin"));
153 ASSERT(pluginContainerTwo
);
154 WebPoint point3
= pluginContainerTwo
->rootFrameToLocalPoint(WebPoint(0, 10));
155 ASSERT_EQ(10, point3
.x
);
156 ASSERT_EQ(0, point3
.y
);
157 WebPoint point4
= pluginContainerTwo
->rootFrameToLocalPoint(WebPoint(-10, 10));
158 ASSERT_EQ(10, point4
.x
);
159 ASSERT_EQ(10, point4
.y
);
162 TEST_F(WebPluginContainerTest
, PrintOnePage
)
164 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL
.c_str()), WebString::fromUTF8("test.pdf"), WebString::fromUTF8("application/pdf"));
166 TestPluginWebFrameClient pluginWebFrameClient
; // Must outlive webViewHelper.
167 FrameTestHelpers::WebViewHelper webViewHelper
;
168 WebView
* webView
= webViewHelper
.initializeAndLoad(m_baseURL
+ "test.pdf", true, &pluginWebFrameClient
);
172 WebFrame
* frame
= webView
->mainFrame();
174 WebPrintParams printParams
;
175 printParams
.printContentArea
.width
= 500;
176 printParams
.printContentArea
.height
= 500;
178 frame
->printBegin(printParams
);
179 SkPictureRecorder recorder
;
180 frame
->printPage(0, recorder
.beginRecording(IntRect()));
182 ASSERT(pluginWebFrameClient
.printedAtLeastOnePage());
185 TEST_F(WebPluginContainerTest
, PrintAllPages
)
187 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL
.c_str()), WebString::fromUTF8("test.pdf"), WebString::fromUTF8("application/pdf"));
189 TestPluginWebFrameClient pluginWebFrameClient
; // Must outlive webViewHelper.
190 FrameTestHelpers::WebViewHelper webViewHelper
;
191 WebView
* webView
= webViewHelper
.initializeAndLoad(m_baseURL
+ "test.pdf", true, &pluginWebFrameClient
);
195 WebFrame
* frame
= webView
->mainFrame();
197 WebPrintParams printParams
;
198 printParams
.printContentArea
.width
= 500;
199 printParams
.printContentArea
.height
= 500;
201 frame
->printBegin(printParams
);
202 SkPictureRecorder recorder
;
203 frame
->printPagesWithBoundaries(recorder
.beginRecording(IntRect()), WebSize());
205 ASSERT(pluginWebFrameClient
.printedAtLeastOnePage());
208 TEST_F(WebPluginContainerTest
, LocalToWindowPointTest
)
210 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL
.c_str()), WebString::fromUTF8("plugin_container.html"));
211 TestPluginWebFrameClient pluginWebFrameClient
; // Must outlive webViewHelper.
212 FrameTestHelpers::WebViewHelper webViewHelper
;
213 WebView
* webView
= webViewHelper
.initializeAndLoad(m_baseURL
+ "plugin_container.html", true, &pluginWebFrameClient
);
215 webView
->settings()->setPluginsEnabled(true);
216 webView
->resize(WebSize(300, 300));
220 WebPluginContainer
* pluginContainerOne
= getWebPluginContainer(webView
, WebString::fromUTF8("translated-plugin"));
221 ASSERT(pluginContainerOne
);
222 WebPoint point1
= pluginContainerOne
->localToRootFramePoint(WebPoint(0, 0));
223 ASSERT_EQ(10, point1
.x
);
224 ASSERT_EQ(10, point1
.y
);
225 WebPoint point2
= pluginContainerOne
->localToRootFramePoint(WebPoint(90, 90));
226 ASSERT_EQ(100, point2
.x
);
227 ASSERT_EQ(100, point2
.y
);
229 WebPluginContainer
* pluginContainerTwo
= getWebPluginContainer(webView
, WebString::fromUTF8("rotated-plugin"));
230 ASSERT(pluginContainerTwo
);
231 WebPoint point3
= pluginContainerTwo
->localToRootFramePoint(WebPoint(10, 0));
232 ASSERT_EQ(0, point3
.x
);
233 ASSERT_EQ(10, point3
.y
);
234 WebPoint point4
= pluginContainerTwo
->localToRootFramePoint(WebPoint(10, 10));
235 ASSERT_EQ(-10, point4
.x
);
236 ASSERT_EQ(10, point4
.y
);
239 // Verifies executing the command 'Copy' results in copying to the clipboard.
240 TEST_F(WebPluginContainerTest
, Copy
)
242 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL
.c_str()), WebString::fromUTF8("plugin_container.html"));
243 TestPluginWebFrameClient pluginWebFrameClient
; // Must outlive webViewHelper.
244 FrameTestHelpers::WebViewHelper webViewHelper
;
245 WebView
* webView
= webViewHelper
.initializeAndLoad(m_baseURL
+ "plugin_container.html", true, &pluginWebFrameClient
);
247 webView
->settings()->setPluginsEnabled(true);
248 webView
->resize(WebSize(300, 300));
252 WebElement pluginContainerOneElement
= webView
->mainFrame()->document().getElementById(WebString::fromUTF8("translated-plugin"));
253 EXPECT_TRUE(webView
->mainFrame()->executeCommand("Copy", pluginContainerOneElement
));
254 EXPECT_EQ(WebString("x"), Platform::current()->clipboard()->readPlainText(WebClipboard::Buffer()));
257 // Verifies |Ctrl-C| and |Ctrl-Insert| keyboard events, results in copying to
259 TEST_F(WebPluginContainerTest
, CopyInsertKeyboardEventsTest
)
261 URLTestHelpers::registerMockedURLFromBaseURL(
262 WebString::fromUTF8(m_baseURL
.c_str()),
263 WebString::fromUTF8("plugin_container.html"));
264 TestPluginWebFrameClient pluginWebFrameClient
; // Must outlive webViewHelper.
265 FrameTestHelpers::WebViewHelper webViewHelper
;
266 WebView
* webView
= webViewHelper
.initializeAndLoad(m_baseURL
+ "plugin_container.html", true, &pluginWebFrameClient
);
268 webView
->settings()->setPluginsEnabled(true);
269 webView
->resize(WebSize(300, 300));
273 WebElement pluginContainerOneElement
= webView
->mainFrame()->document().getElementById(WebString::fromUTF8("translated-plugin"));
274 PlatformEvent::Modifiers modifierKey
= PlatformEvent::CtrlKey
;
276 modifierKey
= PlatformEvent::MetaKey
;
278 PlatformKeyboardEvent
platformKeyboardEventC(PlatformEvent::RawKeyDown
, "", "", "67", "", "", 67, 0, false, modifierKey
, 0.0);
279 RefPtrWillBeRawPtr
<KeyboardEvent
> keyEventC
= KeyboardEvent::create(platformKeyboardEventC
, 0);
280 toWebPluginContainerImpl(pluginContainerOneElement
.pluginContainer())->handleEvent(keyEventC
.get());
281 EXPECT_EQ(WebString("x"), Platform::current()->clipboard()->readPlainText(WebClipboard::Buffer()));
283 // Clearing |Clipboard::Buffer()|.
284 Platform::current()->clipboard()->writePlainText(WebString(""));
285 EXPECT_EQ(WebString(""), Platform::current()->clipboard()->readPlainText(WebClipboard::Buffer()));
287 PlatformKeyboardEvent
platformKeyboardEventInsert(PlatformEvent::RawKeyDown
, "", "", "45", "", "", 45, 0, false, modifierKey
, 0.0);
288 RefPtrWillBeRawPtr
<KeyboardEvent
> keyEventInsert
= KeyboardEvent::create(platformKeyboardEventInsert
, 0);
289 toWebPluginContainerImpl(pluginContainerOneElement
.pluginContainer())->handleEvent(keyEventInsert
.get());
290 EXPECT_EQ(WebString("x"), Platform::current()->clipboard()->readPlainText(WebClipboard::Buffer()));
293 // A class to facilitate testing that events are correctly received by plugins.
294 class EventTestPlugin
: public FakeWebPlugin
{
296 EventTestPlugin(WebFrame
* frame
, const WebPluginParams
& params
)
297 : FakeWebPlugin(frame
, params
)
298 , m_lastEventType(WebInputEvent::Undefined
)
302 bool handleInputEvent(const WebInputEvent
& event
, WebCursorInfo
&) override
304 m_lastEventType
= event
.type
;
307 WebInputEvent::Type
getLastInputEventType() {return m_lastEventType
; }
310 WebInputEvent::Type m_lastEventType
;
313 class EventTestPluginWebFrameClient
: public FrameTestHelpers::TestWebFrameClient
{
314 WebPlugin
* createPlugin(WebLocalFrame
* frame
, const WebPluginParams
& params
) override
316 if (params
.mimeType
== WebString::fromUTF8("application/x-webkit-test-webplugin"))
317 return new EventTestPlugin(frame
, params
);
318 return WebFrameClient::createPlugin(frame
, params
);
322 TEST_F(WebPluginContainerTest
, GestureLongPressReachesPlugin
)
324 URLTestHelpers::registerMockedURLFromBaseURL(
325 WebString::fromUTF8(m_baseURL
.c_str()),
326 WebString::fromUTF8("plugin_container.html"));
327 EventTestPluginWebFrameClient pluginWebFrameClient
; // Must outlive webViewHelper.
328 FrameTestHelpers::WebViewHelper webViewHelper
;
329 WebView
* webView
= webViewHelper
.initializeAndLoad(m_baseURL
+ "plugin_container.html", true, &pluginWebFrameClient
);
331 webView
->settings()->setPluginsEnabled(true);
332 webView
->resize(WebSize(300, 300));
336 WebElement pluginContainerOneElement
= webView
->mainFrame()->document().getElementById(WebString::fromUTF8("translated-plugin"));
337 WebPlugin
* plugin
= static_cast<WebPluginContainerImpl
*>(pluginContainerOneElement
.pluginContainer())->plugin();
338 EventTestPlugin
* testPlugin
= static_cast<EventTestPlugin
*>(plugin
);
340 WebGestureEvent event
;
341 event
.type
= WebInputEvent::GestureLongPress
;
343 // First, send an event that doesn't hit the plugin to verify that the
344 // plugin doesn't receive it.
348 webView
->handleInputEvent(event
);
351 EXPECT_EQ(WebInputEvent::Undefined
, testPlugin
->getLastInputEventType());
353 // Next, send an event that does hit the plugin, and verify it does receive it.
354 WebRect rect
= pluginContainerOneElement
.boundsInViewportSpace();
355 event
.x
= rect
.x
+ rect
.width
/ 2;
356 event
.y
= rect
.y
+ rect
.height
/ 2;
358 webView
->handleInputEvent(event
);
361 EXPECT_EQ(WebInputEvent::GestureLongPress
, testPlugin
->getLastInputEventType());
364 // Verify that isRectTopmost returns false when the document is detached.
365 TEST_F(WebPluginContainerTest
, IsRectTopmostTest
)
367 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL
.c_str()), WebString::fromUTF8("plugin_container.html"));
368 TestPluginWebFrameClient pluginWebFrameClient
; // Must outlive webViewHelper.
369 FrameTestHelpers::WebViewHelper webViewHelper
;
370 WebView
* webView
= webViewHelper
.initializeAndLoad(m_baseURL
+ "plugin_container.html", true, &pluginWebFrameClient
);
372 webView
->settings()->setPluginsEnabled(true);
373 webView
->resize(WebSize(300, 300));
377 RefPtrWillBeRawPtr
<WebPluginContainerImpl
> pluginContainerImpl
=
378 toWebPluginContainerImpl(getWebPluginContainer(webView
, WebString::fromUTF8("translated-plugin")));
379 pluginContainerImpl
->setFrameRect(IntRect(0, 0, 300, 300));
381 WebRect rect
= pluginContainerImpl
->element().boundsInViewportSpace();
382 EXPECT_TRUE(pluginContainerImpl
->isRectTopmost(rect
));
384 // Cause the plugin's frame to be detached.
385 webViewHelper
.reset();
387 EXPECT_FALSE(pluginContainerImpl
->isRectTopmost(rect
));
390 TEST_F(WebPluginContainerTest
, TopmostAfterDetachTest
)
392 static WebRect
topmostRect(10, 10, 40, 40);
394 // Plugin that checks isRectTopmost in destroy().
395 class TopmostPlugin
: public FakeWebPlugin
{
397 TopmostPlugin(WebFrame
* frame
, const WebPluginParams
& params
)
398 : FakeWebPlugin(frame
, params
) {}
402 return container()->isRectTopmost(topmostRect
);
405 void destroy() override
407 // In destroy, isRectTopmost is no longer valid.
408 EXPECT_FALSE(container()->isRectTopmost(topmostRect
));
409 FakeWebPlugin::destroy();
413 class TopmostPluginWebFrameClient
: public FrameTestHelpers::TestWebFrameClient
{
414 WebPlugin
* createPlugin(WebLocalFrame
* frame
, const WebPluginParams
& params
) override
416 return new TopmostPlugin(frame
, params
);
420 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL
.c_str()), WebString::fromUTF8("plugin_container.html"));
421 TopmostPluginWebFrameClient pluginWebFrameClient
; // Must outlive webViewHelper.
422 FrameTestHelpers::WebViewHelper webViewHelper
;
423 WebView
* webView
= webViewHelper
.initializeAndLoad(m_baseURL
+ "plugin_container.html", true, &pluginWebFrameClient
);
425 webView
->settings()->setPluginsEnabled(true);
426 webView
->resize(WebSize(300, 300));
430 RefPtrWillBeRawPtr
<WebPluginContainerImpl
> pluginContainerImpl
=
431 toWebPluginContainerImpl(getWebPluginContainer(webView
, WebString::fromUTF8("translated-plugin")));
432 pluginContainerImpl
->setFrameRect(IntRect(0, 0, 300, 300));
434 EXPECT_TRUE(pluginContainerImpl
->isRectTopmost(topmostRect
));
436 TopmostPlugin
* testPlugin
= static_cast<TopmostPlugin
*>(pluginContainerImpl
->plugin());
437 EXPECT_TRUE(testPlugin
->isRectTopmost());
439 // Cause the plugin's frame to be detached.
440 webViewHelper
.reset();
442 EXPECT_FALSE(pluginContainerImpl
->isRectTopmost(topmostRect
));