Commit 8a3f2d26 authored by pkotwicz@chromium.org's avatar pkotwicz@chromium.org

Require the cursor to move a minimum distance after the mouse press before initiating a mouse drag.

The CL also makes ShelfViewTest send ui::MouseEvents in the coordinates of |ShelfView::drag_view_|.

BUG=377522
TEST=ShelfViewTest.ClickAndMoveSlightly

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278129 0039d316-1c4b-4281-b951-d872f2087c98
parent 209c2efe
...@@ -376,7 +376,6 @@ ShelfView::ShelfView(ShelfModel* model, ...@@ -376,7 +376,6 @@ ShelfView::ShelfView(ShelfModel* model,
owner_overflow_bubble_(NULL), owner_overflow_bubble_(NULL),
drag_pointer_(NONE), drag_pointer_(NONE),
drag_view_(NULL), drag_view_(NULL),
drag_offset_(0),
start_drag_index_(-1), start_drag_index_(-1),
context_menu_id_(0), context_menu_id_(0),
leading_inset_(kDefaultLeadingInset), leading_inset_(kDefaultLeadingInset),
...@@ -968,7 +967,7 @@ void ShelfView::ContinueDrag(const ui::LocatedEvent& event) { ...@@ -968,7 +967,7 @@ void ShelfView::ContinueDrag(const ui::LocatedEvent& event) {
int x = 0, y = 0; int x = 0, y = 0;
if (layout_manager_->IsHorizontalAlignment()) { if (layout_manager_->IsHorizontalAlignment()) {
x = std::max(view_model_->ideal_bounds(indices.first).x(), x = std::max(view_model_->ideal_bounds(indices.first).x(),
drag_point.x() - drag_offset_); drag_point.x() - drag_origin_.x());
x = std::min(view_model_->ideal_bounds(last_drag_index).right() - x = std::min(view_model_->ideal_bounds(last_drag_index).right() -
view_model_->ideal_bounds(current_index).width(), view_model_->ideal_bounds(current_index).width(),
x); x);
...@@ -977,7 +976,7 @@ void ShelfView::ContinueDrag(const ui::LocatedEvent& event) { ...@@ -977,7 +976,7 @@ void ShelfView::ContinueDrag(const ui::LocatedEvent& event) {
drag_view_->SetX(x); drag_view_->SetX(x);
} else { } else {
y = std::max(view_model_->ideal_bounds(indices.first).y(), y = std::max(view_model_->ideal_bounds(indices.first).y(),
drag_point.y() - drag_offset_); drag_point.y() - drag_origin_.y());
y = std::min(view_model_->ideal_bounds(last_drag_index).bottom() - y = std::min(view_model_->ideal_bounds(last_drag_index).bottom() -
view_model_->ideal_bounds(current_index).height(), view_model_->ideal_bounds(current_index).height(),
y); y);
...@@ -1576,7 +1575,7 @@ void ShelfView::PointerPressedOnButton(views::View* view, ...@@ -1576,7 +1575,7 @@ void ShelfView::PointerPressedOnButton(views::View* view,
return; // View is being deleted or not draggable, ignore request. return; // View is being deleted or not draggable, ignore request.
drag_view_ = view; drag_view_ = view;
drag_offset_ = layout_manager_->PrimaryAxisValue(event.x(), event.y()); drag_origin_ = gfx::Point(event.x(), event.y());
UMA_HISTOGRAM_ENUMERATION("Ash.ShelfAlignmentUsage", UMA_HISTOGRAM_ENUMERATION("Ash.ShelfAlignmentUsage",
layout_manager_->SelectValueForShelfAlignment( layout_manager_->SelectValueForShelfAlignment(
SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM, SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM,
...@@ -1592,8 +1591,8 @@ void ShelfView::PointerDraggedOnButton(views::View* view, ...@@ -1592,8 +1591,8 @@ void ShelfView::PointerDraggedOnButton(views::View* view,
// To prepare all drag types (moving an item in the shelf and dragging off), // To prepare all drag types (moving an item in the shelf and dragging off),
// we should check the x-axis and y-axis offset. // we should check the x-axis and y-axis offset.
if (!dragging() && drag_view_ && if (!dragging() && drag_view_ &&
((std::abs(event.x() - drag_offset_) >= kMinimumDragDistance) || ((std::abs(event.x() - drag_origin_.x()) >= kMinimumDragDistance) ||
(std::abs(event.y() - drag_offset_) >= kMinimumDragDistance))) { (std::abs(event.y() - drag_origin_.y()) >= kMinimumDragDistance))) {
PrepareForDrag(pointer, event); PrepareForDrag(pointer, event);
} }
if (drag_pointer_ == pointer) if (drag_pointer_ == pointer)
......
...@@ -358,8 +358,8 @@ class ASH_EXPORT ShelfView : public views::View, ...@@ -358,8 +358,8 @@ class ASH_EXPORT ShelfView : public views::View,
// |dragging_| is set only if the mouse is dragged far enough. // |dragging_| is set only if the mouse is dragged far enough.
views::View* drag_view_; views::View* drag_view_;
// X coordinate of the mouse down event in |drag_view_|s coordinates. // Position of the mouse down event in |drag_view_|'s coordinates.
int drag_offset_; gfx::Point drag_origin_;
// Index |drag_view_| was initially at. // Index |drag_view_| was initially at.
int start_drag_index_; int start_drag_index_;
......
...@@ -120,6 +120,32 @@ class ShelfViewIconObserverTest : public AshTestBase { ...@@ -120,6 +120,32 @@ class ShelfViewIconObserverTest : public AshTestBase {
DISALLOW_COPY_AND_ASSIGN(ShelfViewIconObserverTest); DISALLOW_COPY_AND_ASSIGN(ShelfViewIconObserverTest);
}; };
// TestShelfItemDelegate which tracks whether it gets selected.
class ShelfItemSelectionTracker : public TestShelfItemDelegate {
public:
ShelfItemSelectionTracker() : TestShelfItemDelegate(NULL), selected_(false) {
}
virtual ~ShelfItemSelectionTracker() {
}
// Returns true if the delegate was selected.
bool WasSelected() {
return selected_;
}
// TestShelfItemDelegate:
virtual bool ItemSelected(const ui::Event& event) OVERRIDE {
selected_ = true;
return false;
}
private:
bool selected_;
DISALLOW_COPY_AND_ASSIGN(ShelfItemSelectionTracker);
};
TEST_F(ShelfViewIconObserverTest, AddRemove) { TEST_F(ShelfViewIconObserverTest, AddRemove) {
TestShelfDelegate* shelf_delegate = TestShelfDelegate::instance(); TestShelfDelegate* shelf_delegate = TestShelfDelegate::instance();
ASSERT_TRUE(shelf_delegate); ASSERT_TRUE(shelf_delegate);
...@@ -398,7 +424,7 @@ class ShelfViewTest : public AshTestBase { ...@@ -398,7 +424,7 @@ class ShelfViewTest : public AshTestBase {
ShelfButtonHost* button_host = shelf_view_; ShelfButtonHost* button_host = shelf_view_;
views::View* button = test_api_->GetButton(button_index); views::View* button = test_api_->GetButton(button_index);
ui::MouseEvent click_event(ui::ET_MOUSE_PRESSED, ui::MouseEvent click_event(ui::ET_MOUSE_PRESSED,
button->bounds().origin(), gfx::Point(),
button->GetBoundsInScreen().origin(), 0, 0); button->GetBoundsInScreen().origin(), 0, 0);
button_host->PointerPressedOnButton(button, pointer, click_event); button_host->PointerPressedOnButton(button, pointer, click_event);
return button; return button;
...@@ -421,7 +447,8 @@ class ShelfViewTest : public AshTestBase { ...@@ -421,7 +447,8 @@ class ShelfViewTest : public AshTestBase {
// Drag. // Drag.
views::View* destination = test_api_->GetButton(destination_index); views::View* destination = test_api_->GetButton(destination_index);
ui::MouseEvent drag_event(ui::ET_MOUSE_DRAGGED, ui::MouseEvent drag_event(ui::ET_MOUSE_DRAGGED,
destination->bounds().origin(), gfx::Point(destination->x() - button->x(),
destination->y() - button->y()),
destination->GetBoundsInScreen().origin(), 0, 0); destination->GetBoundsInScreen().origin(), 0, 0);
button_host->PointerDraggedOnButton(button, pointer, drag_event); button_host->PointerDraggedOnButton(button, pointer, drag_event);
return button; return button;
...@@ -1030,6 +1057,54 @@ TEST_F(ShelfViewTest, ClickOneDragAnother) { ...@@ -1030,6 +1057,54 @@ TEST_F(ShelfViewTest, ClickOneDragAnother) {
EXPECT_TRUE(model_->items()[3].type == TYPE_BROWSER_SHORTCUT); EXPECT_TRUE(model_->items()[3].type == TYPE_BROWSER_SHORTCUT);
} }
// Check that clicking an item and jittering the mouse a bit still selects the
// item.
TEST_F(ShelfViewTest, ClickAndMoveSlightly) {
std::vector<std::pair<ShelfID, views::View*> > id_map;
SetupForDragTest(&id_map);
ShelfID shelf_id = (id_map.begin() + 1)->first;
views::View* button = (id_map.begin() + 1)->second;
// Replace the ShelfItemDelegate for |shelf_id| with one which tracks whether
// the shelf item gets selected.
ShelfItemSelectionTracker* selection_tracker = new ShelfItemSelectionTracker;
item_manager_->SetShelfItemDelegate(
shelf_id,
scoped_ptr<ShelfItemDelegate>(selection_tracker).Pass());
gfx::Vector2d press_offset(5, 30);
gfx::Point press_location = gfx::Point() + press_offset;
gfx::Point press_location_in_screen =
button->GetBoundsInScreen().origin() + press_offset;
ui::MouseEvent click_event(ui::ET_MOUSE_PRESSED,
press_location,
press_location_in_screen,
ui::EF_LEFT_MOUSE_BUTTON, 0);
button->OnMousePressed(click_event);
ui::MouseEvent drag_event1(ui::ET_MOUSE_DRAGGED,
press_location + gfx::Vector2d(0, 1),
press_location_in_screen + gfx::Vector2d(0, 1),
ui::EF_LEFT_MOUSE_BUTTON, 0);
button->OnMouseDragged(drag_event1);
ui::MouseEvent drag_event2(ui::ET_MOUSE_DRAGGED,
press_location + gfx::Vector2d(-1, 0),
press_location_in_screen + gfx::Vector2d(-1, 0),
ui::EF_LEFT_MOUSE_BUTTON, 0);
button->OnMouseDragged(drag_event2);
ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED,
press_location + gfx::Vector2d(-1, 0),
press_location_in_screen + gfx::Vector2d(-1, 0),
ui::EF_LEFT_MOUSE_BUTTON, 0);
button->OnMouseReleased(release_event);
EXPECT_TRUE(selection_tracker->WasSelected());
}
// Confirm that item status changes are reflected in the buttons. // Confirm that item status changes are reflected in the buttons.
TEST_F(ShelfViewTest, ShelfItemStatus) { TEST_F(ShelfViewTest, ShelfItemStatus) {
// All buttons should be visible. // All buttons should be visible.
......
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