Index: ui/views/layout/box_layout_unittest.cc |
diff --git a/ui/views/layout/box_layout_unittest.cc b/ui/views/layout/box_layout_unittest.cc |
index 5f58b15c8fd173afb7638ed94db7aab133a22ba0..5a1b92c83a35a707fa1215253cd6d87b647d7086 100644 |
--- a/ui/views/layout/box_layout_unittest.cc |
+++ b/ui/views/layout/box_layout_unittest.cc |
@@ -142,22 +142,6 @@ TEST_F(BoxLayoutTest, InvisibleChild) { |
EXPECT_EQ(gfx::Rect(10, 10, 10, 10), v2->bounds()); |
} |
-TEST_F(BoxLayoutTest, MainAxisAlignmentFill) { |
- layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10)); |
- layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_FILL); |
- |
- View* v1 = new StaticSizedView(gfx::Size(20, 20)); |
- host_->AddChildView(v1); |
- View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
- host_->AddChildView(v2); |
- EXPECT_EQ(gfx::Size(60, 40), layout_->GetPreferredSize(host_.get())); |
- |
- host_->SetBounds(0, 0, 100, 40); |
- layout_->Layout(host_.get()); |
- EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1->bounds().ToString()); |
- EXPECT_EQ(gfx::Rect(60, 10, 30, 20).ToString(), v2->bounds().ToString()); |
-} |
- |
TEST_F(BoxLayoutTest, UseHeightForWidth) { |
layout_.reset(new BoxLayout(BoxLayout::kVertical, 0, 0, 0)); |
View* v1 = new StaticSizedView(gfx::Size(20, 10)); |
@@ -361,4 +345,231 @@ TEST_F(BoxLayoutTest, CrossAxisAlignmentVertical) { |
EXPECT_EQ(gfx::Rect(40, 40, 10, 10).ToString(), v2->bounds().ToString()); |
} |
+TEST_F(BoxLayoutTest, FlexAll) { |
+ layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10)); |
+ layout_->SetDefaultFlex(1); |
+ |
+ View* v1 = new StaticSizedView(gfx::Size(20, 20)); |
+ host_->AddChildView(v1); |
+ View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
+ host_->AddChildView(v2); |
+ View* v3 = new StaticSizedView(gfx::Size(30, 30)); |
+ host_->AddChildView(v3); |
+ EXPECT_EQ(gfx::Size(100, 50), layout_->GetPreferredSize(host_.get())); |
+ |
+ host_->SetBounds(0, 0, 120, 50); |
+ layout_->Layout(host_.get()); |
+ EXPECT_EQ(gfx::Rect(10, 10, 27, 30).ToString(), v1->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(47, 10, 16, 30).ToString(), v2->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(73, 10, 37, 30).ToString(), v3->bounds().ToString()); |
+} |
+ |
+TEST_F(BoxLayoutTest, FlexGrowVertical) { |
+ layout_.reset(new BoxLayout(BoxLayout::kVertical, 10, 10, 10)); |
+ |
+ View* v1 = new StaticSizedView(gfx::Size(20, 20)); |
+ host_->AddChildView(v1); |
+ View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
+ host_->AddChildView(v2); |
+ View* v3 = new StaticSizedView(gfx::Size(30, 30)); |
+ host_->AddChildView(v3); |
+ |
+ host_->SetBounds(0, 0, 50, 130); |
+ |
+ // Views don't fill the available space by default. |
+ layout_->Layout(host_.get()); |
+ EXPECT_EQ(gfx::Rect(10, 10, 30, 20).ToString(), v1->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(10, 40, 30, 10).ToString(), v2->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(10, 60, 30, 30).ToString(), v3->bounds().ToString()); |
+ |
+ std::vector<BoxLayout::MainAxisAlignment> main_alignments; |
+ main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START); |
+ main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); |
+ main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END); |
+ |
+ for (size_t i = 0; i < main_alignments.size(); ++i) { |
+ layout_->set_main_axis_alignment(main_alignments[i]); |
+ |
+ // Set the first view to consume all free space. |
+ layout_->SetFlexForViewAt(0, 1); |
+ layout_->ClearFlexForViewAt(1); |
+ layout_->ClearFlexForViewAt(2); |
+ layout_->Layout(host_.get()); |
+ EXPECT_EQ(gfx::Rect(10, 10, 30, 50).ToString(), v1->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(10, 70, 30, 10).ToString(), v2->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(10, 90, 30, 30).ToString(), v3->bounds().ToString()); |
+ |
+ // Set the third view to take 2/3s of the free space and leave the first |
+ // view |
+ // with 1/3. |
+ layout_->SetFlexForViewAt(2, 2); |
+ layout_->Layout(host_.get()); |
+ EXPECT_EQ(gfx::Rect(10, 10, 30, 30).ToString(), v1->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(10, 50, 30, 10).ToString(), v2->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(10, 70, 30, 50).ToString(), v3->bounds().ToString()); |
+ |
+ // Clear the previously set flex values and set the second view to take all |
+ // the free space. |
+ layout_->ClearFlexForViewAt(0); |
+ layout_->ClearFlexForViewAt(2); |
+ layout_->SetFlexForViewAt(1, 1); |
+ layout_->Layout(host_.get()); |
+ EXPECT_EQ(gfx::Rect(10, 10, 30, 20).ToString(), v1->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(10, 40, 30, 40).ToString(), v2->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(10, 90, 30, 30).ToString(), v3->bounds().ToString()); |
+ } |
+} |
+ |
+TEST_F(BoxLayoutTest, FlexGrowHorizontalWithRemainder) { |
+ layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0)); |
+ layout_->SetDefaultFlex(1); |
+ std::vector<View*> views; |
+ for (int i = 0; i < 5; ++i) { |
+ View* view = new StaticSizedView(gfx::Size(10, 10)); |
+ views.push_back(view); |
+ host_->AddChildView(view); |
+ } |
+ |
+ EXPECT_EQ(gfx::Size(50, 10), layout_->GetPreferredSize(host_.get())); |
+ |
+ host_->SetBounds(0, 0, 52, 10); |
+ layout_->Layout(host_.get()); |
+ // The 2nd and 4th views should have an extra pixel as they correspond to 20.8 |
+ // and 41.6 which round up. |
+ EXPECT_EQ(gfx::Rect(0, 0, 10, 10).ToString(), views[0]->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(10, 0, 11, 10).ToString(), views[1]->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(21, 0, 10, 10).ToString(), views[2]->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(31, 0, 11, 10).ToString(), views[3]->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(42, 0, 10, 10).ToString(), views[4]->bounds().ToString()); |
+} |
+ |
+TEST_F(BoxLayoutTest, FlexGrowHorizontalWithRemainder2) { |
+ layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0)); |
+ layout_->SetDefaultFlex(1); |
+ std::vector<View*> views; |
+ for (int i = 0; i < 4; ++i) { |
+ View* view = new StaticSizedView(gfx::Size(1, 10)); |
+ views.push_back(view); |
+ host_->AddChildView(view); |
+ } |
+ |
+ EXPECT_EQ(gfx::Size(4, 10), layout_->GetPreferredSize(host_.get())); |
+ |
+ host_->SetBounds(0, 0, 10, 10); |
+ layout_->Layout(host_.get()); |
+ // The 1st and 3rd views should have an extra pixel as they correspond to 2.5 |
+ // and 7.5 which round up. |
+ EXPECT_EQ(gfx::Rect(0, 0, 3, 10).ToString(), views[0]->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(3, 0, 2, 10).ToString(), views[1]->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(5, 0, 3, 10).ToString(), views[2]->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(8, 0, 2, 10).ToString(), views[3]->bounds().ToString()); |
+} |
+ |
+TEST_F(BoxLayoutTest, FlexShrinkHorizontal) { |
+ layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10)); |
+ |
+ View* v1 = new StaticSizedView(gfx::Size(20, 20)); |
+ host_->AddChildView(v1); |
+ View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
+ host_->AddChildView(v2); |
+ View* v3 = new StaticSizedView(gfx::Size(30, 30)); |
+ host_->AddChildView(v3); |
+ |
+ host_->SetBounds(0, 0, 85, 50); |
+ |
+ // Truncate width by default. |
+ layout_->Layout(host_.get()); |
+ EXPECT_EQ(gfx::Rect(10, 10, 20, 30).ToString(), v1->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(40, 10, 10, 30).ToString(), v2->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(60, 10, 15, 30).ToString(), v3->bounds().ToString()); |
+ |
+ std::vector<BoxLayout::MainAxisAlignment> main_alignments; |
+ main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START); |
+ main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); |
+ main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END); |
+ |
+ for (size_t i = 0; i < main_alignments.size(); ++i) { |
+ layout_->set_main_axis_alignment(main_alignments[i]); |
+ |
+ // Set the first view to shrink as much as necessary. |
+ layout_->SetFlexForViewAt(0, 1); |
+ layout_->ClearFlexForViewAt(1); |
+ layout_->ClearFlexForViewAt(2); |
+ layout_->Layout(host_.get()); |
+ EXPECT_EQ(gfx::Rect(10, 10, 5, 30).ToString(), v1->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(25, 10, 10, 30).ToString(), v2->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(45, 10, 30, 30).ToString(), v3->bounds().ToString()); |
+ |
+ // Set the third view to shrink 2/3s of the free space and leave the first |
+ // view with 1/3. |
+ layout_->SetFlexForViewAt(2, 2); |
+ layout_->Layout(host_.get()); |
+ EXPECT_EQ(gfx::Rect(10, 10, 15, 30).ToString(), v1->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(35, 10, 10, 30).ToString(), v2->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(55, 10, 20, 30).ToString(), v3->bounds().ToString()); |
+ |
+ // Clear the previously set flex values and set the second view to take all |
+ // the free space with MAIN_AXIS_ALIGNMENT_END set. This causes the second |
+ // view to shrink to zero and the third view still doesn't fit so it |
+ // overflows. |
+ layout_->ClearFlexForViewAt(0); |
+ layout_->ClearFlexForViewAt(2); |
+ layout_->SetFlexForViewAt(1, 2); |
+ layout_->Layout(host_.get()); |
+ EXPECT_EQ(gfx::Rect(10, 10, 20, 30).ToString(), v1->bounds().ToString()); |
+ // Conceptually this view is at 10, 40, 0, 0. |
+ EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(), v2->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(50, 10, 25, 30).ToString(), v3->bounds().ToString()); |
+ } |
+} |
+ |
+TEST_F(BoxLayoutTest, FlexShrinkVerticalWithRemainder) { |
+ layout_.reset(new BoxLayout(BoxLayout::kVertical, 0, 0, 0)); |
+ View* v1 = new StaticSizedView(gfx::Size(20, 10)); |
+ host_->AddChildView(v1); |
+ View* v2 = new StaticSizedView(gfx::Size(20, 20)); |
+ host_->AddChildView(v2); |
+ View* v3 = new StaticSizedView(gfx::Size(20, 10)); |
+ host_->AddChildView(v3); |
+ host_->SetBounds(0, 0, 20, 20); |
+ |
+ std::vector<BoxLayout::MainAxisAlignment> main_alignments; |
+ main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START); |
+ main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); |
+ main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END); |
+ |
+ for (size_t i = 0; i < main_alignments.size(); ++i) { |
+ layout_->set_main_axis_alignment(main_alignments[i]); |
+ |
+ // The first view shrinks by 1/3 of the excess, the second view shrinks by |
+ // 2/3 of the excess and the third view should maintain its preferred size. |
+ layout_->SetFlexForViewAt(0, 1); |
+ layout_->SetFlexForViewAt(1, 2); |
+ layout_->ClearFlexForViewAt(2); |
+ layout_->Layout(host_.get()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 20, 3).ToString(), v1->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(0, 3, 20, 7).ToString(), v2->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(0, 10, 20, 10).ToString(), v3->bounds().ToString()); |
+ |
+ // The second view shrinks to 2/3 of the excess, the third view shrinks to |
+ // 1/3 of the excess and the first view should maintain its preferred size. |
+ layout_->ClearFlexForViewAt(0); |
+ layout_->SetFlexForViewAt(1, 2); |
+ layout_->SetFlexForViewAt(2, 1); |
+ layout_->Layout(host_.get()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 20, 10).ToString(), v1->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(0, 10, 20, 7).ToString(), v2->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(0, 17, 20, 3).ToString(), v3->bounds().ToString()); |
+ |
+ // Each view shrinks equally to fit within the available space. |
+ layout_->SetFlexForViewAt(0, 1); |
+ layout_->SetFlexForViewAt(1, 1); |
+ layout_->SetFlexForViewAt(2, 1); |
+ layout_->Layout(host_.get()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 20, 3).ToString(), v1->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(0, 3, 20, 14).ToString(), v2->bounds().ToString()); |
+ EXPECT_EQ(gfx::Rect(0, 17, 20, 3).ToString(), v3->bounds().ToString()); |
+ } |
+} |
+ |
} // namespace views |