Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(321)

Side by Side Diff: ui/touch_selection/touch_selection_controller_unittest.cc

Issue 1087893003: Support longpress drag selection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/touch_selection/touch_selection_controller.h" 5 #include "ui/touch_selection/touch_selection_controller.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "testing/gmock/include/gmock/gmock.h" 9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/events/test/motion_event_test_utils.h" 11 #include "ui/events/test/motion_event_test_utils.h"
12 12
13 using base::TimeTicks;
13 using testing::ElementsAre; 14 using testing::ElementsAre;
14 using testing::IsEmpty; 15 using testing::IsEmpty;
15 using ui::test::MockMotionEvent; 16 using ui::test::MockMotionEvent;
16 17
17 namespace ui { 18 namespace ui {
18 namespace { 19 namespace {
19 20
20 const int kDefaultTapTimeoutMs = 200; 21 const int kDefaultTapTimeoutMs = 200;
21 const float kDefaulTapSlop = 10.f; 22 const float kDefaulTapSlop = 10.f;
22 23
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 start_bound.set_type(SelectionBound::LEFT); 142 start_bound.set_type(SelectionBound::LEFT);
142 end_bound.set_type(SelectionBound::RIGHT); 143 end_bound.set_type(SelectionBound::RIGHT);
143 start_bound.SetEdge(start_rect.origin(), start_rect.bottom_left()); 144 start_bound.SetEdge(start_rect.origin(), start_rect.bottom_left());
144 end_bound.SetEdge(end_rect.origin(), end_rect.bottom_left()); 145 end_bound.SetEdge(end_rect.origin(), end_rect.bottom_left());
145 start_bound.set_visible(start_visible); 146 start_bound.set_visible(start_visible);
146 end_bound.set_visible(end_visible); 147 end_bound.set_visible(end_visible);
147 controller_->OnSelectionBoundsChanged(start_bound, end_bound); 148 controller_->OnSelectionBoundsChanged(start_bound, end_bound);
148 } 149 }
149 150
150 void Animate() { 151 void Animate() {
151 base::TimeTicks now = base::TimeTicks::Now(); 152 TimeTicks now = TimeTicks::Now();
152 while (needs_animate_) { 153 while (needs_animate_) {
153 needs_animate_ = controller_->Animate(now); 154 needs_animate_ = controller_->Animate(now);
154 now += base::TimeDelta::FromMilliseconds(16); 155 now += base::TimeDelta::FromMilliseconds(16);
155 } 156 }
156 } 157 }
157 158
158 bool GetAndResetNeedsAnimate() { 159 bool GetAndResetNeedsAnimate() {
159 bool needs_animate = needs_animate_; 160 bool needs_animate = needs_animate_;
160 Animate(); 161 Animate();
161 return needs_animate; 162 return needs_animate;
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 ChangeInsertion(insertion_rect, visible); 279 ChangeInsertion(insertion_rect, visible);
279 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_SHOWN)); 280 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_SHOWN));
280 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventStart()); 281 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventStart());
281 282
282 // Reset the selection. 283 // Reset the selection.
283 controller().HideAndDisallowShowingAutomatically(); 284 controller().HideAndDisallowShowingAutomatically();
284 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_CLEARED)); 285 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_CLEARED));
285 286
286 // Long-pressing should show the handle even if the editable region is empty. 287 // Long-pressing should show the handle even if the editable region is empty.
287 insertion_rect.Offset(2, -2); 288 insertion_rect.Offset(2, -2);
288 controller().OnLongPressEvent(); 289 controller().OnLongPressEvent(TimeTicks::Now(), insertion_rect.bottom_left());
289 controller().OnSelectionEmpty(true); 290 controller().OnSelectionEmpty(true);
290 ChangeInsertion(insertion_rect, visible); 291 ChangeInsertion(insertion_rect, visible);
291 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_SHOWN)); 292 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_SHOWN));
292 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventStart()); 293 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventStart());
293 294
294 // Single Tap on an empty edit field should clear insertion handle. 295 // Single Tap on an empty edit field should clear insertion handle.
295 controller().OnTapEvent(); 296 controller().OnTapEvent();
296 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_CLEARED)); 297 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_CLEARED));
297 } 298 }
298 299
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 337
337 // If the user taps the *same* position as the cursor at the end of the text 338 // If the user taps the *same* position as the cursor at the end of the text
338 // entry, the handle should appear. 339 // entry, the handle should appear.
339 controller().OnTapEvent(); 340 controller().OnTapEvent();
340 ChangeInsertion(insertion_rect, visible); 341 ChangeInsertion(insertion_rect, visible);
341 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_SHOWN)); 342 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_SHOWN));
342 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventStart()); 343 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventStart());
343 } 344 }
344 345
345 TEST_F(TouchSelectionControllerTest, InsertionToSelectionTransition) { 346 TEST_F(TouchSelectionControllerTest, InsertionToSelectionTransition) {
346 controller().OnLongPressEvent(); 347 controller().OnLongPressEvent(TimeTicks::Now(), gfx::PointF());
347 controller().OnSelectionEditable(true); 348 controller().OnSelectionEditable(true);
348 349
349 gfx::RectF start_rect(5, 5, 0, 10); 350 gfx::RectF start_rect(5, 5, 0, 10);
350 gfx::RectF end_rect(50, 5, 0, 10); 351 gfx::RectF end_rect(50, 5, 0, 10);
351 bool visible = true; 352 bool visible = true;
352 353
353 ChangeInsertion(start_rect, visible); 354 ChangeInsertion(start_rect, visible);
354 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_SHOWN)); 355 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_SHOWN));
355 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart()); 356 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart());
356 357
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_TAPPED, 520 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_TAPPED,
520 INSERTION_DRAG_STOPPED)); 521 INSERTION_DRAG_STOPPED));
521 EXPECT_EQ(anchor_rect.bottom_left(), GetLastEventStart()); 522 EXPECT_EQ(anchor_rect.bottom_left(), GetLastEventStart());
522 523
523 anchor_rect.Offset(5, 15); 524 anchor_rect.Offset(5, 15);
524 ChangeInsertion(anchor_rect, visible); 525 ChangeInsertion(anchor_rect, visible);
525 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_MOVED)); 526 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_MOVED));
526 EXPECT_EQ(anchor_rect.bottom_left(), GetLastEventStart()); 527 EXPECT_EQ(anchor_rect.bottom_left(), GetLastEventStart());
527 528
528 // Pressing shouldn't reset the active insertion point. 529 // Pressing shouldn't reset the active insertion point.
529 controller().OnLongPressEvent(); 530 controller().OnLongPressEvent(TimeTicks::Now(), gfx::PointF());
530 controller().OnSelectionEmpty(true); 531 controller().OnSelectionEmpty(true);
531 event = MockMotionEvent(MockMotionEvent::ACTION_DOWN, event_time, 0, 0); 532 event = MockMotionEvent(MockMotionEvent::ACTION_DOWN, event_time, 0, 0);
532 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); 533 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
533 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_DRAG_STARTED)); 534 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_DRAG_STARTED));
534 EXPECT_EQ(anchor_rect.bottom_left(), GetLastEventStart()); 535 EXPECT_EQ(anchor_rect.bottom_left(), GetLastEventStart());
535 536
536 event = MockMotionEvent(MockMotionEvent::ACTION_UP, event_time, 0, 0); 537 event = MockMotionEvent(MockMotionEvent::ACTION_UP, event_time, 0, 0);
537 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); 538 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
538 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_TAPPED, 539 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_TAPPED,
539 INSERTION_DRAG_STOPPED)); 540 INSERTION_DRAG_STOPPED));
540 EXPECT_EQ(anchor_rect.bottom_left(), GetLastEventStart()); 541 EXPECT_EQ(anchor_rect.bottom_left(), GetLastEventStart());
541 } 542 }
542 543
543 TEST_F(TouchSelectionControllerTest, SelectionBasic) { 544 TEST_F(TouchSelectionControllerTest, SelectionBasic) {
544 gfx::RectF start_rect(5, 5, 0, 10); 545 gfx::RectF start_rect(5, 5, 0, 10);
545 gfx::RectF end_rect(50, 5, 0, 10); 546 gfx::RectF end_rect(50, 5, 0, 10);
546 bool visible = true; 547 bool visible = true;
547 548
548 // Selection events are ignored until automatic showing is enabled. 549 // Selection events are ignored until automatic showing is enabled.
549 ChangeSelection(start_rect, visible, end_rect, visible); 550 ChangeSelection(start_rect, visible, end_rect, visible);
550 EXPECT_EQ(gfx::PointF(), GetLastEventStart()); 551 EXPECT_EQ(gfx::PointF(), GetLastEventStart());
551 552
552 controller().OnLongPressEvent(); 553 controller().OnLongPressEvent(TimeTicks::Now(), start_rect.bottom_left());
553 ChangeSelection(start_rect, visible, end_rect, visible); 554 ChangeSelection(start_rect, visible, end_rect, visible);
554 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN)); 555 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN));
555 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart()); 556 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart());
556 557
557 start_rect.Offset(1, 0); 558 start_rect.Offset(1, 0);
558 ChangeSelection(start_rect, visible, end_rect, visible); 559 ChangeSelection(start_rect, visible, end_rect, visible);
559 // Selection movement does not currently trigger a separate event. 560 // Selection movement does not currently trigger a separate event.
560 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_MOVED)); 561 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_MOVED));
561 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart()); 562 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart());
562 EXPECT_EQ(end_rect.bottom_left(), GetLastEventEnd()); 563 EXPECT_EQ(end_rect.bottom_left(), GetLastEventEnd());
563 564
564 ClearSelection(); 565 ClearSelection();
565 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_CLEARED)); 566 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_CLEARED));
566 EXPECT_EQ(gfx::PointF(), GetLastEventStart()); 567 EXPECT_EQ(gfx::PointF(), GetLastEventStart());
567 } 568 }
568 569
569 TEST_F(TouchSelectionControllerTest, SelectionRepeatedLongPress) { 570 TEST_F(TouchSelectionControllerTest, SelectionRepeatedLongPress) {
570 gfx::RectF start_rect(5, 5, 0, 10); 571 gfx::RectF start_rect(5, 5, 0, 10);
571 gfx::RectF end_rect(50, 5, 0, 10); 572 gfx::RectF end_rect(50, 5, 0, 10);
572 bool visible = true; 573 bool visible = true;
573 574
574 controller().OnLongPressEvent(); 575 controller().OnLongPressEvent(TimeTicks::Now(), start_rect.bottom_left());
575 ChangeSelection(start_rect, visible, end_rect, visible); 576 ChangeSelection(start_rect, visible, end_rect, visible);
576 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN)); 577 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN));
577 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart()); 578 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart());
578 EXPECT_EQ(end_rect.bottom_left(), GetLastEventEnd()); 579 EXPECT_EQ(end_rect.bottom_left(), GetLastEventEnd());
579 580
580 // A long press triggering a new selection should re-send the SELECTION_SHOWN 581 // A long press triggering a new selection should re-send the SELECTION_SHOWN
581 // event notification. 582 // event notification.
582 start_rect.Offset(10, 10); 583 start_rect.Offset(10, 10);
583 controller().OnLongPressEvent(); 584 controller().OnLongPressEvent(TimeTicks::Now(), start_rect.bottom_left());
584 ChangeSelection(start_rect, visible, end_rect, visible); 585 ChangeSelection(start_rect, visible, end_rect, visible);
585 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN)); 586 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN));
586 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart()); 587 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart());
587 EXPECT_EQ(end_rect.bottom_left(), GetLastEventEnd()); 588 EXPECT_EQ(end_rect.bottom_left(), GetLastEventEnd());
588 } 589 }
589 590
590 TEST_F(TouchSelectionControllerTest, SelectionDragged) { 591 TEST_F(TouchSelectionControllerTest, SelectionDragged) {
591 base::TimeTicks event_time = base::TimeTicks::Now(); 592 base::TimeTicks event_time = base::TimeTicks::Now();
592 controller().OnLongPressEvent(); 593 controller().OnLongPressEvent(event_time, gfx::PointF());
593 594
594 // The touch sequence should not be handled if selection is not active. 595 // The touch sequence should not be handled if selection is not active.
595 MockMotionEvent event(MockMotionEvent::ACTION_DOWN, event_time, 0, 0); 596 MockMotionEvent event(MockMotionEvent::ACTION_DOWN, event_time, 0, 0);
596 EXPECT_FALSE(controller().WillHandleTouchEvent(event)); 597 EXPECT_FALSE(controller().WillHandleTouchEvent(event));
597 598
598 float line_height = 10.f; 599 float line_height = 10.f;
599 gfx::RectF start_rect(0, 0, 0, line_height); 600 gfx::RectF start_rect(0, 0, 0, line_height);
600 gfx::RectF end_rect(50, 0, 0, line_height); 601 gfx::RectF end_rect(50, 0, 0, line_height);
601 bool visible = true; 602 bool visible = true;
602 ChangeSelection(start_rect, visible, end_rect, visible); 603 ChangeSelection(start_rect, visible, end_rect, visible);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
640 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_DRAG_STOPPED)); 641 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_DRAG_STOPPED));
641 EXPECT_FALSE(GetAndResetSelectionMoved()); 642 EXPECT_FALSE(GetAndResetSelectionMoved());
642 643
643 // Once the drag is complete, no more touch events should be consumed until 644 // Once the drag is complete, no more touch events should be consumed until
644 // the next ACTION_DOWN. 645 // the next ACTION_DOWN.
645 EXPECT_FALSE(controller().WillHandleTouchEvent(event)); 646 EXPECT_FALSE(controller().WillHandleTouchEvent(event));
646 } 647 }
647 648
648 TEST_F(TouchSelectionControllerTest, SelectionDraggedWithOverlap) { 649 TEST_F(TouchSelectionControllerTest, SelectionDraggedWithOverlap) {
649 base::TimeTicks event_time = base::TimeTicks::Now(); 650 base::TimeTicks event_time = base::TimeTicks::Now();
650 controller().OnLongPressEvent(); 651 controller().OnLongPressEvent(event_time, gfx::PointF());
651 652
652 float line_height = 10.f; 653 float line_height = 10.f;
653 gfx::RectF start_rect(0, 0, 0, line_height); 654 gfx::RectF start_rect(0, 0, 0, line_height);
654 gfx::RectF end_rect(50, 0, 0, line_height); 655 gfx::RectF end_rect(50, 0, 0, line_height);
655 bool visible = true; 656 bool visible = true;
656 ChangeSelection(start_rect, visible, end_rect, visible); 657 ChangeSelection(start_rect, visible, end_rect, visible);
657 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN)); 658 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN));
658 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart()); 659 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart());
659 660
660 // The ACTION_DOWN should lock to the closest handle. 661 // The ACTION_DOWN should lock to the closest handle.
(...skipping 17 matching lines...) Expand all
678 GetLastSelectionEnd()); 679 GetLastSelectionEnd());
679 680
680 event = MockMotionEvent(MockMotionEvent::ACTION_UP, event_time, 0, 0); 681 event = MockMotionEvent(MockMotionEvent::ACTION_UP, event_time, 0, 0);
681 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); 682 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
682 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_DRAG_STOPPED)); 683 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_DRAG_STOPPED));
683 EXPECT_FALSE(GetAndResetSelectionMoved()); 684 EXPECT_FALSE(GetAndResetSelectionMoved());
684 } 685 }
685 686
686 TEST_F(TouchSelectionControllerTest, SelectionDraggedToSwitchBaseAndExtent) { 687 TEST_F(TouchSelectionControllerTest, SelectionDraggedToSwitchBaseAndExtent) {
687 base::TimeTicks event_time = base::TimeTicks::Now(); 688 base::TimeTicks event_time = base::TimeTicks::Now();
688 controller().OnLongPressEvent(); 689 controller().OnLongPressEvent(event_time, gfx::PointF());
689 690
690 float line_height = 10.f; 691 float line_height = 10.f;
691 gfx::RectF start_rect(50, line_height, 0, line_height); 692 gfx::RectF start_rect(50, line_height, 0, line_height);
692 gfx::RectF end_rect(100, line_height, 0, line_height); 693 gfx::RectF end_rect(100, line_height, 0, line_height);
693 bool visible = true; 694 bool visible = true;
694 ChangeSelection(start_rect, visible, end_rect, visible); 695 ChangeSelection(start_rect, visible, end_rect, visible);
695 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN)); 696 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN));
696 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart()); 697 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart());
697 698
698 SetDraggingEnabled(true); 699 SetDraggingEnabled(true);
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
797 EXPECT_EQ(extent_offset + gfx::Vector2dF(0, 5), GetLastSelectionEnd()); 798 EXPECT_EQ(extent_offset + gfx::Vector2dF(0, 5), GetLastSelectionEnd());
798 799
799 event = MockMotionEvent(MockMotionEvent::ACTION_UP, event_time, 10, 5); 800 event = MockMotionEvent(MockMotionEvent::ACTION_UP, event_time, 10, 5);
800 EXPECT_TRUE(controller().WillHandleTouchEvent(event)); 801 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
801 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_DRAG_STOPPED)); 802 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_DRAG_STOPPED));
802 EXPECT_FALSE(GetAndResetSelectionMoved()); 803 EXPECT_FALSE(GetAndResetSelectionMoved());
803 } 804 }
804 805
805 TEST_F(TouchSelectionControllerTest, SelectionDragExtremeLineSize) { 806 TEST_F(TouchSelectionControllerTest, SelectionDragExtremeLineSize) {
806 base::TimeTicks event_time = base::TimeTicks::Now(); 807 base::TimeTicks event_time = base::TimeTicks::Now();
807 controller().OnLongPressEvent(); 808 controller().OnLongPressEvent(event_time, gfx::PointF());
808 809
809 float small_line_height = 1.f; 810 float small_line_height = 1.f;
810 float large_line_height = 50.f; 811 float large_line_height = 50.f;
811 gfx::RectF small_line_rect(0, 0, 0, small_line_height); 812 gfx::RectF small_line_rect(0, 0, 0, small_line_height);
812 gfx::RectF large_line_rect(50, 50, 0, large_line_height); 813 gfx::RectF large_line_rect(50, 50, 0, large_line_height);
813 bool visible = true; 814 bool visible = true;
814 ChangeSelection(small_line_rect, visible, large_line_rect, visible); 815 ChangeSelection(small_line_rect, visible, large_line_rect, visible);
815 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN)); 816 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN));
816 EXPECT_EQ(small_line_rect.bottom_left(), GetLastEventStart()); 817 EXPECT_EQ(small_line_rect.bottom_left(), GetLastEventStart());
817 818
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
869 870
870 TEST_F(TouchSelectionControllerTest, TemporarilyHidden) { 871 TEST_F(TouchSelectionControllerTest, TemporarilyHidden) {
871 controller().OnTapEvent(); 872 controller().OnTapEvent();
872 controller().OnSelectionEditable(true); 873 controller().OnSelectionEditable(true);
873 874
874 gfx::RectF insertion_rect(5, 5, 0, 10); 875 gfx::RectF insertion_rect(5, 5, 0, 10);
875 876
876 bool visible = true; 877 bool visible = true;
877 ChangeInsertion(insertion_rect, visible); 878 ChangeInsertion(insertion_rect, visible);
878 EXPECT_FALSE(GetAndResetNeedsAnimate()); 879 EXPECT_FALSE(GetAndResetNeedsAnimate());
880 EXPECT_TRUE(controller().GetStartVisibleForTesting());
881 EXPECT_TRUE(controller().GetEndVisibleForTesting());
879 882
880 controller().SetTemporarilyHidden(true); 883 controller().SetTemporarilyHidden(true);
881 EXPECT_TRUE(GetAndResetNeedsAnimate()); 884 EXPECT_TRUE(GetAndResetNeedsAnimate());
885 EXPECT_FALSE(controller().GetStartVisibleForTesting());
886 EXPECT_FALSE(controller().GetEndVisibleForTesting());
882 887
883 visible = false; 888 visible = false;
884 ChangeInsertion(insertion_rect, visible); 889 ChangeInsertion(insertion_rect, visible);
885 EXPECT_FALSE(GetAndResetNeedsAnimate()); 890 EXPECT_FALSE(GetAndResetNeedsAnimate());
891 EXPECT_FALSE(controller().GetStartVisibleForTesting());
886 892
887 visible = true; 893 visible = true;
888 ChangeInsertion(insertion_rect, visible); 894 ChangeInsertion(insertion_rect, visible);
889 EXPECT_FALSE(GetAndResetNeedsAnimate()); 895 EXPECT_FALSE(GetAndResetNeedsAnimate());
896 EXPECT_FALSE(controller().GetStartVisibleForTesting());
890 897
891 controller().SetTemporarilyHidden(false); 898 controller().SetTemporarilyHidden(false);
892 EXPECT_TRUE(GetAndResetNeedsAnimate()); 899 EXPECT_TRUE(GetAndResetNeedsAnimate());
900 EXPECT_TRUE(controller().GetStartVisibleForTesting());
893 } 901 }
894 902
895 TEST_F(TouchSelectionControllerTest, SelectionClearOnTap) { 903 TEST_F(TouchSelectionControllerTest, SelectionClearOnTap) {
896 gfx::RectF start_rect(5, 5, 0, 10); 904 gfx::RectF start_rect(5, 5, 0, 10);
897 gfx::RectF end_rect(50, 5, 0, 10); 905 gfx::RectF end_rect(50, 5, 0, 10);
898 bool visible = true; 906 bool visible = true;
899 907
900 controller().OnLongPressEvent(); 908 controller().OnLongPressEvent(TimeTicks::Now(), start_rect.bottom_left());
901 ChangeSelection(start_rect, visible, end_rect, visible); 909 ChangeSelection(start_rect, visible, end_rect, visible);
902 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN)); 910 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN));
903 911
904 // Selection should not be cleared if the selection bounds have not changed. 912 // Selection should not be cleared if the selection bounds have not changed.
905 controller().OnTapEvent(); 913 controller().OnTapEvent();
906 EXPECT_THAT(GetAndResetEvents(), IsEmpty()); 914 EXPECT_THAT(GetAndResetEvents(), IsEmpty());
907 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart()); 915 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart());
908 916
909 controller().OnTapEvent(); 917 controller().OnTapEvent();
910 ClearSelection(); 918 ClearSelection();
911 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_CLEARED)); 919 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_CLEARED));
912 EXPECT_EQ(gfx::PointF(), GetLastEventStart()); 920 EXPECT_EQ(gfx::PointF(), GetLastEventStart());
913 } 921 }
914 922
915 TEST_F(TouchSelectionControllerTest, NoSelectionAfterLongpressThenTap) { 923 TEST_F(TouchSelectionControllerTest, NoSelectionAfterLongpressThenTap) {
916 gfx::RectF start_rect(5, 5, 0, 10); 924 gfx::RectF start_rect(5, 5, 0, 10);
917 gfx::RectF end_rect(50, 5, 0, 10); 925 gfx::RectF end_rect(50, 5, 0, 10);
918 bool visible = true; 926 bool visible = true;
919 927
920 // Tap-triggered selections should not be allowed. 928 // Tap-triggered selections should not be allowed.
921 controller().OnLongPressEvent(); 929 controller().OnLongPressEvent(TimeTicks::Now(), start_rect.bottom_left());
922 controller().OnTapEvent(); 930 controller().OnTapEvent();
923 ChangeSelection(start_rect, visible, end_rect, visible); 931 ChangeSelection(start_rect, visible, end_rect, visible);
924 EXPECT_THAT(GetAndResetEvents(), IsEmpty()); 932 EXPECT_THAT(GetAndResetEvents(), IsEmpty());
925 933
926 // Subsequent longpress selections will be allowed. 934 // Subsequent longpress selections will be allowed.
927 controller().OnLongPressEvent(); 935 controller().OnLongPressEvent(TimeTicks::Now(), start_rect.bottom_left());
928 ChangeSelection(start_rect, visible, end_rect, visible); 936 ChangeSelection(start_rect, visible, end_rect, visible);
929 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN)); 937 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN));
930 938
931 // Tapping again shouldn't have any effect on subsequent selection events. 939 // Tapping again shouldn't have any effect on subsequent selection events.
932 controller().OnTapEvent(); 940 controller().OnTapEvent();
933 end_rect.Offset(10, 10); 941 end_rect.Offset(10, 10);
934 ChangeSelection(start_rect, visible, end_rect, visible); 942 ChangeSelection(start_rect, visible, end_rect, visible);
935 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_MOVED)); 943 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_MOVED));
936 ClearSelection(); 944 ClearSelection();
937 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_CLEARED)); 945 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_CLEARED));
938 } 946 }
939 947
940 TEST_F(TouchSelectionControllerTest, AllowShowingFromCurrentSelection) { 948 TEST_F(TouchSelectionControllerTest, AllowShowingFromCurrentSelection) {
941 gfx::RectF start_rect(5, 5, 0, 10); 949 gfx::RectF start_rect(5, 5, 0, 10);
942 gfx::RectF end_rect(50, 5, 0, 10); 950 gfx::RectF end_rect(50, 5, 0, 10);
943 bool visible = true; 951 bool visible = true;
944 952
945 // The selection should not be activated, as it wasn't yet allowed. 953 // The selection should not be activated, as it wasn't yet allowed.
946 ChangeSelection(start_rect, visible, end_rect, visible); 954 ChangeSelection(start_rect, visible, end_rect, visible);
947 EXPECT_EQ(gfx::PointF(), GetLastEventStart()); 955 EXPECT_EQ(gfx::PointF(), GetLastEventStart());
948 956
949 // A longpress should have no immediate effect. 957 // A longpress should have no immediate effect.
950 controller().OnLongPressEvent(); 958 controller().OnLongPressEvent(TimeTicks::Now(), start_rect.bottom_left());
951 959
952 // Now explicitly allow showing from the previously supplied bounds. 960 // Now explicitly allow showing from the previously supplied bounds.
953 controller().AllowShowingFromCurrentSelection(); 961 controller().AllowShowingFromCurrentSelection();
954 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN)); 962 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN));
955 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart()); 963 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart());
956 964
957 // Repeated calls to show from the current selection should be ignored. 965 // Repeated calls to show from the current selection should be ignored.
958 controller().AllowShowingFromCurrentSelection(); 966 controller().AllowShowingFromCurrentSelection();
959 EXPECT_THAT(GetAndResetEvents(), IsEmpty()); 967 EXPECT_THAT(GetAndResetEvents(), IsEmpty());
960 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart()); 968 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart());
961 969
962 // Trying to show from an empty selection will have no result. 970 // Trying to show from an empty selection will have no result.
963 ClearSelection(); 971 ClearSelection();
964 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_CLEARED)); 972 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_CLEARED));
965 controller().AllowShowingFromCurrentSelection(); 973 controller().AllowShowingFromCurrentSelection();
966 EXPECT_THAT(GetAndResetEvents(), IsEmpty()); 974 EXPECT_THAT(GetAndResetEvents(), IsEmpty());
967 975
968 // Showing the insertion handle should also be supported. 976 // Showing the insertion handle should also be supported.
969 controller().OnSelectionEditable(true); 977 controller().OnSelectionEditable(true);
970 controller().OnSelectionEmpty(false); 978 controller().OnSelectionEmpty(false);
971 controller().HideAndDisallowShowingAutomatically(); 979 controller().HideAndDisallowShowingAutomatically();
972 gfx::RectF insertion_rect(5, 5, 0, 10); 980 gfx::RectF insertion_rect(5, 5, 0, 10);
973 ChangeInsertion(insertion_rect, visible); 981 ChangeInsertion(insertion_rect, visible);
974 controller().AllowShowingFromCurrentSelection(); 982 controller().AllowShowingFromCurrentSelection();
975 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_SHOWN)); 983 EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_SHOWN));
976 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventStart()); 984 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventStart());
977 } 985 }
978 986
987 TEST_F(TouchSelectionControllerTest, LongPressDrag) {
mfomitchev 2015/05/13 20:52:25 Can we also add a test that the handles reappear i
jdduke (slow) 2015/05/14 18:29:27 Done.
988 gfx::RectF start_rect(-50, 0, 0, 10);
989 gfx::RectF end_rect(50, 0, 0, 10);
990 bool visible = true;
991
992 // Start a touch sequence.
993 MockMotionEvent event;
994 EXPECT_FALSE(controller().WillHandleTouchEvent(event.PressPoint(0, 0)));
995
996 // Activate a longpress-triggered selection.
997 controller().OnLongPressEvent(event.GetEventTime(), gfx::PointF());
998 ChangeSelection(start_rect, visible, end_rect, visible);
999 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_SHOWN));
1000 EXPECT_EQ(start_rect.bottom_left(), GetLastEventStart());
1001
1002 // The handles should remain invisible while the touch release and longpress
1003 // drag gesture are pending.
1004 EXPECT_FALSE(controller().GetStartVisibleForTesting());
1005 EXPECT_FALSE(controller().GetEndVisibleForTesting());
1006
1007 // The selection coordinates should reflect the drag movement.
1008 gfx::PointF fixed_offset = start_rect.CenterPoint();
1009 gfx::PointF end_offset = end_rect.CenterPoint();
1010 EXPECT_TRUE(controller().WillHandleTouchEvent(event.MovePoint(0, 0, 0)));
1011 EXPECT_THAT(GetAndResetEvents(), IsEmpty());
1012
1013 EXPECT_TRUE(controller().WillHandleTouchEvent(event.MovePoint(0, 0, 10)));
mfomitchev 2015/05/13 20:52:25 Can we use kDefaulTapSlop instead of ints here?
jdduke (slow) 2015/05/14 18:29:27 Done.
1014 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_DRAG_STARTED));
1015 EXPECT_EQ(fixed_offset, GetLastSelectionStart());
1016 EXPECT_EQ(end_offset, GetLastSelectionEnd());
1017
1018 // Movement after the start of drag will be relative to the moved endpoint.
1019 EXPECT_TRUE(controller().WillHandleTouchEvent(event.MovePoint(0, 0, 20)));
1020 EXPECT_TRUE(GetAndResetSelectionMoved());
1021 EXPECT_EQ(end_offset + gfx::Vector2dF(0, 10), GetLastSelectionEnd());
1022
1023 EXPECT_TRUE(controller().WillHandleTouchEvent(event.MovePoint(0, 10, 20)));
1024 EXPECT_TRUE(GetAndResetSelectionMoved());
1025 EXPECT_EQ(end_offset + gfx::Vector2dF(10, 10), GetLastSelectionEnd());
1026
1027 EXPECT_TRUE(controller().WillHandleTouchEvent(event.MovePoint(0, 20, 20)));
1028 EXPECT_TRUE(GetAndResetSelectionMoved());
1029 EXPECT_EQ(end_offset + gfx::Vector2dF(20, 10), GetLastSelectionEnd());
1030
1031 // The handles should still be hidden.
1032 EXPECT_FALSE(controller().GetStartVisibleForTesting());
1033 EXPECT_FALSE(controller().GetEndVisibleForTesting());
1034
1035 // Releasing the touch sequence should end the drag and show the handles.
1036 EXPECT_FALSE(controller().WillHandleTouchEvent(event.ReleasePoint()));
1037 EXPECT_THAT(GetAndResetEvents(), ElementsAre(SELECTION_DRAG_STOPPED));
1038 EXPECT_TRUE(controller().GetStartVisibleForTesting());
1039 EXPECT_TRUE(controller().GetEndVisibleForTesting());
1040 }
1041
979 } // namespace ui 1042 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698