Add ICU message format support
[chromium-blink-merge.git] / ui / views / view_targeter_unittest.cc
blob71601e30b4b31d88e9e0eaff47f6a4d594085919
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"
17 namespace views {
19 // A derived class of View used for testing purposes.
20 class TestingView : public View, public ViewTargeterDelegate {
21 public:
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);
37 // View:
38 bool CanProcessEventsWithinSubtree() const override {
39 return can_process_events_within_subtree_;
42 private:
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 {
51 public:
52 TestMaskedView() {}
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);
60 private:
61 // MaskedTargeterDelegate:
62 bool GetHitTestMask(gfx::Path* mask) const override {
63 DCHECK(mask);
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);
69 mask->lineTo(w, h);
70 mask->lineTo(0, h);
71 mask->close();
72 return true;
75 DISALLOW_COPY_AND_ASSIGN(TestMaskedView);
78 namespace test {
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 {
84 public:
85 ViewTargeterTest() {}
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,
93 bool set) {
94 root_view->gesture_handler_set_before_processing_ = set;
97 private:
98 DISALLOW_COPY_AND_ASSIGN(ViewTargeterTest);
101 namespace {
103 gfx::Point ConvertPointFromWidgetToView(View* view, const gfx::Point& p) {
104 gfx::Point tmp(p);
105 View::ConvertPointToTarget(view->GetWidget()->GetRootView(), view, &tmp);
106 return tmp;
109 gfx::Rect ConvertRectFromWidgetToView(View* view, const gfx::Rect& r) {
110 gfx::Rect tmp(r);
111 tmp.set_origin(ConvertPointFromWidgetToView(view, r.origin()));
112 return tmp;
115 } // namespace
117 // Verifies that the the functions ViewTargeter::FindTargetForEvent()
118 // and ViewTargeter::FindNextBestTarget() are implemented correctly
119 // for key events.
120 TEST_F(ViewTargeterTest, ViewTargeterForKeyEvents) {
121 Widget widget;
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,
146 &key_event);
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(),
158 &key_event);
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) {
166 Widget widget;
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,
192 gfx::Point(60, 60),
193 ui::EventTimeForNow(),
195 0, 3,
196 0, 3,
198 ui::EventTarget* current_target = targeter->FindTargetForEvent(root_view,
199 &scroll);
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(),
209 &scroll);
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(),
220 0, 3,
221 0, 3,
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 {
229 public:
230 GestureEventForTest(ui::EventType type, int x, int y)
231 : GestureEvent(x,
234 base::TimeDelta(),
235 ui::GestureEventDetails(type)) {}
237 GestureEventForTest(ui::GestureEventDetails details)
238 : GestureEvent(details.bounding_box().CenterPoint().x(),
239 details.bounding_box().CenterPoint().y(),
241 base::TimeDelta(),
242 details) {}
245 // Verifies that the the functions ViewTargeter::FindTargetForEvent()
246 // and ViewTargeter::FindNextBestTarget() are implemented correctly
247 // for gesture events.
248 TEST_F(ViewTargeterTest, ViewTargeterForGestureEvents) {
249 Widget widget;
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::Rect bounding_box(gfx::Point(46, 46), gfx::Size(8, 8));
273 gfx::Point center_point(bounding_box.CenterPoint());
274 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
275 details.set_bounding_box(bounding_box);
276 GestureEventForTest tap(details);
277 details = ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_BEGIN);
278 details.set_bounding_box(bounding_box);
279 GestureEventForTest scroll_begin(details);
280 details = ui::GestureEventDetails(ui::ET_GESTURE_END);
281 details.set_bounding_box(bounding_box);
282 GestureEventForTest end(details);
284 // Assume that the view currently handling gestures has been set as
285 // |grandchild| by a previous gesture event. Thus subsequent TAP and
286 // SCROLL_BEGIN events should be initially targeted to |grandchild|, and
287 // re-targeting should be prohibited for TAP but permitted for
288 // GESTURE_SCROLL_BEGIN (which should be re-targeted to the parent of
289 // |grandchild|).
290 SetGestureHandlerSetBeforeProcessing(root_view, true);
291 SetGestureHandler(root_view, grandchild);
292 EXPECT_EQ(grandchild, targeter->FindTargetForEvent(root_view, &tap));
293 EXPECT_EQ(NULL, targeter->FindNextBestTarget(grandchild, &tap));
294 EXPECT_EQ(grandchild, targeter->FindTargetForEvent(root_view, &scroll_begin));
295 EXPECT_EQ(child, targeter->FindNextBestTarget(grandchild, &scroll_begin));
297 // GESTURE_END events should be targeted to the existing gesture handler,
298 // but re-targeting should be prohibited.
299 EXPECT_EQ(grandchild, targeter->FindTargetForEvent(root_view, &end));
300 EXPECT_EQ(NULL, targeter->FindNextBestTarget(grandchild, &end));
302 // Assume that the view currently handling gestures is still set as
303 // |grandchild|, but this was not done by a previous gesture. Thus we are
304 // in the process of finding the View to which subsequent gestures will be
305 // dispatched, so TAP and SCROLL_BEGIN events should be re-targeted up
306 // the ancestor chain.
307 SetGestureHandlerSetBeforeProcessing(root_view, false);
308 EXPECT_EQ(child, targeter->FindNextBestTarget(grandchild, &tap));
309 EXPECT_EQ(child, targeter->FindNextBestTarget(grandchild, &scroll_begin));
311 // GESTURE_END events are not permitted to be re-targeted up the ancestor
312 // chain; they are only ever targeted in the case where the gesture handler
313 // was established by a previous gesture.
314 EXPECT_EQ(NULL, targeter->FindNextBestTarget(grandchild, &end));
316 // Assume that the default gesture handler was set by the previous gesture,
317 // but that this handler is currently NULL. No gesture events should be
318 // re-targeted in this case (regardless of the view that is passed in to
319 // FindNextBestTarget() as the previous target).
320 SetGestureHandler(root_view, NULL);
321 SetGestureHandlerSetBeforeProcessing(root_view, true);
322 EXPECT_EQ(NULL, targeter->FindNextBestTarget(child, &tap));
323 EXPECT_EQ(NULL, targeter->FindNextBestTarget(NULL, &tap));
324 EXPECT_EQ(NULL, targeter->FindNextBestTarget(content, &scroll_begin));
325 EXPECT_EQ(NULL, targeter->FindNextBestTarget(content, &end));
327 // Reset the locations of the gesture events to be in the root view
328 // coordinate space since we are about to call FindTargetForEvent()
329 // again (calls to FindTargetForEvent() and FindNextBestTarget()
330 // mutate the location of the gesture events to be in the coordinate
331 // space of the returned view).
332 details = ui::GestureEventDetails(ui::ET_GESTURE_TAP);
333 details.set_bounding_box(bounding_box);
334 tap = GestureEventForTest(details);
335 details = ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_BEGIN);
336 details.set_bounding_box(bounding_box);
337 scroll_begin = GestureEventForTest(details);
338 details = ui::GestureEventDetails(ui::ET_GESTURE_END);
339 details.set_bounding_box(bounding_box);
340 end = GestureEventForTest(details);
342 // If no default gesture handler is currently set, targeting should be
343 // performed using the location of the gesture event for a TAP and a
344 // SCROLL_BEGIN.
345 SetGestureHandlerSetBeforeProcessing(root_view, false);
346 EXPECT_EQ(grandchild, targeter->FindTargetForEvent(root_view, &tap));
347 EXPECT_EQ(grandchild, targeter->FindTargetForEvent(root_view, &scroll_begin));
349 // If no default gesture handler is currently set, GESTURE_END events
350 // should never be re-targeted to any View.
351 EXPECT_EQ(NULL, targeter->FindNextBestTarget(NULL, &end));
352 EXPECT_EQ(NULL, targeter->FindNextBestTarget(child, &end));
355 // Tests that the contents view is targeted instead of the root view for
356 // gesture events that should be targeted to the contents view. Also
357 // tests that the root view is targeted for gesture events which should
358 // not be targeted to any other view in the views tree.
359 TEST_F(ViewTargeterTest, TargetContentsAndRootView) {
360 Widget widget;
361 Widget::InitParams init_params = CreateParams(Widget::InitParams::TYPE_POPUP);
362 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
363 init_params.bounds = gfx::Rect(0, 0, 200, 200);
364 widget.Init(init_params);
366 // The coordinates used for SetBounds() are in the parent coordinate space.
367 View* content = new View;
368 content->SetBounds(0, 0, 100, 100);
369 widget.SetContentsView(content);
371 internal::RootView* root_view =
372 static_cast<internal::RootView*>(widget.GetRootView());
373 ui::EventTargeter* targeter = root_view->targeter();
375 // A gesture event located entirely within the contents view should
376 // target the contents view.
377 gfx::Rect bounding_box(gfx::Point(96, 96), gfx::Size(8, 8));
378 gfx::Point center_point(bounding_box.CenterPoint());
379 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
380 details.set_bounding_box(bounding_box);
381 GestureEventForTest tap(details);
383 EXPECT_EQ(content, targeter->FindTargetForEvent(root_view, &tap));
385 // A gesture event not located entirely within the contents view but
386 // having its center within the contents view should target
387 // the contents view.
388 bounding_box = gfx::Rect(gfx::Point(194, 100), gfx::Size(8, 8));
389 details.set_bounding_box(bounding_box);
390 center_point = bounding_box.CenterPoint();
391 tap = GestureEventForTest(details);
393 EXPECT_EQ(content, targeter->FindTargetForEvent(root_view, &tap));
395 // A gesture event with its center not located within the contents
396 // view but that overlaps the contents view by at least 60% should
397 // target the contents view.
398 bounding_box = gfx::Rect(gfx::Point(50, 0), gfx::Size(400, 200));
399 details.set_bounding_box(bounding_box);
400 center_point = bounding_box.CenterPoint();
401 tap = GestureEventForTest(details);
403 // This only applies if rect-based targeting is enabled.
404 if (views::switches::IsRectBasedTargetingEnabled()) {
405 EXPECT_EQ(content, targeter->FindTargetForEvent(root_view, &tap));
406 } else {
407 EXPECT_EQ(widget.GetRootView(),
408 targeter->FindTargetForEvent(root_view, &tap));
411 // A gesture event not overlapping the contents view by at least
412 // 60% and not having its center within the contents view should
413 // be targeted to the root view.
414 bounding_box = gfx::Rect(gfx::Point(196, 100), gfx::Size(8, 8));
415 details.set_bounding_box(bounding_box);
416 center_point = bounding_box.CenterPoint();
417 tap = GestureEventForTest(details);
419 EXPECT_EQ(widget.GetRootView(),
420 targeter->FindTargetForEvent(root_view, &tap));
422 // A gesture event completely outside the contents view should be targeted
423 // to the root view.
424 bounding_box = gfx::Rect(gfx::Point(205, 100), gfx::Size(8, 8));
425 details.set_bounding_box(bounding_box);
426 center_point = bounding_box.CenterPoint();
427 tap = GestureEventForTest(details);
429 EXPECT_EQ(widget.GetRootView(),
430 targeter->FindTargetForEvent(root_view, &tap));
432 // A gesture event with dimensions 1x1 located entirely within the
433 // contents view should target the contents view.
434 bounding_box = gfx::Rect(gfx::Point(175, 100), gfx::Size(1, 1));
435 details.set_bounding_box(bounding_box);
436 center_point = bounding_box.CenterPoint();
437 tap = GestureEventForTest(details);
439 EXPECT_EQ(content, targeter->FindTargetForEvent(root_view, &tap));
441 // A gesture event with dimensions 1x1 located entirely outside the
442 // contents view should be targeted to the root view.
443 bounding_box = gfx::Rect(gfx::Point(205, 100), gfx::Size(1, 1));
444 details.set_bounding_box(bounding_box);
445 center_point = bounding_box.CenterPoint();
446 tap = GestureEventForTest(details);
448 EXPECT_EQ(widget.GetRootView(),
449 targeter->FindTargetForEvent(root_view, &tap));
452 // Tests that calls to FindTargetForEvent() and FindNextBestTarget() change
453 // the location of a gesture event to be in the correct coordinate space.
454 TEST_F(ViewTargeterTest, GestureEventCoordinateConversion) {
455 Widget widget;
456 Widget::InitParams init_params = CreateParams(Widget::InitParams::TYPE_POPUP);
457 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
458 init_params.bounds = gfx::Rect(0, 0, 200, 200);
459 widget.Init(init_params);
461 // The coordinates used for SetBounds() are in the parent coordinate space.
462 View* content = new View;
463 content->SetBounds(0, 0, 100, 100);
464 View* child = new View;
465 child->SetBounds(50, 50, 20, 20);
466 View* grandchild = new View;
467 grandchild->SetBounds(5, 5, 10, 10);
468 View* great_grandchild = new View;
469 great_grandchild->SetBounds(3, 3, 4, 4);
471 widget.SetContentsView(content);
472 content->AddChildView(child);
473 child->AddChildView(grandchild);
474 grandchild->AddChildView(great_grandchild);
476 internal::RootView* root_view =
477 static_cast<internal::RootView*>(widget.GetRootView());
478 ui::EventTargeter* targeter = root_view->targeter();
480 // Define a GESTURE_TAP event with a bounding box centered at (60, 60)
481 // in root view coordinates with width and height of 4.
482 gfx::Rect bounding_box(gfx::Point(58, 58), gfx::Size(4, 4));
483 gfx::Point center_point(bounding_box.CenterPoint());
484 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
485 details.set_bounding_box(bounding_box);
486 GestureEventForTest tap(details);
488 // Calculate the location of the gesture in each of the different
489 // coordinate spaces.
490 gfx::Point location_in_root(center_point);
491 EXPECT_EQ(gfx::Point(60, 60), location_in_root);
492 gfx::Point location_in_great_grandchild(
493 ConvertPointFromWidgetToView(great_grandchild, location_in_root));
494 EXPECT_EQ(gfx::Point(2, 2), location_in_great_grandchild);
495 gfx::Point location_in_grandchild(
496 ConvertPointFromWidgetToView(grandchild, location_in_root));
497 EXPECT_EQ(gfx::Point(5, 5), location_in_grandchild);
498 gfx::Point location_in_child(
499 ConvertPointFromWidgetToView(child, location_in_root));
500 EXPECT_EQ(gfx::Point(10, 10), location_in_child);
501 gfx::Point location_in_content(
502 ConvertPointFromWidgetToView(content, location_in_root));
503 EXPECT_EQ(gfx::Point(60, 60), location_in_content);
505 // Verify the location of |tap| is in screen coordinates.
506 EXPECT_EQ(gfx::Point(60, 60), tap.location());
508 // The initial target should be |great_grandchild| and the location of
509 // the event should be changed into the coordinate space of the target.
510 EXPECT_EQ(great_grandchild, targeter->FindTargetForEvent(root_view, &tap));
511 EXPECT_EQ(location_in_great_grandchild, tap.location());
512 SetGestureHandler(root_view, great_grandchild);
514 // The next target should be |grandchild| and the location of
515 // the event should be changed into the coordinate space of the target.
516 EXPECT_EQ(grandchild, targeter->FindNextBestTarget(great_grandchild, &tap));
517 EXPECT_EQ(location_in_grandchild, tap.location());
518 SetGestureHandler(root_view, grandchild);
520 // The next target should be |child| and the location of
521 // the event should be changed into the coordinate space of the target.
522 EXPECT_EQ(child, targeter->FindNextBestTarget(grandchild, &tap));
523 EXPECT_EQ(location_in_child, tap.location());
524 SetGestureHandler(root_view, child);
526 // The next target should be |content| and the location of
527 // the event should be changed into the coordinate space of the target.
528 EXPECT_EQ(content, targeter->FindNextBestTarget(child, &tap));
529 EXPECT_EQ(location_in_content, tap.location());
530 SetGestureHandler(root_view, content);
532 // The next target should be |root_view| and the location of
533 // the event should be changed into the coordinate space of the target.
534 EXPECT_EQ(widget.GetRootView(), targeter->FindNextBestTarget(content, &tap));
535 EXPECT_EQ(location_in_root, tap.location());
536 SetGestureHandler(root_view, widget.GetRootView());
538 // The next target should be NULL and the location of the event should
539 // remain unchanged.
540 EXPECT_EQ(NULL, targeter->FindNextBestTarget(widget.GetRootView(), &tap));
541 EXPECT_EQ(location_in_root, tap.location());
544 // Tests that the functions ViewTargeterDelegate::DoesIntersectRect()
545 // and MaskedTargeterDelegate::DoesIntersectRect() work as intended when
546 // called on views which are derived from ViewTargeterDelegate.
547 // Also verifies that ViewTargeterDelegate::DoesIntersectRect() can
548 // be called from the ViewTargeter installed on RootView.
549 TEST_F(ViewTargeterTest, DoesIntersectRect) {
550 Widget widget;
551 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
552 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
553 params.bounds = gfx::Rect(0, 0, 650, 650);
554 widget.Init(params);
556 internal::RootView* root_view =
557 static_cast<internal::RootView*>(widget.GetRootView());
558 ViewTargeter* view_targeter = root_view->targeter();
560 // The coordinates used for SetBounds() are in the parent coordinate space.
561 TestingView v2;
562 TestMaskedView v1, v3;
563 v1.SetBounds(0, 0, 200, 200);
564 v2.SetBounds(300, 0, 300, 300);
565 v3.SetBounds(0, 0, 100, 100);
566 root_view->AddChildView(&v1);
567 root_view->AddChildView(&v2);
568 v2.AddChildView(&v3);
570 // The coordinates used below are in the local coordinate space of the
571 // view that is passed in as an argument.
573 // Hit tests against |v1|, which has a hit test mask.
574 EXPECT_TRUE(v1.TestDoesIntersectRect(&v1, gfx::Rect(0, 0, 200, 200)));
575 EXPECT_TRUE(v1.TestDoesIntersectRect(&v1, gfx::Rect(-10, -10, 110, 12)));
576 EXPECT_TRUE(v1.TestDoesIntersectRect(&v1, gfx::Rect(112, 142, 1, 1)));
577 EXPECT_FALSE(v1.TestDoesIntersectRect(&v1, gfx::Rect(0, 0, 20, 20)));
578 EXPECT_FALSE(v1.TestDoesIntersectRect(&v1, gfx::Rect(-10, -10, 90, 12)));
579 EXPECT_FALSE(v1.TestDoesIntersectRect(&v1, gfx::Rect(150, 49, 1, 1)));
581 // Hit tests against |v2|, which does not have a hit test mask.
582 EXPECT_TRUE(v2.TestDoesIntersectRect(&v2, gfx::Rect(0, 0, 200, 200)));
583 EXPECT_TRUE(v2.TestDoesIntersectRect(&v2, gfx::Rect(-10, 250, 60, 60)));
584 EXPECT_TRUE(v2.TestDoesIntersectRect(&v2, gfx::Rect(250, 250, 1, 1)));
585 EXPECT_FALSE(v2.TestDoesIntersectRect(&v2, gfx::Rect(-10, 250, 7, 7)));
586 EXPECT_FALSE(v2.TestDoesIntersectRect(&v2, gfx::Rect(-1, -1, 1, 1)));
588 // Hit tests against |v3|, which has a hit test mask and is a child of |v2|.
589 EXPECT_TRUE(v3.TestDoesIntersectRect(&v3, gfx::Rect(0, 0, 50, 50)));
590 EXPECT_TRUE(v3.TestDoesIntersectRect(&v3, gfx::Rect(90, 90, 1, 1)));
591 EXPECT_FALSE(v3.TestDoesIntersectRect(&v3, gfx::Rect(10, 125, 50, 50)));
592 EXPECT_FALSE(v3.TestDoesIntersectRect(&v3, gfx::Rect(110, 110, 1, 1)));
594 // Verify that hit-testing is performed correctly when using the
595 // call-through function ViewTargeter::DoesIntersectRect().
596 EXPECT_TRUE(view_targeter->DoesIntersectRect(root_view,
597 gfx::Rect(0, 0, 50, 50)));
598 EXPECT_FALSE(view_targeter->DoesIntersectRect(root_view,
599 gfx::Rect(-20, -20, 10, 10)));
602 // Tests that calls made directly on the hit-testing methods in View
603 // (HitTestPoint(), HitTestRect(), etc.) return the correct values.
604 TEST_F(ViewTargeterTest, HitTestCallsOnView) {
605 // The coordinates in this test are in the coordinate space of the root view.
606 Widget* widget = new Widget;
607 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
608 widget->Init(params);
609 View* root_view = widget->GetRootView();
610 root_view->SetBoundsRect(gfx::Rect(0, 0, 500, 500));
612 // |v1| has no hit test mask. No ViewTargeter is installed on |v1|, which
613 // means that View::HitTestRect() will call into the targeter installed on
614 // the root view instead when we hit test against |v1|.
615 gfx::Rect v1_bounds = gfx::Rect(0, 0, 100, 100);
616 TestingView* v1 = new TestingView();
617 v1->SetBoundsRect(v1_bounds);
618 root_view->AddChildView(v1);
620 // |v2| has a triangular hit test mask. Install a ViewTargeter on |v2| which
621 // will be called into by View::HitTestRect().
622 gfx::Rect v2_bounds = gfx::Rect(105, 0, 100, 100);
623 TestMaskedView* v2 = new TestMaskedView();
624 v2->SetBoundsRect(v2_bounds);
625 root_view->AddChildView(v2);
626 ViewTargeter* view_targeter = new ViewTargeter(v2);
627 v2->SetEventTargeter(make_scoped_ptr(view_targeter));
629 gfx::Point v1_centerpoint = v1_bounds.CenterPoint();
630 gfx::Point v2_centerpoint = v2_bounds.CenterPoint();
631 gfx::Point v1_origin = v1_bounds.origin();
632 gfx::Point v2_origin = v2_bounds.origin();
633 gfx::Rect r1(10, 10, 110, 15);
634 gfx::Rect r2(106, 1, 98, 98);
635 gfx::Rect r3(0, 0, 300, 300);
636 gfx::Rect r4(115, 342, 200, 10);
638 // Test calls into View::HitTestPoint().
639 EXPECT_TRUE(
640 v1->HitTestPoint(ConvertPointFromWidgetToView(v1, v1_centerpoint)));
641 EXPECT_TRUE(
642 v2->HitTestPoint(ConvertPointFromWidgetToView(v2, v2_centerpoint)));
644 EXPECT_TRUE(v1->HitTestPoint(ConvertPointFromWidgetToView(v1, v1_origin)));
645 EXPECT_FALSE(v2->HitTestPoint(ConvertPointFromWidgetToView(v2, v2_origin)));
647 // Test calls into View::HitTestRect().
648 EXPECT_TRUE(v1->HitTestRect(ConvertRectFromWidgetToView(v1, r1)));
649 EXPECT_FALSE(v2->HitTestRect(ConvertRectFromWidgetToView(v2, r1)));
651 EXPECT_FALSE(v1->HitTestRect(ConvertRectFromWidgetToView(v1, r2)));
652 EXPECT_TRUE(v2->HitTestRect(ConvertRectFromWidgetToView(v2, r2)));
654 EXPECT_TRUE(v1->HitTestRect(ConvertRectFromWidgetToView(v1, r3)));
655 EXPECT_TRUE(v2->HitTestRect(ConvertRectFromWidgetToView(v2, r3)));
657 EXPECT_FALSE(v1->HitTestRect(ConvertRectFromWidgetToView(v1, r4)));
658 EXPECT_FALSE(v2->HitTestRect(ConvertRectFromWidgetToView(v2, r4)));
660 // Test calls into View::GetEventHandlerForPoint().
661 EXPECT_EQ(v1, root_view->GetEventHandlerForPoint(v1_centerpoint));
662 EXPECT_EQ(v2, root_view->GetEventHandlerForPoint(v2_centerpoint));
664 EXPECT_EQ(v1, root_view->GetEventHandlerForPoint(v1_origin));
665 EXPECT_EQ(root_view, root_view->GetEventHandlerForPoint(v2_origin));
667 // Test calls into View::GetTooltipHandlerForPoint().
668 EXPECT_EQ(v1, root_view->GetTooltipHandlerForPoint(v1_centerpoint));
669 EXPECT_EQ(v2, root_view->GetTooltipHandlerForPoint(v2_centerpoint));
671 EXPECT_EQ(v1, root_view->GetTooltipHandlerForPoint(v1_origin));
672 EXPECT_EQ(root_view, root_view->GetTooltipHandlerForPoint(v2_origin));
674 EXPECT_FALSE(v1->GetTooltipHandlerForPoint(v2_origin));
676 widget->CloseNow();
679 } // namespace test
680 } // namespace views