Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / tests / WebPluginContainerTest.cpp
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/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>
56
57 using namespace blink;
58
59 namespace {
60
61 class WebPluginContainerTest : public testing::Test {
62 public:
63     WebPluginContainerTest()
64         : m_baseURL("http://www.test.com/")
65     {
66     }
67
68     virtual void TearDown()
69     {
70         Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
71     }
72
73 protected:
74     std::string m_baseURL;
75 };
76
77 // Subclass of FakeWebPlugin that has a selection of 'x' as plain text and 'y' as markup text.
78 class TestPlugin : public FakeWebPlugin {
79 public:
80     TestPlugin(WebFrame* frame, const WebPluginParams& params)
81         : FakeWebPlugin(frame, params)
82     {
83     }
84
85     virtual bool hasSelection() const { return true; }
86     virtual WebString selectionAsText() const { return WebString("x"); }
87     virtual WebString selectionAsMarkup() const { return WebString("y"); }
88 };
89
90 class TestPluginWebFrameClient : public FrameTestHelpers::TestWebFrameClient {
91     virtual WebPlugin* createPlugin(WebLocalFrame* frame, const WebPluginParams& params) override
92     {
93         if (params.mimeType == WebString::fromUTF8("application/x-webkit-test-webplugin"))
94             return new TestPlugin(frame, params);
95         return WebFrameClient::createPlugin(frame, params);
96     }
97 };
98
99 WebPluginContainer* getWebPluginContainer(WebView* webView, const WebString& id)
100 {
101     WebElement element = webView->mainFrame()->document().getElementById(id);
102     return element.pluginContainer();
103 }
104
105 TEST_F(WebPluginContainerTest, WindowToLocalPointTest)
106 {
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());
110     ASSERT(webView);
111     webView->settings()->setPluginsEnabled(true);
112     webView->resize(WebSize(300, 300));
113     webView->layout();
114     FrameTestHelpers::runPendingTasks();
115
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);
124
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);
133 }
134
135 TEST_F(WebPluginContainerTest, LocalToWindowPointTest)
136 {
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());
140     ASSERT(webView);
141     webView->settings()->setPluginsEnabled(true);
142     webView->resize(WebSize(300, 300));
143     webView->layout();
144     FrameTestHelpers::runPendingTasks();
145
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);
154
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);
163 }
164
165 // Verifies executing the command 'Copy' results in copying to the clipboard.
166 TEST_F(WebPluginContainerTest, Copy)
167 {
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());
171     ASSERT(webView);
172     webView->settings()->setPluginsEnabled(true);
173     webView->resize(WebSize(300, 300));
174     webView->layout();
175     FrameTestHelpers::runPendingTasks();
176
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()));
180 }
181
182 // Verifies |Ctrl-C| and |Ctrl-Insert| keyboard events, results in copying to
183 // the clipboard.
184 TEST_F(WebPluginContainerTest, CopyInsertKeyboardEventsTest)
185 {
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());
191     ASSERT(webView);
192     webView->settings()->setPluginsEnabled(true);
193     webView->resize(WebSize(300, 300));
194     webView->layout();
195     FrameTestHelpers::runPendingTasks();
196
197     WebElement pluginContainerOneElement = webView->mainFrame()->document().getElementById(WebString::fromUTF8("translated-plugin"));
198     PlatformEvent::Modifiers modifierKey = PlatformEvent::CtrlKey;
199 #if OS(MACOSX)
200     modifierKey = PlatformEvent::MetaKey;
201 #endif
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()));
206
207     // Clearing |Clipboard::Buffer()|.
208     Platform::current()->clipboard()->writePlainText(WebString(""));
209     EXPECT_EQ(WebString(""), Platform::current()->clipboard()->readPlainText(WebClipboard::Buffer()));
210
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()));
215 }
216 }