1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/public/common/input/web_input_event.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/input/web_gesture_event.h"
#include "third_party/blink/public/common/input/web_mouse_wheel_event.h"
#include "third_party/blink/public/common/input/web_pointer_event.h"
namespace blink {
namespace {
WebMouseEvent CreateWebMouseMoveEvent() {
WebMouseEvent mouse_event;
mouse_event.SetType(WebInputEvent::Type::kMouseMove);
mouse_event.id = 1;
mouse_event.pointer_type = WebPointerProperties::PointerType::kMouse;
return mouse_event;
}
WebPointerEvent CreateWebPointerMoveEvent() {
WebPointerEvent pointer_event;
pointer_event.SetType(WebInputEvent::Type::kPointerMove);
pointer_event.id = 1;
pointer_event.pointer_type = WebPointerProperties::PointerType::kMouse;
return pointer_event;
}
WebTouchEvent CreateWebTouchMoveEvent() {
WebTouchPoint touch_point;
touch_point.id = 1;
touch_point.state = WebTouchPoint::State::kStateMoved;
touch_point.pointer_type = WebPointerProperties::PointerType::kTouch;
WebTouchEvent touch_event;
touch_event.SetType(WebInputEvent::Type::kTouchMove);
touch_event.touches[touch_event.touches_length++] = touch_point;
return touch_event;
}
} // namespace
TEST(WebInputEventTest, TouchEventCoalescing) {
WebTouchEvent coalesced_event = CreateWebTouchMoveEvent();
coalesced_event.SetType(WebInputEvent::Type::kTouchMove);
coalesced_event.touches[0].movement_x = 5;
coalesced_event.touches[0].movement_y = 10;
WebTouchEvent event_to_be_coalesced = CreateWebTouchMoveEvent();
event_to_be_coalesced.touches[0].movement_x = 3;
event_to_be_coalesced.touches[0].movement_y = -4;
EXPECT_TRUE(coalesced_event.CanCoalesce(event_to_be_coalesced));
coalesced_event.Coalesce(event_to_be_coalesced);
EXPECT_EQ(8, coalesced_event.touches[0].movement_x);
EXPECT_EQ(6, coalesced_event.touches[0].movement_y);
coalesced_event.touches[0].pointer_type =
WebPointerProperties::PointerType::kPen;
EXPECT_FALSE(coalesced_event.CanCoalesce(event_to_be_coalesced));
coalesced_event = CreateWebTouchMoveEvent();
event_to_be_coalesced = CreateWebTouchMoveEvent();
event_to_be_coalesced.SetModifiers(WebInputEvent::kControlKey);
EXPECT_FALSE(coalesced_event.CanCoalesce(event_to_be_coalesced));
}
TEST(WebInputEventTest, WebMouseWheelEventCoalescing) {
WebMouseWheelEvent coalesced_event(
WebInputEvent::Type::kMouseWheel, WebInputEvent::kNoModifiers,
WebInputEvent::GetStaticTimeStampForTests());
coalesced_event.delta_x = 1;
coalesced_event.delta_y = 1;
WebMouseWheelEvent event_to_be_coalesced(
WebInputEvent::Type::kMouseWheel, WebInputEvent::kNoModifiers,
WebInputEvent::GetStaticTimeStampForTests());
event_to_be_coalesced.delta_x = 3;
event_to_be_coalesced.delta_y = 4;
EXPECT_TRUE(coalesced_event.CanCoalesce(event_to_be_coalesced));
coalesced_event.Coalesce(event_to_be_coalesced);
EXPECT_EQ(4, coalesced_event.delta_x);
EXPECT_EQ(5, coalesced_event.delta_y);
event_to_be_coalesced.phase = WebMouseWheelEvent::kPhaseBegan;
coalesced_event.phase = WebMouseWheelEvent::kPhaseEnded;
EXPECT_FALSE(coalesced_event.CanCoalesce(event_to_be_coalesced));
// With timer based wheel scroll latching, we break the latching sequence on
// direction change when all prior GSU events in the current sequence are
// ignored. To do so we dispatch the pending wheel event with phaseEnded and
// the first wheel event in the opposite direction will have phaseBegan. The
// GSB generated from this wheel event will cause a new hittesting. To make
// sure that a GSB will actually get created we should not coalesce the wheel
// event with synthetic kPhaseBegan to one with synthetic kPhaseEnded.
event_to_be_coalesced.has_synthetic_phase = true;
coalesced_event.has_synthetic_phase = true;
EXPECT_FALSE(coalesced_event.CanCoalesce(event_to_be_coalesced));
event_to_be_coalesced.phase = WebMouseWheelEvent::kPhaseChanged;
coalesced_event.phase = WebMouseWheelEvent::kPhaseBegan;
EXPECT_TRUE(coalesced_event.CanCoalesce(event_to_be_coalesced));
coalesced_event.Coalesce(event_to_be_coalesced);
EXPECT_EQ(WebMouseWheelEvent::kPhaseBegan, coalesced_event.phase);
EXPECT_EQ(7, coalesced_event.delta_x);
EXPECT_EQ(9, coalesced_event.delta_y);
}
TEST(WebInputEventTest, WebGestureEventCoalescing) {
WebGestureEvent coalesced_event(WebInputEvent::Type::kGestureScrollUpdate,
WebInputEvent::kNoModifiers,
WebInputEvent::GetStaticTimeStampForTests());
coalesced_event.data.scroll_update.delta_x = 1;
coalesced_event.data.scroll_update.delta_y = 1;
WebGestureEvent event_to_be_coalesced(
WebInputEvent::Type::kGestureScrollUpdate, WebInputEvent::kNoModifiers,
WebInputEvent::GetStaticTimeStampForTests());
event_to_be_coalesced.data.scroll_update.delta_x = 3;
event_to_be_coalesced.data.scroll_update.delta_y = 4;
EXPECT_TRUE(coalesced_event.CanCoalesce(event_to_be_coalesced));
coalesced_event.Coalesce(event_to_be_coalesced);
EXPECT_EQ(4, coalesced_event.data.scroll_update.delta_x);
EXPECT_EQ(5, coalesced_event.data.scroll_update.delta_y);
}
TEST(WebInputEventTest, GesturePinchUpdateCoalescing) {
gfx::PointF position(10.f, 10.f);
WebGestureEvent coalesced_event(
WebInputEvent::Type::kGesturePinchUpdate, WebInputEvent::kNoModifiers,
WebInputEvent::GetStaticTimeStampForTests(), WebGestureDevice::kTouchpad);
coalesced_event.data.pinch_update.scale = 1.1f;
coalesced_event.SetPositionInWidget(position);
WebGestureEvent event_to_be_coalesced(coalesced_event);
ASSERT_TRUE(coalesced_event.CanCoalesce(event_to_be_coalesced));
coalesced_event.Coalesce(event_to_be_coalesced);
EXPECT_FLOAT_EQ(1.21, coalesced_event.data.pinch_update.scale);
// Allow the updates to be coalesced if the anchors are nearly equal.
position.Offset(0.1f, 0.1f);
event_to_be_coalesced.SetPositionInWidget(position);
coalesced_event.data.pinch_update.scale = 1.1f;
ASSERT_TRUE(coalesced_event.CanCoalesce(event_to_be_coalesced));
coalesced_event.Coalesce(event_to_be_coalesced);
EXPECT_FLOAT_EQ(1.21, coalesced_event.data.pinch_update.scale);
// The anchors are no longer considered equal, so don't coalesce.
position.Offset(1.f, 1.f);
event_to_be_coalesced.SetPositionInWidget(position);
EXPECT_FALSE(coalesced_event.CanCoalesce(event_to_be_coalesced));
// Don't logically coalesce touchpad pinch events as touchpad pinch events
// don't occur within a gesture scroll sequence.
EXPECT_FALSE(WebGestureEvent::IsCompatibleScrollorPinch(event_to_be_coalesced,
coalesced_event));
// Touchscreen pinch events can be logically coalesced.
coalesced_event.SetSourceDevice(WebGestureDevice::kTouchscreen);
event_to_be_coalesced.SetSourceDevice(WebGestureDevice::kTouchscreen);
coalesced_event.data.pinch_update.scale = 1.1f;
ASSERT_TRUE(WebGestureEvent::IsCompatibleScrollorPinch(event_to_be_coalesced,
coalesced_event));
std::unique_ptr<WebGestureEvent> logical_scroll, logical_pinch;
std::tie(logical_scroll, logical_pinch) =
WebGestureEvent::CoalesceScrollAndPinch(nullptr, coalesced_event,
event_to_be_coalesced);
ASSERT_NE(nullptr, logical_scroll);
ASSERT_NE(nullptr, logical_pinch);
ASSERT_EQ(WebInputEvent::Type::kGestureScrollUpdate,
logical_scroll->GetType());
ASSERT_EQ(WebInputEvent::Type::kGesturePinchUpdate, logical_pinch->GetType());
EXPECT_FLOAT_EQ(1.21, logical_pinch->data.pinch_update.scale);
}
TEST(WebInputEventTest, MouseEventCoalescing) {
WebMouseEvent coalesced_event = CreateWebMouseMoveEvent();
WebMouseEvent event_to_be_coalesced = CreateWebMouseMoveEvent();
EXPECT_TRUE(coalesced_event.CanCoalesce(event_to_be_coalesced));
// Test coalescing movements.
coalesced_event.movement_x = 5;
coalesced_event.movement_y = 10;
event_to_be_coalesced.movement_x = 3;
event_to_be_coalesced.movement_y = -4;
EXPECT_TRUE(coalesced_event.CanCoalesce(event_to_be_coalesced));
coalesced_event.Coalesce(event_to_be_coalesced);
EXPECT_EQ(8, coalesced_event.movement_x);
EXPECT_EQ(6, coalesced_event.movement_y);
// Test id.
coalesced_event = CreateWebMouseMoveEvent();
event_to_be_coalesced = CreateWebMouseMoveEvent();
event_to_be_coalesced.id = 3;
EXPECT_FALSE(coalesced_event.CanCoalesce(event_to_be_coalesced));
// Test pointer_type.
coalesced_event = CreateWebMouseMoveEvent();
event_to_be_coalesced = CreateWebMouseMoveEvent();
event_to_be_coalesced.pointer_type = WebPointerProperties::PointerType::kPen;
EXPECT_FALSE(coalesced_event.CanCoalesce(event_to_be_coalesced));
// Test modifiers
coalesced_event = CreateWebMouseMoveEvent();
event_to_be_coalesced = CreateWebMouseMoveEvent();
event_to_be_coalesced.SetModifiers(WebInputEvent::kControlKey);
EXPECT_FALSE(coalesced_event.CanCoalesce(event_to_be_coalesced));
}
TEST(WebInputEventTest, PointerEventCoalescing) {
WebPointerEvent coalesced_event = CreateWebPointerMoveEvent();
WebPointerEvent event_to_be_coalesced = CreateWebPointerMoveEvent();
EXPECT_TRUE(coalesced_event.CanCoalesce(event_to_be_coalesced));
// Test coalescing movements.
coalesced_event.movement_x = 5;
coalesced_event.movement_y = 10;
event_to_be_coalesced.movement_x = 3;
event_to_be_coalesced.movement_y = -4;
EXPECT_TRUE(coalesced_event.CanCoalesce(event_to_be_coalesced));
coalesced_event.Coalesce(event_to_be_coalesced);
EXPECT_EQ(8, coalesced_event.movement_x);
EXPECT_EQ(6, coalesced_event.movement_y);
// Test id.
coalesced_event = CreateWebPointerMoveEvent();
event_to_be_coalesced = CreateWebPointerMoveEvent();
event_to_be_coalesced.id = 3;
EXPECT_FALSE(coalesced_event.CanCoalesce(event_to_be_coalesced));
// Test pointer_type.
coalesced_event = CreateWebPointerMoveEvent();
event_to_be_coalesced = CreateWebPointerMoveEvent();
event_to_be_coalesced.pointer_type = WebPointerProperties::PointerType::kPen;
EXPECT_FALSE(coalesced_event.CanCoalesce(event_to_be_coalesced));
// Test modifiers
coalesced_event = CreateWebPointerMoveEvent();
event_to_be_coalesced = CreateWebPointerMoveEvent();
event_to_be_coalesced.SetModifiers(WebInputEvent::kControlKey);
EXPECT_FALSE(coalesced_event.CanCoalesce(event_to_be_coalesced));
}
} // namespace blink
|