Revert of Integrate SIMD optimisations for zlib (patchset #14 id:280001 of https...
[chromium-blink-merge.git] / content / common / input / web_input_event_traits_unittest.cc
blob7a7cb7ff8b3e878eec275c8445494fbb8a6aefa4
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"
7 #include <limits>
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::WebKeyboardEvent;
15 using blink::WebMouseEvent;
16 using blink::WebMouseWheelEvent;
17 using blink::WebTouchEvent;
18 using blink::WebTouchPoint;
19 using std::numeric_limits;
21 namespace content {
22 namespace {
24 class WebInputEventTraitsTest : public testing::Test {
25 protected:
26 static WebTouchPoint CreateTouchPoint(WebTouchPoint::State state, int id) {
27 WebTouchPoint touch;
28 touch.state = state;
29 touch.id = id;
30 return touch;
33 static WebTouchEvent CreateTouch(WebInputEvent::Type type) {
34 return CreateTouch(type, 1);
37 static WebTouchEvent CreateTouch(WebInputEvent::Type type,
38 unsigned touch_count) {
39 WebTouchEvent event;
40 event.touchesLength = touch_count;
41 event.type = type;
42 return event;
45 static WebGestureEvent CreateGesture(WebInputEvent::Type type,
46 float x,
47 float y) {
48 WebGestureEvent event;
49 event.type = type;
50 event.x = x;
51 event.y = y;
52 return event;
56 TEST_F(WebInputEventTraitsTest, TouchEventCoalescing) {
57 WebTouchEvent touch0 = CreateTouch(WebInputEvent::TouchStart);
58 WebTouchEvent touch1 = CreateTouch(WebInputEvent::TouchMove);
60 // Non touch-moves won't coalesce.
61 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0, touch0));
63 // Touches of different types won't coalesce.
64 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0, touch1));
66 // Touch moves with idential touch lengths and touch ids should coalesce.
67 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(touch1, touch1));
69 // Touch moves with different touch ids should not coalesce.
70 touch0 = CreateTouch(WebInputEvent::TouchMove);
71 touch1 = CreateTouch(WebInputEvent::TouchMove);
72 touch0.touches[0].id = 7;
73 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0, touch1));
74 touch0 = CreateTouch(WebInputEvent::TouchMove, 2);
75 touch1 = CreateTouch(WebInputEvent::TouchMove, 2);
76 touch0.touches[0].id = 1;
77 touch1.touches[0].id = 0;
78 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0, touch1));
80 // Touch moves with different touch lengths should not coalesce.
81 touch0 = CreateTouch(WebInputEvent::TouchMove, 1);
82 touch1 = CreateTouch(WebInputEvent::TouchMove, 2);
83 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0, touch1));
85 // Touch moves with identical touch ids in different orders should coalesce.
86 touch0 = CreateTouch(WebInputEvent::TouchMove, 2);
87 touch1 = CreateTouch(WebInputEvent::TouchMove, 2);
88 touch0.touches[0] = touch1.touches[1] =
89 CreateTouchPoint(WebTouchPoint::StateMoved, 1);
90 touch0.touches[1] = touch1.touches[0] =
91 CreateTouchPoint(WebTouchPoint::StateMoved, 0);
92 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(touch0, touch1));
94 // Pointers with the same ID's should coalesce.
95 touch0 = CreateTouch(WebInputEvent::TouchMove, 2);
96 touch1 = CreateTouch(WebInputEvent::TouchMove, 2);
97 touch0.touches[0] = touch1.touches[1] =
98 CreateTouchPoint(WebTouchPoint::StateMoved, 1);
99 WebInputEventTraits::Coalesce(touch0, &touch1);
100 ASSERT_EQ(1, touch1.touches[0].id);
101 ASSERT_EQ(0, touch1.touches[1].id);
102 EXPECT_EQ(WebTouchPoint::StateUndefined, touch1.touches[1].state);
103 EXPECT_EQ(WebTouchPoint::StateMoved, touch1.touches[0].state);
105 // Movement from now-stationary pointers should be preserved.
106 touch0 = touch1 = CreateTouch(WebInputEvent::TouchMove, 2);
107 touch0.touches[0] = CreateTouchPoint(WebTouchPoint::StateMoved, 1);
108 touch1.touches[1] = CreateTouchPoint(WebTouchPoint::StateStationary, 1);
109 touch0.touches[1] = CreateTouchPoint(WebTouchPoint::StateStationary, 0);
110 touch1.touches[0] = CreateTouchPoint(WebTouchPoint::StateMoved, 0);
111 WebInputEventTraits::Coalesce(touch0, &touch1);
112 ASSERT_EQ(1, touch1.touches[0].id);
113 ASSERT_EQ(0, touch1.touches[1].id);
114 EXPECT_EQ(WebTouchPoint::StateMoved, touch1.touches[0].state);
115 EXPECT_EQ(WebTouchPoint::StateMoved, touch1.touches[1].state);
118 TEST_F(WebInputEventTraitsTest, PinchEventCoalescing) {
119 WebGestureEvent pinch0 =
120 CreateGesture(WebInputEvent::GesturePinchBegin, 1, 1);
121 WebGestureEvent pinch1 =
122 CreateGesture(WebInputEvent::GesturePinchUpdate, 2, 2);
124 // Only GesturePinchUpdate's coalesce.
125 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(pinch0, pinch0));
127 // Pinch gestures of different types should not coalesce.
128 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(pinch0, pinch1));
130 // Pinches with different focal points should not coalesce.
131 pinch0 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
132 pinch1 = CreateGesture(WebInputEvent::GesturePinchUpdate, 2, 2);
133 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(pinch0, pinch1));
134 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0, pinch0));
136 // Coalesced scales are multiplicative.
137 pinch0 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
138 pinch0.data.pinchUpdate.scale = 2.f;
139 pinch1 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
140 pinch1.data.pinchUpdate.scale = 3.f;
141 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0, pinch0));
142 WebInputEventTraits::Coalesce(pinch0, &pinch1);
143 EXPECT_EQ(2.f * 3.f, pinch1.data.pinchUpdate.scale);
145 // Scales have a minimum value and can never reach 0.
146 ASSERT_GT(numeric_limits<float>::min(), 0);
147 pinch0 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
148 pinch0.data.pinchUpdate.scale = numeric_limits<float>::min() * 2.0f;
149 pinch1 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
150 pinch1.data.pinchUpdate.scale = numeric_limits<float>::min() * 5.0f;
151 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0, pinch1));
152 WebInputEventTraits::Coalesce(pinch0, &pinch1);
153 EXPECT_EQ(numeric_limits<float>::min(), pinch1.data.pinchUpdate.scale);
155 // Scales have a maximum value and can never reach Infinity.
156 pinch0 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
157 pinch0.data.pinchUpdate.scale = numeric_limits<float>::max() / 2.0f;
158 pinch1 = CreateGesture(WebInputEvent::GesturePinchUpdate, 1, 1);
159 pinch1.data.pinchUpdate.scale = 10.0f;
160 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(pinch0, pinch1));
161 WebInputEventTraits::Coalesce(pinch0, &pinch1);
162 EXPECT_EQ(numeric_limits<float>::max(), pinch1.data.pinchUpdate.scale);
165 // Very basic smoke test to ensure stringification doesn't explode.
166 TEST_F(WebInputEventTraitsTest, ToString) {
167 WebKeyboardEvent key;
168 key.type = WebInputEvent::RawKeyDown;
169 EXPECT_FALSE(WebInputEventTraits::ToString(key).empty());
171 WebMouseEvent mouse;
172 mouse.type = WebInputEvent::MouseMove;
173 EXPECT_FALSE(WebInputEventTraits::ToString(mouse).empty());
175 WebMouseWheelEvent mouse_wheel;
176 mouse_wheel.type = WebInputEvent::MouseWheel;
177 EXPECT_FALSE(WebInputEventTraits::ToString(mouse_wheel).empty());
179 WebGestureEvent gesture =
180 CreateGesture(WebInputEvent::GesturePinchBegin, 1, 1);
181 EXPECT_FALSE(WebInputEventTraits::ToString(gesture).empty());
183 WebTouchEvent touch = CreateTouch(WebInputEvent::TouchStart);
184 EXPECT_FALSE(WebInputEventTraits::ToString(touch).empty());
187 } // namespace
188 } // namespace content