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

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: address comment 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.cc ('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_->SetDefaultFlex(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 View* v3 = new StaticSizedView(gfx::Size(30, 30));
357 host_->AddChildView(v3);
358 EXPECT_EQ(gfx::Size(100, 50), layout_->GetPreferredSize(host_.get()));
359
360 host_->SetBounds(0, 0, 120, 50);
361 layout_->Layout(host_.get());
362 EXPECT_EQ(gfx::Rect(10, 10, 27, 30).ToString(), v1->bounds().ToString());
363 EXPECT_EQ(gfx::Rect(47, 10, 16, 30).ToString(), v2->bounds().ToString());
364 EXPECT_EQ(gfx::Rect(73, 10, 37, 30).ToString(), v3->bounds().ToString());
365 }
366
367 TEST_F(BoxLayoutTest, FlexGrowVertical) {
368 layout_.reset(new BoxLayout(BoxLayout::kVertical, 10, 10, 10));
369
370 View* v1 = new StaticSizedView(gfx::Size(20, 20));
371 host_->AddChildView(v1);
372 View* v2 = new StaticSizedView(gfx::Size(10, 10));
373 host_->AddChildView(v2);
374 View* v3 = new StaticSizedView(gfx::Size(30, 30));
375 host_->AddChildView(v3);
376
377 host_->SetBounds(0, 0, 50, 130);
378
379 // Views don't fill the available space by default.
380 layout_->Layout(host_.get());
381 EXPECT_EQ(gfx::Rect(10, 10, 30, 20).ToString(), v1->bounds().ToString());
382 EXPECT_EQ(gfx::Rect(10, 40, 30, 10).ToString(), v2->bounds().ToString());
383 EXPECT_EQ(gfx::Rect(10, 60, 30, 30).ToString(), v3->bounds().ToString());
384
385 std::vector<BoxLayout::MainAxisAlignment> main_alignments;
386 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START);
387 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
388 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END);
389
390 for (size_t i = 0; i < main_alignments.size(); ++i) {
391 layout_->set_main_axis_alignment(main_alignments[i]);
392
393 // Set the first view to consume all free space.
394 layout_->SetFlexForViewAt(0, 1);
395 layout_->ClearFlexForViewAt(1);
396 layout_->ClearFlexForViewAt(2);
397 layout_->Layout(host_.get());
398 EXPECT_EQ(gfx::Rect(10, 10, 30, 50).ToString(), v1->bounds().ToString());
399 EXPECT_EQ(gfx::Rect(10, 70, 30, 10).ToString(), v2->bounds().ToString());
400 EXPECT_EQ(gfx::Rect(10, 90, 30, 30).ToString(), v3->bounds().ToString());
401
402 // Set the third view to take 2/3s of the free space and leave the first
403 // view
404 // with 1/3.
405 layout_->SetFlexForViewAt(2, 2);
406 layout_->Layout(host_.get());
407 EXPECT_EQ(gfx::Rect(10, 10, 30, 30).ToString(), v1->bounds().ToString());
408 EXPECT_EQ(gfx::Rect(10, 50, 30, 10).ToString(), v2->bounds().ToString());
409 EXPECT_EQ(gfx::Rect(10, 70, 30, 50).ToString(), v3->bounds().ToString());
410
411 // Clear the previously set flex values and set the second view to take all
412 // the free space.
413 layout_->ClearFlexForViewAt(0);
414 layout_->ClearFlexForViewAt(2);
415 layout_->SetFlexForViewAt(1, 1);
416 layout_->Layout(host_.get());
417 EXPECT_EQ(gfx::Rect(10, 10, 30, 20).ToString(), v1->bounds().ToString());
418 EXPECT_EQ(gfx::Rect(10, 40, 30, 40).ToString(), v2->bounds().ToString());
419 EXPECT_EQ(gfx::Rect(10, 90, 30, 30).ToString(), v3->bounds().ToString());
420 }
421 }
422
423 TEST_F(BoxLayoutTest, FlexGrowHorizontalWithRemainder) {
424 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0));
425 layout_->SetDefaultFlex(1);
426 std::vector<View*> views;
427 for (int i = 0; i < 5; ++i) {
428 View* view = new StaticSizedView(gfx::Size(10, 10));
429 views.push_back(view);
430 host_->AddChildView(view);
431 }
432
433 EXPECT_EQ(gfx::Size(50, 10), layout_->GetPreferredSize(host_.get()));
434
435 host_->SetBounds(0, 0, 52, 10);
436 layout_->Layout(host_.get());
437 // The 2nd and 4th views should have an extra pixel as they correspond to 20.8
438 // and 41.6 which round up.
439 EXPECT_EQ(gfx::Rect(0, 0, 10, 10).ToString(), views[0]->bounds().ToString());
440 EXPECT_EQ(gfx::Rect(10, 0, 11, 10).ToString(), views[1]->bounds().ToString());
441 EXPECT_EQ(gfx::Rect(21, 0, 10, 10).ToString(), views[2]->bounds().ToString());
442 EXPECT_EQ(gfx::Rect(31, 0, 11, 10).ToString(), views[3]->bounds().ToString());
443 EXPECT_EQ(gfx::Rect(42, 0, 10, 10).ToString(), views[4]->bounds().ToString());
444 }
445
446 TEST_F(BoxLayoutTest, FlexGrowHorizontalWithRemainder2) {
447 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0));
448 layout_->SetDefaultFlex(1);
449 std::vector<View*> views;
450 for (int i = 0; i < 4; ++i) {
451 View* view = new StaticSizedView(gfx::Size(1, 10));
452 views.push_back(view);
453 host_->AddChildView(view);
454 }
455
456 EXPECT_EQ(gfx::Size(4, 10), layout_->GetPreferredSize(host_.get()));
457
458 host_->SetBounds(0, 0, 10, 10);
459 layout_->Layout(host_.get());
460 // The 1st and 3rd views should have an extra pixel as they correspond to 2.5
461 // and 7.5 which round up.
462 EXPECT_EQ(gfx::Rect(0, 0, 3, 10).ToString(), views[0]->bounds().ToString());
463 EXPECT_EQ(gfx::Rect(3, 0, 2, 10).ToString(), views[1]->bounds().ToString());
464 EXPECT_EQ(gfx::Rect(5, 0, 3, 10).ToString(), views[2]->bounds().ToString());
465 EXPECT_EQ(gfx::Rect(8, 0, 2, 10).ToString(), views[3]->bounds().ToString());
466 }
467
468 TEST_F(BoxLayoutTest, FlexShrinkHorizontal) {
469 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10));
470
471 View* v1 = new StaticSizedView(gfx::Size(20, 20));
472 host_->AddChildView(v1);
473 View* v2 = new StaticSizedView(gfx::Size(10, 10));
474 host_->AddChildView(v2);
475 View* v3 = new StaticSizedView(gfx::Size(30, 30));
476 host_->AddChildView(v3);
477
478 host_->SetBounds(0, 0, 85, 50);
479
480 // Truncate width by default.
481 layout_->Layout(host_.get());
482 EXPECT_EQ(gfx::Rect(10, 10, 20, 30).ToString(), v1->bounds().ToString());
483 EXPECT_EQ(gfx::Rect(40, 10, 10, 30).ToString(), v2->bounds().ToString());
484 EXPECT_EQ(gfx::Rect(60, 10, 15, 30).ToString(), v3->bounds().ToString());
485
486 std::vector<BoxLayout::MainAxisAlignment> main_alignments;
487 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START);
488 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
489 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END);
490
491 for (size_t i = 0; i < main_alignments.size(); ++i) {
492 layout_->set_main_axis_alignment(main_alignments[i]);
493
494 // Set the first view to shrink as much as necessary.
495 layout_->SetFlexForViewAt(0, 1);
496 layout_->ClearFlexForViewAt(1);
497 layout_->ClearFlexForViewAt(2);
498 layout_->Layout(host_.get());
499 EXPECT_EQ(gfx::Rect(10, 10, 5, 30).ToString(), v1->bounds().ToString());
500 EXPECT_EQ(gfx::Rect(25, 10, 10, 30).ToString(), v2->bounds().ToString());
501 EXPECT_EQ(gfx::Rect(45, 10, 30, 30).ToString(), v3->bounds().ToString());
502
503 // Set the third view to shrink 2/3s of the free space and leave the first
504 // view with 1/3.
505 layout_->SetFlexForViewAt(2, 2);
506 layout_->Layout(host_.get());
507 EXPECT_EQ(gfx::Rect(10, 10, 15, 30).ToString(), v1->bounds().ToString());
508 EXPECT_EQ(gfx::Rect(35, 10, 10, 30).ToString(), v2->bounds().ToString());
509 EXPECT_EQ(gfx::Rect(55, 10, 20, 30).ToString(), v3->bounds().ToString());
510
511 // Clear the previously set flex values and set the second view to take all
512 // the free space with MAIN_AXIS_ALIGNMENT_END set. This causes the second
513 // view to shrink to zero and the third view still doesn't fit so it
514 // overflows.
515 layout_->ClearFlexForViewAt(0);
516 layout_->ClearFlexForViewAt(2);
517 layout_->SetFlexForViewAt(1, 2);
518 layout_->Layout(host_.get());
519 EXPECT_EQ(gfx::Rect(10, 10, 20, 30).ToString(), v1->bounds().ToString());
520 // Conceptually this view is at 10, 40, 0, 0.
521 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(), v2->bounds().ToString());
522 EXPECT_EQ(gfx::Rect(50, 10, 25, 30).ToString(), v3->bounds().ToString());
523 }
524 }
525
526 TEST_F(BoxLayoutTest, FlexShrinkVerticalWithRemainder) {
527 layout_.reset(new BoxLayout(BoxLayout::kVertical, 0, 0, 0));
528 View* v1 = new StaticSizedView(gfx::Size(20, 10));
529 host_->AddChildView(v1);
530 View* v2 = new StaticSizedView(gfx::Size(20, 20));
531 host_->AddChildView(v2);
532 View* v3 = new StaticSizedView(gfx::Size(20, 10));
533 host_->AddChildView(v3);
534 host_->SetBounds(0, 0, 20, 20);
535
536 std::vector<BoxLayout::MainAxisAlignment> main_alignments;
537 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START);
538 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
539 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END);
540
541 for (size_t i = 0; i < main_alignments.size(); ++i) {
542 layout_->set_main_axis_alignment(main_alignments[i]);
543
544 // The first view shrinks by 1/3 of the excess, the second view shrinks by
545 // 2/3 of the excess and the third view should maintain its preferred size.
546 layout_->SetFlexForViewAt(0, 1);
547 layout_->SetFlexForViewAt(1, 2);
548 layout_->ClearFlexForViewAt(2);
549 layout_->Layout(host_.get());
550 EXPECT_EQ(gfx::Rect(0, 0, 20, 3).ToString(), v1->bounds().ToString());
551 EXPECT_EQ(gfx::Rect(0, 3, 20, 7).ToString(), v2->bounds().ToString());
552 EXPECT_EQ(gfx::Rect(0, 10, 20, 10).ToString(), v3->bounds().ToString());
553
554 // The second view shrinks to 2/3 of the excess, the third view shrinks to
555 // 1/3 of the excess and the first view should maintain its preferred size.
556 layout_->ClearFlexForViewAt(0);
557 layout_->SetFlexForViewAt(1, 2);
558 layout_->SetFlexForViewAt(2, 1);
559 layout_->Layout(host_.get());
560 EXPECT_EQ(gfx::Rect(0, 0, 20, 10).ToString(), v1->bounds().ToString());
561 EXPECT_EQ(gfx::Rect(0, 10, 20, 7).ToString(), v2->bounds().ToString());
562 EXPECT_EQ(gfx::Rect(0, 17, 20, 3).ToString(), v3->bounds().ToString());
563
564 // Each view shrinks equally to fit within the available space.
565 layout_->SetFlexForViewAt(0, 1);
566 layout_->SetFlexForViewAt(1, 1);
567 layout_->SetFlexForViewAt(2, 1);
568 layout_->Layout(host_.get());
569 EXPECT_EQ(gfx::Rect(0, 0, 20, 3).ToString(), v1->bounds().ToString());
570 EXPECT_EQ(gfx::Rect(0, 3, 20, 14).ToString(), v2->bounds().ToString());
571 EXPECT_EQ(gfx::Rect(0, 17, 20, 3).ToString(), v3->bounds().ToString());
572 }
573 }
574
364 } // namespace views 575 } // namespace views
OLDNEW
« ui/views/layout/box_layout.cc ('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