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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/touch_selection/longpress_drag_selector.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/events/test/motion_event_test_utils.h"
using ui::test::MockMotionEvent;
namespace ui {
namespace {
const double kSlop = 10.;
class LongPressDragSelectorTest : public testing::Test,
public LongPressDragSelectorClient {
public:
LongPressDragSelectorTest()
: dragging_(false), active_state_changed_(false) {}
~LongPressDragSelectorTest() override {}
void SetSelection(const gfx::PointF& start, const gfx::PointF& end) {
selection_start_ = start;
selection_end_ = end;
}
bool GetAndResetActiveStateChanged() {
bool active_state_changed = active_state_changed_;
active_state_changed_ = false;
return active_state_changed;
}
bool IsDragging() const { return dragging_; }
const gfx::PointF& DragPosition() const { return drag_position_; }
// LongPressDragSelectorClient implementation.
void OnDragBegin(const TouchSelectionDraggable& handler,
const gfx::PointF& drag_position) override {
dragging_ = true;
drag_position_ = drag_position;
}
void OnDragUpdate(const TouchSelectionDraggable& handler,
const gfx::PointF& drag_position) override {
drag_position_ = drag_position;
}
void OnDragEnd(const TouchSelectionDraggable& handler) override {
dragging_ = false;
}
bool IsWithinTapSlop(const gfx::Vector2dF& delta) const override {
return delta.LengthSquared() < (kSlop * kSlop);
}
void OnLongPressDragActiveStateChanged() override {
active_state_changed_ = true;
}
gfx::PointF GetSelectionStart() const override { return selection_start_; }
gfx::PointF GetSelectionEnd() const override { return selection_end_; }
private:
bool dragging_;
bool active_state_changed_;
gfx::PointF drag_position_;
gfx::PointF selection_start_;
gfx::PointF selection_end_;
};
TEST_F(LongPressDragSelectorTest, BasicDrag) {
LongPressDragSelector selector(this);
MockMotionEvent event;
// Start a touch sequence.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.PressPoint(0, 0)));
EXPECT_FALSE(GetAndResetActiveStateChanged());
// Activate a longpress-triggered selection.
gfx::PointF selection_start(0, 10);
gfx::PointF selection_end(10, 10);
selector.OnLongPressEvent(event.GetEventTime(), gfx::PointF());
EXPECT_FALSE(GetAndResetActiveStateChanged());
// Motion should not be consumed until a selection is detected.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, 0)));
SetSelection(selection_start, selection_end);
selector.OnSelectionActivated();
EXPECT_TRUE(GetAndResetActiveStateChanged());
EXPECT_FALSE(IsDragging());
// Initiate drag motion. Note that the first move event after activation is
// used to initialize the drag start anchor.
EXPECT_TRUE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, 0)));
EXPECT_FALSE(IsDragging());
// The first slop exceeding motion will start the drag. As the motion is
// downward, the end selection point should be moved.
EXPECT_TRUE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, kSlop)));
EXPECT_TRUE(IsDragging());
EXPECT_EQ(selection_end, DragPosition());
// Subsequent motion will extend the selection.
EXPECT_TRUE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, kSlop * 2)));
EXPECT_TRUE(IsDragging());
EXPECT_EQ(selection_end + gfx::Vector2dF(0, kSlop), DragPosition());
EXPECT_TRUE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, kSlop * 3)));
EXPECT_TRUE(IsDragging());
EXPECT_EQ(selection_end + gfx::Vector2dF(0, kSlop * 2), DragPosition());
// Release the touch sequence, ending the drag. The selector will never
// consume the start/end events, only move events after a longpress.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.ReleasePoint()));
EXPECT_FALSE(IsDragging());
EXPECT_TRUE(GetAndResetActiveStateChanged());
}
TEST_F(LongPressDragSelectorTest, DoublePressDrag) {
LongPressDragSelector selector(this);
MockMotionEvent event;
// Start a touch sequence.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.PressPoint(0, 0)));
EXPECT_FALSE(GetAndResetActiveStateChanged());
// Activate a double press triggered selection.
constexpr gfx::PointF selection_start(0, 10);
constexpr gfx::PointF selection_end(10, 10);
selector.OnDoublePressEvent(event.GetEventTime(), gfx::PointF());
EXPECT_FALSE(GetAndResetActiveStateChanged());
// Motion should not be consumed until a selection is detected.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, 0)));
SetSelection(selection_start, selection_end);
selector.OnSelectionActivated();
EXPECT_TRUE(GetAndResetActiveStateChanged());
EXPECT_FALSE(IsDragging());
// Initiate drag motion. Note that the first move event after activation is
// used to initialize the drag start anchor.
EXPECT_TRUE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, 0)));
EXPECT_FALSE(IsDragging());
// The first slop exceeding motion will start the drag. As the motion is
// downward, the end selection point should be moved.
EXPECT_TRUE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, kSlop)));
EXPECT_TRUE(IsDragging());
EXPECT_EQ(selection_end, DragPosition());
// Subsequent motion will extend the selection.
EXPECT_TRUE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, kSlop * 2)));
EXPECT_TRUE(IsDragging());
EXPECT_EQ(selection_end + gfx::Vector2dF(0, kSlop), DragPosition());
EXPECT_TRUE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, kSlop * 3)));
EXPECT_TRUE(IsDragging());
EXPECT_EQ(selection_end + gfx::Vector2dF(0, kSlop * 2), DragPosition());
// Release the touch sequence, ending the drag. The selector will never
// consume the start/end events, only move events after a double press.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.ReleasePoint()));
EXPECT_FALSE(IsDragging());
EXPECT_TRUE(GetAndResetActiveStateChanged());
}
TEST_F(LongPressDragSelectorTest, BasicReverseDrag) {
LongPressDragSelector selector(this);
MockMotionEvent event;
// Start a touch sequence.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.PressPoint(0, 0)));
EXPECT_FALSE(GetAndResetActiveStateChanged());
// Activate a longpress-triggered selection.
gfx::PointF selection_start(0, 10);
gfx::PointF selection_end(10, 10);
selector.OnLongPressEvent(event.GetEventTime(), gfx::PointF());
EXPECT_FALSE(GetAndResetActiveStateChanged());
SetSelection(selection_start, selection_end);
selector.OnSelectionActivated();
EXPECT_TRUE(GetAndResetActiveStateChanged());
EXPECT_FALSE(IsDragging());
// Initiate drag motion.
EXPECT_TRUE(selector.WillHandleTouchEvent(event.MovePoint(0, 5, 0)));
EXPECT_FALSE(IsDragging());
// As the initial motion is leftward, toward the selection start, the
// selection start should be the drag point.
EXPECT_TRUE(selector.WillHandleTouchEvent(event.MovePoint(0, -kSlop, 0)));
EXPECT_TRUE(IsDragging());
EXPECT_EQ(selection_start, DragPosition());
EXPECT_TRUE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, -kSlop)));
EXPECT_TRUE(IsDragging());
EXPECT_EQ(selection_start + gfx::Vector2dF(kSlop, -kSlop), DragPosition());
// Release the touch sequence, ending the drag.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.ReleasePoint()));
EXPECT_FALSE(IsDragging());
EXPECT_TRUE(GetAndResetActiveStateChanged());
}
TEST_F(LongPressDragSelectorTest, NoActiveTouch) {
LongPressDragSelector selector(this);
MockMotionEvent event;
// Activate a longpress-triggered selection.
gfx::PointF selection_start(0, 10);
gfx::PointF selection_end(10, 10);
selector.OnLongPressEvent(event.GetEventTime(), gfx::PointF());
SetSelection(selection_start, selection_end);
selector.OnSelectionActivated();
EXPECT_FALSE(GetAndResetActiveStateChanged());
EXPECT_FALSE(IsDragging());
// Start a new touch sequence; it shouldn't initiate selection drag as there
// was no active touch sequence when the longpress selection started.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.PressPoint(0, 0)));
EXPECT_FALSE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, 0)));
EXPECT_FALSE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, kSlop)));
EXPECT_FALSE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, kSlop * 2)));
EXPECT_FALSE(IsDragging());
EXPECT_EQ(gfx::PointF(), DragPosition());
}
TEST_F(LongPressDragSelectorTest, NoLongPress) {
LongPressDragSelector selector(this);
MockMotionEvent event;
// Start a touch sequence.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.PressPoint(0, 0)));
EXPECT_FALSE(GetAndResetActiveStateChanged());
// Activate a selection without a preceding longpress.
gfx::PointF selection_start(0, 10);
gfx::PointF selection_end(10, 10);
SetSelection(selection_start, selection_end);
selector.OnSelectionActivated();
EXPECT_FALSE(GetAndResetActiveStateChanged());
EXPECT_FALSE(IsDragging());
// Touch movement should not initiate selection drag.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, 0)));
EXPECT_FALSE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, kSlop)));
EXPECT_FALSE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, kSlop * 2)));
EXPECT_FALSE(IsDragging());
EXPECT_EQ(gfx::PointF(), DragPosition());
}
TEST_F(LongPressDragSelectorTest, NoValidLongPress) {
LongPressDragSelector selector(this);
MockMotionEvent event;
// Start a touch sequence.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.PressPoint(0, 0)));
EXPECT_FALSE(GetAndResetActiveStateChanged());
gfx::PointF selection_start(0, 10);
gfx::PointF selection_end(10, 10);
SetSelection(selection_start, selection_end);
// Activate a longpress-triggered selection, but at a time before the current
// touch down event.
selector.OnLongPressEvent(event.GetEventTime() - base::Seconds(1),
gfx::PointF());
selector.OnSelectionActivated();
EXPECT_FALSE(GetAndResetActiveStateChanged());
EXPECT_FALSE(IsDragging());
// Activate a longpress-triggered selection, but at a place different than the
// current touch down event.
selector.OnLongPressEvent(event.GetEventTime(), gfx::PointF(kSlop, 0));
selector.OnSelectionActivated();
EXPECT_FALSE(GetAndResetActiveStateChanged());
EXPECT_FALSE(IsDragging());
// Touch movement should not initiate selection drag.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, 0)));
EXPECT_FALSE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, kSlop)));
EXPECT_FALSE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, kSlop * 2)));
EXPECT_FALSE(IsDragging());
EXPECT_EQ(gfx::PointF(), DragPosition());
}
TEST_F(LongPressDragSelectorTest, NoSelection) {
LongPressDragSelector selector(this);
MockMotionEvent event;
// Start a touch sequence.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.PressPoint(0, 0)));
EXPECT_FALSE(GetAndResetActiveStateChanged());
// Trigger a longpress.
selector.OnLongPressEvent(event.GetEventTime(), gfx::PointF());
EXPECT_FALSE(GetAndResetActiveStateChanged());
EXPECT_FALSE(IsDragging());
// Touch movement should not initiate selection drag, as there is no active
// selection.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, 0)));
EXPECT_FALSE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, kSlop)));
EXPECT_FALSE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, kSlop * 2)));
EXPECT_FALSE(IsDragging());
EXPECT_EQ(gfx::PointF(), DragPosition());
EXPECT_FALSE(selector.WillHandleTouchEvent(event.ReleasePoint()));
}
TEST_F(LongPressDragSelectorTest, NoDragMotion) {
LongPressDragSelector selector(this);
MockMotionEvent event;
// Start a touch sequence.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.PressPoint(0, 0)));
EXPECT_FALSE(GetAndResetActiveStateChanged());
// Activate a longpress-triggered selection.
selector.OnLongPressEvent(event.GetEventTime(), gfx::PointF());
EXPECT_FALSE(GetAndResetActiveStateChanged());
gfx::PointF selection_start(0, 10);
gfx::PointF selection_end(10, 10);
SetSelection(selection_start, selection_end);
selector.OnSelectionActivated();
EXPECT_TRUE(GetAndResetActiveStateChanged());
EXPECT_FALSE(IsDragging());
// Touch movement within the slop region should not initiate selection drag.
EXPECT_TRUE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, 0)));
EXPECT_TRUE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, kSlop / 2)));
EXPECT_TRUE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, -kSlop / 2)));
EXPECT_FALSE(IsDragging());
EXPECT_EQ(gfx::PointF(), DragPosition());
EXPECT_FALSE(selector.WillHandleTouchEvent(event.ReleasePoint()));
EXPECT_TRUE(GetAndResetActiveStateChanged());
}
TEST_F(LongPressDragSelectorTest, SelectionDeactivated) {
LongPressDragSelector selector(this);
MockMotionEvent event;
// Start a touch sequence.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.PressPoint(0, 0)));
EXPECT_FALSE(GetAndResetActiveStateChanged());
// Activate a longpress-triggered selection.
selector.OnLongPressEvent(event.GetEventTime(), gfx::PointF());
EXPECT_FALSE(GetAndResetActiveStateChanged());
gfx::PointF selection_start(0, 10);
gfx::PointF selection_end(10, 10);
SetSelection(selection_start, selection_end);
selector.OnSelectionActivated();
EXPECT_TRUE(GetAndResetActiveStateChanged());
EXPECT_FALSE(IsDragging());
// Start a drag selection.
EXPECT_TRUE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, 0)));
EXPECT_TRUE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, kSlop)));
EXPECT_TRUE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, kSlop * 2)));
EXPECT_TRUE(IsDragging());
// Clearing the selection should force an end to the drag.
selector.OnSelectionDeactivated();
EXPECT_TRUE(GetAndResetActiveStateChanged());
EXPECT_FALSE(IsDragging());
// Subsequent motion should not be consumed.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, 0)));
EXPECT_FALSE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, kSlop)));
EXPECT_FALSE(selector.WillHandleTouchEvent(event.MovePoint(0, 0, kSlop * 2)));
EXPECT_FALSE(IsDragging());
EXPECT_FALSE(selector.WillHandleTouchEvent(event.ReleasePoint()));
}
TEST_F(LongPressDragSelectorTest, DragFast) {
LongPressDragSelector selector(this);
MockMotionEvent event;
// Start a touch sequence.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.PressPoint(0, 0)));
EXPECT_FALSE(GetAndResetActiveStateChanged());
// Activate a longpress-triggered selection.
gfx::PointF selection_start(0, 10);
gfx::PointF selection_end(10, 10);
selector.OnLongPressEvent(event.GetEventTime(), gfx::PointF());
EXPECT_FALSE(GetAndResetActiveStateChanged());
SetSelection(selection_start, selection_end);
selector.OnSelectionActivated();
EXPECT_TRUE(GetAndResetActiveStateChanged());
EXPECT_FALSE(IsDragging());
// Initiate drag motion.
EXPECT_TRUE(selector.WillHandleTouchEvent(event.MovePoint(0, 15, 5)));
EXPECT_FALSE(IsDragging());
// As the initial motion exceeds both endpoints, the closer bound should
// be used for dragging, in this case the selection end.
EXPECT_TRUE(selector.WillHandleTouchEvent(
event.MovePoint(0, 15.f + kSlop * 2.f, 5.f + kSlop)));
EXPECT_TRUE(IsDragging());
EXPECT_EQ(selection_end, DragPosition());
// Release the touch sequence, ending the drag.
EXPECT_FALSE(selector.WillHandleTouchEvent(event.ReleasePoint()));
EXPECT_FALSE(IsDragging());
EXPECT_TRUE(GetAndResetActiveStateChanged());
}
TEST_F(LongPressDragSelectorTest, ScrollAfterLongPress) {
LongPressDragSelector selector(this);
MockMotionEvent event;
gfx::PointF touch_point(0, 0);
// Start a touch sequence.
EXPECT_FALSE(selector.WillHandleTouchEvent(
event.PressPoint(touch_point.x(), touch_point.y())));
// Long-press and hold down.
selector.OnLongPressEvent(event.GetEventTime(), touch_point);
// Scroll the page. This should cancel long-press drag gesture.
touch_point.Offset(0, 2 * kSlop);
EXPECT_FALSE(selector.WillHandleTouchEvent(
event.MovePoint(0, touch_point.x(), touch_point.y())));
selector.OnScrollBeginEvent();
// Now if the selection is activated, because long-press drag gesture was
// canceled, active state of the long-press selector should not change.
selector.OnSelectionActivated();
EXPECT_FALSE(GetAndResetActiveStateChanged());
// Release the touch sequence.
selector.WillHandleTouchEvent(event.ReleasePoint());
}
} // namespace
} // namespace ui
|