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 "ui/views/view_targeter.h"
7 #include "ui/events/event_targeter.h"
8 #include "ui/events/event_utils.h"
9 #include "ui/gfx/path.h"
10 #include "ui/views/masked_targeter_delegate.h"
11 #include "ui/views/test/views_test_base.h"
12 #include "ui/views/view_targeter.h"
13 #include "ui/views/view_targeter_delegate.h"
14 #include "ui/views/views_switches.h"
15 #include "ui/views/widget/root_view.h"
19 // A derived class of View used for testing purposes.
20 class TestingView
: public View
, public ViewTargeterDelegate
{
22 TestingView() : can_process_events_within_subtree_(true) {}
23 ~TestingView() override
{}
25 // Reset all test state.
26 void Reset() { can_process_events_within_subtree_
= true; }
28 void set_can_process_events_within_subtree(bool can_process
) {
29 can_process_events_within_subtree_
= can_process
;
32 // A call-through function to ViewTargeterDelegate::DoesIntersectRect().
33 bool TestDoesIntersectRect(const View
* target
, const gfx::Rect
& rect
) const {
34 return DoesIntersectRect(target
, rect
);
38 bool CanProcessEventsWithinSubtree() const override
{
39 return can_process_events_within_subtree_
;
43 // Value to return from CanProcessEventsWithinSubtree().
44 bool can_process_events_within_subtree_
;
46 DISALLOW_COPY_AND_ASSIGN(TestingView
);
49 // A derived class of View having a triangular-shaped hit test mask.
50 class TestMaskedView
: public View
, public MaskedTargeterDelegate
{
53 ~TestMaskedView() override
{}
55 // A call-through function to MaskedTargeterDelegate::DoesIntersectRect().
56 bool TestDoesIntersectRect(const View
* target
, const gfx::Rect
& rect
) const {
57 return DoesIntersectRect(target
, rect
);
61 // MaskedTargeterDelegate:
62 bool GetHitTestMask(gfx::Path
* mask
) const override
{
64 SkScalar w
= SkIntToScalar(width());
65 SkScalar h
= SkIntToScalar(height());
67 // Create a triangular mask within the bounds of this View.
68 mask
->moveTo(w
/ 2, 0);
75 DISALLOW_COPY_AND_ASSIGN(TestMaskedView
);
80 // TODO(tdanderson): Clean up this test suite by moving common code/state into
81 // ViewTargeterTest and overriding SetUp(), TearDown(), etc.
82 // See crbug.com/355680.
83 class ViewTargeterTest
: public ViewsTestBase
{
86 ~ViewTargeterTest() override
{}
88 void SetGestureHandler(internal::RootView
* root_view
, View
* handler
) {
89 root_view
->gesture_handler_
= handler
;
92 void SetGestureHandlerSetBeforeProcessing(internal::RootView
* root_view
,
94 root_view
->gesture_handler_set_before_processing_
= set
;
98 DISALLOW_COPY_AND_ASSIGN(ViewTargeterTest
);
103 gfx::Point
ConvertPointFromWidgetToView(View
* view
, const gfx::Point
& p
) {
105 View::ConvertPointToTarget(view
->GetWidget()->GetRootView(), view
, &tmp
);
109 gfx::Rect
ConvertRectFromWidgetToView(View
* view
, const gfx::Rect
& r
) {
111 tmp
.set_origin(ConvertPointFromWidgetToView(view
, r
.origin()));
117 // Verifies that the the functions ViewTargeter::FindTargetForEvent()
118 // and ViewTargeter::FindNextBestTarget() are implemented correctly
120 TEST_F(ViewTargeterTest
, ViewTargeterForKeyEvents
) {
122 Widget::InitParams init_params
=
123 CreateParams(Widget::InitParams::TYPE_POPUP
);
124 init_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
125 widget
.Init(init_params
);
127 View
* content
= new View
;
128 View
* child
= new View
;
129 View
* grandchild
= new View
;
131 widget
.SetContentsView(content
);
132 content
->AddChildView(child
);
133 child
->AddChildView(grandchild
);
135 grandchild
->SetFocusable(true);
136 grandchild
->RequestFocus();
138 internal::RootView
* root_view
=
139 static_cast<internal::RootView
*>(widget
.GetRootView());
140 ui::EventTargeter
* targeter
= root_view
->targeter();
142 ui::KeyEvent
key_event('a', ui::VKEY_A
, ui::EF_NONE
);
144 // The focused view should be the initial target of the event.
145 ui::EventTarget
* current_target
= targeter
->FindTargetForEvent(root_view
,
147 EXPECT_EQ(grandchild
, static_cast<View
*>(current_target
));
149 // Verify that FindNextBestTarget() will return the parent view of the
150 // argument (and NULL if the argument has no parent view).
151 current_target
= targeter
->FindNextBestTarget(grandchild
, &key_event
);
152 EXPECT_EQ(child
, static_cast<View
*>(current_target
));
153 current_target
= targeter
->FindNextBestTarget(child
, &key_event
);
154 EXPECT_EQ(content
, static_cast<View
*>(current_target
));
155 current_target
= targeter
->FindNextBestTarget(content
, &key_event
);
156 EXPECT_EQ(widget
.GetRootView(), static_cast<View
*>(current_target
));
157 current_target
= targeter
->FindNextBestTarget(widget
.GetRootView(),
159 EXPECT_EQ(NULL
, static_cast<View
*>(current_target
));
162 // Verifies that the the functions ViewTargeter::FindTargetForEvent()
163 // and ViewTargeter::FindNextBestTarget() are implemented correctly
164 // for scroll events.
165 TEST_F(ViewTargeterTest
, ViewTargeterForScrollEvents
) {
167 Widget::InitParams init_params
=
168 CreateParams(Widget::InitParams::TYPE_POPUP
);
169 init_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
170 init_params
.bounds
= gfx::Rect(0, 0, 200, 200);
171 widget
.Init(init_params
);
173 // The coordinates used for SetBounds() are in the parent coordinate space.
174 View
* content
= new View
;
175 content
->SetBounds(0, 0, 100, 100);
176 View
* child
= new View
;
177 child
->SetBounds(50, 50, 20, 20);
178 View
* grandchild
= new View
;
179 grandchild
->SetBounds(0, 0, 5, 5);
181 widget
.SetContentsView(content
);
182 content
->AddChildView(child
);
183 child
->AddChildView(grandchild
);
185 internal::RootView
* root_view
=
186 static_cast<internal::RootView
*>(widget
.GetRootView());
187 ui::EventTargeter
* targeter
= root_view
->targeter();
189 // The event falls within the bounds of |child| and |content| but not
190 // |grandchild|, so |child| should be the initial target for the event.
191 ui::ScrollEvent
scroll(ui::ET_SCROLL
,
193 ui::EventTimeForNow(),
198 ui::EventTarget
* current_target
= targeter
->FindTargetForEvent(root_view
,
200 EXPECT_EQ(child
, static_cast<View
*>(current_target
));
202 // Verify that FindNextBestTarget() will return the parent view of the
203 // argument (and NULL if the argument has no parent view).
204 current_target
= targeter
->FindNextBestTarget(child
, &scroll
);
205 EXPECT_EQ(content
, static_cast<View
*>(current_target
));
206 current_target
= targeter
->FindNextBestTarget(content
, &scroll
);
207 EXPECT_EQ(widget
.GetRootView(), static_cast<View
*>(current_target
));
208 current_target
= targeter
->FindNextBestTarget(widget
.GetRootView(),
210 EXPECT_EQ(NULL
, static_cast<View
*>(current_target
));
212 // The event falls outside of the original specified bounds of |content|,
213 // |child|, and |grandchild|. But since |content| is the contents view,
214 // and contents views are resized to fill the entire area of the root
215 // view, the event's initial target should still be |content|.
216 scroll
= ui::ScrollEvent(ui::ET_SCROLL
,
217 gfx::Point(150, 150),
218 ui::EventTimeForNow(),
223 current_target
= targeter
->FindTargetForEvent(root_view
, &scroll
);
224 EXPECT_EQ(content
, static_cast<View
*>(current_target
));
227 // Convenience to make constructing a GestureEvent simpler.
228 class GestureEventForTest
: public ui::GestureEvent
{
230 GestureEventForTest(ui::EventType type
, int x
, int y
)
235 ui::GestureEventDetails(type
)) {}
237 GestureEventForTest(ui::GestureEventDetails details
)
238 : GestureEvent(details
.bounding_box().CenterPoint().x(),
239 details
.bounding_box().CenterPoint().y(),
245 // Verifies that the the functions ViewTargeter::FindTargetForEvent()
246 // and ViewTargeter::FindNextBestTarget() are implemented correctly
247 // for gesture events.
248 TEST_F(ViewTargeterTest
, ViewTargeterForGestureEvents
) {
250 Widget::InitParams init_params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
251 init_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
252 init_params
.bounds
= gfx::Rect(0, 0, 200, 200);
253 widget
.Init(init_params
);
255 // The coordinates used for SetBounds() are in the parent coordinate space.
256 View
* content
= new View
;
257 content
->SetBounds(0, 0, 100, 100);
258 View
* child
= new View
;
259 child
->SetBounds(50, 50, 20, 20);
260 View
* grandchild
= new View
;
261 grandchild
->SetBounds(0, 0, 5, 5);
263 widget
.SetContentsView(content
);
264 content
->AddChildView(child
);
265 child
->AddChildView(grandchild
);
267 internal::RootView
* root_view
=
268 static_cast<internal::RootView
*>(widget
.GetRootView());
269 ui::EventTargeter
* targeter
= root_view
->targeter();
271 // Define some gesture events for testing.
272 gfx::RectF
bounding_box(gfx::PointF(46.f
, 46.f
), gfx::SizeF(8.f
, 8.f
));
273 ui::GestureEventDetails
details(ui::ET_GESTURE_TAP
);
274 details
.set_bounding_box(bounding_box
);
275 GestureEventForTest
tap(details
);
276 details
= ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_BEGIN
);
277 details
.set_bounding_box(bounding_box
);
278 GestureEventForTest
scroll_begin(details
);
279 details
= ui::GestureEventDetails(ui::ET_GESTURE_END
);
280 details
.set_bounding_box(bounding_box
);
281 GestureEventForTest
end(details
);
283 // Assume that the view currently handling gestures has been set as
284 // |grandchild| by a previous gesture event. Thus subsequent TAP and
285 // SCROLL_BEGIN events should be initially targeted to |grandchild|, and
286 // re-targeting should be prohibited for TAP but permitted for
287 // GESTURE_SCROLL_BEGIN (which should be re-targeted to the parent of
289 SetGestureHandlerSetBeforeProcessing(root_view
, true);
290 SetGestureHandler(root_view
, grandchild
);
291 EXPECT_EQ(grandchild
, targeter
->FindTargetForEvent(root_view
, &tap
));
292 EXPECT_EQ(NULL
, targeter
->FindNextBestTarget(grandchild
, &tap
));
293 EXPECT_EQ(grandchild
, targeter
->FindTargetForEvent(root_view
, &scroll_begin
));
294 EXPECT_EQ(child
, targeter
->FindNextBestTarget(grandchild
, &scroll_begin
));
296 // GESTURE_END events should be targeted to the existing gesture handler,
297 // but re-targeting should be prohibited.
298 EXPECT_EQ(grandchild
, targeter
->FindTargetForEvent(root_view
, &end
));
299 EXPECT_EQ(NULL
, targeter
->FindNextBestTarget(grandchild
, &end
));
301 // Assume that the view currently handling gestures is still set as
302 // |grandchild|, but this was not done by a previous gesture. Thus we are
303 // in the process of finding the View to which subsequent gestures will be
304 // dispatched, so TAP and SCROLL_BEGIN events should be re-targeted up
305 // the ancestor chain.
306 SetGestureHandlerSetBeforeProcessing(root_view
, false);
307 EXPECT_EQ(child
, targeter
->FindNextBestTarget(grandchild
, &tap
));
308 EXPECT_EQ(child
, targeter
->FindNextBestTarget(grandchild
, &scroll_begin
));
310 // GESTURE_END events are not permitted to be re-targeted up the ancestor
311 // chain; they are only ever targeted in the case where the gesture handler
312 // was established by a previous gesture.
313 EXPECT_EQ(NULL
, targeter
->FindNextBestTarget(grandchild
, &end
));
315 // Assume that the default gesture handler was set by the previous gesture,
316 // but that this handler is currently NULL. No gesture events should be
317 // re-targeted in this case (regardless of the view that is passed in to
318 // FindNextBestTarget() as the previous target).
319 SetGestureHandler(root_view
, NULL
);
320 SetGestureHandlerSetBeforeProcessing(root_view
, true);
321 EXPECT_EQ(NULL
, targeter
->FindNextBestTarget(child
, &tap
));
322 EXPECT_EQ(NULL
, targeter
->FindNextBestTarget(NULL
, &tap
));
323 EXPECT_EQ(NULL
, targeter
->FindNextBestTarget(content
, &scroll_begin
));
324 EXPECT_EQ(NULL
, targeter
->FindNextBestTarget(content
, &end
));
326 // Reset the locations of the gesture events to be in the root view
327 // coordinate space since we are about to call FindTargetForEvent()
328 // again (calls to FindTargetForEvent() and FindNextBestTarget()
329 // mutate the location of the gesture events to be in the coordinate
330 // space of the returned view).
331 details
= ui::GestureEventDetails(ui::ET_GESTURE_TAP
);
332 details
.set_bounding_box(bounding_box
);
333 tap
= GestureEventForTest(details
);
334 details
= ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_BEGIN
);
335 details
.set_bounding_box(bounding_box
);
336 scroll_begin
= GestureEventForTest(details
);
337 details
= ui::GestureEventDetails(ui::ET_GESTURE_END
);
338 details
.set_bounding_box(bounding_box
);
339 end
= GestureEventForTest(details
);
341 // If no default gesture handler is currently set, targeting should be
342 // performed using the location of the gesture event for a TAP and a
344 SetGestureHandlerSetBeforeProcessing(root_view
, false);
345 EXPECT_EQ(grandchild
, targeter
->FindTargetForEvent(root_view
, &tap
));
346 EXPECT_EQ(grandchild
, targeter
->FindTargetForEvent(root_view
, &scroll_begin
));
348 // If no default gesture handler is currently set, GESTURE_END events
349 // should never be re-targeted to any View.
350 EXPECT_EQ(NULL
, targeter
->FindNextBestTarget(NULL
, &end
));
351 EXPECT_EQ(NULL
, targeter
->FindNextBestTarget(child
, &end
));
354 // Tests that the contents view is targeted instead of the root view for
355 // gesture events that should be targeted to the contents view. Also
356 // tests that the root view is targeted for gesture events which should
357 // not be targeted to any other view in the views tree.
358 TEST_F(ViewTargeterTest
, TargetContentsAndRootView
) {
360 Widget::InitParams init_params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
361 init_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
362 init_params
.bounds
= gfx::Rect(0, 0, 200, 200);
363 widget
.Init(init_params
);
365 // The coordinates used for SetBounds() are in the parent coordinate space.
366 View
* content
= new View
;
367 content
->SetBounds(0, 0, 100, 100);
368 widget
.SetContentsView(content
);
370 internal::RootView
* root_view
=
371 static_cast<internal::RootView
*>(widget
.GetRootView());
372 ui::EventTargeter
* targeter
= root_view
->targeter();
374 // A gesture event located entirely within the contents view should
375 // target the contents view.
376 gfx::RectF
bounding_box(gfx::PointF(96.f
, 96.f
), gfx::SizeF(8.f
, 8.f
));
377 ui::GestureEventDetails
details(ui::ET_GESTURE_TAP
);
378 details
.set_bounding_box(bounding_box
);
379 GestureEventForTest
tap(details
);
381 EXPECT_EQ(content
, targeter
->FindTargetForEvent(root_view
, &tap
));
383 // A gesture event not located entirely within the contents view but
384 // having its center within the contents view should target
385 // the contents view.
386 bounding_box
= gfx::RectF(gfx::PointF(194.f
, 100.f
), gfx::SizeF(8.f
, 8.f
));
387 details
.set_bounding_box(bounding_box
);
388 tap
= GestureEventForTest(details
);
390 EXPECT_EQ(content
, targeter
->FindTargetForEvent(root_view
, &tap
));
392 // A gesture event with its center not located within the contents
393 // view but that overlaps the contents view by at least 60% should
394 // target the contents view.
395 bounding_box
= gfx::RectF(gfx::PointF(50.f
, 0.f
), gfx::SizeF(400.f
, 200.f
));
396 details
.set_bounding_box(bounding_box
);
397 tap
= GestureEventForTest(details
);
399 // This only applies if rect-based targeting is enabled.
400 if (views::switches::IsRectBasedTargetingEnabled()) {
401 EXPECT_EQ(content
, targeter
->FindTargetForEvent(root_view
, &tap
));
403 EXPECT_EQ(widget
.GetRootView(),
404 targeter
->FindTargetForEvent(root_view
, &tap
));
407 // A gesture event not overlapping the contents view by at least
408 // 60% and not having its center within the contents view should
409 // be targeted to the root view.
410 bounding_box
= gfx::RectF(gfx::PointF(196.f
, 100.f
), gfx::SizeF(8.f
, 8.f
));
411 details
.set_bounding_box(bounding_box
);
412 tap
= GestureEventForTest(details
);
414 EXPECT_EQ(widget
.GetRootView(),
415 targeter
->FindTargetForEvent(root_view
, &tap
));
417 // A gesture event completely outside the contents view should be targeted
419 bounding_box
= gfx::RectF(gfx::PointF(205.f
, 100.f
), gfx::SizeF(8.f
, 8.f
));
420 details
.set_bounding_box(bounding_box
);
421 tap
= GestureEventForTest(details
);
423 EXPECT_EQ(widget
.GetRootView(),
424 targeter
->FindTargetForEvent(root_view
, &tap
));
426 // A gesture event with dimensions 1x1 located entirely within the
427 // contents view should target the contents view.
428 bounding_box
= gfx::RectF(gfx::PointF(175.f
, 100.f
), gfx::SizeF(1.f
, 1.f
));
429 details
.set_bounding_box(bounding_box
);
430 tap
= GestureEventForTest(details
);
432 EXPECT_EQ(content
, targeter
->FindTargetForEvent(root_view
, &tap
));
434 // A gesture event with dimensions 1x1 located entirely outside the
435 // contents view should be targeted to the root view.
436 bounding_box
= gfx::RectF(gfx::PointF(205.f
, 100.f
), gfx::SizeF(1.f
, 1.f
));
437 details
.set_bounding_box(bounding_box
);
438 tap
= GestureEventForTest(details
);
440 EXPECT_EQ(widget
.GetRootView(),
441 targeter
->FindTargetForEvent(root_view
, &tap
));
444 // Tests that calls to FindTargetForEvent() and FindNextBestTarget() change
445 // the location of a gesture event to be in the correct coordinate space.
446 TEST_F(ViewTargeterTest
, GestureEventCoordinateConversion
) {
448 Widget::InitParams init_params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
449 init_params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
450 init_params
.bounds
= gfx::Rect(0, 0, 200, 200);
451 widget
.Init(init_params
);
453 // The coordinates used for SetBounds() are in the parent coordinate space.
454 View
* content
= new View
;
455 content
->SetBounds(0, 0, 100, 100);
456 View
* child
= new View
;
457 child
->SetBounds(50, 50, 20, 20);
458 View
* grandchild
= new View
;
459 grandchild
->SetBounds(5, 5, 10, 10);
460 View
* great_grandchild
= new View
;
461 great_grandchild
->SetBounds(3, 3, 4, 4);
463 widget
.SetContentsView(content
);
464 content
->AddChildView(child
);
465 child
->AddChildView(grandchild
);
466 grandchild
->AddChildView(great_grandchild
);
468 internal::RootView
* root_view
=
469 static_cast<internal::RootView
*>(widget
.GetRootView());
470 ui::EventTargeter
* targeter
= root_view
->targeter();
472 // Define a GESTURE_TAP event with a bounding box centered at (60, 60)
473 // in root view coordinates with width and height of 4.
474 gfx::RectF
bounding_box(gfx::PointF(58.f
, 58.f
), gfx::SizeF(4.f
, 4.f
));
475 gfx::PointF
center_point(bounding_box
.CenterPoint());
476 ui::GestureEventDetails
details(ui::ET_GESTURE_TAP
);
477 details
.set_bounding_box(bounding_box
);
478 GestureEventForTest
tap(details
);
480 // Calculate the location of the gesture in each of the different
481 // coordinate spaces.
482 gfx::Point
location_in_root(gfx::ToFlooredPoint(center_point
));
483 EXPECT_EQ(gfx::Point(60, 60), location_in_root
);
484 gfx::Point
location_in_great_grandchild(
485 ConvertPointFromWidgetToView(great_grandchild
, location_in_root
));
486 EXPECT_EQ(gfx::Point(2, 2), location_in_great_grandchild
);
487 gfx::Point
location_in_grandchild(
488 ConvertPointFromWidgetToView(grandchild
, location_in_root
));
489 EXPECT_EQ(gfx::Point(5, 5), location_in_grandchild
);
490 gfx::Point
location_in_child(
491 ConvertPointFromWidgetToView(child
, location_in_root
));
492 EXPECT_EQ(gfx::Point(10, 10), location_in_child
);
493 gfx::Point
location_in_content(
494 ConvertPointFromWidgetToView(content
, location_in_root
));
495 EXPECT_EQ(gfx::Point(60, 60), location_in_content
);
497 // Verify the location of |tap| is in screen coordinates.
498 EXPECT_EQ(gfx::Point(60, 60), tap
.location());
500 // The initial target should be |great_grandchild| and the location of
501 // the event should be changed into the coordinate space of the target.
502 EXPECT_EQ(great_grandchild
, targeter
->FindTargetForEvent(root_view
, &tap
));
503 EXPECT_EQ(location_in_great_grandchild
, tap
.location());
504 SetGestureHandler(root_view
, great_grandchild
);
506 // The next target should be |grandchild| and the location of
507 // the event should be changed into the coordinate space of the target.
508 EXPECT_EQ(grandchild
, targeter
->FindNextBestTarget(great_grandchild
, &tap
));
509 EXPECT_EQ(location_in_grandchild
, tap
.location());
510 SetGestureHandler(root_view
, grandchild
);
512 // The next target should be |child| and the location of
513 // the event should be changed into the coordinate space of the target.
514 EXPECT_EQ(child
, targeter
->FindNextBestTarget(grandchild
, &tap
));
515 EXPECT_EQ(location_in_child
, tap
.location());
516 SetGestureHandler(root_view
, child
);
518 // The next target should be |content| and the location of
519 // the event should be changed into the coordinate space of the target.
520 EXPECT_EQ(content
, targeter
->FindNextBestTarget(child
, &tap
));
521 EXPECT_EQ(location_in_content
, tap
.location());
522 SetGestureHandler(root_view
, content
);
524 // The next target should be |root_view| and the location of
525 // the event should be changed into the coordinate space of the target.
526 EXPECT_EQ(widget
.GetRootView(), targeter
->FindNextBestTarget(content
, &tap
));
527 EXPECT_EQ(location_in_root
, tap
.location());
528 SetGestureHandler(root_view
, widget
.GetRootView());
530 // The next target should be NULL and the location of the event should
532 EXPECT_EQ(NULL
, targeter
->FindNextBestTarget(widget
.GetRootView(), &tap
));
533 EXPECT_EQ(location_in_root
, tap
.location());
536 // Tests that the functions ViewTargeterDelegate::DoesIntersectRect()
537 // and MaskedTargeterDelegate::DoesIntersectRect() work as intended when
538 // called on views which are derived from ViewTargeterDelegate.
539 // Also verifies that ViewTargeterDelegate::DoesIntersectRect() can
540 // be called from the ViewTargeter installed on RootView.
541 TEST_F(ViewTargeterTest
, DoesIntersectRect
) {
543 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
544 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
545 params
.bounds
= gfx::Rect(0, 0, 650, 650);
548 internal::RootView
* root_view
=
549 static_cast<internal::RootView
*>(widget
.GetRootView());
550 ViewTargeter
* view_targeter
= root_view
->targeter();
552 // The coordinates used for SetBounds() are in the parent coordinate space.
554 TestMaskedView v1
, v3
;
555 v1
.SetBounds(0, 0, 200, 200);
556 v2
.SetBounds(300, 0, 300, 300);
557 v3
.SetBounds(0, 0, 100, 100);
558 root_view
->AddChildView(&v1
);
559 root_view
->AddChildView(&v2
);
560 v2
.AddChildView(&v3
);
562 // The coordinates used below are in the local coordinate space of the
563 // view that is passed in as an argument.
565 // Hit tests against |v1|, which has a hit test mask.
566 EXPECT_TRUE(v1
.TestDoesIntersectRect(&v1
, gfx::Rect(0, 0, 200, 200)));
567 EXPECT_TRUE(v1
.TestDoesIntersectRect(&v1
, gfx::Rect(-10, -10, 110, 12)));
568 EXPECT_TRUE(v1
.TestDoesIntersectRect(&v1
, gfx::Rect(112, 142, 1, 1)));
569 EXPECT_FALSE(v1
.TestDoesIntersectRect(&v1
, gfx::Rect(0, 0, 20, 20)));
570 EXPECT_FALSE(v1
.TestDoesIntersectRect(&v1
, gfx::Rect(-10, -10, 90, 12)));
571 EXPECT_FALSE(v1
.TestDoesIntersectRect(&v1
, gfx::Rect(150, 49, 1, 1)));
573 // Hit tests against |v2|, which does not have a hit test mask.
574 EXPECT_TRUE(v2
.TestDoesIntersectRect(&v2
, gfx::Rect(0, 0, 200, 200)));
575 EXPECT_TRUE(v2
.TestDoesIntersectRect(&v2
, gfx::Rect(-10, 250, 60, 60)));
576 EXPECT_TRUE(v2
.TestDoesIntersectRect(&v2
, gfx::Rect(250, 250, 1, 1)));
577 EXPECT_FALSE(v2
.TestDoesIntersectRect(&v2
, gfx::Rect(-10, 250, 7, 7)));
578 EXPECT_FALSE(v2
.TestDoesIntersectRect(&v2
, gfx::Rect(-1, -1, 1, 1)));
580 // Hit tests against |v3|, which has a hit test mask and is a child of |v2|.
581 EXPECT_TRUE(v3
.TestDoesIntersectRect(&v3
, gfx::Rect(0, 0, 50, 50)));
582 EXPECT_TRUE(v3
.TestDoesIntersectRect(&v3
, gfx::Rect(90, 90, 1, 1)));
583 EXPECT_FALSE(v3
.TestDoesIntersectRect(&v3
, gfx::Rect(10, 125, 50, 50)));
584 EXPECT_FALSE(v3
.TestDoesIntersectRect(&v3
, gfx::Rect(110, 110, 1, 1)));
586 // Verify that hit-testing is performed correctly when using the
587 // call-through function ViewTargeter::DoesIntersectRect().
588 EXPECT_TRUE(view_targeter
->DoesIntersectRect(root_view
,
589 gfx::Rect(0, 0, 50, 50)));
590 EXPECT_FALSE(view_targeter
->DoesIntersectRect(root_view
,
591 gfx::Rect(-20, -20, 10, 10)));
594 // Tests that calls made directly on the hit-testing methods in View
595 // (HitTestPoint(), HitTestRect(), etc.) return the correct values.
596 TEST_F(ViewTargeterTest
, HitTestCallsOnView
) {
597 // The coordinates in this test are in the coordinate space of the root view.
598 Widget
* widget
= new Widget
;
599 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_POPUP
);
600 widget
->Init(params
);
601 View
* root_view
= widget
->GetRootView();
602 root_view
->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
604 // |v1| has no hit test mask. No ViewTargeter is installed on |v1|, which
605 // means that View::HitTestRect() will call into the targeter installed on
606 // the root view instead when we hit test against |v1|.
607 gfx::Rect v1_bounds
= gfx::Rect(0, 0, 100, 100);
608 TestingView
* v1
= new TestingView();
609 v1
->SetBoundsRect(v1_bounds
);
610 root_view
->AddChildView(v1
);
612 // |v2| has a triangular hit test mask. Install a ViewTargeter on |v2| which
613 // will be called into by View::HitTestRect().
614 gfx::Rect v2_bounds
= gfx::Rect(105, 0, 100, 100);
615 TestMaskedView
* v2
= new TestMaskedView();
616 v2
->SetBoundsRect(v2_bounds
);
617 root_view
->AddChildView(v2
);
618 ViewTargeter
* view_targeter
= new ViewTargeter(v2
);
619 v2
->SetEventTargeter(make_scoped_ptr(view_targeter
));
621 gfx::Point v1_centerpoint
= v1_bounds
.CenterPoint();
622 gfx::Point v2_centerpoint
= v2_bounds
.CenterPoint();
623 gfx::Point v1_origin
= v1_bounds
.origin();
624 gfx::Point v2_origin
= v2_bounds
.origin();
625 gfx::Rect
r1(10, 10, 110, 15);
626 gfx::Rect
r2(106, 1, 98, 98);
627 gfx::Rect
r3(0, 0, 300, 300);
628 gfx::Rect
r4(115, 342, 200, 10);
630 // Test calls into View::HitTestPoint().
632 v1
->HitTestPoint(ConvertPointFromWidgetToView(v1
, v1_centerpoint
)));
634 v2
->HitTestPoint(ConvertPointFromWidgetToView(v2
, v2_centerpoint
)));
636 EXPECT_TRUE(v1
->HitTestPoint(ConvertPointFromWidgetToView(v1
, v1_origin
)));
637 EXPECT_FALSE(v2
->HitTestPoint(ConvertPointFromWidgetToView(v2
, v2_origin
)));
639 // Test calls into View::HitTestRect().
640 EXPECT_TRUE(v1
->HitTestRect(ConvertRectFromWidgetToView(v1
, r1
)));
641 EXPECT_FALSE(v2
->HitTestRect(ConvertRectFromWidgetToView(v2
, r1
)));
643 EXPECT_FALSE(v1
->HitTestRect(ConvertRectFromWidgetToView(v1
, r2
)));
644 EXPECT_TRUE(v2
->HitTestRect(ConvertRectFromWidgetToView(v2
, r2
)));
646 EXPECT_TRUE(v1
->HitTestRect(ConvertRectFromWidgetToView(v1
, r3
)));
647 EXPECT_TRUE(v2
->HitTestRect(ConvertRectFromWidgetToView(v2
, r3
)));
649 EXPECT_FALSE(v1
->HitTestRect(ConvertRectFromWidgetToView(v1
, r4
)));
650 EXPECT_FALSE(v2
->HitTestRect(ConvertRectFromWidgetToView(v2
, r4
)));
652 // Test calls into View::GetEventHandlerForPoint().
653 EXPECT_EQ(v1
, root_view
->GetEventHandlerForPoint(v1_centerpoint
));
654 EXPECT_EQ(v2
, root_view
->GetEventHandlerForPoint(v2_centerpoint
));
656 EXPECT_EQ(v1
, root_view
->GetEventHandlerForPoint(v1_origin
));
657 EXPECT_EQ(root_view
, root_view
->GetEventHandlerForPoint(v2_origin
));
659 // Test calls into View::GetTooltipHandlerForPoint().
660 EXPECT_EQ(v1
, root_view
->GetTooltipHandlerForPoint(v1_centerpoint
));
661 EXPECT_EQ(v2
, root_view
->GetTooltipHandlerForPoint(v2_centerpoint
));
663 EXPECT_EQ(v1
, root_view
->GetTooltipHandlerForPoint(v1_origin
));
664 EXPECT_EQ(root_view
, root_view
->GetTooltipHandlerForPoint(v2_origin
));
666 EXPECT_FALSE(v1
->GetTooltipHandlerForPoint(v2_origin
));