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_length_utils.h" | 5 #include "core/layout/ng/ng_length_utils.h" |
6 | 6 |
7 #include "core/layout/ng/ng_box_fragment.h" | 7 #include "core/layout/ng/ng_box_fragment.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_fragment_builder.h" | 10 #include "core/layout/ng/ng_fragment_builder.h" |
11 #include "core/layout/ng/ng_physical_box_fragment.h" | 11 #include "core/layout/ng/ng_physical_box_fragment.h" |
| 12 #include "core/layout/ng/ng_units.h" |
12 #include "core/style/ComputedStyle.h" | 13 #include "core/style/ComputedStyle.h" |
13 #include "platform/CalculationValue.h" | 14 #include "platform/CalculationValue.h" |
14 #include "platform/LayoutUnit.h" | 15 #include "platform/LayoutUnit.h" |
15 #include "platform/Length.h" | 16 #include "platform/Length.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "wtf/RefPtr.h" | 18 #include "wtf/RefPtr.h" |
18 | 19 |
19 namespace blink { | 20 namespace blink { |
20 namespace { | 21 namespace { |
21 | 22 |
(...skipping 11 matching lines...) Expand all Loading... |
33 .SetPercentageResolutionSize( | 34 .SetPercentageResolutionSize( |
34 NGLogicalSize(LayoutUnit(inline_size), LayoutUnit(block_size))) | 35 NGLogicalSize(LayoutUnit(inline_size), LayoutUnit(block_size))) |
35 .SetIsFixedSizeInline(fixed_inline) | 36 .SetIsFixedSizeInline(fixed_inline) |
36 .SetIsFixedSizeBlock(fixed_block) | 37 .SetIsFixedSizeBlock(fixed_block) |
37 .ToConstraintSpace(kHorizontalTopBottom); | 38 .ToConstraintSpace(kHorizontalTopBottom); |
38 } | 39 } |
39 | 40 |
40 LayoutUnit ResolveInlineLength( | 41 LayoutUnit ResolveInlineLength( |
41 const Length& length, | 42 const Length& length, |
42 LengthResolveType type = LengthResolveType::kContentSize, | 43 LengthResolveType type = LengthResolveType::kContentSize, |
43 const WTF::Optional<MinMaxContentSize>& sizes = WTF::nullopt) { | 44 const WTF::Optional<MinAndMaxContentSizes>& sizes = WTF::nullopt) { |
44 NGConstraintSpace* constraintSpace = ConstructConstraintSpace(200, 300); | 45 NGConstraintSpace* constraintSpace = ConstructConstraintSpace(200, 300); |
45 return ::blink::ResolveInlineLength(*constraintSpace, *style_, sizes, | 46 return ::blink::ResolveInlineLength(*constraintSpace, *style_, sizes, |
46 length, type); | 47 length, type); |
47 } | 48 } |
48 | 49 |
49 LayoutUnit ResolveBlockLength( | 50 LayoutUnit ResolveBlockLength( |
50 const Length& length, | 51 const Length& length, |
51 LengthResolveType type = LengthResolveType::kContentSize, | 52 LengthResolveType type = LengthResolveType::kContentSize, |
52 LayoutUnit contentSize = LayoutUnit()) { | 53 LayoutUnit contentSize = LayoutUnit()) { |
53 NGConstraintSpace* constraintSpace = ConstructConstraintSpace(200, 300); | 54 NGConstraintSpace* constraintSpace = ConstructConstraintSpace(200, 300); |
54 return ::blink::ResolveBlockLength(*constraintSpace, *style_, length, | 55 return ::blink::ResolveBlockLength(*constraintSpace, *style_, length, |
55 contentSize, type); | 56 contentSize, type); |
56 } | 57 } |
57 | 58 |
58 LayoutUnit ComputeInlineSizeForFragment( | 59 LayoutUnit ComputeInlineSizeForFragment( |
59 const NGConstraintSpace* constraintSpace = ConstructConstraintSpace(200, | 60 const NGConstraintSpace* constraintSpace = ConstructConstraintSpace(200, |
60 300), | 61 300), |
61 const MinMaxContentSize& sizes = MinMaxContentSize()) { | 62 const MinAndMaxContentSizes& sizes = MinAndMaxContentSizes()) { |
62 return ::blink::ComputeInlineSizeForFragment(*constraintSpace, *style_, | 63 return ::blink::ComputeInlineSizeForFragment(*constraintSpace, *style_, |
63 sizes); | 64 sizes); |
64 } | 65 } |
65 | 66 |
66 LayoutUnit ComputeBlockSizeForFragment( | 67 LayoutUnit ComputeBlockSizeForFragment( |
67 const NGConstraintSpace* constraintSpace = ConstructConstraintSpace(200, | 68 const NGConstraintSpace* constraintSpace = ConstructConstraintSpace(200, |
68 300), | 69 300), |
69 LayoutUnit contentSize = LayoutUnit()) { | 70 LayoutUnit contentSize = LayoutUnit()) { |
70 return ::blink::ComputeBlockSizeForFragment(*constraintSpace, *style_, | 71 return ::blink::ComputeBlockSizeForFragment(*constraintSpace, *style_, |
71 contentSize); | 72 contentSize); |
72 } | 73 } |
73 | 74 |
74 RefPtr<ComputedStyle> style_; | 75 RefPtr<ComputedStyle> style_; |
75 }; | 76 }; |
76 | 77 |
77 TEST_F(NGLengthUtilsTest, testResolveInlineLength) { | 78 TEST_F(NGLengthUtilsTest, testResolveInlineLength) { |
78 EXPECT_EQ(LayoutUnit(60), ResolveInlineLength(Length(30, Percent))); | 79 EXPECT_EQ(LayoutUnit(60), ResolveInlineLength(Length(30, Percent))); |
79 EXPECT_EQ(LayoutUnit(150), ResolveInlineLength(Length(150, Fixed))); | 80 EXPECT_EQ(LayoutUnit(150), ResolveInlineLength(Length(150, Fixed))); |
80 EXPECT_EQ(LayoutUnit(0), | 81 EXPECT_EQ(LayoutUnit(0), |
81 ResolveInlineLength(Length(Auto), LengthResolveType::kMinSize)); | 82 ResolveInlineLength(Length(Auto), LengthResolveType::kMinSize)); |
82 EXPECT_EQ(LayoutUnit(200), ResolveInlineLength(Length(Auto))); | 83 EXPECT_EQ(LayoutUnit(200), ResolveInlineLength(Length(Auto))); |
83 EXPECT_EQ(LayoutUnit(200), ResolveInlineLength(Length(FillAvailable))); | 84 EXPECT_EQ(LayoutUnit(200), ResolveInlineLength(Length(FillAvailable))); |
84 | 85 |
85 EXPECT_EQ(LayoutUnit(200), | 86 EXPECT_EQ(LayoutUnit(200), |
86 ResolveInlineLength(Length(Auto), LengthResolveType::kMaxSize)); | 87 ResolveInlineLength(Length(Auto), LengthResolveType::kMaxSize)); |
87 EXPECT_EQ(LayoutUnit(200), ResolveInlineLength(Length(FillAvailable), | 88 EXPECT_EQ(LayoutUnit(200), ResolveInlineLength(Length(FillAvailable), |
88 LengthResolveType::kMaxSize)); | 89 LengthResolveType::kMaxSize)); |
89 MinMaxContentSize sizes; | 90 MinAndMaxContentSizes sizes; |
90 sizes.min_content = LayoutUnit(30); | 91 sizes.min_content = LayoutUnit(30); |
91 sizes.max_content = LayoutUnit(40); | 92 sizes.max_content = LayoutUnit(40); |
92 EXPECT_EQ(LayoutUnit(30), | 93 EXPECT_EQ(LayoutUnit(30), |
93 ResolveInlineLength(Length(MinContent), | 94 ResolveInlineLength(Length(MinContent), |
94 LengthResolveType::kContentSize, sizes)); | 95 LengthResolveType::kContentSize, sizes)); |
95 EXPECT_EQ(LayoutUnit(40), | 96 EXPECT_EQ(LayoutUnit(40), |
96 ResolveInlineLength(Length(MaxContent), | 97 ResolveInlineLength(Length(MaxContent), |
97 LengthResolveType::kContentSize, sizes)); | 98 LengthResolveType::kContentSize, sizes)); |
98 EXPECT_EQ(LayoutUnit(40), | 99 EXPECT_EQ(LayoutUnit(40), |
99 ResolveInlineLength(Length(FitContent), | 100 ResolveInlineLength(Length(FitContent), |
(...skipping 15 matching lines...) Expand all Loading... |
115 EXPECT_EQ(LayoutUnit(300), ResolveBlockLength(Length(FillAvailable))); | 116 EXPECT_EQ(LayoutUnit(300), ResolveBlockLength(Length(FillAvailable))); |
116 | 117 |
117 EXPECT_EQ(LayoutUnit(0), | 118 EXPECT_EQ(LayoutUnit(0), |
118 ResolveBlockLength(Length(Auto), LengthResolveType::kContentSize)); | 119 ResolveBlockLength(Length(Auto), LengthResolveType::kContentSize)); |
119 EXPECT_EQ(LayoutUnit(300), | 120 EXPECT_EQ(LayoutUnit(300), |
120 ResolveBlockLength(Length(FillAvailable), | 121 ResolveBlockLength(Length(FillAvailable), |
121 LengthResolveType::kContentSize)); | 122 LengthResolveType::kContentSize)); |
122 } | 123 } |
123 | 124 |
124 TEST_F(NGLengthUtilsTest, testComputeContentContribution) { | 125 TEST_F(NGLengthUtilsTest, testComputeContentContribution) { |
125 MinMaxContentSize sizes; | 126 MinAndMaxContentSizes sizes; |
126 sizes.min_content = LayoutUnit(30); | 127 sizes.min_content = LayoutUnit(30); |
127 sizes.max_content = LayoutUnit(40); | 128 sizes.max_content = LayoutUnit(40); |
128 | 129 |
129 MinMaxContentSize expected{LayoutUnit(), LayoutUnit()}; | 130 MinAndMaxContentSizes expected{LayoutUnit(), LayoutUnit()}; |
130 style_->setLogicalWidth(Length(30, Percent)); | 131 style_->setLogicalWidth(Length(30, Percent)); |
131 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); | 132 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); |
132 | 133 |
133 style_->setLogicalWidth(Length(FillAvailable)); | 134 style_->setLogicalWidth(Length(FillAvailable)); |
134 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); | 135 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); |
135 | 136 |
136 expected = MinMaxContentSize{LayoutUnit(150), LayoutUnit(150)}; | 137 expected = MinAndMaxContentSizes{LayoutUnit(150), LayoutUnit(150)}; |
137 style_->setLogicalWidth(Length(150, Fixed)); | 138 style_->setLogicalWidth(Length(150, Fixed)); |
138 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); | 139 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); |
139 | 140 |
140 expected = sizes; | 141 expected = sizes; |
141 style_->setLogicalWidth(Length(Auto)); | 142 style_->setLogicalWidth(Length(Auto)); |
142 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); | 143 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); |
143 | 144 |
144 expected = MinMaxContentSize{LayoutUnit(430), LayoutUnit(440)}; | 145 expected = MinAndMaxContentSizes{LayoutUnit(430), LayoutUnit(440)}; |
145 style_->setPaddingLeft(Length(400, Fixed)); | 146 style_->setPaddingLeft(Length(400, Fixed)); |
146 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); | 147 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); |
147 | 148 |
148 expected = MinMaxContentSize{LayoutUnit(100), LayoutUnit(100)}; | 149 expected = MinAndMaxContentSizes{LayoutUnit(100), LayoutUnit(100)}; |
149 style_->setPaddingLeft(Length(0, Fixed)); | 150 style_->setPaddingLeft(Length(0, Fixed)); |
150 style_->setLogicalWidth(Length(CalculationValue::create( | 151 style_->setLogicalWidth(Length(CalculationValue::create( |
151 PixelsAndPercent(100, -10), ValueRangeNonNegative))); | 152 PixelsAndPercent(100, -10), ValueRangeNonNegative))); |
152 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); | 153 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); |
153 | 154 |
154 expected = MinMaxContentSize{LayoutUnit(30), LayoutUnit(35)}; | 155 expected = MinAndMaxContentSizes{LayoutUnit(30), LayoutUnit(35)}; |
155 style_->setLogicalWidth(Length(Auto)); | 156 style_->setLogicalWidth(Length(Auto)); |
156 style_->setMaxWidth(Length(35, Fixed)); | 157 style_->setMaxWidth(Length(35, Fixed)); |
157 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); | 158 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); |
158 | 159 |
159 expected = MinMaxContentSize{LayoutUnit(80), LayoutUnit(80)}; | 160 expected = MinAndMaxContentSizes{LayoutUnit(80), LayoutUnit(80)}; |
160 style_->setLogicalWidth(Length(50, Fixed)); | 161 style_->setLogicalWidth(Length(50, Fixed)); |
161 style_->setMinWidth(Length(80, Fixed)); | 162 style_->setMinWidth(Length(80, Fixed)); |
162 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); | 163 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); |
163 | 164 |
164 expected = MinMaxContentSize{LayoutUnit(150), LayoutUnit(150)}; | 165 expected = MinAndMaxContentSizes{LayoutUnit(150), LayoutUnit(150)}; |
165 style_ = ComputedStyle::create(); | 166 style_ = ComputedStyle::create(); |
166 style_->setLogicalWidth(Length(100, Fixed)); | 167 style_->setLogicalWidth(Length(100, Fixed)); |
167 style_->setPaddingLeft(Length(50, Fixed)); | 168 style_->setPaddingLeft(Length(50, Fixed)); |
168 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); | 169 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); |
169 | 170 |
170 expected = MinMaxContentSize{LayoutUnit(100), LayoutUnit(100)}; | 171 expected = MinAndMaxContentSizes{LayoutUnit(100), LayoutUnit(100)}; |
171 style_->setBoxSizing(EBoxSizing::kBorderBox); | 172 style_->setBoxSizing(EBoxSizing::kBorderBox); |
172 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); | 173 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); |
173 | 174 |
174 // Content size should never be below zero, even with box-sizing: border-box | 175 // Content size should never be below zero, even with box-sizing: border-box |
175 // and a large padding... | 176 // and a large padding... |
176 expected = MinMaxContentSize{LayoutUnit(400), LayoutUnit(400)}; | 177 expected = MinAndMaxContentSizes{LayoutUnit(400), LayoutUnit(400)}; |
177 style_->setPaddingLeft(Length(400, Fixed)); | 178 style_->setPaddingLeft(Length(400, Fixed)); |
178 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); | 179 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); |
179 | 180 |
180 expected.min_content = expected.max_content = | 181 expected.min_content = expected.max_content = |
181 sizes.min_content + LayoutUnit(400); | 182 sizes.min_content + LayoutUnit(400); |
182 style_->setLogicalWidth(Length(MinContent)); | 183 style_->setLogicalWidth(Length(MinContent)); |
183 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); | 184 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); |
184 style_->setLogicalWidth(Length(100, Fixed)); | 185 style_->setLogicalWidth(Length(100, Fixed)); |
185 style_->setMaxWidth(Length(MaxContent)); | 186 style_->setMaxWidth(Length(MaxContent)); |
186 // Due to padding and box-sizing, width computes to 400px and max-width to | 187 // Due to padding and box-sizing, width computes to 400px and max-width to |
187 // 440px, so the result is 400. | 188 // 440px, so the result is 400. |
188 expected = MinMaxContentSize{LayoutUnit(400), LayoutUnit(400)}; | 189 expected = MinAndMaxContentSizes{LayoutUnit(400), LayoutUnit(400)}; |
189 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); | 190 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); |
190 expected = MinMaxContentSize{LayoutUnit(40), LayoutUnit(40)}; | 191 expected = MinAndMaxContentSizes{LayoutUnit(40), LayoutUnit(40)}; |
191 style_->setPaddingLeft(Length(0, Fixed)); | 192 style_->setPaddingLeft(Length(0, Fixed)); |
192 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); | 193 EXPECT_EQ(expected, ComputeMinAndMaxContentContribution(*style_, sizes)); |
193 } | 194 } |
194 | 195 |
195 TEST_F(NGLengthUtilsTest, testComputeInlineSizeForFragment) { | 196 TEST_F(NGLengthUtilsTest, testComputeInlineSizeForFragment) { |
196 MinMaxContentSize sizes; | 197 MinAndMaxContentSizes sizes; |
197 sizes.min_content = LayoutUnit(30); | 198 sizes.min_content = LayoutUnit(30); |
198 sizes.max_content = LayoutUnit(40); | 199 sizes.max_content = LayoutUnit(40); |
199 | 200 |
200 style_->setLogicalWidth(Length(30, Percent)); | 201 style_->setLogicalWidth(Length(30, Percent)); |
201 EXPECT_EQ(LayoutUnit(60), ComputeInlineSizeForFragment()); | 202 EXPECT_EQ(LayoutUnit(60), ComputeInlineSizeForFragment()); |
202 | 203 |
203 style_->setLogicalWidth(Length(150, Fixed)); | 204 style_->setLogicalWidth(Length(150, Fixed)); |
204 EXPECT_EQ(LayoutUnit(150), ComputeInlineSizeForFragment()); | 205 EXPECT_EQ(LayoutUnit(150), ComputeInlineSizeForFragment()); |
205 | 206 |
206 style_->setLogicalWidth(Length(Auto)); | 207 style_->setLogicalWidth(Length(Auto)); |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 EXPECT_EQ(125, GetUsedColumnWidth(4, 100, 10, 530)); | 490 EXPECT_EQ(125, GetUsedColumnWidth(4, 100, 10, 530)); |
490 EXPECT_EQ(4, GetUsedColumnCount(4, 100, 10, 530)); | 491 EXPECT_EQ(4, GetUsedColumnCount(4, 100, 10, 530)); |
491 EXPECT_EQ(100, GetUsedColumnWidth(6, 100, 10, 540)); | 492 EXPECT_EQ(100, GetUsedColumnWidth(6, 100, 10, 540)); |
492 EXPECT_EQ(5, GetUsedColumnCount(6, 100, 10, 540)); | 493 EXPECT_EQ(5, GetUsedColumnCount(6, 100, 10, 540)); |
493 EXPECT_EQ(100, GetUsedColumnWidth(0, 100, 10, 540)); | 494 EXPECT_EQ(100, GetUsedColumnWidth(0, 100, 10, 540)); |
494 EXPECT_EQ(5, GetUsedColumnCount(0, 100, 10, 540)); | 495 EXPECT_EQ(5, GetUsedColumnCount(0, 100, 10, 540)); |
495 } | 496 } |
496 | 497 |
497 } // namespace | 498 } // namespace |
498 } // namespace blink | 499 } // namespace blink |
OLD | NEW |