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