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

Side by Side Diff: ui/views/layout/box_layout_unittest.cc

Issue 284753002: Add main axis alignment for BoxLayout. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove set_spread_blank_space, rename to MainAxisAlignment Created 6 years, 7 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/views/layout/box_layout.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 EXPECT_EQ(gfx::Rect(25, 7, 10, 86), v2->bounds()); 68 EXPECT_EQ(gfx::Rect(25, 7, 10, 86), v2->bounds());
69 } 69 }
70 70
71 TEST_F(BoxLayoutTest, Overflow) { 71 TEST_F(BoxLayoutTest, Overflow) {
72 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0)); 72 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0));
73 View* v1 = new StaticSizedView(gfx::Size(20, 20)); 73 View* v1 = new StaticSizedView(gfx::Size(20, 20));
74 host_->AddChildView(v1); 74 host_->AddChildView(v1);
75 View* v2 = new StaticSizedView(gfx::Size(10, 20)); 75 View* v2 = new StaticSizedView(gfx::Size(10, 20));
76 host_->AddChildView(v2); 76 host_->AddChildView(v2);
77 host_->SetBounds(0, 0, 10, 10); 77 host_->SetBounds(0, 0, 10, 10);
78
79 // Overflows by positioning views at the start and truncating anything that
80 // doesn't fit.
78 layout_->Layout(host_.get()); 81 layout_->Layout(host_.get());
79 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds()); 82 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds());
80 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds()); 83 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds());
84
85 // All values of main axis alignment should overflow in the same manner.
86 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START);
87 layout_->Layout(host_.get());
88 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds());
89 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds());
90
91 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
92 layout_->Layout(host_.get());
93 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds());
94 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds());
95
96 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END);
97 layout_->Layout(host_.get());
98 EXPECT_EQ(gfx::Rect(0, 0, 10, 10), v1->bounds());
99 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), v2->bounds());
81 } 100 }
82 101
83 TEST_F(BoxLayoutTest, NoSpace) { 102 TEST_F(BoxLayoutTest, NoSpace) {
84 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10)); 103 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10));
85 View* childView = new StaticSizedView(gfx::Size(20, 20)); 104 View* childView = new StaticSizedView(gfx::Size(20, 20));
86 host_->AddChildView(childView); 105 host_->AddChildView(childView);
87 host_->SetBounds(0, 0, 10, 10); 106 host_->SetBounds(0, 0, 10, 10);
88 layout_->Layout(host_.get()); 107 layout_->Layout(host_.get());
89 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), childView->bounds()); 108 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), childView->bounds());
90 } 109 }
91 110
92 TEST_F(BoxLayoutTest, InvisibleChild) { 111 TEST_F(BoxLayoutTest, InvisibleChild) {
93 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10)); 112 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10));
94 View* v1 = new StaticSizedView(gfx::Size(20, 20)); 113 View* v1 = new StaticSizedView(gfx::Size(20, 20));
95 v1->SetVisible(false); 114 v1->SetVisible(false);
96 host_->AddChildView(v1); 115 host_->AddChildView(v1);
97 View* v2 = new StaticSizedView(gfx::Size(10, 10)); 116 View* v2 = new StaticSizedView(gfx::Size(10, 10));
98 host_->AddChildView(v2); 117 host_->AddChildView(v2);
99 EXPECT_EQ(gfx::Size(30, 30), layout_->GetPreferredSize(host_.get())); 118 EXPECT_EQ(gfx::Size(30, 30), layout_->GetPreferredSize(host_.get()));
100 host_->SetBounds(0, 0, 30, 30); 119 host_->SetBounds(0, 0, 30, 30);
101 layout_->Layout(host_.get()); 120 layout_->Layout(host_.get());
102 EXPECT_EQ(gfx::Rect(10, 10, 10, 10), v2->bounds()); 121 EXPECT_EQ(gfx::Rect(10, 10, 10, 10), v2->bounds());
103 } 122 }
104 123
105 TEST_F(BoxLayoutTest, DistributeEmptySpace) { 124 TEST_F(BoxLayoutTest, MainAxisAlignmentFill) {
106 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10)); 125 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10));
107 layout_->set_spread_blank_space(true); 126 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_FILL);
108 127
109 View* v1 = new StaticSizedView(gfx::Size(20, 20)); 128 View* v1 = new StaticSizedView(gfx::Size(20, 20));
110 host_->AddChildView(v1); 129 host_->AddChildView(v1);
111 View* v2 = new StaticSizedView(gfx::Size(10, 10)); 130 View* v2 = new StaticSizedView(gfx::Size(10, 10));
112 host_->AddChildView(v2); 131 host_->AddChildView(v2);
113 EXPECT_EQ(gfx::Size(60, 40), layout_->GetPreferredSize(host_.get())); 132 EXPECT_EQ(gfx::Size(60, 40), layout_->GetPreferredSize(host_.get()));
114 133
115 host_->SetBounds(0, 0, 100, 40); 134 host_->SetBounds(0, 0, 100, 40);
116 layout_->Layout(host_.get()); 135 layout_->Layout(host_.get());
117 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1->bounds().ToString()); 136 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1->bounds().ToString());
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 168
150 EXPECT_EQ(v2->GetPreferredSize().width(), host_->bounds().width()) << i; 169 EXPECT_EQ(v2->GetPreferredSize().width(), host_->bounds().width()) << i;
151 EXPECT_EQ(v2->GetPreferredSize().height(), host_->bounds().height()) << i; 170 EXPECT_EQ(v2->GetPreferredSize().height(), host_->bounds().height()) << i;
152 EXPECT_EQ(v1->GetPreferredSize().width(), v1->bounds().width()) << i; 171 EXPECT_EQ(v1->GetPreferredSize().width(), v1->bounds().width()) << i;
153 EXPECT_EQ(v1->GetPreferredSize().height(), v1->bounds().height()) << i; 172 EXPECT_EQ(v1->GetPreferredSize().height(), v1->bounds().height()) << i;
154 EXPECT_EQ(v2->GetPreferredSize().width(), v2->bounds().width()) << i; 173 EXPECT_EQ(v2->GetPreferredSize().width(), v2->bounds().width()) << i;
155 EXPECT_EQ(v2->GetPreferredSize().height(), v2->bounds().height()) << i; 174 EXPECT_EQ(v2->GetPreferredSize().height(), v2->bounds().height()) << i;
156 } 175 }
157 } 176 }
158 177
178 TEST_F(BoxLayoutTest, MainAxisAlignmentHorizontal) {
179 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10));
180
181 View* v1 = new StaticSizedView(gfx::Size(20, 20));
182 host_->AddChildView(v1);
183 View* v2 = new StaticSizedView(gfx::Size(10, 10));
184 host_->AddChildView(v2);
185
186 host_->SetBounds(0, 0, 100, 40);
187
188 // Align children to the horizontal start by default.
189 layout_->Layout(host_.get());
190 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString());
191 EXPECT_EQ(gfx::Rect(40, 10, 10, 20).ToString(), v2->bounds().ToString());
192
193 // Ensure same results for MAIN_AXIS_ALIGNMENT_START.
194 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START);
195 layout_->Layout(host_.get());
196 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString());
197 EXPECT_EQ(gfx::Rect(40, 10, 10, 20).ToString(), v2->bounds().ToString());
198
199 // Aligns children to the center horizontally.
200 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
201 layout_->Layout(host_.get());
202 EXPECT_EQ(gfx::Rect(30, 10, 20, 20).ToString(), v1->bounds().ToString());
203 EXPECT_EQ(gfx::Rect(60, 10, 10, 20).ToString(), v2->bounds().ToString());
204
205 // Aligns children to the end of the host horizontally, accounting for the
206 // inside border spacing.
207 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END);
208 layout_->Layout(host_.get());
209 EXPECT_EQ(gfx::Rect(50, 10, 20, 20).ToString(), v1->bounds().ToString());
210 EXPECT_EQ(gfx::Rect(80, 10, 10, 20).ToString(), v2->bounds().ToString());
211 }
212
213 TEST_F(BoxLayoutTest, MainAxisAlignmentVertical) {
214 layout_.reset(new BoxLayout(BoxLayout::kVertical, 10, 10, 10));
215
216 View* v1 = new StaticSizedView(gfx::Size(20, 20));
217 host_->AddChildView(v1);
218 View* v2 = new StaticSizedView(gfx::Size(10, 10));
219 host_->AddChildView(v2);
220
221 host_->SetBounds(0, 0, 40, 100);
222
223 // Align children to the vertical start by default.
224 layout_->Layout(host_.get());
225 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString());
226 EXPECT_EQ(gfx::Rect(10, 40, 20, 10).ToString(), v2->bounds().ToString());
227
228 // Ensure same results for MAIN_AXIS_ALIGNMENT_START.
229 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_START);
230 layout_->Layout(host_.get());
231 EXPECT_EQ(gfx::Rect(10, 10, 20, 20).ToString(), v1->bounds().ToString());
232 EXPECT_EQ(gfx::Rect(10, 40, 20, 10).ToString(), v2->bounds().ToString());
233
234 // Aligns children to the center vertically.
235 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
236 layout_->Layout(host_.get());
237 EXPECT_EQ(gfx::Rect(10, 30, 20, 20).ToString(), v1->bounds().ToString());
238 EXPECT_EQ(gfx::Rect(10, 60, 20, 10).ToString(), v2->bounds().ToString());
239
240 // Aligns children to the end of the host vertically, accounting for the
241 // inside border spacing.
242 layout_->set_main_axis_alignment(BoxLayout::MAIN_AXIS_ALIGNMENT_END);
243 layout_->Layout(host_.get());
244 EXPECT_EQ(gfx::Rect(10, 50, 20, 20).ToString(), v1->bounds().ToString());
245 EXPECT_EQ(gfx::Rect(10, 80, 20, 10).ToString(), v2->bounds().ToString());
246 }
247
159 } // namespace views 248 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/layout/box_layout.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698