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 "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/input/EventHandler.h"
42 #include "core/layout/HitTestResult.h"
43 #include "core/layout/LayoutTreeAsText.h"
44 #include "platform/testing/URLTestHelpers.h"
45 #include "platform/testing/UnitTestHelpers.h"
46 #include "public/platform/Platform.h"
47 #include "public/platform/WebUnitTestSupport.h"
48 #include "public/web/WebDocument.h"
49 #include "public/web/WebFrame.h"
50 #include "public/web/WebHitTestResult.h"
51 #include "public/web/WebInputEvent.h"
52 #include "public/web/WebTouchAction.h"
53 #include "public/web/WebView.h"
54 #include "public/web/WebViewClient.h"
55 #include "public/web/WebWidgetClient.h"
56 #include "web/WebViewImpl.h"
57 #include "web/tests/FrameTestHelpers.h"
58 #include <gtest/gtest.h>
60 using blink::testing::runPendingTasks
;
64 class TouchActionTrackingWebViewClient
: public FrameTestHelpers::TestWebViewClient
{
66 TouchActionTrackingWebViewClient() :
68 m_action(WebTouchActionAuto
)
72 // WebWidgetClient methods
73 void setTouchAction(WebTouchAction touchAction
) override
76 m_action
= touchAction
;
83 m_action
= WebTouchActionAuto
;
86 int touchActionSetCount()
88 return m_actionSetCount
;
91 WebTouchAction
lastTouchAction()
98 WebTouchAction m_action
;
101 const int kfakeTouchId
= 7;
103 class TouchActionTest
: public ::testing::Test
{
106 : m_baseURL("http://www.test.com/")
108 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL
), "touch-action-tests.css");
109 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL
), "touch-action-tests.js");
112 void TearDown() override
114 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
118 void runTouchActionTest(std::string file
);
119 void runShadowDOMTest(std::string file
);
120 void sendTouchEvent(WebView
*, WebInputEvent::Type
, IntPoint clientPoint
);
121 WebView
* setupTest(std::string file
, TouchActionTrackingWebViewClient
&);
122 void runTestOnTree(ContainerNode
* root
, WebView
*, TouchActionTrackingWebViewClient
&);
124 std::string m_baseURL
;
125 FrameTestHelpers::WebViewHelper m_webViewHelper
;
128 void TouchActionTest::runTouchActionTest(std::string file
)
130 TouchActionTrackingWebViewClient client
;
132 // runTouchActionTest() loads a document in a frame, setting up a
133 // nested message loop. Should any Oilpan GC happen while it is in
134 // effect, the implicit assumption that we're outside any event
135 // loop (=> there being no pointers on the stack needing scanning)
136 // when that GC strikes will no longer hold.
138 // To ensure that the references on the stack are also traced, we
139 // turn them into persistent, stack allocated references. This
140 // workaround is sufficient to handle this artificial test
142 WebView
* webView
= setupTest(file
, client
);
144 RefPtrWillBePersistent
<Document
> document
= static_cast<PassRefPtrWillBeRawPtr
<Document
>>(webView
->mainFrame()->document());
145 runTestOnTree(document
.get(), webView
, client
);
147 m_webViewHelper
.reset(); // Explicitly reset to break dependency on locally scoped client.
150 void TouchActionTest::runShadowDOMTest(std::string file
)
152 TouchActionTrackingWebViewClient client
;
154 WebView
* webView
= setupTest(file
, client
);
156 TrackExceptionState es
;
158 // Oilpan: see runTouchActionTest() comment why these are persistent references.
159 RefPtrWillBePersistent
<Document
> document
= static_cast<PassRefPtrWillBeRawPtr
<Document
>>(webView
->mainFrame()->document());
160 RefPtrWillBePersistent
<StaticElementList
> hostNodes
= document
->querySelectorAll("[shadow-host]", es
);
161 ASSERT_FALSE(es
.hadException());
162 ASSERT_GE(hostNodes
->length(), 1u);
164 for (unsigned index
= 0; index
< hostNodes
->length(); index
++) {
165 ShadowRoot
* shadowRoot
= hostNodes
->item(index
)->openShadowRoot();
166 runTestOnTree(shadowRoot
, webView
, client
);
169 // Projections show up in the main document.
170 runTestOnTree(document
.get(), webView
, client
);
172 m_webViewHelper
.reset(); // Explicitly reset to break dependency on locally scoped client.
175 WebView
* TouchActionTest::setupTest(std::string file
, TouchActionTrackingWebViewClient
& client
)
177 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseURL
), WebString::fromUTF8(file
));
178 // Note that JavaScript must be enabled for shadow DOM tests.
179 WebView
* webView
= m_webViewHelper
.initializeAndLoad(m_baseURL
+ file
, true, 0, &client
);
181 // Set size to enable hit testing, and avoid line wrapping for consistency with browser.
182 webView
->resize(WebSize(800, 1200));
184 // Scroll to verify the code properly transforms windows to client co-ords.
185 const int kScrollOffset
= 100;
186 RefPtrWillBeRawPtr
<Document
> document
= static_cast<PassRefPtrWillBeRawPtr
<Document
>>(webView
->mainFrame()->document());
187 document
->frame()->view()->setScrollPosition(IntPoint(0, kScrollOffset
), ProgrammaticScroll
);
192 void TouchActionTest::runTestOnTree(ContainerNode
* root
, WebView
* webView
, TouchActionTrackingWebViewClient
& client
)
194 // Find all elements to test the touch-action of in the document.
195 TrackExceptionState es
;
197 // Oilpan: see runTouchActionTest() comment why these are persistent references.
198 RefPtrWillBePersistent
<StaticElementList
> elements
= root
->querySelectorAll("[expected-action]", es
);
199 ASSERT_FALSE(es
.hadException());
201 for (unsigned index
= 0; index
< elements
->length(); index
++) {
202 Element
* element
= elements
->item(index
);
203 element
->scrollIntoViewIfNeeded();
205 std::string
failureContext("Test case: ");
206 if (element
->hasID()) {
207 failureContext
.append(element
->getIdAttribute().ascii().data());
208 } else if (element
->firstChild()) {
209 failureContext
.append("\"");
210 failureContext
.append(element
->firstChild()->textContent(false).stripWhiteSpace().ascii().data());
211 failureContext
.append("\"");
213 failureContext
+= "<missing ID>";
216 // Run each test three times at different positions in the element.
217 // Note that we don't want the bounding box because our tests sometimes have elements with
218 // multiple border boxes with other elements in between. Use the first border box (which
219 // we can easily visualize in a browser for debugging).
220 Persistent
<ClientRectList
> rects
= element
->getClientRects();
221 ASSERT_GE(rects
->length(), 0u) << failureContext
;
222 Persistent
<ClientRect
> r
= rects
->item(0);
223 FloatRect clientFloatRect
= FloatRect(r
->left(), r
->top(), r
->width(), r
->height());
224 IntRect clientRect
= enclosedIntRect(clientFloatRect
);
225 for (int locIdx
= 0; locIdx
< 3; locIdx
++) {
226 IntPoint clientPoint
;
227 std::stringstream contextStream
;
228 contextStream
<< failureContext
<< " (";
231 clientPoint
= clientRect
.center();
232 contextStream
<< "center";
235 clientPoint
= clientRect
.location();
236 contextStream
<< "top-left";
239 clientPoint
= clientRect
.maxXMaxYCorner();
240 clientPoint
.move(-1, -1);
241 contextStream
<< "bottom-right";
244 FAIL() << "Invalid location index.";
246 contextStream
<< "=" << clientPoint
.x() << "," << clientPoint
.y() << ").";
247 std::string failureContextPos
= contextStream
.str();
249 LocalFrame
* frame
= root
->document().frame();
250 FrameView
* frameView
= frame
->view();
251 IntRect visibleRect
= frameView
->windowClipRect();
252 ASSERT_TRUE(visibleRect
.contains(clientPoint
)) << failureContextPos
253 << " Test point not contained in visible area: " << visibleRect
.x() << "," << visibleRect
.y()
254 << "-" << visibleRect
.maxX() << "," << visibleRect
.maxY();
256 // First validate that a hit test at this point will really hit the element
257 // we intended. This is the easiest way for a test to be broken, but has nothing really
258 // to do with touch action.
259 // Note that we can't use WebView's hit test API because it doesn't look into shadow DOM.
260 IntPoint
docPoint(frameView
->rootFrameToContents(clientPoint
));
261 HitTestResult result
= frame
->eventHandler().hitTestResultAtPoint(docPoint
, HitTestRequest::ReadOnly
| HitTestRequest::Active
);
262 ASSERT_EQ(element
, result
.innerElement()) << "Unexpected hit test result " << failureContextPos
263 << " Got element: \"" << result
.innerElement()->outerHTML().stripWhiteSpace().left(80).ascii().data() << "\""
264 << std::endl
<< "Document render tree:" << std::endl
<< externalRepresentation(root
->document().frame()).utf8().data();
266 // Now send the touch event and check any touch action result.
267 sendTouchEvent(webView
, WebInputEvent::TouchStart
, clientPoint
);
269 AtomicString expectedAction
= element
->getAttribute("expected-action");
270 if (expectedAction
== "auto") {
271 // Auto is the default - no action set.
272 EXPECT_EQ(0, client
.touchActionSetCount()) << failureContextPos
;
273 EXPECT_EQ(WebTouchActionAuto
, client
.lastTouchAction()) << failureContextPos
;
275 // Should have received exactly one touch action.
276 EXPECT_EQ(1, client
.touchActionSetCount()) << failureContextPos
;
277 if (client
.touchActionSetCount()) {
278 if (expectedAction
== "none") {
279 EXPECT_EQ(WebTouchActionNone
, client
.lastTouchAction()) << failureContextPos
;
280 } else if (expectedAction
== "pan-x") {
281 EXPECT_EQ(WebTouchActionPanX
, client
.lastTouchAction()) << failureContextPos
;
282 } else if (expectedAction
== "pan-y") {
283 EXPECT_EQ(WebTouchActionPanY
, client
.lastTouchAction()) << failureContextPos
;
284 } else if (expectedAction
== "pan-x-y") {
285 EXPECT_EQ((WebTouchActionPanX
| WebTouchActionPanY
), client
.lastTouchAction()) << failureContextPos
;
286 } else if (expectedAction
== "manipulation") {
287 EXPECT_EQ((WebTouchActionPanX
| WebTouchActionPanY
| WebTouchActionPinchZoom
), client
.lastTouchAction()) << failureContextPos
;
289 FAIL() << "Unrecognized expected-action \"" << expectedAction
.ascii().data()
290 << "\" " << failureContextPos
;
295 // Reset webview touch state.
297 sendTouchEvent(webView
, WebInputEvent::TouchCancel
, clientPoint
);
298 EXPECT_EQ(0, client
.touchActionSetCount());
302 void TouchActionTest::sendTouchEvent(WebView
* webView
, WebInputEvent::Type type
, IntPoint clientPoint
)
304 ASSERT_TRUE(type
== WebInputEvent::TouchStart
|| type
== WebInputEvent::TouchCancel
);
306 WebTouchEvent webTouchEvent
;
307 webTouchEvent
.type
= type
;
308 if (type
== WebInputEvent::TouchCancel
)
309 webTouchEvent
.cancelable
= false;
310 webTouchEvent
.touchesLength
= 1;
311 webTouchEvent
.touches
[0].state
= (type
== WebInputEvent::TouchStart
?
312 WebTouchPoint::StatePressed
:
313 WebTouchPoint::StateCancelled
);
314 webTouchEvent
.touches
[0].id
= kfakeTouchId
;
315 webTouchEvent
.touches
[0].screenPosition
.x
= clientPoint
.x();
316 webTouchEvent
.touches
[0].screenPosition
.y
= clientPoint
.y();
317 webTouchEvent
.touches
[0].position
.x
= clientPoint
.x();
318 webTouchEvent
.touches
[0].position
.y
= clientPoint
.y();
319 webTouchEvent
.touches
[0].radiusX
= 10;
320 webTouchEvent
.touches
[0].radiusY
= 10;
322 webView
->handleInputEvent(webTouchEvent
);
327 TEST_F(TouchActionTest
, Simple
)
329 runTouchActionTest("touch-action-simple.html");
332 TEST_F(TouchActionTest
, Overflow
)
334 runTouchActionTest("touch-action-overflow.html");
337 TEST_F(TouchActionTest
, ShadowDOM
)
339 runShadowDOMTest("touch-action-shadow-dom.html");
342 TEST_F(TouchActionTest
, Pan
)
344 runTouchActionTest("touch-action-pan.html");