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
3 // found in the LICENSE file.
5 #include "ui/views/layout/box_layout.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/views/view.h"
14 class StaticSizedView
: public View
{
16 explicit StaticSizedView(const gfx::Size
& size
)
20 virtual gfx::Size
GetPreferredSize() OVERRIDE
{
28 class BoxLayoutTest
: public testing::Test
{
30 virtual void SetUp() OVERRIDE
{
31 host_
.reset(new View
);
34 scoped_ptr
<View
> host_
;
35 scoped_ptr
<BoxLayout
> layout_
;
40 TEST_F(BoxLayoutTest
, Empty
) {
41 layout_
.reset(new BoxLayout(BoxLayout::kHorizontal
, 10, 10, 20));
42 EXPECT_EQ(gfx::Size(20, 20), layout_
->GetPreferredSize(host_
.get()));
45 TEST_F(BoxLayoutTest
, AlignmentHorizontal
) {
46 layout_
.reset(new BoxLayout(BoxLayout::kHorizontal
, 0, 0, 0));
47 View
* v1
= new StaticSizedView(gfx::Size(10, 20));
48 host_
->AddChildView(v1
);
49 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
50 host_
->AddChildView(v2
);
51 EXPECT_EQ(gfx::Size(20, 20), layout_
->GetPreferredSize(host_
.get()));
52 host_
->SetBounds(0, 0, 20, 20);
53 layout_
->Layout(host_
.get());
54 EXPECT_EQ(gfx::Rect(0, 0, 10, 20), v1
->bounds());
55 EXPECT_EQ(gfx::Rect(10, 0, 10, 20), v2
->bounds());
58 TEST_F(BoxLayoutTest
, AlignmentVertical
) {
59 layout_
.reset(new BoxLayout(BoxLayout::kVertical
, 0, 0, 0));
60 View
* v1
= new StaticSizedView(gfx::Size(20, 10));
61 host_
->AddChildView(v1
);
62 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
63 host_
->AddChildView(v2
);
64 EXPECT_EQ(gfx::Size(20, 20), layout_
->GetPreferredSize(host_
.get()));
65 host_
->SetBounds(0, 0, 20, 20);
66 layout_
->Layout(host_
.get());
67 EXPECT_EQ(gfx::Rect(0, 0, 20, 10), v1
->bounds());
68 EXPECT_EQ(gfx::Rect(0, 10, 20, 10), v2
->bounds());
71 TEST_F(BoxLayoutTest
, Spacing
) {
72 layout_
.reset(new BoxLayout(BoxLayout::kHorizontal
, 7, 7, 8));
73 View
* v1
= new StaticSizedView(gfx::Size(10, 20));
74 host_
->AddChildView(v1
);
75 View
* v2
= new StaticSizedView(gfx::Size(10, 20));
76 host_
->AddChildView(v2
);
77 EXPECT_EQ(gfx::Size(42, 34), layout_
->GetPreferredSize(host_
.get()));
78 host_
->SetBounds(0, 0, 100, 100);
79 layout_
->Layout(host_
.get());
80 EXPECT_EQ(gfx::Rect(7, 7, 10, 86), v1
->bounds());
81 EXPECT_EQ(gfx::Rect(25, 7, 10, 86), v2
->bounds());
84 TEST_F(BoxLayoutTest
, Overflow
) {
85 layout_
.reset(new BoxLayout(BoxLayout::kHorizontal
, 0, 0, 0));
86 View
* v1
= new StaticSizedView(gfx::Size(20, 20));
87 host_
->AddChildView(v1
);
88 View
* v2
= new StaticSizedView(gfx::Size(10, 20));
89 host_
->AddChildView(v2
);
90 host_
->SetBounds(0, 0, 10, 10);
91 layout_
->Layout(host_
.get());
92 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1
->bounds());
93 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2
->bounds());
96 TEST_F(BoxLayoutTest
, NoSpace
) {
97 layout_
.reset(new BoxLayout(BoxLayout::kHorizontal
, 10, 10, 10));
98 View
* childView
= new StaticSizedView(gfx::Size(20, 20));
99 host_
->AddChildView(childView
);
100 host_
->SetBounds(0, 0, 10, 10);
101 layout_
->Layout(host_
.get());
102 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), childView
->bounds());
105 TEST_F(BoxLayoutTest
, InvisibleChild
) {
106 layout_
.reset(new BoxLayout(BoxLayout::kHorizontal
, 10, 10, 10));
107 View
* v1
= new StaticSizedView(gfx::Size(20, 20));
108 v1
->SetVisible(false);
109 host_
->AddChildView(v1
);
110 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
111 host_
->AddChildView(v2
);
112 EXPECT_EQ(gfx::Size(30, 30), layout_
->GetPreferredSize(host_
.get()));
113 host_
->SetBounds(0, 0, 30, 30);
114 layout_
->Layout(host_
.get());
115 EXPECT_EQ(gfx::Rect(10, 10, 10, 10), v2
->bounds());
118 TEST_F(BoxLayoutTest
, DistributeEmptySpace
) {
119 layout_
.reset(new BoxLayout(BoxLayout::kHorizontal
, 10, 10, 10));
120 layout_
->set_spread_blank_space(true);
122 View
* v1
= new StaticSizedView(gfx::Size(20, 20));
123 host_
->AddChildView(v1
);
124 View
* v2
= new StaticSizedView(gfx::Size(10, 10));
125 host_
->AddChildView(v2
);
126 EXPECT_EQ(gfx::Size(60, 40), layout_
->GetPreferredSize(host_
.get()));
128 host_
->SetBounds(0, 0, 100, 40);
129 layout_
->Layout(host_
.get());
130 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1
->bounds().ToString());
131 EXPECT_EQ(gfx::Rect(60, 10, 30, 20).ToString(), v2
->bounds().ToString());