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_constraint_space.h" | 7 #include "core/layout/ng/ng_constraint_space.h" |
8 #include "core/style/ComputedStyle.h" | 8 #include "core/style/ComputedStyle.h" |
9 #include "platform/CalculationValue.h" | 9 #include "platform/CalculationValue.h" |
10 #include "platform/LayoutUnit.h" | 10 #include "platform/LayoutUnit.h" |
11 #include "platform/Length.h" | 11 #include "platform/Length.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "wtf/RefPtr.h" | 13 #include "wtf/RefPtr.h" |
14 | 14 |
15 namespace blink { | 15 namespace blink { |
16 namespace { | 16 namespace { |
17 | 17 |
18 class NGLengthUtilsTest : public ::testing::Test { | 18 class NGLengthUtilsTest : public ::testing::Test { |
19 protected: | 19 protected: |
20 void SetUp() override { style_ = ComputedStyle::create(); } | 20 void SetUp() override { style_ = ComputedStyle::create(); } |
21 | 21 |
| 22 static NGConstraintSpace constructConstraintSpace(int inline_size, |
| 23 int block_size) { |
| 24 NGLogicalSize container_size; |
| 25 container_size.inlineSize = LayoutUnit(inline_size); |
| 26 container_size.blockSize = LayoutUnit(block_size); |
| 27 return NGConstraintSpace(container_size); |
| 28 } |
| 29 |
22 LayoutUnit resolveInlineLength( | 30 LayoutUnit resolveInlineLength( |
23 const Length& length, | 31 const Length& length, |
24 LengthResolveType type = LengthResolveType::ContentSize) { | 32 LengthResolveType type = LengthResolveType::ContentSize) { |
25 NGConstraintSpace constraintSpace(LayoutUnit(200), LayoutUnit(300)); | 33 NGConstraintSpace constraintSpace = constructConstraintSpace(200, 300); |
26 return ::blink::resolveInlineLength(constraintSpace, length, type); | 34 return ::blink::resolveInlineLength(constraintSpace, length, type); |
27 } | 35 } |
28 | 36 |
29 LayoutUnit resolveBlockLength( | 37 LayoutUnit resolveBlockLength( |
30 const Length& length, | 38 const Length& length, |
31 LengthResolveType type = LengthResolveType::ContentSize, | 39 LengthResolveType type = LengthResolveType::ContentSize, |
32 LayoutUnit contentSize = LayoutUnit()) { | 40 LayoutUnit contentSize = LayoutUnit()) { |
33 NGConstraintSpace constraintSpace(LayoutUnit(200), LayoutUnit(300)); | 41 NGConstraintSpace constraintSpace = constructConstraintSpace(200, 300); |
34 return ::blink::resolveBlockLength(constraintSpace, length, contentSize, | 42 return ::blink::resolveBlockLength(constraintSpace, length, contentSize, |
35 type); | 43 type); |
36 } | 44 } |
37 | 45 |
38 LayoutUnit computeInlineSizeForFragment( | 46 LayoutUnit computeInlineSizeForFragment( |
39 const NGConstraintSpace& constraintSpace = | 47 const NGConstraintSpace constraintSpace = constructConstraintSpace(200, |
40 NGConstraintSpace(LayoutUnit(200), LayoutUnit(300))) { | 48 300)) { |
41 return ::blink::computeInlineSizeForFragment(constraintSpace, *style_); | 49 return ::blink::computeInlineSizeForFragment(constraintSpace, *style_); |
42 } | 50 } |
43 | 51 |
44 LayoutUnit computeBlockSizeForFragment( | 52 LayoutUnit computeBlockSizeForFragment( |
45 const NGConstraintSpace& constraintSpace = | 53 const NGConstraintSpace constraintSpace = constructConstraintSpace(200, |
46 NGConstraintSpace(LayoutUnit(200), LayoutUnit(300)), | 54 300), |
47 LayoutUnit contentSize = LayoutUnit()) { | 55 LayoutUnit contentSize = LayoutUnit()) { |
48 return ::blink::computeBlockSizeForFragment(constraintSpace, *style_, | 56 return ::blink::computeBlockSizeForFragment(constraintSpace, *style_, |
49 contentSize); | 57 contentSize); |
50 } | 58 } |
51 | 59 |
52 RefPtr<ComputedStyle> style_; | 60 RefPtr<ComputedStyle> style_; |
53 }; | 61 }; |
54 | 62 |
55 TEST_F(NGLengthUtilsTest, testResolveInlineLength) { | 63 TEST_F(NGLengthUtilsTest, testResolveInlineLength) { |
56 EXPECT_EQ(LayoutUnit(60), resolveInlineLength(Length(30, Percent))); | 64 EXPECT_EQ(LayoutUnit(60), resolveInlineLength(Length(30, Percent))); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 style_->setLogicalWidth(Length(Auto)); | 97 style_->setLogicalWidth(Length(Auto)); |
90 EXPECT_EQ(LayoutUnit(200), computeInlineSizeForFragment()); | 98 EXPECT_EQ(LayoutUnit(200), computeInlineSizeForFragment()); |
91 | 99 |
92 style_->setLogicalWidth(Length(FillAvailable)); | 100 style_->setLogicalWidth(Length(FillAvailable)); |
93 EXPECT_EQ(LayoutUnit(200), computeInlineSizeForFragment()); | 101 EXPECT_EQ(LayoutUnit(200), computeInlineSizeForFragment()); |
94 | 102 |
95 style_->setLogicalWidth(Length(CalculationValue::create( | 103 style_->setLogicalWidth(Length(CalculationValue::create( |
96 PixelsAndPercent(100, -10), ValueRangeNonNegative))); | 104 PixelsAndPercent(100, -10), ValueRangeNonNegative))); |
97 EXPECT_EQ(LayoutUnit(80), computeInlineSizeForFragment()); | 105 EXPECT_EQ(LayoutUnit(80), computeInlineSizeForFragment()); |
98 | 106 |
99 NGConstraintSpace constraintSpace(LayoutUnit(120), LayoutUnit(120)); | 107 NGConstraintSpace constraintSpace = constructConstraintSpace(120, 120); |
100 constraintSpace.setFixedSize(true, true); | 108 constraintSpace.setFixedSize(true, true); |
101 style_->setLogicalWidth(Length(150, Fixed)); | 109 style_->setLogicalWidth(Length(150, Fixed)); |
102 EXPECT_EQ(LayoutUnit(120), computeInlineSizeForFragment(constraintSpace)); | 110 EXPECT_EQ(LayoutUnit(120), computeInlineSizeForFragment(constraintSpace)); |
103 | 111 |
104 style_->setLogicalWidth(Length(200, Fixed)); | 112 style_->setLogicalWidth(Length(200, Fixed)); |
105 style_->setMaxWidth(Length(80, Percent)); | 113 style_->setMaxWidth(Length(80, Percent)); |
106 EXPECT_EQ(LayoutUnit(160), computeInlineSizeForFragment()); | 114 EXPECT_EQ(LayoutUnit(160), computeInlineSizeForFragment()); |
107 | 115 |
108 style_->setLogicalWidth(Length(100, Fixed)); | 116 style_->setLogicalWidth(Length(100, Fixed)); |
109 style_->setMinWidth(Length(80, Percent)); | 117 style_->setMinWidth(Length(80, Percent)); |
110 EXPECT_EQ(LayoutUnit(160), computeInlineSizeForFragment()); | 118 EXPECT_EQ(LayoutUnit(160), computeInlineSizeForFragment()); |
111 | 119 |
112 // TODO(layout-ng): test {min,max}-content on max-width. | 120 // TODO(layout-ng): test {min,max}-content on max-width. |
113 } | 121 } |
114 | 122 |
115 TEST_F(NGLengthUtilsTest, testComputeBlockSizeForFragment) { | 123 TEST_F(NGLengthUtilsTest, testComputeBlockSizeForFragment) { |
116 style_->setLogicalHeight(Length(30, Percent)); | 124 style_->setLogicalHeight(Length(30, Percent)); |
117 EXPECT_EQ(LayoutUnit(90), computeBlockSizeForFragment()); | 125 EXPECT_EQ(LayoutUnit(90), computeBlockSizeForFragment()); |
118 | 126 |
119 style_->setLogicalHeight(Length(150, Fixed)); | 127 style_->setLogicalHeight(Length(150, Fixed)); |
120 EXPECT_EQ(LayoutUnit(150), computeBlockSizeForFragment()); | 128 EXPECT_EQ(LayoutUnit(150), computeBlockSizeForFragment()); |
121 | 129 |
122 style_->setLogicalHeight(Length(Auto)); | 130 style_->setLogicalHeight(Length(Auto)); |
123 EXPECT_EQ(LayoutUnit(0), computeBlockSizeForFragment()); | 131 EXPECT_EQ(LayoutUnit(0), computeBlockSizeForFragment()); |
124 | 132 |
125 style_->setLogicalHeight(Length(Auto)); | 133 style_->setLogicalHeight(Length(Auto)); |
126 EXPECT_EQ(LayoutUnit(120), | 134 EXPECT_EQ(LayoutUnit(120), |
127 computeBlockSizeForFragment( | 135 computeBlockSizeForFragment(constructConstraintSpace(200, 300), |
128 NGConstraintSpace(LayoutUnit(200), LayoutUnit(300)), | 136 LayoutUnit(120))); |
129 LayoutUnit(120))); | |
130 | 137 |
131 style_->setLogicalHeight(Length(FillAvailable)); | 138 style_->setLogicalHeight(Length(FillAvailable)); |
132 EXPECT_EQ(LayoutUnit(300), computeBlockSizeForFragment()); | 139 EXPECT_EQ(LayoutUnit(300), computeBlockSizeForFragment()); |
133 | 140 |
134 style_->setLogicalHeight(Length(CalculationValue::create( | 141 style_->setLogicalHeight(Length(CalculationValue::create( |
135 PixelsAndPercent(100, -10), ValueRangeNonNegative))); | 142 PixelsAndPercent(100, -10), ValueRangeNonNegative))); |
136 EXPECT_EQ(LayoutUnit(70), computeBlockSizeForFragment()); | 143 EXPECT_EQ(LayoutUnit(70), computeBlockSizeForFragment()); |
137 | 144 |
138 NGConstraintSpace constraintSpace(LayoutUnit(200), LayoutUnit(200)); | 145 NGConstraintSpace constraintSpace = constructConstraintSpace(200, 200); |
139 constraintSpace.setFixedSize(true, true); | 146 constraintSpace.setFixedSize(true, true); |
140 style_->setLogicalHeight(Length(150, Fixed)); | 147 style_->setLogicalHeight(Length(150, Fixed)); |
141 EXPECT_EQ(LayoutUnit(200), computeBlockSizeForFragment(constraintSpace)); | 148 EXPECT_EQ(LayoutUnit(200), computeBlockSizeForFragment(constraintSpace)); |
142 | 149 |
143 style_->setLogicalHeight(Length(300, Fixed)); | 150 style_->setLogicalHeight(Length(300, Fixed)); |
144 style_->setMaxHeight(Length(80, Percent)); | 151 style_->setMaxHeight(Length(80, Percent)); |
145 EXPECT_EQ(LayoutUnit(240), computeBlockSizeForFragment()); | 152 EXPECT_EQ(LayoutUnit(240), computeBlockSizeForFragment()); |
146 | 153 |
147 style_->setLogicalHeight(Length(100, Fixed)); | 154 style_->setLogicalHeight(Length(100, Fixed)); |
148 style_->setMinHeight(Length(80, Percent)); | 155 style_->setMinHeight(Length(80, Percent)); |
149 EXPECT_EQ(LayoutUnit(240), computeBlockSizeForFragment()); | 156 EXPECT_EQ(LayoutUnit(240), computeBlockSizeForFragment()); |
150 | 157 |
151 // TODO(layout-ng): test {min,max}-content on max-height. | 158 // TODO(layout-ng): test {min,max}-content on max-height. |
152 } | 159 } |
153 | 160 |
154 } // namespace | 161 } // namespace |
155 } // namespace blink | 162 } // namespace blink |
OLD | NEW |