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 "content/browser/renderer_host/input/stylus_text_selector.h" | 5 #include "content/browser/renderer_host/input/stylus_text_selector.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 } | 71 } |
72 | 72 |
73 { // Touched with a stylus, but no button pressed. | 73 { // Touched with a stylus, but no button pressed. |
74 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f); | 74 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f); |
75 e.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | 75 e.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
76 e.set_button_state(0); | 76 e.set_button_state(0); |
77 EXPECT_FALSE(selector_->ShouldStartTextSelection(e)); | 77 EXPECT_FALSE(selector_->ShouldStartTextSelection(e)); |
78 } | 78 } |
79 | 79 |
80 { // Touched with a stylus, with first button (BUTTON_SECONDARY) pressed. | 80 { // Touched with a stylus, with first button (BUTTON_SECONDARY) pressed. |
| 81 // For Android version < M, this stylus state is BUTTON_SECONDARY. |
81 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f); | 82 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f); |
82 e.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | 83 e.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
83 e.set_button_state(MotionEvent::BUTTON_SECONDARY); | 84 e.set_button_state(MotionEvent::BUTTON_SECONDARY); |
84 EXPECT_TRUE(selector_->ShouldStartTextSelection(e)); | 85 EXPECT_TRUE(selector_->ShouldStartTextSelection(e)); |
85 } | 86 } |
86 | 87 |
| 88 { // Touched with a stylus, with first button (BUTTON_STYLUS_PRIMARY) |
| 89 // pressed. From Android M, this stylus state has been changed to |
| 90 // BUTTON_STYLUS_PRIMARY. |
| 91 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f); |
| 92 e.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
| 93 e.set_button_state(MotionEvent::BUTTON_STYLUS_PRIMARY); |
| 94 EXPECT_TRUE(selector_->ShouldStartTextSelection(e)); |
| 95 } |
| 96 |
87 { // Touched with a stylus, with two buttons pressed. | 97 { // Touched with a stylus, with two buttons pressed. |
| 98 // For Android version < M, these states are BUTTON_SECONDARY, |
| 99 // BUTTON_TERTIARY. |
88 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f); | 100 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f); |
89 e.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | 101 e.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
90 e.set_button_state(MotionEvent::BUTTON_SECONDARY | | 102 e.set_button_state(MotionEvent::BUTTON_SECONDARY | |
91 MotionEvent::BUTTON_TERTIARY); | 103 MotionEvent::BUTTON_TERTIARY); |
92 EXPECT_FALSE(selector_->ShouldStartTextSelection(e)); | 104 EXPECT_FALSE(selector_->ShouldStartTextSelection(e)); |
93 } | 105 } |
| 106 |
| 107 { // Touched with a stylus, with two buttons pressed. |
| 108 // From Android M, these state are BUTTON_STYLUS_PRIMARY, |
| 109 // BUTTON_STYLUS_SECONDARY. |
| 110 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f); |
| 111 e.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
| 112 e.set_button_state(MotionEvent::BUTTON_STYLUS_PRIMARY | |
| 113 MotionEvent::BUTTON_STYLUS_SECONDARY); |
| 114 EXPECT_FALSE(selector_->ShouldStartTextSelection(e)); |
| 115 } |
94 } | 116 } |
95 | 117 |
96 TEST_F(StylusTextSelectorTest, FingerTouch) { | 118 TEST_F(StylusTextSelectorTest, FingerTouch) { |
97 base::TimeTicks event_time = base::TimeTicks::Now(); | 119 base::TimeTicks event_time = base::TimeTicks::Now(); |
98 const float x = 50.0f; | 120 const float x = 50.0f; |
99 const float y = 30.0f; | 121 const float y = 30.0f; |
100 // 1. Touched with a finger: ignored | 122 // 1. Touched with a finger: ignored |
101 MockMotionEvent finger(MotionEvent::ACTION_DOWN, event_time, x, y); | 123 MockMotionEvent finger(MotionEvent::ACTION_DOWN, event_time, x, y); |
102 finger.SetToolType(0, MotionEvent::TOOL_TYPE_FINGER); | 124 finger.SetToolType(0, MotionEvent::TOOL_TYPE_FINGER); |
103 EXPECT_FALSE(selector_->OnTouchEvent(finger)); | 125 EXPECT_FALSE(selector_->OnTouchEvent(finger)); |
104 // We do not consume finger events. | 126 // We do not consume finger events. |
105 EXPECT_TRUE(event_log_.empty()); | 127 EXPECT_TRUE(event_log_.empty()); |
106 } | 128 } |
107 | 129 |
108 TEST_F(StylusTextSelectorTest, PenDragging) { | 130 // The following Tests cover BUTTON_SECONDARY case, which is the stylus button |
| 131 // pressed state for Android version < M. |
| 132 TEST_F(StylusTextSelectorTest, PenDraggingButtonSecondary) { |
109 base::TimeTicks event_time = base::TimeTicks::Now(); | 133 base::TimeTicks event_time = base::TimeTicks::Now(); |
110 const float x1 = 50.0f; | 134 const float x1 = 50.0f; |
111 const float y1 = 30.0f; | 135 const float y1 = 30.0f; |
112 const float x2 = 100.0f; | 136 const float x2 = 100.0f; |
113 const float y2 = 90.0f; | 137 const float y2 = 90.0f; |
114 const float x3 = 150.0f; | 138 const float x3 = 150.0f; |
115 const float y3 = 150.0f; | 139 const float y3 = 150.0f; |
116 // 1. ACTION_DOWN with stylus + button | 140 // 1. ACTION_DOWN with stylus + button |
117 event_time += base::TimeDelta::FromMilliseconds(10); | 141 event_time += base::TimeDelta::FromMilliseconds(10); |
118 MockMotionEvent action_down(MotionEvent::ACTION_DOWN, event_time, x1, y1); | 142 MockMotionEvent action_down(MotionEvent::ACTION_DOWN, event_time, x1, y1); |
(...skipping 22 matching lines...) Expand all Loading... |
141 // 3. ACTION_UP | 165 // 3. ACTION_UP |
142 event_time += base::TimeDelta::FromMilliseconds(10); | 166 event_time += base::TimeDelta::FromMilliseconds(10); |
143 MockMotionEvent action_up(MotionEvent::ACTION_UP, event_time, x3, y3); | 167 MockMotionEvent action_up(MotionEvent::ACTION_UP, event_time, x3, y3); |
144 action_up.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | 168 action_up.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
145 action_up.set_button_state(0); | 169 action_up.set_button_state(0); |
146 EXPECT_TRUE(selector_->OnTouchEvent(action_up)); | 170 EXPECT_TRUE(selector_->OnTouchEvent(action_up)); |
147 ASSERT_EQ(3u, event_log_.size()); // NO CHANGE | 171 ASSERT_EQ(3u, event_log_.size()); // NO CHANGE |
148 EXPECT_STREQ("End", event_log_.back().c_str()); | 172 EXPECT_STREQ("End", event_log_.back().c_str()); |
149 } | 173 } |
150 | 174 |
151 TEST_F(StylusTextSelectorTest, PenDraggingButtonNotPressed) { | 175 TEST_F(StylusTextSelectorTest, PenDraggingButtonSecondaryNotPressed) { |
152 base::TimeTicks event_time = base::TimeTicks::Now(); | 176 base::TimeTicks event_time = base::TimeTicks::Now(); |
153 float x = 50.0f; | 177 float x = 50.0f; |
154 float y = 30.0f; | 178 float y = 30.0f; |
155 | 179 |
156 // 1. ACTION_DOWN with stylus + button | 180 // 1. ACTION_DOWN with stylus + button |
157 event_time += base::TimeDelta::FromMilliseconds(10); | 181 event_time += base::TimeDelta::FromMilliseconds(10); |
158 MockMotionEvent action_down(MotionEvent::ACTION_DOWN, event_time, x, y); | 182 MockMotionEvent action_down(MotionEvent::ACTION_DOWN, event_time, x, y); |
159 action_down.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | 183 action_down.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
160 action_down.set_button_state(MotionEvent::BUTTON_SECONDARY); | 184 action_down.set_button_state(MotionEvent::BUTTON_SECONDARY); |
161 EXPECT_TRUE(selector_->OnTouchEvent(action_down)); | 185 EXPECT_TRUE(selector_->OnTouchEvent(action_down)); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 | 261 |
238 // 3. ACTION_UP | 262 // 3. ACTION_UP |
239 event_time += base::TimeDelta::FromMilliseconds(1); | 263 event_time += base::TimeDelta::FromMilliseconds(1); |
240 MockMotionEvent action_up(MotionEvent::ACTION_UP, event_time, x2, y2); | 264 MockMotionEvent action_up(MotionEvent::ACTION_UP, event_time, x2, y2); |
241 action_up.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); | 265 action_up.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
242 action_up.set_button_state(0); | 266 action_up.set_button_state(0); |
243 EXPECT_TRUE(selector_->OnTouchEvent(action_up)); | 267 EXPECT_TRUE(selector_->OnTouchEvent(action_up)); |
244 ASSERT_EQ(1u, event_log_.size()); | 268 ASSERT_EQ(1u, event_log_.size()); |
245 EXPECT_STREQ("Tap", event_log_.back().c_str()); | 269 EXPECT_STREQ("Tap", event_log_.back().c_str()); |
246 } | 270 } |
| 271 // End of Tests for BUTTON_SECONDARY case. |
| 272 |
| 273 // The following Tests cover BUTTON_STYLUS_PRIMARY case, which is the stylus |
| 274 // button pressed state from Android M. |
| 275 TEST_F(StylusTextSelectorTest, PenDraggingButtonStylusPrimary) { |
| 276 base::TimeTicks event_time = base::TimeTicks::Now(); |
| 277 const float x1 = 50.0f; |
| 278 const float y1 = 30.0f; |
| 279 const float x2 = 100.0f; |
| 280 const float y2 = 90.0f; |
| 281 const float x3 = 150.0f; |
| 282 const float y3 = 150.0f; |
| 283 // 1. ACTION_DOWN with stylus + button |
| 284 event_time += base::TimeDelta::FromMilliseconds(10); |
| 285 MockMotionEvent action_down(MotionEvent::ACTION_DOWN, event_time, x1, y1); |
| 286 action_down.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
| 287 action_down.set_button_state(MotionEvent::BUTTON_STYLUS_PRIMARY); |
| 288 EXPECT_TRUE(selector_->OnTouchEvent(action_down)); |
| 289 EXPECT_TRUE(event_log_.empty()); |
| 290 |
| 291 // 2. ACTION_MOVE |
| 292 event_time += base::TimeDelta::FromMilliseconds(10); |
| 293 MockMotionEvent action_move(MotionEvent::ACTION_MOVE, event_time, x2, y2); |
| 294 action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
| 295 action_move.set_button_state(MotionEvent::BUTTON_STYLUS_PRIMARY); |
| 296 EXPECT_TRUE(selector_->OnTouchEvent(action_move)); |
| 297 ASSERT_EQ(1u, event_log_.size()); |
| 298 EXPECT_STREQ("Begin(50, 30, 100, 90)", event_log_.back().c_str()); |
| 299 |
| 300 event_time += base::TimeDelta::FromMilliseconds(10); |
| 301 action_move = MockMotionEvent(MotionEvent::ACTION_MOVE, event_time, x3, y3); |
| 302 action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
| 303 action_move.set_button_state(MotionEvent::BUTTON_STYLUS_PRIMARY); |
| 304 EXPECT_TRUE(selector_->OnTouchEvent(action_move)); |
| 305 ASSERT_EQ(2u, event_log_.size()); |
| 306 EXPECT_STREQ("Update(150, 150)", event_log_.back().c_str()); |
| 307 |
| 308 // 3. ACTION_UP |
| 309 event_time += base::TimeDelta::FromMilliseconds(10); |
| 310 MockMotionEvent action_up(MotionEvent::ACTION_UP, event_time, x3, y3); |
| 311 action_up.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
| 312 action_up.set_button_state(0); |
| 313 EXPECT_TRUE(selector_->OnTouchEvent(action_up)); |
| 314 ASSERT_EQ(3u, event_log_.size()); // NO CHANGE |
| 315 EXPECT_STREQ("End", event_log_.back().c_str()); |
| 316 } |
| 317 |
| 318 TEST_F(StylusTextSelectorTest, PenDraggingButtonStylusPrimaryNotPressed) { |
| 319 base::TimeTicks event_time = base::TimeTicks::Now(); |
| 320 float x = 50.0f; |
| 321 float y = 30.0f; |
| 322 |
| 323 // 1. ACTION_DOWN with stylus + button |
| 324 event_time += base::TimeDelta::FromMilliseconds(10); |
| 325 MockMotionEvent action_down(MotionEvent::ACTION_DOWN, event_time, x, y); |
| 326 action_down.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
| 327 action_down.set_button_state(MotionEvent::BUTTON_STYLUS_PRIMARY); |
| 328 EXPECT_TRUE(selector_->OnTouchEvent(action_down)); |
| 329 EXPECT_TRUE(event_log_.empty()); |
| 330 |
| 331 // 2. ACTION_MOVE |
| 332 event_time += base::TimeDelta::FromMilliseconds(10); |
| 333 x += 20; // 70 |
| 334 y += 20; // 50 |
| 335 MockMotionEvent action_move(MotionEvent::ACTION_MOVE, event_time, x, y); |
| 336 action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
| 337 action_move.set_button_state(MotionEvent::BUTTON_STYLUS_PRIMARY); |
| 338 EXPECT_TRUE(selector_->OnTouchEvent(action_move)); |
| 339 ASSERT_EQ(1u, event_log_.size()); |
| 340 EXPECT_STREQ("Begin(50, 30, 70, 50)", event_log_.back().c_str()); |
| 341 |
| 342 // 3. ACTION_MOVE with stylus + no button |
| 343 event_time += base::TimeDelta::FromMilliseconds(10); |
| 344 x += 20; // 90 |
| 345 y += 20; // 70 |
| 346 action_move = MockMotionEvent(MotionEvent::ACTION_MOVE, event_time, x, y); |
| 347 action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
| 348 action_move.set_button_state(0); |
| 349 EXPECT_TRUE(selector_->OnTouchEvent(action_move)); |
| 350 EXPECT_EQ(1u, event_log_.size()); // NO CHANGE |
| 351 |
| 352 // 4. ACTION_MOVE with stylus + button pressed again |
| 353 // Note that the end action is deferred until the stylus is lifted. |
| 354 event_time += base::TimeDelta::FromMilliseconds(10); |
| 355 x += 20; // 110 |
| 356 y += 20; // 90 |
| 357 action_move = MockMotionEvent(MotionEvent::ACTION_MOVE, event_time, x, y); |
| 358 action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
| 359 action_move.set_button_state(MotionEvent::BUTTON_STYLUS_PRIMARY); |
| 360 EXPECT_TRUE(selector_->OnTouchEvent(action_move)); |
| 361 EXPECT_EQ(2u, event_log_.size()); |
| 362 EXPECT_STREQ("Begin(90, 70, 110, 90)", event_log_.back().c_str()); |
| 363 |
| 364 // 5. ACTION_MOVE with stylus + no button |
| 365 event_time += base::TimeDelta::FromMilliseconds(10); |
| 366 x += 20; // 130 |
| 367 y += 20; // 110 |
| 368 action_move = MockMotionEvent(MotionEvent::ACTION_MOVE, event_time, x, y); |
| 369 action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
| 370 EXPECT_TRUE(selector_->OnTouchEvent(action_move)); |
| 371 EXPECT_EQ(2u, event_log_.size()); // NO CHANGE |
| 372 |
| 373 // 5. ACTION_UP |
| 374 event_time += base::TimeDelta::FromMilliseconds(10); |
| 375 MockMotionEvent action_up(MotionEvent::ACTION_UP, event_time, x, y); |
| 376 action_up.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
| 377 action_up.set_button_state(0); |
| 378 EXPECT_TRUE(selector_->OnTouchEvent(action_up)); |
| 379 EXPECT_EQ(3u, event_log_.size()); |
| 380 EXPECT_STREQ("End", event_log_.back().c_str()); |
| 381 } |
| 382 |
| 383 TEST_F(StylusTextSelectorTest, TapTriggersLongPressSelection2) { |
| 384 base::TimeTicks event_time = base::TimeTicks::Now(); |
| 385 const float x1 = 50.0f; |
| 386 const float y1 = 30.0f; |
| 387 const float x2 = 51.0f; |
| 388 const float y2 = 31.0f; |
| 389 // 1. ACTION_DOWN with stylus + button |
| 390 event_time += base::TimeDelta::FromMilliseconds(1); |
| 391 MockMotionEvent action_down(MotionEvent::ACTION_DOWN, event_time, x1, y1); |
| 392 action_down.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
| 393 action_down.set_button_state(MotionEvent::BUTTON_STYLUS_PRIMARY); |
| 394 EXPECT_TRUE(selector_->OnTouchEvent(action_down)); |
| 395 EXPECT_TRUE(event_log_.empty()); |
| 396 |
| 397 // 2. ACTION_MOVE |
| 398 event_time += base::TimeDelta::FromMilliseconds(1); |
| 399 MockMotionEvent action_move(MotionEvent::ACTION_MOVE, event_time, x2, y2); |
| 400 action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
| 401 action_move.set_button_state(MotionEvent::BUTTON_STYLUS_PRIMARY); |
| 402 EXPECT_TRUE(selector_->OnTouchEvent(action_move)); |
| 403 EXPECT_TRUE(event_log_.empty()); |
| 404 |
| 405 // 3. ACTION_UP |
| 406 event_time += base::TimeDelta::FromMilliseconds(1); |
| 407 MockMotionEvent action_up(MotionEvent::ACTION_UP, event_time, x2, y2); |
| 408 action_up.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS); |
| 409 action_up.set_button_state(0); |
| 410 EXPECT_TRUE(selector_->OnTouchEvent(action_up)); |
| 411 ASSERT_EQ(1u, event_log_.size()); |
| 412 EXPECT_STREQ("Tap", event_log_.back().c_str()); |
| 413 } |
| 414 // End of tests for BUTTON_STLUS_PRIMARY case. |
247 | 415 |
248 } // namespace content | 416 } // namespace content |
OLD | NEW |