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 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 v1->SetVisible(false); | 135 v1->SetVisible(false); |
136 host_->AddChildView(v1); | 136 host_->AddChildView(v1); |
137 View* v2 = new StaticSizedView(gfx::Size(10, 10)); | 137 View* v2 = new StaticSizedView(gfx::Size(10, 10)); |
138 host_->AddChildView(v2); | 138 host_->AddChildView(v2); |
139 EXPECT_EQ(gfx::Size(30, 30), layout_->GetPreferredSize(host_.get())); | 139 EXPECT_EQ(gfx::Size(30, 30), layout_->GetPreferredSize(host_.get())); |
140 host_->SetBounds(0, 0, 30, 30); | 140 host_->SetBounds(0, 0, 30, 30); |
141 layout_->Layout(host_.get()); | 141 layout_->Layout(host_.get()); |
142 EXPECT_EQ(gfx::Rect(10, 10, 10, 10), v2->bounds()); | 142 EXPECT_EQ(gfx::Rect(10, 10, 10, 10), v2->bounds()); |
143 } | 143 } |
144 | 144 |
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) { | 145 TEST_F(BoxLayoutTest, UseHeightForWidth) { |
162 layout_.reset(new BoxLayout(BoxLayout::kVertical, 0, 0, 0)); | 146 layout_.reset(new BoxLayout(BoxLayout::kVertical, 0, 0, 0)); |
163 View* v1 = new StaticSizedView(gfx::Size(20, 10)); | 147 View* v1 = new StaticSizedView(gfx::Size(20, 10)); |
164 host_->AddChildView(v1); | 148 host_->AddChildView(v1); |
165 ProportionallySizedView* v2 = new ProportionallySizedView(2); | 149 ProportionallySizedView* v2 = new ProportionallySizedView(2); |
166 v2->set_preferred_width(10); | 150 v2->set_preferred_width(10); |
167 host_->AddChildView(v2); | 151 host_->AddChildView(v2); |
168 EXPECT_EQ(gfx::Size(20, 50), layout_->GetPreferredSize(host_.get())); | 152 EXPECT_EQ(gfx::Size(20, 50), layout_->GetPreferredSize(host_.get())); |
169 | 153 |
170 host_->SetBounds(0, 0, 20, 50); | 154 host_->SetBounds(0, 0, 20, 50); |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
354 EXPECT_EQ(gfx::Rect(25, 40, 10, 10).ToString(), v2->bounds().ToString()); | 338 EXPECT_EQ(gfx::Rect(25, 40, 10, 10).ToString(), v2->bounds().ToString()); |
355 | 339 |
356 // Aligns children to the end of the host horizontally, accounting for the | 340 // Aligns children to the end of the host horizontally, accounting for the |
357 // inside border spacing. | 341 // inside border spacing. |
358 layout_->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END); | 342 layout_->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END); |
359 layout_->Layout(host_.get()); | 343 layout_->Layout(host_.get()); |
360 EXPECT_EQ(gfx::Rect(30, 10, 20, 20).ToString(), v1->bounds().ToString()); | 344 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()); | 345 EXPECT_EQ(gfx::Rect(40, 40, 10, 10).ToString(), v2->bounds().ToString()); |
362 } | 346 } |
363 | 347 |
348 TEST_F(BoxLayoutTest, FlexAll) { | |
349 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10)); | |
350 layout_->SetDefaultFlex(1); | |
351 | |
352 View* v1 = new StaticSizedView(gfx::Size(20, 20)); | |
353 host_->AddChildView(v1); | |
354 View* v2 = new StaticSizedView(gfx::Size(10, 10)); | |
355 host_->AddChildView(v2); | |
356 EXPECT_EQ(gfx::Size(60, 40), layout_->GetPreferredSize(host_.get())); | |
357 | |
358 host_->SetBounds(0, 0, 100, 40); | |
359 layout_->Layout(host_.get()); | |
360 EXPECT_EQ(gfx::Rect(10, 10, 40, 20).ToString(), v1->bounds().ToString()); | |
361 EXPECT_EQ(gfx::Rect(60, 10, 30, 20).ToString(), v2->bounds().ToString()); | |
362 } | |
363 | |
364 TEST_F(BoxLayoutTest, FlexGrowVertical) { | |
365 layout_.reset(new BoxLayout(BoxLayout::kVertical, 10, 10, 10)); | |
366 | |
367 View* v1 = new StaticSizedView(gfx::Size(20, 20)); | |
368 host_->AddChildView(v1); | |
369 View* v2 = new StaticSizedView(gfx::Size(10, 10)); | |
370 host_->AddChildView(v2); | |
371 View* v3 = new StaticSizedView(gfx::Size(30, 30)); | |
372 host_->AddChildView(v3); | |
373 | |
374 host_->SetBounds(0, 0, 50, 130); | |
375 | |
376 // Views don't fill the available space by default. | |
377 layout_->Layout(host_.get()); | |
378 EXPECT_EQ(gfx::Rect(10, 10, 30, 20).ToString(), v1->bounds().ToString()); | |
379 EXPECT_EQ(gfx::Rect(10, 40, 30, 10).ToString(), v2->bounds().ToString()); | |
380 EXPECT_EQ(gfx::Rect(10, 60, 30, 30).ToString(), v3->bounds().ToString()); | |
381 | |
382 std::vector<BoxLayout::MainAxisAlignment> main_alignments; | |
383 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START); | |
384 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); | |
385 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END); | |
386 | |
387 for (size_t i = 0; i < main_alignments.size(); ++i) { | |
benwells
2014/07/03 07:29:59
What is this loop for? It doesn't seem to do anyth
calamity
2014/07/03 08:31:24
Oh wups. This needs to set the alignment. The idea
| |
388 // Set the first view to consume all free space. | |
389 layout_->SetFlexForViewAt(0, 1); | |
390 layout_->ClearFlexForViewAt(1); | |
391 layout_->ClearFlexForViewAt(2); | |
392 layout_->Layout(host_.get()); | |
393 EXPECT_EQ(gfx::Rect(10, 10, 30, 50).ToString(), v1->bounds().ToString()); | |
394 EXPECT_EQ(gfx::Rect(10, 70, 30, 10).ToString(), v2->bounds().ToString()); | |
395 EXPECT_EQ(gfx::Rect(10, 90, 30, 30).ToString(), v3->bounds().ToString()); | |
396 | |
397 // Set the third view to take 2/3s of the free space and leave the first | |
398 // view | |
399 // with 1/3. | |
400 layout_->SetFlexForViewAt(2, 2); | |
401 layout_->Layout(host_.get()); | |
402 EXPECT_EQ(gfx::Rect(10, 10, 30, 30).ToString(), v1->bounds().ToString()); | |
403 EXPECT_EQ(gfx::Rect(10, 50, 30, 10).ToString(), v2->bounds().ToString()); | |
404 EXPECT_EQ(gfx::Rect(10, 70, 30, 50).ToString(), v3->bounds().ToString()); | |
405 | |
406 // Clear the previously set flex values and set the second view to take all | |
407 // the free space. | |
408 layout_->ClearFlexForViewAt(0); | |
409 layout_->ClearFlexForViewAt(2); | |
410 layout_->SetFlexForViewAt(1, 1); | |
411 layout_->Layout(host_.get()); | |
412 EXPECT_EQ(gfx::Rect(10, 10, 30, 20).ToString(), v1->bounds().ToString()); | |
413 EXPECT_EQ(gfx::Rect(10, 40, 30, 40).ToString(), v2->bounds().ToString()); | |
414 EXPECT_EQ(gfx::Rect(10, 90, 30, 30).ToString(), v3->bounds().ToString()); | |
415 } | |
416 } | |
417 | |
418 TEST_F(BoxLayoutTest, FlexGrowHorizontalWithRemainder) { | |
419 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0)); | |
420 layout_->SetDefaultFlex(1); | |
421 std::vector<View*> views; | |
422 for (int i = 0; i < 5; ++i) { | |
423 View* view = new StaticSizedView(gfx::Size(10, 10)); | |
424 views.push_back(view); | |
425 host_->AddChildView(view); | |
426 } | |
427 | |
428 EXPECT_EQ(gfx::Size(50, 10), layout_->GetPreferredSize(host_.get())); | |
429 | |
430 host_->SetBounds(0, 0, 52, 10); | |
431 layout_->Layout(host_.get()); | |
432 // The 2nd and 4th views should have an extra pixel as they correspond to 20.8 | |
433 // and 41.6 which round up. | |
434 EXPECT_EQ(gfx::Rect(0, 0, 10, 10).ToString(), views[0]->bounds().ToString()); | |
435 EXPECT_EQ(gfx::Rect(10, 0, 11, 10).ToString(), views[1]->bounds().ToString()); | |
436 EXPECT_EQ(gfx::Rect(21, 0, 10, 10).ToString(), views[2]->bounds().ToString()); | |
437 EXPECT_EQ(gfx::Rect(31, 0, 11, 10).ToString(), views[3]->bounds().ToString()); | |
438 EXPECT_EQ(gfx::Rect(42, 0, 10, 10).ToString(), views[4]->bounds().ToString()); | |
439 } | |
440 | |
441 TEST_F(BoxLayoutTest, FlexGrowHorizontalWithRemainder2) { | |
442 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0)); | |
443 layout_->SetDefaultFlex(1); | |
444 std::vector<View*> views; | |
445 for (int i = 0; i < 4; ++i) { | |
446 View* view = new StaticSizedView(gfx::Size(1, 10)); | |
447 views.push_back(view); | |
448 host_->AddChildView(view); | |
449 } | |
450 | |
451 EXPECT_EQ(gfx::Size(4, 10), layout_->GetPreferredSize(host_.get())); | |
452 | |
453 host_->SetBounds(0, 0, 10, 10); | |
454 layout_->Layout(host_.get()); | |
455 // The 1st and 3rd views should have an extra pixel as they correspond to 2.5 | |
456 // and 7.5 which round up. | |
457 EXPECT_EQ(gfx::Rect(0, 0, 3, 10).ToString(), views[0]->bounds().ToString()); | |
458 EXPECT_EQ(gfx::Rect(3, 0, 2, 10).ToString(), views[1]->bounds().ToString()); | |
459 EXPECT_EQ(gfx::Rect(5, 0, 3, 10).ToString(), views[2]->bounds().ToString()); | |
460 EXPECT_EQ(gfx::Rect(8, 0, 2, 10).ToString(), views[3]->bounds().ToString()); | |
461 } | |
462 | |
463 TEST_F(BoxLayoutTest, FlexShrinkHorizontal) { | |
464 layout_.reset(new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10)); | |
465 | |
466 View* v1 = new StaticSizedView(gfx::Size(20, 20)); | |
467 host_->AddChildView(v1); | |
468 View* v2 = new StaticSizedView(gfx::Size(10, 10)); | |
469 host_->AddChildView(v2); | |
470 View* v3 = new StaticSizedView(gfx::Size(30, 30)); | |
471 host_->AddChildView(v3); | |
472 | |
473 host_->SetBounds(0, 0, 85, 50); | |
474 | |
475 // Truncate width by default. | |
476 layout_->Layout(host_.get()); | |
477 EXPECT_EQ(gfx::Rect(10, 10, 20, 30).ToString(), v1->bounds().ToString()); | |
478 EXPECT_EQ(gfx::Rect(40, 10, 10, 30).ToString(), v2->bounds().ToString()); | |
479 EXPECT_EQ(gfx::Rect(60, 10, 15, 30).ToString(), v3->bounds().ToString()); | |
480 | |
481 std::vector<BoxLayout::MainAxisAlignment> main_alignments; | |
482 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START); | |
483 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); | |
484 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END); | |
485 | |
486 for (size_t i = 0; i < main_alignments.size(); ++i) { | |
487 // Set the first view to shrink as much as necessary. | |
488 layout_->SetFlexForViewAt(0, 1); | |
489 layout_->ClearFlexForViewAt(1); | |
490 layout_->ClearFlexForViewAt(2); | |
491 layout_->Layout(host_.get()); | |
492 EXPECT_EQ(gfx::Rect(10, 10, 5, 30).ToString(), v1->bounds().ToString()); | |
493 EXPECT_EQ(gfx::Rect(25, 10, 10, 30).ToString(), v2->bounds().ToString()); | |
494 EXPECT_EQ(gfx::Rect(45, 10, 30, 30).ToString(), v3->bounds().ToString()); | |
495 | |
496 // Set the third view to shrink 2/3s of the free space and leave the first | |
497 // view with 1/3. | |
498 layout_->SetFlexForViewAt(2, 2); | |
499 layout_->Layout(host_.get()); | |
500 EXPECT_EQ(gfx::Rect(10, 10, 15, 30).ToString(), v1->bounds().ToString()); | |
501 EXPECT_EQ(gfx::Rect(35, 10, 10, 30).ToString(), v2->bounds().ToString()); | |
502 EXPECT_EQ(gfx::Rect(55, 10, 20, 30).ToString(), v3->bounds().ToString()); | |
503 | |
504 // Clear the previously set flex values and set the second view to take all | |
505 // the free space with MAIN_AXIS_ALIGNMENT_END set. This causes the second | |
506 // view to shrink to zero and the third view still doesn't fit so it | |
507 // overflows. | |
508 layout_->ClearFlexForViewAt(0); | |
509 layout_->ClearFlexForViewAt(2); | |
510 layout_->SetFlexForViewAt(1, 2); | |
511 layout_->Layout(host_.get()); | |
512 EXPECT_EQ(gfx::Rect(10, 10, 20, 30).ToString(), v1->bounds().ToString()); | |
513 // Conceptually this view is at 10, 40, 0, 0. | |
514 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(), v2->bounds().ToString()); | |
515 EXPECT_EQ(gfx::Rect(50, 10, 25, 30).ToString(), v3->bounds().ToString()); | |
516 } | |
517 } | |
518 | |
519 TEST_F(BoxLayoutTest, FlexShrinkVerticalWithRemainder) { | |
520 layout_.reset(new BoxLayout(BoxLayout::kVertical, 0, 0, 0)); | |
521 View* v1 = new StaticSizedView(gfx::Size(20, 10)); | |
522 host_->AddChildView(v1); | |
523 View* v2 = new StaticSizedView(gfx::Size(20, 20)); | |
524 host_->AddChildView(v2); | |
525 View* v3 = new StaticSizedView(gfx::Size(20, 10)); | |
526 host_->AddChildView(v3); | |
527 host_->SetBounds(0, 0, 20, 20); | |
528 | |
529 std::vector<BoxLayout::MainAxisAlignment> main_alignments; | |
530 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_START); | |
531 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); | |
532 main_alignments.push_back(BoxLayout::MAIN_AXIS_ALIGNMENT_END); | |
533 | |
534 for (size_t i = 0; i < main_alignments.size(); ++i) { | |
535 // The first view shrinks by 1/3 of the excess, the second view shrinks by | |
536 // 2/3 of the excess and the third view should maintain its preferred size. | |
537 layout_->SetFlexForViewAt(0, 1); | |
538 layout_->SetFlexForViewAt(1, 2); | |
539 layout_->ClearFlexForViewAt(2); | |
540 layout_->Layout(host_.get()); | |
541 EXPECT_EQ(gfx::Rect(0, 0, 20, 3).ToString(), v1->bounds().ToString()); | |
542 EXPECT_EQ(gfx::Rect(0, 3, 20, 7).ToString(), v2->bounds().ToString()); | |
543 EXPECT_EQ(gfx::Rect(0, 10, 20, 10).ToString(), v3->bounds().ToString()); | |
544 | |
545 // The second view shrinks to 2/3 of the excess, the third view shrinks to | |
546 // 1/3 of the excess and the first view should maintain its preferred size. | |
547 layout_->ClearFlexForViewAt(0); | |
548 layout_->SetFlexForViewAt(1, 2); | |
549 layout_->SetFlexForViewAt(2, 1); | |
550 layout_->Layout(host_.get()); | |
551 EXPECT_EQ(gfx::Rect(0, 0, 20, 10).ToString(), v1->bounds().ToString()); | |
552 EXPECT_EQ(gfx::Rect(0, 10, 20, 7).ToString(), v2->bounds().ToString()); | |
553 EXPECT_EQ(gfx::Rect(0, 17, 20, 3).ToString(), v3->bounds().ToString()); | |
554 | |
555 // Each view shrinks equally to fit within the available space. | |
556 layout_->SetFlexForViewAt(0, 1); | |
557 layout_->SetFlexForViewAt(1, 1); | |
558 layout_->SetFlexForViewAt(2, 1); | |
559 layout_->Layout(host_.get()); | |
560 EXPECT_EQ(gfx::Rect(0, 0, 20, 3).ToString(), v1->bounds().ToString()); | |
561 EXPECT_EQ(gfx::Rect(0, 3, 20, 14).ToString(), v2->bounds().ToString()); | |
562 EXPECT_EQ(gfx::Rect(0, 17, 20, 3).ToString(), v3->bounds().ToString()); | |
563 } | |
564 } | |
565 | |
364 } // namespace views | 566 } // namespace views |
OLD | NEW |