• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 "web/WebInputEventConversion.h"
34 
35 #include "core/dom/Touch.h"
36 #include "core/dom/TouchList.h"
37 #include "core/events/GestureEvent.h"
38 #include "core/events/KeyboardEvent.h"
39 #include "core/events/MouseEvent.h"
40 #include "core/events/TouchEvent.h"
41 #include "core/frame/FrameHost.h"
42 #include "core/frame/FrameView.h"
43 #include "core/frame/LocalFrame.h"
44 #include "core/page/Page.h"
45 #include "core/rendering/RenderView.h"
46 #include "core/testing/URLTestHelpers.h"
47 #include "public/web/WebFrame.h"
48 #include "public/web/WebSettings.h"
49 #include "web/WebViewImpl.h"
50 #include "web/tests/FrameTestHelpers.h"
51 #include <gtest/gtest.h>
52 
53 using namespace blink;
54 
55 namespace {
56 
createKeyboardEventWithLocation(KeyboardEvent::KeyLocationCode location)57 PassRefPtrWillBeRawPtr<KeyboardEvent> createKeyboardEventWithLocation(KeyboardEvent::KeyLocationCode location)
58 {
59     return KeyboardEvent::create("keydown", true, true, 0, "", location, false, false, false, false);
60 }
61 
getModifiersForKeyLocationCode(KeyboardEvent::KeyLocationCode location)62 int getModifiersForKeyLocationCode(KeyboardEvent::KeyLocationCode location)
63 {
64     RefPtrWillBeRawPtr<KeyboardEvent> event = createKeyboardEventWithLocation(location);
65     WebKeyboardEventBuilder convertedEvent(*event);
66     return convertedEvent.modifiers;
67 }
68 
TEST(WebInputEventConversionTest,WebKeyboardEventBuilder)69 TEST(WebInputEventConversionTest, WebKeyboardEventBuilder)
70 {
71     // Test key location conversion.
72     int modifiers = getModifiersForKeyLocationCode(KeyboardEvent::DOM_KEY_LOCATION_STANDARD);
73     EXPECT_FALSE(modifiers & WebInputEvent::IsKeyPad || modifiers & WebInputEvent::IsLeft || modifiers & WebInputEvent::IsRight);
74 
75     modifiers = getModifiersForKeyLocationCode(KeyboardEvent::DOM_KEY_LOCATION_LEFT);
76     EXPECT_TRUE(modifiers & WebInputEvent::IsLeft);
77     EXPECT_FALSE(modifiers & WebInputEvent::IsKeyPad || modifiers & WebInputEvent::IsRight);
78 
79     modifiers = getModifiersForKeyLocationCode(KeyboardEvent::DOM_KEY_LOCATION_RIGHT);
80     EXPECT_TRUE(modifiers & WebInputEvent::IsRight);
81     EXPECT_FALSE(modifiers & WebInputEvent::IsKeyPad || modifiers & WebInputEvent::IsLeft);
82 
83     modifiers = getModifiersForKeyLocationCode(KeyboardEvent::DOM_KEY_LOCATION_NUMPAD);
84     EXPECT_TRUE(modifiers & WebInputEvent::IsKeyPad);
85     EXPECT_FALSE(modifiers & WebInputEvent::IsLeft || modifiers & WebInputEvent::IsRight);
86 }
87 
TEST(WebInputEventConversionTest,WebMouseEventBuilder)88 TEST(WebInputEventConversionTest, WebMouseEventBuilder)
89 {
90     RefPtrWillBeRawPtr<TouchEvent> event = TouchEvent::create();
91     WebMouseEventBuilder mouse(0, 0, *event);
92     EXPECT_EQ(WebInputEvent::Undefined, mouse.type);
93 }
94 
TEST(WebInputEventConversionTest,WebTouchEventBuilder)95 TEST(WebInputEventConversionTest, WebTouchEventBuilder)
96 {
97     const std::string baseURL("http://www.test0.com/");
98     const std::string fileName("fixed_layout.html");
99 
100     URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(baseURL.c_str()), WebString::fromUTF8("fixed_layout.html"));
101     FrameTestHelpers::WebViewHelper webViewHelper;
102     WebViewImpl* webViewImpl = webViewHelper.initializeAndLoad(baseURL + fileName, true);
103     int pageWidth = 640;
104     int pageHeight = 480;
105     webViewImpl->resize(WebSize(pageWidth, pageHeight));
106     webViewImpl->layout();
107 
108     FrameView* view = toLocalFrame(webViewImpl->page()->mainFrame())->view();
109     RefPtrWillBeRawPtr<Document> document = toLocalFrame(webViewImpl->page()->mainFrame())->document();
110     LocalDOMWindow* domWindow = document->domWindow();
111     RenderView* documentRenderView = document->renderView();
112 
113     WebTouchPoint p0, p1;
114     p0.id = 1;
115     p1.id = 2;
116     p0.screenPosition = WebFloatPoint(100.f, 50.f);
117     p1.screenPosition = WebFloatPoint(150.f, 25.f);
118     p0.position = WebFloatPoint(10.f, 10.f);
119     p1.position = WebFloatPoint(5.f, 5.f);
120     p0.radiusX = p1.radiusY = 10.f;
121     p0.radiusY = p1.radiusX = 5.f;
122     p0.rotationAngle = p1.rotationAngle = 1.f;
123     p0.force = p1.force = 25.f;
124 
125     RefPtrWillBeRawPtr<Touch> touch0 = Touch::create(toLocalFrame(webViewImpl->page()->mainFrame()), document.get(), p0.id, p0.screenPosition, p0.position, FloatSize(p0.radiusX, p0.radiusY), p0.rotationAngle, p0.force);
126     RefPtrWillBeRawPtr<Touch> touch1 = Touch::create(toLocalFrame(webViewImpl->page()->mainFrame()), document.get(), p1.id, p1.screenPosition, p1.position, FloatSize(p1.radiusX, p1.radiusY), p1.rotationAngle, p1.force);
127 
128     // Test touchstart.
129     {
130         RefPtrWillBeRawPtr<TouchList> touchList = TouchList::create();
131         touchList->append(touch0);
132         RefPtrWillBeRawPtr<TouchEvent> touchEvent = TouchEvent::create(touchList.get(), touchList.get(), touchList.get(), EventTypeNames::touchstart, domWindow, false, false, false, false, false);
133 
134         WebTouchEventBuilder webTouchBuilder(view, documentRenderView, *touchEvent);
135         ASSERT_EQ(1u, webTouchBuilder.touchesLength);
136         EXPECT_EQ(WebInputEvent::TouchStart, webTouchBuilder.type);
137         EXPECT_EQ(WebTouchPoint::StatePressed, webTouchBuilder.touches[0].state);
138         EXPECT_FLOAT_EQ(p0.screenPosition.x, webTouchBuilder.touches[0].screenPosition.x);
139         EXPECT_FLOAT_EQ(p0.screenPosition.y, webTouchBuilder.touches[0].screenPosition.y);
140         EXPECT_FLOAT_EQ(p0.position.x, webTouchBuilder.touches[0].position.x);
141         EXPECT_FLOAT_EQ(p0.position.y, webTouchBuilder.touches[0].position.y);
142         EXPECT_FLOAT_EQ(p0.radiusX, webTouchBuilder.touches[0].radiusX);
143         EXPECT_FLOAT_EQ(p0.radiusY, webTouchBuilder.touches[0].radiusY);
144         EXPECT_FLOAT_EQ(p0.rotationAngle, webTouchBuilder.touches[0].rotationAngle);
145         EXPECT_FLOAT_EQ(p0.force, webTouchBuilder.touches[0].force);
146     }
147 
148     // Test touchmove.
149     {
150         RefPtrWillBeRawPtr<TouchList> activeTouchList = TouchList::create();
151         RefPtrWillBeRawPtr<TouchList> movedTouchList = TouchList::create();
152         activeTouchList->append(touch0);
153         activeTouchList->append(touch1);
154         movedTouchList->append(touch0);
155         RefPtrWillBeRawPtr<TouchEvent> touchEvent = TouchEvent::create(activeTouchList.get(), activeTouchList.get(), movedTouchList.get(), EventTypeNames::touchmove, domWindow, false, false, false, false, false);
156 
157         WebTouchEventBuilder webTouchBuilder(view, documentRenderView, *touchEvent);
158         ASSERT_EQ(2u, webTouchBuilder.touchesLength);
159         EXPECT_EQ(WebInputEvent::TouchMove, webTouchBuilder.type);
160         EXPECT_EQ(WebTouchPoint::StateMoved, webTouchBuilder.touches[0].state);
161         EXPECT_EQ(WebTouchPoint::StateStationary, webTouchBuilder.touches[1].state);
162         EXPECT_EQ(p0.id, webTouchBuilder.touches[0].id);
163         EXPECT_EQ(p1.id, webTouchBuilder.touches[1].id);
164     }
165 
166     // Test touchend.
167     {
168         RefPtrWillBeRawPtr<TouchList> activeTouchList = TouchList::create();
169         RefPtrWillBeRawPtr<TouchList> releasedTouchList = TouchList::create();
170         activeTouchList->append(touch0);
171         releasedTouchList->append(touch1);
172         RefPtrWillBeRawPtr<TouchEvent> touchEvent = TouchEvent::create(activeTouchList.get(), activeTouchList.get(), releasedTouchList.get(), EventTypeNames::touchend, domWindow, false, false, false, false, false);
173 
174         WebTouchEventBuilder webTouchBuilder(view, documentRenderView, *touchEvent);
175         ASSERT_EQ(2u, webTouchBuilder.touchesLength);
176         EXPECT_EQ(WebInputEvent::TouchEnd, webTouchBuilder.type);
177         EXPECT_EQ(WebTouchPoint::StateReleased, webTouchBuilder.touches[0].state);
178         EXPECT_EQ(WebTouchPoint::StateStationary, webTouchBuilder.touches[1].state);
179         EXPECT_EQ(p1.id, webTouchBuilder.touches[0].id);
180         EXPECT_EQ(p0.id, webTouchBuilder.touches[1].id);
181     }
182 
183     // Test touchcancel.
184     {
185         RefPtrWillBeRawPtr<TouchList> activeTouchList = TouchList::create();
186         RefPtrWillBeRawPtr<TouchList> cancelledTouchList = TouchList::create();
187         cancelledTouchList->append(touch0);
188         cancelledTouchList->append(touch1);
189         RefPtrWillBeRawPtr<TouchEvent> touchEvent = TouchEvent::create(activeTouchList.get(), activeTouchList.get(), cancelledTouchList.get(), EventTypeNames::touchcancel, domWindow, false, false, false, false, false);
190 
191         WebTouchEventBuilder webTouchBuilder(view, documentRenderView, *touchEvent);
192         ASSERT_EQ(2u, webTouchBuilder.touchesLength);
193         EXPECT_EQ(WebInputEvent::TouchCancel, webTouchBuilder.type);
194         EXPECT_EQ(WebTouchPoint::StateCancelled, webTouchBuilder.touches[0].state);
195         EXPECT_EQ(WebTouchPoint::StateCancelled, webTouchBuilder.touches[1].state);
196         EXPECT_EQ(p0.id, webTouchBuilder.touches[0].id);
197         EXPECT_EQ(p1.id, webTouchBuilder.touches[1].id);
198     }
199 
200     // Test max point limit.
201     {
202         RefPtrWillBeRawPtr<TouchList> touchList = TouchList::create();
203         RefPtrWillBeRawPtr<TouchList> changedTouchList = TouchList::create();
204         for (unsigned i = 0; i <= static_cast<unsigned>(WebTouchEvent::touchesLengthCap) * 2; ++i) {
205             RefPtrWillBeRawPtr<Touch> touch = Touch::create(toLocalFrame(webViewImpl->page()->mainFrame()), document.get(), i, p0.screenPosition, p0.position, FloatSize(p0.radiusX, p0.radiusY), p0.rotationAngle, p0.force);
206             touchList->append(touch);
207             changedTouchList->append(touch);
208         }
209         RefPtrWillBeRawPtr<TouchEvent> touchEvent = TouchEvent::create(touchList.get(), touchList.get(), touchList.get(), EventTypeNames::touchstart, domWindow, false, false, false, false, false);
210 
211         WebTouchEventBuilder webTouchBuilder(view, documentRenderView, *touchEvent);
212         ASSERT_EQ(static_cast<unsigned>(WebTouchEvent::touchesLengthCap), webTouchBuilder.touchesLength);
213     }
214 }
215 
TEST(WebInputEventConversionTest,InputEventsScaling)216 TEST(WebInputEventConversionTest, InputEventsScaling)
217 {
218     const std::string baseURL("http://www.test.com/");
219     const std::string fileName("fixed_layout.html");
220 
221     URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(baseURL.c_str()), WebString::fromUTF8("fixed_layout.html"));
222     FrameTestHelpers::WebViewHelper webViewHelper;
223     WebViewImpl* webViewImpl = webViewHelper.initializeAndLoad(baseURL + fileName, true);
224     webViewImpl->settings()->setViewportEnabled(true);
225     int pageWidth = 640;
226     int pageHeight = 480;
227     webViewImpl->resize(WebSize(pageWidth, pageHeight));
228     webViewImpl->layout();
229 
230     webViewImpl->setPageScaleFactor(2);
231 
232     FrameView* view = toLocalFrame(webViewImpl->page()->mainFrame())->view();
233     RefPtrWillBeRawPtr<Document> document = toLocalFrame(webViewImpl->page()->mainFrame())->document();
234     LocalDOMWindow* domWindow = document->domWindow();
235     RenderView* documentRenderView = document->renderView();
236 
237     {
238         WebMouseEvent webMouseEvent;
239         webMouseEvent.type = WebInputEvent::MouseMove;
240         webMouseEvent.x = 10;
241         webMouseEvent.y = 10;
242         webMouseEvent.windowX = 10;
243         webMouseEvent.windowY = 10;
244         webMouseEvent.globalX = 10;
245         webMouseEvent.globalY = 10;
246         webMouseEvent.movementX = 10;
247         webMouseEvent.movementY = 10;
248 
249         PlatformMouseEventBuilder platformMouseBuilder(view, webMouseEvent);
250         EXPECT_EQ(5, platformMouseBuilder.position().x());
251         EXPECT_EQ(5, platformMouseBuilder.position().y());
252         EXPECT_EQ(10, platformMouseBuilder.globalPosition().x());
253         EXPECT_EQ(10, platformMouseBuilder.globalPosition().y());
254         EXPECT_EQ(5, platformMouseBuilder.movementDelta().x());
255         EXPECT_EQ(5, platformMouseBuilder.movementDelta().y());
256     }
257 
258     {
259         WebGestureEvent webGestureEvent;
260         webGestureEvent.type = WebInputEvent::GestureScrollUpdate;
261         webGestureEvent.x = 10;
262         webGestureEvent.y = 10;
263         webGestureEvent.globalX = 10;
264         webGestureEvent.globalY = 10;
265         webGestureEvent.data.scrollUpdate.deltaX = 10;
266         webGestureEvent.data.scrollUpdate.deltaY = 10;
267 
268         PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent);
269         EXPECT_EQ(5, platformGestureBuilder.position().x());
270         EXPECT_EQ(5, platformGestureBuilder.position().y());
271         EXPECT_EQ(10, platformGestureBuilder.globalPosition().x());
272         EXPECT_EQ(10, platformGestureBuilder.globalPosition().y());
273         EXPECT_EQ(5, platformGestureBuilder.deltaX());
274         EXPECT_EQ(5, platformGestureBuilder.deltaY());
275     }
276 
277     {
278         WebGestureEvent webGestureEvent;
279         webGestureEvent.type = WebInputEvent::GestureTap;
280         webGestureEvent.data.tap.width = 10;
281         webGestureEvent.data.tap.height = 10;
282 
283         PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent);
284         EXPECT_EQ(5, platformGestureBuilder.area().width());
285         EXPECT_EQ(5, platformGestureBuilder.area().height());
286     }
287 
288     {
289         WebGestureEvent webGestureEvent;
290         webGestureEvent.type = WebInputEvent::GestureTapUnconfirmed;
291         webGestureEvent.data.tap.width = 10;
292         webGestureEvent.data.tap.height = 10;
293 
294         PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent);
295         EXPECT_EQ(5, platformGestureBuilder.area().width());
296         EXPECT_EQ(5, platformGestureBuilder.area().height());
297     }
298 
299     {
300         WebGestureEvent webGestureEvent;
301         webGestureEvent.type = WebInputEvent::GestureTapDown;
302         webGestureEvent.data.tapDown.width = 10;
303         webGestureEvent.data.tapDown.height = 10;
304 
305         PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent);
306         EXPECT_EQ(5, platformGestureBuilder.area().width());
307         EXPECT_EQ(5, platformGestureBuilder.area().height());
308     }
309 
310     {
311         WebGestureEvent webGestureEvent;
312         webGestureEvent.type = WebInputEvent::GestureShowPress;
313         webGestureEvent.data.showPress.width = 10;
314         webGestureEvent.data.showPress.height = 10;
315 
316         PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent);
317         EXPECT_EQ(5, platformGestureBuilder.area().width());
318         EXPECT_EQ(5, platformGestureBuilder.area().height());
319     }
320 
321     {
322         WebGestureEvent webGestureEvent;
323         webGestureEvent.type = WebInputEvent::GestureLongPress;
324         webGestureEvent.data.longPress.width = 10;
325         webGestureEvent.data.longPress.height = 10;
326 
327         PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent);
328         EXPECT_EQ(5, platformGestureBuilder.area().width());
329         EXPECT_EQ(5, platformGestureBuilder.area().height());
330     }
331 
332     {
333         WebGestureEvent webGestureEvent;
334         webGestureEvent.type = WebInputEvent::GestureTwoFingerTap;
335         webGestureEvent.data.twoFingerTap.firstFingerWidth = 10;
336         webGestureEvent.data.twoFingerTap.firstFingerHeight = 10;
337 
338         PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent);
339         EXPECT_EQ(5, platformGestureBuilder.area().width());
340         EXPECT_EQ(5, platformGestureBuilder.area().height());
341     }
342 
343     {
344         WebTouchEvent webTouchEvent;
345         webTouchEvent.type = WebInputEvent::TouchMove;
346         webTouchEvent.touchesLength = 1;
347         webTouchEvent.touches[0].state = WebTouchPoint::StateMoved;
348         webTouchEvent.touches[0].screenPosition.x = 10.6f;
349         webTouchEvent.touches[0].screenPosition.y = 10.4f;
350         webTouchEvent.touches[0].position.x = 10.6f;
351         webTouchEvent.touches[0].position.y = 10.4f;
352         webTouchEvent.touches[0].radiusX = 10.6f;
353         webTouchEvent.touches[0].radiusY = 10.4f;
354 
355         EXPECT_FLOAT_EQ(10.6f, webTouchEvent.touches[0].screenPosition.x);
356         EXPECT_FLOAT_EQ(10.4f, webTouchEvent.touches[0].screenPosition.y);
357         EXPECT_FLOAT_EQ(10.6f, webTouchEvent.touches[0].position.x);
358         EXPECT_FLOAT_EQ(10.4f, webTouchEvent.touches[0].position.y);
359         EXPECT_FLOAT_EQ(10.6f, webTouchEvent.touches[0].radiusX);
360         EXPECT_FLOAT_EQ(10.4f, webTouchEvent.touches[0].radiusY);
361 
362         PlatformTouchEventBuilder platformTouchBuilder(view, webTouchEvent);
363         EXPECT_FLOAT_EQ(10.6f, platformTouchBuilder.touchPoints()[0].screenPos().x());
364         EXPECT_FLOAT_EQ(10.4f, platformTouchBuilder.touchPoints()[0].screenPos().y());
365         EXPECT_FLOAT_EQ(5.3f, platformTouchBuilder.touchPoints()[0].pos().x());
366         EXPECT_FLOAT_EQ(5.2f, platformTouchBuilder.touchPoints()[0].pos().y());
367         EXPECT_FLOAT_EQ(5.3f, platformTouchBuilder.touchPoints()[0].radius().width());
368         EXPECT_FLOAT_EQ(5.2f, platformTouchBuilder.touchPoints()[0].radius().height());
369     }
370 
371     // Reverse builders should *not* go back to physical pixels, as they are used for plugins
372     // which expect CSS pixel coordinates.
373     {
374         PlatformMouseEvent platformMouseEvent(IntPoint(10, 10), IntPoint(10, 10), LeftButton, PlatformEvent::MouseMoved, 1, false, false, false, false, PlatformMouseEvent::RealOrIndistinguishable, 0);
375         RefPtrWillBeRawPtr<MouseEvent> mouseEvent = MouseEvent::create(EventTypeNames::mousemove, domWindow, platformMouseEvent, 0, document);
376         WebMouseEventBuilder webMouseBuilder(view, documentRenderView, *mouseEvent);
377 
378         EXPECT_EQ(10, webMouseBuilder.x);
379         EXPECT_EQ(10, webMouseBuilder.y);
380         EXPECT_EQ(10, webMouseBuilder.globalX);
381         EXPECT_EQ(10, webMouseBuilder.globalY);
382         EXPECT_EQ(10, webMouseBuilder.windowX);
383         EXPECT_EQ(10, webMouseBuilder.windowY);
384     }
385 
386     {
387         PlatformMouseEvent platformMouseEvent(IntPoint(10, 10), IntPoint(10, 10), NoButton, PlatformEvent::MouseMoved, 1, false, false, false, false, PlatformMouseEvent::RealOrIndistinguishable, 0);
388         RefPtrWillBeRawPtr<MouseEvent> mouseEvent = MouseEvent::create(EventTypeNames::mousemove, domWindow, platformMouseEvent, 0, document);
389         WebMouseEventBuilder webMouseBuilder(view, documentRenderView, *mouseEvent);
390         EXPECT_EQ(WebMouseEvent::ButtonNone, webMouseBuilder.button);
391     }
392 
393     {
394         PlatformGestureEvent platformGestureEvent(PlatformEvent::GestureScrollUpdate, IntPoint(10, 10), IntPoint(10, 10), IntSize(10, 10), 0, false, false, false, false, 10, 10, 10, 10);
395         RefPtrWillBeRawPtr<GestureEvent> gestureEvent = GestureEvent::create(domWindow, platformGestureEvent);
396         WebGestureEventBuilder webGestureBuilder(view, documentRenderView, *gestureEvent);
397 
398         EXPECT_EQ(10, webGestureBuilder.x);
399         EXPECT_EQ(10, webGestureBuilder.y);
400         EXPECT_EQ(10, webGestureBuilder.globalX);
401         EXPECT_EQ(10, webGestureBuilder.globalY);
402         EXPECT_EQ(10, webGestureBuilder.data.scrollUpdate.deltaX);
403         EXPECT_EQ(10, webGestureBuilder.data.scrollUpdate.deltaY);
404     }
405 
406     {
407         RefPtrWillBeRawPtr<Touch> touch = Touch::create(toLocalFrame(webViewImpl->page()->mainFrame()), document.get(), 0, FloatPoint(10, 9.5), FloatPoint(3.5, 2), FloatSize(4, 4.5), 0, 0);
408         RefPtrWillBeRawPtr<TouchList> touchList = TouchList::create();
409         touchList->append(touch);
410         RefPtrWillBeRawPtr<TouchEvent> touchEvent = TouchEvent::create(touchList.get(), touchList.get(), touchList.get(), EventTypeNames::touchmove, domWindow, false, false, false, false, false);
411 
412         WebTouchEventBuilder webTouchBuilder(view, documentRenderView, *touchEvent);
413         ASSERT_EQ(1u, webTouchBuilder.touchesLength);
414         EXPECT_EQ(10, webTouchBuilder.touches[0].screenPosition.x);
415         EXPECT_FLOAT_EQ(9.5, webTouchBuilder.touches[0].screenPosition.y);
416         EXPECT_FLOAT_EQ(3.5, webTouchBuilder.touches[0].position.x);
417         EXPECT_FLOAT_EQ(2, webTouchBuilder.touches[0].position.y);
418         EXPECT_FLOAT_EQ(4, webTouchBuilder.touches[0].radiusX);
419         EXPECT_FLOAT_EQ(4.5, webTouchBuilder.touches[0].radiusY);
420         EXPECT_FALSE(webTouchBuilder.cancelable);
421     }
422 }
423 
TEST(WebInputEventConversionTest,InputEventsTransform)424 TEST(WebInputEventConversionTest, InputEventsTransform)
425 {
426     const std::string baseURL("http://www.test2.com/");
427     const std::string fileName("fixed_layout.html");
428 
429     URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(baseURL.c_str()), WebString::fromUTF8("fixed_layout.html"));
430     FrameTestHelpers::WebViewHelper webViewHelper;
431     WebViewImpl* webViewImpl = webViewHelper.initializeAndLoad(baseURL + fileName, true);
432     webViewImpl->settings()->setViewportEnabled(true);
433     int pageWidth = 640;
434     int pageHeight = 480;
435     webViewImpl->resize(WebSize(pageWidth, pageHeight));
436     webViewImpl->layout();
437 
438     webViewImpl->setPageScaleFactor(2);
439     webViewImpl->setRootLayerTransform(WebSize(10, 20), 1.5);
440 
441     FrameView* view = toLocalFrame(webViewImpl->page()->mainFrame())->view();
442 
443     {
444         WebMouseEvent webMouseEvent;
445         webMouseEvent.type = WebInputEvent::MouseMove;
446         webMouseEvent.x = 100;
447         webMouseEvent.y = 110;
448         webMouseEvent.windowX = 100;
449         webMouseEvent.windowY = 110;
450         webMouseEvent.globalX = 100;
451         webMouseEvent.globalY = 110;
452         webMouseEvent.movementX = 60;
453         webMouseEvent.movementY = 60;
454 
455         PlatformMouseEventBuilder platformMouseBuilder(view, webMouseEvent);
456         EXPECT_EQ(30, platformMouseBuilder.position().x());
457         EXPECT_EQ(30, platformMouseBuilder.position().y());
458         EXPECT_EQ(100, platformMouseBuilder.globalPosition().x());
459         EXPECT_EQ(110, platformMouseBuilder.globalPosition().y());
460         EXPECT_EQ(20, platformMouseBuilder.movementDelta().x());
461         EXPECT_EQ(20, platformMouseBuilder.movementDelta().y());
462     }
463 
464     {
465         WebGestureEvent webGestureEvent;
466         webGestureEvent.type = WebInputEvent::GestureScrollUpdate;
467         webGestureEvent.x = 100;
468         webGestureEvent.y = 110;
469         webGestureEvent.globalX = 100;
470         webGestureEvent.globalY = 110;
471         webGestureEvent.data.scrollUpdate.deltaX = 60;
472         webGestureEvent.data.scrollUpdate.deltaY = 60;
473 
474         PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent);
475         EXPECT_EQ(30, platformGestureBuilder.position().x());
476         EXPECT_EQ(30, platformGestureBuilder.position().y());
477         EXPECT_EQ(100, platformGestureBuilder.globalPosition().x());
478         EXPECT_EQ(110, platformGestureBuilder.globalPosition().y());
479         EXPECT_EQ(20, platformGestureBuilder.deltaX());
480         EXPECT_EQ(20, platformGestureBuilder.deltaY());
481     }
482 
483     {
484         WebGestureEvent webGestureEvent;
485         webGestureEvent.type = WebInputEvent::GestureTap;
486         webGestureEvent.data.tap.width = 30;
487         webGestureEvent.data.tap.height = 30;
488 
489         PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent);
490         EXPECT_EQ(10, platformGestureBuilder.area().width());
491         EXPECT_EQ(10, platformGestureBuilder.area().height());
492     }
493 
494     {
495         WebGestureEvent webGestureEvent;
496         webGestureEvent.type = WebInputEvent::GestureTapUnconfirmed;
497         webGestureEvent.data.tap.width = 30;
498         webGestureEvent.data.tap.height = 30;
499 
500         PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent);
501         EXPECT_EQ(10, platformGestureBuilder.area().width());
502         EXPECT_EQ(10, platformGestureBuilder.area().height());
503     }
504 
505     {
506         WebGestureEvent webGestureEvent;
507         webGestureEvent.type = WebInputEvent::GestureTapDown;
508         webGestureEvent.data.tapDown.width = 30;
509         webGestureEvent.data.tapDown.height = 30;
510 
511         PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent);
512         EXPECT_EQ(10, platformGestureBuilder.area().width());
513         EXPECT_EQ(10, platformGestureBuilder.area().height());
514     }
515 
516     {
517         WebGestureEvent webGestureEvent;
518         webGestureEvent.type = WebInputEvent::GestureShowPress;
519         webGestureEvent.data.showPress.width = 30;
520         webGestureEvent.data.showPress.height = 30;
521 
522         PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent);
523         EXPECT_EQ(10, platformGestureBuilder.area().width());
524         EXPECT_EQ(10, platformGestureBuilder.area().height());
525     }
526 
527     {
528         WebGestureEvent webGestureEvent;
529         webGestureEvent.type = WebInputEvent::GestureLongPress;
530         webGestureEvent.data.longPress.width = 30;
531         webGestureEvent.data.longPress.height = 30;
532 
533         PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent);
534         EXPECT_EQ(10, platformGestureBuilder.area().width());
535         EXPECT_EQ(10, platformGestureBuilder.area().height());
536     }
537 
538     {
539         WebGestureEvent webGestureEvent;
540         webGestureEvent.type = WebInputEvent::GestureTwoFingerTap;
541         webGestureEvent.data.twoFingerTap.firstFingerWidth = 30;
542         webGestureEvent.data.twoFingerTap.firstFingerHeight = 30;
543 
544         PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent);
545         EXPECT_EQ(10, platformGestureBuilder.area().width());
546         EXPECT_EQ(10, platformGestureBuilder.area().height());
547     }
548 
549     {
550         WebTouchEvent webTouchEvent;
551         webTouchEvent.type = WebInputEvent::TouchMove;
552         webTouchEvent.touchesLength = 1;
553         webTouchEvent.touches[0].state = WebTouchPoint::StateMoved;
554         webTouchEvent.touches[0].screenPosition.x = 100;
555         webTouchEvent.touches[0].screenPosition.y = 110;
556         webTouchEvent.touches[0].position.x = 100;
557         webTouchEvent.touches[0].position.y = 110;
558         webTouchEvent.touches[0].radiusX = 30;
559         webTouchEvent.touches[0].radiusY = 30;
560 
561         PlatformTouchEventBuilder platformTouchBuilder(view, webTouchEvent);
562         EXPECT_FLOAT_EQ(100, platformTouchBuilder.touchPoints()[0].screenPos().x());
563         EXPECT_FLOAT_EQ(110, platformTouchBuilder.touchPoints()[0].screenPos().y());
564         EXPECT_FLOAT_EQ(30, platformTouchBuilder.touchPoints()[0].pos().x());
565         EXPECT_FLOAT_EQ(30, platformTouchBuilder.touchPoints()[0].pos().y());
566         EXPECT_FLOAT_EQ(10, platformTouchBuilder.touchPoints()[0].radius().width());
567         EXPECT_FLOAT_EQ(10, platformTouchBuilder.touchPoints()[0].radius().height());
568     }
569 }
570 
TEST(WebInputEventConversionTest,InputEventsConversions)571 TEST(WebInputEventConversionTest, InputEventsConversions)
572 {
573     const std::string baseURL("http://www.test3.com/");
574     const std::string fileName("fixed_layout.html");
575 
576     URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(baseURL.c_str()), WebString::fromUTF8("fixed_layout.html"));
577     FrameTestHelpers::WebViewHelper webViewHelper;
578     WebViewImpl* webViewImpl = webViewHelper.initializeAndLoad(baseURL + fileName, true);
579     int pageWidth = 640;
580     int pageHeight = 480;
581     webViewImpl->resize(WebSize(pageWidth, pageHeight));
582     webViewImpl->layout();
583 
584     FrameView* view = toLocalFrame(webViewImpl->page()->mainFrame())->view();
585     RefPtrWillBeRawPtr<Document> document = toLocalFrame(webViewImpl->page()->mainFrame())->document();
586     LocalDOMWindow* domWindow = document->domWindow();
587     RenderView* documentRenderView = document->renderView();
588 
589     {
590         WebGestureEvent webGestureEvent;
591         webGestureEvent.type = WebInputEvent::GestureTap;
592         webGestureEvent.x = 10;
593         webGestureEvent.y = 10;
594         webGestureEvent.globalX = 10;
595         webGestureEvent.globalY = 10;
596         webGestureEvent.data.tap.tapCount = 1;
597         webGestureEvent.data.tap.width = 10;
598         webGestureEvent.data.tap.height = 10;
599 
600         PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent);
601         EXPECT_EQ(10.f, platformGestureBuilder.position().x());
602         EXPECT_EQ(10.f, platformGestureBuilder.position().y());
603         EXPECT_EQ(10.f, platformGestureBuilder.globalPosition().x());
604         EXPECT_EQ(10.f, platformGestureBuilder.globalPosition().y());
605         EXPECT_EQ(1, platformGestureBuilder.tapCount());
606 
607         RefPtrWillBeRawPtr<GestureEvent> coreGestureEvent = GestureEvent::create(domWindow, platformGestureBuilder);
608         WebGestureEventBuilder recreatedWebGestureEvent(view, documentRenderView, *coreGestureEvent);
609         EXPECT_EQ(webGestureEvent.type, recreatedWebGestureEvent.type);
610         EXPECT_EQ(webGestureEvent.x, recreatedWebGestureEvent.x);
611         EXPECT_EQ(webGestureEvent.y, recreatedWebGestureEvent.y);
612         EXPECT_EQ(webGestureEvent.globalX, recreatedWebGestureEvent.globalX);
613         EXPECT_EQ(webGestureEvent.globalY, recreatedWebGestureEvent.globalY);
614         EXPECT_EQ(webGestureEvent.data.tap.tapCount, recreatedWebGestureEvent.data.tap.tapCount);
615     }
616 }
617 
setupVirtualViewportPinch(WebSettings * settings)618 static void setupVirtualViewportPinch(WebSettings* settings)
619 {
620     settings->setPinchVirtualViewportEnabled(true);
621     settings->setAcceleratedCompositingEnabled(true);
622 }
623 
TEST(WebInputEventConversionTest,PinchViewportOffset)624 TEST(WebInputEventConversionTest, PinchViewportOffset)
625 {
626     const std::string baseURL("http://www.test4.com/");
627     const std::string fileName("fixed_layout.html");
628 
629     URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(baseURL.c_str()), WebString::fromUTF8("fixed_layout.html"));
630     FrameTestHelpers::WebViewHelper webViewHelper;
631     WebViewImpl* webViewImpl = webViewHelper.initializeAndLoad(baseURL + fileName, true, 0, 0, setupVirtualViewportPinch);
632     int pageWidth = 640;
633     int pageHeight = 480;
634     webViewImpl->resize(WebSize(pageWidth, pageHeight));
635     webViewImpl->layout();
636 
637     webViewImpl->setPageScaleFactor(2);
638 
639     IntPoint pinchOffset(35, 60);
640     webViewImpl->page()->frameHost().pinchViewport().setLocation(pinchOffset);
641 
642     FrameView* view = toLocalFrame(webViewImpl->page()->mainFrame())->view();
643 
644     {
645         WebMouseEvent webMouseEvent;
646         webMouseEvent.type = WebInputEvent::MouseMove;
647         webMouseEvent.x = 10;
648         webMouseEvent.y = 10;
649         webMouseEvent.windowX = 10;
650         webMouseEvent.windowY = 10;
651         webMouseEvent.globalX = 10;
652         webMouseEvent.globalY = 10;
653 
654         PlatformMouseEventBuilder platformMouseBuilder(view, webMouseEvent);
655         EXPECT_EQ(5 + pinchOffset.x(), platformMouseBuilder.position().x());
656         EXPECT_EQ(5 + pinchOffset.y(), platformMouseBuilder.position().y());
657         EXPECT_EQ(10, platformMouseBuilder.globalPosition().x());
658         EXPECT_EQ(10, platformMouseBuilder.globalPosition().y());
659     }
660 
661     {
662         WebMouseWheelEvent webMouseWheelEvent;
663         webMouseWheelEvent.type = WebInputEvent::MouseWheel;
664         webMouseWheelEvent.x = 10;
665         webMouseWheelEvent.y = 10;
666         webMouseWheelEvent.windowX = 10;
667         webMouseWheelEvent.windowY = 10;
668         webMouseWheelEvent.globalX = 10;
669         webMouseWheelEvent.globalY = 10;
670 
671         PlatformWheelEventBuilder platformWheelBuilder(view, webMouseWheelEvent);
672         EXPECT_EQ(5 + pinchOffset.x(), platformWheelBuilder.position().x());
673         EXPECT_EQ(5 + pinchOffset.y(), platformWheelBuilder.position().y());
674         EXPECT_EQ(10, platformWheelBuilder.globalPosition().x());
675         EXPECT_EQ(10, platformWheelBuilder.globalPosition().y());
676     }
677 
678     {
679         WebGestureEvent webGestureEvent;
680         webGestureEvent.type = WebInputEvent::GestureScrollUpdate;
681         webGestureEvent.x = 10;
682         webGestureEvent.y = 10;
683         webGestureEvent.globalX = 10;
684         webGestureEvent.globalY = 10;
685 
686         PlatformGestureEventBuilder platformGestureBuilder(view, webGestureEvent);
687         EXPECT_EQ(5 + pinchOffset.x(), platformGestureBuilder.position().x());
688         EXPECT_EQ(5 + pinchOffset.y(), platformGestureBuilder.position().y());
689         EXPECT_EQ(10, platformGestureBuilder.globalPosition().x());
690         EXPECT_EQ(10, platformGestureBuilder.globalPosition().y());
691     }
692 
693     {
694         WebTouchEvent webTouchEvent;
695         webTouchEvent.type = WebInputEvent::TouchMove;
696         webTouchEvent.touchesLength = 1;
697         webTouchEvent.touches[0].state = WebTouchPoint::StateMoved;
698         webTouchEvent.touches[0].screenPosition.x = 10.6f;
699         webTouchEvent.touches[0].screenPosition.y = 10.4f;
700         webTouchEvent.touches[0].position.x = 10.6f;
701         webTouchEvent.touches[0].position.y = 10.4f;
702 
703         EXPECT_FLOAT_EQ(10.6f, webTouchEvent.touches[0].screenPosition.x);
704         EXPECT_FLOAT_EQ(10.4f, webTouchEvent.touches[0].screenPosition.y);
705         EXPECT_FLOAT_EQ(10.6f, webTouchEvent.touches[0].position.x);
706         EXPECT_FLOAT_EQ(10.4f, webTouchEvent.touches[0].position.y);
707 
708         PlatformTouchEventBuilder platformTouchBuilder(view, webTouchEvent);
709         EXPECT_FLOAT_EQ(10.6f, platformTouchBuilder.touchPoints()[0].screenPos().x());
710         EXPECT_FLOAT_EQ(10.4f, platformTouchBuilder.touchPoints()[0].screenPos().y());
711         EXPECT_FLOAT_EQ(5.3f + pinchOffset.x(), platformTouchBuilder.touchPoints()[0].pos().x());
712         EXPECT_FLOAT_EQ(5.2f + pinchOffset.y(), platformTouchBuilder.touchPoints()[0].pos().y());
713     }
714 }
715 
716 } // anonymous namespace
717