| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/base/hit_test.h" | 5 #include "ui/base/hit_test.h" |
| 6 #include "ui/gfx/insets.h" | 6 #include "ui/gfx/insets.h" |
| 7 #include "ui/views/bubble/bubble_border.h" | 7 #include "ui/views/bubble/bubble_border.h" |
| 8 #include "ui/views/bubble/bubble_delegate.h" | 8 #include "ui/views/bubble/bubble_delegate.h" |
| 9 #include "ui/views/bubble/bubble_frame_view.h" | 9 #include "ui/views/bubble/bubble_frame_view.h" |
| 10 #include "ui/views/test/views_test_base.h" | 10 #include "ui/views/test/views_test_base.h" |
| 11 #include "ui/views/widget/widget.h" | 11 #include "ui/views/widget/widget.h" |
| 12 | 12 |
| 13 namespace views { | 13 namespace views { |
| 14 | 14 |
| 15 typedef ViewsTestBase BubbleFrameViewTest; | 15 typedef ViewsTestBase BubbleFrameViewTest; |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 const BubbleBorder::Arrow kArrow = BubbleBorder::TOP_LEFT; | 19 const BubbleBorder::Arrow kArrow = BubbleBorder::TOP_LEFT; |
| 20 const int kBubbleWidth = 200; | 20 const int kBubbleWidth = 200; |
| 21 const int kBubbleHeight = 200; | 21 const int kBubbleHeight = 200; |
| 22 const SkColor kColor = SK_ColorRED; | 22 const SkColor kColor = SK_ColorRED; |
| 23 const int kMargin = 6; | 23 const int kMargin = 6; |
| 24 | 24 |
| 25 class SizedBubbleDelegateView : public BubbleDelegateView { | 25 class TestBubbleDelegateView : public BubbleDelegateView { |
| 26 public: | 26 public: |
| 27 SizedBubbleDelegateView(View* anchor_view); | 27 explicit TestBubbleDelegateView(View* anchor_view); |
| 28 virtual ~SizedBubbleDelegateView(); | 28 virtual ~TestBubbleDelegateView(); |
| 29 | 29 |
| 30 // View overrides: | 30 // View overrides: |
| 31 virtual gfx::Size GetPreferredSize() OVERRIDE; | 31 virtual gfx::Size GetPreferredSize() OVERRIDE; |
| 32 | 32 |
| 33 // BubbleDelegateView overrides: |
| 34 virtual bool CanResize() const OVERRIDE; |
| 35 virtual DialogDelegate* AsDialogDelegate() OVERRIDE; |
| 36 |
| 37 void set_can_resize(bool can_resize) { can_resize_ = can_resize; } |
| 38 |
| 33 private: | 39 private: |
| 34 DISALLOW_COPY_AND_ASSIGN(SizedBubbleDelegateView); | 40 bool can_resize_; |
| 41 |
| 42 DISALLOW_COPY_AND_ASSIGN(TestBubbleDelegateView); |
| 35 }; | 43 }; |
| 36 | 44 |
| 37 SizedBubbleDelegateView::SizedBubbleDelegateView(View* anchor_view) | 45 TestBubbleDelegateView::TestBubbleDelegateView(View* anchor_view) |
| 38 : BubbleDelegateView(anchor_view, BubbleBorder::TOP_LEFT) { | 46 : BubbleDelegateView(anchor_view, BubbleBorder::TOP_LEFT), |
| 47 can_resize_(false) { |
| 39 } | 48 } |
| 40 | 49 |
| 41 SizedBubbleDelegateView::~SizedBubbleDelegateView() {} | 50 TestBubbleDelegateView::~TestBubbleDelegateView() {} |
| 42 | 51 |
| 43 gfx::Size SizedBubbleDelegateView::GetPreferredSize() { | 52 gfx::Size TestBubbleDelegateView::GetPreferredSize() { |
| 44 return gfx::Size(kBubbleWidth, kBubbleHeight); | 53 return gfx::Size(kBubbleWidth, kBubbleHeight); |
| 45 } | 54 } |
| 46 | 55 |
| 56 bool TestBubbleDelegateView::CanResize() const { |
| 57 return can_resize_; |
| 58 } |
| 59 |
| 60 DialogDelegate* TestBubbleDelegateView::AsDialogDelegate() { |
| 61 // This hack lets the bubble frame view be tested as its used with dialogs. |
| 62 // In particular, this is checked while hit-testing for the ability to drag. |
| 63 // The value should never be dereferenced in any of the tests below. |
| 64 return reinterpret_cast<DialogDelegate*>(1); |
| 65 } |
| 66 |
| 47 class TestBubbleFrameView : public BubbleFrameView { | 67 class TestBubbleFrameView : public BubbleFrameView { |
| 48 public: | 68 public: |
| 49 TestBubbleFrameView(); | 69 TestBubbleFrameView(); |
| 50 virtual ~TestBubbleFrameView(); | 70 virtual ~TestBubbleFrameView(); |
| 51 | 71 |
| 52 protected: | 72 protected: |
| 53 virtual gfx::Rect GetMonitorBounds(const gfx::Rect& rect) OVERRIDE; | 73 virtual gfx::Rect GetMonitorBounds(const gfx::Rect& rect) OVERRIDE; |
| 54 | 74 |
| 55 private: | 75 private: |
| 56 gfx::Rect monitor_bounds_; | 76 gfx::Rect monitor_bounds_; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 78 EXPECT_EQ(kColor, frame.bubble_border()->background_color()); | 98 EXPECT_EQ(kColor, frame.bubble_border()->background_color()); |
| 79 | 99 |
| 80 int margin_x = frame.content_margins().left(); | 100 int margin_x = frame.content_margins().left(); |
| 81 int margin_y = frame.content_margins().top(); | 101 int margin_y = frame.content_margins().top(); |
| 82 gfx::Insets insets = frame.bubble_border()->GetInsets(); | 102 gfx::Insets insets = frame.bubble_border()->GetInsets(); |
| 83 EXPECT_EQ(insets.left() + margin_x, frame.GetBoundsForClientView().x()); | 103 EXPECT_EQ(insets.left() + margin_x, frame.GetBoundsForClientView().x()); |
| 84 EXPECT_EQ(insets.top() + margin_y, frame.GetBoundsForClientView().y()); | 104 EXPECT_EQ(insets.top() + margin_y, frame.GetBoundsForClientView().y()); |
| 85 } | 105 } |
| 86 | 106 |
| 87 TEST_F(BubbleFrameViewTest, NonClientHitTest) { | 107 TEST_F(BubbleFrameViewTest, NonClientHitTest) { |
| 88 // Create the anchor and parent widgets. | 108 // Create the anchor view, its parent widget is needed on Aura. |
| 89 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); | 109 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); |
| 90 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 110 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 91 scoped_ptr<Widget> anchor_widget(new Widget); | 111 scoped_ptr<Widget> anchor_widget(new Widget); |
| 92 anchor_widget->Init(params); | 112 anchor_widget->Init(params); |
| 93 anchor_widget->Show(); | 113 anchor_widget->Show(); |
| 94 | 114 |
| 95 BubbleDelegateView* delegate = | 115 TestBubbleDelegateView bubble(anchor_widget->GetContentsView()); |
| 96 new SizedBubbleDelegateView(anchor_widget->GetContentsView()); | 116 BubbleDelegateView::CreateBubble(&bubble); |
| 97 Widget* widget(BubbleDelegateView::CreateBubble(delegate)); | 117 BubbleFrameView* frame = bubble.GetBubbleFrameView(); |
| 98 widget->Show(); | 118 const int border = frame->bubble_border()->GetBorderThickness(); |
| 99 gfx::Point kPtInBound(100, 100); | 119 |
| 100 gfx::Point kPtOutsideBound(1000, 1000); | 120 struct { |
| 101 BubbleFrameView* bubble_frame_view = delegate->GetBubbleFrameView(); | 121 const int point; |
| 102 EXPECT_EQ(HTCLIENT, bubble_frame_view->NonClientHitTest(kPtInBound)); | 122 const int cannot_resize_hit; |
| 103 EXPECT_EQ(HTNOWHERE, bubble_frame_view->NonClientHitTest(kPtOutsideBound)); | 123 const int can_resize_hit; |
| 104 widget->CloseNow(); | 124 } cases[] = { |
| 105 RunPendingMessages(); | 125 { border, HTCAPTION, HTTOPLEFT }, |
| 126 { border + 3, HTCAPTION, HTTOPLEFT }, |
| 127 { border + 4, HTCAPTION, HTCAPTION }, |
| 128 { border + 8, HTCAPTION, HTCAPTION }, |
| 129 { border + 50, HTCLIENT, HTCLIENT }, |
| 130 { 1000, HTNOWHERE, HTNOWHERE }, |
| 131 }; |
| 132 |
| 133 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 134 gfx::Point point(cases[i].point, cases[i].point); |
| 135 bubble.set_can_resize(false); |
| 136 EXPECT_EQ(cases[i].cannot_resize_hit, frame->NonClientHitTest(point)); |
| 137 bubble.set_can_resize(true); |
| 138 EXPECT_EQ(cases[i].can_resize_hit, frame->NonClientHitTest(point)); |
| 139 } |
| 106 } | 140 } |
| 107 | 141 |
| 108 // Tests that the arrow is mirrored as needed to better fit the screen. | 142 // Tests that the arrow is mirrored as needed to better fit the screen. |
| 109 TEST_F(BubbleFrameViewTest, GetUpdatedWindowBounds) { | 143 TEST_F(BubbleFrameViewTest, GetUpdatedWindowBounds) { |
| 110 TestBubbleFrameView frame; | 144 TestBubbleFrameView frame; |
| 111 gfx::Rect window_bounds; | 145 gfx::Rect window_bounds; |
| 112 | 146 |
| 113 gfx::Insets insets = frame.bubble_border()->GetInsets(); | 147 gfx::Insets insets = frame.bubble_border()->GetInsets(); |
| 114 int xposition = 95 - insets.width(); | 148 int xposition = 95 - insets.width(); |
| 115 | 149 |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 gfx::Rect(900, 900, 50, 50), // |anchor_rect| | 429 gfx::Rect(900, 900, 50, 50), // |anchor_rect| |
| 396 gfx::Size(500, 500), // |client_size| | 430 gfx::Size(500, 500), // |client_size| |
| 397 true); // |adjust_if_offscreen| | 431 true); // |adjust_if_offscreen| |
| 398 EXPECT_EQ(BubbleBorder::RIGHT_CENTER, frame.bubble_border()->arrow()); | 432 EXPECT_EQ(BubbleBorder::RIGHT_CENTER, frame.bubble_border()->arrow()); |
| 399 EXPECT_EQ(window_bounds.bottom(), 1000); | 433 EXPECT_EQ(window_bounds.bottom(), 1000); |
| 400 EXPECT_EQ(window_bounds.y() + | 434 EXPECT_EQ(window_bounds.y() + |
| 401 frame.bubble_border()->GetArrowOffset(window_bounds.size()), 925); | 435 frame.bubble_border()->GetArrowOffset(window_bounds.size()), 925); |
| 402 } | 436 } |
| 403 | 437 |
| 404 } // namespace views | 438 } // namespace views |
| OLD | NEW |