OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "core/layout/ng/ng_block_layout_algorithm.h" | 5 #include "core/layout/ng/ng_block_layout_algorithm.h" |
6 | 6 |
7 #include "core/layout/ng/ng_block_node.h" | 7 #include "core/layout/ng/ng_block_node.h" |
8 #include "core/layout/ng/ng_constraint_space.h" | 8 #include "core/layout/ng/ng_constraint_space.h" |
9 #include "core/layout/ng/ng_constraint_space_builder.h" | 9 #include "core/layout/ng/ng_constraint_space_builder.h" |
10 #include "core/layout/ng/ng_physical_fragment_base.h" | 10 #include "core/layout/ng/ng_physical_fragment_base.h" |
11 #include "core/layout/ng/ng_physical_fragment.h" | 11 #include "core/layout/ng/ng_physical_fragment.h" |
12 #include "core/layout/ng/ng_layout_coordinator.h" | 12 #include "core/layout/ng/ng_layout_coordinator.h" |
13 #include "core/layout/ng/ng_length_utils.h" | 13 #include "core/layout/ng/ng_length_utils.h" |
14 #include "core/layout/ng/ng_units.h" | 14 #include "core/layout/ng/ng_units.h" |
15 #include "core/style/ComputedStyle.h" | 15 #include "core/style/ComputedStyle.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 namespace blink { | 18 namespace blink { |
19 namespace { | 19 namespace { |
20 | 20 |
21 NGConstraintSpace* ConstructConstraintSpace(NGWritingMode writing_mode, | 21 NGConstraintSpace* ConstructConstraintSpace(NGWritingMode writing_mode, |
22 TextDirection direction, | 22 TextDirection direction, |
23 NGLogicalSize size) { | 23 NGLogicalSize size, |
| 24 bool shrink_to_fit = false) { |
24 return NGConstraintSpaceBuilder(writing_mode) | 25 return NGConstraintSpaceBuilder(writing_mode) |
25 .SetAvailableSize(size) | 26 .SetAvailableSize(size) |
26 .SetPercentageResolutionSize(size) | 27 .SetPercentageResolutionSize(size) |
27 .SetTextDirection(direction) | 28 .SetTextDirection(direction) |
28 .SetWritingMode(writing_mode) | 29 .SetWritingMode(writing_mode) |
| 30 .SetIsShrinkToFit(shrink_to_fit) |
29 .ToConstraintSpace(); | 31 .ToConstraintSpace(); |
30 } | 32 } |
31 | 33 |
32 class NGBlockLayoutAlgorithmTest : public ::testing::Test { | 34 class NGBlockLayoutAlgorithmTest : public ::testing::Test { |
33 protected: | 35 protected: |
34 void SetUp() override { style_ = ComputedStyle::create(); } | 36 void SetUp() override { style_ = ComputedStyle::create(); } |
35 | 37 |
36 NGPhysicalFragment* RunBlockLayoutAlgorithm(NGConstraintSpace* space, | 38 NGPhysicalFragment* RunBlockLayoutAlgorithm(NGConstraintSpace* space, |
37 NGBlockNode* first_child) { | 39 NGBlockNode* first_child) { |
38 NGBlockNode parent(style_.get()); | 40 NGBlockNode parent(style_.get()); |
39 parent.SetFirstChild(first_child); | 41 parent.SetFirstChild(first_child); |
40 | 42 |
41 NGLayoutCoordinator coordinator(&parent, space); | 43 NGLayoutCoordinator coordinator(&parent, space); |
42 NGPhysicalFragmentBase* fragment; | 44 NGPhysicalFragmentBase* fragment; |
43 coordinator.Tick(&fragment); | 45 coordinator.Tick(&fragment); |
44 EXPECT_EQ(kBlockLayoutAlgorithm, | 46 EXPECT_EQ(kBlockLayoutAlgorithm, |
45 coordinator.GetAlgorithmStackForTesting()[0]->algorithmType()); | 47 coordinator.GetAlgorithmStackForTesting()[0]->algorithmType()); |
46 while (!coordinator.Tick(&fragment)) | 48 while (!coordinator.Tick(&fragment)) |
47 ; | 49 ; |
48 | 50 |
49 return toNGPhysicalFragment(fragment); | 51 return toNGPhysicalFragment(fragment); |
50 } | 52 } |
51 | 53 |
| 54 MinAndMaxContentSizes RunComputeMinAndMax(NGBlockNode* first_child) { |
| 55 // The constraint space is not used for min/max computation, but we need |
| 56 // it to create the algorithm. |
| 57 NGConstraintSpace* space = |
| 58 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::Ltr, |
| 59 NGLogicalSize(LayoutUnit(), LayoutUnit())); |
| 60 NGBlockLayoutAlgorithm algorithm(style_.get(), first_child, space); |
| 61 MinAndMaxContentSizes sizes; |
| 62 NGLayoutAlgorithm::MinAndMaxState state; |
| 63 while ((state = algorithm.ComputeMinAndMaxContentSizes(&sizes)) != |
| 64 NGLayoutAlgorithm::kSuccess) { |
| 65 EXPECT_NE(NGLayoutAlgorithm::kNotImplemented, state); |
| 66 // shouldn't happen but let's avoid an infinite loop |
| 67 if (state == NGLayoutAlgorithm::kNotImplemented) |
| 68 break; |
| 69 } |
| 70 return sizes; |
| 71 } |
| 72 |
52 RefPtr<ComputedStyle> style_; | 73 RefPtr<ComputedStyle> style_; |
53 }; | 74 }; |
54 | 75 |
55 TEST_F(NGBlockLayoutAlgorithmTest, FixedSize) { | 76 TEST_F(NGBlockLayoutAlgorithmTest, FixedSize) { |
56 style_->setWidth(Length(30, Fixed)); | 77 style_->setWidth(Length(30, Fixed)); |
57 style_->setHeight(Length(40, Fixed)); | 78 style_->setHeight(Length(40, Fixed)); |
58 | 79 |
59 auto* space = ConstructConstraintSpace( | 80 auto* space = ConstructConstraintSpace( |
60 kHorizontalTopBottom, TextDirection::Ltr, | 81 kHorizontalTopBottom, TextDirection::Ltr, |
61 NGLogicalSize(LayoutUnit(100), NGSizeIndefinite)); | 82 NGLogicalSize(LayoutUnit(100), NGSizeIndefinite)); |
(...skipping 710 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
772 kHorizontalTopBottom, TextDirection::Ltr, | 793 kHorizontalTopBottom, TextDirection::Ltr, |
773 NGLogicalSize(LayoutUnit(kParentSize), LayoutUnit(kParentSize))); | 794 NGLogicalSize(LayoutUnit(kParentSize), LayoutUnit(kParentSize))); |
774 frag = RunBlockLayoutAlgorithm(space, div1); | 795 frag = RunBlockLayoutAlgorithm(space, div1); |
775 space = ConstructConstraintSpace( | 796 space = ConstructConstraintSpace( |
776 kHorizontalTopBottom, TextDirection::Ltr, | 797 kHorizontalTopBottom, TextDirection::Ltr, |
777 NGLogicalSize(LayoutUnit(kParentSize), LayoutUnit(kParentSize))); | 798 NGLogicalSize(LayoutUnit(kParentSize), LayoutUnit(kParentSize))); |
778 child3 = frag->Children()[2]; | 799 child3 = frag->Children()[2]; |
779 EXPECT_EQ(kDiv1Size + kDiv2Size, child3->TopOffset()); | 800 EXPECT_EQ(kDiv1Size + kDiv2Size, child3->TopOffset()); |
780 } | 801 } |
781 | 802 |
| 803 // Verifies that we compute the right min and max-content size. |
| 804 TEST_F(NGBlockLayoutAlgorithmTest, ComputeMinMaxContent) { |
| 805 const int kWidth = 50; |
| 806 const int kWidthChild1 = 20; |
| 807 const int kWidthChild2 = 30; |
| 808 |
| 809 // This should have no impact on the min/max content size. |
| 810 style_->setWidth(Length(kWidth, Fixed)); |
| 811 |
| 812 RefPtr<ComputedStyle> first_style = ComputedStyle::create(); |
| 813 first_style->setWidth(Length(kWidthChild1, Fixed)); |
| 814 NGBlockNode* first_child = new NGBlockNode(first_style.get()); |
| 815 |
| 816 RefPtr<ComputedStyle> second_style = ComputedStyle::create(); |
| 817 second_style->setWidth(Length(kWidthChild2, Fixed)); |
| 818 NGBlockNode* second_child = new NGBlockNode(second_style.get()); |
| 819 |
| 820 first_child->SetNextSibling(second_child); |
| 821 |
| 822 MinAndMaxContentSizes sizes = RunComputeMinAndMax(first_child); |
| 823 EXPECT_EQ(kWidthChild2, sizes.min_content); |
| 824 EXPECT_EQ(kWidthChild2, sizes.max_content); |
| 825 } |
| 826 |
| 827 // Tests that we correctly handle shrink-to-fit |
| 828 TEST_F(NGBlockLayoutAlgorithmTest, ShrinkToFit) { |
| 829 const int kWidthChild1 = 20; |
| 830 const int kWidthChild2 = 30; |
| 831 |
| 832 RefPtr<ComputedStyle> first_style = ComputedStyle::create(); |
| 833 first_style->setWidth(Length(kWidthChild1, Fixed)); |
| 834 NGBlockNode* first_child = new NGBlockNode(first_style.get()); |
| 835 |
| 836 RefPtr<ComputedStyle> second_style = ComputedStyle::create(); |
| 837 second_style->setWidth(Length(kWidthChild2, Fixed)); |
| 838 NGBlockNode* second_child = new NGBlockNode(second_style.get()); |
| 839 |
| 840 first_child->SetNextSibling(second_child); |
| 841 |
| 842 auto* space = ConstructConstraintSpace( |
| 843 kHorizontalTopBottom, TextDirection::Ltr, |
| 844 NGLogicalSize(LayoutUnit(100), NGSizeIndefinite), true); |
| 845 NGPhysicalFragmentBase* frag = RunBlockLayoutAlgorithm(space, first_child); |
| 846 |
| 847 EXPECT_EQ(LayoutUnit(30), frag->Width()); |
| 848 } |
| 849 |
782 } // namespace | 850 } // namespace |
783 } // namespace blink | 851 } // namespace blink |
OLD | NEW |