Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 <string> | |
| 6 #include <vector> | |
| 7 | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/time/time.h" | |
| 10 #include "content/browser/renderer_host/input/gesture_text_selector.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 #include "ui/events/event_constants.h" | |
| 13 #include "ui/events/gesture_detection/gesture_event_data.h" | |
| 14 #include "ui/events/gesture_detection/mock_motion_event.h" | |
| 15 #include "ui/events/gesture_detection/motion_event.h" | |
| 16 #include "ui/gfx/geometry/rect_f.h" | |
| 17 | |
| 18 using ui::GestureEventData; | |
| 19 using ui::GestureEventDetails; | |
| 20 using ui::MockMotionEvent; | |
| 21 using ui::MotionEvent; | |
| 22 | |
| 23 namespace content { | |
| 24 | |
| 25 class GestureTextSelectorTest : public testing::Test, | |
| 26 public GestureTextSelectorClient { | |
| 27 public: | |
| 28 GestureTextSelectorTest() {} | |
| 29 virtual ~GestureTextSelectorTest() {} | |
| 30 | |
| 31 // Test implementation. | |
| 32 virtual void SetUp() OVERRIDE { | |
| 33 selector_.reset(new GestureTextSelector(this)); | |
| 34 event_log_.clear(); | |
| 35 } | |
| 36 | |
| 37 virtual void TearDown() OVERRIDE { | |
| 38 selector_.reset(); | |
| 39 event_log_.clear(); | |
| 40 } | |
| 41 | |
| 42 // GestureTextSelectorClient implementation. | |
| 43 virtual void ShowSelectionHandlesAutomatically() OVERRIDE { | |
| 44 event_log_.push_back("Show"); | |
| 45 } | |
| 46 | |
| 47 virtual void SelectRange(float x1, float y1, float x2, float y2) OVERRIDE { | |
| 48 event_log_.push_back("SelectRange"); | |
| 49 } | |
| 50 | |
| 51 virtual void SelectWord(float x, float y) OVERRIDE { | |
| 52 event_log_.push_back("SelectWord"); | |
| 53 } | |
| 54 | |
| 55 virtual void Unselect() OVERRIDE { | |
| 56 event_log_.push_back("Unselect"); | |
| 57 } | |
| 58 | |
| 59 protected: | |
| 60 scoped_ptr<GestureTextSelector> selector_; | |
| 61 std::vector<std::string> event_log_; | |
| 62 }; | |
| 63 | |
| 64 TEST_F(GestureTextSelectorTest, ShouldStartTextSelection) { | |
| 65 base::TimeTicks event_time = base::TimeTicks::Now(); | |
| 66 { // Touched with a finger. | |
| 67 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f); | |
| 68 e.SetToolType(0, MotionEvent::TOOL_TYPE_FINGER); | |
| 69 e.SetButtonState(0); | |
| 70 EXPECT_FALSE(selector_->ShouldStartTextSelection(e)); | |
| 71 } | |
| 72 | |
| 73 { // Touched with a stylus, but no button pressed. | |
| 74 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f); | |
| 75 e.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | |
| 76 e.SetButtonState(0); | |
| 77 EXPECT_FALSE(selector_->ShouldStartTextSelection(e)); | |
| 78 } | |
| 79 | |
| 80 { // Touched with a stylus, with first button (BUTTON_SECONDARY) pressed. | |
| 81 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f); | |
| 82 e.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | |
| 83 e.SetButtonState(MotionEvent::BUTTON_SECONDARY); | |
| 84 EXPECT_TRUE(selector_->ShouldStartTextSelection(e)); | |
| 85 } | |
| 86 | |
| 87 { // Touched with a stylus, with two buttons pressed. | |
|
jdduke (slow)
2014/06/24 15:38:37
Do you know what the SPen does in this case? Will
Changwan Ryu
2014/06/25 07:26:21
SPen has only one button. We may want to assign so
| |
| 88 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f); | |
| 89 e.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | |
| 90 e.SetButtonState( | |
| 91 MotionEvent::BUTTON_SECONDARY | MotionEvent::BUTTON_TERTIARY); | |
| 92 EXPECT_FALSE(selector_->ShouldStartTextSelection(e)); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 TEST_F(GestureTextSelectorTest, EventConsumption) { | |
| 97 base::TimeTicks event_time = base::TimeTicks::Now(); | |
| 98 const float x1 = 50.0f; | |
| 99 const float y1 = 30.0f; | |
| 100 const float x2 = 100.0f; | |
| 101 const float y2 = 90.0f; | |
| 102 const GestureEventData long_press( | |
| 103 GestureEventDetails(ui::ET_GESTURE_LONG_PRESS, 0, 0), 0, event_time, | |
| 104 x2, y2, x2, y2, 1, gfx::RectF(0, 0, 0, 0)); | |
| 105 const GestureEventData double_tap( | |
| 106 GestureEventDetails(ui::ET_GESTURE_DOUBLE_TAP, 0, 0), 0, event_time, | |
| 107 x2, y2, x2, y2, 1, gfx::RectF(0, 0, 0, 0)); | |
| 108 // 0. Touched with a finger: ignored | |
| 109 MockMotionEvent finger(MotionEvent::ACTION_DOWN, event_time, x1, y1); | |
| 110 finger.SetToolType(0, MotionEvent::TOOL_TYPE_FINGER); | |
| 111 EXPECT_FALSE(selector_->OnTouchEvent(finger)); | |
| 112 // We do not consume finger events. | |
| 113 EXPECT_FALSE(selector_->OnGestureEvent(long_press)); | |
| 114 EXPECT_FALSE(selector_->OnGestureEvent(double_tap)); | |
| 115 EXPECT_TRUE(event_log_.empty()); | |
| 116 | |
| 117 // 1. ACTION_DOWN with stylus + button | |
| 118 event_time += base::TimeDelta::FromMilliseconds(10); | |
| 119 MockMotionEvent action_down(MotionEvent::ACTION_DOWN, event_time, x1, y1); | |
| 120 action_down.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | |
| 121 action_down.SetButtonState(MotionEvent::BUTTON_SECONDARY); | |
| 122 EXPECT_TRUE(selector_->OnTouchEvent(action_down)); | |
| 123 EXPECT_EQ(2, event_log_.size()); // Show, Unselect | |
| 124 EXPECT_STREQ("Unselect", event_log_.back().c_str()); | |
| 125 | |
| 126 // 2. ACTION_MOVE | |
| 127 event_time += base::TimeDelta::FromMilliseconds(10); | |
| 128 MockMotionEvent action_move(MotionEvent::ACTION_MOVE, event_time, x2, y2); | |
| 129 action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | |
| 130 action_move.SetButtonState(MotionEvent::BUTTON_SECONDARY); | |
| 131 EXPECT_TRUE(selector_->OnTouchEvent(action_move)); | |
| 132 // Consume and suppress events when in text selection mode (except LONG PRESS | |
| 133 // and some other events). | |
| 134 EXPECT_TRUE(selector_->OnGestureEvent(double_tap)); | |
| 135 EXPECT_EQ(4, event_log_.size()); // Show, Unselect, Show, SelectRange | |
| 136 EXPECT_STREQ("SelectRange", event_log_.back().c_str()); | |
| 137 | |
| 138 // 3. ACTION_UP | |
| 139 event_time += base::TimeDelta::FromMilliseconds(10); | |
| 140 MockMotionEvent action_up(MotionEvent::ACTION_UP, event_time, x2, y2); | |
| 141 action_up.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | |
| 142 action_up.SetButtonState(0); | |
| 143 EXPECT_TRUE(selector_->OnTouchEvent(action_up)); | |
| 144 // Show, Unselect, Show, SelectRange, Show, SelectRange | |
| 145 EXPECT_EQ(6, event_log_.size()); | |
| 146 EXPECT_STREQ("SelectRange", event_log_.back().c_str()); | |
| 147 | |
| 148 // 4. LONG_PRESS (This can occur before or after ACTION_UP.) | |
|
jdduke (slow)
2014/06/24 15:38:37
Hmm, LONG_PRESS should only ever occur before ACTI
Changwan Ryu
2014/06/25 07:26:21
Removed as LONG_PRESS requirement is still in talk
| |
| 149 event_time += base::TimeDelta::FromMilliseconds(10); | |
| 150 EXPECT_TRUE(selector_->OnGestureEvent(long_press)); | |
| 151 // Show, Unselect, Show, SelectRange, Show, SelectRange, SelectWord | |
| 152 EXPECT_EQ(7, event_log_.size()); | |
| 153 EXPECT_STREQ("SelectWord", event_log_.back().c_str()); | |
| 154 } | |
| 155 | |
| 156 } // namespace content | |
| OLD | NEW |