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.
4
5 #include "ui/views/layout/box_layout.h"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/views/test/test_views.h"
9 #include "ui/views/view.h"
10
11 namespace views {
12
13 namespace {
14
15 class BoxLayoutTest : public testing::Test {
16 public:
SetUp()17 virtual void SetUp() OVERRIDE {
18 host_.reset(new View);
19 }
20
21 scoped_ptr<View> host_;
22 scoped_ptr<BoxLayout> layout_;
23 };
24
25 } // namespace
26
TEST_F(BoxLayoutTest,Empty)27 TEST_F(BoxLayoutTest, Empty) {
28 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 20));
29 EXPECT_EQ(gfx::Size(20, 20), layout_->GetPreferredSize(host_.get()));
30 }
31
TEST_F(BoxLayoutTest,AlignmentHorizontal)32 TEST_F(BoxLayoutTest, AlignmentHorizontal) {
33 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0));
34 View* v1 = new StaticSizedView(gfx::Size(10, 20));
35 host_->AddChildView(v1);
36 View* v2 = new StaticSizedView(gfx::Size(10, 10));
37 host_->AddChildView(v2);
38 EXPECT_EQ(gfx::Size(20, 20), layout_->GetPreferredSize(host_.get()));
39 host_->SetBounds(0, 0, 20, 20);
40 layout_->Layout(host_.get());
41 EXPECT_EQ(gfx::Rect(0, 0, 10, 20), v1->bounds());
42 EXPECT_EQ(gfx::Rect(10, 0, 10, 20), v2->bounds());
43 }
44
TEST_F(BoxLayoutTest,AlignmentVertical)45 TEST_F(BoxLayoutTest, AlignmentVertical) {
46 layout_.reset(new BoxLayout(BoxLayout::kVertical, 0, 0, 0));
47 View* v1 = new StaticSizedView(gfx::Size(20, 10));
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, 20, 10), v1->bounds());
55 EXPECT_EQ(gfx::Rect(0, 10, 20, 10), v2->bounds());
56 }
57
TEST_F(BoxLayoutTest,SetInsideBorderInsets)58 TEST_F(BoxLayoutTest, SetInsideBorderInsets) {
59 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 20, 0));
60 View* v1 = new StaticSizedView(gfx::Size(10, 20));
61 host_->AddChildView(v1);
62 View* v2 = new StaticSizedView(gfx::Size(10, 10));
63 host_->AddChildView(v2);
64 EXPECT_EQ(gfx::Size(40, 60), layout_->GetPreferredSize(host_.get()));
65 host_->SetBounds(0, 0, 40, 60);
66 layout_->Layout(host_.get());
67 EXPECT_EQ(gfx::Rect(10, 20, 10, 20), v1->bounds());
68 EXPECT_EQ(gfx::Rect(20, 20, 10, 20), v2->bounds());
69
70 layout_->set_inside_border_insets(
71 gfx::Insets(5, 10, 15, 20));
72 EXPECT_EQ(gfx::Size(50, 40), layout_->GetPreferredSize(host_.get()));
73 host_->SetBounds(0, 0, 50, 40);
74 layout_->Layout(host_.get());
75 EXPECT_EQ(gfx::Rect(10, 5, 10, 20), v1->bounds());
76 EXPECT_EQ(gfx::Rect(20, 5, 10, 20), v2->bounds());
77 }
78
TEST_F(BoxLayoutTest,Spacing)79 TEST_F(BoxLayoutTest, Spacing) {
80 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 7, 7, 8));
81 View* v1 = new StaticSizedView(gfx::Size(10, 20));
82 host_->AddChildView(v1);
83 View* v2 = new StaticSizedView(gfx::Size(10, 20));
84 host_->AddChildView(v2);
85 EXPECT_EQ(gfx::Size(42, 34), layout_->GetPreferredSize(host_.get()));
86 host_->SetBounds(0, 0, 100, 100);
87 layout_->Layout(host_.get());
88 EXPECT_EQ(gfx::Rect(7, 7, 10, 86), v1->bounds());
89 EXPECT_EQ(gfx::Rect(25, 7, 10, 86), v2->bounds());
90 }
91
TEST_F(BoxLayoutTest,Overflow)92 TEST_F(BoxLayoutTest, Overflow) {
93 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0));
94 View* v1 = new StaticSizedView(gfx::Size(20, 20));
95 host_->AddChildView(v1);
96 View* v2 = new StaticSizedView(gfx::Size(10, 20));
97 host_->AddChildView(v2);
98 host_->SetBounds(0, 0, 10, 10);
99
100 // Overflows by positioning views at the start and truncating anything that
101 // doesn't fit.
102 layout_->Layout(host_.get());
103 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds());
104 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds());
105
106 // All values of main axis alignment should overflow in the same manner.
107 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START);
108 layout_->Layout(host_.get());
109 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds());
110 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds());
111
112 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
113 layout_->Layout(host_.get());
114 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds());
115 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds());
116
117 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END);
118 layout_->Layout(host_.get());
119 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds());
120 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds());
121 }
122
TEST_F(BoxLayoutTest,NoSpace)123 TEST_F(BoxLayoutTest, NoSpace) {
124 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10));
125 View* childView = new StaticSizedView(gfx::Size(20, 20));
126 host_->AddChildView(childView);
127 host_->SetBounds(0, 0, 10, 10);
128 layout_->Layout(host_.get());
129 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), childView->bounds());
130 }
131
TEST_F(BoxLayoutTest,InvisibleChild)132 TEST_F(BoxLayoutTest, InvisibleChild) {
133 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10));
134 View* v1 = new StaticSizedView(gfx::Size(20, 20));
135 v1->SetVisible(false);
136 host_->AddChildView(v1);
137 View* v2 = new StaticSizedView(gfx::Size(10, 10));
138 host_->AddChildView(v2);
139 EXPECT_EQ(gfx::Size(30, 30), layout_->GetPreferredSize(host_.get()));
140 host_->SetBounds(0, 0, 30, 30);
141 layout_->Layout(host_.get());
142 EXPECT_EQ(gfx::Rect(10, 10, 10, 10), v2->bounds());
143 }
144
TEST_F(BoxLayoutTest,MainAxisAlignmentFill)145 TEST_F(BoxLayoutTest, MainAxisAlignmentFill) {
146 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10));
147 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_FILL);
148
149 View* v1 = new StaticSizedView(gfx::Size(20, 20));
150 host_->AddChildView(v1);
151 View* v2 = new StaticSizedView(gfx::Size(10, 10));
152 host_->AddChildView(v2);
153 EXPECT_EQ(gfx::Size(60, 40), layout_->GetPreferredSize(host_.get()));
154
155 host_->SetBounds(0, 0, 100, 40);
156 layout_->Layout(host_.get());
157 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1->bounds().ToString());
158 EXPECT_EQ(gfx::Rect(60, 10, 30, 20).ToString(), v2->bounds().ToString());
159 }
160
TEST_F(BoxLayoutTest,UseHeightForWidth)161 TEST_F(BoxLayoutTest, UseHeightForWidth) {
162 layout_.reset(new BoxLayout(BoxLayout::kVertical, 0, 0, 0));
163 View* v1 = new StaticSizedView(gfx::Size(20, 10));
164 host_->AddChildView(v1);
165 View* v2 = new ProportionallySizedView(2);
166 host_->AddChildView(v2);
167 EXPECT_EQ(gfx::Size(20, 50), layout_->GetPreferredSize(host_.get()));
168
169 host_->SetBounds(0, 0, 20, 50);
170 layout_->Layout(host_.get());
171 EXPECT_EQ(gfx::Rect(0, 0, 20, 10), v1->bounds());
172 EXPECT_EQ(gfx::Rect(0, 10, 20, 40), v2->bounds());
173
174 EXPECT_EQ(110, layout_->GetPreferredHeightForWidth(host_.get(), 50));
175 }
176
TEST_F(BoxLayoutTest,EmptyPreferredSize)177 TEST_F(BoxLayoutTest, EmptyPreferredSize) {
178 for (size_t i = 0; i < 2; i++) {
179 BoxLayout::Orientation orientation = i == 0 ? BoxLayout::kHorizontal :
180 BoxLayout::kVertical;
181 host_->RemoveAllChildViews(true);
182 host_->SetLayoutManager(new BoxLayout(orientation, 0, 0, 5));
183 View* v1 = new StaticSizedView(gfx::Size());
184 host_->AddChildView(v1);
185 View* v2 = new StaticSizedView(gfx::Size(10, 10));
186 host_->AddChildView(v2);
187 host_->SizeToPreferredSize();
188 host_->Layout();
189
190 EXPECT_EQ(v2->GetPreferredSize().width(), host_->bounds().width()) << i;
191 EXPECT_EQ(v2->GetPreferredSize().height(), host_->bounds().height()) << i;
192 EXPECT_EQ(v1->GetPreferredSize().width(), v1->bounds().width()) << i;
193 EXPECT_EQ(v1->GetPreferredSize().height(), v1->bounds().height()) << i;
194 EXPECT_EQ(v2->GetPreferredSize().width(), v2->bounds().width()) << i;
195 EXPECT_EQ(v2->GetPreferredSize().height(), v2->bounds().height()) << i;
196 }
197 }
198
TEST_F(BoxLayoutTest,MainAxisAlignmentHorizontal)199 TEST_F(BoxLayoutTest, MainAxisAlignmentHorizontal) {
200 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10));
201
202 View* v1 = new StaticSizedView(gfx::Size(20, 20));
203 host_->AddChildView(v1);
204 View* v2 = new StaticSizedView(gfx::Size(10, 10));
205 host_->AddChildView(v2);
206
207 host_->SetBounds(0, 0, 100, 40);
208
209 // Align children to the horizontal start by default.
210 layout_->Layout(host_.get());
211 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString());
212 EXPECT_EQ(gfx::Rect(40, 10, 10, 20).ToString(), v2->bounds().ToString());
213
214 // Ensure same results for MAIN_AXIS_ALIGNMENT_START.
215 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START);
216 layout_->Layout(host_.get());
217 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString());
218 EXPECT_EQ(gfx::Rect(40, 10, 10, 20).ToString(), v2->bounds().ToString());
219
220 // Aligns children to the center horizontally.
221 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
222 layout_->Layout(host_.get());
223 EXPECT_EQ(gfx::Rect(30, 10, 20, 20).ToString(), v1->bounds().ToString());
224 EXPECT_EQ(gfx::Rect(60, 10, 10, 20).ToString(), v2->bounds().ToString());
225
226 // Aligns children to the end of the host horizontally, accounting for the
227 // inside border spacing.
228 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END);
229 layout_->Layout(host_.get());
230 EXPECT_EQ(gfx::Rect(50, 10, 20, 20).ToString(), v1->bounds().ToString());
231 EXPECT_EQ(gfx::Rect(80, 10, 10, 20).ToString(), v2->bounds().ToString());
232 }
233
TEST_F(BoxLayoutTest,MainAxisAlignmentVertical)234 TEST_F(BoxLayoutTest, MainAxisAlignmentVertical) {
235 layout_.reset(new BoxLayout(BoxLayout::kVertical, 10, 10, 10));
236
237 View* v1 = new StaticSizedView(gfx::Size(20, 20));
238 host_->AddChildView(v1);
239 View* v2 = new StaticSizedView(gfx::Size(10, 10));
240 host_->AddChildView(v2);
241
242 host_->SetBounds(0, 0, 40, 100);
243
244 // Align children to the vertical start by default.
245 layout_->Layout(host_.get());
246 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString());
247 EXPECT_EQ(gfx::Rect(10, 40, 20, 10).ToString(), v2->bounds().ToString());
248
249 // Ensure same results for MAIN_AXIS_ALIGNMENT_START.
250 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START);
251 layout_->Layout(host_.get());
252 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString());
253 EXPECT_EQ(gfx::Rect(10, 40, 20, 10).ToString(), v2->bounds().ToString());
254
255 // Aligns children to the center vertically.
256 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
257 layout_->Layout(host_.get());
258 EXPECT_EQ(gfx::Rect(10, 30, 20, 20).ToString(), v1->bounds().ToString());
259 EXPECT_EQ(gfx::Rect(10, 60, 20, 10).ToString(), v2->bounds().ToString());
260
261 // Aligns children to the end of the host vertically, accounting for the
262 // inside border spacing.
263 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END);
264 layout_->Layout(host_.get());
265 EXPECT_EQ(gfx::Rect(10, 50, 20, 20).ToString(), v1->bounds().ToString());
266 EXPECT_EQ(gfx::Rect(10, 80, 20, 10).ToString(), v2->bounds().ToString());
267 }
268
269 } // namespace views
270