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

Side by Side Diff: content/browser/renderer_host/input/gesture_text_selector_unittest.cc

Issue 342633003: [Android] Select text when stylus first button is pressed. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixed signed unsigned comparison error Created 6 years, 5 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
(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/motion_event.h"
15 #include "ui/events/test/mock_motion_event.h"
16 #include "ui/gfx/geometry/rect_f.h"
17
18 using ui::GestureEventData;
19 using ui::GestureEventDetails;
20 using ui::MotionEvent;
21 using ui::test::MockMotionEvent;
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 Unselect() OVERRIDE {
52 event_log_.push_back("Unselect");
53 }
54
55 protected:
56 scoped_ptr<GestureTextSelector> selector_;
57 std::vector<std::string> event_log_;
58 };
59
60 TEST_F(GestureTextSelectorTest, ShouldStartTextSelection) {
61 base::TimeTicks event_time = base::TimeTicks::Now();
62 { // Touched with a finger.
63 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f);
64 e.SetToolType(0, MotionEvent::TOOL_TYPE_FINGER);
65 e.SetButtonState(0);
66 EXPECT_FALSE(selector_->ShouldStartTextSelection(e));
67 }
68
69 { // Touched with a stylus, but no button pressed.
70 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f);
71 e.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS);
72 e.SetButtonState(0);
73 EXPECT_FALSE(selector_->ShouldStartTextSelection(e));
74 }
75
76 { // Touched with a stylus, with first button (BUTTON_SECONDARY) pressed.
77 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f);
78 e.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS);
79 e.SetButtonState(MotionEvent::BUTTON_SECONDARY);
80 EXPECT_TRUE(selector_->ShouldStartTextSelection(e));
81 }
82
83 { // Touched with a stylus, with two buttons pressed.
84 MockMotionEvent e(MotionEvent::ACTION_DOWN, event_time, 50.0f, 50.0f);
85 e.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS);
86 e.SetButtonState(
87 MotionEvent::BUTTON_SECONDARY | MotionEvent::BUTTON_TERTIARY);
88 EXPECT_FALSE(selector_->ShouldStartTextSelection(e));
89 }
90 }
91
92 TEST_F(GestureTextSelectorTest, EventConsumption) {
93 base::TimeTicks event_time = base::TimeTicks::Now();
94 const float x1 = 50.0f;
95 const float y1 = 30.0f;
96 const float x2 = 100.0f;
97 const float y2 = 90.0f;
98 // 0. Touched with a finger: ignored
99 MockMotionEvent finger(MotionEvent::ACTION_DOWN, event_time, x1, y1);
100 finger.SetToolType(0, MotionEvent::TOOL_TYPE_FINGER);
101 EXPECT_FALSE(selector_->OnTouchEvent(finger));
102 // We do not consume finger events.
103 EXPECT_TRUE(event_log_.empty());
104
105 // 1. ACTION_DOWN with stylus + button
106 event_time += base::TimeDelta::FromMilliseconds(10);
107 MockMotionEvent action_down(MotionEvent::ACTION_DOWN, event_time, x1, y1);
108 action_down.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS);
109 action_down.SetButtonState(MotionEvent::BUTTON_SECONDARY);
110 EXPECT_TRUE(selector_->OnTouchEvent(action_down));
111 EXPECT_TRUE(event_log_.empty());
112
113 // 2. ACTION_MOVE
114 event_time += base::TimeDelta::FromMilliseconds(10);
115 MockMotionEvent action_move(MotionEvent::ACTION_MOVE, event_time, x2, y2);
116 action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS);
117 action_move.SetButtonState(MotionEvent::BUTTON_SECONDARY);
118 EXPECT_TRUE(selector_->OnTouchEvent(action_move));
119 EXPECT_TRUE(event_log_.empty());
120
121 // 3. DOUBLE TAP
122 // Suppress most gesture events when in text selection mode.
123 event_time += base::TimeDelta::FromMilliseconds(10);
124 const GestureEventData double_tap(
125 GestureEventDetails(ui::ET_GESTURE_DOUBLE_TAP, 0, 0), 0, event_time,
126 x2, y2, x2, y2, 1, gfx::RectF(0, 0, 0, 0));
127 EXPECT_TRUE(selector_->OnGestureEvent(double_tap));
128 EXPECT_TRUE(event_log_.empty());
129
130 // 4. ET_GESTURE_SCROLL_BEGIN
131 event_time += base::TimeDelta::FromMilliseconds(10);
132 const GestureEventData scroll_begin(
133 GestureEventDetails(ui::ET_GESTURE_SCROLL_BEGIN, 0, 0), 0, event_time,
134 x1, y1, x1, y1, 1, gfx::RectF(0, 0, 0, 0));
135 EXPECT_TRUE(selector_->OnGestureEvent(scroll_begin));
136 EXPECT_EQ(2u, event_log_.size()); // Unselect, Show
137 EXPECT_STREQ("Show", event_log_.back().c_str());
138
139 // 5. ET_GESTURE_SCROLL_UPDATE
140 event_time += base::TimeDelta::FromMilliseconds(10);
141 const GestureEventData scroll_update(
142 GestureEventDetails(ui::ET_GESTURE_SCROLL_UPDATE, 0, 0), 0, event_time,
143 x2, y2, x2, y2, 1, gfx::RectF(0, 0, 0, 0));
144 EXPECT_TRUE(selector_->OnGestureEvent(scroll_update));
145 EXPECT_EQ(3u, event_log_.size()); // Unselect, Show, SelectRange
146 EXPECT_STREQ("SelectRange", event_log_.back().c_str());
147
148 // 6. ACTION_UP
149 event_time += base::TimeDelta::FromMilliseconds(10);
150 MockMotionEvent action_up(MotionEvent::ACTION_UP, event_time, x2, y2);
151 action_up.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS);
152 action_up.SetButtonState(0);
153 EXPECT_TRUE(selector_->OnTouchEvent(action_up));
154 EXPECT_EQ(3u, event_log_.size()); // NO CHANGE
155
156 // 7. ET_GESTURE_SCROLL_END
157 event_time += base::TimeDelta::FromMilliseconds(10);
158 const GestureEventData scroll_end(
159 GestureEventDetails(ui::ET_GESTURE_SCROLL_END, 0, 0), 0, event_time,
160 x2, y2, x2, y2, 1, gfx::RectF(0, 0, 0, 0));
161 EXPECT_TRUE(selector_->OnGestureEvent(scroll_end));
162 EXPECT_EQ(3u, event_log_.size()); // NO CHANGE
163 }
164
165 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698