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 void SetUp() override
{ host_
.reset(new View
); }
19 scoped_ptr
<View
> host_
;
24 TEST_F(BoxLayoutTest
, Empty
) {
25 BoxLayout
* layout
= new BoxLayout(BoxLayout::kHorizontal
, 10, 10, 20);
26 host_
->SetLayoutManager(layout
);
27 EXPECT_EQ(gfx::Size(20, 20), layout
->GetPreferredSize(host_
.get()));
30 TEST_F(BoxLayoutTest
, AlignmentHorizontal
) {
31 BoxLayout
* layout
= new BoxLayout(BoxLayout::kHorizontal
, 0, 0, 0);
32 host_
->SetLayoutManager(layout
);
33 View
* v1
= new StaticSizedView(gfx::Size(10, 20));
34 host_
->AddChildView(v1
);
35 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
36 host_
->AddChildView(v2
);
37 EXPECT_EQ(gfx::Size(20, 20), layout
->GetPreferredSize(host_
.get()));
38 host_
->SetBounds(0, 0, 20, 20);
40 EXPECT_EQ(gfx::Rect(0, 0, 10, 20), v1
->bounds());
41 EXPECT_EQ(gfx::Rect(10, 0, 10, 20), v2
->bounds());
44 TEST_F(BoxLayoutTest
, AlignmentVertical
) {
45 BoxLayout
* layout
= new BoxLayout(BoxLayout::kVertical
, 0, 0, 0);
46 host_
->SetLayoutManager(layout
);
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);
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 BoxLayout
* layout
= new BoxLayout(BoxLayout::kHorizontal
, 10, 20, 0);
60 host_
->SetLayoutManager(layout
);
61 View
* v1
= new StaticSizedView(gfx::Size(10, 20));
62 host_
->AddChildView(v1
);
63 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
64 host_
->AddChildView(v2
);
65 EXPECT_EQ(gfx::Size(40, 60), layout
->GetPreferredSize(host_
.get()));
66 host_
->SetBounds(0, 0, 40, 60);
68 EXPECT_EQ(gfx::Rect(10, 20, 10, 20), v1
->bounds());
69 EXPECT_EQ(gfx::Rect(20, 20, 10, 20), v2
->bounds());
71 layout
->set_inside_border_insets(
72 gfx::Insets(5, 10, 15, 20));
73 EXPECT_EQ(gfx::Size(50, 40), layout
->GetPreferredSize(host_
.get()));
74 host_
->SetBounds(0, 0, 50, 40);
76 EXPECT_EQ(gfx::Rect(10, 5, 10, 20), v1
->bounds());
77 EXPECT_EQ(gfx::Rect(20, 5, 10, 20), v2
->bounds());
80 TEST_F(BoxLayoutTest
, Spacing
) {
81 BoxLayout
* layout
= new BoxLayout(BoxLayout::kHorizontal
, 7, 7, 8);
82 host_
->SetLayoutManager(layout
);
83 View
* v1
= new StaticSizedView(gfx::Size(10, 20));
84 host_
->AddChildView(v1
);
85 View
* v2
= new StaticSizedView(gfx::Size(10, 20));
86 host_
->AddChildView(v2
);
87 EXPECT_EQ(gfx::Size(42, 34), layout
->GetPreferredSize(host_
.get()));
88 host_
->SetBounds(0, 0, 100, 100);
90 EXPECT_EQ(gfx::Rect(7, 7, 10, 86), v1
->bounds());
91 EXPECT_EQ(gfx::Rect(25, 7, 10, 86), v2
->bounds());
94 TEST_F(BoxLayoutTest
, Overflow
) {
95 BoxLayout
* layout
= new BoxLayout(BoxLayout::kHorizontal
, 0, 0, 0);
96 host_
->SetLayoutManager(layout
);
97 View
* v1
= new StaticSizedView(gfx::Size(20, 20));
98 host_
->AddChildView(v1
);
99 View
* v2
= new StaticSizedView(gfx::Size(10, 20));
100 host_
->AddChildView(v2
);
101 host_
->SetBounds(0, 0, 10, 10);
103 // Overflows by positioning views at the start and truncating anything that
106 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1
->bounds());
107 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2
->bounds());
109 // All values of main axis alignment should overflow in the same manner.
110 layout
->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START
);
112 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1
->bounds());
113 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2
->bounds());
115 layout
->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER
);
117 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1
->bounds());
118 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2
->bounds());
120 layout
->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END
);
122 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1
->bounds());
123 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2
->bounds());
126 TEST_F(BoxLayoutTest
, NoSpace
) {
127 BoxLayout
* layout
= new BoxLayout(BoxLayout::kHorizontal
, 10, 10, 10);
128 host_
->SetLayoutManager(layout
);
129 View
* childView
= new StaticSizedView(gfx::Size(20, 20));
130 host_
->AddChildView(childView
);
131 host_
->SetBounds(0, 0, 10, 10);
133 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), childView
->bounds());
136 TEST_F(BoxLayoutTest
, InvisibleChild
) {
137 BoxLayout
* layout
= new BoxLayout(BoxLayout::kHorizontal
, 10, 10, 10);
138 host_
->SetLayoutManager(layout
);
139 View
* v1
= new StaticSizedView(gfx::Size(20, 20));
140 v1
->SetVisible(false);
141 host_
->AddChildView(v1
);
142 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
143 host_
->AddChildView(v2
);
144 EXPECT_EQ(gfx::Size(30, 30), layout
->GetPreferredSize(host_
.get()));
145 host_
->SetBounds(0, 0, 30, 30);
147 EXPECT_EQ(gfx::Rect(10, 10, 10, 10), v2
->bounds());
150 TEST_F(BoxLayoutTest
, UseHeightForWidth
) {
151 BoxLayout
* layout
= new BoxLayout(BoxLayout::kVertical
, 0, 0, 0);
152 host_
->SetLayoutManager(layout
);
153 View
* v1
= new StaticSizedView(gfx::Size(20, 10));
154 host_
->AddChildView(v1
);
155 ProportionallySizedView
* v2
= new ProportionallySizedView(2);
156 v2
->set_preferred_width(10);
157 host_
->AddChildView(v2
);
158 EXPECT_EQ(gfx::Size(20, 50), layout
->GetPreferredSize(host_
.get()));
160 host_
->SetBounds(0, 0, 20, 50);
162 EXPECT_EQ(gfx::Rect(0, 0, 20, 10), v1
->bounds());
163 EXPECT_EQ(gfx::Rect(0, 10, 20, 40), v2
->bounds());
165 EXPECT_EQ(110, layout
->GetPreferredHeightForWidth(host_
.get(), 50));
167 // Test without horizontal stretching of the views.
168 layout
->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END
);
169 EXPECT_EQ(gfx::Size(20, 30).ToString(),
170 layout
->GetPreferredSize(host_
.get()).ToString());
172 host_
->SetBounds(0, 0, 20, 30);
174 EXPECT_EQ(gfx::Rect(0, 0, 20, 10), v1
->bounds());
175 EXPECT_EQ(gfx::Rect(10, 10, 10, 20), v2
->bounds());
177 EXPECT_EQ(30, layout
->GetPreferredHeightForWidth(host_
.get(), 50));
180 TEST_F(BoxLayoutTest
, EmptyPreferredSize
) {
181 for (size_t i
= 0; i
< 2; i
++) {
182 BoxLayout::Orientation orientation
= i
== 0 ? BoxLayout::kHorizontal
:
183 BoxLayout::kVertical
;
184 host_
->RemoveAllChildViews(true);
185 host_
->SetLayoutManager(new BoxLayout(orientation
, 0, 0, 5));
186 View
* v1
= new StaticSizedView(gfx::Size());
187 host_
->AddChildView(v1
);
188 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
189 host_
->AddChildView(v2
);
190 host_
->SizeToPreferredSize();
193 EXPECT_EQ(v2
->GetPreferredSize().width(), host_
->bounds().width()) << i
;
194 EXPECT_EQ(v2
->GetPreferredSize().height(), host_
->bounds().height()) << i
;
195 EXPECT_EQ(v1
->GetPreferredSize().width(), v1
->bounds().width()) << i
;
196 EXPECT_EQ(v1
->GetPreferredSize().height(), v1
->bounds().height()) << i
;
197 EXPECT_EQ(v2
->GetPreferredSize().width(), v2
->bounds().width()) << i
;
198 EXPECT_EQ(v2
->GetPreferredSize().height(), v2
->bounds().height()) << i
;
202 TEST_F(BoxLayoutTest
, MainAxisAlignmentHorizontal
) {
203 BoxLayout
* layout
= new BoxLayout(BoxLayout::kHorizontal
, 10, 10, 10);
204 host_
->SetLayoutManager(layout
);
206 View
* v1
= new StaticSizedView(gfx::Size(20, 20));
207 host_
->AddChildView(v1
);
208 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
209 host_
->AddChildView(v2
);
211 host_
->SetBounds(0, 0, 100, 40);
213 // Align children to the horizontal start by default.
215 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1
->bounds().ToString());
216 EXPECT_EQ(gfx::Rect(40, 10, 10, 20).ToString(), v2
->bounds().ToString());
218 // Ensure same results for MAIN_AXIS_ALIGNMENT_START.
219 layout
->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START
);
221 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1
->bounds().ToString());
222 EXPECT_EQ(gfx::Rect(40, 10, 10, 20).ToString(), v2
->bounds().ToString());
224 // Aligns children to the center horizontally.
225 layout
->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER
);
227 EXPECT_EQ(gfx::Rect(30, 10, 20, 20).ToString(), v1
->bounds().ToString());
228 EXPECT_EQ(gfx::Rect(60, 10, 10, 20).ToString(), v2
->bounds().ToString());
230 // Aligns children to the end of the host horizontally, accounting for the
231 // inside border spacing.
232 layout
->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END
);
234 EXPECT_EQ(gfx::Rect(50, 10, 20, 20).ToString(), v1
->bounds().ToString());
235 EXPECT_EQ(gfx::Rect(80, 10, 10, 20).ToString(), v2
->bounds().ToString());
238 TEST_F(BoxLayoutTest
, MainAxisAlignmentVertical
) {
239 BoxLayout
* layout
= new BoxLayout(BoxLayout::kVertical
, 10, 10, 10);
240 host_
->SetLayoutManager(layout
);
242 View
* v1
= new StaticSizedView(gfx::Size(20, 20));
243 host_
->AddChildView(v1
);
244 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
245 host_
->AddChildView(v2
);
247 host_
->SetBounds(0, 0, 40, 100);
249 // Align children to the vertical start by default.
251 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1
->bounds().ToString());
252 EXPECT_EQ(gfx::Rect(10, 40, 20, 10).ToString(), v2
->bounds().ToString());
254 // Ensure same results for MAIN_AXIS_ALIGNMENT_START.
255 layout
->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START
);
257 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1
->bounds().ToString());
258 EXPECT_EQ(gfx::Rect(10, 40, 20, 10).ToString(), v2
->bounds().ToString());
260 // Aligns children to the center vertically.
261 layout
->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER
);
263 EXPECT_EQ(gfx::Rect(10, 30, 20, 20).ToString(), v1
->bounds().ToString());
264 EXPECT_EQ(gfx::Rect(10, 60, 20, 10).ToString(), v2
->bounds().ToString());
266 // Aligns children to the end of the host vertically, accounting for the
267 // inside border spacing.
268 layout
->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END
);
270 EXPECT_EQ(gfx::Rect(10, 50, 20, 20).ToString(), v1
->bounds().ToString());
271 EXPECT_EQ(gfx::Rect(10, 80, 20, 10).ToString(), v2
->bounds().ToString());
274 TEST_F(BoxLayoutTest
, CrossAxisAlignmentHorizontal
) {
275 BoxLayout
* layout
= new BoxLayout(BoxLayout::kHorizontal
, 10, 10, 10);
276 host_
->SetLayoutManager(layout
);
278 View
* v1
= new StaticSizedView(gfx::Size(20, 20));
279 host_
->AddChildView(v1
);
280 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
281 host_
->AddChildView(v2
);
283 host_
->SetBounds(0, 0, 100, 60);
285 // Stretch children to fill the available height by default.
287 EXPECT_EQ(gfx::Rect(10, 10, 20, 40).ToString(), v1
->bounds().ToString());
288 EXPECT_EQ(gfx::Rect(40, 10, 10, 40).ToString(), v2
->bounds().ToString());
290 // Ensure same results for CROSS_AXIS_ALIGNMENT_STRETCH.
291 layout
->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH
);
293 EXPECT_EQ(gfx::Rect(10, 10, 20, 40).ToString(), v1
->bounds().ToString());
294 EXPECT_EQ(gfx::Rect(40, 10, 10, 40).ToString(), v2
->bounds().ToString());
296 // Aligns children to the start vertically.
297 layout
->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_START
);
299 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1
->bounds().ToString());
300 EXPECT_EQ(gfx::Rect(40, 10, 10, 10).ToString(), v2
->bounds().ToString());
302 // Aligns children to the center vertically.
303 layout
->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER
);
305 EXPECT_EQ(gfx::Rect(10, 20, 20, 20).ToString(), v1
->bounds().ToString());
306 EXPECT_EQ(gfx::Rect(40, 25, 10, 10).ToString(), v2
->bounds().ToString());
308 // Aligns children to the end of the host vertically, accounting for the
309 // inside border spacing.
310 layout
->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END
);
312 EXPECT_EQ(gfx::Rect(10, 30, 20, 20).ToString(), v1
->bounds().ToString());
313 EXPECT_EQ(gfx::Rect(40, 40, 10, 10).ToString(), v2
->bounds().ToString());
316 TEST_F(BoxLayoutTest
, CrossAxisAlignmentVertical
) {
317 BoxLayout
* layout
= new BoxLayout(BoxLayout::kVertical
, 10, 10, 10);
318 host_
->SetLayoutManager(layout
);
320 View
* v1
= new StaticSizedView(gfx::Size(20, 20));
321 host_
->AddChildView(v1
);
322 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
323 host_
->AddChildView(v2
);
325 host_
->SetBounds(0, 0, 60, 100);
327 // Stretch children to fill the available width by default.
329 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1
->bounds().ToString());
330 EXPECT_EQ(gfx::Rect(10, 40, 40, 10).ToString(), v2
->bounds().ToString());
332 // Ensure same results for CROSS_AXIS_ALIGNMENT_STRETCH.
333 layout
->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH
);
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 // Aligns children to the start horizontally.
339 layout
->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_START
);
341 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1
->bounds().ToString());
342 EXPECT_EQ(gfx::Rect(10, 40, 10, 10).ToString(), v2
->bounds().ToString());
344 // Aligns children to the center horizontally.
345 layout
->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER
);
347 EXPECT_EQ(gfx::Rect(20, 10, 20, 20).ToString(), v1
->bounds().ToString());
348 EXPECT_EQ(gfx::Rect(25, 40, 10, 10).ToString(), v2
->bounds().ToString());
350 // Aligns children to the end of the host horizontally, accounting for the
351 // inside border spacing.
352 layout
->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END
);
354 EXPECT_EQ(gfx::Rect(30, 10, 20, 20).ToString(), v1
->bounds().ToString());
355 EXPECT_EQ(gfx::Rect(40, 40, 10, 10).ToString(), v2
->bounds().ToString());
358 TEST_F(BoxLayoutTest
, FlexAll
) {
359 BoxLayout
* layout
= new BoxLayout(BoxLayout::kHorizontal
, 10, 10, 10);
360 host_
->SetLayoutManager(layout
);
361 layout
->SetDefaultFlex(1);
363 View
* v1
= new StaticSizedView(gfx::Size(20, 20));
364 host_
->AddChildView(v1
);
365 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
366 host_
->AddChildView(v2
);
367 View
* v3
= new StaticSizedView(gfx::Size(30, 30));
368 host_
->AddChildView(v3
);
369 EXPECT_EQ(gfx::Size(100, 50), layout
->GetPreferredSize(host_
.get()));
371 host_
->SetBounds(0, 0, 120, 50);
373 EXPECT_EQ(gfx::Rect(10, 10, 27, 30).ToString(), v1
->bounds().ToString());
374 EXPECT_EQ(gfx::Rect(47, 10, 16, 30).ToString(), v2
->bounds().ToString());
375 EXPECT_EQ(gfx::Rect(73, 10, 37, 30).ToString(), v3
->bounds().ToString());
378 TEST_F(BoxLayoutTest
, FlexGrowVertical
) {
379 BoxLayout
* layout
= new BoxLayout(BoxLayout::kVertical
, 10, 10, 10);
380 host_
->SetLayoutManager(layout
);
382 View
* v1
= new StaticSizedView(gfx::Size(20, 20));
383 host_
->AddChildView(v1
);
384 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
385 host_
->AddChildView(v2
);
386 View
* v3
= new StaticSizedView(gfx::Size(30, 30));
387 host_
->AddChildView(v3
);
389 host_
->SetBounds(0, 0, 50, 130);
391 // Views don't fill the available space by default.
393 EXPECT_EQ(gfx::Rect(10, 10, 30, 20).ToString(), v1
->bounds().ToString());
394 EXPECT_EQ(gfx::Rect(10, 40, 30, 10).ToString(), v2
->bounds().ToString());
395 EXPECT_EQ(gfx::Rect(10, 60, 30, 30).ToString(), v3
->bounds().ToString());
397 std::vector
<BoxLayout::MainAxisAlignment
> main_alignments
;
398 main_alignments
.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START
);
399 main_alignments
.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER
);
400 main_alignments
.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END
);
402 for (size_t i
= 0; i
< main_alignments
.size(); ++i
) {
403 layout
->set_main_axis_alignment(main_alignments
[i
]);
405 // Set the first view to consume all free space.
406 layout
->SetFlexForView(v1
, 1);
407 layout
->ClearFlexForView(v2
);
408 layout
->ClearFlexForView(v3
);
410 EXPECT_EQ(gfx::Rect(10, 10, 30, 50).ToString(), v1
->bounds().ToString());
411 EXPECT_EQ(gfx::Rect(10, 70, 30, 10).ToString(), v2
->bounds().ToString());
412 EXPECT_EQ(gfx::Rect(10, 90, 30, 30).ToString(), v3
->bounds().ToString());
414 // Set the third view to take 2/3s of the free space and leave the first
417 layout
->SetFlexForView(v3
, 2);
419 EXPECT_EQ(gfx::Rect(10, 10, 30, 30).ToString(), v1
->bounds().ToString());
420 EXPECT_EQ(gfx::Rect(10, 50, 30, 10).ToString(), v2
->bounds().ToString());
421 EXPECT_EQ(gfx::Rect(10, 70, 30, 50).ToString(), v3
->bounds().ToString());
423 // Clear the previously set flex values and set the second view to take all
425 layout
->ClearFlexForView(v1
);
426 layout
->SetFlexForView(v2
, 1);
427 layout
->ClearFlexForView(v3
);
429 EXPECT_EQ(gfx::Rect(10, 10, 30, 20).ToString(), v1
->bounds().ToString());
430 EXPECT_EQ(gfx::Rect(10, 40, 30, 40).ToString(), v2
->bounds().ToString());
431 EXPECT_EQ(gfx::Rect(10, 90, 30, 30).ToString(), v3
->bounds().ToString());
435 TEST_F(BoxLayoutTest
, FlexGrowHorizontalWithRemainder
) {
436 BoxLayout
* layout
= new BoxLayout(BoxLayout::kHorizontal
, 0, 0, 0);
437 host_
->SetLayoutManager(layout
);
438 layout
->SetDefaultFlex(1);
439 std::vector
<View
*> views
;
440 for (int i
= 0; i
< 5; ++i
) {
441 View
* view
= new StaticSizedView(gfx::Size(10, 10));
442 views
.push_back(view
);
443 host_
->AddChildView(view
);
446 EXPECT_EQ(gfx::Size(50, 10), layout
->GetPreferredSize(host_
.get()));
448 host_
->SetBounds(0, 0, 52, 10);
450 // The 2nd and 4th views should have an extra pixel as they correspond to 20.8
451 // and 41.6 which round up.
452 EXPECT_EQ(gfx::Rect(0, 0, 10, 10).ToString(), views
[0]->bounds().ToString());
453 EXPECT_EQ(gfx::Rect(10, 0, 11, 10).ToString(), views
[1]->bounds().ToString());
454 EXPECT_EQ(gfx::Rect(21, 0, 10, 10).ToString(), views
[2]->bounds().ToString());
455 EXPECT_EQ(gfx::Rect(31, 0, 11, 10).ToString(), views
[3]->bounds().ToString());
456 EXPECT_EQ(gfx::Rect(42, 0, 10, 10).ToString(), views
[4]->bounds().ToString());
459 TEST_F(BoxLayoutTest
, FlexGrowHorizontalWithRemainder2
) {
460 BoxLayout
* layout
= new BoxLayout(BoxLayout::kHorizontal
, 0, 0, 0);
461 host_
->SetLayoutManager(layout
);
462 layout
->SetDefaultFlex(1);
463 std::vector
<View
*> views
;
464 for (int i
= 0; i
< 4; ++i
) {
465 View
* view
= new StaticSizedView(gfx::Size(1, 10));
466 views
.push_back(view
);
467 host_
->AddChildView(view
);
470 EXPECT_EQ(gfx::Size(4, 10), layout
->GetPreferredSize(host_
.get()));
472 host_
->SetBounds(0, 0, 10, 10);
474 // The 1st and 3rd views should have an extra pixel as they correspond to 2.5
475 // and 7.5 which round up.
476 EXPECT_EQ(gfx::Rect(0, 0, 3, 10).ToString(), views
[0]->bounds().ToString());
477 EXPECT_EQ(gfx::Rect(3, 0, 2, 10).ToString(), views
[1]->bounds().ToString());
478 EXPECT_EQ(gfx::Rect(5, 0, 3, 10).ToString(), views
[2]->bounds().ToString());
479 EXPECT_EQ(gfx::Rect(8, 0, 2, 10).ToString(), views
[3]->bounds().ToString());
482 TEST_F(BoxLayoutTest
, FlexShrinkHorizontal
) {
483 BoxLayout
* layout
= new BoxLayout(BoxLayout::kHorizontal
, 10, 10, 10);
484 host_
->SetLayoutManager(layout
);
486 View
* v1
= new StaticSizedView(gfx::Size(20, 20));
487 host_
->AddChildView(v1
);
488 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
489 host_
->AddChildView(v2
);
490 View
* v3
= new StaticSizedView(gfx::Size(30, 30));
491 host_
->AddChildView(v3
);
493 host_
->SetBounds(0, 0, 85, 50);
495 // Truncate width by default.
497 EXPECT_EQ(gfx::Rect(10, 10, 20, 30).ToString(), v1
->bounds().ToString());
498 EXPECT_EQ(gfx::Rect(40, 10, 10, 30).ToString(), v2
->bounds().ToString());
499 EXPECT_EQ(gfx::Rect(60, 10, 15, 30).ToString(), v3
->bounds().ToString());
501 std::vector
<BoxLayout::MainAxisAlignment
> main_alignments
;
502 main_alignments
.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START
);
503 main_alignments
.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER
);
504 main_alignments
.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END
);
506 for (size_t i
= 0; i
< main_alignments
.size(); ++i
) {
507 layout
->set_main_axis_alignment(main_alignments
[i
]);
509 // Set the first view to shrink as much as necessary.
510 layout
->SetFlexForView(v1
, 1);
511 layout
->ClearFlexForView(v2
);
512 layout
->ClearFlexForView(v3
);
514 EXPECT_EQ(gfx::Rect(10, 10, 5, 30).ToString(), v1
->bounds().ToString());
515 EXPECT_EQ(gfx::Rect(25, 10, 10, 30).ToString(), v2
->bounds().ToString());
516 EXPECT_EQ(gfx::Rect(45, 10, 30, 30).ToString(), v3
->bounds().ToString());
518 // Set the third view to shrink 2/3s of the free space and leave the first
520 layout
->SetFlexForView(v3
, 2);
522 EXPECT_EQ(gfx::Rect(10, 10, 15, 30).ToString(), v1
->bounds().ToString());
523 EXPECT_EQ(gfx::Rect(35, 10, 10, 30).ToString(), v2
->bounds().ToString());
524 EXPECT_EQ(gfx::Rect(55, 10, 20, 30).ToString(), v3
->bounds().ToString());
526 // Clear the previously set flex values and set the second view to take all
527 // the free space with MAIN_AXIS_ALIGNMENT_END set. This causes the second
528 // view to shrink to zero and the third view still doesn't fit so it
530 layout
->ClearFlexForView(v1
);
531 layout
->SetFlexForView(v2
, 2);
532 layout
->ClearFlexForView(v3
);
534 EXPECT_EQ(gfx::Rect(10, 10, 20, 30).ToString(), v1
->bounds().ToString());
535 // Conceptually this view is at 10, 40, 0, 0.
536 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(), v2
->bounds().ToString());
537 EXPECT_EQ(gfx::Rect(50, 10, 25, 30).ToString(), v3
->bounds().ToString());
541 TEST_F(BoxLayoutTest
, FlexShrinkVerticalWithRemainder
) {
542 BoxLayout
* layout
= new BoxLayout(BoxLayout::kVertical
, 0, 0, 0);
543 host_
->SetLayoutManager(layout
);
544 View
* v1
= new StaticSizedView(gfx::Size(20, 10));
545 host_
->AddChildView(v1
);
546 View
* v2
= new StaticSizedView(gfx::Size(20, 20));
547 host_
->AddChildView(v2
);
548 View
* v3
= new StaticSizedView(gfx::Size(20, 10));
549 host_
->AddChildView(v3
);
550 host_
->SetBounds(0, 0, 20, 20);
552 std::vector
<BoxLayout::MainAxisAlignment
> main_alignments
;
553 main_alignments
.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START
);
554 main_alignments
.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER
);
555 main_alignments
.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END
);
557 for (size_t i
= 0; i
< main_alignments
.size(); ++i
) {
558 layout
->set_main_axis_alignment(main_alignments
[i
]);
560 // The first view shrinks by 1/3 of the excess, the second view shrinks by
561 // 2/3 of the excess and the third view should maintain its preferred size.
562 layout
->SetFlexForView(v1
, 1);
563 layout
->SetFlexForView(v2
, 2);
564 layout
->ClearFlexForView(v3
);
566 EXPECT_EQ(gfx::Rect(0, 0, 20, 3).ToString(), v1
->bounds().ToString());
567 EXPECT_EQ(gfx::Rect(0, 3, 20, 7).ToString(), v2
->bounds().ToString());
568 EXPECT_EQ(gfx::Rect(0, 10, 20, 10).ToString(), v3
->bounds().ToString());
570 // The second view shrinks to 2/3 of the excess, the third view shrinks to
571 // 1/3 of the excess and the first view should maintain its preferred size.
572 layout
->ClearFlexForView(v1
);
573 layout
->SetFlexForView(v2
, 2);
574 layout
->SetFlexForView(v3
, 1);
576 EXPECT_EQ(gfx::Rect(0, 0, 20, 10).ToString(), v1
->bounds().ToString());
577 EXPECT_EQ(gfx::Rect(0, 10, 20, 7).ToString(), v2
->bounds().ToString());
578 EXPECT_EQ(gfx::Rect(0, 17, 20, 3).ToString(), v3
->bounds().ToString());
580 // Each view shrinks equally to fit within the available space.
581 layout
->SetFlexForView(v1
, 1);
582 layout
->SetFlexForView(v2
, 1);
583 layout
->SetFlexForView(v3
, 1);
585 EXPECT_EQ(gfx::Rect(0, 0, 20, 3).ToString(), v1
->bounds().ToString());
586 EXPECT_EQ(gfx::Rect(0, 3, 20, 14).ToString(), v2
->bounds().ToString());
587 EXPECT_EQ(gfx::Rect(0, 17, 20, 3).ToString(), v3
->bounds().ToString());
591 TEST_F(BoxLayoutTest
, MinimumCrossAxisVertical
) {
592 BoxLayout
* layout
= new BoxLayout(BoxLayout::kVertical
, 0, 0, 0);
593 host_
->SetLayoutManager(layout
);
594 View
* v1
= new StaticSizedView(gfx::Size(20, 10));
595 host_
->AddChildView(v1
);
596 layout
->set_minimum_cross_axis_size(30);
598 EXPECT_EQ(gfx::Size(30, 10), layout
->GetPreferredSize(host_
.get()));
601 TEST_F(BoxLayoutTest
, MinimumCrossAxisHorizontal
) {
602 BoxLayout
* layout
= new BoxLayout(BoxLayout::kHorizontal
, 0, 0, 0);
603 host_
->SetLayoutManager(layout
);
604 View
* v1
= new StaticSizedView(gfx::Size(20, 10));
605 host_
->AddChildView(v1
);
606 layout
->set_minimum_cross_axis_size(30);
608 EXPECT_EQ(gfx::Size(20, 30), layout
->GetPreferredSize(host_
.get()));