1 /* 2 * Copyright (C) 2013 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 33 #include "core/dom/ClientRect.h" 34 #include "core/dom/ClientRectList.h" 35 #include "core/dom/Document.h" 36 #include "core/dom/Element.h" 37 #include "core/dom/StaticNodeList.h" 38 #include "core/dom/shadow/ShadowRoot.h" 39 #include "core/frame/FrameView.h" 40 #include "core/frame/LocalFrame.h" 41 #include "core/page/EventHandler.h" 42 #include "core/rendering/HitTestResult.h" 43 #include "core/rendering/RenderTreeAsText.h" 44 #include "platform/RuntimeEnabledFeatures.h" 45 #include "public/platform/Platform.h" 46 #include "public/platform/WebUnitTestSupport.h" 47 #include "public/web/WebDocument.h" 48 #include "public/web/WebFrame.h" 49 #include "public/web/WebHitTestResult.h" 50 #include "public/web/WebInputEvent.h" 51 #include "public/web/WebTouchAction.h" 52 #include "public/web/WebView.h" 53 #include "public/web/WebViewClient.h" 54 #include "public/web/WebWidgetClient.h" 55 #include "web/WebViewImpl.h" 56 #include "web/tests/FrameTestHelpers.h" 57 #include "web/tests/URLTestHelpers.h" 58 59 #include <gtest/gtest.h> 60 61 using namespace blink; 62 using blink::FrameTestHelpers::runPendingTasks; 63 64 namespace { 65 66 class TouchActionTrackingWebViewClient : public FrameTestHelpers::TestWebViewClient { 67 public: 68 TouchActionTrackingWebViewClient() : 69 m_actionSetCount(0), 70 m_action(WebTouchActionAuto) 71 { 72 } 73 74 // WebWidgetClient methods 75 virtual void setTouchAction(WebTouchAction touchAction) 76 { 77 m_actionSetCount++; 78 m_action = touchAction; 79 } 80 81 // Local methods 82 void reset() 83 { 84 m_actionSetCount = 0; 85 m_action = WebTouchActionAuto; 86 } 87 88 int touchActionSetCount() 89 { 90 return m_actionSetCount; 91 } 92 93 WebTouchAction lastTouchAction() 94 { 95 return m_action; 96 } 97 98 private: 99 int m_actionSetCount; 100 WebTouchAction m_action; 101 }; 102 103 const int kfakeTouchId = 7; 104 105 class TouchActionTest : public testing::Test { 106 public: 107 TouchActionTest() 108 : m_baseURL("http://www.test.com/") 109 { 110 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL), "touch-action-tests.css"); 111 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL), "touch-action-tests.js"); 112 WebCore::RuntimeEnabledFeatures::setCSSTouchActionEnabled(true); 113 } 114 115 virtual void TearDown() 116 { 117 Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); 118 } 119 120 protected: 121 void runTouchActionTest(std::string file); 122 void runShadowDOMTest(std::string file); 123 void sendTouchEvent(WebView*, WebInputEvent::Type, WebCore::IntPoint clientPoint); 124 WebView* setupTest(std::string file, TouchActionTrackingWebViewClient&); 125 void runTestOnTree(WebCore::ContainerNode* root, WebView*, TouchActionTrackingWebViewClient&); 126 127 std::string m_baseURL; 128 FrameTestHelpers::WebViewHelper m_webViewHelper; 129 }; 130 131 void TouchActionTest::runTouchActionTest(std::string file) 132 { 133 TouchActionTrackingWebViewClient client; 134 135 // runTouchActionTest() loads a document in a frame, setting up a 136 // nested message loop. Should any Oilpan GC happen while it is in 137 // effect, the implicit assumption that we're outside any event 138 // loop (=> there being no pointers on the stack needing scanning) 139 // when that GC strikes will no longer hold. 140 // 141 // To ensure that the references on the stack are also traced, we 142 // turn them into persistent, stack allocated references. This 143 // workaround is sufficient to handle this artificial test 144 // scenario. 145 WebView* webView = setupTest(file, client); 146 147 RefPtrWillBePersistent<WebCore::Document> document = static_cast<PassRefPtrWillBeRawPtr<WebCore::Document> >(webView->mainFrame()->document()); 148 runTestOnTree(document.get(), webView, client); 149 150 m_webViewHelper.reset(); // Explicitly reset to break dependency on locally scoped client. 151 } 152 153 void TouchActionTest::runShadowDOMTest(std::string file) 154 { 155 TouchActionTrackingWebViewClient client; 156 157 WebView* webView = setupTest(file, client); 158 159 WebCore::TrackExceptionState es; 160 161 // Oilpan: see runTouchActionTest() comment why these are persistent references. 162 RefPtrWillBePersistent<WebCore::Document> document = static_cast<PassRefPtrWillBeRawPtr<WebCore::Document> >(webView->mainFrame()->document()); 163 RefPtrWillBePersistent<WebCore::StaticNodeList> hostNodes = document->querySelectorAll("[shadow-host]", es); 164 ASSERT_FALSE(es.hadException()); 165 ASSERT_GE(hostNodes->length(), 1u); 166 167 for (unsigned index = 0; index < hostNodes->length(); index++) { 168 WebCore::ShadowRoot* shadowRoot = WebCore::toElement(hostNodes->item(index))->shadowRoot(); 169 runTestOnTree(shadowRoot, webView, client); 170 } 171 172 // Projections show up in the main document. 173 runTestOnTree(document.get(), webView, client); 174 175 m_webViewHelper.reset(); // Explicitly reset to break dependency on locally scoped client. 176 } 177 178 WebView* TouchActionTest::setupTest(std::string file, TouchActionTrackingWebViewClient& client) 179 { 180 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL), WebString::fromUTF8(file)); 181 // Note that JavaScript must be enabled for shadow DOM tests. 182 WebView* webView = m_webViewHelper.initializeAndLoad(m_baseURL + file, true, 0, &client); 183 184 // Lock page scale factor to avoid zooming out to contents size. 185 m_webViewHelper.webView()->setPageScaleFactorLimits(1, 1); 186 187 // Set size to enable hit testing, and avoid line wrapping for consistency with browser. 188 webView->resize(WebSize(800, 1200)); 189 190 // Scroll to verify the code properly transforms windows to client co-ords. 191 const int kScrollOffset = 100; 192 RefPtrWillBeRawPtr<WebCore::Document> document = static_cast<PassRefPtrWillBeRawPtr<WebCore::Document> >(webView->mainFrame()->document()); 193 document->frame()->view()->setScrollOffset(WebCore::IntPoint(0, kScrollOffset)); 194 195 return webView; 196 } 197 198 void TouchActionTest::runTestOnTree(WebCore::ContainerNode* root, WebView* webView, TouchActionTrackingWebViewClient& client) 199 { 200 // Find all elements to test the touch-action of in the document. 201 WebCore::TrackExceptionState es; 202 203 // Oilpan: see runTouchActionTest() comment why these are persistent references. 204 RefPtrWillBePersistent<WebCore::StaticNodeList> nodes = root->querySelectorAll("[expected-action]", es); 205 ASSERT_FALSE(es.hadException()); 206 207 for (unsigned index = 0; index < nodes->length(); index++) { 208 WebCore::Element* element = toElement(nodes->item(index)); 209 element->scrollIntoViewIfNeeded(); 210 ASSERT_TRUE(nodes->item(index)->isElementNode()); 211 212 std::string failureContext("Test case: "); 213 if (element->hasID()) { 214 failureContext.append(element->getIdAttribute().ascii().data()); 215 } else if (element->firstChild()) { 216 failureContext.append("\""); 217 failureContext.append(element->firstChild()->textContent(false).stripWhiteSpace().ascii().data()); 218 failureContext.append("\""); 219 } else { 220 failureContext += "<missing ID>"; 221 } 222 223 // Run each test three times at different positions in the element. 224 // Note that we don't want the bounding box because our tests sometimes have elements with 225 // multiple border boxes with other elements in between. Use the first border box (which 226 // we can easily visualize in a browser for debugging). 227 RefPtrWillBePersistent<WebCore::ClientRectList> rects = element->getClientRects(); 228 ASSERT_GE(rects->length(), 0u) << failureContext; 229 RefPtrWillBePersistent<WebCore::ClientRect> r = rects->item(0); 230 WebCore::FloatRect clientFloatRect = WebCore::FloatRect(r->left(), r->top(), r->width(), r->height()); 231 WebCore::IntRect clientRect = enclosedIntRect(clientFloatRect); 232 for (int locIdx = 0; locIdx < 3; locIdx++) { 233 WebCore::IntPoint clientPoint; 234 std::stringstream contextStream; 235 contextStream << failureContext << " ("; 236 switch (locIdx) { 237 case 0: 238 clientPoint = clientRect.center(); 239 contextStream << "center"; 240 break; 241 case 1: 242 clientPoint = clientRect.location(); 243 contextStream << "top-left"; 244 break; 245 case 2: 246 clientPoint = clientRect.maxXMaxYCorner(); 247 clientPoint.move(-1, -1); 248 contextStream << "bottom-right"; 249 break; 250 default: 251 FAIL() << "Invalid location index."; 252 } 253 contextStream << "=" << clientPoint.x() << "," << clientPoint.y() << ")."; 254 std::string failureContextPos = contextStream.str(); 255 256 WebCore::LocalFrame* frame = root->document().frame(); 257 WebCore::FrameView* frameView = frame->view(); 258 WebCore::IntRect visibleRect = frameView->windowClipRect(); 259 ASSERT_TRUE(visibleRect.contains(clientPoint)) << failureContextPos 260 << " Test point not contained in visible area: " << visibleRect.x() << "," << visibleRect.y() 261 << "-" << visibleRect.maxX() << "," << visibleRect.maxY(); 262 263 // First validate that a hit test at this point will really hit the element 264 // we intended. This is the easiest way for a test to be broken, but has nothing really 265 // to do with touch action. 266 // Note that we can't use WebView's hit test API because it doesn't look into shadow DOM. 267 WebCore::IntPoint docPoint(frameView->windowToContents(clientPoint)); 268 WebCore::HitTestResult result = frame->eventHandler().hitTestResultAtPoint(docPoint, WebCore::HitTestRequest::ReadOnly | WebCore::HitTestRequest::Active); 269 ASSERT_EQ(element, result.innerElement()) << "Unexpected hit test result " << failureContextPos 270 << " Got element: \"" << result.innerElement()->outerHTML().stripWhiteSpace().left(80).ascii().data() << "\"" 271 << std::endl << "Document render tree:" << std::endl << externalRepresentation(root->document().frame()).utf8().data(); 272 273 // Now send the touch event and check any touch action result. 274 sendTouchEvent(webView, WebInputEvent::TouchStart, clientPoint); 275 276 AtomicString expectedAction = element->getAttribute("expected-action"); 277 if (expectedAction == "auto") { 278 // Auto is the default - no action set. 279 EXPECT_EQ(0, client.touchActionSetCount()) << failureContextPos; 280 EXPECT_EQ(WebTouchActionAuto, client.lastTouchAction()) << failureContextPos; 281 } else { 282 // Should have received exactly one touch action. 283 EXPECT_EQ(1, client.touchActionSetCount()) << failureContextPos; 284 if (client.touchActionSetCount()) { 285 if (expectedAction == "none") { 286 EXPECT_EQ(WebTouchActionNone, client.lastTouchAction()) << failureContextPos; 287 } else if (expectedAction == "pan-x") { 288 EXPECT_EQ(WebTouchActionPanX, client.lastTouchAction()) << failureContextPos; 289 } else if (expectedAction == "pan-y") { 290 EXPECT_EQ(WebTouchActionPanY, client.lastTouchAction()) << failureContextPos; 291 } else if (expectedAction == "pan-x-y") { 292 EXPECT_EQ((WebTouchActionPanX | WebTouchActionPanY), client.lastTouchAction()) << failureContextPos; 293 } else if (expectedAction == "manipulation") { 294 EXPECT_EQ((WebTouchActionPanX | WebTouchActionPanY | WebTouchActionPinchZoom), client.lastTouchAction()) << failureContextPos; 295 } else { 296 FAIL() << "Unrecognized expected-action \"" << expectedAction.ascii().data() 297 << "\" " << failureContextPos; 298 } 299 } 300 } 301 302 // Reset webview touch state. 303 client.reset(); 304 sendTouchEvent(webView, WebInputEvent::TouchCancel, clientPoint); 305 EXPECT_EQ(0, client.touchActionSetCount()); 306 } 307 } 308 } 309 void TouchActionTest::sendTouchEvent(WebView* webView, WebInputEvent::Type type, WebCore::IntPoint clientPoint) 310 { 311 ASSERT_TRUE(type == WebInputEvent::TouchStart || type == WebInputEvent::TouchCancel); 312 313 WebTouchEvent webTouchEvent; 314 webTouchEvent.type = type; 315 if (type == WebInputEvent::TouchCancel) 316 webTouchEvent.cancelable = false; 317 webTouchEvent.touchesLength = 1; 318 webTouchEvent.touches[0].state = (type == WebInputEvent::TouchStart ? 319 WebTouchPoint::StatePressed : 320 WebTouchPoint::StateCancelled); 321 webTouchEvent.touches[0].id = kfakeTouchId; 322 webTouchEvent.touches[0].screenPosition.x = clientPoint.x(); 323 webTouchEvent.touches[0].screenPosition.y = clientPoint.y(); 324 webTouchEvent.touches[0].position.x = clientPoint.x(); 325 webTouchEvent.touches[0].position.y = clientPoint.y(); 326 webTouchEvent.touches[0].radiusX = 10; 327 webTouchEvent.touches[0].radiusY = 10; 328 329 webView->handleInputEvent(webTouchEvent); 330 runPendingTasks(); 331 } 332 333 TEST_F(TouchActionTest, Simple) 334 { 335 runTouchActionTest("touch-action-simple.html"); 336 } 337 338 TEST_F(TouchActionTest, Overflow) 339 { 340 runTouchActionTest("touch-action-overflow.html"); 341 } 342 343 TEST_F(TouchActionTest, ShadowDOM) 344 { 345 runShadowDOMTest("touch-action-shadow-dom.html"); 346 } 347 348 TEST_F(TouchActionTest, Pan) 349 { 350 runTouchActionTest("touch-action-pan.html"); 351 } 352 353 } 354