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

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

Issue 2836313002: BoxLayout now suports per-view margins. (Closed)
Patch Set: Merged with master. Removed cached orientation_ from ViewWrapper. Created 3 years, 6 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
« no previous file with comments | « ui/views/layout/box_layout.cc ('k') | ui/views/view.h » ('j') | 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 <stddef.h> 7 #include <stddef.h>
8 8
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/gfx/geometry/insets.h" 10 #include "ui/gfx/geometry/insets.h"
11 #include "ui/views/test/test_views.h" 11 #include "ui/views/test/test_views.h"
12 #include "ui/views/view.h" 12 #include "ui/views/view.h"
13 #include "ui/views/view_properties.h"
13 14
14 namespace views { 15 namespace views {
15 16
16 namespace { 17 namespace {
17 18
18 class BoxLayoutTest : public testing::Test { 19 class BoxLayoutTest : public testing::Test {
19 public: 20 public:
20 void SetUp() override { host_.reset(new View); } 21 void SetUp() override { host_.reset(new View); }
21 22
22 std::unique_ptr<View> host_; 23 std::unique_ptr<View> host_;
(...skipping 608 matching lines...) Expand 10 before | Expand all | Expand 10 after
631 TEST_F(BoxLayoutTest, MinimumCrossAxisHorizontal) { 632 TEST_F(BoxLayoutTest, MinimumCrossAxisHorizontal) {
632 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal); 633 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal);
633 host_->SetLayoutManager(layout); 634 host_->SetLayoutManager(layout);
634 View* v1 = new StaticSizedView(gfx::Size(20, 10)); 635 View* v1 = new StaticSizedView(gfx::Size(20, 10));
635 host_->AddChildView(v1); 636 host_->AddChildView(v1);
636 layout->set_minimum_cross_axis_size(30); 637 layout->set_minimum_cross_axis_size(30);
637 638
638 EXPECT_EQ(gfx::Size(20, 30), layout->GetPreferredSize(host_.get())); 639 EXPECT_EQ(gfx::Size(20, 30), layout->GetPreferredSize(host_.get()));
639 } 640 }
640 641
642 TEST_F(BoxLayoutTest, MarginsUncollapsedHorizontal) {
643 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal);
644 host_->SetLayoutManager(layout);
645 View* v1 = new StaticSizedView(gfx::Size(20, 10));
646 v1->SetProperty(kMarginsKey, new gfx::Insets(5, 5, 5, 5));
647 host_->AddChildView(v1);
648 View* v2 = new StaticSizedView(gfx::Size(20, 10));
649 v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 6, 4));
650 host_->AddChildView(v2);
651
652 EXPECT_EQ(gfx::Size(58, 22), layout->GetPreferredSize(host_.get()));
653 host_->SizeToPreferredSize();
654 layout->Layout(host_.get());
655 EXPECT_EQ(gfx::Rect(5, 5, 20, 12), v1->bounds());
656 EXPECT_EQ(gfx::Rect(34, 6, 20, 10), v2->bounds());
657 }
658
659 TEST_F(BoxLayoutTest, MarginsCollapsedHorizontal) {
660 BoxLayout* layout =
661 new BoxLayout(BoxLayout::kHorizontal, gfx::Insets(0, 0), 0, true);
662 host_->SetLayoutManager(layout);
663 View* v1 = new StaticSizedView(gfx::Size(20, 10));
664 v1->SetProperty(kMarginsKey, new gfx::Insets(5, 5, 5, 5));
665 host_->AddChildView(v1);
666 View* v2 = new StaticSizedView(gfx::Size(20, 10));
667 v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 6, 4));
668 host_->AddChildView(v2);
669
670 EXPECT_EQ(gfx::Size(54, 22), layout->GetPreferredSize(host_.get()));
671 host_->SizeToPreferredSize();
672 layout->Layout(host_.get());
673 EXPECT_EQ(gfx::Rect(5, 5, 20, 12), v1->bounds());
674 EXPECT_EQ(gfx::Rect(30, 6, 20, 10), v2->bounds());
675 }
676
677 TEST_F(BoxLayoutTest, MarginsUncollapsedVertical) {
678 BoxLayout* layout = new BoxLayout(BoxLayout::kVertical);
679 host_->SetLayoutManager(layout);
680 View* v1 = new StaticSizedView(gfx::Size(20, 10));
681 v1->SetProperty(kMarginsKey, new gfx::Insets(5, 5, 5, 5));
682 host_->AddChildView(v1);
683 View* v2 = new StaticSizedView(gfx::Size(20, 10));
684 v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 6, 4));
685 host_->AddChildView(v2);
686
687 EXPECT_EQ(gfx::Size(30, 42), layout->GetPreferredSize(host_.get()));
688 host_->SizeToPreferredSize();
689 layout->Layout(host_.get());
690 EXPECT_EQ(gfx::Rect(5, 5, 20, 10), v1->bounds());
691 EXPECT_EQ(gfx::Rect(4, 26, 22, 10), v2->bounds());
692 }
693
694 TEST_F(BoxLayoutTest, MarginsCollapsedVertical) {
695 BoxLayout* layout =
696 new BoxLayout(BoxLayout::kVertical, gfx::Insets(0, 0), 0, true);
697 host_->SetLayoutManager(layout);
698 View* v1 = new StaticSizedView(gfx::Size(20, 10));
699 v1->SetProperty(kMarginsKey, new gfx::Insets(5, 5, 5, 5));
700 host_->AddChildView(v1);
701 View* v2 = new StaticSizedView(gfx::Size(20, 10));
702 v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 6, 4));
703 host_->AddChildView(v2);
704
705 EXPECT_EQ(gfx::Size(30, 37), layout->GetPreferredSize(host_.get()));
706 host_->SizeToPreferredSize();
707 layout->Layout(host_.get());
708 EXPECT_EQ(gfx::Rect(5, 5, 20, 10), v1->bounds());
709 EXPECT_EQ(gfx::Rect(4, 21, 22, 10), v2->bounds());
710 }
711
712 TEST_F(BoxLayoutTest, UnbalancedMarginsUncollapsedHorizontal) {
713 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal);
714 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
715 host_->SetLayoutManager(layout);
716 View* v1 = new StaticSizedView(gfx::Size(20, 10));
717 v1->SetProperty(kMarginsKey, new gfx::Insets(5, 5, 4, 4));
718 host_->AddChildView(v1);
719 View* v2 = new StaticSizedView(gfx::Size(20, 10));
720 v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 3, 6));
721 host_->AddChildView(v2);
722
723 EXPECT_EQ(gfx::Size(59, 20), layout->GetPreferredSize(host_.get()));
724 host_->SizeToPreferredSize();
725 layout->Layout(host_.get());
726 EXPECT_EQ(gfx::Rect(5, 5, 20, 10), v1->bounds());
727 EXPECT_EQ(gfx::Rect(33, 6, 20, 10), v2->bounds());
728 }
729
730 TEST_F(BoxLayoutTest, UnbalancedMarginsCollapsedHorizontal) {
731 BoxLayout* layout =
732 new BoxLayout(BoxLayout::kHorizontal, gfx::Insets(0, 0), 0, true);
733 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
734 host_->SetLayoutManager(layout);
735 View* v1 = new StaticSizedView(gfx::Size(20, 10));
736 v1->SetProperty(kMarginsKey, new gfx::Insets(5, 5, 4, 4));
737 host_->AddChildView(v1);
738 View* v2 = new StaticSizedView(gfx::Size(20, 10));
739 v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 3, 6));
740 host_->AddChildView(v2);
741
742 EXPECT_EQ(gfx::Size(55, 20), layout->GetPreferredSize(host_.get()));
743 host_->SizeToPreferredSize();
744 layout->Layout(host_.get());
745 EXPECT_EQ(gfx::Rect(5, 5, 20, 10), v1->bounds());
746 EXPECT_EQ(gfx::Rect(29, 6, 20, 10), v2->bounds());
747 }
748
749 TEST_F(BoxLayoutTest, UnbalancedMarginsUncollapsedVertical) {
750 BoxLayout* layout = new BoxLayout(BoxLayout::kVertical);
751 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
752 host_->SetLayoutManager(layout);
753 View* v1 = new StaticSizedView(gfx::Size(20, 10));
754 v1->SetProperty(kMarginsKey, new gfx::Insets(4, 5, 5, 3));
755 host_->AddChildView(v1);
756 View* v2 = new StaticSizedView(gfx::Size(20, 10));
757 v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 3, 5));
758 host_->AddChildView(v2);
759
760 EXPECT_EQ(gfx::Size(30, 38), layout->GetPreferredSize(host_.get()));
761 host_->SizeToPreferredSize();
762 layout->Layout(host_.get());
763 EXPECT_EQ(gfx::Rect(5, 4, 20, 10), v1->bounds());
764 EXPECT_EQ(gfx::Rect(5, 25, 20, 10), v2->bounds());
765 }
766
767 TEST_F(BoxLayoutTest, UnbalancedMarginsCollapsedVertical) {
768 BoxLayout* layout =
769 new BoxLayout(BoxLayout::kVertical, gfx::Insets(0, 0), 0, true);
770 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
771 host_->SetLayoutManager(layout);
772 View* v1 = new StaticSizedView(gfx::Size(20, 10));
773 v1->SetProperty(kMarginsKey, new gfx::Insets(4, 5, 5, 3));
774 host_->AddChildView(v1);
775 View* v2 = new StaticSizedView(gfx::Size(20, 10));
776 v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 3, 5));
777 host_->AddChildView(v2);
778
779 EXPECT_EQ(gfx::Size(30, 33), layout->GetPreferredSize(host_.get()));
780 host_->SizeToPreferredSize();
781 layout->Layout(host_.get());
782 EXPECT_EQ(gfx::Rect(5, 4, 20, 10), v1->bounds());
783 EXPECT_EQ(gfx::Rect(5, 20, 20, 10), v2->bounds());
784 }
785
786 TEST_F(BoxLayoutTest, OverlappingCrossMarginsAlignEnd) {
787 {
788 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal);
789 host_->SetLayoutManager(layout);
790 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END);
791 View* v1 = new StaticSizedView(gfx::Size(20, 4));
792 v1->SetProperty(kMarginsKey, new gfx::Insets(3, 0, 0, 0));
793 host_->AddChildView(v1);
794 View* v2 = new StaticSizedView(gfx::Size(20, 5));
795 v2->SetProperty(kMarginsKey, new gfx::Insets(0, 0, 2, 0));
796 host_->AddChildView(v2);
797
798 EXPECT_EQ(9, layout->GetPreferredSize(host_.get()).height());
799 }
800 host_->RemoveAllChildViews(true);
801 {
802 BoxLayout* layout =
803 new BoxLayout(BoxLayout::kHorizontal, gfx::Insets(0, 0), 0, true);
804 host_->SetLayoutManager(layout);
805 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END);
806 View* v1 = new StaticSizedView(gfx::Size(20, 4));
807 v1->SetProperty(kMarginsKey, new gfx::Insets(3, 0, 0, 0));
808 host_->AddChildView(v1);
809 View* v2 = new StaticSizedView(gfx::Size(20, 5));
810 v2->SetProperty(kMarginsKey, new gfx::Insets(0, 0, 2, 0));
811 host_->AddChildView(v2);
812
813 EXPECT_EQ(9, layout->GetPreferredSize(host_.get()).height());
814 }
815 }
816
817 TEST_F(BoxLayoutTest, OverlappingCrossMarginsAlignStretch) {
818 {
819 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal);
820 host_->SetLayoutManager(layout);
821 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH);
822 View* v1 = new StaticSizedView(gfx::Size(20, 4));
823 v1->SetProperty(kMarginsKey, new gfx::Insets(3, 0, 0, 0));
824 host_->AddChildView(v1);
825 View* v2 = new StaticSizedView(gfx::Size(20, 5));
826 v2->SetProperty(kMarginsKey, new gfx::Insets(0, 0, 2, 0));
827 host_->AddChildView(v2);
828
829 EXPECT_EQ(10, layout->GetPreferredSize(host_.get()).height());
830 }
831 host_->RemoveAllChildViews(true);
832 {
833 BoxLayout* layout =
834 new BoxLayout(BoxLayout::kHorizontal, gfx::Insets(0, 0), 0, true);
835 host_->SetLayoutManager(layout);
836 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH);
837 View* v1 = new StaticSizedView(gfx::Size(20, 4));
838 v1->SetProperty(kMarginsKey, new gfx::Insets(3, 0, 0, 0));
839 host_->AddChildView(v1);
840 View* v2 = new StaticSizedView(gfx::Size(20, 5));
841 v2->SetProperty(kMarginsKey, new gfx::Insets(0, 0, 2, 0));
842 host_->AddChildView(v2);
843
844 EXPECT_EQ(10, layout->GetPreferredSize(host_.get()).height());
845 }
846 }
847
848 TEST_F(BoxLayoutTest, OverlappingCrossMarginsAlignStart) {
849 {
850 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal);
851 host_->SetLayoutManager(layout);
852 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_START);
853 View* v1 = new StaticSizedView(gfx::Size(20, 4));
854 v1->SetProperty(kMarginsKey, new gfx::Insets(0, 0, 3, 0));
855 host_->AddChildView(v1);
856 View* v2 = new StaticSizedView(gfx::Size(20, 5));
857 v2->SetProperty(kMarginsKey, new gfx::Insets(2, 0, 0, 0));
858 host_->AddChildView(v2);
859
860 EXPECT_EQ(9, layout->GetPreferredSize(host_.get()).height());
861 }
862 host_->RemoveAllChildViews(true);
863 {
864 BoxLayout* layout =
865 new BoxLayout(BoxLayout::kHorizontal, gfx::Insets(0, 0), 0, true);
866 host_->SetLayoutManager(layout);
867 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_START);
868 View* v1 = new StaticSizedView(gfx::Size(20, 4));
869 v1->SetProperty(kMarginsKey, new gfx::Insets(0, 0, 3, 0));
870 host_->AddChildView(v1);
871 View* v2 = new StaticSizedView(gfx::Size(20, 5));
872 v2->SetProperty(kMarginsKey, new gfx::Insets(2, 0, 0, 0));
873 host_->AddChildView(v2);
874
875 EXPECT_EQ(9, layout->GetPreferredSize(host_.get()).height());
876 }
877 }
878
641 } // namespace views 879 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/layout/box_layout.cc ('k') | ui/views/view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698