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. 4 5 #include "content/common/input/web_input_event_traits.h" 6 7 #include <limits> 8 9 #include "testing/gtest/include/gtest/gtest.h" 10 #include "third_party/WebKit/public/web/WebInputEvent.h" 11 12 using blink::WebGestureEvent; 13 using blink::WebInputEvent; 14 using blink::WebTouchEvent; 15 using blink::WebTouchPoint; 16 using std::numeric_limits; 17 18 namespace content { 19 namespace { 20 21 class WebInputEventTraitsTest : public testing::Test { 22 protected: 23 static WebTouchPoint CreateTouchPoint(WebTouchPoint::State state, int id) { 24 WebTouchPoint touch; 25 touch.state = state; 26 touch.id = id; 27 return touch; 28 } 29 30 static WebTouchEvent CreateTouch(WebInputEvent::Type type) { 31 return CreateTouch(type, 1); 32 } 33 34 static WebTouchEvent CreateTouch(WebInputEvent::Type type, 35 unsigned touch_count) { 36 WebTouchEvent event; 37 event.touchesLength = touch_count; 38 event.type = type; 39 return event; 40 } 41 42 static WebGestureEvent CreateGesture(WebInputEvent::Type type, 43 float x, 44 float y) { 45 WebGestureEvent event; 46 event.type = type; 47 event.x = x; 48 event.y = y; 49 return event; 50 } 51 }; 52 53 TEST_F(WebInputEventTraitsTest, TouchEventCoalescing) { 54 WebTouchEvent touch0 = CreateTouch(WebInputEvent::TouchStart); 55 WebTouchEvent touch1 = CreateTouch(WebInputEvent::TouchMove); 56 57 // Non touch-moves won't coalesce. 58 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0, touch0)); 59 60 // Touches of different types won't coalesce. 61 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(touch0, touch1)); 62 63 // Touch moves with idential touch lengths and touch ids should coalesce. 64 EXPECT_TRUE(WebInputEventTraits::CanCoalesce(touch1, touch1)); 65 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)); 76 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)); 81 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)); 90 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); 101 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); 113 } 114 115 TEST_F(WebInputEventTraitsTest, PinchEventCoalescing) { 116 WebGestureEvent pinch0 = 117 CreateGesture(WebInputEvent::GesturePinchBegin, 1, 1); 118 WebGestureEvent pinch1 = 119 CreateGesture(WebInputEvent::GesturePinchUpdate, 2, 2); 120 121 // Only GesturePinchUpdate's coalesce. 122 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(pinch0, pinch0)); 123 124 // Pinch gestures of different types should not coalesce. 125 EXPECT_FALSE(WebInputEventTraits::CanCoalesce(pinch0, pinch1)); 126 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)); 132 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); 141 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); 151 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); 160 } 161 162 } // namespace 163 } // namespace content 164