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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "ui/views/test/test_views.h" | 10 #include "ui/views/test/test_views.h" |
11 #include "ui/views/view.h" | 11 #include "ui/views/view.h" |
| 12 #include "ui/views/view_properties.h" |
12 | 13 |
13 namespace views { | 14 namespace views { |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 class BoxLayoutTest : public testing::Test { | 18 class BoxLayoutTest : public testing::Test { |
18 public: | 19 public: |
19 void SetUp() override { host_.reset(new View); } | 20 void SetUp() override { host_.reset(new View); } |
20 | 21 |
21 std::unique_ptr<View> host_; | 22 std::unique_ptr<View> host_; |
(...skipping 600 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
622 TEST_F(BoxLayoutTest, MinimumCrossAxisHorizontal) { | 623 TEST_F(BoxLayoutTest, MinimumCrossAxisHorizontal) { |
623 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0); | 624 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0); |
624 host_->SetLayoutManager(layout); | 625 host_->SetLayoutManager(layout); |
625 View* v1 = new StaticSizedView(gfx::Size(20, 10)); | 626 View* v1 = new StaticSizedView(gfx::Size(20, 10)); |
626 host_->AddChildView(v1); | 627 host_->AddChildView(v1); |
627 layout->set_minimum_cross_axis_size(30); | 628 layout->set_minimum_cross_axis_size(30); |
628 | 629 |
629 EXPECT_EQ(gfx::Size(20, 30), layout->GetPreferredSize(host_.get())); | 630 EXPECT_EQ(gfx::Size(20, 30), layout->GetPreferredSize(host_.get())); |
630 } | 631 } |
631 | 632 |
| 633 TEST_F(BoxLayoutTest, MarginsUncollapsedHorizontal) { |
| 634 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, false); |
| 635 host_->SetLayoutManager(layout); |
| 636 View* v1 = new StaticSizedView(gfx::Size(20, 10)); |
| 637 v1->SetProperty(kMarginsKey, new gfx::Insets(5, 5, 5, 5)); |
| 638 host_->AddChildView(v1); |
| 639 View* v2 = new StaticSizedView(gfx::Size(20, 10)); |
| 640 v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 6, 4)); |
| 641 host_->AddChildView(v2); |
| 642 |
| 643 EXPECT_EQ(gfx::Size(58, 22), layout->GetPreferredSize(host_.get())); |
| 644 host_->SizeToPreferredSize(); |
| 645 layout->Layout(host_.get()); |
| 646 EXPECT_EQ(gfx::Rect(5, 5, 20, 12), v1->bounds()); |
| 647 EXPECT_EQ(gfx::Rect(34, 6, 20, 10), v2->bounds()); |
| 648 } |
| 649 |
| 650 TEST_F(BoxLayoutTest, MarginsCollapsedHorizontal) { |
| 651 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, true); |
| 652 host_->SetLayoutManager(layout); |
| 653 View* v1 = new StaticSizedView(gfx::Size(20, 10)); |
| 654 v1->SetProperty(kMarginsKey, new gfx::Insets(5, 5, 5, 5)); |
| 655 host_->AddChildView(v1); |
| 656 View* v2 = new StaticSizedView(gfx::Size(20, 10)); |
| 657 v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 6, 4)); |
| 658 host_->AddChildView(v2); |
| 659 |
| 660 EXPECT_EQ(gfx::Size(54, 22), layout->GetPreferredSize(host_.get())); |
| 661 host_->SizeToPreferredSize(); |
| 662 layout->Layout(host_.get()); |
| 663 EXPECT_EQ(gfx::Rect(5, 5, 20, 12), v1->bounds()); |
| 664 EXPECT_EQ(gfx::Rect(30, 6, 20, 10), v2->bounds()); |
| 665 } |
| 666 |
| 667 TEST_F(BoxLayoutTest, MarginsUncollapsedVertical) { |
| 668 BoxLayout* layout = new BoxLayout(BoxLayout::kVertical, 0, 0, 0, false); |
| 669 host_->SetLayoutManager(layout); |
| 670 View* v1 = new StaticSizedView(gfx::Size(20, 10)); |
| 671 v1->SetProperty(kMarginsKey, new gfx::Insets(5, 5, 5, 5)); |
| 672 host_->AddChildView(v1); |
| 673 View* v2 = new StaticSizedView(gfx::Size(20, 10)); |
| 674 v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 6, 4)); |
| 675 host_->AddChildView(v2); |
| 676 |
| 677 EXPECT_EQ(gfx::Size(30, 42), layout->GetPreferredSize(host_.get())); |
| 678 host_->SizeToPreferredSize(); |
| 679 layout->Layout(host_.get()); |
| 680 EXPECT_EQ(gfx::Rect(5, 5, 20, 10), v1->bounds()); |
| 681 EXPECT_EQ(gfx::Rect(4, 26, 22, 10), v2->bounds()); |
| 682 } |
| 683 |
| 684 TEST_F(BoxLayoutTest, MarginsCollapsedVertical) { |
| 685 BoxLayout* layout = new BoxLayout(BoxLayout::kVertical, 0, 0, 0, true); |
| 686 host_->SetLayoutManager(layout); |
| 687 View* v1 = new StaticSizedView(gfx::Size(20, 10)); |
| 688 v1->SetProperty(kMarginsKey, new gfx::Insets(5, 5, 5, 5)); |
| 689 host_->AddChildView(v1); |
| 690 View* v2 = new StaticSizedView(gfx::Size(20, 10)); |
| 691 v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 6, 4)); |
| 692 host_->AddChildView(v2); |
| 693 |
| 694 EXPECT_EQ(gfx::Size(30, 37), layout->GetPreferredSize(host_.get())); |
| 695 host_->SizeToPreferredSize(); |
| 696 layout->Layout(host_.get()); |
| 697 EXPECT_EQ(gfx::Rect(5, 5, 20, 10), v1->bounds()); |
| 698 EXPECT_EQ(gfx::Rect(4, 21, 22, 10), v2->bounds()); |
| 699 } |
| 700 |
| 701 TEST_F(BoxLayoutTest, UnbalancedMarginsUncollapsedHorizontal) { |
| 702 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, false); |
| 703 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER); |
| 704 host_->SetLayoutManager(layout); |
| 705 View* v1 = new StaticSizedView(gfx::Size(20, 10)); |
| 706 v1->SetProperty(kMarginsKey, new gfx::Insets(5, 5, 4, 4)); |
| 707 host_->AddChildView(v1); |
| 708 View* v2 = new StaticSizedView(gfx::Size(20, 10)); |
| 709 v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 3, 6)); |
| 710 host_->AddChildView(v2); |
| 711 |
| 712 EXPECT_EQ(gfx::Size(59, 20), layout->GetPreferredSize(host_.get())); |
| 713 host_->SizeToPreferredSize(); |
| 714 layout->Layout(host_.get()); |
| 715 EXPECT_EQ(gfx::Rect(5, 5, 20, 10), v1->bounds()); |
| 716 EXPECT_EQ(gfx::Rect(33, 6, 20, 10), v2->bounds()); |
| 717 } |
| 718 |
| 719 TEST_F(BoxLayoutTest, UnbalancedMarginsCollapsedHorizontal) { |
| 720 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, true); |
| 721 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER); |
| 722 host_->SetLayoutManager(layout); |
| 723 View* v1 = new StaticSizedView(gfx::Size(20, 10)); |
| 724 v1->SetProperty(kMarginsKey, new gfx::Insets(5, 5, 4, 4)); |
| 725 host_->AddChildView(v1); |
| 726 View* v2 = new StaticSizedView(gfx::Size(20, 10)); |
| 727 v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 3, 6)); |
| 728 host_->AddChildView(v2); |
| 729 |
| 730 EXPECT_EQ(gfx::Size(55, 20), layout->GetPreferredSize(host_.get())); |
| 731 host_->SizeToPreferredSize(); |
| 732 layout->Layout(host_.get()); |
| 733 EXPECT_EQ(gfx::Rect(5, 5, 20, 10), v1->bounds()); |
| 734 EXPECT_EQ(gfx::Rect(29, 6, 20, 10), v2->bounds()); |
| 735 } |
| 736 |
| 737 TEST_F(BoxLayoutTest, UnbalancedMarginsUncollapsedVertical) { |
| 738 BoxLayout* layout = new BoxLayout(BoxLayout::kVertical, 0, 0, 0, false); |
| 739 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER); |
| 740 host_->SetLayoutManager(layout); |
| 741 View* v1 = new StaticSizedView(gfx::Size(20, 10)); |
| 742 v1->SetProperty(kMarginsKey, new gfx::Insets(4, 5, 5, 3)); |
| 743 host_->AddChildView(v1); |
| 744 View* v2 = new StaticSizedView(gfx::Size(20, 10)); |
| 745 v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 3, 5)); |
| 746 host_->AddChildView(v2); |
| 747 |
| 748 EXPECT_EQ(gfx::Size(30, 38), layout->GetPreferredSize(host_.get())); |
| 749 host_->SizeToPreferredSize(); |
| 750 layout->Layout(host_.get()); |
| 751 EXPECT_EQ(gfx::Rect(5, 4, 20, 10), v1->bounds()); |
| 752 EXPECT_EQ(gfx::Rect(5, 25, 20, 10), v2->bounds()); |
| 753 } |
| 754 |
| 755 TEST_F(BoxLayoutTest, UnbalancedMarginsCollapsedVertical) { |
| 756 BoxLayout* layout = new BoxLayout(BoxLayout::kVertical, 0, 0, 0, true); |
| 757 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER); |
| 758 host_->SetLayoutManager(layout); |
| 759 View* v1 = new StaticSizedView(gfx::Size(20, 10)); |
| 760 v1->SetProperty(kMarginsKey, new gfx::Insets(4, 5, 5, 3)); |
| 761 host_->AddChildView(v1); |
| 762 View* v2 = new StaticSizedView(gfx::Size(20, 10)); |
| 763 v2->SetProperty(kMarginsKey, new gfx::Insets(6, 4, 3, 5)); |
| 764 host_->AddChildView(v2); |
| 765 |
| 766 EXPECT_EQ(gfx::Size(30, 33), layout->GetPreferredSize(host_.get())); |
| 767 host_->SizeToPreferredSize(); |
| 768 layout->Layout(host_.get()); |
| 769 EXPECT_EQ(gfx::Rect(5, 4, 20, 10), v1->bounds()); |
| 770 EXPECT_EQ(gfx::Rect(5, 20, 20, 10), v2->bounds()); |
| 771 } |
| 772 |
| 773 TEST_F(BoxLayoutTest, OverlappingCrossMarginsAlignEnd) { |
| 774 { |
| 775 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, false); |
| 776 host_->SetLayoutManager(layout); |
| 777 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END); |
| 778 View* v1 = new StaticSizedView(gfx::Size(20, 4)); |
| 779 v1->SetProperty(kMarginsKey, new gfx::Insets(3, 0, 0, 0)); |
| 780 host_->AddChildView(v1); |
| 781 View* v2 = new StaticSizedView(gfx::Size(20, 5)); |
| 782 v2->SetProperty(kMarginsKey, new gfx::Insets(0, 0, 2, 0)); |
| 783 host_->AddChildView(v2); |
| 784 |
| 785 EXPECT_EQ(9, layout->GetPreferredSize(host_.get()).height()); |
| 786 } |
| 787 host_->RemoveAllChildViews(true); |
| 788 { |
| 789 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, true); |
| 790 host_->SetLayoutManager(layout); |
| 791 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_END); |
| 792 View* v1 = new StaticSizedView(gfx::Size(20, 4)); |
| 793 v1->SetProperty(kMarginsKey, new gfx::Insets(3, 0, 0, 0)); |
| 794 host_->AddChildView(v1); |
| 795 View* v2 = new StaticSizedView(gfx::Size(20, 5)); |
| 796 v2->SetProperty(kMarginsKey, new gfx::Insets(0, 0, 2, 0)); |
| 797 host_->AddChildView(v2); |
| 798 |
| 799 EXPECT_EQ(9, layout->GetPreferredSize(host_.get()).height()); |
| 800 } |
| 801 } |
| 802 |
| 803 TEST_F(BoxLayoutTest, OverlappingCrossMarginsAlignStretch) { |
| 804 { |
| 805 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, false); |
| 806 host_->SetLayoutManager(layout); |
| 807 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH); |
| 808 View* v1 = new StaticSizedView(gfx::Size(20, 4)); |
| 809 v1->SetProperty(kMarginsKey, new gfx::Insets(3, 0, 0, 0)); |
| 810 host_->AddChildView(v1); |
| 811 View* v2 = new StaticSizedView(gfx::Size(20, 5)); |
| 812 v2->SetProperty(kMarginsKey, new gfx::Insets(0, 0, 2, 0)); |
| 813 host_->AddChildView(v2); |
| 814 |
| 815 EXPECT_EQ(10, layout->GetPreferredSize(host_.get()).height()); |
| 816 } |
| 817 host_->RemoveAllChildViews(true); |
| 818 { |
| 819 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, true); |
| 820 host_->SetLayoutManager(layout); |
| 821 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH); |
| 822 View* v1 = new StaticSizedView(gfx::Size(20, 4)); |
| 823 v1->SetProperty(kMarginsKey, new gfx::Insets(3, 0, 0, 0)); |
| 824 host_->AddChildView(v1); |
| 825 View* v2 = new StaticSizedView(gfx::Size(20, 5)); |
| 826 v2->SetProperty(kMarginsKey, new gfx::Insets(0, 0, 2, 0)); |
| 827 host_->AddChildView(v2); |
| 828 |
| 829 EXPECT_EQ(10, layout->GetPreferredSize(host_.get()).height()); |
| 830 } |
| 831 } |
| 832 |
| 833 TEST_F(BoxLayoutTest, OverlappingCrossMarginsAlignStart) { |
| 834 { |
| 835 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, false); |
| 836 host_->SetLayoutManager(layout); |
| 837 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_START); |
| 838 View* v1 = new StaticSizedView(gfx::Size(20, 4)); |
| 839 v1->SetProperty(kMarginsKey, new gfx::Insets(0, 0, 3, 0)); |
| 840 host_->AddChildView(v1); |
| 841 View* v2 = new StaticSizedView(gfx::Size(20, 5)); |
| 842 v2->SetProperty(kMarginsKey, new gfx::Insets(2, 0, 0, 0)); |
| 843 host_->AddChildView(v2); |
| 844 |
| 845 EXPECT_EQ(9, layout->GetPreferredSize(host_.get()).height()); |
| 846 } |
| 847 host_->RemoveAllChildViews(true); |
| 848 { |
| 849 BoxLayout* layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0, true); |
| 850 host_->SetLayoutManager(layout); |
| 851 layout->set_cross_axis_alignment(BoxLayout::CROSS_AXIS_ALIGNMENT_START); |
| 852 View* v1 = new StaticSizedView(gfx::Size(20, 4)); |
| 853 v1->SetProperty(kMarginsKey, new gfx::Insets(0, 0, 3, 0)); |
| 854 host_->AddChildView(v1); |
| 855 View* v2 = new StaticSizedView(gfx::Size(20, 5)); |
| 856 v2->SetProperty(kMarginsKey, new gfx::Insets(2, 0, 0, 0)); |
| 857 host_->AddChildView(v2); |
| 858 |
| 859 EXPECT_EQ(9, layout->GetPreferredSize(host_.get()).height()); |
| 860 } |
| 861 } |
| 862 |
632 } // namespace views | 863 } // namespace views |
OLD | NEW |