Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(498)

Side by Side Diff: ui/views/bubble/bubble_frame_view_unittest.cc

Issue 8870003: Merge BorderContentsView into BubbleFrameView; simplify. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync and merge. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/views/bubble/bubble_frame_view.cc ('k') | ui/views/views.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/views/bubble/border_contents_view.h"
7 #include "ui/views/bubble/bubble_border.h" 6 #include "ui/views/bubble/bubble_border.h"
8 #include "ui/views/bubble/bubble_delegate.h" 7 #include "ui/views/bubble/bubble_delegate.h"
9 #include "ui/views/bubble/bubble_frame_view.h" 8 #include "ui/views/bubble/bubble_frame_view.h"
10 #include "ui/views/test/views_test_base.h" 9 #include "ui/views/test/views_test_base.h"
11 #include "ui/views/widget/widget.h" 10 #include "ui/views/widget/widget.h"
12 11
13 namespace views { 12 namespace views {
14 13
15 typedef ViewsTestBase BubbleFrameViewBasicTest; 14 typedef ViewsTestBase BubbleFrameViewTest;
16 15
17 const BubbleBorder::ArrowLocation kArrow = BubbleBorder::TOP_LEFT; 16 const BubbleBorder::ArrowLocation kArrow = BubbleBorder::TOP_LEFT;
18 const gfx::Rect kRect(10, 10, 200, 200); 17 const gfx::Rect kRect(10, 10, 200, 200);
19 const SkColor kBackgroundColor = SK_ColorRED; 18 const SkColor kBackgroundColor = SK_ColorRED;
20 const bool kAllowBubbleOffscreen = true;
21
22 TEST_F(BubbleFrameViewBasicTest, GetBoundsForClientView) {
23 BubbleFrameView frame(kArrow, kRect.size(), kBackgroundColor,
24 kAllowBubbleOffscreen);
25 EXPECT_EQ(frame.GetWindowBoundsForClientBounds(kRect).size(), frame.size());
26 EXPECT_EQ(kArrow, frame.bubble_border()->arrow_location());
27 EXPECT_EQ(kBackgroundColor, frame.bubble_border()->background_color());
28
29 int margin_x = frame.border_contents_->content_margins().left();
30 int margin_y = frame.border_contents_->content_margins().top();
31 gfx::Insets insets;
32 frame.bubble_border()->GetInsets(&insets);
33 EXPECT_EQ(insets.left() + margin_x, frame.GetBoundsForClientView().x());
34 EXPECT_EQ(insets.top() + margin_y, frame.GetBoundsForClientView().y());
35 }
36 19
37 namespace { 20 namespace {
38 21
39 class SizedBubbleDelegateView : public BubbleDelegateView { 22 class SizedBubbleDelegateView : public BubbleDelegateView {
40 public: 23 public:
41 SizedBubbleDelegateView() {} 24 SizedBubbleDelegateView();
42 virtual ~SizedBubbleDelegateView() {} 25 virtual ~SizedBubbleDelegateView();
43 26
44 // View overrides: 27 // View overrides:
45 virtual gfx::Size GetPreferredSize() OVERRIDE; 28 virtual gfx::Size GetPreferredSize() OVERRIDE;
46 29
47 private: 30 private:
48 DISALLOW_COPY_AND_ASSIGN(SizedBubbleDelegateView); 31 DISALLOW_COPY_AND_ASSIGN(SizedBubbleDelegateView);
49 }; 32 };
50 33
34 SizedBubbleDelegateView::SizedBubbleDelegateView() {}
35
36 SizedBubbleDelegateView::~SizedBubbleDelegateView() {}
37
51 gfx::Size SizedBubbleDelegateView::GetPreferredSize() { return kRect.size(); } 38 gfx::Size SizedBubbleDelegateView::GetPreferredSize() { return kRect.size(); }
52 39
40 class TestBubbleFrameView : public BubbleFrameView {
41 public:
42 TestBubbleFrameView(const gfx::Rect& bounds);
43 virtual ~TestBubbleFrameView();
44
45 protected:
46 virtual gfx::Rect GetMonitorBounds(const gfx::Rect& rect) OVERRIDE;
47
48 private:
49 gfx::Rect monitor_bounds_;
50
51 DISALLOW_COPY_AND_ASSIGN(TestBubbleFrameView);
52 };
53
54 TestBubbleFrameView::TestBubbleFrameView(const gfx::Rect& bounds)
55 : BubbleFrameView(kArrow, kBackgroundColor),
56 monitor_bounds_(bounds) {
57 }
58
59 TestBubbleFrameView::~TestBubbleFrameView() {}
60
61 gfx::Rect TestBubbleFrameView::GetMonitorBounds(const gfx::Rect& rect) {
62 return monitor_bounds_;
63 }
64
53 } // namespace 65 } // namespace
54 66
55 TEST_F(BubbleFrameViewBasicTest, NonClientHitTest) { 67 TEST_F(BubbleFrameViewTest, GetBoundsForClientView) {
68 BubbleFrameView frame(kArrow, kBackgroundColor);
69 EXPECT_EQ(kArrow, frame.bubble_border()->arrow_location());
70 EXPECT_EQ(kBackgroundColor, frame.bubble_border()->background_color());
71
72 int margin_x = frame.content_margins().left();
73 int margin_y = frame.content_margins().top();
74 gfx::Insets insets;
75 frame.bubble_border()->GetInsets(&insets);
76 EXPECT_EQ(insets.left() + margin_x, frame.GetBoundsForClientView().x());
77 EXPECT_EQ(insets.top() + margin_y, frame.GetBoundsForClientView().y());
78 }
79
80 TEST_F(BubbleFrameViewTest, NonClientHitTest) {
56 BubbleDelegateView* delegate = new SizedBubbleDelegateView(); 81 BubbleDelegateView* delegate = new SizedBubbleDelegateView();
57 Widget* widget(BubbleDelegateView::CreateBubble(delegate)); 82 Widget* widget(BubbleDelegateView::CreateBubble(delegate));
58 delegate->Show(); 83 delegate->Show();
59 gfx::Point kPtInBound(100, 100); 84 gfx::Point kPtInBound(100, 100);
60 gfx::Point kPtOutsideBound(1000, 1000); 85 gfx::Point kPtOutsideBound(1000, 1000);
61 BubbleFrameView* bubble_frame_view = delegate->GetBubbleFrameView(); 86 BubbleFrameView* bubble_frame_view = delegate->GetBubbleFrameView();
62 EXPECT_EQ(HTCLIENT, bubble_frame_view->NonClientHitTest(kPtInBound)); 87 EXPECT_EQ(HTCLIENT, bubble_frame_view->NonClientHitTest(kPtInBound));
63 EXPECT_EQ(HTNOWHERE, bubble_frame_view->NonClientHitTest(kPtOutsideBound)); 88 EXPECT_EQ(HTNOWHERE, bubble_frame_view->NonClientHitTest(kPtOutsideBound));
64 widget->CloseNow(); 89 widget->CloseNow();
65 RunPendingMessages(); 90 RunPendingMessages();
66 } 91 }
67 92
93 // Tests that the arrow is mirrored as needed to better fit the screen.
94 TEST_F(BubbleFrameViewTest, GetUpdatedWindowBounds) {
95 TestBubbleFrameView frame(gfx::Rect(0, 0, 1000, 1000));
96 gfx::Rect window_bounds;
97
98 gfx::Insets insets;
99 frame.bubble_border()->GetInsets(&insets);
100 int xposition = 95 - insets.width();
101
102 // Test that the info bubble displays normally when it fits.
103 frame.bubble_border()->set_arrow_location(BubbleBorder::TOP_LEFT);
104 window_bounds = frame.GetUpdatedWindowBounds(
105 gfx::Rect(100, 100, 50, 50), // |anchor_rect|
106 gfx::Size(500, 500), // |client_size|
107 true); // |try_mirroring_arrow|
108 EXPECT_EQ(BubbleBorder::TOP_LEFT, frame.bubble_border()->arrow_location());
109 EXPECT_GT(window_bounds.x(), xposition);
110 EXPECT_GT(window_bounds.y(), 100 + 50 - 10); // -10 to roughly compensate for
111 // arrow overlap.
112
113 // Test bubble not fitting on left.
114 frame.bubble_border()->set_arrow_location(BubbleBorder::TOP_RIGHT);
115 window_bounds = frame.GetUpdatedWindowBounds(
116 gfx::Rect(100, 100, 50, 50), // |anchor_rect|
117 gfx::Size(500, 500), // |client_size|
118 true); // |try_mirroring_arrow|
119 EXPECT_EQ(BubbleBorder::TOP_LEFT, frame.bubble_border()->arrow_location());
120 EXPECT_GT(window_bounds.x(), xposition);
121 EXPECT_GT(window_bounds.y(), 100 + 50 - 10); // -10 to roughly compensate for
122 // arrow overlap.
123
124 // Test bubble not fitting on left or top.
125 frame.bubble_border()->set_arrow_location(BubbleBorder::BOTTOM_RIGHT);
126 window_bounds = frame.GetUpdatedWindowBounds(
127 gfx::Rect(100, 100, 50, 50), // |anchor_rect|
128 gfx::Size(500, 500), // |client_size|
129 true); // |try_mirroring_arrow|
130 EXPECT_EQ(BubbleBorder::TOP_LEFT, frame.bubble_border()->arrow_location());
131 EXPECT_GT(window_bounds.x(), xposition);
132 EXPECT_GT(window_bounds.y(), 100 + 50 - 10); // -10 to roughly compensate for
133 // arrow overlap.
134
135 // Test bubble not fitting on top.
136 frame.bubble_border()->set_arrow_location(BubbleBorder::BOTTOM_LEFT);
137 window_bounds = frame.GetUpdatedWindowBounds(
138 gfx::Rect(100, 100, 50, 50), // |anchor_rect|
139 gfx::Size(500, 500), // |client_size|
140 true); // |try_mirroring_arrow|
141 EXPECT_EQ(BubbleBorder::TOP_LEFT, frame.bubble_border()->arrow_location());
142 EXPECT_GT(window_bounds.x(), xposition);
143 EXPECT_GT(window_bounds.y(), 100 + 50 - 10); // -10 to roughly compensate for
144 // arrow overlap.
145
146 // Test bubble not fitting on top and right.
147 frame.bubble_border()->set_arrow_location(BubbleBorder::BOTTOM_LEFT);
148 window_bounds = frame.GetUpdatedWindowBounds(
149 gfx::Rect(900, 100, 50, 50), // |anchor_rect|
150 gfx::Size(500, 500), // |client_size|
151 true); // |try_mirroring_arrow|
152 EXPECT_EQ(BubbleBorder::TOP_RIGHT, frame.bubble_border()->arrow_location());
153 EXPECT_LT(window_bounds.x(), 900 + 50 - 500);
154 EXPECT_GT(window_bounds.y(), 100 + 50 - 10); // -10 to roughly compensate for
155 // arrow overlap.
156
157 // Test bubble not fitting on right.
158 frame.bubble_border()->set_arrow_location(BubbleBorder::TOP_LEFT);
159 window_bounds = frame.GetUpdatedWindowBounds(
160 gfx::Rect(900, 100, 50, 50), // |anchor_rect|
161 gfx::Size(500, 500), // |client_size|
162 true); // |try_mirroring_arrow|
163 EXPECT_EQ(BubbleBorder::TOP_RIGHT, frame.bubble_border()->arrow_location());
164 EXPECT_LT(window_bounds.x(), 900 + 50 - 500);
165 EXPECT_GT(window_bounds.y(), 100 + 50 - 10); // -10 to roughly compensate for
166 // arrow overlap.
167
168 // Test bubble not fitting on bottom and right.
169 frame.bubble_border()->set_arrow_location(BubbleBorder::TOP_LEFT);
170 window_bounds = frame.GetUpdatedWindowBounds(
171 gfx::Rect(900, 900, 50, 50), // |anchor_rect|
172 gfx::Size(500, 500), // |client_size|
173 true); // |try_mirroring_arrow|
174 EXPECT_EQ(BubbleBorder::BOTTOM_RIGHT,
175 frame.bubble_border()->arrow_location());
176 EXPECT_LT(window_bounds.x(), 900 + 50 - 500);
177 EXPECT_LT(window_bounds.y(), 900 - 500 - 15); // -15 to roughly compensate
178 // for arrow height.
179
180 // Test bubble not fitting at the bottom.
181 frame.bubble_border()->set_arrow_location(BubbleBorder::TOP_LEFT);
182 window_bounds = frame.GetUpdatedWindowBounds(
183 gfx::Rect(100, 900, 50, 50), // |anchor_rect|
184 gfx::Size(500, 500), // |client_size|
185 true); // |try_mirroring_arrow|
186 EXPECT_EQ(BubbleBorder::BOTTOM_LEFT, frame.bubble_border()->arrow_location());
187 // The window should be right aligned with the anchor_rect.
188 EXPECT_LT(window_bounds.x(), 900 + 50 - 500);
189 EXPECT_LT(window_bounds.y(), 900 - 500 - 15); // -15 to roughly compensate
190 // for arrow height.
191
192 // Test bubble not fitting at the bottom and left.
193 frame.bubble_border()->set_arrow_location(BubbleBorder::TOP_RIGHT);
194 window_bounds = frame.GetUpdatedWindowBounds(
195 gfx::Rect(100, 900, 50, 50), // |anchor_rect|
196 gfx::Size(500, 500), // |client_size|
197 true); // |try_mirroring_arrow|
198 EXPECT_EQ(BubbleBorder::BOTTOM_LEFT, frame.bubble_border()->arrow_location());
199 // The window should be right aligned with the anchor_rect.
200 EXPECT_LT(window_bounds.x(), 900 + 50 - 500);
201 EXPECT_LT(window_bounds.y(), 900 - 500 - 15); // -15 to roughly compensate
202 // for arrow height.
203 }
204
205 // Tests that the arrow is not moved when the info-bubble does not fit the
206 // screen but moving it would make matter worse.
207 TEST_F(BubbleFrameViewTest, GetUpdatedWindowBoundsMirroringFails) {
208 TestBubbleFrameView frame(gfx::Rect(0, 0, 1000, 1000));
209 frame.bubble_border()->set_arrow_location(BubbleBorder::TOP_LEFT);
210 gfx::Rect window_bounds = frame.GetUpdatedWindowBounds(
211 gfx::Rect(400, 100, 50, 50), // |anchor_rect|
212 gfx::Size(500, 700), // |client_size|
213 true); // |try_mirroring_arrow|
214 EXPECT_EQ(BubbleBorder::TOP_LEFT, frame.bubble_border()->arrow_location());
215 }
216
217 // Test that the arrow will not be mirrored when |try_mirroring_arrow| is false.
218 TEST_F(BubbleFrameViewTest, GetUpdatedWindowBoundsDontTryMirror) {
219 TestBubbleFrameView frame(gfx::Rect(0, 0, 1000, 1000));
220 frame.bubble_border()->set_arrow_location(BubbleBorder::TOP_RIGHT);
221 gfx::Rect window_bounds = frame.GetUpdatedWindowBounds(
222 gfx::Rect(100, 900, 50, 50), // |anchor_rect|
223 gfx::Size(500, 500), // |client_size|
224 false); // |try_mirroring_arrow|
225 EXPECT_EQ(BubbleBorder::TOP_RIGHT, frame.bubble_border()->arrow_location());
226 // The coordinates should be pointing to anchor_rect from TOP_RIGHT.
227 EXPECT_LT(window_bounds.x(), 100 + 50 - 500);
228 EXPECT_GT(window_bounds.y(), 900 + 50 - 10); // -10 to roughly compensate for
229 // arrow overlap.
230 }
231
68 } // namespace views 232 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/bubble/bubble_frame_view.cc ('k') | ui/views/views.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698