| 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 "base/run_loop.h" | 5 #include "base/run_loop.h" |
| 6 #include "ui/base/hit_test.h" | 6 #include "ui/base/hit_test.h" |
| 7 #include "ui/views/bubble/bubble_delegate.h" | 7 #include "ui/views/bubble/bubble_delegate.h" |
| 8 #include "ui/views/bubble/bubble_frame_view.h" | 8 #include "ui/views/bubble/bubble_frame_view.h" |
| 9 #include "ui/views/test/test_widget_observer.h" | 9 #include "ui/views/test/test_widget_observer.h" |
| 10 #include "ui/views/test/views_test_base.h" | 10 #include "ui/views/test/views_test_base.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 class TestBubbleDelegateView : public BubbleDelegateView { | 22 class TestBubbleDelegateView : public BubbleDelegateView { |
| 23 public: | 23 public: |
| 24 TestBubbleDelegateView(View* anchor_view) | 24 TestBubbleDelegateView(View* anchor_view) |
| 25 : BubbleDelegateView(anchor_view, BubbleBorder::TOP_LEFT), | 25 : BubbleDelegateView(anchor_view, BubbleBorder::TOP_LEFT), |
| 26 view_(new View()) { | 26 view_(new View()) { |
| 27 view_->set_focusable(true); | 27 view_->set_focusable(true); |
| 28 AddChildView(view_); | 28 AddChildView(view_); |
| 29 } | 29 } |
| 30 virtual ~TestBubbleDelegateView() {} | 30 virtual ~TestBubbleDelegateView() {} |
| 31 | 31 |
| 32 void SetAnchorRectForTest(gfx::Rect rect) { |
| 33 set_anchor_rect(rect); |
| 34 } |
| 35 |
| 36 void SetAnchorViewForTest(View* view) { |
| 37 SetAnchorView(view); |
| 38 } |
| 39 |
| 32 // BubbleDelegateView overrides: | 40 // BubbleDelegateView overrides: |
| 33 virtual View* GetInitiallyFocusedView() OVERRIDE { return view_; } | 41 virtual View* GetInitiallyFocusedView() OVERRIDE { return view_; } |
| 34 virtual gfx::Size GetPreferredSize() OVERRIDE { return gfx::Size(200, 200); } | 42 virtual gfx::Size GetPreferredSize() OVERRIDE { return gfx::Size(200, 200); } |
| 35 virtual int GetFadeDuration() OVERRIDE { return 1; } | 43 virtual int GetFadeDuration() OVERRIDE { return 1; } |
| 36 | 44 |
| 37 private: | 45 private: |
| 38 View* view_; | 46 View* view_; |
| 39 | 47 |
| 40 DISALLOW_COPY_AND_ASSIGN(TestBubbleDelegateView); | 48 DISALLOW_COPY_AND_ASSIGN(TestBubbleDelegateView); |
| 41 }; | 49 }; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 // aura::test::TestActivationClient::OnWindowDestroyed(). | 110 // aura::test::TestActivationClient::OnWindowDestroyed(). |
| 103 scoped_ptr<Widget> smoke_and_mirrors_widget(CreateTestWidget()); | 111 scoped_ptr<Widget> smoke_and_mirrors_widget(CreateTestWidget()); |
| 104 EXPECT_FALSE(bubble_observer.widget_closed()); | 112 EXPECT_FALSE(bubble_observer.widget_closed()); |
| 105 #endif | 113 #endif |
| 106 | 114 |
| 107 // Ensure that closing the anchor widget also closes the bubble itself. | 115 // Ensure that closing the anchor widget also closes the bubble itself. |
| 108 anchor_widget->CloseNow(); | 116 anchor_widget->CloseNow(); |
| 109 EXPECT_TRUE(bubble_observer.widget_closed()); | 117 EXPECT_TRUE(bubble_observer.widget_closed()); |
| 110 } | 118 } |
| 111 | 119 |
| 120 TEST_F(BubbleDelegateTest, CloseAnchorViewTest) { |
| 121 // Create an anchor widget and add a view to be used as an anchor view. |
| 122 scoped_ptr<Widget> anchor_widget(CreateTestWidget()); |
| 123 scoped_ptr<View> anchor_view(new View()); |
| 124 anchor_widget->GetContentsView()->AddChildView(anchor_view.get()); |
| 125 TestBubbleDelegateView* bubble_delegate = new TestBubbleDelegateView( |
| 126 anchor_view.get()); |
| 127 Widget* bubble_widget = BubbleDelegateView::CreateBubble(bubble_delegate); |
| 128 |
| 129 // Check that the anchor view is correct and set up an anchor view rect. |
| 130 // Make sure that this rect will get ignored (as long as the anchor view is |
| 131 // attached). |
| 132 EXPECT_EQ(anchor_view, bubble_delegate->GetAnchorView()); |
| 133 const gfx::Rect set_anchor_rect = gfx::Rect(10, 10, 100, 100); |
| 134 bubble_delegate->SetAnchorRectForTest(set_anchor_rect); |
| 135 const gfx::Rect view_rect = bubble_delegate->GetAnchorRect(); |
| 136 EXPECT_NE(view_rect.ToString(), set_anchor_rect.ToString()); |
| 137 |
| 138 // Create the bubble. |
| 139 bubble_widget->Show(); |
| 140 EXPECT_EQ(anchor_widget, bubble_delegate->anchor_widget()); |
| 141 |
| 142 // Remove now the anchor view and make sure that the original found rect |
| 143 // is still kept, so that the bubble does not jump when the view gets deleted. |
| 144 anchor_widget->GetContentsView()->RemoveChildView(anchor_view.get()); |
| 145 anchor_view.reset(); |
| 146 EXPECT_EQ(NULL, bubble_delegate->GetAnchorView()); |
| 147 EXPECT_EQ(view_rect.ToString(), bubble_delegate->GetAnchorRect().ToString()); |
| 148 } |
| 149 |
| 150 TEST_F(BubbleDelegateTest, TestAnchorRectMovesWithViewTest) { |
| 151 // Create an anchor widget and add a view to be used as anchor view. |
| 152 scoped_ptr<Widget> anchor_widget(CreateTestWidget()); |
| 153 TestBubbleDelegateView* bubble_delegate = new TestBubbleDelegateView( |
| 154 anchor_widget->GetContentsView()); |
| 155 BubbleDelegateView::CreateBubble(bubble_delegate); |
| 156 |
| 157 anchor_widget->GetContentsView()->SetBounds(10, 10, 100, 100); |
| 158 const gfx::Rect view_rect = bubble_delegate->GetAnchorRect(); |
| 159 |
| 160 anchor_widget->GetContentsView()->SetBounds(20, 10, 100, 100); |
| 161 const gfx::Rect view_rect_2 = bubble_delegate->GetAnchorRect(); |
| 162 EXPECT_NE(view_rect.ToString(), view_rect_2.ToString()); |
| 163 } |
| 164 |
| 112 TEST_F(BubbleDelegateTest, ResetAnchorWidget) { | 165 TEST_F(BubbleDelegateTest, ResetAnchorWidget) { |
| 113 scoped_ptr<Widget> anchor_widget(CreateTestWidget()); | 166 scoped_ptr<Widget> anchor_widget(CreateTestWidget()); |
| 114 BubbleDelegateView* bubble_delegate = new BubbleDelegateView( | 167 BubbleDelegateView* bubble_delegate = new BubbleDelegateView( |
| 115 anchor_widget->GetContentsView(), BubbleBorder::NONE); | 168 anchor_widget->GetContentsView(), BubbleBorder::NONE); |
| 116 | 169 |
| 117 // Make sure the bubble widget is parented to a widget other than the anchor | 170 // Make sure the bubble widget is parented to a widget other than the anchor |
| 118 // widget so that closing the anchor widget does not close the bubble widget. | 171 // widget so that closing the anchor widget does not close the bubble widget. |
| 119 scoped_ptr<Widget> parent_widget(CreateTestWidget()); | 172 scoped_ptr<Widget> parent_widget(CreateTestWidget()); |
| 120 bubble_delegate->set_parent_window(parent_widget->GetNativeView()); | 173 bubble_delegate->set_parent_window(parent_widget->GetNativeView()); |
| 121 // Preventing close on deactivate should not prevent closing with the parent. | 174 // Preventing close on deactivate should not prevent closing with the parent. |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 bubble_widget->GetFocusManager()->GetFocusedView()); | 292 bubble_widget->GetFocusManager()->GetFocusedView()); |
| 240 | 293 |
| 241 Observe(bubble_widget); | 294 Observe(bubble_widget); |
| 242 | 295 |
| 243 bubble_delegate->StartFade(false); | 296 bubble_delegate->StartFade(false); |
| 244 RunNestedLoop(); | 297 RunNestedLoop(); |
| 245 EXPECT_TRUE(bubble_destroyed()); | 298 EXPECT_TRUE(bubble_destroyed()); |
| 246 } | 299 } |
| 247 | 300 |
| 248 } // namespace views | 301 } // namespace views |
| OLD | NEW |