Clean up Throbber.
[chromium-blink-merge.git] / ui / views / view_targeter_unittest.cc
blob3abe606dcb5d4341b244f31be0fc5e06ac10cad8
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/widget/root_view.h"
16 namespace views {
18 // A derived class of View used for testing purposes.
19 class TestingView : public View, public ViewTargeterDelegate {
20 public:
21 TestingView() : can_process_events_within_subtree_(true) {}
22 ~TestingView() override {}
24 // Reset all test state.
25 void Reset() { can_process_events_within_subtree_ = true; }
27 void set_can_process_events_within_subtree(bool can_process) {
28 can_process_events_within_subtree_ = can_process;
31 // A call-through function to ViewTargeterDelegate::DoesIntersectRect().
32 bool TestDoesIntersectRect(const View* target, const gfx::Rect& rect) const {
33 return DoesIntersectRect(target, rect);
36 // View:
37 bool CanProcessEventsWithinSubtree() const override {
38 return can_process_events_within_subtree_;
41 private:
42 // Value to return from CanProcessEventsWithinSubtree().
43 bool can_process_events_within_subtree_;
45 DISALLOW_COPY_AND_ASSIGN(TestingView);
48 // A derived class of View having a triangular-shaped hit test mask.
49 class TestMaskedView : public View, public MaskedTargeterDelegate {
50 public:
51 TestMaskedView() {}
52 ~TestMaskedView() override {}
54 // A call-through function to MaskedTargeterDelegate::DoesIntersectRect().
55 bool TestDoesIntersectRect(const View* target, const gfx::Rect& rect) const {
56 return DoesIntersectRect(target, rect);
59 private:
60 // MaskedTargeterDelegate:
61 bool GetHitTestMask(gfx::Path* mask) const override {
62 DCHECK(mask);
63 SkScalar w = SkIntToScalar(width());
64 SkScalar h = SkIntToScalar(height());
66 // Create a triangular mask within the bounds of this View.
67 mask->moveTo(w / 2, 0);
68 mask->lineTo(w, h);
69 mask->lineTo(0, h);
70 mask->close();
71 return true;
74 DISALLOW_COPY_AND_ASSIGN(TestMaskedView);
77 namespace test {
79 // TODO(tdanderson): Clean up this test suite by moving common code/state into
80 // ViewTargeterTest and overriding SetUp(), TearDown(), etc.
81 // See crbug.com/355680.
82 class ViewTargeterTest : public ViewsTestBase {
83 public:
84 ViewTargeterTest() {}
85 ~ViewTargeterTest() override {}
87 void SetGestureHandler(internal::RootView* root_view, View* handler) {
88 root_view->gesture_handler_ = handler;
91 void SetGestureHandlerSetBeforeProcessing(internal::RootView* root_view,
92 bool set) {
93 root_view->gesture_handler_set_before_processing_ = set;
96 private:
97 DISALLOW_COPY_AND_ASSIGN(ViewTargeterTest);
100 namespace {
102 gfx::Point ConvertPointFromWidgetToView(View* view, const gfx::Point& p) {
103 gfx::Point tmp(p);
104 View::ConvertPointToTarget(view->GetWidget()->GetRootView(), view, &tmp);
105 return tmp;
108 gfx::Rect ConvertRectFromWidgetToView(View* view, const gfx::Rect& r) {
109 gfx::Rect tmp(r);
110 tmp.set_origin(ConvertPointFromWidgetToView(view, r.origin()));
111 return tmp;
114 } // namespace
116 // Verifies that the the functions ViewTargeter::FindTargetForEvent()
117 // and ViewTargeter::FindNextBestTarget() are implemented correctly
118 // for key events.
119 TEST_F(ViewTargeterTest, ViewTargeterForKeyEvents) {
120 Widget widget;
121 Widget::InitParams init_params =
122 CreateParams(Widget::InitParams::TYPE_POPUP);
123 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
124 widget.Init(init_params);
126 View* content = new View;
127 View* child = new View;
128 View* grandchild = new View;
130 widget.SetContentsView(content);
131 content->AddChildView(child);
132 child->AddChildView(grandchild);
134 grandchild->SetFocusable(true);
135 grandchild->RequestFocus();
137 internal::RootView* root_view =
138 static_cast<internal::RootView*>(widget.GetRootView());
139 ui::EventTargeter* targeter = root_view->targeter();
141 ui::KeyEvent key_event('a', ui::VKEY_A, ui::EF_NONE);
143 // The focused view should be the initial target of the event.
144 ui::EventTarget* current_target = targeter->FindTargetForEvent(root_view,
145 &key_event);
146 EXPECT_EQ(grandchild, static_cast<View*>(current_target));
148 // Verify that FindNextBestTarget() will return the parent view of the
149 // argument (and NULL if the argument has no parent view).
150 current_target = targeter->FindNextBestTarget(grandchild, &key_event);
151 EXPECT_EQ(child, static_cast<View*>(current_target));
152 current_target = targeter->FindNextBestTarget(child, &key_event);
153 EXPECT_EQ(content, static_cast<View*>(current_target));
154 current_target = targeter->FindNextBestTarget(content, &key_event);
155 EXPECT_EQ(widget.GetRootView(), static_cast<View*>(current_target));
156 current_target = targeter->FindNextBestTarget(widget.GetRootView(),
157 &key_event);
158 EXPECT_EQ(NULL, static_cast<View*>(current_target));
161 // Verifies that the the functions ViewTargeter::FindTargetForEvent()
162 // and ViewTargeter::FindNextBestTarget() are implemented correctly
163 // for scroll events.
164 TEST_F(ViewTargeterTest, ViewTargeterForScrollEvents) {
165 Widget widget;
166 Widget::InitParams init_params =
167 CreateParams(Widget::InitParams::TYPE_POPUP);
168 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
169 init_params.bounds = gfx::Rect(0, 0, 200, 200);
170 widget.Init(init_params);
172 // The coordinates used for SetBounds() are in the parent coordinate space.
173 View* content = new View;
174 content->SetBounds(0, 0, 100, 100);
175 View* child = new View;
176 child->SetBounds(50, 50, 20, 20);
177 View* grandchild = new View;
178 grandchild->SetBounds(0, 0, 5, 5);
180 widget.SetContentsView(content);
181 content->AddChildView(child);
182 child->AddChildView(grandchild);
184 internal::RootView* root_view =
185 static_cast<internal::RootView*>(widget.GetRootView());
186 ui::EventTargeter* targeter = root_view->targeter();
188 // The event falls within the bounds of |child| and |content| but not
189 // |grandchild|, so |child| should be the initial target for the event.
190 ui::ScrollEvent scroll(ui::ET_SCROLL,
191 gfx::Point(60, 60),
192 ui::EventTimeForNow(),
194 0, 3,
195 0, 3,
197 ui::EventTarget* current_target = targeter->FindTargetForEvent(root_view,
198 &scroll);
199 EXPECT_EQ(child, static_cast<View*>(current_target));
201 // Verify that FindNextBestTarget() will return the parent view of the
202 // argument (and NULL if the argument has no parent view).
203 current_target = targeter->FindNextBestTarget(child, &scroll);
204 EXPECT_EQ(content, static_cast<View*>(current_target));
205 current_target = targeter->FindNextBestTarget(content, &scroll);
206 EXPECT_EQ(widget.GetRootView(), static_cast<View*>(current_target));
207 current_target = targeter->FindNextBestTarget(widget.GetRootView(),
208 &scroll);
209 EXPECT_EQ(NULL, static_cast<View*>(current_target));
211 // The event falls outside of the original specified bounds of |content|,
212 // |child|, and |grandchild|. But since |content| is the contents view,
213 // and contents views are resized to fill the entire area of the root
214 // view, the event's initial target should still be |content|.
215 scroll = ui::ScrollEvent(ui::ET_SCROLL,
216 gfx::Point(150, 150),
217 ui::EventTimeForNow(),
219 0, 3,
220 0, 3,
222 current_target = targeter->FindTargetForEvent(root_view, &scroll);
223 EXPECT_EQ(content, static_cast<View*>(current_target));
226 // Convenience to make constructing a GestureEvent simpler.
227 class GestureEventForTest : public ui::GestureEvent {
228 public:
229 GestureEventForTest(ui::EventType type, int x, int y)
230 : GestureEvent(x,
233 base::TimeDelta(),
234 ui::GestureEventDetails(type)) {}
236 GestureEventForTest(ui::GestureEventDetails details)
237 : GestureEvent(details.bounding_box().CenterPoint().x(),
238 details.bounding_box().CenterPoint().y(),
240 base::TimeDelta(),
241 details) {}
244 // Verifies that the the functions ViewTargeter::FindTargetForEvent()
245 // and ViewTargeter::FindNextBestTarget() are implemented correctly
246 // for gesture events.
247 TEST_F(ViewTargeterTest, ViewTargeterForGestureEvents) {
248 Widget widget;
249 Widget::InitParams init_params = CreateParams(Widget::InitParams::TYPE_POPUP);
250 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
251 init_params.bounds = gfx::Rect(0, 0, 200, 200);
252 widget.Init(init_params);
254 // The coordinates used for SetBounds() are in the parent coordinate space.
255 View* content = new View;
256 content->SetBounds(0, 0, 100, 100);
257 View* child = new View;
258 child->SetBounds(50, 50, 20, 20);
259 View* grandchild = new View;
260 grandchild->SetBounds(0, 0, 5, 5);
262 widget.SetContentsView(content);
263 content->AddChildView(child);
264 child->AddChildView(grandchild);
266 internal::RootView* root_view =
267 static_cast<internal::RootView*>(widget.GetRootView());
268 ui::EventTargeter* targeter = root_view->targeter();
270 // Define some gesture events for testing.
271 gfx::Rect bounding_box(gfx::Point(46, 46), gfx::Size(8, 8));
272 gfx::Point center_point(bounding_box.CenterPoint());
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
288 // |grandchild|).
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
343 // SCROLL_BEGIN.
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) {
359 Widget widget;
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::Rect bounding_box(gfx::Point(96, 96), gfx::Size(8, 8));
377 gfx::Point center_point(bounding_box.CenterPoint());
378 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
379 details.set_bounding_box(bounding_box);
380 GestureEventForTest tap(details);
382 EXPECT_EQ(content, targeter->FindTargetForEvent(root_view, &tap));
384 // A gesture event not located entirely within the contents view but
385 // having its center within the contents view should target
386 // the contents view.
387 bounding_box = gfx::Rect(gfx::Point(194, 100), gfx::Size(8, 8));
388 details.set_bounding_box(bounding_box);
389 center_point = bounding_box.CenterPoint();
390 tap = GestureEventForTest(details);
392 EXPECT_EQ(content, targeter->FindTargetForEvent(root_view, &tap));
394 // A gesture event with its center not located within the contents
395 // view but that overlaps the contents view by at least 60% should
396 // target the contents view.
397 bounding_box = gfx::Rect(gfx::Point(50, 0), gfx::Size(400, 200));
398 details.set_bounding_box(bounding_box);
399 center_point = bounding_box.CenterPoint();
400 tap = GestureEventForTest(details);
402 EXPECT_EQ(content, targeter->FindTargetForEvent(root_view, &tap));
404 // A gesture event not overlapping the contents view by at least
405 // 60% and not having its center within the contents view should
406 // be targeted to the root view.
407 bounding_box = gfx::Rect(gfx::Point(196, 100), gfx::Size(8, 8));
408 details.set_bounding_box(bounding_box);
409 center_point = bounding_box.CenterPoint();
410 tap = GestureEventForTest(details);
412 EXPECT_EQ(widget.GetRootView(),
413 targeter->FindTargetForEvent(root_view, &tap));
415 // A gesture event completely outside the contents view should be targeted
416 // to the root view.
417 bounding_box = gfx::Rect(gfx::Point(205, 100), gfx::Size(8, 8));
418 details.set_bounding_box(bounding_box);
419 center_point = bounding_box.CenterPoint();
420 tap = GestureEventForTest(details);
422 EXPECT_EQ(widget.GetRootView(),
423 targeter->FindTargetForEvent(root_view, &tap));
425 // A gesture event with dimensions 1x1 located entirely within the
426 // contents view should target the contents view.
427 bounding_box = gfx::Rect(gfx::Point(175, 100), gfx::Size(1, 1));
428 details.set_bounding_box(bounding_box);
429 center_point = bounding_box.CenterPoint();
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::Rect(gfx::Point(205, 100), gfx::Size(1, 1));
437 details.set_bounding_box(bounding_box);
438 center_point = bounding_box.CenterPoint();
439 tap = GestureEventForTest(details);
441 EXPECT_EQ(widget.GetRootView(),
442 targeter->FindTargetForEvent(root_view, &tap));
445 // Tests that calls to FindTargetForEvent() and FindNextBestTarget() change
446 // the location of a gesture event to be in the correct coordinate space.
447 TEST_F(ViewTargeterTest, GestureEventCoordinateConversion) {
448 Widget widget;
449 Widget::InitParams init_params = CreateParams(Widget::InitParams::TYPE_POPUP);
450 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
451 init_params.bounds = gfx::Rect(0, 0, 200, 200);
452 widget.Init(init_params);
454 // The coordinates used for SetBounds() are in the parent coordinate space.
455 View* content = new View;
456 content->SetBounds(0, 0, 100, 100);
457 View* child = new View;
458 child->SetBounds(50, 50, 20, 20);
459 View* grandchild = new View;
460 grandchild->SetBounds(5, 5, 10, 10);
461 View* great_grandchild = new View;
462 great_grandchild->SetBounds(3, 3, 4, 4);
464 widget.SetContentsView(content);
465 content->AddChildView(child);
466 child->AddChildView(grandchild);
467 grandchild->AddChildView(great_grandchild);
469 internal::RootView* root_view =
470 static_cast<internal::RootView*>(widget.GetRootView());
471 ui::EventTargeter* targeter = root_view->targeter();
473 // Define a GESTURE_TAP event with a bounding box centered at (60, 60)
474 // in root view coordinates with width and height of 4.
475 gfx::Rect bounding_box(gfx::Point(58, 58), gfx::Size(4, 4));
476 gfx::Point center_point(bounding_box.CenterPoint());
477 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
478 details.set_bounding_box(bounding_box);
479 GestureEventForTest tap(details);
481 // Calculate the location of the gesture in each of the different
482 // coordinate spaces.
483 gfx::Point location_in_root(center_point);
484 EXPECT_EQ(gfx::Point(60, 60), location_in_root);
485 gfx::Point location_in_great_grandchild(
486 ConvertPointFromWidgetToView(great_grandchild, location_in_root));
487 EXPECT_EQ(gfx::Point(2, 2), location_in_great_grandchild);
488 gfx::Point location_in_grandchild(
489 ConvertPointFromWidgetToView(grandchild, location_in_root));
490 EXPECT_EQ(gfx::Point(5, 5), location_in_grandchild);
491 gfx::Point location_in_child(
492 ConvertPointFromWidgetToView(child, location_in_root));
493 EXPECT_EQ(gfx::Point(10, 10), location_in_child);
494 gfx::Point location_in_content(
495 ConvertPointFromWidgetToView(content, location_in_root));
496 EXPECT_EQ(gfx::Point(60, 60), location_in_content);
498 // Verify the location of |tap| is in screen coordinates.
499 EXPECT_EQ(gfx::Point(60, 60), tap.location());
501 // The initial target should be |great_grandchild| and the location of
502 // the event should be changed into the coordinate space of the target.
503 EXPECT_EQ(great_grandchild, targeter->FindTargetForEvent(root_view, &tap));
504 EXPECT_EQ(location_in_great_grandchild, tap.location());
505 SetGestureHandler(root_view, great_grandchild);
507 // The next target should be |grandchild| and the location of
508 // the event should be changed into the coordinate space of the target.
509 EXPECT_EQ(grandchild, targeter->FindNextBestTarget(great_grandchild, &tap));
510 EXPECT_EQ(location_in_grandchild, tap.location());
511 SetGestureHandler(root_view, grandchild);
513 // The next target should be |child| and the location of
514 // the event should be changed into the coordinate space of the target.
515 EXPECT_EQ(child, targeter->FindNextBestTarget(grandchild, &tap));
516 EXPECT_EQ(location_in_child, tap.location());
517 SetGestureHandler(root_view, child);
519 // The next target should be |content| and the location of
520 // the event should be changed into the coordinate space of the target.
521 EXPECT_EQ(content, targeter->FindNextBestTarget(child, &tap));
522 EXPECT_EQ(location_in_content, tap.location());
523 SetGestureHandler(root_view, content);
525 // The next target should be |root_view| and the location of
526 // the event should be changed into the coordinate space of the target.
527 EXPECT_EQ(widget.GetRootView(), targeter->FindNextBestTarget(content, &tap));
528 EXPECT_EQ(location_in_root, tap.location());
529 SetGestureHandler(root_view, widget.GetRootView());
531 // The next target should be NULL and the location of the event should
532 // remain unchanged.
533 EXPECT_EQ(NULL, targeter->FindNextBestTarget(widget.GetRootView(), &tap));
534 EXPECT_EQ(location_in_root, tap.location());
537 // Tests that the functions ViewTargeterDelegate::DoesIntersectRect()
538 // and MaskedTargeterDelegate::DoesIntersectRect() work as intended when
539 // called on views which are derived from ViewTargeterDelegate.
540 // Also verifies that ViewTargeterDelegate::DoesIntersectRect() can
541 // be called from the ViewTargeter installed on RootView.
542 TEST_F(ViewTargeterTest, DoesIntersectRect) {
543 Widget widget;
544 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
545 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
546 params.bounds = gfx::Rect(0, 0, 650, 650);
547 widget.Init(params);
549 internal::RootView* root_view =
550 static_cast<internal::RootView*>(widget.GetRootView());
551 ViewTargeter* view_targeter = root_view->targeter();
553 // The coordinates used for SetBounds() are in the parent coordinate space.
554 TestingView v2;
555 TestMaskedView v1, v3;
556 v1.SetBounds(0, 0, 200, 200);
557 v2.SetBounds(300, 0, 300, 300);
558 v3.SetBounds(0, 0, 100, 100);
559 root_view->AddChildView(&v1);
560 root_view->AddChildView(&v2);
561 v2.AddChildView(&v3);
563 // The coordinates used below are in the local coordinate space of the
564 // view that is passed in as an argument.
566 // Hit tests against |v1|, which has a hit test mask.
567 EXPECT_TRUE(v1.TestDoesIntersectRect(&v1, gfx::Rect(0, 0, 200, 200)));
568 EXPECT_TRUE(v1.TestDoesIntersectRect(&v1, gfx::Rect(-10, -10, 110, 12)));
569 EXPECT_TRUE(v1.TestDoesIntersectRect(&v1, gfx::Rect(112, 142, 1, 1)));
570 EXPECT_FALSE(v1.TestDoesIntersectRect(&v1, gfx::Rect(0, 0, 20, 20)));
571 EXPECT_FALSE(v1.TestDoesIntersectRect(&v1, gfx::Rect(-10, -10, 90, 12)));
572 EXPECT_FALSE(v1.TestDoesIntersectRect(&v1, gfx::Rect(150, 49, 1, 1)));
574 // Hit tests against |v2|, which does not have a hit test mask.
575 EXPECT_TRUE(v2.TestDoesIntersectRect(&v2, gfx::Rect(0, 0, 200, 200)));
576 EXPECT_TRUE(v2.TestDoesIntersectRect(&v2, gfx::Rect(-10, 250, 60, 60)));
577 EXPECT_TRUE(v2.TestDoesIntersectRect(&v2, gfx::Rect(250, 250, 1, 1)));
578 EXPECT_FALSE(v2.TestDoesIntersectRect(&v2, gfx::Rect(-10, 250, 7, 7)));
579 EXPECT_FALSE(v2.TestDoesIntersectRect(&v2, gfx::Rect(-1, -1, 1, 1)));
581 // Hit tests against |v3|, which has a hit test mask and is a child of |v2|.
582 EXPECT_TRUE(v3.TestDoesIntersectRect(&v3, gfx::Rect(0, 0, 50, 50)));
583 EXPECT_TRUE(v3.TestDoesIntersectRect(&v3, gfx::Rect(90, 90, 1, 1)));
584 EXPECT_FALSE(v3.TestDoesIntersectRect(&v3, gfx::Rect(10, 125, 50, 50)));
585 EXPECT_FALSE(v3.TestDoesIntersectRect(&v3, gfx::Rect(110, 110, 1, 1)));
587 // Verify that hit-testing is performed correctly when using the
588 // call-through function ViewTargeter::DoesIntersectRect().
589 EXPECT_TRUE(view_targeter->DoesIntersectRect(root_view,
590 gfx::Rect(0, 0, 50, 50)));
591 EXPECT_FALSE(view_targeter->DoesIntersectRect(root_view,
592 gfx::Rect(-20, -20, 10, 10)));
595 // Tests that calls made directly on the hit-testing methods in View
596 // (HitTestPoint(), HitTestRect(), etc.) return the correct values.
597 TEST_F(ViewTargeterTest, HitTestCallsOnView) {
598 // The coordinates in this test are in the coordinate space of the root view.
599 Widget* widget = new Widget;
600 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
601 widget->Init(params);
602 View* root_view = widget->GetRootView();
603 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
605 // |v1| has no hit test mask. No ViewTargeter is installed on |v1|, which
606 // means that View::HitTestRect() will call into the targeter installed on
607 // the root view instead when we hit test against |v1|.
608 gfx::Rect v1_bounds = gfx::Rect(0, 0, 100, 100);
609 TestingView* v1 = new TestingView();
610 v1->SetBoundsRect(v1_bounds);
611 root_view->AddChildView(v1);
613 // |v2| has a triangular hit test mask. Install a ViewTargeter on |v2| which
614 // will be called into by View::HitTestRect().
615 gfx::Rect v2_bounds = gfx::Rect(105, 0, 100, 100);
616 TestMaskedView* v2 = new TestMaskedView();
617 v2->SetBoundsRect(v2_bounds);
618 root_view->AddChildView(v2);
619 ViewTargeter* view_targeter = new ViewTargeter(v2);
620 v2->SetEventTargeter(make_scoped_ptr(view_targeter));
622 gfx::Point v1_centerpoint = v1_bounds.CenterPoint();
623 gfx::Point v2_centerpoint = v2_bounds.CenterPoint();
624 gfx::Point v1_origin = v1_bounds.origin();
625 gfx::Point v2_origin = v2_bounds.origin();
626 gfx::Rect r1(10, 10, 110, 15);
627 gfx::Rect r2(106, 1, 98, 98);
628 gfx::Rect r3(0, 0, 300, 300);
629 gfx::Rect r4(115, 342, 200, 10);
631 // Test calls into View::HitTestPoint().
632 EXPECT_TRUE(
633 v1->HitTestPoint(ConvertPointFromWidgetToView(v1, v1_centerpoint)));
634 EXPECT_TRUE(
635 v2->HitTestPoint(ConvertPointFromWidgetToView(v2, v2_centerpoint)));
637 EXPECT_TRUE(v1->HitTestPoint(ConvertPointFromWidgetToView(v1, v1_origin)));
638 EXPECT_FALSE(v2->HitTestPoint(ConvertPointFromWidgetToView(v2, v2_origin)));
640 // Test calls into View::HitTestRect().
641 EXPECT_TRUE(v1->HitTestRect(ConvertRectFromWidgetToView(v1, r1)));
642 EXPECT_FALSE(v2->HitTestRect(ConvertRectFromWidgetToView(v2, r1)));
644 EXPECT_FALSE(v1->HitTestRect(ConvertRectFromWidgetToView(v1, r2)));
645 EXPECT_TRUE(v2->HitTestRect(ConvertRectFromWidgetToView(v2, r2)));
647 EXPECT_TRUE(v1->HitTestRect(ConvertRectFromWidgetToView(v1, r3)));
648 EXPECT_TRUE(v2->HitTestRect(ConvertRectFromWidgetToView(v2, r3)));
650 EXPECT_FALSE(v1->HitTestRect(ConvertRectFromWidgetToView(v1, r4)));
651 EXPECT_FALSE(v2->HitTestRect(ConvertRectFromWidgetToView(v2, r4)));
653 // Test calls into View::GetEventHandlerForPoint().
654 EXPECT_EQ(v1, root_view->GetEventHandlerForPoint(v1_centerpoint));
655 EXPECT_EQ(v2, root_view->GetEventHandlerForPoint(v2_centerpoint));
657 EXPECT_EQ(v1, root_view->GetEventHandlerForPoint(v1_origin));
658 EXPECT_EQ(root_view, root_view->GetEventHandlerForPoint(v2_origin));
660 // Test calls into View::GetTooltipHandlerForPoint().
661 EXPECT_EQ(v1, root_view->GetTooltipHandlerForPoint(v1_centerpoint));
662 EXPECT_EQ(v2, root_view->GetTooltipHandlerForPoint(v2_centerpoint));
664 EXPECT_EQ(v1, root_view->GetTooltipHandlerForPoint(v1_origin));
665 EXPECT_EQ(root_view, root_view->GetTooltipHandlerForPoint(v2_origin));
667 EXPECT_FALSE(v1->GetTooltipHandlerForPoint(v2_origin));
669 widget->CloseNow();
672 } // namespace test
673 } // namespace views