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

Unified Diff: ui/views/view_targeter_unittest.cc

Issue 542323002: Convert ui::GestureEvent coordinates during targeting (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: mutate event location during targeting Created 6 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: ui/views/view_targeter_unittest.cc
diff --git a/ui/views/view_targeter_unittest.cc b/ui/views/view_targeter_unittest.cc
index 98110382457de6a48b2f6fdd8a1bf284a615791e..d853920364279ba5568a70f53306e17a70d503f8 100644
--- a/ui/views/view_targeter_unittest.cc
+++ b/ui/views/view_targeter_unittest.cc
@@ -292,7 +292,7 @@ TEST_F(ViewTargeterTest, ViewTargeterForGestureEvents) {
// GESTURE_END events should be targeted to the existing gesture handler,
// but re-targeting should be prohibited.
EXPECT_EQ(grandchild, targeter->FindTargetForEvent(root_view, &end));
- EXPECT_EQ(NULL, targeter->FindNextBestTarget(grandchild, &tap));
+ EXPECT_EQ(NULL, targeter->FindNextBestTarget(grandchild, &end));
// Assume that the view currently handling gestures is still set as
// |grandchild|, but this was not done by a previous gesture. Thus we are
@@ -319,6 +319,22 @@ TEST_F(ViewTargeterTest, ViewTargeterForGestureEvents) {
EXPECT_EQ(NULL, targeter->FindNextBestTarget(content, &scroll_begin));
EXPECT_EQ(NULL, targeter->FindNextBestTarget(content, &end));
+ // Reset the locations of the gesture events to be in the root view
+ // coordinate space since we are about to call FindTargetForEvent()
+ // again (calls to FindTargetForEvent() and FindNextBestTarget()
+ // mutate the location of the gesture events to be in the coordinate
+ // space of the returned view).
+ details = ui::GestureEventDetails(ui::ET_GESTURE_TAP, 0.0f, 0.0f);
+ details.set_bounding_box(bounding_box);
+ tap = GestureEventForTest(details, center_point.x(), center_point.y());
+ details = ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_BEGIN, 0.0f, 0.0f);
+ details.set_bounding_box(bounding_box);
+ scroll_begin =
+ GestureEventForTest(details, center_point.x(), center_point.y());
+ details = ui::GestureEventDetails(ui::ET_GESTURE_END, 0.0f, 0.0f);
+ details.set_bounding_box(bounding_box);
+ end = GestureEventForTest(details, center_point.x(), center_point.y());
+
// If no default gesture handler is currently set, targeting should be
// performed using the location of the gesture event for a TAP and a
// SCROLL_BEGIN.
@@ -333,6 +349,96 @@ TEST_F(ViewTargeterTest, ViewTargeterForGestureEvents) {
EXPECT_EQ(NULL, targeter->FindNextBestTarget(child, &end));
}
+// Tests that calls to FindTargetForEvent() and FindNextBestTarget() change
+// the location of a gesture event to be in the correct coordinate space.
+TEST_F(ViewTargeterTest, GestureEventCoordinateConversion) {
+ Widget widget;
+ Widget::InitParams init_params = CreateParams(Widget::InitParams::TYPE_POPUP);
+ init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
+ init_params.bounds = gfx::Rect(0, 0, 200, 200);
+ widget.Init(init_params);
+
+ // The coordinates used for SetBounds() are in the parent coordinate space.
+ View* content = new View;
+ content->SetBounds(0, 0, 100, 100);
+ View* child = new View;
+ child->SetBounds(50, 50, 20, 20);
+ View* grandchild = new View;
+ grandchild->SetBounds(5, 5, 10, 10);
+ View* great_grandchild = new View;
+ great_grandchild->SetBounds(3, 3, 4, 4);
+
+ widget.SetContentsView(content);
+ content->AddChildView(child);
+ child->AddChildView(grandchild);
+ grandchild->AddChildView(great_grandchild);
+
+ internal::RootView* root_view =
+ static_cast<internal::RootView*>(widget.GetRootView());
+ ui::EventTargeter* targeter = root_view->targeter();
+
+ // Define a GESTURE_TAP event with a bounding box centered at (60, 60)
+ // in root view coordinates with width and height of 4.
+ gfx::Rect bounding_box(gfx::Point(58, 58), gfx::Size(4, 4));
+ gfx::Point center_point(bounding_box.CenterPoint());
+ ui::GestureEventDetails details(ui::ET_GESTURE_TAP, 0.0f, 0.0f);
+ details.set_bounding_box(bounding_box);
+ GestureEventForTest tap(details, center_point.x(), center_point.y());
+
+ // Calculate the location of the gesture in each of the different
+ // coordinate spaces.
+ gfx::Point location_in_root(center_point);
+ EXPECT_EQ(gfx::Point(60, 60), location_in_root);
+ gfx::Point location_in_great_grandchild(
+ ConvertPointToView(great_grandchild, location_in_root));
+ EXPECT_EQ(gfx::Point(2, 2), location_in_great_grandchild);
+ gfx::Point location_in_grandchild(
+ ConvertPointToView(grandchild, location_in_root));
+ EXPECT_EQ(gfx::Point(5, 5), location_in_grandchild);
+ gfx::Point location_in_child(ConvertPointToView(child, location_in_root));
+ EXPECT_EQ(gfx::Point(10, 10), location_in_child);
+ gfx::Point location_in_content(ConvertPointToView(content, location_in_root));
+ EXPECT_EQ(gfx::Point(60, 60), location_in_content);
+
+ // Verify the location of |tap| is in screen coordinates.
+ EXPECT_EQ(gfx::Point(60, 60), tap.location());
+
+ // The initial target should be |great_grandchild| and the location of
+ // the event should be changed into the coordinate space of the target.
+ EXPECT_EQ(great_grandchild, targeter->FindTargetForEvent(root_view, &tap));
+ EXPECT_EQ(location_in_great_grandchild, tap.location());
+ SetGestureHandler(root_view, great_grandchild);
+
+ // The next target should be |grandchild| and the location of
+ // the event should be changed into the coordinate space of the target.
+ EXPECT_EQ(grandchild, targeter->FindNextBestTarget(great_grandchild, &tap));
+ EXPECT_EQ(location_in_grandchild, tap.location());
+ SetGestureHandler(root_view, grandchild);
+
+ // The next target should be |child| and the location of
+ // the event should be changed into the coordinate space of the target.
+ EXPECT_EQ(child, targeter->FindNextBestTarget(grandchild, &tap));
+ EXPECT_EQ(location_in_child, tap.location());
+ SetGestureHandler(root_view, child);
+
+ // The next target should be |content| and the location of
+ // the event should be changed into the coordinate space of the target.
+ EXPECT_EQ(content, targeter->FindNextBestTarget(child, &tap));
+ EXPECT_EQ(location_in_content, tap.location());
+ SetGestureHandler(root_view, content);
+
+ // The next target should be |root_view| and the location of
+ // the event should be changed into the coordinate space of the target.
+ EXPECT_EQ(widget.GetRootView(), targeter->FindNextBestTarget(content, &tap));
+ EXPECT_EQ(location_in_root, tap.location());
+ SetGestureHandler(root_view, widget.GetRootView());
+
+ // The next target should be NULL and the location of the event should
+ // remain unchanged.
+ EXPECT_EQ(NULL, targeter->FindNextBestTarget(widget.GetRootView(), &tap));
+ EXPECT_EQ(location_in_root, tap.location());
+}
+
// Tests that the functions ViewTargeterDelegate::DoesIntersectRect()
// and MaskedTargeterDelegate::DoesIntersectRect() work as intended when
// called on views which are derived from ViewTargeterDelegate.

Powered by Google App Engine
This is Rietveld 408576698