OLD | NEW |
---|---|
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 <sstream> | |
5 #include <string> | 6 #include <string> |
6 #include <vector> | 7 #include <vector> |
7 | 8 |
8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
9 #include "base/time/time.h" | 10 #include "base/time/time.h" |
10 #include "content/browser/renderer_host/input/gesture_text_selector.h" | 11 #include "content/browser/renderer_host/input/gesture_text_selector.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "ui/events/event_constants.h" | 13 #include "ui/events/event_constants.h" |
13 #include "ui/events/gesture_detection/motion_event.h" | 14 #include "ui/events/gesture_detection/motion_event.h" |
14 #include "ui/events/test/mock_motion_event.h" | 15 #include "ui/events/test/mock_motion_event.h" |
(...skipping 20 matching lines...) Expand all Loading... | |
35 selector_.reset(); | 36 selector_.reset(); |
36 event_log_.clear(); | 37 event_log_.clear(); |
37 } | 38 } |
38 | 39 |
39 // GestureTextSelectorClient implementation. | 40 // GestureTextSelectorClient implementation. |
40 virtual void ShowSelectionHandlesAutomatically() override { | 41 virtual void ShowSelectionHandlesAutomatically() override { |
41 event_log_.push_back("Show"); | 42 event_log_.push_back("Show"); |
42 } | 43 } |
43 | 44 |
44 virtual void SelectRange(float x1, float y1, float x2, float y2) override { | 45 virtual void SelectRange(float x1, float y1, float x2, float y2) override { |
45 event_log_.push_back("SelectRange"); | 46 event_log_.push_back("SelectRange(" + to_string(x1) + ", " + to_string(y1) + |
jdduke (slow)
2014/10/10 16:22:12
Nit: No need for a helper function here, just do:
| |
47 ", " + to_string(x2) + ", " + to_string(y2) + ")"); | |
46 } | 48 } |
47 | 49 |
48 virtual void LongPress(base::TimeTicks time, float x, float y) override { | 50 virtual void LongPress(base::TimeTicks time, float x, float y) override { |
49 event_log_.push_back("LongPress"); | 51 event_log_.push_back("LongPress"); |
50 } | 52 } |
51 | 53 |
52 protected: | 54 protected: |
53 scoped_ptr<GestureTextSelector> selector_; | 55 scoped_ptr<GestureTextSelector> selector_; |
54 std::vector<std::string> event_log_; | 56 std::vector<std::string> event_log_; |
57 | |
58 std::string to_string(float val) { | |
59 std::ostringstream os; | |
60 os << val; | |
61 return os.str(); | |
62 } | |
55 }; | 63 }; |
56 | 64 |
57 TEST_F(GestureTextSelectorTest, ShouldStartTextSelection) { | 65 TEST_F(GestureTextSelectorTest, ShouldStartTextSelection) { |
58 base::TimeTicks event_time = base::TimeTicks::Now(); | 66 base::TimeTicks event_time = base::TimeTicks::Now(); |
59 { // Touched with a finger. | 67 { // Touched with a finger. |
60 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f); | 68 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f); |
61 e.SetToolType(0, MotionEvent::TOOL_TYPE_FINGER); | 69 e.SetToolType(0, MotionEvent::TOOL_TYPE_FINGER); |
62 e.set_button_state(0); | 70 e.set_button_state(0); |
63 EXPECT_FALSE(selector_->ShouldStartTextSelection(e)); | 71 EXPECT_FALSE(selector_->ShouldStartTextSelection(e)); |
64 } | 72 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 EXPECT_TRUE(event_log_.empty()); | 121 EXPECT_TRUE(event_log_.empty()); |
114 | 122 |
115 // 2. ACTION_MOVE | 123 // 2. ACTION_MOVE |
116 event_time += base::TimeDelta::FromMilliseconds(10); | 124 event_time += base::TimeDelta::FromMilliseconds(10); |
117 MockMotionEvent action_move(MotionEvent::ACTION_MOVE, event_time, x2, y2); | 125 MockMotionEvent action_move(MotionEvent::ACTION_MOVE, event_time, x2, y2); |
118 action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | 126 action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
119 action_move.set_button_state(MotionEvent::BUTTON_SECONDARY); | 127 action_move.set_button_state(MotionEvent::BUTTON_SECONDARY); |
120 EXPECT_TRUE(selector_->OnTouchEvent(action_move)); | 128 EXPECT_TRUE(selector_->OnTouchEvent(action_move)); |
121 ASSERT_EQ(2u, event_log_.size()); | 129 ASSERT_EQ(2u, event_log_.size()); |
122 EXPECT_STREQ("Show", event_log_[0].c_str()); | 130 EXPECT_STREQ("Show", event_log_[0].c_str()); |
123 EXPECT_STREQ("SelectRange", event_log_[1].c_str()); | 131 EXPECT_STREQ("SelectRange(50, 30, 100, 90)", event_log_[1].c_str()); |
124 | 132 |
125 // 3. ACTION_UP | 133 // 3. ACTION_UP |
126 event_time += base::TimeDelta::FromMilliseconds(10); | 134 event_time += base::TimeDelta::FromMilliseconds(10); |
127 MockMotionEvent action_up(MotionEvent::ACTION_UP, event_time, x2, y2); | 135 MockMotionEvent action_up(MotionEvent::ACTION_UP, event_time, x2, y2); |
128 action_up.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | 136 action_up.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
129 action_up.set_button_state(0); | 137 action_up.set_button_state(0); |
130 EXPECT_TRUE(selector_->OnTouchEvent(action_up)); | 138 EXPECT_TRUE(selector_->OnTouchEvent(action_up)); |
131 ASSERT_EQ(2u, event_log_.size()); // NO CHANGE | 139 ASSERT_EQ(2u, event_log_.size()); // NO CHANGE |
132 } | 140 } |
133 | 141 |
142 TEST_F(GestureTextSelectorTest, PenDraggingButtonNotPressed) { | |
143 base::TimeTicks event_time = base::TimeTicks::Now(); | |
144 float x = 50.0f; | |
145 float y = 30.0f; | |
146 | |
147 // 1. ACTION_DOWN with stylus + button | |
148 event_time += base::TimeDelta::FromMilliseconds(10); | |
149 MockMotionEvent action_down(MotionEvent::ACTION_DOWN, event_time, x, y); | |
150 action_down.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | |
151 action_down.set_button_state(MotionEvent::BUTTON_SECONDARY); | |
152 EXPECT_TRUE(selector_->OnTouchEvent(action_down)); | |
153 EXPECT_TRUE(event_log_.empty()); | |
154 | |
155 // 2. ACTION_MOVE | |
156 event_time += base::TimeDelta::FromMilliseconds(10); | |
157 x += 20; // 70 | |
158 y += 20; // 50 | |
159 MockMotionEvent action_move(MotionEvent::ACTION_MOVE, event_time, x, y); | |
160 action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | |
161 action_move.set_button_state(MotionEvent::BUTTON_SECONDARY); | |
162 EXPECT_TRUE(selector_->OnTouchEvent(action_move)); | |
163 ASSERT_EQ(2u, event_log_.size()); | |
164 EXPECT_STREQ("Show", event_log_[0].c_str()); | |
165 EXPECT_STREQ("SelectRange(50, 30, 70, 50)", event_log_[1].c_str()); | |
166 | |
167 // 3. ACTION_MOVE with stylus + no button | |
168 event_time += base::TimeDelta::FromMilliseconds(10); | |
169 x += 20; // 90 | |
170 y += 20; // 70 | |
171 action_move = MockMotionEvent(MotionEvent::ACTION_MOVE, event_time, x, y); | |
172 action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | |
173 action_move.set_button_state(0); | |
174 EXPECT_TRUE(selector_->OnTouchEvent(action_move)); | |
175 EXPECT_EQ(2u, event_log_.size()); // NO CHANGE | |
176 | |
177 // 4. ACTION_MOVE with stylus + button pressed again | |
178 event_time += base::TimeDelta::FromMilliseconds(10); | |
179 x += 20; // 110 | |
180 y += 20; // 90 | |
181 action_move = MockMotionEvent(MotionEvent::ACTION_MOVE, event_time, x, y); | |
182 action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | |
183 action_move.set_button_state(MotionEvent::BUTTON_SECONDARY); | |
184 EXPECT_TRUE(selector_->OnTouchEvent(action_move)); | |
185 EXPECT_EQ(4u, event_log_.size()); | |
186 EXPECT_STREQ("SelectRange(90, 70, 110, 90)", event_log_.back().c_str()); | |
187 | |
188 // 5. ACTION_UP | |
189 event_time += base::TimeDelta::FromMilliseconds(10); | |
190 MockMotionEvent action_up(MotionEvent::ACTION_UP, event_time, x, y); | |
191 action_up.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | |
192 action_up.set_button_state(0); | |
193 EXPECT_TRUE(selector_->OnTouchEvent(action_up)); | |
194 EXPECT_EQ(4u, event_log_.size()); // NO CHANGE | |
195 } | |
196 | |
134 TEST_F(GestureTextSelectorTest, TapTriggersLongPressSelection) { | 197 TEST_F(GestureTextSelectorTest, TapTriggersLongPressSelection) { |
135 base::TimeTicks event_time = base::TimeTicks::Now(); | 198 base::TimeTicks event_time = base::TimeTicks::Now(); |
136 const float x1 = 50.0f; | 199 const float x1 = 50.0f; |
137 const float y1 = 30.0f; | 200 const float y1 = 30.0f; |
138 const float x2 = 51.0f; | 201 const float x2 = 51.0f; |
139 const float y2 = 31.0f; | 202 const float y2 = 31.0f; |
140 // 1. ACTION_DOWN with stylus + button | 203 // 1. ACTION_DOWN with stylus + button |
141 event_time += base::TimeDelta::FromMilliseconds(1); | 204 event_time += base::TimeDelta::FromMilliseconds(1); |
142 MockMotionEvent action_down(MotionEvent::ACTION_DOWN, event_time, x1, y1); | 205 MockMotionEvent action_down(MotionEvent::ACTION_DOWN, event_time, x1, y1); |
143 action_down.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | 206 action_down.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
(...skipping 13 matching lines...) Expand all Loading... | |
157 event_time += base::TimeDelta::FromMilliseconds(1); | 220 event_time += base::TimeDelta::FromMilliseconds(1); |
158 MockMotionEvent action_up(MotionEvent::ACTION_UP, event_time, x2, y2); | 221 MockMotionEvent action_up(MotionEvent::ACTION_UP, event_time, x2, y2); |
159 action_up.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | 222 action_up.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
160 action_up.set_button_state(0); | 223 action_up.set_button_state(0); |
161 EXPECT_TRUE(selector_->OnTouchEvent(action_up)); | 224 EXPECT_TRUE(selector_->OnTouchEvent(action_up)); |
162 ASSERT_EQ(1u, event_log_.size()); | 225 ASSERT_EQ(1u, event_log_.size()); |
163 EXPECT_STREQ("LongPress", event_log_.back().c_str()); | 226 EXPECT_STREQ("LongPress", event_log_.back().c_str()); |
164 } | 227 } |
165 | 228 |
166 } // namespace content | 229 } // namespace content |
OLD | NEW |