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 "LinkHighlight.h"
29 #include <gtest/gtest.h>
30 #include "FrameTestHelpers.h"
31 #include "URLTestHelpers.h"
33 #include "WebFrameClient.h"
34 #include "WebFrameImpl.h"
35 #include "WebInputEvent.h"
36 #include "WebInputEventConversion.h"
37 #include "WebViewClient.h"
38 #include "WebViewImpl.h"
39 #include "bindings/v8/ExceptionStatePlaceholder.h"
40 #include "core/dom/Node.h"
41 #include "core/frame/FrameView.h"
42 #include "core/page/TouchDisambiguation.h"
43 #include "platform/geometry/IntRect.h"
44 #include "public/platform/Platform.h"
45 #include "public/platform/WebContentLayer.h"
46 #include "public/platform/WebFloatPoint.h"
47 #include "public/platform/WebSize.h"
48 #include "public/platform/WebUnitTestSupport.h"
49 #include "wtf/PassOwnPtr.h"
52 using namespace WebKit;
53 using namespace WebCore;
57 TEST(LinkHighlightTest, verifyWebViewImplIntegration)
59 const std::string baseURL("http://www.test.com/");
60 const std::string fileName("test_touch_link_highlight.html");
62 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(baseURL.c_str()), WebString::fromUTF8("test_touch_link_highlight.html"));
63 FrameTestHelpers::WebViewHelper webViewHelper;
64 WebViewImpl* webViewImpl = webViewHelper.initializeAndLoad(baseURL + fileName, true);
67 webViewImpl->resize(WebSize(pageWidth, pageHeight));
68 webViewImpl->layout();
70 WebGestureEvent touchEvent;
71 touchEvent.type = WebInputEvent::GestureShowPress;
73 // The coordinates below are linked to absolute positions in the referenced .html file.
78 PlatformGestureEventBuilder platformEvent(webViewImpl->mainFrameImpl()->frameView(), touchEvent);
79 Node* touchNode = webViewImpl->bestTapNode(platformEvent);
80 ASSERT_TRUE(touchNode);
85 PlatformGestureEventBuilder platformEvent(webViewImpl->mainFrameImpl()->frameView(), touchEvent);
86 EXPECT_FALSE(webViewImpl->bestTapNode(platformEvent));
93 PlatformGestureEventBuilder platformEvent(webViewImpl->mainFrameImpl()->frameView(), touchEvent);
94 webViewImpl->enableTapHighlightAtPoint(platformEvent);
97 EXPECT_TRUE(webViewImpl->linkHighlight(0));
98 EXPECT_TRUE(webViewImpl->linkHighlight(0)->contentLayer());
99 EXPECT_TRUE(webViewImpl->linkHighlight(0)->clipLayer());
101 // Find a target inside a scrollable div
105 PlatformGestureEventBuilder platformEvent(webViewImpl->mainFrameImpl()->frameView(), touchEvent);
106 webViewImpl->enableTapHighlightAtPoint(platformEvent);
109 ASSERT_TRUE(webViewImpl->linkHighlight(0));
111 // Don't highlight if no "hand cursor"
112 touchEvent.y = 220; // An A-link with cross-hair cursor.
114 PlatformGestureEventBuilder platformEvent(webViewImpl->mainFrameImpl()->frameView(), touchEvent);
115 webViewImpl->enableTapHighlightAtPoint(platformEvent);
117 ASSERT_EQ(0U, webViewImpl->numLinkHighlights());
119 touchEvent.y = 260; // A text input box.
121 PlatformGestureEventBuilder platformEvent(webViewImpl->mainFrameImpl()->frameView(), touchEvent);
122 webViewImpl->enableTapHighlightAtPoint(platformEvent);
124 ASSERT_EQ(0U, webViewImpl->numLinkHighlights());
126 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
129 class FakeWebFrameClient : public WebFrameClient {
130 // To make the destructor public.
133 class FakeCompositingWebViewClient : public WebViewClient {
135 virtual ~FakeCompositingWebViewClient()
139 virtual void initializeLayerTreeView() OVERRIDE
141 m_layerTreeView = adoptPtr(Platform::current()->unitTestSupport()->createLayerTreeViewForTesting(WebUnitTestSupport::TestViewTypeUnitTest));
142 ASSERT(m_layerTreeView);
145 virtual WebLayerTreeView* layerTreeView() OVERRIDE
147 return m_layerTreeView.get();
150 FakeWebFrameClient m_fakeWebFrameClient;
153 OwnPtr<WebLayerTreeView> m_layerTreeView;
156 static WebViewClient* compositingWebViewClient()
158 DEFINE_STATIC_LOCAL(FakeCompositingWebViewClient, client, ());
162 TEST(LinkHighlightTest, resetDuringNodeRemoval)
164 const std::string baseURL("http://www.test.com/");
165 const std::string fileName("test_touch_link_highlight.html");
167 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(baseURL.c_str()), WebString::fromUTF8("test_touch_link_highlight.html"));
168 FrameTestHelpers::WebViewHelper webViewHelper;
169 WebViewImpl* webViewImpl = webViewHelper.initializeAndLoad(baseURL + fileName, true, 0, compositingWebViewClient());
172 int pageHeight = 480;
173 webViewImpl->resize(WebSize(pageWidth, pageHeight));
174 webViewImpl->layout();
176 WebGestureEvent touchEvent;
177 touchEvent.type = WebInputEvent::GestureShowPress;
181 PlatformGestureEventBuilder platformEvent(webViewImpl->mainFrameImpl()->frameView(), touchEvent);
182 Node* touchNode = webViewImpl->bestTapNode(platformEvent);
183 ASSERT_TRUE(touchNode);
185 webViewImpl->enableTapHighlightAtPoint(platformEvent);
186 ASSERT_TRUE(webViewImpl->linkHighlight(0));
188 GraphicsLayer* highlightLayer = webViewImpl->linkHighlight(0)->currentGraphicsLayerForTesting();
189 ASSERT_TRUE(highlightLayer);
190 EXPECT_TRUE(highlightLayer->linkHighlight(0));
192 touchNode->remove(IGNORE_EXCEPTION);
193 webViewImpl->layout();
194 ASSERT_EQ(0U, highlightLayer->numLinkHighlights());
196 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
199 TEST(LinkHighlightTest, multipleHighlights)
201 const std::string baseURL("http://www.test.com/");
202 const std::string fileName("test_touch_link_highlight.html");
204 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(baseURL.c_str()), WebString::fromUTF8("test_touch_link_highlight.html"));
205 FrameTestHelpers::WebViewHelper webViewHelper;
206 WebViewImpl* webViewImpl = webViewHelper.initializeAndLoad(baseURL + fileName, true, 0, compositingWebViewClient());
209 int pageHeight = 480;
210 webViewImpl->resize(WebSize(pageWidth, pageHeight));
211 webViewImpl->layout();
213 WebGestureEvent touchEvent;
216 touchEvent.data.tap.width = 30;
217 touchEvent.data.tap.height = 30;
219 Vector<IntRect> goodTargets;
220 Vector<Node*> highlightNodes;
221 IntRect boundingBox(touchEvent.x - touchEvent.data.tap.width / 2, touchEvent.y - touchEvent.data.tap.height / 2, touchEvent.data.tap.width, touchEvent.data.tap.height);
222 findGoodTouchTargets(boundingBox, webViewImpl->mainFrameImpl()->frame(), goodTargets, highlightNodes);
224 webViewImpl->enableTapHighlights(highlightNodes);
225 EXPECT_EQ(2U, webViewImpl->numLinkHighlights());
227 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();