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 #include "content/common/input/web_input_event_traits.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/WebKit/public/web/WebInputEvent.h"
12 using blink::WebGestureEvent
;
13 using blink::WebInputEvent
;
14 using blink::WebTouchEvent
;
15 using blink::WebTouchPoint
;
16 using std::numeric_limits
;
21 class WebInputEventTraitsTest
: public testing::Test
{
23 static WebTouchPoint
CreateTouchPoint(WebTouchPoint::State state
, int id
) {
30 static WebTouchEvent
CreateTouch(WebInputEvent::Type type
) {
31 return CreateTouch(type
, 1);
34 static WebTouchEvent
CreateTouch(WebInputEvent::Type type
,
35 unsigned touch_count
) {
37 event
.touchesLength
= touch_count
;
42 static WebGestureEvent
CreateGesture(WebInputEvent::Type type
,
45 WebGestureEvent event
;
53 TEST_F(WebInputEventTraitsTest
, TouchEventCoalescing
) {
54 WebTouchEvent touch0
= CreateTouch(WebInputEvent::TouchStart
);
55 WebTouchEvent touch1
= CreateTouch(WebInputEvent::TouchMove
);
57 // Non touch-moves won't coalesce.
58 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0
, touch0
));
60 // Touches of different types won't coalesce.
61 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0
, touch1
));
63 // Touch moves with idential touch lengths and touch ids should coalesce.
64 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(touch1
, touch1
));
66 // Touch moves with different touch ids should not coalesce.
67 touch0
= CreateTouch(WebInputEvent::TouchMove
);
68 touch1
= CreateTouch(WebInputEvent::TouchMove
);
69 touch0
.touches
[0].id
= 7;
70 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0
, touch1
));
71 touch0
= CreateTouch(WebInputEvent::TouchMove
, 2);
72 touch1
= CreateTouch(WebInputEvent::TouchMove
, 2);
73 touch0
.touches
[0].id
= 1;
74 touch1
.touches
[0].id
= 0;
75 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0
, touch1
));
77 // Touch moves with different touch lengths should not coalesce.
78 touch0
= CreateTouch(WebInputEvent::TouchMove
, 1);
79 touch1
= CreateTouch(WebInputEvent::TouchMove
, 2);
80 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0
, touch1
));
82 // Touch moves with identical touch ids in different orders should coalesce.
83 touch0
= CreateTouch(WebInputEvent::TouchMove
, 2);
84 touch1
= CreateTouch(WebInputEvent::TouchMove
, 2);
85 touch0
.touches
[0] = touch1
.touches
[1] =
86 CreateTouchPoint(WebTouchPoint::StateMoved
, 1);
87 touch0
.touches
[1] = touch1
.touches
[0] =
88 CreateTouchPoint(WebTouchPoint::StateMoved
, 0);
89 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(touch0
, touch1
));
91 // Pointers with the same ID's should coalesce.
92 touch0
= CreateTouch(WebInputEvent::TouchMove
, 2);
93 touch1
= CreateTouch(WebInputEvent::TouchMove
, 2);
94 touch0
.touches
[0] = touch1
.touches
[1] =
95 CreateTouchPoint(WebTouchPoint::StateMoved
, 1);
96 WebInputEventTraits::Coalesce(touch0
, &touch1
);
97 ASSERT_EQ(1, touch1
.touches
[0].id
);
98 ASSERT_EQ(0, touch1
.touches
[1].id
);
99 EXPECT_EQ(WebTouchPoint::StateUndefined
, touch1
.touches
[1].state
);
100 EXPECT_EQ(WebTouchPoint::StateMoved
, touch1
.touches
[0].state
);
102 // Movement from now-stationary pointers should be preserved.
103 touch0
= touch1
= CreateTouch(WebInputEvent::TouchMove
, 2);
104 touch0
.touches
[0] = CreateTouchPoint(WebTouchPoint::StateMoved
, 1);
105 touch1
.touches
[1] = CreateTouchPoint(WebTouchPoint::StateStationary
, 1);
106 touch0
.touches
[1] = CreateTouchPoint(WebTouchPoint::StateStationary
, 0);
107 touch1
.touches
[0] = CreateTouchPoint(WebTouchPoint::StateMoved
, 0);
108 WebInputEventTraits::Coalesce(touch0
, &touch1
);
109 ASSERT_EQ(1, touch1
.touches
[0].id
);
110 ASSERT_EQ(0, touch1
.touches
[1].id
);
111 EXPECT_EQ(WebTouchPoint::StateMoved
, touch1
.touches
[0].state
);
112 EXPECT_EQ(WebTouchPoint::StateMoved
, touch1
.touches
[1].state
);
115 TEST_F(WebInputEventTraitsTest
, PinchEventCoalescing
) {
116 WebGestureEvent pinch0
=
117 CreateGesture(WebInputEvent::GesturePinchBegin
, 1, 1);
118 WebGestureEvent pinch1
=
119 CreateGesture(WebInputEvent::GesturePinchUpdate
, 2, 2);
121 // Only GesturePinchUpdate's coalesce.
122 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(pinch0
, pinch0
));
124 // Pinch gestures of different types should not coalesce.
125 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(pinch0
, pinch1
));
127 // Pinches with different focal points should not coalesce.
128 pinch0
= CreateGesture(WebInputEvent::GesturePinchUpdate
, 1, 1);
129 pinch1
= CreateGesture(WebInputEvent::GesturePinchUpdate
, 2, 2);
130 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(pinch0
, pinch1
));
131 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0
, pinch0
));
133 // Coalesced scales are multiplicative.
134 pinch0
= CreateGesture(WebInputEvent::GesturePinchUpdate
, 1, 1);
135 pinch0
.data
.pinchUpdate
.scale
= 2.f
;
136 pinch1
= CreateGesture(WebInputEvent::GesturePinchUpdate
, 1, 1);
137 pinch1
.data
.pinchUpdate
.scale
= 3.f
;
138 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0
, pinch0
));
139 WebInputEventTraits::Coalesce(pinch0
, &pinch1
);
140 EXPECT_EQ(2.f
* 3.f
, pinch1
.data
.pinchUpdate
.scale
);
142 // Scales have a minimum value and can never reach 0.
143 ASSERT_GT(numeric_limits
<float>::min(), 0);
144 pinch0
= CreateGesture(WebInputEvent::GesturePinchUpdate
, 1, 1);
145 pinch0
.data
.pinchUpdate
.scale
= numeric_limits
<float>::min() * 2.0f
;
146 pinch1
= CreateGesture(WebInputEvent::GesturePinchUpdate
, 1, 1);
147 pinch1
.data
.pinchUpdate
.scale
= numeric_limits
<float>::min() * 5.0f
;
148 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0
, pinch1
));
149 WebInputEventTraits::Coalesce(pinch0
, &pinch1
);
150 EXPECT_EQ(numeric_limits
<float>::min(), pinch1
.data
.pinchUpdate
.scale
);
152 // Scales have a maximum value and can never reach Infinity.
153 pinch0
= CreateGesture(WebInputEvent::GesturePinchUpdate
, 1, 1);
154 pinch0
.data
.pinchUpdate
.scale
= numeric_limits
<float>::max() / 2.0f
;
155 pinch1
= CreateGesture(WebInputEvent::GesturePinchUpdate
, 1, 1);
156 pinch1
.data
.pinchUpdate
.scale
= 10.0f
;
157 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0
, pinch1
));
158 WebInputEventTraits::Coalesce(pinch0
, &pinch1
);
159 EXPECT_EQ(numeric_limits
<float>::max(), pinch1
.data
.pinchUpdate
.scale
);
163 } // namespace content