Commit 169b875a authored by avi.nitk's avatar avi.nitk Committed by Commit bot

Check if Button is pressed for changing selection

Adding flags to check if SPen button is pressed
when gesture text selection is in progress.
The selection should be updated only when the
button is pressed.

BUG=416004

Review URL: https://codereview.chromium.org/590483002

Cr-Commit-Position: refs/heads/master@{#299149}
parent ebe3c763
...@@ -34,7 +34,11 @@ scoped_ptr<GestureDetector> CreateGestureDetector( ...@@ -34,7 +34,11 @@ scoped_ptr<GestureDetector> CreateGestureDetector(
} // namespace } // namespace
GestureTextSelector::GestureTextSelector(GestureTextSelectorClient* client) GestureTextSelector::GestureTextSelector(GestureTextSelectorClient* client)
: client_(client), text_selection_triggered_(false) { : client_(client),
text_selection_triggered_(false),
secondary_button_pressed_(false),
anchor_x_(0.0f),
anchor_y_(0.0f) {
DCHECK(client); DCHECK(client);
} }
...@@ -46,11 +50,24 @@ bool GestureTextSelector::OnTouchEvent(const MotionEvent& event) { ...@@ -46,11 +50,24 @@ bool GestureTextSelector::OnTouchEvent(const MotionEvent& event) {
// Only trigger selection on ACTION_DOWN to prevent partial touch or gesture // Only trigger selection on ACTION_DOWN to prevent partial touch or gesture
// sequences from being forwarded. // sequences from being forwarded.
text_selection_triggered_ = ShouldStartTextSelection(event); text_selection_triggered_ = ShouldStartTextSelection(event);
secondary_button_pressed_ =
event.GetButtonState() == MotionEvent::BUTTON_SECONDARY;
anchor_x_ = event.GetX();
anchor_y_ = event.GetY();
} }
if (!text_selection_triggered_) if (!text_selection_triggered_)
return false; return false;
if (event.GetAction() == MotionEvent::ACTION_MOVE) {
secondary_button_pressed_ =
event.GetButtonState() == MotionEvent::BUTTON_SECONDARY;
if (!secondary_button_pressed_) {
anchor_x_ = event.GetX();
anchor_y_ = event.GetY();
}
}
if (!gesture_detector_) if (!gesture_detector_)
gesture_detector_ = CreateGestureDetector(this); gesture_detector_ = CreateGestureDetector(this);
...@@ -72,11 +89,16 @@ bool GestureTextSelector::OnScroll(const MotionEvent& e1, ...@@ -72,11 +89,16 @@ bool GestureTextSelector::OnScroll(const MotionEvent& e1,
float distance_x, float distance_x,
float distance_y) { float distance_y) {
DCHECK(text_selection_triggered_); DCHECK(text_selection_triggered_);
// Return if Stylus button is not pressed.
if (!secondary_button_pressed_)
return true;
// TODO(changwan): check if we can show handles after the scroll finishes // TODO(changwan): check if we can show handles after the scroll finishes
// instead. Currently it is not possible as ShowSelectionHandles should // instead. Currently it is not possible as ShowSelectionHandles should
// be called before we change the selection. // be called before we change the selection.
client_->ShowSelectionHandlesAutomatically(); client_->ShowSelectionHandlesAutomatically();
client_->SelectRange(e1.GetX(), e1.GetY(), e2.GetX(), e2.GetY()); client_->SelectRange(anchor_x_, anchor_y_, e2.GetX(), e2.GetY());
return true; return true;
} }
......
...@@ -59,6 +59,9 @@ class CONTENT_EXPORT GestureTextSelector : public ui::SimpleGestureListener { ...@@ -59,6 +59,9 @@ class CONTENT_EXPORT GestureTextSelector : public ui::SimpleGestureListener {
GestureTextSelectorClient* client_; GestureTextSelectorClient* client_;
bool text_selection_triggered_; bool text_selection_triggered_;
bool secondary_button_pressed_;
float anchor_x_;
float anchor_y_;
scoped_ptr<ui::GestureDetector> gesture_detector_; scoped_ptr<ui::GestureDetector> gesture_detector_;
DISALLOW_COPY_AND_ASSIGN(GestureTextSelector); DISALLOW_COPY_AND_ASSIGN(GestureTextSelector);
......
...@@ -42,7 +42,9 @@ class GestureTextSelectorTest : public testing::Test, ...@@ -42,7 +42,9 @@ class GestureTextSelectorTest : public testing::Test,
} }
virtual void SelectRange(float x1, float y1, float x2, float y2) override { virtual void SelectRange(float x1, float y1, float x2, float y2) override {
event_log_.push_back("SelectRange"); std::stringstream ss;
ss << "SelectRange(" << x1 << ", " << y1 << ", " << x2 << ", " << y2 << ")";
event_log_.push_back(ss.str());
} }
virtual void LongPress(base::TimeTicks time, float x, float y) override { virtual void LongPress(base::TimeTicks time, float x, float y) override {
...@@ -120,7 +122,7 @@ TEST_F(GestureTextSelectorTest, PenDragging) { ...@@ -120,7 +122,7 @@ TEST_F(GestureTextSelectorTest, PenDragging) {
EXPECT_TRUE(selector_->OnTouchEvent(action_move)); EXPECT_TRUE(selector_->OnTouchEvent(action_move));
ASSERT_EQ(2u, event_log_.size()); ASSERT_EQ(2u, event_log_.size());
EXPECT_STREQ("Show", event_log_[0].c_str()); EXPECT_STREQ("Show", event_log_[0].c_str());
EXPECT_STREQ("SelectRange", event_log_[1].c_str()); EXPECT_STREQ("SelectRange(50, 30, 100, 90)", event_log_[1].c_str());
// 3. ACTION_UP // 3. ACTION_UP
event_time += base::TimeDelta::FromMilliseconds(10); event_time += base::TimeDelta::FromMilliseconds(10);
...@@ -131,6 +133,61 @@ TEST_F(GestureTextSelectorTest, PenDragging) { ...@@ -131,6 +133,61 @@ TEST_F(GestureTextSelectorTest, PenDragging) {
ASSERT_EQ(2u, event_log_.size()); // NO CHANGE ASSERT_EQ(2u, event_log_.size()); // NO CHANGE
} }
TEST_F(GestureTextSelectorTest, PenDraggingButtonNotPressed) {
base::TimeTicks event_time = base::TimeTicks::Now();
float x = 50.0f;
float y = 30.0f;
// 1. ACTION_DOWN with stylus + button
event_time += base::TimeDelta::FromMilliseconds(10);
MockMotionEvent action_down(MotionEvent::ACTION_DOWN, event_time, x, y);
action_down.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS);
action_down.set_button_state(MotionEvent::BUTTON_SECONDARY);
EXPECT_TRUE(selector_->OnTouchEvent(action_down));
EXPECT_TRUE(event_log_.empty());
// 2. ACTION_MOVE
event_time += base::TimeDelta::FromMilliseconds(10);
x += 20; // 70
y += 20; // 50
MockMotionEvent action_move(MotionEvent::ACTION_MOVE, event_time, x, y);
action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS);
action_move.set_button_state(MotionEvent::BUTTON_SECONDARY);
EXPECT_TRUE(selector_->OnTouchEvent(action_move));
ASSERT_EQ(2u, event_log_.size());
EXPECT_STREQ("Show", event_log_[0].c_str());
EXPECT_STREQ("SelectRange(50, 30, 70, 50)", event_log_[1].c_str());
// 3. ACTION_MOVE with stylus + no button
event_time += base::TimeDelta::FromMilliseconds(10);
x += 20; // 90
y += 20; // 70
action_move = MockMotionEvent(MotionEvent::ACTION_MOVE, event_time, x, y);
action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS);
action_move.set_button_state(0);
EXPECT_TRUE(selector_->OnTouchEvent(action_move));
EXPECT_EQ(2u, event_log_.size()); // NO CHANGE
// 4. ACTION_MOVE with stylus + button pressed again
event_time += base::TimeDelta::FromMilliseconds(10);
x += 20; // 110
y += 20; // 90
action_move = MockMotionEvent(MotionEvent::ACTION_MOVE, event_time, x, y);
action_move.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS);
action_move.set_button_state(MotionEvent::BUTTON_SECONDARY);
EXPECT_TRUE(selector_->OnTouchEvent(action_move));
EXPECT_EQ(4u, event_log_.size());
EXPECT_STREQ("SelectRange(90, 70, 110, 90)", event_log_.back().c_str());
// 5. ACTION_UP
event_time += base::TimeDelta::FromMilliseconds(10);
MockMotionEvent action_up(MotionEvent::ACTION_UP, event_time, x, y);
action_up.SetToolType(0, MotionEvent::TOOL_TYPE_STYLUS);
action_up.set_button_state(0);
EXPECT_TRUE(selector_->OnTouchEvent(action_up));
EXPECT_EQ(4u, event_log_.size()); // NO CHANGE
}
TEST_F(GestureTextSelectorTest, TapTriggersLongPressSelection) { TEST_F(GestureTextSelectorTest, TapTriggersLongPressSelection) {
base::TimeTicks event_time = base::TimeTicks::Now(); base::TimeTicks event_time = base::TimeTicks::Now();
const float x1 = 50.0f; const float x1 = 50.0f;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment