1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Needed on Windows to get |M_PI| from <cmath>.
7 #define _USE_MATH_DEFINES
12 #include "content/browser/renderer_host/input/web_input_event_util.h"
13 #include "content/common/input/web_input_event_traits.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/events/blink/blink_event_util.h"
16 #include "ui/events/event_constants.h"
17 #include "ui/events/gesture_detection/gesture_event_data.h"
18 #include "ui/events/gesture_detection/motion_event_generic.h"
19 #include "ui/events/gesture_event_details.h"
20 #include "ui/gfx/geometry/safe_integer_conversions.h"
22 using blink::WebInputEvent
;
23 using blink::WebTouchEvent
;
24 using blink::WebTouchPoint
;
25 using ui::MotionEvent
;
26 using ui::MotionEventGeneric
;
30 TEST(WebInputEventUtilTest
, MotionEventConversion
) {
31 ui::PointerProperties
pointer(5, 10, 40);
35 pointer
.pressure
= 30;
36 pointer
.touch_minor
= 35;
37 pointer
.orientation
= static_cast<float>(-M_PI
/ 2);
38 MotionEventGeneric
event(
39 MotionEvent::ACTION_DOWN
, base::TimeTicks::Now(), pointer
);
40 event
.set_flags(ui::EF_SHIFT_DOWN
| ui::EF_ALT_DOWN
);
42 WebTouchEvent expected_event
;
43 expected_event
.type
= WebInputEvent::TouchStart
;
44 expected_event
.touchesLength
= 1;
45 expected_event
.timeStampSeconds
=
46 (event
.GetEventTime() - base::TimeTicks()).InSecondsF();
47 expected_event
.modifiers
= WebInputEvent::ShiftKey
| WebInputEvent::AltKey
;
48 WebTouchPoint expected_pointer
;
49 expected_pointer
.id
= pointer
.id
;
50 expected_pointer
.state
= WebTouchPoint::StatePressed
;
51 expected_pointer
.position
= blink::WebFloatPoint(pointer
.x
, pointer
.y
);
52 expected_pointer
.screenPosition
=
53 blink::WebFloatPoint(pointer
.raw_x
, pointer
.raw_y
);
54 expected_pointer
.radiusX
= pointer
.touch_major
/ 2.f
;
55 expected_pointer
.radiusY
= pointer
.touch_minor
/ 2.f
;
56 expected_pointer
.rotationAngle
= 0.f
;
57 expected_pointer
.force
= pointer
.pressure
;
58 expected_event
.touches
[0] = expected_pointer
;
60 WebTouchEvent actual_event
=
61 ui::CreateWebTouchEventFromMotionEvent(event
, false);
62 EXPECT_EQ(WebInputEventTraits::ToString(expected_event
),
63 WebInputEventTraits::ToString(actual_event
));
66 TEST(WebInputEventUtilTest
, ScrollUpdateConversion
) {
67 int motion_event_id
= 0;
68 MotionEvent::ToolType tool_type
= MotionEvent::TOOL_TYPE_UNKNOWN
;
69 base::TimeTicks timestamp
= base::TimeTicks::Now();
70 gfx::Vector2dF
delta(-5.f
, 10.f
);
71 gfx::PointF
pos(1.f
, 2.f
);
72 gfx::PointF
raw_pos(11.f
, 12.f
);
73 size_t touch_points
= 1;
74 gfx::RectF
rect(pos
, gfx::SizeF());
76 ui::GestureEventDetails
details(ui::ET_GESTURE_SCROLL_UPDATE
,
79 details
.mark_previous_scroll_update_in_sequence_prevented();
80 ui::GestureEventData
event(details
,
92 blink::WebGestureEvent web_event
=
93 ui::CreateWebGestureEventFromGestureEventData(event
);
94 EXPECT_EQ(WebInputEvent::GestureScrollUpdate
, web_event
.type
);
95 EXPECT_EQ(0, web_event
.modifiers
);
96 EXPECT_EQ((timestamp
- base::TimeTicks()).InSecondsF(),
97 web_event
.timeStampSeconds
);
98 EXPECT_EQ(gfx::ToFlooredInt(pos
.x()), web_event
.x
);
99 EXPECT_EQ(gfx::ToFlooredInt(pos
.y()), web_event
.y
);
100 EXPECT_EQ(gfx::ToFlooredInt(raw_pos
.x()), web_event
.globalX
);
101 EXPECT_EQ(gfx::ToFlooredInt(raw_pos
.y()), web_event
.globalY
);
102 EXPECT_EQ(blink::WebGestureDeviceTouchscreen
, web_event
.sourceDevice
);
103 EXPECT_EQ(delta
.x(), web_event
.data
.scrollUpdate
.deltaX
);
104 EXPECT_EQ(delta
.y(), web_event
.data
.scrollUpdate
.deltaY
);
105 EXPECT_TRUE(web_event
.data
.scrollUpdate
.previousUpdateInSequencePrevented
);
108 } // namespace content