Update CrOS OOBE throbber to MD throbber; delete old asset
[chromium-blink-merge.git] / ui / views / layout / box_layout_unittest.cc
blob59866b14d1034658a182963e4aaf351094b315a3
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"
11 namespace views {
13 namespace {
15 class BoxLayoutTest : public testing::Test {
16 public:
17 void SetUp() override { host_.reset(new View); }
19 scoped_ptr<View> host_;
22 } // namespace
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);
39 host_->Layout();
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);
53 host_->Layout();
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);
67 host_->Layout();
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);
75 host_->Layout();
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);
89 host_->Layout();
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
104 // doesn't fit.
105 host_->Layout();
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);
111 host_->Layout();
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);
116 host_->Layout();
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);
121 host_->Layout();
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);
132 host_->Layout();
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);
146 host_->Layout();
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);
161 host_->Layout();
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);
173 host_->Layout();
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();
191 host_->Layout();
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 // Verifies that a BoxLayout correctly handles child spacing, flex layout, and
203 // empty preferred size, simultaneously.
204 TEST_F(BoxLayoutTest, EmptyPreferredSizeWithFlexLayoutAndChildSpacing) {
205 host_->RemoveAllChildViews(true);
206 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 5);
207 host_->SetLayoutManager(layout);
208 View* v1 = new StaticSizedView(gfx::Size());
209 host_->AddChildView(v1);
210 View* v2 = new StaticSizedView(gfx::Size(10, 10));
211 host_->AddChildView(v2);
212 View* v3 = new StaticSizedView(gfx::Size(1, 1));
213 host_->AddChildViewAt(v3, 0);
214 layout->SetFlexForView(v3, 1);
215 host_->SetSize(gfx::Size(1000, 15));
216 host_->Layout();
218 EXPECT_EQ(host_->bounds().right(), v2->bounds().right());
221 TEST_F(BoxLayoutTest, MainAxisAlignmentHorizontal) {
222 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10);
223 host_->SetLayoutManager(layout);
225 View* v1 = new StaticSizedView(gfx::Size(20, 20));
226 host_->AddChildView(v1);
227 View* v2 = new StaticSizedView(gfx::Size(10, 10));
228 host_->AddChildView(v2);
230 host_->SetBounds(0, 0, 100, 40);
232 // Align children to the horizontal start by default.
233 host_->Layout();
234 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString());
235 EXPECT_EQ(gfx::Rect(40, 10, 10, 20).ToString(), v2->bounds().ToString());
237 // Ensure same results for MAIN_AXIS_ALIGNMENT_START.
238 layout->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START);
239 host_->Layout();
240 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString());
241 EXPECT_EQ(gfx::Rect(40, 10, 10, 20).ToString(), v2->bounds().ToString());
243 // Aligns children to the center horizontally.
244 layout->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
245 host_->Layout();
246 EXPECT_EQ(gfx::Rect(30, 10, 20, 20).ToString(), v1->bounds().ToString());
247 EXPECT_EQ(gfx::Rect(60, 10, 10, 20).ToString(), v2->bounds().ToString());
249 // Aligns children to the end of the host horizontally, accounting for the
250 // inside border spacing.
251 layout->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END);
252 host_->Layout();
253 EXPECT_EQ(gfx::Rect(50, 10, 20, 20).ToString(), v1->bounds().ToString());
254 EXPECT_EQ(gfx::Rect(80, 10, 10, 20).ToString(), v2->bounds().ToString());
257 TEST_F(BoxLayoutTest, MainAxisAlignmentVertical) {
258 BoxLayout* layout = new BoxLayout(BoxLayout::kVertical, 10, 10, 10);
259 host_->SetLayoutManager(layout);
261 View* v1 = new StaticSizedView(gfx::Size(20, 20));
262 host_->AddChildView(v1);
263 View* v2 = new StaticSizedView(gfx::Size(10, 10));
264 host_->AddChildView(v2);
266 host_->SetBounds(0, 0, 40, 100);
268 // Align children to the vertical start by default.
269 host_->Layout();
270 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString());
271 EXPECT_EQ(gfx::Rect(10, 40, 20, 10).ToString(), v2->bounds().ToString());
273 // Ensure same results for MAIN_AXIS_ALIGNMENT_START.
274 layout->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START);
275 host_->Layout();
276 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString());
277 EXPECT_EQ(gfx::Rect(10, 40, 20, 10).ToString(), v2->bounds().ToString());
279 // Aligns children to the center vertically.
280 layout->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
281 host_->Layout();
282 EXPECT_EQ(gfx::Rect(10, 30, 20, 20).ToString(), v1->bounds().ToString());
283 EXPECT_EQ(gfx::Rect(10, 60, 20, 10).ToString(), v2->bounds().ToString());
285 // Aligns children to the end of the host vertically, accounting for the
286 // inside border spacing.
287 layout->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END);
288 host_->Layout();
289 EXPECT_EQ(gfx::Rect(10, 50, 20, 20).ToString(), v1->bounds().ToString());
290 EXPECT_EQ(gfx::Rect(10, 80, 20, 10).ToString(), v2->bounds().ToString());
293 TEST_F(BoxLayoutTest, CrossAxisAlignmentHorizontal) {
294 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10);
295 host_->SetLayoutManager(layout);
297 View* v1 = new StaticSizedView(gfx::Size(20, 20));
298 host_->AddChildView(v1);
299 View* v2 = new StaticSizedView(gfx::Size(10, 10));
300 host_->AddChildView(v2);
302 host_->SetBounds(0, 0, 100, 60);
304 // Stretch children to fill the available height by default.
305 host_->Layout();
306 EXPECT_EQ(gfx::Rect(10, 10, 20, 40).ToString(), v1->bounds().ToString());
307 EXPECT_EQ(gfx::Rect(40, 10, 10, 40).ToString(), v2->bounds().ToString());
309 // Ensure same results for CROSS_AXIS_ALIGNMENT_STRETCH.
310 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH);
311 host_->Layout();
312 EXPECT_EQ(gfx::Rect(10, 10, 20, 40).ToString(), v1->bounds().ToString());
313 EXPECT_EQ(gfx::Rect(40, 10, 10, 40).ToString(), v2->bounds().ToString());
315 // Aligns children to the start vertically.
316 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_START);
317 host_->Layout();
318 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString());
319 EXPECT_EQ(gfx::Rect(40, 10, 10, 10).ToString(), v2->bounds().ToString());
321 // Aligns children to the center vertically.
322 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
323 host_->Layout();
324 EXPECT_EQ(gfx::Rect(10, 20, 20, 20).ToString(), v1->bounds().ToString());
325 EXPECT_EQ(gfx::Rect(40, 25, 10, 10).ToString(), v2->bounds().ToString());
327 // Aligns children to the end of the host vertically, accounting for the
328 // inside border spacing.
329 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END);
330 host_->Layout();
331 EXPECT_EQ(gfx::Rect(10, 30, 20, 20).ToString(), v1->bounds().ToString());
332 EXPECT_EQ(gfx::Rect(40, 40, 10, 10).ToString(), v2->bounds().ToString());
335 TEST_F(BoxLayoutTest, CrossAxisAlignmentVertical) {
336 BoxLayout* layout = new BoxLayout(BoxLayout::kVertical, 10, 10, 10);
337 host_->SetLayoutManager(layout);
339 View* v1 = new StaticSizedView(gfx::Size(20, 20));
340 host_->AddChildView(v1);
341 View* v2 = new StaticSizedView(gfx::Size(10, 10));
342 host_->AddChildView(v2);
344 host_->SetBounds(0, 0, 60, 100);
346 // Stretch children to fill the available width by default.
347 host_->Layout();
348 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1->bounds().ToString());
349 EXPECT_EQ(gfx::Rect(10, 40, 40, 10).ToString(), v2->bounds().ToString());
351 // Ensure same results for CROSS_AXIS_ALIGNMENT_STRETCH.
352 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH);
353 host_->Layout();
354 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1->bounds().ToString());
355 EXPECT_EQ(gfx::Rect(10, 40, 40, 10).ToString(), v2->bounds().ToString());
357 // Aligns children to the start horizontally.
358 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_START);
359 host_->Layout();
360 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString());
361 EXPECT_EQ(gfx::Rect(10, 40, 10, 10).ToString(), v2->bounds().ToString());
363 // Aligns children to the center horizontally.
364 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
365 host_->Layout();
366 EXPECT_EQ(gfx::Rect(20, 10, 20, 20).ToString(), v1->bounds().ToString());
367 EXPECT_EQ(gfx::Rect(25, 40, 10, 10).ToString(), v2->bounds().ToString());
369 // Aligns children to the end of the host horizontally, accounting for the
370 // inside border spacing.
371 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END);
372 host_->Layout();
373 EXPECT_EQ(gfx::Rect(30, 10, 20, 20).ToString(), v1->bounds().ToString());
374 EXPECT_EQ(gfx::Rect(40, 40, 10, 10).ToString(), v2->bounds().ToString());
377 TEST_F(BoxLayoutTest, FlexAll) {
378 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10);
379 host_->SetLayoutManager(layout);
380 layout->SetDefaultFlex(1);
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);
388 EXPECT_EQ(gfx::Size(100, 50), layout->GetPreferredSize(host_.get()));
390 host_->SetBounds(0, 0, 120, 50);
391 host_->Layout();
392 EXPECT_EQ(gfx::Rect(10, 10, 27, 30).ToString(), v1->bounds().ToString());
393 EXPECT_EQ(gfx::Rect(47, 10, 16, 30).ToString(), v2->bounds().ToString());
394 EXPECT_EQ(gfx::Rect(73, 10, 37, 30).ToString(), v3->bounds().ToString());
397 TEST_F(BoxLayoutTest, FlexGrowVertical) {
398 BoxLayout* layout = new BoxLayout(BoxLayout::kVertical, 10, 10, 10);
399 host_->SetLayoutManager(layout);
401 View* v1 = new StaticSizedView(gfx::Size(20, 20));
402 host_->AddChildView(v1);
403 View* v2 = new StaticSizedView(gfx::Size(10, 10));
404 host_->AddChildView(v2);
405 View* v3 = new StaticSizedView(gfx::Size(30, 30));
406 host_->AddChildView(v3);
408 host_->SetBounds(0, 0, 50, 130);
410 // Views don't fill the available space by default.
411 host_->Layout();
412 EXPECT_EQ(gfx::Rect(10, 10, 30, 20).ToString(), v1->bounds().ToString());
413 EXPECT_EQ(gfx::Rect(10, 40, 30, 10).ToString(), v2->bounds().ToString());
414 EXPECT_EQ(gfx::Rect(10, 60, 30, 30).ToString(), v3->bounds().ToString());
416 std::vector<BoxLayout::MainAxisAlignment> main_alignments;
417 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START);
418 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
419 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END);
421 for (size_t i = 0; i < main_alignments.size(); ++i) {
422 layout->set_main_axis_alignment(main_alignments[i]);
424 // Set the first view to consume all free space.
425 layout->SetFlexForView(v1, 1);
426 layout->ClearFlexForView(v2);
427 layout->ClearFlexForView(v3);
428 host_->Layout();
429 EXPECT_EQ(gfx::Rect(10, 10, 30, 50).ToString(), v1->bounds().ToString());
430 EXPECT_EQ(gfx::Rect(10, 70, 30, 10).ToString(), v2->bounds().ToString());
431 EXPECT_EQ(gfx::Rect(10, 90, 30, 30).ToString(), v3->bounds().ToString());
433 // Set the third view to take 2/3s of the free space and leave the first
434 // view
435 // with 1/3.
436 layout->SetFlexForView(v3, 2);
437 host_->Layout();
438 EXPECT_EQ(gfx::Rect(10, 10, 30, 30).ToString(), v1->bounds().ToString());
439 EXPECT_EQ(gfx::Rect(10, 50, 30, 10).ToString(), v2->bounds().ToString());
440 EXPECT_EQ(gfx::Rect(10, 70, 30, 50).ToString(), v3->bounds().ToString());
442 // Clear the previously set flex values and set the second view to take all
443 // the free space.
444 layout->ClearFlexForView(v1);
445 layout->SetFlexForView(v2, 1);
446 layout->ClearFlexForView(v3);
447 host_->Layout();
448 EXPECT_EQ(gfx::Rect(10, 10, 30, 20).ToString(), v1->bounds().ToString());
449 EXPECT_EQ(gfx::Rect(10, 40, 30, 40).ToString(), v2->bounds().ToString());
450 EXPECT_EQ(gfx::Rect(10, 90, 30, 30).ToString(), v3->bounds().ToString());
454 TEST_F(BoxLayoutTest, FlexGrowHorizontalWithRemainder) {
455 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0);
456 host_->SetLayoutManager(layout);
457 layout->SetDefaultFlex(1);
458 std::vector<View*> views;
459 for (int i = 0; i < 5; ++i) {
460 View* view = new StaticSizedView(gfx::Size(10, 10));
461 views.push_back(view);
462 host_->AddChildView(view);
465 EXPECT_EQ(gfx::Size(50, 10), layout->GetPreferredSize(host_.get()));
467 host_->SetBounds(0, 0, 52, 10);
468 host_->Layout();
469 // The 2nd and 4th views should have an extra pixel as they correspond to 20.8
470 // and 41.6 which round up.
471 EXPECT_EQ(gfx::Rect(0, 0, 10, 10).ToString(), views[0]->bounds().ToString());
472 EXPECT_EQ(gfx::Rect(10, 0, 11, 10).ToString(), views[1]->bounds().ToString());
473 EXPECT_EQ(gfx::Rect(21, 0, 10, 10).ToString(), views[2]->bounds().ToString());
474 EXPECT_EQ(gfx::Rect(31, 0, 11, 10).ToString(), views[3]->bounds().ToString());
475 EXPECT_EQ(gfx::Rect(42, 0, 10, 10).ToString(), views[4]->bounds().ToString());
478 TEST_F(BoxLayoutTest, FlexGrowHorizontalWithRemainder2) {
479 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0);
480 host_->SetLayoutManager(layout);
481 layout->SetDefaultFlex(1);
482 std::vector<View*> views;
483 for (int i = 0; i < 4; ++i) {
484 View* view = new StaticSizedView(gfx::Size(1, 10));
485 views.push_back(view);
486 host_->AddChildView(view);
489 EXPECT_EQ(gfx::Size(4, 10), layout->GetPreferredSize(host_.get()));
491 host_->SetBounds(0, 0, 10, 10);
492 host_->Layout();
493 // The 1st and 3rd views should have an extra pixel as they correspond to 2.5
494 // and 7.5 which round up.
495 EXPECT_EQ(gfx::Rect(0, 0, 3, 10).ToString(), views[0]->bounds().ToString());
496 EXPECT_EQ(gfx::Rect(3, 0, 2, 10).ToString(), views[1]->bounds().ToString());
497 EXPECT_EQ(gfx::Rect(5, 0, 3, 10).ToString(), views[2]->bounds().ToString());
498 EXPECT_EQ(gfx::Rect(8, 0, 2, 10).ToString(), views[3]->bounds().ToString());
501 TEST_F(BoxLayoutTest, FlexShrinkHorizontal) {
502 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10);
503 host_->SetLayoutManager(layout);
505 View* v1 = new StaticSizedView(gfx::Size(20, 20));
506 host_->AddChildView(v1);
507 View* v2 = new StaticSizedView(gfx::Size(10, 10));
508 host_->AddChildView(v2);
509 View* v3 = new StaticSizedView(gfx::Size(30, 30));
510 host_->AddChildView(v3);
512 host_->SetBounds(0, 0, 85, 50);
514 // Truncate width by default.
515 host_->Layout();
516 EXPECT_EQ(gfx::Rect(10, 10, 20, 30).ToString(), v1->bounds().ToString());
517 EXPECT_EQ(gfx::Rect(40, 10, 10, 30).ToString(), v2->bounds().ToString());
518 EXPECT_EQ(gfx::Rect(60, 10, 15, 30).ToString(), v3->bounds().ToString());
520 std::vector<BoxLayout::MainAxisAlignment> main_alignments;
521 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START);
522 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
523 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END);
525 for (size_t i = 0; i < main_alignments.size(); ++i) {
526 layout->set_main_axis_alignment(main_alignments[i]);
528 // Set the first view to shrink as much as necessary.
529 layout->SetFlexForView(v1, 1);
530 layout->ClearFlexForView(v2);
531 layout->ClearFlexForView(v3);
532 host_->Layout();
533 EXPECT_EQ(gfx::Rect(10, 10, 5, 30).ToString(), v1->bounds().ToString());
534 EXPECT_EQ(gfx::Rect(25, 10, 10, 30).ToString(), v2->bounds().ToString());
535 EXPECT_EQ(gfx::Rect(45, 10, 30, 30).ToString(), v3->bounds().ToString());
537 // Set the third view to shrink 2/3s of the free space and leave the first
538 // view with 1/3.
539 layout->SetFlexForView(v3, 2);
540 host_->Layout();
541 EXPECT_EQ(gfx::Rect(10, 10, 15, 30).ToString(), v1->bounds().ToString());
542 EXPECT_EQ(gfx::Rect(35, 10, 10, 30).ToString(), v2->bounds().ToString());
543 EXPECT_EQ(gfx::Rect(55, 10, 20, 30).ToString(), v3->bounds().ToString());
545 // Clear the previously set flex values and set the second view to take all
546 // the free space with MAIN_AXIS_ALIGNMENT_END set. This causes the second
547 // view to shrink to zero and the third view still doesn't fit so it
548 // overflows.
549 layout->ClearFlexForView(v1);
550 layout->SetFlexForView(v2, 2);
551 layout->ClearFlexForView(v3);
552 host_->Layout();
553 EXPECT_EQ(gfx::Rect(10, 10, 20, 30).ToString(), v1->bounds().ToString());
554 // Conceptually this view is at 10, 40, 0, 0.
555 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(), v2->bounds().ToString());
556 EXPECT_EQ(gfx::Rect(50, 10, 25, 30).ToString(), v3->bounds().ToString());
560 TEST_F(BoxLayoutTest, FlexShrinkVerticalWithRemainder) {
561 BoxLayout* layout = new BoxLayout(BoxLayout::kVertical, 0, 0, 0);
562 host_->SetLayoutManager(layout);
563 View* v1 = new StaticSizedView(gfx::Size(20, 10));
564 host_->AddChildView(v1);
565 View* v2 = new StaticSizedView(gfx::Size(20, 20));
566 host_->AddChildView(v2);
567 View* v3 = new StaticSizedView(gfx::Size(20, 10));
568 host_->AddChildView(v3);
569 host_->SetBounds(0, 0, 20, 20);
571 std::vector<BoxLayout::MainAxisAlignment> main_alignments;
572 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START);
573 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
574 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END);
576 for (size_t i = 0; i < main_alignments.size(); ++i) {
577 layout->set_main_axis_alignment(main_alignments[i]);
579 // The first view shrinks by 1/3 of the excess, the second view shrinks by
580 // 2/3 of the excess and the third view should maintain its preferred size.
581 layout->SetFlexForView(v1, 1);
582 layout->SetFlexForView(v2, 2);
583 layout->ClearFlexForView(v3);
584 host_->Layout();
585 EXPECT_EQ(gfx::Rect(0, 0, 20, 3).ToString(), v1->bounds().ToString());
586 EXPECT_EQ(gfx::Rect(0, 3, 20, 7).ToString(), v2->bounds().ToString());
587 EXPECT_EQ(gfx::Rect(0, 10, 20, 10).ToString(), v3->bounds().ToString());
589 // The second view shrinks to 2/3 of the excess, the third view shrinks to
590 // 1/3 of the excess and the first view should maintain its preferred size.
591 layout->ClearFlexForView(v1);
592 layout->SetFlexForView(v2, 2);
593 layout->SetFlexForView(v3, 1);
594 host_->Layout();
595 EXPECT_EQ(gfx::Rect(0, 0, 20, 10).ToString(), v1->bounds().ToString());
596 EXPECT_EQ(gfx::Rect(0, 10, 20, 7).ToString(), v2->bounds().ToString());
597 EXPECT_EQ(gfx::Rect(0, 17, 20, 3).ToString(), v3->bounds().ToString());
599 // Each view shrinks equally to fit within the available space.
600 layout->SetFlexForView(v1, 1);
601 layout->SetFlexForView(v2, 1);
602 layout->SetFlexForView(v3, 1);
603 host_->Layout();
604 EXPECT_EQ(gfx::Rect(0, 0, 20, 3).ToString(), v1->bounds().ToString());
605 EXPECT_EQ(gfx::Rect(0, 3, 20, 14).ToString(), v2->bounds().ToString());
606 EXPECT_EQ(gfx::Rect(0, 17, 20, 3).ToString(), v3->bounds().ToString());
610 TEST_F(BoxLayoutTest, MinimumCrossAxisVertical) {
611 BoxLayout* layout = new BoxLayout(BoxLayout::kVertical, 0, 0, 0);
612 host_->SetLayoutManager(layout);
613 View* v1 = new StaticSizedView(gfx::Size(20, 10));
614 host_->AddChildView(v1);
615 layout->set_minimum_cross_axis_size(30);
617 EXPECT_EQ(gfx::Size(30, 10), layout->GetPreferredSize(host_.get()));
620 TEST_F(BoxLayoutTest, MinimumCrossAxisHorizontal) {
621 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0);
622 host_->SetLayoutManager(layout);
623 View* v1 = new StaticSizedView(gfx::Size(20, 10));
624 host_->AddChildView(v1);
625 layout->set_minimum_cross_axis_size(30);
627 EXPECT_EQ(gfx::Size(20, 30), layout->GetPreferredSize(host_.get()));
630 } // namespace views