Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / tests / TouchActionTest.cpp
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 "RuntimeEnabledFeatures.h"
34 #include "core/dom/ClientRect.h"
35 #include "core/dom/ClientRectList.h"
36 #include "core/dom/Document.h"
37 #include "core/dom/Element.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 "public/platform/Platform.h"
45 #include "public/platform/WebUnitTestSupport.h"
46 #include "public/web/WebDocument.h"
47 #include "public/web/WebFrame.h"
48 #include "public/web/WebHitTestResult.h"
49 #include "public/web/WebInputEvent.h"
50 #include "public/web/WebTouchAction.h"
51 #include "public/web/WebView.h"
52 #include "public/web/WebViewClient.h"
53 #include "public/web/WebWidgetClient.h"
54 #include "web/WebViewImpl.h"
55 #include "web/tests/FrameTestHelpers.h"
56 #include "web/tests/URLTestHelpers.h"
57
58 #include <gtest/gtest.h>
59
60 using namespace blink;
61 using blink::FrameTestHelpers::runPendingTasks;
62
63 namespace {
64
65 class TouchActionTrackingWebViewClient : public FrameTestHelpers::TestWebViewClient {
66 public:
67     TouchActionTrackingWebViewClient() :
68         m_actionSetCount(0),
69         m_action(WebTouchActionAuto)
70     {
71     }
72
73     // WebWidgetClient methods
74     virtual void setTouchAction(WebTouchAction touchAction)
75     {
76         m_actionSetCount++;
77         m_action = touchAction;
78     }
79
80     // Local methods
81     void reset()
82     {
83         m_actionSetCount = 0;
84         m_action = WebTouchActionAuto;
85     }
86
87     int touchActionSetCount()
88     {
89         return m_actionSetCount;
90     }
91
92     WebTouchAction lastTouchAction()
93     {
94         return m_action;
95     }
96
97 private:
98     int m_actionSetCount;
99     WebTouchAction m_action;
100 };
101
102 const int kfakeTouchId = 7;
103
104 class TouchActionTest : public testing::Test {
105 public:
106     TouchActionTest()
107         : m_baseURL("http://www.test.com/")
108     {
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);
112     }
113
114     virtual void TearDown()
115     {
116         Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
117     }
118
119 protected:
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&);
125
126     std::string m_baseURL;
127     FrameTestHelpers::WebViewHelper m_webViewHelper;
128 };
129
130 void TouchActionTest::runTouchActionTest(std::string file)
131 {
132     TouchActionTrackingWebViewClient client;
133
134     WebView* webView = setupTest(file, client);
135
136     RefPtrWillBeRawPtr<WebCore::Document> document = static_cast<PassRefPtrWillBeRawPtr<WebCore::Document> >(webView->mainFrame()->document());
137     runTestOnTree(document.get(), webView, client);
138
139     m_webViewHelper.reset(); // Explicitly reset to break dependency on locally scoped client.
140 }
141
142 void TouchActionTest::runShadowDOMTest(std::string file)
143 {
144     TouchActionTrackingWebViewClient client;
145
146     WebView* webView = setupTest(file, client);
147
148     WebCore::TrackExceptionState es;
149     RefPtrWillBeRawPtr<WebCore::Document> document = static_cast<PassRefPtrWillBeRawPtr<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);
153
154     for (unsigned index = 0; index < hostNodes->length(); index++) {
155         WebCore::ShadowRoot* shadowRoot = WebCore::toElement(hostNodes->item(index))->shadowRoot();
156         runTestOnTree(shadowRoot, webView, client);
157     }
158
159     // Projections show up in the main document.
160     runTestOnTree(document.get(), webView, client);
161
162     m_webViewHelper.reset(); // Explicitly reset to break dependency on locally scoped client.
163 }
164
165 WebView* TouchActionTest::setupTest(std::string file, TouchActionTrackingWebViewClient& client)
166 {
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);
170
171     // Set size to enable hit testing, and avoid line wrapping for consistency with browser.
172     webView->resize(WebSize(800, 1200));
173
174     // Scroll to verify the code properly transforms windows to client co-ords.
175     const int kScrollOffset = 100;
176     RefPtrWillBeRawPtr<WebCore::Document> document = static_cast<PassRefPtrWillBeRawPtr<WebCore::Document> >(webView->mainFrame()->document());
177     document->frame()->view()->setScrollOffset(WebCore::IntPoint(0, kScrollOffset));
178
179     return webView;
180 }
181
182 void TouchActionTest::runTestOnTree(WebCore::ContainerNode* root, WebView* webView, TouchActionTrackingWebViewClient& client)
183 {
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());
188
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());
193
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("\"");
201         } else {
202             failureContext += "<missing ID>";
203         }
204
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         RefPtrWillBeRawPtr<WebCore::ClientRectList> rects = element->getClientRects();
210         ASSERT_GE(rects->length(), 0u) << failureContext;
211         RefPtrWillBeRawPtr<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 << " (";
218             switch (locIdx) {
219             case 0:
220                 clientPoint = clientRect.center();
221                 contextStream << "center";
222                 break;
223             case 1:
224                 clientPoint = clientRect.location();
225                 contextStream << "top-left";
226                 break;
227             case 2:
228                 clientPoint = clientRect.maxXMaxYCorner();
229                 clientPoint.move(-1, -1);
230                 contextStream << "bottom-right";
231                 break;
232             default:
233                 FAIL() << "Invalid location index.";
234             }
235             contextStream << "=" << clientPoint.x() << "," << clientPoint.y() << ").";
236             std::string failureContextPos = contextStream.str();
237
238             WebCore::LocalFrame* 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();
244
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();
254
255             // Now send the touch event and check any touch action result.
256             sendTouchEvent(webView, WebInputEvent::TouchStart, clientPoint);
257
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;
263             } else {
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;
275                     } else if (expectedAction == "manipulation") {
276                         EXPECT_EQ((WebTouchActionPanX | WebTouchActionPanY | WebTouchActionPinchZoom), client.lastTouchAction()) << failureContextPos;
277                     } else {
278                         FAIL() << "Unrecognized expected-action \"" << expectedAction.ascii().data()
279                             << "\" " << failureContextPos;
280                     }
281                 }
282             }
283
284             // Reset webview touch state.
285             client.reset();
286             sendTouchEvent(webView, WebInputEvent::TouchCancel, clientPoint);
287             EXPECT_EQ(0, client.touchActionSetCount());
288         }
289     }
290 }
291 void TouchActionTest::sendTouchEvent(WebView* webView, WebInputEvent::Type type, WebCore::IntPoint clientPoint)
292 {
293     ASSERT_TRUE(type == WebInputEvent::TouchStart || type == WebInputEvent::TouchCancel);
294
295     WebTouchEvent webTouchEvent;
296     webTouchEvent.type = type;
297     if (type == WebInputEvent::TouchCancel)
298         webTouchEvent.cancelable = false;
299     webTouchEvent.touchesLength = 1;
300     webTouchEvent.touches[0].state = (type == WebInputEvent::TouchStart ?
301         WebTouchPoint::StatePressed :
302         WebTouchPoint::StateCancelled);
303     webTouchEvent.touches[0].id = kfakeTouchId;
304     webTouchEvent.touches[0].screenPosition.x = clientPoint.x();
305     webTouchEvent.touches[0].screenPosition.y = clientPoint.y();
306     webTouchEvent.touches[0].position.x = clientPoint.x();
307     webTouchEvent.touches[0].position.y = clientPoint.y();
308     webTouchEvent.touches[0].radiusX = 10;
309     webTouchEvent.touches[0].radiusY = 10;
310
311     webView->handleInputEvent(webTouchEvent);
312     runPendingTasks();
313 }
314
315 TEST_F(TouchActionTest, Simple)
316 {
317     runTouchActionTest("touch-action-simple.html");
318 }
319
320 TEST_F(TouchActionTest, Overflow)
321 {
322     runTouchActionTest("touch-action-overflow.html");
323 }
324
325 TEST_F(TouchActionTest, ShadowDOM)
326 {
327     runShadowDOMTest("touch-action-shadow-dom.html");
328 }
329
330 TEST_F(TouchActionTest, Pan)
331 {
332     runTouchActionTest("touch-action-pan.html");
333 }
334
335 }