2 * Copyright (C) 2013 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.
33 #include "FrameTestHelpers.h"
34 #include "RuntimeEnabledFeatures.h"
35 #include "URLTestHelpers.h"
36 #include "WebDocument.h"
38 #include "WebInputEvent.h"
40 #include "WebViewClient.h"
41 #include "WebViewImpl.h"
42 #include "core/dom/ClientRect.h"
43 #include "core/dom/ClientRectList.h"
44 #include "core/dom/Document.h"
45 #include "core/dom/Element.h"
46 #include "core/dom/shadow/ShadowRoot.h"
47 #include "core/frame/Frame.h"
48 #include "core/frame/FrameView.h"
49 #include "core/page/EventHandler.h"
50 #include "core/rendering/HitTestResult.h"
51 #include "core/rendering/RenderTreeAsText.h"
52 #include "public/platform/Platform.h"
53 #include "public/platform/WebUnitTestSupport.h"
54 #include "public/web/WebHitTestResult.h"
55 #include "public/web/WebTouchAction.h"
56 #include "public/web/WebWidgetClient.h"
58 #include <gtest/gtest.h>
60 using namespace blink;
61 using blink::FrameTestHelpers::runPendingTasks;
65 class TouchActionTrackingWebViewClient : public WebViewClient {
67 TouchActionTrackingWebViewClient() :
69 m_action(WebTouchActionAuto)
73 // WebWidgetClient methods
74 virtual void setTouchAction(WebTouchAction touchAction)
77 m_action = touchAction;
84 m_action = WebTouchActionAuto;
87 int touchActionSetCount()
89 return m_actionSetCount;
92 WebTouchAction lastTouchAction()
99 WebTouchAction m_action;
102 const int kfakeTouchId = 7;
104 class TouchActionTest : public testing::Test {
107 : m_baseURL("http://www.test.com/")
109 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL), "touch-action-tests.css");
110 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL), "touch-action-tests.js");
111 WebCore::RuntimeEnabledFeatures::setCSSTouchActionEnabled(true);
114 virtual void TearDown()
116 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
120 void runTouchActionTest(std::string file);
121 void runShadowDOMTest(std::string file);
122 void sendTouchEvent(WebView*, WebInputEvent::Type, WebCore::IntPoint clientPoint);
123 WebView* setupTest(std::string file, TouchActionTrackingWebViewClient&);
124 void runTestOnTree(WebCore::ContainerNode* root, WebView*, TouchActionTrackingWebViewClient&);
126 std::string m_baseURL;
127 FrameTestHelpers::WebViewHelper m_webViewHelper;
130 void TouchActionTest::runTouchActionTest(std::string file)
132 TouchActionTrackingWebViewClient client;
134 WebView* webView = setupTest(file, client);
136 RefPtr<WebCore::Document> document = static_cast<PassRefPtr<WebCore::Document> >(webView->mainFrame()->document());
137 runTestOnTree(document.get(), webView, client);
139 m_webViewHelper.reset(); // Explicitly reset to break dependency on locally scoped client.
142 void TouchActionTest::runShadowDOMTest(std::string file)
144 TouchActionTrackingWebViewClient client;
146 WebView* webView = setupTest(file, client);
148 WebCore::TrackExceptionState es;
149 RefPtr<WebCore::Document> document = static_cast<PassRefPtr<WebCore::Document> >(webView->mainFrame()->document());
150 RefPtr<WebCore::NodeList> hostNodes = document->querySelectorAll("[shadow-host]", es);
151 ASSERT_FALSE(es.hadException());
152 ASSERT_GE(hostNodes->length(), 1u);
154 for (unsigned index = 0; index < hostNodes->length(); index++) {
155 WebCore::ShadowRoot* shadowRoot = WebCore::toElement(hostNodes->item(index))->shadowRoot();
156 runTestOnTree(shadowRoot, webView, client);
159 // Projections show up in the main document.
160 runTestOnTree(document.get(), webView, client);
162 m_webViewHelper.reset(); // Explicitly reset to break dependency on locally scoped client.
165 WebView* TouchActionTest::setupTest(std::string file, TouchActionTrackingWebViewClient& client)
167 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL), WebString::fromUTF8(file));
168 // Note that JavaScript must be enabled for shadow DOM tests.
169 WebView* webView = m_webViewHelper.initializeAndLoad(m_baseURL + file, true, 0, &client);
171 // Set size to enable hit testing, and avoid line wrapping for consistency with browser.
172 webView->resize(WebSize(700, 1000));
174 // Scroll to verify the code properly transforms windows to client co-ords.
175 const int kScrollOffset = 100;
176 RefPtr<WebCore::Document> document = static_cast<PassRefPtr<WebCore::Document> >(webView->mainFrame()->document());
177 document->frame()->view()->setScrollOffset(WebCore::IntPoint(0, kScrollOffset));
182 void TouchActionTest::runTestOnTree(WebCore::ContainerNode* root, WebView* webView, TouchActionTrackingWebViewClient& client)
184 // Find all elements to test the touch-action of in the document.
185 WebCore::TrackExceptionState es;
186 RefPtr<WebCore::NodeList> nodes = root->querySelectorAll("[expected-action]", es);
187 ASSERT_FALSE(es.hadException());
189 for (unsigned index = 0; index < nodes->length(); index++) {
190 WebCore::Element* element = toElement(nodes->item(index));
191 element->scrollIntoViewIfNeeded();
192 ASSERT_TRUE(nodes->item(index)->isElementNode());
194 std::string failureContext("Test case: ");
195 if (element->hasID()) {
196 failureContext.append(element->getIdAttribute().ascii().data());
197 } else if (element->firstChild()) {
198 failureContext.append("\"");
199 failureContext.append(element->firstChild()->textContent(false).stripWhiteSpace().ascii().data());
200 failureContext.append("\"");
202 failureContext += "<missing ID>";
205 // Run each test three times at different positions in the element.
206 // Note that we don't want the bounding box because our tests sometimes have elements with
207 // multiple border boxes with other elements in between. Use the first border box (which
208 // we can easily visualize in a browser for debugging).
209 RefPtr<WebCore::ClientRectList> rects = element->getClientRects();
210 ASSERT_GE(rects->length(), 0u) << failureContext;
211 RefPtr<WebCore::ClientRect> r = rects->item(0);
212 WebCore::FloatRect clientFloatRect = WebCore::FloatRect(r->left(), r->top(), r->width(), r->height());
213 WebCore::IntRect clientRect = enclosedIntRect(clientFloatRect);
214 for (int locIdx = 0; locIdx < 3; locIdx++) {
215 WebCore::IntPoint clientPoint;
216 std::stringstream contextStream;
217 contextStream << failureContext << " (";
220 clientPoint = clientRect.center();
221 contextStream << "center";
224 clientPoint = clientRect.location();
225 contextStream << "top-left";
228 clientPoint = clientRect.maxXMaxYCorner();
229 clientPoint.move(-1, -1);
230 contextStream << "bottom-right";
233 FAIL() << "Invalid location index.";
235 contextStream << "=" << clientPoint.x() << "," << clientPoint.y() << ").";
236 std::string failureContextPos = contextStream.str();
238 WebCore::Frame* frame = root->document().frame();
239 WebCore::FrameView* frameView = frame->view();
240 WebCore::IntRect visibleRect = frameView->windowClipRect();
241 ASSERT_TRUE(visibleRect.contains(clientPoint)) << failureContextPos
242 << " Test point not contained in visible area: " << visibleRect.x() << "," << visibleRect.y()
243 << "-" << visibleRect.maxX() << "," << visibleRect.maxY();
245 // First validate that a hit test at this point will really hit the element
246 // we intended. This is the easiest way for a test to be broken, but has nothing really
247 // to do with touch action.
248 // Note that we can't use WebView's hit test API because it doesn't look into shadow DOM.
249 WebCore::IntPoint docPoint(frameView->windowToContents(clientPoint));
250 WebCore::HitTestResult result = frame->eventHandler().hitTestResultAtPoint(docPoint, WebCore::HitTestRequest::ReadOnly | WebCore::HitTestRequest::Active);
251 ASSERT_EQ(element, result.innerElement()) << "Unexpected hit test result " << failureContextPos
252 << " Got element: \"" << result.innerElement()->outerHTML().stripWhiteSpace().left(80).ascii().data() << "\""
253 << std::endl << "Document render tree:" << std::endl << externalRepresentation(root->document().frame()).utf8().data();
255 // Now send the touch event and check any touch action result.
256 sendTouchEvent(webView, WebInputEvent::TouchStart, clientPoint);
258 AtomicString expectedAction = element->getAttribute("expected-action");
259 if (expectedAction == "auto") {
260 // Auto is the default - no action set.
261 EXPECT_EQ(0, client.touchActionSetCount()) << failureContextPos;
262 EXPECT_EQ(WebTouchActionAuto, client.lastTouchAction()) << failureContextPos;
264 // Should have received exactly one touch action.
265 EXPECT_EQ(1, client.touchActionSetCount()) << failureContextPos;
266 if (client.touchActionSetCount()) {
267 if (expectedAction == "none") {
268 EXPECT_EQ(WebTouchActionNone, client.lastTouchAction()) << failureContextPos;
269 } else if (expectedAction == "pan-x") {
270 EXPECT_EQ(WebTouchActionPanX, client.lastTouchAction()) << failureContextPos;
271 } else if (expectedAction == "pan-y") {
272 EXPECT_EQ(WebTouchActionPanY, client.lastTouchAction()) << failureContextPos;
273 } else if (expectedAction == "pan-x-y") {
274 EXPECT_EQ((WebTouchActionPanX | WebTouchActionPanY), client.lastTouchAction()) << failureContextPos;
276 FAIL() << "Unrecognized expected-action \"" << expectedAction.ascii().data()
277 << "\" " << failureContextPos;
282 // Reset webview touch state.
284 sendTouchEvent(webView, WebInputEvent::TouchCancel, clientPoint);
285 EXPECT_EQ(0, client.touchActionSetCount());
289 void TouchActionTest::sendTouchEvent(WebView* webView, WebInputEvent::Type type, WebCore::IntPoint clientPoint)
291 ASSERT_TRUE(type == WebInputEvent::TouchStart || type == WebInputEvent::TouchCancel);
293 WebTouchEvent webTouchEvent;
294 webTouchEvent.type = type;
295 webTouchEvent.touchesLength = 1;
296 webTouchEvent.touches[0].state = (type == WebInputEvent::TouchStart ?
297 WebTouchPoint::StatePressed :
298 WebTouchPoint::StateCancelled);
299 webTouchEvent.touches[0].id = kfakeTouchId;
300 webTouchEvent.touches[0].screenPosition.x = clientPoint.x();
301 webTouchEvent.touches[0].screenPosition.y = clientPoint.y();
302 webTouchEvent.touches[0].position.x = clientPoint.x();
303 webTouchEvent.touches[0].position.y = clientPoint.y();
304 webTouchEvent.touches[0].radiusX = 10;
305 webTouchEvent.touches[0].radiusY = 10;
307 webView->handleInputEvent(webTouchEvent);
311 TEST_F(TouchActionTest, Simple)
313 runTouchActionTest("touch-action-simple.html");
316 TEST_F(TouchActionTest, Overflow)
318 runTouchActionTest("touch-action-overflow.html");
321 TEST_F(TouchActionTest, ShadowDOM)
323 runShadowDOMTest("touch-action-shadow-dom.html");
326 TEST_F(TouchActionTest, Pan)
328 runTouchActionTest("touch-action-pan.html");