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 // This test checks that the bubble delegate is capable to handle an early |
| 121 // destruction of the used anchor view. (Animations and delayed closure of the |
| 122 // bubble will call upon the anchor view to get its location). |
| 123 TEST_F(BubbleDelegateTest, CloseAnchorViewTest) { |
| 124 // Create an anchor widget and add a view to be used as an anchor view. |
| 125 scoped_ptr<Widget> anchor_widget(CreateTestWidget()); |
| 126 scoped_ptr<View> anchor_view(new View()); |
| 127 anchor_widget->GetContentsView()->AddChildView(anchor_view.get()); |
| 128 TestBubbleDelegateView* bubble_delegate = new TestBubbleDelegateView( |
| 129 anchor_view.get()); |
| 130 // Prevent flakes by avoiding closing on activation changes. |
| 131 bubble_delegate->set_close_on_deactivate(false); |
| 132 Widget* bubble_widget = BubbleDelegateView::CreateBubble(bubble_delegate); |
| 133 |
| 134 // Check that the anchor view is correct and set up an anchor view rect. |
| 135 // Make sure that this rect will get ignored (as long as the anchor view is |
| 136 // attached). |
| 137 EXPECT_EQ(anchor_view, bubble_delegate->GetAnchorView()); |
| 138 const gfx::Rect set_anchor_rect = gfx::Rect(10, 10, 100, 100); |
| 139 bubble_delegate->SetAnchorRectForTest(set_anchor_rect); |
| 140 const gfx::Rect view_rect = bubble_delegate->GetAnchorRect(); |
| 141 EXPECT_NE(view_rect.ToString(), set_anchor_rect.ToString()); |
| 142 |
| 143 // Create the bubble. |
| 144 bubble_widget->Show(); |
| 145 EXPECT_EQ(anchor_widget, bubble_delegate->anchor_widget()); |
| 146 |
| 147 // Remove now the anchor view and make sure that the original found rect |
| 148 // is still kept, so that the bubble does not jump when the view gets deleted. |
| 149 anchor_widget->GetContentsView()->RemoveChildView(anchor_view.get()); |
| 150 anchor_view.reset(); |
| 151 EXPECT_EQ(NULL, bubble_delegate->GetAnchorView()); |
| 152 EXPECT_EQ(view_rect.ToString(), bubble_delegate->GetAnchorRect().ToString()); |
| 153 } |
| 154 |
| 155 // Testing that a move of the anchor view will lead to new bubble locations. |
| 156 TEST_F(BubbleDelegateTest, TestAnchorRectMovesWithViewTest) { |
| 157 // Create an anchor widget and add a view to be used as anchor view. |
| 158 scoped_ptr<Widget> anchor_widget(CreateTestWidget()); |
| 159 TestBubbleDelegateView* bubble_delegate = new TestBubbleDelegateView( |
| 160 anchor_widget->GetContentsView()); |
| 161 BubbleDelegateView::CreateBubble(bubble_delegate); |
| 162 |
| 163 anchor_widget->GetContentsView()->SetBounds(10, 10, 100, 100); |
| 164 const gfx::Rect view_rect = bubble_delegate->GetAnchorRect(); |
| 165 |
| 166 anchor_widget->GetContentsView()->SetBounds(20, 10, 100, 100); |
| 167 const gfx::Rect view_rect_2 = bubble_delegate->GetAnchorRect(); |
| 168 EXPECT_NE(view_rect.ToString(), view_rect_2.ToString()); |
| 169 } |
| 170 |
112 TEST_F(BubbleDelegateTest, ResetAnchorWidget) { | 171 TEST_F(BubbleDelegateTest, ResetAnchorWidget) { |
113 scoped_ptr<Widget> anchor_widget(CreateTestWidget()); | 172 scoped_ptr<Widget> anchor_widget(CreateTestWidget()); |
114 BubbleDelegateView* bubble_delegate = new BubbleDelegateView( | 173 BubbleDelegateView* bubble_delegate = new BubbleDelegateView( |
115 anchor_widget->GetContentsView(), BubbleBorder::NONE); | 174 anchor_widget->GetContentsView(), BubbleBorder::NONE); |
116 | 175 |
117 // Make sure the bubble widget is parented to a widget other than the anchor | 176 // 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. | 177 // widget so that closing the anchor widget does not close the bubble widget. |
119 scoped_ptr<Widget> parent_widget(CreateTestWidget()); | 178 scoped_ptr<Widget> parent_widget(CreateTestWidget()); |
120 bubble_delegate->set_parent_window(parent_widget->GetNativeView()); | 179 bubble_delegate->set_parent_window(parent_widget->GetNativeView()); |
121 // Preventing close on deactivate should not prevent closing with the parent. | 180 // 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()); | 298 bubble_widget->GetFocusManager()->GetFocusedView()); |
240 | 299 |
241 Observe(bubble_widget); | 300 Observe(bubble_widget); |
242 | 301 |
243 bubble_delegate->StartFade(false); | 302 bubble_delegate->StartFade(false); |
244 RunNestedLoop(); | 303 RunNestedLoop(); |
245 EXPECT_TRUE(bubble_destroyed()); | 304 EXPECT_TRUE(bubble_destroyed()); |
246 } | 305 } |
247 | 306 |
248 } // namespace views | 307 } // namespace views |
OLD | NEW |