Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / renderer_host / input / touch_selection_controller_unittest.cc
blob460ed20250f5e3982c8f1c24f6cf36e49f70cc9d
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.
5 #include "content/browser/renderer_host/input/touch_selection_controller.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/events/test/mock_motion_event.h"
10 using ui::test::MockMotionEvent;
12 namespace content {
13 namespace {
15 class MockTouchHandleDrawable : public TouchHandleDrawable {
16 public:
17 explicit MockTouchHandleDrawable(bool* contains_point)
18 : intersects_rect_(contains_point) {}
19 virtual ~MockTouchHandleDrawable() {}
20 virtual void SetEnabled(bool enabled) OVERRIDE {}
21 virtual void SetOrientation(TouchHandleOrientation orientation) OVERRIDE {}
22 virtual void SetAlpha(float alpha) OVERRIDE {}
23 virtual void SetFocus(const gfx::PointF& position) OVERRIDE {}
24 virtual void SetVisible(bool visible) OVERRIDE {}
25 virtual bool IntersectsWith(const gfx::RectF& rect) const OVERRIDE {
26 return *intersects_rect_;
29 private:
30 bool* intersects_rect_;
33 } // namespace
35 class TouchSelectionControllerTest : public testing::Test,
36 public TouchSelectionControllerClient {
37 public:
38 TouchSelectionControllerTest()
39 : last_event_(SELECTION_CLEARED),
40 caret_moved_(false),
41 selection_moved_(false),
42 needs_animate_(false),
43 animation_enabled_(true),
44 dragging_enabled_(false) {}
46 virtual ~TouchSelectionControllerTest() {}
48 // testing::Test implementation.
49 virtual void SetUp() OVERRIDE {
50 controller_.reset(new TouchSelectionController(this));
53 virtual void TearDown() OVERRIDE { controller_.reset(); }
55 // TouchSelectionControllerClient implementation.
57 virtual bool SupportsAnimation() const OVERRIDE { return animation_enabled_; }
59 virtual void SetNeedsAnimate() OVERRIDE { needs_animate_ = true; }
61 virtual void MoveCaret(const gfx::PointF& position) OVERRIDE {
62 caret_moved_ = true;
63 caret_position_ = position;
66 virtual void SelectBetweenCoordinates(const gfx::PointF& start,
67 const gfx::PointF& end) OVERRIDE {
68 selection_moved_ = true;
69 selection_start_ = start;
70 selection_end_ = end;
73 virtual void OnSelectionEvent(SelectionEventType event,
74 const gfx::PointF& end_position) OVERRIDE {
75 last_event_ = event;
76 last_event_start_ = end_position;
79 virtual scoped_ptr<TouchHandleDrawable> CreateDrawable() OVERRIDE {
80 return scoped_ptr<TouchHandleDrawable>(
81 new MockTouchHandleDrawable(&dragging_enabled_));
84 void SetAnimationEnabled(bool enabled) { animation_enabled_ = enabled; }
85 void SetDraggingEnabled(bool enabled) { dragging_enabled_ = enabled; }
87 void ClearSelection() {
88 controller_->OnSelectionBoundsChanged(cc::ViewportSelectionBound(),
89 cc::ViewportSelectionBound());
92 void ClearInsertion() { ClearSelection(); }
94 void ChangeInsertion(const gfx::RectF& rect, bool visible) {
95 cc::ViewportSelectionBound bound;
96 bound.type = cc::SELECTION_BOUND_CENTER;
97 bound.edge_top = rect.origin();
98 bound.edge_bottom = rect.bottom_left();
99 bound.visible = visible;
100 controller_->OnSelectionBoundsChanged(bound, bound);
103 void ChangeSelection(const gfx::RectF& start_rect,
104 bool start_visible,
105 const gfx::RectF& end_rect,
106 bool end_visible) {
107 cc::ViewportSelectionBound start_bound, end_bound;
108 start_bound.type = cc::SELECTION_BOUND_LEFT;
109 end_bound.type = cc::SELECTION_BOUND_RIGHT;
110 start_bound.edge_top = start_rect.origin();
111 start_bound.edge_bottom = start_rect.bottom_left();
112 end_bound.edge_top = end_rect.origin();
113 end_bound.edge_bottom = end_rect.bottom_left();
114 start_bound.visible = start_visible;
115 end_bound.visible = end_visible;
116 controller_->OnSelectionBoundsChanged(start_bound, end_bound);
119 void Animate() {
120 base::TimeTicks now = base::TimeTicks::Now();
121 while (needs_animate_) {
122 needs_animate_ = controller_->Animate(now);
123 now += base::TimeDelta::FromMilliseconds(16);
127 bool GetAndResetNeedsAnimate() {
128 bool needs_animate = needs_animate_;
129 Animate();
130 return needs_animate;
133 bool GetAndResetCaretMoved() {
134 bool moved = caret_moved_;
135 caret_moved_ = false;
136 return moved;
139 bool GetAndResetSelectionMoved() {
140 bool moved = selection_moved_;
141 selection_moved_ = false;
142 return moved;
145 const gfx::PointF& GetLastCaretPosition() const { return caret_position_; }
146 const gfx::PointF& GetLastSelectionStart() const { return selection_start_; }
147 const gfx::PointF& GetLastSelectionEnd() const { return selection_end_; }
148 SelectionEventType GetLastEventType() const { return last_event_; }
149 const gfx::PointF& GetLastEventAnchor() const { return last_event_start_; }
151 TouchSelectionController& controller() { return *controller_; }
153 private:
154 gfx::PointF last_event_start_;
155 gfx::PointF caret_position_;
156 gfx::PointF selection_start_;
157 gfx::PointF selection_end_;
158 SelectionEventType last_event_;
159 bool caret_moved_;
160 bool selection_moved_;
161 bool needs_animate_;
162 bool animation_enabled_;
163 bool dragging_enabled_;
164 scoped_ptr<TouchSelectionController> controller_;
167 TEST_F(TouchSelectionControllerTest, InsertionBasic) {
168 gfx::RectF insertion_rect(5, 5, 0, 10);
169 bool visible = true;
171 // Insertion events are ignored until automatic showing is enabled.
172 ChangeInsertion(insertion_rect, visible);
173 EXPECT_EQ(gfx::PointF(), GetLastEventAnchor());
174 controller().OnTapEvent();
176 // Insertion events are ignored until the selection region is marked editable.
177 ChangeInsertion(insertion_rect, visible);
178 EXPECT_EQ(gfx::PointF(), GetLastEventAnchor());
180 controller().OnTapEvent();
181 controller().OnSelectionEditable(true);
182 ChangeInsertion(insertion_rect, visible);
183 EXPECT_EQ(INSERTION_SHOWN, GetLastEventType());
184 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventAnchor());
186 insertion_rect.Offset(1, 0);
187 ChangeInsertion(insertion_rect, visible);
188 EXPECT_EQ(INSERTION_MOVED, GetLastEventType());
189 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventAnchor());
191 insertion_rect.Offset(0, 1);
192 ChangeInsertion(insertion_rect, visible);
193 EXPECT_EQ(INSERTION_MOVED, GetLastEventType());
194 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventAnchor());
196 ClearInsertion();
197 EXPECT_EQ(INSERTION_CLEARED, GetLastEventType());
200 TEST_F(TouchSelectionControllerTest, InsertionClearedWhenNoLongerEditable) {
201 gfx::RectF insertion_rect(5, 5, 0, 10);
202 bool visible = true;
203 controller().OnTapEvent();
204 controller().OnSelectionEditable(true);
206 ChangeInsertion(insertion_rect, visible);
207 EXPECT_EQ(INSERTION_SHOWN, GetLastEventType());
208 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventAnchor());
210 controller().OnSelectionEditable(false);
211 EXPECT_EQ(INSERTION_CLEARED, GetLastEventType());
214 TEST_F(TouchSelectionControllerTest, InsertionStaysHiddenIfEmptyRegionTapped) {
215 gfx::RectF insertion_rect(5, 5, 0, 10);
216 bool visible = true;
217 controller().OnSelectionEditable(true);
219 // Taps should be ignored if they're in an empty editable region.
220 controller().OnTapEvent();
221 controller().OnSelectionEmpty(true);
222 ChangeInsertion(insertion_rect, visible);
223 EXPECT_EQ(gfx::PointF(), GetLastEventAnchor());
225 // Once the region becomes editable, taps should show the insertion handle.
226 controller().OnTapEvent();
227 controller().OnSelectionEmpty(false);
228 ChangeInsertion(insertion_rect, visible);
229 EXPECT_EQ(INSERTION_SHOWN, GetLastEventType());
230 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventAnchor());
232 // Reset the selection.
233 controller().HideAndDisallowShowingAutomatically();
234 EXPECT_EQ(INSERTION_CLEARED, GetLastEventType());
236 // Long-pressing should show the handle even if the editable region is empty.
237 insertion_rect.Offset(2, -2);
238 controller().OnLongPressEvent();
239 controller().OnSelectionEmpty(true);
240 ChangeInsertion(insertion_rect, visible);
241 EXPECT_EQ(INSERTION_SHOWN, GetLastEventType());
242 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventAnchor());
244 // Single Tap on an empty edit field should clear insertion handle.
245 controller().OnTapEvent();
246 EXPECT_EQ(INSERTION_CLEARED, GetLastEventType());
249 TEST_F(TouchSelectionControllerTest, InsertionAppearsAfterTapFollowingTyping) {
250 gfx::RectF insertion_rect(5, 5, 0, 10);
251 bool visible = true;
253 // Simulate the user tapping an empty text field.
254 controller().OnTapEvent();
255 controller().OnSelectionEditable(true);
256 controller().OnSelectionEmpty(true);
257 ChangeInsertion(insertion_rect, visible);
258 EXPECT_EQ(gfx::PointF(), GetLastEventAnchor());
260 // Simulate the cursor moving while a user is typing.
261 insertion_rect.Offset(10, 0);
262 controller().OnSelectionEmpty(false);
263 ChangeInsertion(insertion_rect, visible);
264 EXPECT_EQ(gfx::PointF(), GetLastEventAnchor());
266 // If the user taps the *same* position as the cursor at the end of the text
267 // entry, the handle should appear.
268 controller().OnTapEvent();
269 ChangeInsertion(insertion_rect, visible);
270 EXPECT_EQ(INSERTION_SHOWN, GetLastEventType());
271 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventAnchor());
274 TEST_F(TouchSelectionControllerTest, InsertionToSelectionTransition) {
275 controller().OnLongPressEvent();
276 controller().OnSelectionEditable(true);
278 gfx::RectF start_rect(5, 5, 0, 10);
279 gfx::RectF end_rect(50, 5, 0, 10);
280 bool visible = true;
282 ChangeInsertion(start_rect, visible);
283 EXPECT_EQ(INSERTION_SHOWN, GetLastEventType());
284 EXPECT_EQ(start_rect.bottom_left(), GetLastEventAnchor());
286 ChangeSelection(start_rect, visible, end_rect, visible);
287 EXPECT_EQ(SELECTION_SHOWN, GetLastEventType());
288 EXPECT_EQ(start_rect.bottom_left(), GetLastEventAnchor());
290 ChangeInsertion(end_rect, visible);
291 EXPECT_EQ(INSERTION_SHOWN, GetLastEventType());
292 EXPECT_EQ(end_rect.bottom_left(), GetLastEventAnchor());
294 controller().HideAndDisallowShowingAutomatically();
295 EXPECT_EQ(INSERTION_CLEARED, GetLastEventType());
297 controller().OnTapEvent();
298 ChangeInsertion(end_rect, visible);
299 EXPECT_EQ(INSERTION_SHOWN, GetLastEventType());
300 EXPECT_EQ(end_rect.bottom_left(), GetLastEventAnchor());
303 TEST_F(TouchSelectionControllerTest, InsertionDragged) {
304 base::TimeTicks event_time = base::TimeTicks::Now();
305 controller().OnTapEvent();
306 controller().OnSelectionEditable(true);
308 // The touch sequence should not be handled if insertion is not active.
309 MockMotionEvent event(MockMotionEvent::ACTION_DOWN, event_time, 0, 0);
310 EXPECT_FALSE(controller().WillHandleTouchEvent(event));
312 float line_height = 10.f;
313 gfx::RectF start_rect(10, 0, 0, line_height);
314 bool visible = true;
315 ChangeInsertion(start_rect, visible);
316 EXPECT_EQ(INSERTION_SHOWN, GetLastEventType());
317 EXPECT_EQ(start_rect.bottom_left(), GetLastEventAnchor());
319 // The touch sequence should be handled only if the drawable reports a hit.
320 EXPECT_FALSE(controller().WillHandleTouchEvent(event));
321 SetDraggingEnabled(true);
322 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
323 EXPECT_FALSE(GetAndResetCaretMoved());
325 // The MoveCaret() result should reflect the movement.
326 // The reported position is offset from the center of |start_rect|.
327 gfx::PointF start_offset = start_rect.CenterPoint();
328 event = MockMotionEvent(MockMotionEvent::ACTION_MOVE, event_time, 0, 5);
329 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
330 EXPECT_TRUE(GetAndResetCaretMoved());
331 EXPECT_EQ(start_offset + gfx::Vector2dF(0, 5), GetLastCaretPosition());
333 event = MockMotionEvent(MockMotionEvent::ACTION_MOVE, event_time, 5, 5);
334 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
335 EXPECT_TRUE(GetAndResetCaretMoved());
336 EXPECT_EQ(start_offset + gfx::Vector2dF(5, 5), GetLastCaretPosition());
338 event = MockMotionEvent(MockMotionEvent::ACTION_MOVE, event_time, 10, 10);
339 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
340 EXPECT_TRUE(GetAndResetCaretMoved());
341 EXPECT_EQ(start_offset + gfx::Vector2dF(10, 10), GetLastCaretPosition());
343 event = MockMotionEvent(MockMotionEvent::ACTION_UP, event_time, 10, 5);
344 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
345 EXPECT_FALSE(GetAndResetCaretMoved());
347 // Once the drag is complete, no more touch events should be consumed until
348 // the next ACTION_DOWN.
349 EXPECT_FALSE(controller().WillHandleTouchEvent(event));
352 TEST_F(TouchSelectionControllerTest, InsertionTapped) {
353 base::TimeTicks event_time = base::TimeTicks::Now();
354 controller().OnTapEvent();
355 controller().OnSelectionEditable(true);
356 SetDraggingEnabled(true);
358 gfx::RectF start_rect(10, 0, 0, 10);
359 bool visible = true;
360 ChangeInsertion(start_rect, visible);
361 EXPECT_EQ(INSERTION_SHOWN, GetLastEventType());
363 MockMotionEvent event(MockMotionEvent::ACTION_DOWN, event_time, 0, 0);
364 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
365 //TODO(AKV): this test case has to be modified once crbug.com/394093 is fixed.
366 EXPECT_EQ(INSERTION_DRAG_STARTED, GetLastEventType());
368 event = MockMotionEvent(MockMotionEvent::ACTION_UP, event_time, 0, 0);
369 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
370 EXPECT_EQ(INSERTION_TAPPED, GetLastEventType());
372 // Reset the insertion.
373 ClearInsertion();
374 controller().OnTapEvent();
375 ChangeInsertion(start_rect, visible);
376 ASSERT_EQ(INSERTION_SHOWN, GetLastEventType());
378 // No tap should be signalled if the time between DOWN and UP was too long.
379 event = MockMotionEvent(MockMotionEvent::ACTION_DOWN, event_time, 0, 0);
380 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
381 event = MockMotionEvent(MockMotionEvent::ACTION_UP,
382 event_time + base::TimeDelta::FromSeconds(1),
385 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
386 EXPECT_EQ(INSERTION_DRAG_STARTED, GetLastEventType());
388 // Reset the insertion.
389 ClearInsertion();
390 controller().OnTapEvent();
391 ChangeInsertion(start_rect, visible);
392 ASSERT_EQ(INSERTION_SHOWN, GetLastEventType());
394 // No tap should be signalled if the touch sequence is cancelled.
395 event = MockMotionEvent(MockMotionEvent::ACTION_DOWN, event_time, 0, 0);
396 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
397 event = MockMotionEvent(MockMotionEvent::ACTION_CANCEL, event_time, 0, 0);
398 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
399 EXPECT_EQ(INSERTION_DRAG_STARTED, GetLastEventType());
402 TEST_F(TouchSelectionControllerTest, InsertionNotResetByRepeatedTapOrPress) {
403 base::TimeTicks event_time = base::TimeTicks::Now();
404 controller().OnTapEvent();
405 controller().OnSelectionEditable(true);
406 SetDraggingEnabled(true);
408 gfx::RectF anchor_rect(10, 0, 0, 10);
409 bool visible = true;
410 ChangeInsertion(anchor_rect, visible);
411 EXPECT_EQ(INSERTION_SHOWN, GetLastEventType());
412 EXPECT_EQ(anchor_rect.bottom_left(), GetLastEventAnchor());
414 // Tapping again shouldn't reset the active insertion point.
415 controller().OnTapEvent();
416 MockMotionEvent event(MockMotionEvent::ACTION_DOWN, event_time, 0, 0);
417 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
418 EXPECT_EQ(INSERTION_DRAG_STARTED, GetLastEventType());
419 EXPECT_EQ(anchor_rect.bottom_left(), GetLastEventAnchor());
421 event = MockMotionEvent(MockMotionEvent::ACTION_UP, event_time, 0, 0);
422 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
423 EXPECT_EQ(INSERTION_TAPPED, GetLastEventType());
424 EXPECT_EQ(anchor_rect.bottom_left(), GetLastEventAnchor());
426 anchor_rect.Offset(5, 15);
427 ChangeInsertion(anchor_rect, visible);
428 EXPECT_EQ(INSERTION_MOVED, GetLastEventType());
429 EXPECT_EQ(anchor_rect.bottom_left(), GetLastEventAnchor());
431 // Pressing shouldn't reset the active insertion point.
432 controller().OnLongPressEvent();
433 controller().OnSelectionEmpty(true);
434 event = MockMotionEvent(MockMotionEvent::ACTION_DOWN, event_time, 0, 0);
435 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
436 EXPECT_EQ(INSERTION_DRAG_STARTED, GetLastEventType());
437 EXPECT_EQ(anchor_rect.bottom_left(), GetLastEventAnchor());
439 event = MockMotionEvent(MockMotionEvent::ACTION_UP, event_time, 0, 0);
440 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
441 EXPECT_EQ(INSERTION_TAPPED, GetLastEventType());
442 EXPECT_EQ(anchor_rect.bottom_left(), GetLastEventAnchor());
445 TEST_F(TouchSelectionControllerTest, SelectionBasic) {
446 gfx::RectF start_rect(5, 5, 0, 10);
447 gfx::RectF end_rect(50, 5, 0, 10);
448 bool visible = true;
450 // Selection events are ignored until automatic showing is enabled.
451 ChangeSelection(start_rect, visible, end_rect, visible);
452 EXPECT_EQ(gfx::PointF(), GetLastEventAnchor());
454 controller().OnLongPressEvent();
455 ChangeSelection(start_rect, visible, end_rect, visible);
456 EXPECT_EQ(SELECTION_SHOWN, GetLastEventType());
457 EXPECT_EQ(start_rect.bottom_left(), GetLastEventAnchor());
459 start_rect.Offset(1, 0);
460 ChangeSelection(start_rect, visible, end_rect, visible);
461 // Selection movement does not currently trigger a separate event.
462 EXPECT_EQ(SELECTION_SHOWN, GetLastEventType());
464 ClearSelection();
465 EXPECT_EQ(SELECTION_CLEARED, GetLastEventType());
466 EXPECT_EQ(gfx::PointF(), GetLastEventAnchor());
469 TEST_F(TouchSelectionControllerTest, SelectionRepeatedLongPress) {
470 gfx::RectF start_rect(5, 5, 0, 10);
471 gfx::RectF end_rect(50, 5, 0, 10);
472 bool visible = true;
474 controller().OnLongPressEvent();
475 ChangeSelection(start_rect, visible, end_rect, visible);
476 EXPECT_EQ(SELECTION_SHOWN, GetLastEventType());
477 EXPECT_EQ(start_rect.bottom_left(), GetLastEventAnchor());
479 // A long press triggering a new selection should re-send the SELECTION_SHOWN
480 // event notification.
481 start_rect.Offset(10, 10);
482 controller().OnLongPressEvent();
483 ChangeSelection(start_rect, visible, end_rect, visible);
484 EXPECT_EQ(SELECTION_SHOWN, GetLastEventType());
485 EXPECT_EQ(start_rect.bottom_left(), GetLastEventAnchor());
488 TEST_F(TouchSelectionControllerTest, SelectionDragged) {
489 base::TimeTicks event_time = base::TimeTicks::Now();
490 controller().OnLongPressEvent();
492 // The touch sequence should not be handled if selection is not active.
493 MockMotionEvent event(MockMotionEvent::ACTION_DOWN, event_time, 0, 0);
494 EXPECT_FALSE(controller().WillHandleTouchEvent(event));
496 float line_height = 10.f;
497 gfx::RectF start_rect(0, 0, 0, line_height);
498 gfx::RectF end_rect(50, 0, 0, line_height);
499 bool visible = true;
500 ChangeSelection(start_rect, visible, end_rect, visible);
501 EXPECT_EQ(SELECTION_SHOWN, GetLastEventType());
502 EXPECT_EQ(start_rect.bottom_left(), GetLastEventAnchor());
504 // The touch sequence should be handled only if the drawable reports a hit.
505 EXPECT_FALSE(controller().WillHandleTouchEvent(event));
506 SetDraggingEnabled(true);
507 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
508 EXPECT_FALSE(GetAndResetSelectionMoved());
510 // The SelectBetweenCoordinates() result should reflect the movement. Note
511 // that the start coordinate will always reflect the "fixed" handle's
512 // position, in this case the position from |end_rect|.
513 // Note that the reported position is offset from the center of the
514 // input rects (i.e., the middle of the corresponding text line).
515 gfx::PointF fixed_offset = end_rect.CenterPoint();
516 gfx::PointF start_offset = start_rect.CenterPoint();
517 event = MockMotionEvent(MockMotionEvent::ACTION_MOVE, event_time, 0, 5);
518 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
519 EXPECT_EQ(SELECTION_DRAG_STARTED, GetLastEventType());
520 EXPECT_TRUE(GetAndResetSelectionMoved());
521 EXPECT_EQ(fixed_offset, GetLastSelectionStart());
522 EXPECT_EQ(start_offset + gfx::Vector2dF(0, 5), GetLastSelectionEnd());
524 event = MockMotionEvent(MockMotionEvent::ACTION_MOVE, event_time, 5, 5);
525 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
526 EXPECT_TRUE(GetAndResetSelectionMoved());
527 EXPECT_EQ(fixed_offset, GetLastSelectionStart());
528 EXPECT_EQ(start_offset + gfx::Vector2dF(5, 5), GetLastSelectionEnd());
530 event = MockMotionEvent(MockMotionEvent::ACTION_MOVE, event_time, 10, 5);
531 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
532 EXPECT_TRUE(GetAndResetSelectionMoved());
533 EXPECT_EQ(fixed_offset, GetLastSelectionStart());
534 EXPECT_EQ(start_offset + gfx::Vector2dF(10, 5), GetLastSelectionEnd());
536 event = MockMotionEvent(MockMotionEvent::ACTION_UP, event_time, 10, 5);
537 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
538 EXPECT_EQ(SELECTION_DRAG_STOPPED, GetLastEventType());
539 EXPECT_FALSE(GetAndResetSelectionMoved());
541 // Once the drag is complete, no more touch events should be consumed until
542 // the next ACTION_DOWN.
543 EXPECT_FALSE(controller().WillHandleTouchEvent(event));
546 TEST_F(TouchSelectionControllerTest, SelectionDraggedWithOverlap) {
547 base::TimeTicks event_time = base::TimeTicks::Now();
548 controller().OnLongPressEvent();
550 float line_height = 10.f;
551 gfx::RectF start_rect(0, 0, 0, line_height);
552 gfx::RectF end_rect(50, 0, 0, line_height);
553 bool visible = true;
554 ChangeSelection(start_rect, visible, end_rect, visible);
555 EXPECT_EQ(SELECTION_SHOWN, GetLastEventType());
556 EXPECT_EQ(start_rect.bottom_left(), GetLastEventAnchor());
558 // The ACTION_DOWN should lock to the closest handle.
559 gfx::PointF end_offset = end_rect.CenterPoint();
560 gfx::PointF fixed_offset = start_rect.CenterPoint();
561 float touch_down_x = (end_offset.x() + fixed_offset.x()) / 2 + 1.f;
562 MockMotionEvent event(
563 MockMotionEvent::ACTION_DOWN, event_time, touch_down_x, 0);
564 SetDraggingEnabled(true);
565 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
566 EXPECT_EQ(SELECTION_DRAG_STARTED, GetLastEventType());
567 EXPECT_FALSE(GetAndResetSelectionMoved());
569 // Even though the ACTION_MOVE is over the start handle, it should continue
570 // targetting the end handle that consumed the ACTION_DOWN.
571 event = MockMotionEvent(MockMotionEvent::ACTION_MOVE, event_time, 0, 0);
572 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
573 EXPECT_TRUE(GetAndResetSelectionMoved());
574 EXPECT_EQ(fixed_offset, GetLastSelectionStart());
575 EXPECT_EQ(end_offset - gfx::Vector2dF(touch_down_x, 0),
576 GetLastSelectionEnd());
578 event = MockMotionEvent(MockMotionEvent::ACTION_UP, event_time, 0, 0);
579 EXPECT_TRUE(controller().WillHandleTouchEvent(event));
580 EXPECT_EQ(SELECTION_DRAG_STOPPED, GetLastEventType());
581 EXPECT_FALSE(GetAndResetSelectionMoved());
584 TEST_F(TouchSelectionControllerTest, Animation) {
585 controller().OnTapEvent();
586 controller().OnSelectionEditable(true);
588 gfx::RectF insertion_rect(5, 5, 0, 10);
590 bool visible = true;
591 ChangeInsertion(insertion_rect, visible);
592 EXPECT_FALSE(GetAndResetNeedsAnimate());
594 visible = false;
595 ChangeInsertion(insertion_rect, visible);
596 EXPECT_TRUE(GetAndResetNeedsAnimate());
598 visible = true;
599 ChangeInsertion(insertion_rect, visible);
600 EXPECT_TRUE(GetAndResetNeedsAnimate());
602 // If the handles are explicity hidden, no animation should be triggered.
603 controller().HideAndDisallowShowingAutomatically();
604 EXPECT_FALSE(GetAndResetNeedsAnimate());
606 // If the client doesn't support animation, no animation should be triggered.
607 SetAnimationEnabled(false);
608 controller().OnTapEvent();
609 visible = true;
610 ChangeInsertion(insertion_rect, visible);
611 EXPECT_FALSE(GetAndResetNeedsAnimate());
614 TEST_F(TouchSelectionControllerTest, TemporarilyHidden) {
615 controller().OnTapEvent();
616 controller().OnSelectionEditable(true);
618 gfx::RectF insertion_rect(5, 5, 0, 10);
620 bool visible = true;
621 ChangeInsertion(insertion_rect, visible);
622 EXPECT_FALSE(GetAndResetNeedsAnimate());
624 controller().SetTemporarilyHidden(true);
625 EXPECT_TRUE(GetAndResetNeedsAnimate());
627 visible = false;
628 ChangeInsertion(insertion_rect, visible);
629 EXPECT_FALSE(GetAndResetNeedsAnimate());
631 visible = true;
632 ChangeInsertion(insertion_rect, visible);
633 EXPECT_FALSE(GetAndResetNeedsAnimate());
635 controller().SetTemporarilyHidden(false);
636 EXPECT_TRUE(GetAndResetNeedsAnimate());
639 TEST_F(TouchSelectionControllerTest, SelectionClearOnTap) {
640 gfx::RectF start_rect(5, 5, 0, 10);
641 gfx::RectF end_rect(50, 5, 0, 10);
642 bool visible = true;
644 controller().OnLongPressEvent();
645 ChangeSelection(start_rect, visible, end_rect, visible);
647 // Selection should not be cleared if the selection bounds have not changed.
648 controller().OnTapEvent();
649 EXPECT_EQ(SELECTION_SHOWN, GetLastEventType());
650 EXPECT_EQ(start_rect.bottom_left(), GetLastEventAnchor());
652 controller().OnTapEvent();
653 ClearSelection();
654 EXPECT_EQ(SELECTION_CLEARED, GetLastEventType());
655 EXPECT_EQ(gfx::PointF(), GetLastEventAnchor());
658 } // namespace content