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/bubble/bubble_window_targeter.h"
7 #include "ui/aura/window.h"
8 #include "ui/aura/window_event_dispatcher.h"
9 #include "ui/events/event_utils.h"
10 #include "ui/views/bubble/bubble_border.h"
11 #include "ui/views/bubble/bubble_delegate.h"
12 #include "ui/views/test/views_test_base.h"
13 #include "ui/views/widget/widget.h"
19 class WidgetOwnsNativeBubble
: public BubbleDelegateView
{
21 WidgetOwnsNativeBubble(View
* content
, BubbleBorder::Arrow arrow
)
22 : BubbleDelegateView(content
, arrow
) {
25 ~WidgetOwnsNativeBubble() override
{}
28 // BubbleDelegateView:
29 void OnBeforeBubbleWidgetInit(Widget::InitParams
* params
,
30 Widget
* widget
) const override
{
31 params
->ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
34 DISALLOW_COPY_AND_ASSIGN(WidgetOwnsNativeBubble
);
39 class BubbleWindowTargeterTest
: public ViewsTestBase
{
41 BubbleWindowTargeterTest()
42 : bubble_delegate_(NULL
) {
44 ~BubbleWindowTargeterTest() override
{}
46 void SetUp() override
{
47 ViewsTestBase::SetUp();
51 anchor_widget()->Show();
52 bubble_widget()->Show();
55 void TearDown() override
{
56 bubble_delegate_
= NULL
;
57 bubble_widget_
.reset();
59 ViewsTestBase::TearDown();
62 Widget
* anchor_widget() { return anchor_
.get(); }
63 Widget
* bubble_widget() { return bubble_widget_
.get(); }
64 BubbleDelegateView
* bubble_delegate() { return bubble_delegate_
; }
67 void CreateAnchorWidget() {
68 anchor_
.reset(new Widget());
69 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_WINDOW
);
70 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
71 anchor_
->Init(params
);
74 void CreateBubbleWidget() {
75 bubble_delegate_
= new WidgetOwnsNativeBubble(
76 anchor_
->GetContentsView(), BubbleBorder::NONE
);
77 bubble_delegate_
->set_color(SK_ColorGREEN
);
78 bubble_widget_
.reset(BubbleDelegateView::CreateBubble(bubble_delegate_
));
81 scoped_ptr
<Widget
> anchor_
;
82 scoped_ptr
<Widget
> bubble_widget_
;
83 BubbleDelegateView
* bubble_delegate_
;
85 DISALLOW_COPY_AND_ASSIGN(BubbleWindowTargeterTest
);
88 TEST_F(BubbleWindowTargeterTest
, HitTest
) {
89 ui::EventTarget
* root
= bubble_widget()->GetNativeWindow()->GetRootWindow();
90 ui::EventTargeter
* targeter
= root
->GetEventTargeter();
91 aura::Window
* bubble_window
= bubble_widget()->GetNativeWindow();
92 gfx::Rect bubble_bounds
= bubble_window
->GetBoundsInRootWindow();
95 bubble_delegate()->set_margins(gfx::Insets());
96 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, bubble_bounds
.origin(),
97 bubble_bounds
.origin(), ui::EventTimeForNow(),
98 ui::EF_NONE
, ui::EF_NONE
);
99 EXPECT_EQ(bubble_window
, targeter
->FindTargetForEvent(root
, &move1
));
102 bubble_delegate()->set_margins(gfx::Insets(20, 20, 20, 20));
103 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, bubble_bounds
.origin(),
104 bubble_bounds
.origin(), ui::EventTimeForNow(),
105 ui::EF_NONE
, ui::EF_NONE
);
106 EXPECT_EQ(bubble_window
, targeter
->FindTargetForEvent(root
, &move1
));
109 bubble_window
->SetEventTargeter(scoped_ptr
<ui::EventTargeter
>(
110 new BubbleWindowTargeter(bubble_delegate())));
112 bubble_delegate()->set_margins(gfx::Insets(20, 20, 20, 20));
113 ui::MouseEvent
move1(ui::ET_MOUSE_MOVED
, bubble_bounds
.origin(),
114 bubble_bounds
.origin(), ui::EventTimeForNow(),
115 ui::EF_NONE
, ui::EF_NONE
);
116 EXPECT_NE(bubble_window
, targeter
->FindTargetForEvent(root
, &move1
));