OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/layout/box_layout.h" | 5 #include "ui/views/layout/box_layout.h" |
6 | 6 |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "ui/views/test/test_views.h" | 8 #include "ui/views/test/test_views.h" |
9 #include "ui/views/view.h" | 9 #include "ui/views/view.h" |
10 | 10 |
11 namespace views { | 11 namespace views { |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 class BoxLayoutTest : public testing::Test { | 15 class BoxLayoutTest : public testing::Test { |
16 public: | 16 public: |
17 virtual void SetUp() OVERRIDE { | 17 virtual void SetUp() OVERRIDE { |
18 host_.reset(new View); | 18 host_.reset(new View); |
19 } | 19 } |
20 | 20 |
21 scoped_ptr<View> host_; | 21 scoped_ptr<View> host_; |
22 scoped_ptr<BoxLayout> layout_; | |
23 }; | 22 }; |
24 | 23 |
25 } // namespace | 24 } // namespace |
26 | 25 |
27 TEST_F(BoxLayoutTest, Empty) { | 26 TEST_F(BoxLayoutTest, Empty) { |
28 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 20)); | 27 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 10, 10, 20); |
29 EXPECT_EQ(gfx::Size(20, 20), layout_->GetPreferredSize(host_.get())); | 28 host_->SetLayoutManager(layout); |
| 29 EXPECT_EQ(gfx::Size(20, 20), layout->GetPreferredSize(host_.get())); |
30 } | 30 } |
31 | 31 |
32 TEST_F(BoxLayoutTest, AlignmentHorizontal) { | 32 TEST_F(BoxLayoutTest, AlignmentHorizontal) { |
33 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0)); | 33 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0); |
| 34 host_->SetLayoutManager(layout); |
34 View* v1 = new StaticSizedView(gfx::Size(10, 20)); | 35 View* v1 = new StaticSizedView(gfx::Size(10, 20)); |
35 host_->AddChildView(v1); | 36 host_->AddChildView(v1); |
36 View* v2 = new StaticSizedView(gfx::Size(10, 10)); | 37 View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
37 host_->AddChildView(v2); | 38 host_->AddChildView(v2); |
38 EXPECT_EQ(gfx::Size(20, 20), layout_->GetPreferredSize(host_.get())); | 39 EXPECT_EQ(gfx::Size(20, 20), layout->GetPreferredSize(host_.get())); |
39 host_->SetBounds(0, 0, 20, 20); | 40 host_->SetBounds(0, 0, 20, 20); |
40 layout_->Layout(host_.get()); | 41 host_->Layout(); |
41 EXPECT_EQ(gfx::Rect(0, 0, 10, 20), v1->bounds()); | 42 EXPECT_EQ(gfx::Rect(0, 0, 10, 20), v1->bounds()); |
42 EXPECT_EQ(gfx::Rect(10, 0, 10, 20), v2->bounds()); | 43 EXPECT_EQ(gfx::Rect(10, 0, 10, 20), v2->bounds()); |
43 } | 44 } |
44 | 45 |
45 TEST_F(BoxLayoutTest, AlignmentVertical) { | 46 TEST_F(BoxLayoutTest, AlignmentVertical) { |
46 layout_.reset(new BoxLayout(BoxLayout::kVertical, 0, 0, 0)); | 47 BoxLayout* layout = new BoxLayout(BoxLayout::kVertical, 0, 0, 0); |
| 48 host_->SetLayoutManager(layout); |
47 View* v1 = new StaticSizedView(gfx::Size(20, 10)); | 49 View* v1 = new StaticSizedView(gfx::Size(20, 10)); |
48 host_->AddChildView(v1); | 50 host_->AddChildView(v1); |
49 View* v2 = new StaticSizedView(gfx::Size(10, 10)); | 51 View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
50 host_->AddChildView(v2); | 52 host_->AddChildView(v2); |
51 EXPECT_EQ(gfx::Size(20, 20), layout_->GetPreferredSize(host_.get())); | 53 EXPECT_EQ(gfx::Size(20, 20), layout->GetPreferredSize(host_.get())); |
52 host_->SetBounds(0, 0, 20, 20); | 54 host_->SetBounds(0, 0, 20, 20); |
53 layout_->Layout(host_.get()); | 55 host_->Layout(); |
54 EXPECT_EQ(gfx::Rect(0, 0, 20, 10), v1->bounds()); | 56 EXPECT_EQ(gfx::Rect(0, 0, 20, 10), v1->bounds()); |
55 EXPECT_EQ(gfx::Rect(0, 10, 20, 10), v2->bounds()); | 57 EXPECT_EQ(gfx::Rect(0, 10, 20, 10), v2->bounds()); |
56 } | 58 } |
57 | 59 |
58 TEST_F(BoxLayoutTest, SetInsideBorderInsets) { | 60 TEST_F(BoxLayoutTest, SetInsideBorderInsets) { |
59 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 20, 0)); | 61 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 10, 20, 0); |
| 62 host_->SetLayoutManager(layout); |
60 View* v1 = new StaticSizedView(gfx::Size(10, 20)); | 63 View* v1 = new StaticSizedView(gfx::Size(10, 20)); |
61 host_->AddChildView(v1); | 64 host_->AddChildView(v1); |
62 View* v2 = new StaticSizedView(gfx::Size(10, 10)); | 65 View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
63 host_->AddChildView(v2); | 66 host_->AddChildView(v2); |
64 EXPECT_EQ(gfx::Size(40, 60), layout_->GetPreferredSize(host_.get())); | 67 EXPECT_EQ(gfx::Size(40, 60), layout->GetPreferredSize(host_.get())); |
65 host_->SetBounds(0, 0, 40, 60); | 68 host_->SetBounds(0, 0, 40, 60); |
66 layout_->Layout(host_.get()); | 69 host_->Layout(); |
67 EXPECT_EQ(gfx::Rect(10, 20, 10, 20), v1->bounds()); | 70 EXPECT_EQ(gfx::Rect(10, 20, 10, 20), v1->bounds()); |
68 EXPECT_EQ(gfx::Rect(20, 20, 10, 20), v2->bounds()); | 71 EXPECT_EQ(gfx::Rect(20, 20, 10, 20), v2->bounds()); |
69 | 72 |
70 layout_->set_inside_border_insets( | 73 layout->set_inside_border_insets( |
71 gfx::Insets(5, 10, 15, 20)); | 74 gfx::Insets(5, 10, 15, 20)); |
72 EXPECT_EQ(gfx::Size(50, 40), layout_->GetPreferredSize(host_.get())); | 75 EXPECT_EQ(gfx::Size(50, 40), layout->GetPreferredSize(host_.get())); |
73 host_->SetBounds(0, 0, 50, 40); | 76 host_->SetBounds(0, 0, 50, 40); |
74 layout_->Layout(host_.get()); | 77 host_->Layout(); |
75 EXPECT_EQ(gfx::Rect(10, 5, 10, 20), v1->bounds()); | 78 EXPECT_EQ(gfx::Rect(10, 5, 10, 20), v1->bounds()); |
76 EXPECT_EQ(gfx::Rect(20, 5, 10, 20), v2->bounds()); | 79 EXPECT_EQ(gfx::Rect(20, 5, 10, 20), v2->bounds()); |
77 } | 80 } |
78 | 81 |
79 TEST_F(BoxLayoutTest, Spacing) { | 82 TEST_F(BoxLayoutTest, Spacing) { |
80 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 7, 7, 8)); | 83 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 7, 7, 8); |
| 84 host_->SetLayoutManager(layout); |
81 View* v1 = new StaticSizedView(gfx::Size(10, 20)); | 85 View* v1 = new StaticSizedView(gfx::Size(10, 20)); |
82 host_->AddChildView(v1); | 86 host_->AddChildView(v1); |
83 View* v2 = new StaticSizedView(gfx::Size(10, 20)); | 87 View* v2 = new StaticSizedView(gfx::Size(10, 20)); |
84 host_->AddChildView(v2); | 88 host_->AddChildView(v2); |
85 EXPECT_EQ(gfx::Size(42, 34), layout_->GetPreferredSize(host_.get())); | 89 EXPECT_EQ(gfx::Size(42, 34), layout->GetPreferredSize(host_.get())); |
86 host_->SetBounds(0, 0, 100, 100); | 90 host_->SetBounds(0, 0, 100, 100); |
87 layout_->Layout(host_.get()); | 91 host_->Layout(); |
88 EXPECT_EQ(gfx::Rect(7, 7, 10, 86), v1->bounds()); | 92 EXPECT_EQ(gfx::Rect(7, 7, 10, 86), v1->bounds()); |
89 EXPECT_EQ(gfx::Rect(25, 7, 10, 86), v2->bounds()); | 93 EXPECT_EQ(gfx::Rect(25, 7, 10, 86), v2->bounds()); |
90 } | 94 } |
91 | 95 |
92 TEST_F(BoxLayoutTest, Overflow) { | 96 TEST_F(BoxLayoutTest, Overflow) { |
93 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0)); | 97 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0); |
| 98 host_->SetLayoutManager(layout); |
94 View* v1 = new StaticSizedView(gfx::Size(20, 20)); | 99 View* v1 = new StaticSizedView(gfx::Size(20, 20)); |
95 host_->AddChildView(v1); | 100 host_->AddChildView(v1); |
96 View* v2 = new StaticSizedView(gfx::Size(10, 20)); | 101 View* v2 = new StaticSizedView(gfx::Size(10, 20)); |
97 host_->AddChildView(v2); | 102 host_->AddChildView(v2); |
98 host_->SetBounds(0, 0, 10, 10); | 103 host_->SetBounds(0, 0, 10, 10); |
99 | 104 |
100 // Overflows by positioning views at the start and truncating anything that | 105 // Overflows by positioning views at the start and truncating anything that |
101 // doesn't fit. | 106 // doesn't fit. |
102 layout_->Layout(host_.get()); | 107 host_->Layout(); |
103 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds()); | 108 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds()); |
104 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds()); | 109 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds()); |
105 | 110 |
106 // All values of main axis alignment should overflow in the same manner. | 111 // All values of main axis alignment should overflow in the same manner. |
107 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START); | 112 layout->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START); |
108 layout_->Layout(host_.get()); | 113 host_->Layout(); |
109 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds()); | 114 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds()); |
110 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds()); | 115 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds()); |
111 | 116 |
112 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); | 117 layout->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); |
113 layout_->Layout(host_.get()); | 118 host_->Layout(); |
114 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds()); | 119 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds()); |
115 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds()); | 120 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds()); |
116 | 121 |
117 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END); | 122 layout->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END); |
118 layout_->Layout(host_.get()); | 123 host_->Layout(); |
119 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds()); | 124 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds()); |
120 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds()); | 125 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds()); |
121 } | 126 } |
122 | 127 |
123 TEST_F(BoxLayoutTest, NoSpace) { | 128 TEST_F(BoxLayoutTest, NoSpace) { |
124 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10)); | 129 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10); |
| 130 host_->SetLayoutManager(layout); |
125 View* childView = new StaticSizedView(gfx::Size(20, 20)); | 131 View* childView = new StaticSizedView(gfx::Size(20, 20)); |
126 host_->AddChildView(childView); | 132 host_->AddChildView(childView); |
127 host_->SetBounds(0, 0, 10, 10); | 133 host_->SetBounds(0, 0, 10, 10); |
128 layout_->Layout(host_.get()); | 134 host_->Layout(); |
129 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), childView->bounds()); | 135 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), childView->bounds()); |
130 } | 136 } |
131 | 137 |
132 TEST_F(BoxLayoutTest, InvisibleChild) { | 138 TEST_F(BoxLayoutTest, InvisibleChild) { |
133 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10)); | 139 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10); |
| 140 host_->SetLayoutManager(layout); |
134 View* v1 = new StaticSizedView(gfx::Size(20, 20)); | 141 View* v1 = new StaticSizedView(gfx::Size(20, 20)); |
135 v1->SetVisible(false); | 142 v1->SetVisible(false); |
136 host_->AddChildView(v1); | 143 host_->AddChildView(v1); |
137 View* v2 = new StaticSizedView(gfx::Size(10, 10)); | 144 View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
138 host_->AddChildView(v2); | 145 host_->AddChildView(v2); |
139 EXPECT_EQ(gfx::Size(30, 30), layout_->GetPreferredSize(host_.get())); | 146 EXPECT_EQ(gfx::Size(30, 30), layout->GetPreferredSize(host_.get())); |
140 host_->SetBounds(0, 0, 30, 30); | 147 host_->SetBounds(0, 0, 30, 30); |
141 layout_->Layout(host_.get()); | 148 host_->Layout(); |
142 EXPECT_EQ(gfx::Rect(10, 10, 10, 10), v2->bounds()); | 149 EXPECT_EQ(gfx::Rect(10, 10, 10, 10), v2->bounds()); |
143 } | 150 } |
144 | 151 |
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 | |
161 TEST_F(BoxLayoutTest, UseHeightForWidth) { | 152 TEST_F(BoxLayoutTest, UseHeightForWidth) { |
162 layout_.reset(new BoxLayout(BoxLayout::kVertical, 0, 0, 0)); | 153 BoxLayout* layout = new BoxLayout(BoxLayout::kVertical, 0, 0, 0); |
| 154 host_->SetLayoutManager(layout); |
163 View* v1 = new StaticSizedView(gfx::Size(20, 10)); | 155 View* v1 = new StaticSizedView(gfx::Size(20, 10)); |
164 host_->AddChildView(v1); | 156 host_->AddChildView(v1); |
165 ProportionallySizedView* v2 = new ProportionallySizedView(2); | 157 ProportionallySizedView* v2 = new ProportionallySizedView(2); |
166 v2->set_preferred_width(10); | 158 v2->set_preferred_width(10); |
167 host_->AddChildView(v2); | 159 host_->AddChildView(v2); |
168 EXPECT_EQ(gfx::Size(20, 50), layout_->GetPreferredSize(host_.get())); | 160 EXPECT_EQ(gfx::Size(20, 50), layout->GetPreferredSize(host_.get())); |
169 | 161 |
170 host_->SetBounds(0, 0, 20, 50); | 162 host_->SetBounds(0, 0, 20, 50); |
171 layout_->Layout(host_.get()); | 163 host_->Layout(); |
172 EXPECT_EQ(gfx::Rect(0, 0, 20, 10), v1->bounds()); | 164 EXPECT_EQ(gfx::Rect(0, 0, 20, 10), v1->bounds()); |
173 EXPECT_EQ(gfx::Rect(0, 10, 20, 40), v2->bounds()); | 165 EXPECT_EQ(gfx::Rect(0, 10, 20, 40), v2->bounds()); |
174 | 166 |
175 EXPECT_EQ(110, layout_->GetPreferredHeightForWidth(host_.get(), 50)); | 167 EXPECT_EQ(110, layout->GetPreferredHeightForWidth(host_.get(), 50)); |
176 | 168 |
177 // Test without horizontal stretching of the views. | 169 // Test without horizontal stretching of the views. |
178 layout_->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END); | 170 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END); |
179 EXPECT_EQ(gfx::Size(20, 30).ToString(), | 171 EXPECT_EQ(gfx::Size(20, 30).ToString(), |
180 layout_->GetPreferredSize(host_.get()).ToString()); | 172 layout->GetPreferredSize(host_.get()).ToString()); |
181 | 173 |
182 host_->SetBounds(0, 0, 20, 30); | 174 host_->SetBounds(0, 0, 20, 30); |
183 layout_->Layout(host_.get()); | 175 host_->Layout(); |
184 EXPECT_EQ(gfx::Rect(0, 0, 20, 10), v1->bounds()); | 176 EXPECT_EQ(gfx::Rect(0, 0, 20, 10), v1->bounds()); |
185 EXPECT_EQ(gfx::Rect(10, 10, 10, 20), v2->bounds()); | 177 EXPECT_EQ(gfx::Rect(10, 10, 10, 20), v2->bounds()); |
186 | 178 |
187 EXPECT_EQ(30, layout_->GetPreferredHeightForWidth(host_.get(), 50)); | 179 EXPECT_EQ(30, layout->GetPreferredHeightForWidth(host_.get(), 50)); |
188 } | 180 } |
189 | 181 |
190 TEST_F(BoxLayoutTest, EmptyPreferredSize) { | 182 TEST_F(BoxLayoutTest, EmptyPreferredSize) { |
191 for (size_t i = 0; i < 2; i++) { | 183 for (size_t i = 0; i < 2; i++) { |
192 BoxLayout::Orientation orientation = i == 0 ? BoxLayout::kHorizontal : | 184 BoxLayout::Orientation orientation = i == 0 ? BoxLayout::kHorizontal : |
193 BoxLayout::kVertical; | 185 BoxLayout::kVertical; |
194 host_->RemoveAllChildViews(true); | 186 host_->RemoveAllChildViews(true); |
195 host_->SetLayoutManager(new BoxLayout(orientation, 0, 0, 5)); | 187 host_->SetLayoutManager(new BoxLayout(orientation, 0, 0, 5)); |
196 View* v1 = new StaticSizedView(gfx::Size()); | 188 View* v1 = new StaticSizedView(gfx::Size()); |
197 host_->AddChildView(v1); | 189 host_->AddChildView(v1); |
198 View* v2 = new StaticSizedView(gfx::Size(10, 10)); | 190 View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
199 host_->AddChildView(v2); | 191 host_->AddChildView(v2); |
200 host_->SizeToPreferredSize(); | 192 host_->SizeToPreferredSize(); |
201 host_->Layout(); | 193 host_->Layout(); |
202 | 194 |
203 EXPECT_EQ(v2->GetPreferredSize().width(), host_->bounds().width()) << i; | 195 EXPECT_EQ(v2->GetPreferredSize().width(), host_->bounds().width()) << i; |
204 EXPECT_EQ(v2->GetPreferredSize().height(), host_->bounds().height()) << i; | 196 EXPECT_EQ(v2->GetPreferredSize().height(), host_->bounds().height()) << i; |
205 EXPECT_EQ(v1->GetPreferredSize().width(), v1->bounds().width()) << i; | 197 EXPECT_EQ(v1->GetPreferredSize().width(), v1->bounds().width()) << i; |
206 EXPECT_EQ(v1->GetPreferredSize().height(), v1->bounds().height()) << i; | 198 EXPECT_EQ(v1->GetPreferredSize().height(), v1->bounds().height()) << i; |
207 EXPECT_EQ(v2->GetPreferredSize().width(), v2->bounds().width()) << i; | 199 EXPECT_EQ(v2->GetPreferredSize().width(), v2->bounds().width()) << i; |
208 EXPECT_EQ(v2->GetPreferredSize().height(), v2->bounds().height()) << i; | 200 EXPECT_EQ(v2->GetPreferredSize().height(), v2->bounds().height()) << i; |
209 } | 201 } |
210 } | 202 } |
211 | 203 |
212 TEST_F(BoxLayoutTest, MainAxisAlignmentHorizontal) { | 204 TEST_F(BoxLayoutTest, MainAxisAlignmentHorizontal) { |
213 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10)); | 205 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10); |
| 206 host_->SetLayoutManager(layout); |
214 | 207 |
215 View* v1 = new StaticSizedView(gfx::Size(20, 20)); | 208 View* v1 = new StaticSizedView(gfx::Size(20, 20)); |
216 host_->AddChildView(v1); | 209 host_->AddChildView(v1); |
217 View* v2 = new StaticSizedView(gfx::Size(10, 10)); | 210 View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
218 host_->AddChildView(v2); | 211 host_->AddChildView(v2); |
219 | 212 |
220 host_->SetBounds(0, 0, 100, 40); | 213 host_->SetBounds(0, 0, 100, 40); |
221 | 214 |
222 // Align children to the horizontal start by default. | 215 // Align children to the horizontal start by default. |
223 layout_->Layout(host_.get()); | 216 host_->Layout(); |
224 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString()); | 217 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString()); |
225 EXPECT_EQ(gfx::Rect(40, 10, 10, 20).ToString(), v2->bounds().ToString()); | 218 EXPECT_EQ(gfx::Rect(40, 10, 10, 20).ToString(), v2->bounds().ToString()); |
226 | 219 |
227 // Ensure same results for MAIN_AXIS_ALIGNMENT_START. | 220 // Ensure same results for MAIN_AXIS_ALIGNMENT_START. |
228 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START); | 221 layout->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START); |
229 layout_->Layout(host_.get()); | 222 host_->Layout(); |
230 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString()); | 223 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString()); |
231 EXPECT_EQ(gfx::Rect(40, 10, 10, 20).ToString(), v2->bounds().ToString()); | 224 EXPECT_EQ(gfx::Rect(40, 10, 10, 20).ToString(), v2->bounds().ToString()); |
232 | 225 |
233 // Aligns children to the center horizontally. | 226 // Aligns children to the center horizontally. |
234 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); | 227 layout->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); |
235 layout_->Layout(host_.get()); | 228 host_->Layout(); |
236 EXPECT_EQ(gfx::Rect(30, 10, 20, 20).ToString(), v1->bounds().ToString()); | 229 EXPECT_EQ(gfx::Rect(30, 10, 20, 20).ToString(), v1->bounds().ToString()); |
237 EXPECT_EQ(gfx::Rect(60, 10, 10, 20).ToString(), v2->bounds().ToString()); | 230 EXPECT_EQ(gfx::Rect(60, 10, 10, 20).ToString(), v2->bounds().ToString()); |
238 | 231 |
239 // Aligns children to the end of the host horizontally, accounting for the | 232 // Aligns children to the end of the host horizontally, accounting for the |
240 // inside border spacing. | 233 // inside border spacing. |
241 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END); | 234 layout->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END); |
242 layout_->Layout(host_.get()); | 235 host_->Layout(); |
243 EXPECT_EQ(gfx::Rect(50, 10, 20, 20).ToString(), v1->bounds().ToString()); | 236 EXPECT_EQ(gfx::Rect(50, 10, 20, 20).ToString(), v1->bounds().ToString()); |
244 EXPECT_EQ(gfx::Rect(80, 10, 10, 20).ToString(), v2->bounds().ToString()); | 237 EXPECT_EQ(gfx::Rect(80, 10, 10, 20).ToString(), v2->bounds().ToString()); |
245 } | 238 } |
246 | 239 |
247 TEST_F(BoxLayoutTest, MainAxisAlignmentVertical) { | 240 TEST_F(BoxLayoutTest, MainAxisAlignmentVertical) { |
248 layout_.reset(new BoxLayout(BoxLayout::kVertical, 10, 10, 10)); | 241 BoxLayout* layout = new BoxLayout(BoxLayout::kVertical, 10, 10, 10); |
| 242 host_->SetLayoutManager(layout); |
249 | 243 |
250 View* v1 = new StaticSizedView(gfx::Size(20, 20)); | 244 View* v1 = new StaticSizedView(gfx::Size(20, 20)); |
251 host_->AddChildView(v1); | 245 host_->AddChildView(v1); |
252 View* v2 = new StaticSizedView(gfx::Size(10, 10)); | 246 View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
253 host_->AddChildView(v2); | 247 host_->AddChildView(v2); |
254 | 248 |
255 host_->SetBounds(0, 0, 40, 100); | 249 host_->SetBounds(0, 0, 40, 100); |
256 | 250 |
257 // Align children to the vertical start by default. | 251 // Align children to the vertical start by default. |
258 layout_->Layout(host_.get()); | 252 host_->Layout(); |
259 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString()); | 253 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString()); |
260 EXPECT_EQ(gfx::Rect(10, 40, 20, 10).ToString(), v2->bounds().ToString()); | 254 EXPECT_EQ(gfx::Rect(10, 40, 20, 10).ToString(), v2->bounds().ToString()); |
261 | 255 |
262 // Ensure same results for MAIN_AXIS_ALIGNMENT_START. | 256 // Ensure same results for MAIN_AXIS_ALIGNMENT_START. |
263 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START); | 257 layout->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START); |
264 layout_->Layout(host_.get()); | 258 host_->Layout(); |
265 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString()); | 259 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString()); |
266 EXPECT_EQ(gfx::Rect(10, 40, 20, 10).ToString(), v2->bounds().ToString()); | 260 EXPECT_EQ(gfx::Rect(10, 40, 20, 10).ToString(), v2->bounds().ToString()); |
267 | 261 |
268 // Aligns children to the center vertically. | 262 // Aligns children to the center vertically. |
269 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); | 263 layout->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); |
270 layout_->Layout(host_.get()); | 264 host_->Layout(); |
271 EXPECT_EQ(gfx::Rect(10, 30, 20, 20).ToString(), v1->bounds().ToString()); | 265 EXPECT_EQ(gfx::Rect(10, 30, 20, 20).ToString(), v1->bounds().ToString()); |
272 EXPECT_EQ(gfx::Rect(10, 60, 20, 10).ToString(), v2->bounds().ToString()); | 266 EXPECT_EQ(gfx::Rect(10, 60, 20, 10).ToString(), v2->bounds().ToString()); |
273 | 267 |
274 // Aligns children to the end of the host vertically, accounting for the | 268 // Aligns children to the end of the host vertically, accounting for the |
275 // inside border spacing. | 269 // inside border spacing. |
276 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END); | 270 layout->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END); |
277 layout_->Layout(host_.get()); | 271 host_->Layout(); |
278 EXPECT_EQ(gfx::Rect(10, 50, 20, 20).ToString(), v1->bounds().ToString()); | 272 EXPECT_EQ(gfx::Rect(10, 50, 20, 20).ToString(), v1->bounds().ToString()); |
279 EXPECT_EQ(gfx::Rect(10, 80, 20, 10).ToString(), v2->bounds().ToString()); | 273 EXPECT_EQ(gfx::Rect(10, 80, 20, 10).ToString(), v2->bounds().ToString()); |
280 } | 274 } |
281 | 275 |
282 TEST_F(BoxLayoutTest, CrossAxisAlignmentHorizontal) { | 276 TEST_F(BoxLayoutTest, CrossAxisAlignmentHorizontal) { |
283 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10)); | 277 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10); |
| 278 host_->SetLayoutManager(layout); |
284 | 279 |
285 View* v1 = new StaticSizedView(gfx::Size(20, 20)); | 280 View* v1 = new StaticSizedView(gfx::Size(20, 20)); |
286 host_->AddChildView(v1); | 281 host_->AddChildView(v1); |
287 View* v2 = new StaticSizedView(gfx::Size(10, 10)); | 282 View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
288 host_->AddChildView(v2); | 283 host_->AddChildView(v2); |
289 | 284 |
290 host_->SetBounds(0, 0, 100, 60); | 285 host_->SetBounds(0, 0, 100, 60); |
291 | 286 |
292 // Stretch children to fill the available height by default. | 287 // Stretch children to fill the available height by default. |
293 layout_->Layout(host_.get()); | 288 host_->Layout(); |
294 EXPECT_EQ(gfx::Rect(10, 10, 20, 40).ToString(), v1->bounds().ToString()); | 289 EXPECT_EQ(gfx::Rect(10, 10, 20, 40).ToString(), v1->bounds().ToString()); |
295 EXPECT_EQ(gfx::Rect(40, 10, 10, 40).ToString(), v2->bounds().ToString()); | 290 EXPECT_EQ(gfx::Rect(40, 10, 10, 40).ToString(), v2->bounds().ToString()); |
296 | 291 |
297 // Ensure same results for CROSS_AXIS_ALIGNMENT_STRETCH. | 292 // Ensure same results for CROSS_AXIS_ALIGNMENT_STRETCH. |
298 layout_->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH); | 293 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH); |
299 layout_->Layout(host_.get()); | 294 host_->Layout(); |
300 EXPECT_EQ(gfx::Rect(10, 10, 20, 40).ToString(), v1->bounds().ToString()); | 295 EXPECT_EQ(gfx::Rect(10, 10, 20, 40).ToString(), v1->bounds().ToString()); |
301 EXPECT_EQ(gfx::Rect(40, 10, 10, 40).ToString(), v2->bounds().ToString()); | 296 EXPECT_EQ(gfx::Rect(40, 10, 10, 40).ToString(), v2->bounds().ToString()); |
302 | 297 |
303 // Aligns children to the start vertically. | 298 // Aligns children to the start vertically. |
304 layout_->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_START); | 299 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_START); |
305 layout_->Layout(host_.get()); | 300 host_->Layout(); |
306 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString()); | 301 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString()); |
307 EXPECT_EQ(gfx::Rect(40, 10, 10, 10).ToString(), v2->bounds().ToString()); | 302 EXPECT_EQ(gfx::Rect(40, 10, 10, 10).ToString(), v2->bounds().ToString()); |
308 | 303 |
309 // Aligns children to the center vertically. | 304 // Aligns children to the center vertically. |
310 layout_->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER); | 305 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER); |
311 layout_->Layout(host_.get()); | 306 host_->Layout(); |
312 EXPECT_EQ(gfx::Rect(10, 20, 20, 20).ToString(), v1->bounds().ToString()); | 307 EXPECT_EQ(gfx::Rect(10, 20, 20, 20).ToString(), v1->bounds().ToString()); |
313 EXPECT_EQ(gfx::Rect(40, 25, 10, 10).ToString(), v2->bounds().ToString()); | 308 EXPECT_EQ(gfx::Rect(40, 25, 10, 10).ToString(), v2->bounds().ToString()); |
314 | 309 |
315 // Aligns children to the end of the host vertically, accounting for the | 310 // Aligns children to the end of the host vertically, accounting for the |
316 // inside border spacing. | 311 // inside border spacing. |
317 layout_->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END); | 312 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END); |
318 layout_->Layout(host_.get()); | 313 host_->Layout(); |
319 EXPECT_EQ(gfx::Rect(10, 30, 20, 20).ToString(), v1->bounds().ToString()); | 314 EXPECT_EQ(gfx::Rect(10, 30, 20, 20).ToString(), v1->bounds().ToString()); |
320 EXPECT_EQ(gfx::Rect(40, 40, 10, 10).ToString(), v2->bounds().ToString()); | 315 EXPECT_EQ(gfx::Rect(40, 40, 10, 10).ToString(), v2->bounds().ToString()); |
321 } | 316 } |
322 | 317 |
323 TEST_F(BoxLayoutTest, CrossAxisAlignmentVertical) { | 318 TEST_F(BoxLayoutTest, CrossAxisAlignmentVertical) { |
324 layout_.reset(new BoxLayout(BoxLayout::kVertical, 10, 10, 10)); | 319 BoxLayout* layout = new BoxLayout(BoxLayout::kVertical, 10, 10, 10); |
| 320 host_->SetLayoutManager(layout); |
325 | 321 |
326 View* v1 = new StaticSizedView(gfx::Size(20, 20)); | 322 View* v1 = new StaticSizedView(gfx::Size(20, 20)); |
327 host_->AddChildView(v1); | 323 host_->AddChildView(v1); |
328 View* v2 = new StaticSizedView(gfx::Size(10, 10)); | 324 View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
329 host_->AddChildView(v2); | 325 host_->AddChildView(v2); |
330 | 326 |
331 host_->SetBounds(0, 0, 60, 100); | 327 host_->SetBounds(0, 0, 60, 100); |
332 | 328 |
333 // Stretch children to fill the available width by default. | 329 // Stretch children to fill the available width by default. |
334 layout_->Layout(host_.get()); | 330 host_->Layout(); |
335 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1->bounds().ToString()); | 331 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1->bounds().ToString()); |
336 EXPECT_EQ(gfx::Rect(10, 40, 40, 10).ToString(), v2->bounds().ToString()); | 332 EXPECT_EQ(gfx::Rect(10, 40, 40, 10).ToString(), v2->bounds().ToString()); |
337 | 333 |
338 // Ensure same results for CROSS_AXIS_ALIGNMENT_STRETCH. | 334 // Ensure same results for CROSS_AXIS_ALIGNMENT_STRETCH. |
339 layout_->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH); | 335 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH); |
340 layout_->Layout(host_.get()); | 336 host_->Layout(); |
341 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1->bounds().ToString()); | 337 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1->bounds().ToString()); |
342 EXPECT_EQ(gfx::Rect(10, 40, 40, 10).ToString(), v2->bounds().ToString()); | 338 EXPECT_EQ(gfx::Rect(10, 40, 40, 10).ToString(), v2->bounds().ToString()); |
343 | 339 |
344 // Aligns children to the start horizontally. | 340 // Aligns children to the start horizontally. |
345 layout_->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_START); | 341 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_START); |
346 layout_->Layout(host_.get()); | 342 host_->Layout(); |
347 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString()); | 343 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString()); |
348 EXPECT_EQ(gfx::Rect(10, 40, 10, 10).ToString(), v2->bounds().ToString()); | 344 EXPECT_EQ(gfx::Rect(10, 40, 10, 10).ToString(), v2->bounds().ToString()); |
349 | 345 |
350 // Aligns children to the center horizontally. | 346 // Aligns children to the center horizontally. |
351 layout_->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER); | 347 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER); |
352 layout_->Layout(host_.get()); | 348 host_->Layout(); |
353 EXPECT_EQ(gfx::Rect(20, 10, 20, 20).ToString(), v1->bounds().ToString()); | 349 EXPECT_EQ(gfx::Rect(20, 10, 20, 20).ToString(), v1->bounds().ToString()); |
354 EXPECT_EQ(gfx::Rect(25, 40, 10, 10).ToString(), v2->bounds().ToString()); | 350 EXPECT_EQ(gfx::Rect(25, 40, 10, 10).ToString(), v2->bounds().ToString()); |
355 | 351 |
356 // Aligns children to the end of the host horizontally, accounting for the | 352 // Aligns children to the end of the host horizontally, accounting for the |
357 // inside border spacing. | 353 // inside border spacing. |
358 layout_->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END); | 354 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END); |
359 layout_->Layout(host_.get()); | 355 host_->Layout(); |
360 EXPECT_EQ(gfx::Rect(30, 10, 20, 20).ToString(), v1->bounds().ToString()); | 356 EXPECT_EQ(gfx::Rect(30, 10, 20, 20).ToString(), v1->bounds().ToString()); |
361 EXPECT_EQ(gfx::Rect(40, 40, 10, 10).ToString(), v2->bounds().ToString()); | 357 EXPECT_EQ(gfx::Rect(40, 40, 10, 10).ToString(), v2->bounds().ToString()); |
362 } | 358 } |
363 | 359 |
| 360 TEST_F(BoxLayoutTest, FlexAll) { |
| 361 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10); |
| 362 host_->SetLayoutManager(layout); |
| 363 layout->SetDefaultFlex(1); |
| 364 |
| 365 View* v1 = new StaticSizedView(gfx::Size(20, 20)); |
| 366 host_->AddChildView(v1); |
| 367 View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
| 368 host_->AddChildView(v2); |
| 369 View* v3 = new StaticSizedView(gfx::Size(30, 30)); |
| 370 host_->AddChildView(v3); |
| 371 EXPECT_EQ(gfx::Size(100, 50), layout->GetPreferredSize(host_.get())); |
| 372 |
| 373 host_->SetBounds(0, 0, 120, 50); |
| 374 host_->Layout(); |
| 375 EXPECT_EQ(gfx::Rect(10, 10, 27, 30).ToString(), v1->bounds().ToString()); |
| 376 EXPECT_EQ(gfx::Rect(47, 10, 16, 30).ToString(), v2->bounds().ToString()); |
| 377 EXPECT_EQ(gfx::Rect(73, 10, 37, 30).ToString(), v3->bounds().ToString()); |
| 378 } |
| 379 |
| 380 TEST_F(BoxLayoutTest, FlexGrowVertical) { |
| 381 BoxLayout* layout = new BoxLayout(BoxLayout::kVertical, 10, 10, 10); |
| 382 host_->SetLayoutManager(layout); |
| 383 |
| 384 View* v1 = new StaticSizedView(gfx::Size(20, 20)); |
| 385 host_->AddChildView(v1); |
| 386 View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
| 387 host_->AddChildView(v2); |
| 388 View* v3 = new StaticSizedView(gfx::Size(30, 30)); |
| 389 host_->AddChildView(v3); |
| 390 |
| 391 host_->SetBounds(0, 0, 50, 130); |
| 392 |
| 393 // Views don't fill the available space by default. |
| 394 host_->Layout(); |
| 395 EXPECT_EQ(gfx::Rect(10, 10, 30, 20).ToString(), v1->bounds().ToString()); |
| 396 EXPECT_EQ(gfx::Rect(10, 40, 30, 10).ToString(), v2->bounds().ToString()); |
| 397 EXPECT_EQ(gfx::Rect(10, 60, 30, 30).ToString(), v3->bounds().ToString()); |
| 398 |
| 399 std::vector<BoxLayout::MainAxisAlignment> main_alignments; |
| 400 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START); |
| 401 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); |
| 402 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END); |
| 403 |
| 404 for (size_t i = 0; i < main_alignments.size(); ++i) { |
| 405 layout->set_main_axis_alignment(main_alignments[i]); |
| 406 |
| 407 // Set the first view to consume all free space. |
| 408 layout->SetFlexForView(v1, 1); |
| 409 layout->ClearFlexForView(v2); |
| 410 layout->ClearFlexForView(v3); |
| 411 host_->Layout(); |
| 412 EXPECT_EQ(gfx::Rect(10, 10, 30, 50).ToString(), v1->bounds().ToString()); |
| 413 EXPECT_EQ(gfx::Rect(10, 70, 30, 10).ToString(), v2->bounds().ToString()); |
| 414 EXPECT_EQ(gfx::Rect(10, 90, 30, 30).ToString(), v3->bounds().ToString()); |
| 415 |
| 416 // Set the third view to take 2/3s of the free space and leave the first |
| 417 // view |
| 418 // with 1/3. |
| 419 layout->SetFlexForView(v3, 2); |
| 420 host_->Layout(); |
| 421 EXPECT_EQ(gfx::Rect(10, 10, 30, 30).ToString(), v1->bounds().ToString()); |
| 422 EXPECT_EQ(gfx::Rect(10, 50, 30, 10).ToString(), v2->bounds().ToString()); |
| 423 EXPECT_EQ(gfx::Rect(10, 70, 30, 50).ToString(), v3->bounds().ToString()); |
| 424 |
| 425 // Clear the previously set flex values and set the second view to take all |
| 426 // the free space. |
| 427 layout->ClearFlexForView(v1); |
| 428 layout->SetFlexForView(v2, 1); |
| 429 layout->ClearFlexForView(v3); |
| 430 host_->Layout(); |
| 431 EXPECT_EQ(gfx::Rect(10, 10, 30, 20).ToString(), v1->bounds().ToString()); |
| 432 EXPECT_EQ(gfx::Rect(10, 40, 30, 40).ToString(), v2->bounds().ToString()); |
| 433 EXPECT_EQ(gfx::Rect(10, 90, 30, 30).ToString(), v3->bounds().ToString()); |
| 434 } |
| 435 } |
| 436 |
| 437 TEST_F(BoxLayoutTest, FlexGrowHorizontalWithRemainder) { |
| 438 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0); |
| 439 host_->SetLayoutManager(layout); |
| 440 layout->SetDefaultFlex(1); |
| 441 std::vector<View*> views; |
| 442 for (int i = 0; i < 5; ++i) { |
| 443 View* view = new StaticSizedView(gfx::Size(10, 10)); |
| 444 views.push_back(view); |
| 445 host_->AddChildView(view); |
| 446 } |
| 447 |
| 448 EXPECT_EQ(gfx::Size(50, 10), layout->GetPreferredSize(host_.get())); |
| 449 |
| 450 host_->SetBounds(0, 0, 52, 10); |
| 451 host_->Layout(); |
| 452 // The 2nd and 4th views should have an extra pixel as they correspond to 20.8 |
| 453 // and 41.6 which round up. |
| 454 EXPECT_EQ(gfx::Rect(0, 0, 10, 10).ToString(), views[0]->bounds().ToString()); |
| 455 EXPECT_EQ(gfx::Rect(10, 0, 11, 10).ToString(), views[1]->bounds().ToString()); |
| 456 EXPECT_EQ(gfx::Rect(21, 0, 10, 10).ToString(), views[2]->bounds().ToString()); |
| 457 EXPECT_EQ(gfx::Rect(31, 0, 11, 10).ToString(), views[3]->bounds().ToString()); |
| 458 EXPECT_EQ(gfx::Rect(42, 0, 10, 10).ToString(), views[4]->bounds().ToString()); |
| 459 } |
| 460 |
| 461 TEST_F(BoxLayoutTest, FlexGrowHorizontalWithRemainder2) { |
| 462 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0); |
| 463 host_->SetLayoutManager(layout); |
| 464 layout->SetDefaultFlex(1); |
| 465 std::vector<View*> views; |
| 466 for (int i = 0; i < 4; ++i) { |
| 467 View* view = new StaticSizedView(gfx::Size(1, 10)); |
| 468 views.push_back(view); |
| 469 host_->AddChildView(view); |
| 470 } |
| 471 |
| 472 EXPECT_EQ(gfx::Size(4, 10), layout->GetPreferredSize(host_.get())); |
| 473 |
| 474 host_->SetBounds(0, 0, 10, 10); |
| 475 host_->Layout(); |
| 476 // The 1st and 3rd views should have an extra pixel as they correspond to 2.5 |
| 477 // and 7.5 which round up. |
| 478 EXPECT_EQ(gfx::Rect(0, 0, 3, 10).ToString(), views[0]->bounds().ToString()); |
| 479 EXPECT_EQ(gfx::Rect(3, 0, 2, 10).ToString(), views[1]->bounds().ToString()); |
| 480 EXPECT_EQ(gfx::Rect(5, 0, 3, 10).ToString(), views[2]->bounds().ToString()); |
| 481 EXPECT_EQ(gfx::Rect(8, 0, 2, 10).ToString(), views[3]->bounds().ToString()); |
| 482 } |
| 483 |
| 484 TEST_F(BoxLayoutTest, FlexShrinkHorizontal) { |
| 485 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10); |
| 486 host_->SetLayoutManager(layout); |
| 487 |
| 488 View* v1 = new StaticSizedView(gfx::Size(20, 20)); |
| 489 host_->AddChildView(v1); |
| 490 View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
| 491 host_->AddChildView(v2); |
| 492 View* v3 = new StaticSizedView(gfx::Size(30, 30)); |
| 493 host_->AddChildView(v3); |
| 494 |
| 495 host_->SetBounds(0, 0, 85, 50); |
| 496 |
| 497 // Truncate width by default. |
| 498 host_->Layout(); |
| 499 EXPECT_EQ(gfx::Rect(10, 10, 20, 30).ToString(), v1->bounds().ToString()); |
| 500 EXPECT_EQ(gfx::Rect(40, 10, 10, 30).ToString(), v2->bounds().ToString()); |
| 501 EXPECT_EQ(gfx::Rect(60, 10, 15, 30).ToString(), v3->bounds().ToString()); |
| 502 |
| 503 std::vector<BoxLayout::MainAxisAlignment> main_alignments; |
| 504 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START); |
| 505 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); |
| 506 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END); |
| 507 |
| 508 for (size_t i = 0; i < main_alignments.size(); ++i) { |
| 509 layout->set_main_axis_alignment(main_alignments[i]); |
| 510 |
| 511 // Set the first view to shrink as much as necessary. |
| 512 layout->SetFlexForView(v1, 1); |
| 513 layout->ClearFlexForView(v2); |
| 514 layout->ClearFlexForView(v3); |
| 515 host_->Layout(); |
| 516 EXPECT_EQ(gfx::Rect(10, 10, 5, 30).ToString(), v1->bounds().ToString()); |
| 517 EXPECT_EQ(gfx::Rect(25, 10, 10, 30).ToString(), v2->bounds().ToString()); |
| 518 EXPECT_EQ(gfx::Rect(45, 10, 30, 30).ToString(), v3->bounds().ToString()); |
| 519 |
| 520 // Set the third view to shrink 2/3s of the free space and leave the first |
| 521 // view with 1/3. |
| 522 layout->SetFlexForView(v3, 2); |
| 523 host_->Layout(); |
| 524 EXPECT_EQ(gfx::Rect(10, 10, 15, 30).ToString(), v1->bounds().ToString()); |
| 525 EXPECT_EQ(gfx::Rect(35, 10, 10, 30).ToString(), v2->bounds().ToString()); |
| 526 EXPECT_EQ(gfx::Rect(55, 10, 20, 30).ToString(), v3->bounds().ToString()); |
| 527 |
| 528 // Clear the previously set flex values and set the second view to take all |
| 529 // the free space with MAIN_AXIS_ALIGNMENT_END set. This causes the second |
| 530 // view to shrink to zero and the third view still doesn't fit so it |
| 531 // overflows. |
| 532 layout->ClearFlexForView(v1); |
| 533 layout->SetFlexForView(v2, 2); |
| 534 layout->ClearFlexForView(v3); |
| 535 host_->Layout(); |
| 536 EXPECT_EQ(gfx::Rect(10, 10, 20, 30).ToString(), v1->bounds().ToString()); |
| 537 // Conceptually this view is at 10, 40, 0, 0. |
| 538 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(), v2->bounds().ToString()); |
| 539 EXPECT_EQ(gfx::Rect(50, 10, 25, 30).ToString(), v3->bounds().ToString()); |
| 540 } |
| 541 } |
| 542 |
| 543 TEST_F(BoxLayoutTest, FlexShrinkVerticalWithRemainder) { |
| 544 BoxLayout* layout = new BoxLayout(BoxLayout::kVertical, 0, 0, 0); |
| 545 host_->SetLayoutManager(layout); |
| 546 View* v1 = new StaticSizedView(gfx::Size(20, 10)); |
| 547 host_->AddChildView(v1); |
| 548 View* v2 = new StaticSizedView(gfx::Size(20, 20)); |
| 549 host_->AddChildView(v2); |
| 550 View* v3 = new StaticSizedView(gfx::Size(20, 10)); |
| 551 host_->AddChildView(v3); |
| 552 host_->SetBounds(0, 0, 20, 20); |
| 553 |
| 554 std::vector<BoxLayout::MainAxisAlignment> main_alignments; |
| 555 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START); |
| 556 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); |
| 557 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END); |
| 558 |
| 559 for (size_t i = 0; i < main_alignments.size(); ++i) { |
| 560 layout->set_main_axis_alignment(main_alignments[i]); |
| 561 |
| 562 // The first view shrinks by 1/3 of the excess, the second view shrinks by |
| 563 // 2/3 of the excess and the third view should maintain its preferred size. |
| 564 layout->SetFlexForView(v1, 1); |
| 565 layout->SetFlexForView(v2, 2); |
| 566 layout->ClearFlexForView(v3); |
| 567 host_->Layout(); |
| 568 EXPECT_EQ(gfx::Rect(0, 0, 20, 3).ToString(), v1->bounds().ToString()); |
| 569 EXPECT_EQ(gfx::Rect(0, 3, 20, 7).ToString(), v2->bounds().ToString()); |
| 570 EXPECT_EQ(gfx::Rect(0, 10, 20, 10).ToString(), v3->bounds().ToString()); |
| 571 |
| 572 // The second view shrinks to 2/3 of the excess, the third view shrinks to |
| 573 // 1/3 of the excess and the first view should maintain its preferred size. |
| 574 layout->ClearFlexForView(v1); |
| 575 layout->SetFlexForView(v2, 2); |
| 576 layout->SetFlexForView(v3, 1); |
| 577 host_->Layout(); |
| 578 EXPECT_EQ(gfx::Rect(0, 0, 20, 10).ToString(), v1->bounds().ToString()); |
| 579 EXPECT_EQ(gfx::Rect(0, 10, 20, 7).ToString(), v2->bounds().ToString()); |
| 580 EXPECT_EQ(gfx::Rect(0, 17, 20, 3).ToString(), v3->bounds().ToString()); |
| 581 |
| 582 // Each view shrinks equally to fit within the available space. |
| 583 layout->SetFlexForView(v1, 1); |
| 584 layout->SetFlexForView(v2, 1); |
| 585 layout->SetFlexForView(v3, 1); |
| 586 host_->Layout(); |
| 587 EXPECT_EQ(gfx::Rect(0, 0, 20, 3).ToString(), v1->bounds().ToString()); |
| 588 EXPECT_EQ(gfx::Rect(0, 3, 20, 14).ToString(), v2->bounds().ToString()); |
| 589 EXPECT_EQ(gfx::Rect(0, 17, 20, 3).ToString(), v3->bounds().ToString()); |
| 590 } |
| 591 } |
| 592 |
364 } // namespace views | 593 } // namespace views |
OLD | NEW |