OLD | NEW |
(Empty) | |
| 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 <stddef.h> |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "ui/views/anchor_attribute.h" |
| 9 #include "ui/views/fill_attribute.h" |
| 10 #include "ui/views/layout/align_layout.h" |
| 11 #include "ui/views/test/test_views.h" |
| 12 #include "ui/views/view.h" |
| 13 |
| 14 namespace views { |
| 15 |
| 16 namespace { |
| 17 |
| 18 class AnchorLayoutTest : public testing::Test { |
| 19 public: |
| 20 void SetUp() override { |
| 21 host_.reset(new View); |
| 22 host_->SetBounds(0, 0, 100, 100); |
| 23 } |
| 24 |
| 25 std::unique_ptr<View> host_; |
| 26 }; |
| 27 |
| 28 } // namespace |
| 29 |
| 30 TEST_F(AnchorLayoutTest, Empty) { |
| 31 AlignLayout* layout = new AlignLayout(); |
| 32 host_->SetLayoutManager(layout); |
| 33 EXPECT_EQ(gfx::Size(0, 0), layout->GetPreferredSize(host_.get())); |
| 34 } |
| 35 |
| 36 TEST_F(AnchorLayoutTest, AnchorLeftTop) { |
| 37 AlignLayout* layout = new AlignLayout(); |
| 38 host_->SetLayoutManager(layout); |
| 39 View* v1 = new View(); |
| 40 v1->attributes().Add( |
| 41 base::WrapUnique(new AnchorAttribute({Anchor::Left, Anchor::Top}))); |
| 42 gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
| 43 v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
| 44 host_->AddChildView(v1); |
| 45 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
| 46 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
| 47 host_->SetBounds(0, 0, 120, 120); |
| 48 host_->Layout(); |
| 49 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
| 50 EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
| 51 } |
| 52 |
| 53 TEST_F(AnchorLayoutTest, AnchorTop) { |
| 54 AlignLayout* layout = new AlignLayout(); |
| 55 host_->SetLayoutManager(layout); |
| 56 View* v1 = new View(); |
| 57 v1->attributes().Add(base::WrapUnique(new AnchorAttribute({Anchor::Top}))); |
| 58 gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
| 59 v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
| 60 host_->AddChildView(v1); |
| 61 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
| 62 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
| 63 host_->SetBounds(0, 0, 120, 120); |
| 64 host_->Layout(); |
| 65 EXPECT_EQ(gfx::Rect(v1pos.x() + 10, v1pos.y(), 10, 10), v1->bounds()); |
| 66 EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
| 67 } |
| 68 |
| 69 TEST_F(AnchorLayoutTest, AnchorLeft) { |
| 70 AlignLayout* layout = new AlignLayout(); |
| 71 host_->SetLayoutManager(layout); |
| 72 View* v1 = new View(); |
| 73 v1->attributes().Add(base::WrapUnique(new AnchorAttribute({Anchor::Left}))); |
| 74 gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
| 75 v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
| 76 host_->AddChildView(v1); |
| 77 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
| 78 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
| 79 host_->SetBounds(0, 0, 120, 120); |
| 80 host_->Layout(); |
| 81 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y() + 10, 10, 10), v1->bounds()); |
| 82 EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
| 83 } |
| 84 |
| 85 TEST_F(AnchorLayoutTest, AnchorRightTop) { |
| 86 AlignLayout* layout = new AlignLayout(); |
| 87 host_->SetLayoutManager(layout); |
| 88 View* v1 = new View(); |
| 89 v1->attributes().Add( |
| 90 base::WrapUnique(new AnchorAttribute({Anchor::Right, Anchor::Top}))); |
| 91 gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
| 92 v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
| 93 host_->AddChildView(v1); |
| 94 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
| 95 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
| 96 host_->SetBounds(0, 0, 120, 120); |
| 97 host_->Layout(); |
| 98 EXPECT_EQ(gfx::Rect(v1pos.x() + 20, v1pos.y(), 10, 10), v1->bounds()); |
| 99 EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
| 100 } |
| 101 |
| 102 TEST_F(AnchorLayoutTest, AnchorLeftTopRight) { |
| 103 AlignLayout* layout = new AlignLayout(); |
| 104 host_->SetLayoutManager(layout); |
| 105 View* v1 = new View(); |
| 106 v1->attributes().Add(base::WrapUnique( |
| 107 new AnchorAttribute({Anchor::Left, Anchor::Top, Anchor::Right}))); |
| 108 gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
| 109 v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
| 110 host_->AddChildView(v1); |
| 111 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
| 112 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
| 113 host_->SetBounds(0, 0, 120, 120); |
| 114 host_->Layout(); |
| 115 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 30, 10), v1->bounds()); |
| 116 EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
| 117 } |
| 118 |
| 119 TEST_F(AnchorLayoutTest, AnchorLeftTopBottom) { |
| 120 AlignLayout* layout = new AlignLayout(); |
| 121 host_->SetLayoutManager(layout); |
| 122 View* v1 = new View(); |
| 123 v1->attributes().Add(base::WrapUnique( |
| 124 new AnchorAttribute({Anchor::Left, Anchor::Top, Anchor::Bottom}))); |
| 125 gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
| 126 v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
| 127 host_->AddChildView(v1); |
| 128 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
| 129 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
| 130 host_->SetBounds(0, 0, 120, 120); |
| 131 host_->Layout(); |
| 132 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 30), v1->bounds()); |
| 133 EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
| 134 } |
| 135 |
| 136 TEST_F(AnchorLayoutTest, AnchorLeftRight) { |
| 137 AlignLayout* layout = new AlignLayout(); |
| 138 host_->SetLayoutManager(layout); |
| 139 View* v1 = new View(); |
| 140 v1->attributes().Add(base::WrapUnique( |
| 141 new AnchorAttribute({Anchor::Left, Anchor::Right}))); |
| 142 gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
| 143 v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
| 144 host_->AddChildView(v1); |
| 145 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
| 146 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
| 147 host_->SetBounds(0, 0, 120, 120); |
| 148 host_->Layout(); |
| 149 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y() + 10, 30, 10), v1->bounds()); |
| 150 EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
| 151 } |
| 152 |
| 153 TEST_F(AnchorLayoutTest, AnchorTopBottom) { |
| 154 AlignLayout* layout = new AlignLayout(); |
| 155 host_->SetLayoutManager(layout); |
| 156 View* v1 = new View(); |
| 157 v1->attributes().Add(base::WrapUnique( |
| 158 new AnchorAttribute({Anchor::Top, Anchor::Bottom}))); |
| 159 gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
| 160 v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
| 161 host_->AddChildView(v1); |
| 162 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
| 163 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
| 164 host_->SetBounds(0, 0, 120, 120); |
| 165 host_->Layout(); |
| 166 EXPECT_EQ(gfx::Rect(v1pos.x() + 10, v1pos.y(), 10, 30), v1->bounds()); |
| 167 EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
| 168 } |
| 169 |
| 170 TEST_F(AnchorLayoutTest, AnchorNone) { |
| 171 AlignLayout* layout = new AlignLayout(); |
| 172 host_->SetLayoutManager(layout); |
| 173 View* v1 = new View(); |
| 174 v1->attributes().Add(base::WrapUnique(new AnchorAttribute(Anchors()))); |
| 175 gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
| 176 v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
| 177 host_->AddChildView(v1); |
| 178 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
| 179 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
| 180 host_->SetBounds(0, 0, 120, 120); |
| 181 host_->Layout(); |
| 182 EXPECT_EQ(gfx::Rect(v1pos.x() + 10, v1pos.y() + 10, 10, 10), v1->bounds()); |
| 183 EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
| 184 } |
| 185 |
| 186 TEST_F(AnchorLayoutTest, AnchorRightBottom) { |
| 187 AlignLayout* layout = new AlignLayout(); |
| 188 host_->SetLayoutManager(layout); |
| 189 View* v1 = new View(); |
| 190 v1->attributes().Add(base::WrapUnique( |
| 191 new AnchorAttribute({Anchor::Right, Anchor::Bottom}))); |
| 192 gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
| 193 v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
| 194 host_->AddChildView(v1); |
| 195 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
| 196 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
| 197 host_->SetBounds(0, 0, 120, 120); |
| 198 host_->Layout(); |
| 199 EXPECT_EQ(gfx::Rect(v1pos.x() + 20, v1pos.y() + 20, 10, 10), v1->bounds()); |
| 200 EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
| 201 } |
| 202 |
| 203 TEST_F(AnchorLayoutTest, AnchorRight) { |
| 204 AlignLayout* layout = new AlignLayout(); |
| 205 host_->SetLayoutManager(layout); |
| 206 View* v1 = new View(); |
| 207 v1->attributes().Add(base::WrapUnique(new AnchorAttribute({Anchor::Right}))); |
| 208 gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
| 209 v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
| 210 host_->AddChildView(v1); |
| 211 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
| 212 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
| 213 host_->SetBounds(0, 0, 120, 120); |
| 214 host_->Layout(); |
| 215 EXPECT_EQ(gfx::Rect(v1pos.x() + 20, v1pos.y() + 10, 10, 10), v1->bounds()); |
| 216 EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
| 217 } |
| 218 |
| 219 TEST_F(AnchorLayoutTest, AnchorBottom) { |
| 220 AlignLayout* layout = new AlignLayout(); |
| 221 host_->SetLayoutManager(layout); |
| 222 View* v1 = new View(); |
| 223 v1->attributes().Add(base::WrapUnique(new AnchorAttribute({Anchor::Bottom}))); |
| 224 gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
| 225 v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
| 226 host_->AddChildView(v1); |
| 227 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
| 228 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
| 229 host_->SetBounds(0, 0, 120, 120); |
| 230 host_->Layout(); |
| 231 EXPECT_EQ(gfx::Rect(v1pos.x() + 10, v1pos.y() + 20, 10, 10), v1->bounds()); |
| 232 EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
| 233 } |
| 234 |
| 235 TEST_F(AnchorLayoutTest, AnchorTopRightBottom) { |
| 236 AlignLayout* layout = new AlignLayout(); |
| 237 host_->SetLayoutManager(layout); |
| 238 View* v1 = new View(); |
| 239 v1->attributes().Add(base::WrapUnique( |
| 240 new AnchorAttribute({Anchor::Top, Anchor::Right, Anchor::Bottom}))); |
| 241 gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
| 242 v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
| 243 host_->AddChildView(v1); |
| 244 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
| 245 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
| 246 host_->SetBounds(0, 0, 120, 120); |
| 247 host_->Layout(); |
| 248 EXPECT_EQ(gfx::Rect(v1pos.x() + 20, v1pos.y(), 10, 30), v1->bounds()); |
| 249 EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
| 250 } |
| 251 |
| 252 TEST_F(AnchorLayoutTest, AnchorLeftTopRightBottom) { |
| 253 AlignLayout* layout = new AlignLayout(); |
| 254 host_->SetLayoutManager(layout); |
| 255 View* v1 = new View(); |
| 256 v1->attributes().Add(base::WrapUnique(new AnchorAttribute( |
| 257 {Anchor::Left, Anchor::Top, Anchor::Right, Anchor::Bottom}))); |
| 258 gfx::Point v1pos((host_->width() - 10) / 2, (host_->height() - 10) / 2); |
| 259 v1->SetBounds(v1pos.x(), v1pos.y(), 10, 10); |
| 260 host_->AddChildView(v1); |
| 261 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 10, 10), v1->bounds()); |
| 262 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
| 263 host_->SetBounds(0, 0, 120, 120); |
| 264 host_->Layout(); |
| 265 EXPECT_EQ(gfx::Rect(v1pos.x(), v1pos.y(), 30, 30), v1->bounds()); |
| 266 EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
| 267 } |
| 268 |
| 269 TEST_F(AnchorLayoutTest, FillTopAnchorAll) { |
| 270 AlignLayout* layout = new AlignLayout(); |
| 271 host_->SetLayoutManager(layout); |
| 272 View* v1 = new View(); |
| 273 v1->attributes().Add(base::WrapUnique(new FillAttribute(Fill::Top))); |
| 274 v1->attributes().Add(base::WrapUnique(new AnchorAttribute( |
| 275 {Anchor::Left, Anchor::Top, Anchor::Right, Anchor::Bottom}))); |
| 276 v1->SetBounds(0, 0, 10, 10); |
| 277 host_->AddChildView(v1); |
| 278 host_->Layout(); |
| 279 EXPECT_EQ(gfx::Rect(0, 0, 100, 10), v1->bounds()); |
| 280 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
| 281 host_->SetBounds(0, 0, 120, 120); |
| 282 host_->Layout(); |
| 283 EXPECT_EQ(gfx::Rect(0, 0, 120, 30), v1->bounds()); |
| 284 EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
| 285 } |
| 286 |
| 287 TEST_F(AnchorLayoutTest, FillBottomAnchorAll) { |
| 288 AlignLayout* layout = new AlignLayout(); |
| 289 host_->SetLayoutManager(layout); |
| 290 View* v1 = new View(); |
| 291 v1->attributes().Add(base::WrapUnique(new FillAttribute(Fill::Bottom))); |
| 292 v1->attributes().Add(base::WrapUnique(new AnchorAttribute( |
| 293 {Anchor::Left, Anchor::Top, Anchor::Right, Anchor::Bottom}))); |
| 294 v1->SetBounds(0, 0, 10, 10); |
| 295 host_->AddChildView(v1); |
| 296 host_->Layout(); |
| 297 EXPECT_EQ(gfx::Rect(0, 90, 100, 10), v1->bounds()); |
| 298 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
| 299 host_->SetBounds(0, 0, 120, 120); |
| 300 host_->Layout(); |
| 301 EXPECT_EQ(gfx::Rect(0, 90, 120, 30), v1->bounds()); |
| 302 EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
| 303 } |
| 304 |
| 305 TEST_F(AnchorLayoutTest, FillLeftAnchorAll) { |
| 306 AlignLayout* layout = new AlignLayout(); |
| 307 host_->SetLayoutManager(layout); |
| 308 View* v1 = new View(); |
| 309 v1->attributes().Add(base::WrapUnique(new FillAttribute(Fill::Left))); |
| 310 v1->attributes().Add(base::WrapUnique(new AnchorAttribute( |
| 311 {Anchor::Left, Anchor::Top, Anchor::Right, Anchor::Bottom}))); |
| 312 v1->SetBounds(0, 0, 10, 10); |
| 313 host_->AddChildView(v1); |
| 314 host_->Layout(); |
| 315 EXPECT_EQ(gfx::Rect(0, 0, 10, 100), v1->bounds()); |
| 316 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
| 317 host_->SetBounds(0, 0, 120, 120); |
| 318 host_->Layout(); |
| 319 EXPECT_EQ(gfx::Rect(0, 0, 30, 120), v1->bounds()); |
| 320 EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
| 321 } |
| 322 |
| 323 TEST_F(AnchorLayoutTest, FillRightAnchorAll) { |
| 324 AlignLayout* layout = new AlignLayout(); |
| 325 host_->SetLayoutManager(layout); |
| 326 View* v1 = new View(); |
| 327 v1->attributes().Add(base::WrapUnique(new FillAttribute(Fill::Right))); |
| 328 v1->attributes().Add(base::WrapUnique(new AnchorAttribute( |
| 329 {Anchor::Left, Anchor::Top, Anchor::Right, Anchor::Bottom}))); |
| 330 v1->SetBounds(0, 0, 10, 10); |
| 331 host_->AddChildView(v1); |
| 332 host_->Layout(); |
| 333 EXPECT_EQ(gfx::Rect(90, 0, 10, 100), v1->bounds()); |
| 334 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), host_->bounds()); |
| 335 host_->SetBounds(0, 0, 120, 120); |
| 336 host_->Layout(); |
| 337 EXPECT_EQ(gfx::Rect(90, 0, 30, 120), v1->bounds()); |
| 338 EXPECT_EQ(gfx::Rect(0, 0, 120, 120), host_->bounds()); |
| 339 } |
| 340 |
| 341 } // namespace views |
OLD | NEW |