Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / views / bubble / bubble_window_targeter_unittest.cc
blob479f6e93d3917d43111c469f30c3c41614246a31
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/views/bubble/bubble_border.h"
10 #include "ui/views/bubble/bubble_delegate.h"
11 #include "ui/views/test/views_test_base.h"
12 #include "ui/views/widget/widget.h"
14 namespace views {
16 namespace {
18 class WidgetOwnsNativeBubble : public BubbleDelegateView {
19 public:
20 WidgetOwnsNativeBubble(View* content, BubbleBorder::Arrow arrow)
21 : BubbleDelegateView(content, arrow) {
24 virtual ~WidgetOwnsNativeBubble() {}
26 private:
27 // BubbleDelegateView:
28 virtual void OnBeforeBubbleWidgetInit(Widget::InitParams* params,
29 Widget* widget) const OVERRIDE {
30 params->ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
33 DISALLOW_COPY_AND_ASSIGN(WidgetOwnsNativeBubble);
36 } // namespace
38 class BubbleWindowTargeterTest : public ViewsTestBase {
39 public:
40 BubbleWindowTargeterTest()
41 : bubble_delegate_(NULL) {
43 virtual ~BubbleWindowTargeterTest() {}
45 virtual void SetUp() OVERRIDE {
46 ViewsTestBase::SetUp();
47 CreateAnchorWidget();
48 CreateBubbleWidget();
50 anchor_widget()->Show();
51 bubble_widget()->Show();
54 virtual void TearDown() OVERRIDE {
55 bubble_delegate_ = NULL;
56 bubble_widget_.reset();
57 anchor_.reset();
58 ViewsTestBase::TearDown();
61 Widget* anchor_widget() { return anchor_.get(); }
62 Widget* bubble_widget() { return bubble_widget_.get(); }
63 BubbleDelegateView* bubble_delegate() { return bubble_delegate_; }
65 private:
66 void CreateAnchorWidget() {
67 anchor_.reset(new Widget());
68 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
69 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
70 anchor_->Init(params);
73 void CreateBubbleWidget() {
74 bubble_delegate_ = new WidgetOwnsNativeBubble(
75 anchor_->GetContentsView(), BubbleBorder::NONE);
76 bubble_delegate_->set_color(SK_ColorGREEN);
77 bubble_widget_.reset(BubbleDelegateView::CreateBubble(bubble_delegate_));
80 scoped_ptr<Widget> anchor_;
81 scoped_ptr<Widget> bubble_widget_;
82 BubbleDelegateView* bubble_delegate_;
84 DISALLOW_COPY_AND_ASSIGN(BubbleWindowTargeterTest);
87 TEST_F(BubbleWindowTargeterTest, HitTest) {
88 ui::EventTarget* root = bubble_widget()->GetNativeWindow()->GetRootWindow();
89 ui::EventTargeter* targeter = root->GetEventTargeter();
90 aura::Window* bubble_window = bubble_widget()->GetNativeWindow();
91 gfx::Rect bubble_bounds = bubble_window->GetBoundsInRootWindow();
94 bubble_delegate()->set_margins(gfx::Insets());
95 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, bubble_bounds.origin(),
96 bubble_bounds.origin(), ui::EF_NONE, ui::EF_NONE);
97 EXPECT_EQ(bubble_window, targeter->FindTargetForEvent(root, &move1));
100 bubble_delegate()->set_margins(gfx::Insets(20, 20, 20, 20));
101 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, bubble_bounds.origin(),
102 bubble_bounds.origin(), ui::EF_NONE, ui::EF_NONE);
103 EXPECT_EQ(bubble_window, targeter->FindTargetForEvent(root, &move1));
106 bubble_window->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
107 new BubbleWindowTargeter(bubble_delegate())));
109 bubble_delegate()->set_margins(gfx::Insets(20, 20, 20, 20));
110 ui::MouseEvent move1(ui::ET_MOUSE_MOVED, bubble_bounds.origin(),
111 bubble_bounds.origin(), ui::EF_NONE, ui::EF_NONE);
112 EXPECT_NE(bubble_window, targeter->FindTargetForEvent(root, &move1));
116 } // namespace views