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

Side by Side Diff: ui/views/layout/box_layout_unittest.cc

Issue 360213002: Add Flex to views::BoxLayout. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months 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
« ui/views/layout/box_layout.h ('K') | « ui/views/layout/box_layout.cc ('k') | no next file » | 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) 2012 The Chromium Authors. All rights reserved. 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 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/views/layout/box_layout.h" 5 #include "ui/views/layout/box_layout.h"
6 6
7 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/views/test/test_views.h" 8 #include "ui/views/test/test_views.h"
9 #include "ui/views/view.h" 9 #include "ui/views/view.h"
10 10
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 v1->SetVisible(false); 135 v1->SetVisible(false);
136 host_->AddChildView(v1); 136 host_->AddChildView(v1);
137 View* v2 = new StaticSizedView(gfx::Size(10, 10)); 137 View* v2 = new StaticSizedView(gfx::Size(10, 10));
138 host_->AddChildView(v2); 138 host_->AddChildView(v2);
139 EXPECT_EQ(gfx::Size(30, 30), layout_->GetPreferredSize(host_.get())); 139 EXPECT_EQ(gfx::Size(30, 30), layout_->GetPreferredSize(host_.get()));
140 host_->SetBounds(0, 0, 30, 30); 140 host_->SetBounds(0, 0, 30, 30);
141 layout_->Layout(host_.get()); 141 layout_->Layout(host_.get());
142 EXPECT_EQ(gfx::Rect(10, 10, 10, 10), v2->bounds()); 142 EXPECT_EQ(gfx::Rect(10, 10, 10, 10), v2->bounds());
143 } 143 }
144 144
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);
148
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()));
154
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());
159 }
160
161 TEST_F(BoxLayoutTest, UseHeightForWidth) { 145 TEST_F(BoxLayoutTest, UseHeightForWidth) {
162 layout_.reset(new BoxLayout(BoxLayout::kVertical, 0, 0, 0)); 146 layout_.reset(new BoxLayout(BoxLayout::kVertical, 0, 0, 0));
163 View* v1 = new StaticSizedView(gfx::Size(20, 10)); 147 View* v1 = new StaticSizedView(gfx::Size(20, 10));
164 host_->AddChildView(v1); 148 host_->AddChildView(v1);
165 ProportionallySizedView* v2 = new ProportionallySizedView(2); 149 ProportionallySizedView* v2 = new ProportionallySizedView(2);
166 v2->set_preferred_width(10); 150 v2->set_preferred_width(10);
167 host_->AddChildView(v2); 151 host_->AddChildView(v2);
168 EXPECT_EQ(gfx::Size(20, 50), layout_->GetPreferredSize(host_.get())); 152 EXPECT_EQ(gfx::Size(20, 50), layout_->GetPreferredSize(host_.get()));
169 153
170 host_->SetBounds(0, 0, 20, 50); 154 host_->SetBounds(0, 0, 20, 50);
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 EXPECT_EQ(gfx::Rect(25, 40, 10, 10).ToString(), v2->bounds().ToString()); 338 EXPECT_EQ(gfx::Rect(25, 40, 10, 10).ToString(), v2->bounds().ToString());
355 339
356 // Aligns children to the end of the host horizontally, accounting for the 340 // Aligns children to the end of the host horizontally, accounting for the
357 // inside border spacing. 341 // inside border spacing.
358 layout_->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END); 342 layout_->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END);
359 layout_->Layout(host_.get()); 343 layout_->Layout(host_.get());
360 EXPECT_EQ(gfx::Rect(30, 10, 20, 20).ToString(), v1->bounds().ToString()); 344 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()); 345 EXPECT_EQ(gfx::Rect(40, 40, 10, 10).ToString(), v2->bounds().ToString());
362 } 346 }
363 347
348 TEST_F(BoxLayoutTest, FlexAll) {
349 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10));
350 layout_->set_default_flex(1);
351
352 View* v1 = new StaticSizedView(gfx::Size(20, 20));
353 host_->AddChildView(v1);
354 View* v2 = new StaticSizedView(gfx::Size(10, 10));
355 host_->AddChildView(v2);
356 EXPECT_EQ(gfx::Size(60, 40), layout_->GetPreferredSize(host_.get()));
357
358 host_->SetBounds(0, 0, 100, 40);
359 layout_->Layout(host_.get());
360 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1->bounds().ToString());
361 EXPECT_EQ(gfx::Rect(60, 10, 30, 20).ToString(), v2->bounds().ToString());
362 }
363
364 TEST_F(BoxLayoutTest, FlexGrowVertical) {
365 layout_.reset(new BoxLayout(BoxLayout::kVertical, 10, 10, 10));
366
367 View* v1 = new StaticSizedView(gfx::Size(20, 20));
368 host_->AddChildView(v1);
369 View* v2 = new StaticSizedView(gfx::Size(10, 10));
370 host_->AddChildView(v2);
371 View* v3 = new StaticSizedView(gfx::Size(30, 30));
372 host_->AddChildView(v3);
373
374 host_->SetBounds(0, 0, 50, 130);
375
376 // Views don't fill the available space by default.
377 layout_->Layout(host_.get());
378 EXPECT_EQ(gfx::Rect(10, 10, 30, 20).ToString(), v1->bounds().ToString());
379 EXPECT_EQ(gfx::Rect(10, 40, 30, 10).ToString(), v2->bounds().ToString());
380 EXPECT_EQ(gfx::Rect(10, 60, 30, 30).ToString(), v3->bounds().ToString());
381
382 std::vector<BoxLayout::MainAxisAlignment> main_alignments;
383 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START);
384 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
385 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END);
386
387 for (size_t i = 0; i < main_alignments.size(); ++i) {
388 // Set the first view to consume all free space.
389 layout_->SetFlexForViewAt(0, 1);
390 layout_->ClearFlexForViewAt(1);
391 layout_->ClearFlexForViewAt(2);
392 layout_->Layout(host_.get());
393 EXPECT_EQ(gfx::Rect(10, 10, 30, 50).ToString(), v1->bounds().ToString());
394 EXPECT_EQ(gfx::Rect(10, 70, 30, 10).ToString(), v2->bounds().ToString());
395 EXPECT_EQ(gfx::Rect(10, 90, 30, 30).ToString(), v3->bounds().ToString());
396
397 // Set the third view to take 2/3s of the free space and leave the first
398 // view
399 // with 1/3.
400 layout_->SetFlexForViewAt(2, 2);
401 layout_->Layout(host_.get());
402 EXPECT_EQ(gfx::Rect(10, 10, 30, 30).ToString(), v1->bounds().ToString());
403 EXPECT_EQ(gfx::Rect(10, 50, 30, 10).ToString(), v2->bounds().ToString());
404 EXPECT_EQ(gfx::Rect(10, 70, 30, 50).ToString(), v3->bounds().ToString());
405
406 // Clear the previously set flex values and set the second view to take all
407 // the free space.
408 layout_->ClearFlexForViewAt(0);
409 layout_->ClearFlexForViewAt(2);
410 layout_->SetFlexForViewAt(1, 1);
411 layout_->Layout(host_.get());
412 EXPECT_EQ(gfx::Rect(10, 10, 30, 20).ToString(), v1->bounds().ToString());
413 EXPECT_EQ(gfx::Rect(10, 40, 30, 40).ToString(), v2->bounds().ToString());
414 EXPECT_EQ(gfx::Rect(10, 90, 30, 30).ToString(), v3->bounds().ToString());
415 }
416 }
417
418 TEST_F(BoxLayoutTest, FlexGrowHorizontalWithRemainder) {
419 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0));
420 layout_->set_default_flex(1);
421 std::vector<View*> views;
422 for (int i = 0; i < 5; ++i) {
423 View* view = new StaticSizedView(gfx::Size(10, 10));
424 views.push_back(view);
425 host_->AddChildView(view);
426 }
427
428 EXPECT_EQ(gfx::Size(50, 10), layout_->GetPreferredSize(host_.get()));
429
430 host_->SetBounds(0, 0, 52, 10);
431 layout_->Layout(host_.get());
432 // The 2nd and 4th views should have an extra pixel as they correspond to 20.8
433 // and 41.6 which round up.
434 EXPECT_EQ(gfx::Rect(0, 0, 10, 10).ToString(), views[0]->bounds().ToString());
435 EXPECT_EQ(gfx::Rect(10, 0, 11, 10).ToString(), views[1]->bounds().ToString());
436 EXPECT_EQ(gfx::Rect(21, 0, 10, 10).ToString(), views[2]->bounds().ToString());
437 EXPECT_EQ(gfx::Rect(31, 0, 11, 10).ToString(), views[3]->bounds().ToString());
438 EXPECT_EQ(gfx::Rect(42, 0, 10, 10).ToString(), views[4]->bounds().ToString());
439 }
440
441 TEST_F(BoxLayoutTest, FlexShrinkHorizontal) {
442 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10));
443
444 View* v1 = new StaticSizedView(gfx::Size(20, 20));
445 host_->AddChildView(v1);
446 View* v2 = new StaticSizedView(gfx::Size(10, 10));
447 host_->AddChildView(v2);
448 View* v3 = new StaticSizedView(gfx::Size(30, 30));
449 host_->AddChildView(v3);
450
451 host_->SetBounds(0, 0, 85, 50);
452
453 // Truncate width by default.
454 layout_->Layout(host_.get());
455 EXPECT_EQ(gfx::Rect(10, 10, 20, 30).ToString(), v1->bounds().ToString());
456 EXPECT_EQ(gfx::Rect(40, 10, 10, 30).ToString(), v2->bounds().ToString());
457 EXPECT_EQ(gfx::Rect(60, 10, 15, 30).ToString(), v3->bounds().ToString());
458
459 std::vector<BoxLayout::MainAxisAlignment> main_alignments;
460 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START);
461 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
462 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END);
463
464 for (size_t i = 0; i < main_alignments.size(); ++i) {
465 // Set the first view to shrink as much as necessary.
466 layout_->SetFlexForViewAt(0, 1);
467 layout_->ClearFlexForViewAt(1);
468 layout_->ClearFlexForViewAt(2);
469 layout_->Layout(host_.get());
470 EXPECT_EQ(gfx::Rect(10, 10, 5, 30).ToString(), v1->bounds().ToString());
471 EXPECT_EQ(gfx::Rect(25, 10, 10, 30).ToString(), v2->bounds().ToString());
472 EXPECT_EQ(gfx::Rect(45, 10, 30, 30).ToString(), v3->bounds().ToString());
473
474 // Set the third view to shrink 2/3s of the free space and leave the first
475 // view with 1/3.
476 layout_->SetFlexForViewAt(2, 2);
477 layout_->Layout(host_.get());
478 EXPECT_EQ(gfx::Rect(10, 10, 15, 30).ToString(), v1->bounds().ToString());
479 EXPECT_EQ(gfx::Rect(35, 10, 10, 30).ToString(), v2->bounds().ToString());
480 EXPECT_EQ(gfx::Rect(55, 10, 20, 30).ToString(), v3->bounds().ToString());
481
482 // Clear the previously set flex values and set the second view to take all
483 // the free space with MAIN_AXIS_ALIGNMENT_END set. This causes the second
484 // view to shrink to zero and the third view still doesn't fit so it
485 // overflows.
486 layout_->ClearFlexForViewAt(0);
487 layout_->ClearFlexForViewAt(2);
488 layout_->SetFlexForViewAt(1, 2);
489 layout_->Layout(host_.get());
490 EXPECT_EQ(gfx::Rect(10, 10, 20, 30).ToString(), v1->bounds().ToString());
491 // Conceptually this view is at 10, 40, 0, 0.
492 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(), v2->bounds().ToString());
493 EXPECT_EQ(gfx::Rect(50, 10, 25, 30).ToString(), v3->bounds().ToString());
494 }
495 }
496
497 TEST_F(BoxLayoutTest, FlexShrinkVerticalWithRemainder) {
498 layout_.reset(new BoxLayout(BoxLayout::kVertical, 0, 0, 0));
499 View* v1 = new StaticSizedView(gfx::Size(20, 10));
500 host_->AddChildView(v1);
501 View* v2 = new StaticSizedView(gfx::Size(20, 20));
502 host_->AddChildView(v2);
503 View* v3 = new StaticSizedView(gfx::Size(20, 10));
504 host_->AddChildView(v3);
505 host_->SetBounds(0, 0, 20, 20);
506
507 std::vector<BoxLayout::MainAxisAlignment> main_alignments;
508 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START);
509 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
510 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END);
511
512 for (size_t i = 0; i < main_alignments.size(); ++i) {
513 // The first view shrinks by 1/3 of the excess, the second view shrinks by
514 // 2/3 of the excess and the third view should maintain its preferred size.
515 layout_->SetFlexForViewAt(0, 1);
516 layout_->SetFlexForViewAt(1, 2);
517 layout_->ClearFlexForViewAt(2);
518 layout_->Layout(host_.get());
519 EXPECT_EQ(gfx::Rect(0, 0, 20, 3).ToString(), v1->bounds().ToString());
520 EXPECT_EQ(gfx::Rect(0, 3, 20, 7).ToString(), v2->bounds().ToString());
521 EXPECT_EQ(gfx::Rect(0, 10, 20, 10).ToString(), v3->bounds().ToString());
522
523 // The second view shrinks to 2/3 of the excess, the third view shrinks to
524 // 1/3 of the excess and the first view should maintain its preferred size.
525 layout_->ClearFlexForViewAt(0);
526 layout_->SetFlexForViewAt(1, 2);
527 layout_->SetFlexForViewAt(2, 1);
528 layout_->Layout(host_.get());
529 EXPECT_EQ(gfx::Rect(0, 0, 20, 10).ToString(), v1->bounds().ToString());
530 EXPECT_EQ(gfx::Rect(0, 10, 20, 7).ToString(), v2->bounds().ToString());
531 EXPECT_EQ(gfx::Rect(0, 17, 20, 3).ToString(), v3->bounds().ToString());
532
533 // Each view shrinks equally to fit within the available space.
534 layout_->SetFlexForViewAt(0, 1);
535 layout_->SetFlexForViewAt(1, 1);
536 layout_->SetFlexForViewAt(2, 1);
537 layout_->Layout(host_.get());
538 EXPECT_EQ(gfx::Rect(0, 0, 20, 3).ToString(), v1->bounds().ToString());
539 EXPECT_EQ(gfx::Rect(0, 3, 20, 14).ToString(), v2->bounds().ToString());
540 EXPECT_EQ(gfx::Rect(0, 17, 20, 3).ToString(), v3->bounds().ToString());
541 }
542 }
543
364 } // namespace views 544 } // namespace views
OLDNEW
« ui/views/layout/box_layout.h ('K') | « ui/views/layout/box_layout.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698