Index: ui/views/layout/anchor_layout_unittest.cc |
diff --git a/ui/views/layout/anchor_layout_unittest.cc b/ui/views/layout/anchor_layout_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e3da2d96a0806a363bd8980a668c47c7ea39dd00 |
--- /dev/null |
+++ b/ui/views/layout/anchor_layout_unittest.cc |
@@ -0,0 +1,341 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <stddef.h> |
+ |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "ui/views/anchor_attribute.h" |
+#include "ui/views/fill_attribute.h" |
+#include "ui/views/layout/align_layout.h" |
+#include "ui/views/test/test_views.h" |
+#include "ui/views/view.h" |
+ |
+namespace views { |
+ |
+namespace { |
+ |
+class AnchorLayoutTest : public testing::Test { |
+ public: |
+ void SetUp() override { |
+ host_.reset(new View); |
+ host_->SetBounds(0, 0, 100, 100); |
+ } |
+ |
+ std::unique_ptr<View> host_; |
+}; |
+ |
+} // namespace |
+ |
+TEST_F(AnchorLayoutTest, Empty) { |
+ AlignLayout* layout = new AlignLayout(); |
+ host_->SetLayoutManager(layout); |
+ EXPECT_EQ(gfx::Size(0, 0), layout->GetPreferredSize(host_.get())); |
+} |
+ |
+TEST_F(AnchorLayoutTest, AnchorLeftTop) { |
+ AlignLayout* layout = new AlignLayout(); |
+ host_->SetLayoutManager(layout); |
+ View* v1 = new View(); |
+ v1->attributes().Add( |
+ base::WrapUnique(new AnchorAttribute({Anchor::Left, Anchor::Top}))); |
+ gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
+ v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
+ host_->AddChildView(v1); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
+ host_->SetBounds(0, 0, 120, 120); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
+} |
+ |
+TEST_F(AnchorLayoutTest, AnchorTop) { |
+ AlignLayout* layout = new AlignLayout(); |
+ host_->SetLayoutManager(layout); |
+ View* v1 = new View(); |
+ v1->attributes().Add(base::WrapUnique(new AnchorAttribute({Anchor::Top}))); |
+ gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
+ v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
+ host_->AddChildView(v1); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
+ host_->SetBounds(0, 0, 120, 120); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(v1pos.x() + 10, v1pos.y(), 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
+} |
+ |
+TEST_F(AnchorLayoutTest, AnchorLeft) { |
+ AlignLayout* layout = new AlignLayout(); |
+ host_->SetLayoutManager(layout); |
+ View* v1 = new View(); |
+ v1->attributes().Add(base::WrapUnique(new AnchorAttribute({Anchor::Left}))); |
+ gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
+ v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
+ host_->AddChildView(v1); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
+ host_->SetBounds(0, 0, 120, 120); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y() + 10, 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
+} |
+ |
+TEST_F(AnchorLayoutTest, AnchorRightTop) { |
+ AlignLayout* layout = new AlignLayout(); |
+ host_->SetLayoutManager(layout); |
+ View* v1 = new View(); |
+ v1->attributes().Add( |
+ base::WrapUnique(new AnchorAttribute({Anchor::Right, Anchor::Top}))); |
+ gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
+ v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
+ host_->AddChildView(v1); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
+ host_->SetBounds(0, 0, 120, 120); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(v1pos.x() + 20, v1pos.y(), 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
+} |
+ |
+TEST_F(AnchorLayoutTest, AnchorLeftTopRight) { |
+ AlignLayout* layout = new AlignLayout(); |
+ host_->SetLayoutManager(layout); |
+ View* v1 = new View(); |
+ v1->attributes().Add(base::WrapUnique( |
+ new AnchorAttribute({Anchor::Left, Anchor::Top, Anchor::Right}))); |
+ gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
+ v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
+ host_->AddChildView(v1); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
+ host_->SetBounds(0, 0, 120, 120); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 30, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
+} |
+ |
+TEST_F(AnchorLayoutTest, AnchorLeftTopBottom) { |
+ AlignLayout* layout = new AlignLayout(); |
+ host_->SetLayoutManager(layout); |
+ View* v1 = new View(); |
+ v1->attributes().Add(base::WrapUnique( |
+ new AnchorAttribute({Anchor::Left, Anchor::Top, Anchor::Bottom}))); |
+ gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
+ v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
+ host_->AddChildView(v1); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
+ host_->SetBounds(0, 0, 120, 120); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 30), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
+} |
+ |
+TEST_F(AnchorLayoutTest, AnchorLeftRight) { |
+ AlignLayout* layout = new AlignLayout(); |
+ host_->SetLayoutManager(layout); |
+ View* v1 = new View(); |
+ v1->attributes().Add(base::WrapUnique( |
+ new AnchorAttribute({Anchor::Left, Anchor::Right}))); |
+ gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
+ v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
+ host_->AddChildView(v1); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
+ host_->SetBounds(0, 0, 120, 120); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y() + 10, 30, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
+} |
+ |
+TEST_F(AnchorLayoutTest, AnchorTopBottom) { |
+ AlignLayout* layout = new AlignLayout(); |
+ host_->SetLayoutManager(layout); |
+ View* v1 = new View(); |
+ v1->attributes().Add(base::WrapUnique( |
+ new AnchorAttribute({Anchor::Top, Anchor::Bottom}))); |
+ gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
+ v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
+ host_->AddChildView(v1); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
+ host_->SetBounds(0, 0, 120, 120); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(v1pos.x() + 10, v1pos.y(), 10, 30), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
+} |
+ |
+TEST_F(AnchorLayoutTest, AnchorNone) { |
+ AlignLayout* layout = new AlignLayout(); |
+ host_->SetLayoutManager(layout); |
+ View* v1 = new View(); |
+ v1->attributes().Add(base::WrapUnique(new AnchorAttribute(Anchors()))); |
+ gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
+ v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
+ host_->AddChildView(v1); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
+ host_->SetBounds(0, 0, 120, 120); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(v1pos.x() + 10, v1pos.y() + 10, 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
+} |
+ |
+TEST_F(AnchorLayoutTest, AnchorRightBottom) { |
+ AlignLayout* layout = new AlignLayout(); |
+ host_->SetLayoutManager(layout); |
+ View* v1 = new View(); |
+ v1->attributes().Add(base::WrapUnique( |
+ new AnchorAttribute({Anchor::Right, Anchor::Bottom}))); |
+ gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
+ v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
+ host_->AddChildView(v1); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
+ host_->SetBounds(0, 0, 120, 120); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(v1pos.x() + 20, v1pos.y() + 20, 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
+} |
+ |
+TEST_F(AnchorLayoutTest, AnchorRight) { |
+ AlignLayout* layout = new AlignLayout(); |
+ host_->SetLayoutManager(layout); |
+ View* v1 = new View(); |
+ v1->attributes().Add(base::WrapUnique(new AnchorAttribute({Anchor::Right}))); |
+ gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
+ v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
+ host_->AddChildView(v1); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
+ host_->SetBounds(0, 0, 120, 120); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(v1pos.x() + 20, v1pos.y() + 10, 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
+} |
+ |
+TEST_F(AnchorLayoutTest, AnchorBottom) { |
+ AlignLayout* layout = new AlignLayout(); |
+ host_->SetLayoutManager(layout); |
+ View* v1 = new View(); |
+ v1->attributes().Add(base::WrapUnique(new AnchorAttribute({Anchor::Bottom}))); |
+ gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
+ v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
+ host_->AddChildView(v1); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
+ host_->SetBounds(0, 0, 120, 120); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(v1pos.x() + 10, v1pos.y() + 20, 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
+} |
+ |
+TEST_F(AnchorLayoutTest, AnchorTopRightBottom) { |
+ AlignLayout* layout = new AlignLayout(); |
+ host_->SetLayoutManager(layout); |
+ View* v1 = new View(); |
+ v1->attributes().Add(base::WrapUnique( |
+ new AnchorAttribute({Anchor::Top, Anchor::Right, Anchor::Bottom}))); |
+ gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
+ v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
+ host_->AddChildView(v1); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
+ host_->SetBounds(0, 0, 120, 120); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(v1pos.x() + 20, v1pos.y(), 10, 30), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
+} |
+ |
+TEST_F(AnchorLayoutTest, AnchorLeftTopRightBottom) { |
+ AlignLayout* layout = new AlignLayout(); |
+ host_->SetLayoutManager(layout); |
+ View* v1 = new View(); |
+ v1->attributes().Add(base::WrapUnique(new AnchorAttribute( |
+ {Anchor::Left, Anchor::Top, Anchor::Right, Anchor::Bottom}))); |
+ gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
+ v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
+ host_->AddChildView(v1); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
+ host_->SetBounds(0, 0, 120, 120); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 30, 30), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
+} |
+ |
+TEST_F(AnchorLayoutTest, FillTopAnchorAll) { |
+ AlignLayout* layout = new AlignLayout(); |
+ host_->SetLayoutManager(layout); |
+ View* v1 = new View(); |
+ v1->attributes().Add(base::WrapUnique(new FillAttribute(Fill::Top))); |
+ v1->attributes().Add(base::WrapUnique(new AnchorAttribute( |
+ {Anchor::Left, Anchor::Top, Anchor::Right, Anchor::Bottom}))); |
+ v1->SetBounds(0, 0, 10, 10); |
+ host_->AddChildView(v1); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
+ host_->SetBounds(0, 0, 120, 120); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(0, 0, 120, 30), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
+} |
+ |
+TEST_F(AnchorLayoutTest, FillBottomAnchorAll) { |
+ AlignLayout* layout = new AlignLayout(); |
+ host_->SetLayoutManager(layout); |
+ View* v1 = new View(); |
+ v1->attributes().Add(base::WrapUnique(new FillAttribute(Fill::Bottom))); |
+ v1->attributes().Add(base::WrapUnique(new AnchorAttribute( |
+ {Anchor::Left, Anchor::Top, Anchor::Right, Anchor::Bottom}))); |
+ v1->SetBounds(0, 0, 10, 10); |
+ host_->AddChildView(v1); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(0, 90, 100, 10), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
+ host_->SetBounds(0, 0, 120, 120); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(0, 90, 120, 30), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
+} |
+ |
+TEST_F(AnchorLayoutTest, FillLeftAnchorAll) { |
+ AlignLayout* layout = new AlignLayout(); |
+ host_->SetLayoutManager(layout); |
+ View* v1 = new View(); |
+ v1->attributes().Add(base::WrapUnique(new FillAttribute(Fill::Left))); |
+ v1->attributes().Add(base::WrapUnique(new AnchorAttribute( |
+ {Anchor::Left, Anchor::Top, Anchor::Right, Anchor::Bottom}))); |
+ v1->SetBounds(0, 0, 10, 10); |
+ host_->AddChildView(v1); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(0, 0, 10, 100), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
+ host_->SetBounds(0, 0, 120, 120); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(0, 0, 30, 120), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
+} |
+ |
+TEST_F(AnchorLayoutTest, FillRightAnchorAll) { |
+ AlignLayout* layout = new AlignLayout(); |
+ host_->SetLayoutManager(layout); |
+ View* v1 = new View(); |
+ v1->attributes().Add(base::WrapUnique(new FillAttribute(Fill::Right))); |
+ v1->attributes().Add(base::WrapUnique(new AnchorAttribute( |
+ {Anchor::Left, Anchor::Top, Anchor::Right, Anchor::Bottom}))); |
+ v1->SetBounds(0, 0, 10, 10); |
+ host_->AddChildView(v1); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(90, 0, 10, 100), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
+ host_->SetBounds(0, 0, 120, 120); |
+ host_->Layout(); |
+ EXPECT_EQ(gfx::Rect(90, 0, 30, 120), v1->bounds()); |
+ EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
+} |
+ |
+} // namespace views |