Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / tests / LinkHighlightTest.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
6  * are met:
7  * 1.  Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include "config.h"
26
27 #include "web/LinkHighlight.h"
28
29 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
30 #include "core/dom/Node.h"
31 #include "core/frame/FrameView.h"
32 #include "core/page/EventHandler.h"
33 #include "core/page/Page.h"
34 #include "core/page/TouchDisambiguation.h"
35 #include "core/testing/URLTestHelpers.h"
36 #include "platform/geometry/IntRect.h"
37 #include "public/platform/Platform.h"
38 #include "public/platform/WebContentLayer.h"
39 #include "public/platform/WebFloatPoint.h"
40 #include "public/platform/WebSize.h"
41 #include "public/platform/WebUnitTestSupport.h"
42 #include "public/web/WebFrame.h"
43 #include "public/web/WebFrameClient.h"
44 #include "public/web/WebInputEvent.h"
45 #include "public/web/WebViewClient.h"
46 #include "web/WebInputEventConversion.h"
47 #include "web/WebLocalFrameImpl.h"
48 #include "web/WebViewImpl.h"
49 #include "web/tests/FrameTestHelpers.h"
50 #include "wtf/PassOwnPtr.h"
51 #include <gtest/gtest.h>
52
53 using namespace blink;
54
55 namespace {
56
57 GestureEventWithHitTestResults getTargetedEvent(WebViewImpl* webViewImpl, WebGestureEvent& touchEvent)
58 {
59     PlatformGestureEventBuilder platformEvent(webViewImpl->mainFrameImpl()->frameView(), touchEvent);
60     return webViewImpl->page()->deprecatedLocalMainFrame()->eventHandler().targetGestureEvent(platformEvent, true);
61 }
62
63 TEST(LinkHighlightTest, verifyWebViewImplIntegration)
64 {
65     const std::string baseURL("http://www.test.com/");
66     const std::string fileName("test_touch_link_highlight.html");
67
68     URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(baseURL.c_str()), WebString::fromUTF8("test_touch_link_highlight.html"));
69     FrameTestHelpers::WebViewHelper webViewHelper;
70     WebViewImpl* webViewImpl = webViewHelper.initializeAndLoad(baseURL + fileName, true);
71     int pageWidth = 640;
72     int pageHeight = 480;
73     webViewImpl->resize(WebSize(pageWidth, pageHeight));
74     webViewImpl->layout();
75
76     WebGestureEvent touchEvent;
77     touchEvent.type = WebInputEvent::GestureShowPress;
78
79     // The coordinates below are linked to absolute positions in the referenced .html file.
80     touchEvent.x = 20;
81     touchEvent.y = 20;
82
83     ASSERT_TRUE(webViewImpl->bestTapNode(getTargetedEvent(webViewImpl, touchEvent)));
84
85     touchEvent.y = 40;
86     EXPECT_FALSE(webViewImpl->bestTapNode(getTargetedEvent(webViewImpl, touchEvent)));
87
88     touchEvent.y = 20;
89     // Shouldn't crash.
90     webViewImpl->enableTapHighlightAtPoint(getTargetedEvent(webViewImpl, touchEvent));
91
92     EXPECT_TRUE(webViewImpl->linkHighlight(0));
93     EXPECT_TRUE(webViewImpl->linkHighlight(0)->contentLayer());
94     EXPECT_TRUE(webViewImpl->linkHighlight(0)->clipLayer());
95
96     // Find a target inside a scrollable div
97     touchEvent.y = 100;
98     webViewImpl->enableTapHighlightAtPoint(getTargetedEvent(webViewImpl, touchEvent));
99     ASSERT_TRUE(webViewImpl->linkHighlight(0));
100
101     // Don't highlight if no "hand cursor"
102     touchEvent.y = 220; // An A-link with cross-hair cursor.
103     webViewImpl->enableTapHighlightAtPoint(getTargetedEvent(webViewImpl, touchEvent));
104     ASSERT_EQ(0U, webViewImpl->numLinkHighlights());
105
106     touchEvent.y = 260; // A text input box.
107     webViewImpl->enableTapHighlightAtPoint(getTargetedEvent(webViewImpl, touchEvent));
108     ASSERT_EQ(0U, webViewImpl->numLinkHighlights());
109
110     Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
111 }
112
113 class FakeWebFrameClient : public WebFrameClient {
114     // To make the destructor public.
115 };
116
117 class FakeCompositingWebViewClient : public FrameTestHelpers::TestWebViewClient {
118 public:
119     FakeWebFrameClient m_fakeWebFrameClient;
120 };
121
122 static WebViewClient* compositingWebViewClient()
123 {
124     DEFINE_STATIC_LOCAL(FakeCompositingWebViewClient, client, ());
125     return &client;
126 }
127
128 TEST(LinkHighlightTest, resetDuringNodeRemoval)
129 {
130     const std::string baseURL("http://www.test.com/");
131     const std::string fileName("test_touch_link_highlight.html");
132
133     URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(baseURL.c_str()), WebString::fromUTF8("test_touch_link_highlight.html"));
134     FrameTestHelpers::WebViewHelper webViewHelper;
135     WebViewImpl* webViewImpl = webViewHelper.initializeAndLoad(baseURL + fileName, true, 0, compositingWebViewClient());
136
137     int pageWidth = 640;
138     int pageHeight = 480;
139     webViewImpl->resize(WebSize(pageWidth, pageHeight));
140     webViewImpl->layout();
141
142     WebGestureEvent touchEvent;
143     touchEvent.type = WebInputEvent::GestureShowPress;
144     touchEvent.x = 20;
145     touchEvent.y = 20;
146
147     GestureEventWithHitTestResults targetedEvent = getTargetedEvent(webViewImpl, touchEvent);
148     Node* touchNode = webViewImpl->bestTapNode(targetedEvent);
149     ASSERT_TRUE(touchNode);
150
151     webViewImpl->enableTapHighlightAtPoint(targetedEvent);
152     ASSERT_TRUE(webViewImpl->linkHighlight(0));
153
154     GraphicsLayer* highlightLayer = webViewImpl->linkHighlight(0)->currentGraphicsLayerForTesting();
155     ASSERT_TRUE(highlightLayer);
156     EXPECT_TRUE(highlightLayer->linkHighlight(0));
157
158     touchNode->remove(IGNORE_EXCEPTION);
159     webViewImpl->layout();
160     ASSERT_EQ(0U, highlightLayer->numLinkHighlights());
161
162     Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
163 }
164
165 TEST(LinkHighlightTest, multipleHighlights)
166 {
167     const std::string baseURL("http://www.test.com/");
168     const std::string fileName("test_touch_link_highlight.html");
169
170     URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(baseURL.c_str()), WebString::fromUTF8("test_touch_link_highlight.html"));
171     FrameTestHelpers::WebViewHelper webViewHelper;
172     WebViewImpl* webViewImpl = webViewHelper.initializeAndLoad(baseURL + fileName, true, 0, compositingWebViewClient());
173
174     int pageWidth = 640;
175     int pageHeight = 480;
176     webViewImpl->resize(WebSize(pageWidth, pageHeight));
177     webViewImpl->layout();
178
179     WebGestureEvent touchEvent;
180     touchEvent.x = 50;
181     touchEvent.y = 310;
182     touchEvent.data.tap.width = 30;
183     touchEvent.data.tap.height = 30;
184
185     Vector<IntRect> goodTargets;
186     WillBeHeapVector<RawPtrWillBeMember<Node> > highlightNodes;
187     IntRect boundingBox(touchEvent.x - touchEvent.data.tap.width / 2, touchEvent.y - touchEvent.data.tap.height / 2, touchEvent.data.tap.width, touchEvent.data.tap.height);
188     findGoodTouchTargets(boundingBox, webViewImpl->mainFrameImpl()->frame(), goodTargets, highlightNodes);
189
190     webViewImpl->enableTapHighlights(highlightNodes);
191     EXPECT_EQ(2U, webViewImpl->numLinkHighlights());
192
193     Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
194 }
195
196 } // namespace