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
3 // found in the LICENSE file.
5 #include "ui/views/layout/box_layout.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/views/test/test_views.h"
9 #include "ui/views/view.h"
15 class BoxLayoutTest
: public testing::Test
{
17 virtual void SetUp() OVERRIDE
{
18 host_
.reset(new View
);
21 scoped_ptr
<View
> host_
;
22 scoped_ptr
<BoxLayout
> layout_
;
27 TEST_F(BoxLayoutTest
, Empty
) {
28 layout_
.reset(new BoxLayout(BoxLayout::kHorizontal
, 10, 10, 20));
29 EXPECT_EQ(gfx::Size(20, 20), layout_
->GetPreferredSize(host_
.get()));
32 TEST_F(BoxLayoutTest
, AlignmentHorizontal
) {
33 layout_
.reset(new BoxLayout(BoxLayout::kHorizontal
, 0, 0, 0));
34 View
* v1
= new StaticSizedView(gfx::Size(10, 20));
35 host_
->AddChildView(v1
);
36 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
37 host_
->AddChildView(v2
);
38 EXPECT_EQ(gfx::Size(20, 20), layout_
->GetPreferredSize(host_
.get()));
39 host_
->SetBounds(0, 0, 20, 20);
40 layout_
->Layout(host_
.get());
41 EXPECT_EQ(gfx::Rect(0, 0, 10, 20), v1
->bounds());
42 EXPECT_EQ(gfx::Rect(10, 0, 10, 20), v2
->bounds());
45 TEST_F(BoxLayoutTest
, AlignmentVertical
) {
46 layout_
.reset(new BoxLayout(BoxLayout::kVertical
, 0, 0, 0));
47 View
* v1
= new StaticSizedView(gfx::Size(20, 10));
48 host_
->AddChildView(v1
);
49 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
50 host_
->AddChildView(v2
);
51 EXPECT_EQ(gfx::Size(20, 20), layout_
->GetPreferredSize(host_
.get()));
52 host_
->SetBounds(0, 0, 20, 20);
53 layout_
->Layout(host_
.get());
54 EXPECT_EQ(gfx::Rect(0, 0, 20, 10), v1
->bounds());
55 EXPECT_EQ(gfx::Rect(0, 10, 20, 10), v2
->bounds());
58 TEST_F(BoxLayoutTest
, SetInsideBorderInsets
) {
59 layout_
.reset(new BoxLayout(BoxLayout::kHorizontal
, 10, 20, 0));
60 View
* v1
= new StaticSizedView(gfx::Size(10, 20));
61 host_
->AddChildView(v1
);
62 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
63 host_
->AddChildView(v2
);
64 EXPECT_EQ(gfx::Size(40, 60), layout_
->GetPreferredSize(host_
.get()));
65 host_
->SetBounds(0, 0, 40, 60);
66 layout_
->Layout(host_
.get());
67 EXPECT_EQ(gfx::Rect(10, 20, 10, 20), v1
->bounds());
68 EXPECT_EQ(gfx::Rect(20, 20, 10, 20), v2
->bounds());
70 layout_
->set_inside_border_insets(
71 gfx::Insets(5, 10, 15, 20));
72 EXPECT_EQ(gfx::Size(50, 40), layout_
->GetPreferredSize(host_
.get()));
73 host_
->SetBounds(0, 0, 50, 40);
74 layout_
->Layout(host_
.get());
75 EXPECT_EQ(gfx::Rect(10, 5, 10, 20), v1
->bounds());
76 EXPECT_EQ(gfx::Rect(20, 5, 10, 20), v2
->bounds());
79 TEST_F(BoxLayoutTest
, Spacing
) {
80 layout_
.reset(new BoxLayout(BoxLayout::kHorizontal
, 7, 7, 8));
81 View
* v1
= new StaticSizedView(gfx::Size(10, 20));
82 host_
->AddChildView(v1
);
83 View
* v2
= new StaticSizedView(gfx::Size(10, 20));
84 host_
->AddChildView(v2
);
85 EXPECT_EQ(gfx::Size(42, 34), layout_
->GetPreferredSize(host_
.get()));
86 host_
->SetBounds(0, 0, 100, 100);
87 layout_
->Layout(host_
.get());
88 EXPECT_EQ(gfx::Rect(7, 7, 10, 86), v1
->bounds());
89 EXPECT_EQ(gfx::Rect(25, 7, 10, 86), v2
->bounds());
92 TEST_F(BoxLayoutTest
, Overflow
) {
93 layout_
.reset(new BoxLayout(BoxLayout::kHorizontal
, 0, 0, 0));
94 View
* v1
= new StaticSizedView(gfx::Size(20, 20));
95 host_
->AddChildView(v1
);
96 View
* v2
= new StaticSizedView(gfx::Size(10, 20));
97 host_
->AddChildView(v2
);
98 host_
->SetBounds(0, 0, 10, 10);
100 // Overflows by positioning views at the start and truncating anything that
102 layout_
->Layout(host_
.get());
103 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1
->bounds());
104 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2
->bounds());
106 // All values of main axis alignment should overflow in the same manner.
107 layout_
->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START
);
108 layout_
->Layout(host_
.get());
109 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1
->bounds());
110 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2
->bounds());
112 layout_
->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER
);
113 layout_
->Layout(host_
.get());
114 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1
->bounds());
115 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2
->bounds());
117 layout_
->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END
);
118 layout_
->Layout(host_
.get());
119 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1
->bounds());
120 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2
->bounds());
123 TEST_F(BoxLayoutTest
, NoSpace
) {
124 layout_
.reset(new BoxLayout(BoxLayout::kHorizontal
, 10, 10, 10));
125 View
* childView
= new StaticSizedView(gfx::Size(20, 20));
126 host_
->AddChildView(childView
);
127 host_
->SetBounds(0, 0, 10, 10);
128 layout_
->Layout(host_
.get());
129 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), childView
->bounds());
132 TEST_F(BoxLayoutTest
, InvisibleChild
) {
133 layout_
.reset(new BoxLayout(BoxLayout::kHorizontal
, 10, 10, 10));
134 View
* v1
= new StaticSizedView(gfx::Size(20, 20));
135 v1
->SetVisible(false);
136 host_
->AddChildView(v1
);
137 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
138 host_
->AddChildView(v2
);
139 EXPECT_EQ(gfx::Size(30, 30), layout_
->GetPreferredSize(host_
.get()));
140 host_
->SetBounds(0, 0, 30, 30);
141 layout_
->Layout(host_
.get());
142 EXPECT_EQ(gfx::Rect(10, 10, 10, 10), v2
->bounds());
145 TEST_F(BoxLayoutTest
, MainAxisAlignmentFill
) {
146 layout_
.reset(new BoxLayout(BoxLayout::kHorizontal
, 10, 10, 10));
147 layout_
->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_FILL
);
149 View
* v1
= new StaticSizedView(gfx::Size(20, 20));
150 host_
->AddChildView(v1
);
151 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
152 host_
->AddChildView(v2
);
153 EXPECT_EQ(gfx::Size(60, 40), layout_
->GetPreferredSize(host_
.get()));
155 host_
->SetBounds(0, 0, 100, 40);
156 layout_
->Layout(host_
.get());
157 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1
->bounds().ToString());
158 EXPECT_EQ(gfx::Rect(60, 10, 30, 20).ToString(), v2
->bounds().ToString());
161 TEST_F(BoxLayoutTest
, UseHeightForWidth
) {
162 layout_
.reset(new BoxLayout(BoxLayout::kVertical
, 0, 0, 0));
163 View
* v1
= new StaticSizedView(gfx::Size(20, 10));
164 host_
->AddChildView(v1
);
165 ProportionallySizedView
* v2
= new ProportionallySizedView(2);
166 v2
->set_preferred_width(10);
167 host_
->AddChildView(v2
);
168 EXPECT_EQ(gfx::Size(20, 50), layout_
->GetPreferredSize(host_
.get()));
170 host_
->SetBounds(0, 0, 20, 50);
171 layout_
->Layout(host_
.get());
172 EXPECT_EQ(gfx::Rect(0, 0, 20, 10), v1
->bounds());
173 EXPECT_EQ(gfx::Rect(0, 10, 20, 40), v2
->bounds());
175 EXPECT_EQ(110, layout_
->GetPreferredHeightForWidth(host_
.get(), 50));
177 // Test without horizontal stretching of the views.
178 layout_
->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END
);
179 EXPECT_EQ(gfx::Size(20, 30).ToString(),
180 layout_
->GetPreferredSize(host_
.get()).ToString());
182 host_
->SetBounds(0, 0, 20, 30);
183 layout_
->Layout(host_
.get());
184 EXPECT_EQ(gfx::Rect(0, 0, 20, 10), v1
->bounds());
185 EXPECT_EQ(gfx::Rect(10, 10, 10, 20), v2
->bounds());
187 EXPECT_EQ(30, layout_
->GetPreferredHeightForWidth(host_
.get(), 50));
190 TEST_F(BoxLayoutTest
, EmptyPreferredSize
) {
191 for (size_t i
= 0; i
< 2; i
++) {
192 BoxLayout::Orientation orientation
= i
== 0 ? BoxLayout::kHorizontal
:
193 BoxLayout::kVertical
;
194 host_
->RemoveAllChildViews(true);
195 host_
->SetLayoutManager(new BoxLayout(orientation
, 0, 0, 5));
196 View
* v1
= new StaticSizedView(gfx::Size());
197 host_
->AddChildView(v1
);
198 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
199 host_
->AddChildView(v2
);
200 host_
->SizeToPreferredSize();
203 EXPECT_EQ(v2
->GetPreferredSize().width(), host_
->bounds().width()) << i
;
204 EXPECT_EQ(v2
->GetPreferredSize().height(), host_
->bounds().height()) << i
;
205 EXPECT_EQ(v1
->GetPreferredSize().width(), v1
->bounds().width()) << i
;
206 EXPECT_EQ(v1
->GetPreferredSize().height(), v1
->bounds().height()) << i
;
207 EXPECT_EQ(v2
->GetPreferredSize().width(), v2
->bounds().width()) << i
;
208 EXPECT_EQ(v2
->GetPreferredSize().height(), v2
->bounds().height()) << i
;
212 TEST_F(BoxLayoutTest
, MainAxisAlignmentHorizontal
) {
213 layout_
.reset(new BoxLayout(BoxLayout::kHorizontal
, 10, 10, 10));
215 View
* v1
= new StaticSizedView(gfx::Size(20, 20));
216 host_
->AddChildView(v1
);
217 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
218 host_
->AddChildView(v2
);
220 host_
->SetBounds(0, 0, 100, 40);
222 // Align children to the horizontal start by default.
223 layout_
->Layout(host_
.get());
224 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1
->bounds().ToString());
225 EXPECT_EQ(gfx::Rect(40, 10, 10, 20).ToString(), v2
->bounds().ToString());
227 // Ensure same results for MAIN_AXIS_ALIGNMENT_START.
228 layout_
->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START
);
229 layout_
->Layout(host_
.get());
230 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1
->bounds().ToString());
231 EXPECT_EQ(gfx::Rect(40, 10, 10, 20).ToString(), v2
->bounds().ToString());
233 // Aligns children to the center horizontally.
234 layout_
->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER
);
235 layout_
->Layout(host_
.get());
236 EXPECT_EQ(gfx::Rect(30, 10, 20, 20).ToString(), v1
->bounds().ToString());
237 EXPECT_EQ(gfx::Rect(60, 10, 10, 20).ToString(), v2
->bounds().ToString());
239 // Aligns children to the end of the host horizontally, accounting for the
240 // inside border spacing.
241 layout_
->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END
);
242 layout_
->Layout(host_
.get());
243 EXPECT_EQ(gfx::Rect(50, 10, 20, 20).ToString(), v1
->bounds().ToString());
244 EXPECT_EQ(gfx::Rect(80, 10, 10, 20).ToString(), v2
->bounds().ToString());
247 TEST_F(BoxLayoutTest
, MainAxisAlignmentVertical
) {
248 layout_
.reset(new BoxLayout(BoxLayout::kVertical
, 10, 10, 10));
250 View
* v1
= new StaticSizedView(gfx::Size(20, 20));
251 host_
->AddChildView(v1
);
252 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
253 host_
->AddChildView(v2
);
255 host_
->SetBounds(0, 0, 40, 100);
257 // Align children to the vertical start by default.
258 layout_
->Layout(host_
.get());
259 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1
->bounds().ToString());
260 EXPECT_EQ(gfx::Rect(10, 40, 20, 10).ToString(), v2
->bounds().ToString());
262 // Ensure same results for MAIN_AXIS_ALIGNMENT_START.
263 layout_
->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START
);
264 layout_
->Layout(host_
.get());
265 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1
->bounds().ToString());
266 EXPECT_EQ(gfx::Rect(10, 40, 20, 10).ToString(), v2
->bounds().ToString());
268 // Aligns children to the center vertically.
269 layout_
->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER
);
270 layout_
->Layout(host_
.get());
271 EXPECT_EQ(gfx::Rect(10, 30, 20, 20).ToString(), v1
->bounds().ToString());
272 EXPECT_EQ(gfx::Rect(10, 60, 20, 10).ToString(), v2
->bounds().ToString());
274 // Aligns children to the end of the host vertically, accounting for the
275 // inside border spacing.
276 layout_
->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END
);
277 layout_
->Layout(host_
.get());
278 EXPECT_EQ(gfx::Rect(10, 50, 20, 20).ToString(), v1
->bounds().ToString());
279 EXPECT_EQ(gfx::Rect(10, 80, 20, 10).ToString(), v2
->bounds().ToString());
282 TEST_F(BoxLayoutTest
, CrossAxisAlignmentHorizontal
) {
283 layout_
.reset(new BoxLayout(BoxLayout::kHorizontal
, 10, 10, 10));
285 View
* v1
= new StaticSizedView(gfx::Size(20, 20));
286 host_
->AddChildView(v1
);
287 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
288 host_
->AddChildView(v2
);
290 host_
->SetBounds(0, 0, 100, 60);
292 // Stretch children to fill the available height by default.
293 layout_
->Layout(host_
.get());
294 EXPECT_EQ(gfx::Rect(10, 10, 20, 40).ToString(), v1
->bounds().ToString());
295 EXPECT_EQ(gfx::Rect(40, 10, 10, 40).ToString(), v2
->bounds().ToString());
297 // Ensure same results for CROSS_AXIS_ALIGNMENT_STRETCH.
298 layout_
->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH
);
299 layout_
->Layout(host_
.get());
300 EXPECT_EQ(gfx::Rect(10, 10, 20, 40).ToString(), v1
->bounds().ToString());
301 EXPECT_EQ(gfx::Rect(40, 10, 10, 40).ToString(), v2
->bounds().ToString());
303 // Aligns children to the start vertically.
304 layout_
->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_START
);
305 layout_
->Layout(host_
.get());
306 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1
->bounds().ToString());
307 EXPECT_EQ(gfx::Rect(40, 10, 10, 10).ToString(), v2
->bounds().ToString());
309 // Aligns children to the center vertically.
310 layout_
->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER
);
311 layout_
->Layout(host_
.get());
312 EXPECT_EQ(gfx::Rect(10, 20, 20, 20).ToString(), v1
->bounds().ToString());
313 EXPECT_EQ(gfx::Rect(40, 25, 10, 10).ToString(), v2
->bounds().ToString());
315 // Aligns children to the end of the host vertically, accounting for the
316 // inside border spacing.
317 layout_
->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END
);
318 layout_
->Layout(host_
.get());
319 EXPECT_EQ(gfx::Rect(10, 30, 20, 20).ToString(), v1
->bounds().ToString());
320 EXPECT_EQ(gfx::Rect(40, 40, 10, 10).ToString(), v2
->bounds().ToString());
323 TEST_F(BoxLayoutTest
, CrossAxisAlignmentVertical
) {
324 layout_
.reset(new BoxLayout(BoxLayout::kVertical
, 10, 10, 10));
326 View
* v1
= new StaticSizedView(gfx::Size(20, 20));
327 host_
->AddChildView(v1
);
328 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
329 host_
->AddChildView(v2
);
331 host_
->SetBounds(0, 0, 60, 100);
333 // Stretch children to fill the available width by default.
334 layout_
->Layout(host_
.get());
335 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1
->bounds().ToString());
336 EXPECT_EQ(gfx::Rect(10, 40, 40, 10).ToString(), v2
->bounds().ToString());
338 // Ensure same results for CROSS_AXIS_ALIGNMENT_STRETCH.
339 layout_
->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH
);
340 layout_
->Layout(host_
.get());
341 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1
->bounds().ToString());
342 EXPECT_EQ(gfx::Rect(10, 40, 40, 10).ToString(), v2
->bounds().ToString());
344 // Aligns children to the start horizontally.
345 layout_
->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_START
);
346 layout_
->Layout(host_
.get());
347 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1
->bounds().ToString());
348 EXPECT_EQ(gfx::Rect(10, 40, 10, 10).ToString(), v2
->bounds().ToString());
350 // Aligns children to the center horizontally.
351 layout_
->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER
);
352 layout_
->Layout(host_
.get());
353 EXPECT_EQ(gfx::Rect(20, 10, 20, 20).ToString(), v1
->bounds().ToString());
354 EXPECT_EQ(gfx::Rect(25, 40, 10, 10).ToString(), v2
->bounds().ToString());
356 // Aligns children to the end of the host horizontally, accounting for the
357 // inside border spacing.
358 layout_
->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END
);
359 layout_
->Layout(host_
.get());
360 EXPECT_EQ(gfx::Rect(30, 10, 20, 20).ToString(), v1
->bounds().ToString());
361 EXPECT_EQ(gfx::Rect(40, 40, 10, 10).ToString(), v2
->bounds().ToString());