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
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.
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.
27 #include "web/LinkHighlight.h"
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>
53 using namespace blink;
57 GestureEventWithHitTestResults getTargetedEvent(WebViewImpl* webViewImpl, WebGestureEvent& touchEvent)
59 PlatformGestureEventBuilder platformEvent(webViewImpl->mainFrameImpl()->frameView(), touchEvent);
60 return webViewImpl->page()->deprecatedLocalMainFrame()->eventHandler().targetGestureEvent(platformEvent, true);
63 TEST(LinkHighlightTest, verifyWebViewImplIntegration)
65 const std::string baseURL("http://www.test.com/");
66 const std::string fileName("test_touch_link_highlight.html");
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);
73 webViewImpl->resize(WebSize(pageWidth, pageHeight));
74 webViewImpl->layout();
76 WebGestureEvent touchEvent;
77 touchEvent.type = WebInputEvent::GestureShowPress;
79 // The coordinates below are linked to absolute positions in the referenced .html file.
83 ASSERT_TRUE(webViewImpl->bestTapNode(getTargetedEvent(webViewImpl, touchEvent)));
86 EXPECT_FALSE(webViewImpl->bestTapNode(getTargetedEvent(webViewImpl, touchEvent)));
90 webViewImpl->enableTapHighlightAtPoint(getTargetedEvent(webViewImpl, touchEvent));
92 EXPECT_TRUE(webViewImpl->linkHighlight(0));
93 EXPECT_TRUE(webViewImpl->linkHighlight(0)->contentLayer());
94 EXPECT_TRUE(webViewImpl->linkHighlight(0)->clipLayer());
96 // Find a target inside a scrollable div
98 webViewImpl->enableTapHighlightAtPoint(getTargetedEvent(webViewImpl, touchEvent));
99 ASSERT_TRUE(webViewImpl->linkHighlight(0));
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());
106 touchEvent.y = 260; // A text input box.
107 webViewImpl->enableTapHighlightAtPoint(getTargetedEvent(webViewImpl, touchEvent));
108 ASSERT_EQ(0U, webViewImpl->numLinkHighlights());
110 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
113 class FakeWebFrameClient : public WebFrameClient {
114 // To make the destructor public.
117 class FakeCompositingWebViewClient : public FrameTestHelpers::TestWebViewClient {
119 FakeWebFrameClient m_fakeWebFrameClient;
122 static WebViewClient* compositingWebViewClient()
124 DEFINE_STATIC_LOCAL(FakeCompositingWebViewClient, client, ());
128 TEST(LinkHighlightTest, resetDuringNodeRemoval)
130 const std::string baseURL("http://www.test.com/");
131 const std::string fileName("test_touch_link_highlight.html");
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());
138 int pageHeight = 480;
139 webViewImpl->resize(WebSize(pageWidth, pageHeight));
140 webViewImpl->layout();
142 WebGestureEvent touchEvent;
143 touchEvent.type = WebInputEvent::GestureShowPress;
147 GestureEventWithHitTestResults targetedEvent = getTargetedEvent(webViewImpl, touchEvent);
148 Node* touchNode = webViewImpl->bestTapNode(targetedEvent);
149 ASSERT_TRUE(touchNode);
151 webViewImpl->enableTapHighlightAtPoint(targetedEvent);
152 ASSERT_TRUE(webViewImpl->linkHighlight(0));
154 GraphicsLayer* highlightLayer = webViewImpl->linkHighlight(0)->currentGraphicsLayerForTesting();
155 ASSERT_TRUE(highlightLayer);
156 EXPECT_TRUE(highlightLayer->linkHighlight(0));
158 touchNode->remove(IGNORE_EXCEPTION);
159 webViewImpl->layout();
160 ASSERT_EQ(0U, highlightLayer->numLinkHighlights());
162 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
165 TEST(LinkHighlightTest, multipleHighlights)
167 const std::string baseURL("http://www.test.com/");
168 const std::string fileName("test_touch_link_highlight.html");
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());
175 int pageHeight = 480;
176 webViewImpl->resize(WebSize(pageWidth, pageHeight));
177 webViewImpl->layout();
179 WebGestureEvent touchEvent;
182 touchEvent.data.tap.width = 30;
183 touchEvent.data.tap.height = 30;
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);
190 webViewImpl->enableTapHighlights(highlightNodes);
191 EXPECT_EQ(2U, webViewImpl->numLinkHighlights());
193 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();