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

Unified Diff: ui/views/layout/box_layout_unittest.cc

Issue 2836313002: BoxLayout now suports per-view margins. (Closed)
Patch Set: Don't collapse cross axis margins with adjacent margins in STRETCH or CENTER. 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 side-by-side diff with in-line comments
Download patch
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 d2d073e5cb78d3cbae27b80b204e13adab569fce..cee72945eee0be3ac7b189cdd8eb565ee33117d7 100644
--- a/ui/views/layout/box_layout_unittest.cc
+++ b/ui/views/layout/box_layout_unittest.cc
@@ -9,6 +9,7 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/views/test/test_views.h"
#include "ui/views/view.h"
+#include "ui/views/view_properties.h"
namespace views {
@@ -629,4 +630,234 @@ TEST_F(BoxLayoutTest, MinimumCrossAxisHorizontal) {
EXPECT_EQ(gfx::Size(20, 30), layout->GetPreferredSize(host_.get()));
}
+TEST_F(BoxLayoutTest, MarginsUncollapsedHorizontal) {
+ BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, false);
+ host_->SetLayoutManager(layout);
+ View* v1 = new StaticSizedView(gfx::Size(20, 10));
+ v1->SetProperty(kMarginsKey, new gfx::Insets(5, 5, 5, 5));
+ host_->AddChildView(v1);
+ View* v2 = new StaticSizedView(gfx::Size(20, 10));
+ v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 6, 4));
+ host_->AddChildView(v2);
+
+ EXPECT_EQ(gfx::Size(58, 22), layout->GetPreferredSize(host_.get()));
+ host_->SizeToPreferredSize();
+ layout->Layout(host_.get());
+ EXPECT_EQ(gfx::Rect(5, 5, 20, 12), v1->bounds());
+ EXPECT_EQ(gfx::Rect(34, 6, 20, 10), v2->bounds());
+}
+
+TEST_F(BoxLayoutTest, MarginsCollapsedHorizontal) {
+ BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, true);
+ host_->SetLayoutManager(layout);
+ View* v1 = new StaticSizedView(gfx::Size(20, 10));
+ v1->SetProperty(kMarginsKey, new gfx::Insets(5, 5, 5, 5));
+ host_->AddChildView(v1);
+ View* v2 = new StaticSizedView(gfx::Size(20, 10));
+ v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 6, 4));
+ host_->AddChildView(v2);
+
+ EXPECT_EQ(gfx::Size(54, 22), layout->GetPreferredSize(host_.get()));
+ host_->SizeToPreferredSize();
+ layout->Layout(host_.get());
+ EXPECT_EQ(gfx::Rect(5, 5, 20, 12), v1->bounds());
+ EXPECT_EQ(gfx::Rect(30, 6, 20, 10), v2->bounds());
+}
+
+TEST_F(BoxLayoutTest, MarginsUncollapsedVertical) {
+ BoxLayout* layout = new BoxLayout(BoxLayout::kVertical, 0, 0, 0, false);
+ host_->SetLayoutManager(layout);
+ View* v1 = new StaticSizedView(gfx::Size(20, 10));
+ v1->SetProperty(kMarginsKey, new gfx::Insets(5, 5, 5, 5));
+ host_->AddChildView(v1);
+ View* v2 = new StaticSizedView(gfx::Size(20, 10));
+ v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 6, 4));
+ host_->AddChildView(v2);
+
+ EXPECT_EQ(gfx::Size(30, 42), layout->GetPreferredSize(host_.get()));
+ host_->SizeToPreferredSize();
+ layout->Layout(host_.get());
+ EXPECT_EQ(gfx::Rect(5, 5, 20, 10), v1->bounds());
+ EXPECT_EQ(gfx::Rect(4, 26, 22, 10), v2->bounds());
+}
+
+TEST_F(BoxLayoutTest, MarginsCollapsedVertical) {
+ BoxLayout* layout = new BoxLayout(BoxLayout::kVertical, 0, 0, 0, true);
+ host_->SetLayoutManager(layout);
+ View* v1 = new StaticSizedView(gfx::Size(20, 10));
+ v1->SetProperty(kMarginsKey, new gfx::Insets(5, 5, 5, 5));
+ host_->AddChildView(v1);
+ View* v2 = new StaticSizedView(gfx::Size(20, 10));
+ v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 6, 4));
+ host_->AddChildView(v2);
+
+ EXPECT_EQ(gfx::Size(30, 37), layout->GetPreferredSize(host_.get()));
+ host_->SizeToPreferredSize();
+ layout->Layout(host_.get());
+ EXPECT_EQ(gfx::Rect(5, 5, 20, 10), v1->bounds());
+ EXPECT_EQ(gfx::Rect(4, 21, 22, 10), v2->bounds());
+}
+
+TEST_F(BoxLayoutTest, UnbalancedMarginsUncollapsedHorizontal) {
+ BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, false);
+ layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
+ host_->SetLayoutManager(layout);
+ View* v1 = new StaticSizedView(gfx::Size(20, 10));
+ v1->SetProperty(kMarginsKey, new gfx::Insets(5, 5, 4, 4));
+ host_->AddChildView(v1);
+ View* v2 = new StaticSizedView(gfx::Size(20, 10));
+ v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 3, 6));
+ host_->AddChildView(v2);
+
+ EXPECT_EQ(gfx::Size(59, 20), layout->GetPreferredSize(host_.get()));
+ host_->SizeToPreferredSize();
+ layout->Layout(host_.get());
+ EXPECT_EQ(gfx::Rect(5, 5, 20, 10), v1->bounds());
+ EXPECT_EQ(gfx::Rect(33, 6, 20, 10), v2->bounds());
+}
+
+TEST_F(BoxLayoutTest, UnbalancedMarginsCollapsedHorizontal) {
+ BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, true);
+ layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
+ host_->SetLayoutManager(layout);
+ View* v1 = new StaticSizedView(gfx::Size(20, 10));
+ v1->SetProperty(kMarginsKey, new gfx::Insets(5, 5, 4, 4));
+ host_->AddChildView(v1);
+ View* v2 = new StaticSizedView(gfx::Size(20, 10));
+ v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 3, 6));
+ host_->AddChildView(v2);
+
+ EXPECT_EQ(gfx::Size(55, 20), layout->GetPreferredSize(host_.get()));
+ host_->SizeToPreferredSize();
+ layout->Layout(host_.get());
+ EXPECT_EQ(gfx::Rect(5, 5, 20, 10), v1->bounds());
+ EXPECT_EQ(gfx::Rect(29, 6, 20, 10), v2->bounds());
+}
+
+TEST_F(BoxLayoutTest, UnbalancedMarginsUncollapsedVertical) {
+ BoxLayout* layout = new BoxLayout(BoxLayout::kVertical, 0, 0, 0, false);
+ layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
+ host_->SetLayoutManager(layout);
+ View* v1 = new StaticSizedView(gfx::Size(20, 10));
+ v1->SetProperty(kMarginsKey, new gfx::Insets(4, 5, 5, 3));
+ host_->AddChildView(v1);
+ View* v2 = new StaticSizedView(gfx::Size(20, 10));
+ v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 3, 5));
+ host_->AddChildView(v2);
+
+ EXPECT_EQ(gfx::Size(30, 38), layout->GetPreferredSize(host_.get()));
+ host_->SizeToPreferredSize();
+ layout->Layout(host_.get());
+ EXPECT_EQ(gfx::Rect(5, 4, 20, 10), v1->bounds());
+ EXPECT_EQ(gfx::Rect(5, 25, 20, 10), v2->bounds());
+}
+
+TEST_F(BoxLayoutTest, UnbalancedMarginsCollapsedVertical) {
+ BoxLayout* layout = new BoxLayout(BoxLayout::kVertical, 0, 0, 0, true);
+ layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
+ host_->SetLayoutManager(layout);
+ View* v1 = new StaticSizedView(gfx::Size(20, 10));
+ v1->SetProperty(kMarginsKey, new gfx::Insets(4, 5, 5, 3));
+ host_->AddChildView(v1);
+ View* v2 = new StaticSizedView(gfx::Size(20, 10));
+ v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 3, 5));
+ host_->AddChildView(v2);
+
+ EXPECT_EQ(gfx::Size(30, 33), layout->GetPreferredSize(host_.get()));
+ host_->SizeToPreferredSize();
+ layout->Layout(host_.get());
+ EXPECT_EQ(gfx::Rect(5, 4, 20, 10), v1->bounds());
+ EXPECT_EQ(gfx::Rect(5, 20, 20, 10), v2->bounds());
+}
+
+TEST_F(BoxLayoutTest, OverlappingCrossMarginsAlignEnd) {
+ {
+ BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, false);
+ host_->SetLayoutManager(layout);
+ layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END);
+ View* v1 = new StaticSizedView(gfx::Size(20, 4));
+ v1->SetProperty(kMarginsKey, new gfx::Insets(3, 0, 0, 0));
+ host_->AddChildView(v1);
+ View* v2 = new StaticSizedView(gfx::Size(20, 5));
+ v2->SetProperty(kMarginsKey, new gfx::Insets(0, 0, 2, 0));
+ host_->AddChildView(v2);
+
+ EXPECT_EQ(9, layout->GetPreferredSize(host_.get()).height());
+ }
+ host_->RemoveAllChildViews(true);
+ {
+ BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, true);
+ host_->SetLayoutManager(layout);
+ layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END);
+ View* v1 = new StaticSizedView(gfx::Size(20, 4));
+ v1->SetProperty(kMarginsKey, new gfx::Insets(3, 0, 0, 0));
+ host_->AddChildView(v1);
+ View* v2 = new StaticSizedView(gfx::Size(20, 5));
+ v2->SetProperty(kMarginsKey, new gfx::Insets(0, 0, 2, 0));
+ host_->AddChildView(v2);
+
+ EXPECT_EQ(9, layout->GetPreferredSize(host_.get()).height());
+ }
+}
+
+TEST_F(BoxLayoutTest, OverlappingCrossMarginsAlignStretch) {
+ {
+ BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, false);
+ host_->SetLayoutManager(layout);
+ layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH);
+ View* v1 = new StaticSizedView(gfx::Size(20, 4));
+ v1->SetProperty(kMarginsKey, new gfx::Insets(3, 0, 0, 0));
+ host_->AddChildView(v1);
+ View* v2 = new StaticSizedView(gfx::Size(20, 5));
+ v2->SetProperty(kMarginsKey, new gfx::Insets(0, 0, 2, 0));
+ host_->AddChildView(v2);
+
+ EXPECT_EQ(10, layout->GetPreferredSize(host_.get()).height());
+ }
+ host_->RemoveAllChildViews(true);
+ {
+ BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, true);
+ host_->SetLayoutManager(layout);
+ layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH);
+ View* v1 = new StaticSizedView(gfx::Size(20, 4));
+ v1->SetProperty(kMarginsKey, new gfx::Insets(3, 0, 0, 0));
+ host_->AddChildView(v1);
+ View* v2 = new StaticSizedView(gfx::Size(20, 5));
+ v2->SetProperty(kMarginsKey, new gfx::Insets(0, 0, 2, 0));
+ host_->AddChildView(v2);
+
+ EXPECT_EQ(10, layout->GetPreferredSize(host_.get()).height());
+ }
+}
+
+TEST_F(BoxLayoutTest, OverlappingCrossMarginsAlignStart) {
+ {
+ BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, false);
+ host_->SetLayoutManager(layout);
+ layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_START);
+ View* v1 = new StaticSizedView(gfx::Size(20, 4));
+ v1->SetProperty(kMarginsKey, new gfx::Insets(0, 0, 3, 0));
+ host_->AddChildView(v1);
+ View* v2 = new StaticSizedView(gfx::Size(20, 5));
+ v2->SetProperty(kMarginsKey, new gfx::Insets(2, 0, 0, 0));
+ host_->AddChildView(v2);
+
+ EXPECT_EQ(9, layout->GetPreferredSize(host_.get()).height());
+ }
+ host_->RemoveAllChildViews(true);
+ {
+ BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, true);
+ host_->SetLayoutManager(layout);
+ layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_START);
+ View* v1 = new StaticSizedView(gfx::Size(20, 4));
+ v1->SetProperty(kMarginsKey, new gfx::Insets(0, 0, 3, 0));
+ host_->AddChildView(v1);
+ View* v2 = new StaticSizedView(gfx::Size(20, 5));
+ v2->SetProperty(kMarginsKey, new gfx::Insets(2, 0, 0, 0));
+ host_->AddChildView(v2);
+
+ EXPECT_EQ(9, layout->GetPreferredSize(host_.get()).height());
+ }
+}
+
} // namespace views

Powered by Google App Engine
This is Rietveld 408576698