| 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_constraint_space.h" | 5 #include "core/layout/ng/ng_constraint_space.h" |
| 6 | 6 |
| 7 #include "core/layout/ng/ng_constraint_space.h" | 7 #include "core/layout/ng/ng_constraint_space.h" |
| 8 #include "core/layout/ng/ng_constraint_space_builder.h" | 8 #include "core/layout/ng/ng_constraint_space_builder.h" |
| 9 #include "core/layout/ng/ng_layout_opportunity_iterator.h" | 9 #include "core/layout/ng/ng_layout_opportunity_iterator.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace blink { | 12 namespace blink { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 NGConstraintSpace* ConstructConstraintSpace(NGWritingMode writing_mode, | 16 RefPtr<NGConstraintSpace> ConstructConstraintSpace(NGWritingMode writing_mode, |
| 17 TextDirection direction, | 17 TextDirection direction, |
| 18 NGLogicalSize size) { | 18 NGLogicalSize size) { |
| 19 return NGConstraintSpaceBuilder(writing_mode) | 19 return NGConstraintSpaceBuilder(writing_mode) |
| 20 .SetTextDirection(direction) | 20 .SetTextDirection(direction) |
| 21 .SetAvailableSize(size) | 21 .SetAvailableSize(size) |
| 22 .SetPercentageResolutionSize(size) | 22 .SetPercentageResolutionSize(size) |
| 23 .SetIsFixedSizeInline(true) | 23 .SetIsFixedSizeInline(true) |
| 24 .SetIsInlineDirectionTriggersScrollbar(true) | 24 .SetIsInlineDirectionTriggersScrollbar(true) |
| 25 .SetFragmentationType(NGFragmentationType::kFragmentColumn) | 25 .SetFragmentationType(NGFragmentationType::kFragmentColumn) |
| 26 .ToConstraintSpace(writing_mode); | 26 .ToConstraintSpace(writing_mode); |
| 27 } | 27 } |
| 28 | 28 |
| 29 static String OpportunityToString(const NGLayoutOpportunity& opportunity) { | 29 static String OpportunityToString(const NGLayoutOpportunity& opportunity) { |
| 30 return opportunity.IsEmpty() ? String("(empty)") : opportunity.ToString(); | 30 return opportunity.IsEmpty() ? String("(empty)") : opportunity.ToString(); |
| 31 } | 31 } |
| 32 | 32 |
| 33 TEST(NGConstraintSpaceTest, LayoutOpportunitiesNoExclusions) { | 33 TEST(NGConstraintSpaceTest, LayoutOpportunitiesNoExclusions) { |
| 34 NGLogicalSize size; | 34 NGLogicalSize size; |
| 35 size.inline_size = LayoutUnit(600); | 35 size.inline_size = LayoutUnit(600); |
| 36 size.block_size = LayoutUnit(400); | 36 size.block_size = LayoutUnit(400); |
| 37 auto* space = | 37 RefPtr<NGConstraintSpace> space = |
| 38 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); | 38 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); |
| 39 | 39 |
| 40 NGLayoutOpportunityIterator iterator(space); | 40 NGLayoutOpportunityIterator iterator(space.get()); |
| 41 EXPECT_EQ("0,0 600x400", OpportunityToString(iterator.Next())); | 41 EXPECT_EQ("0,0 600x400", OpportunityToString(iterator.Next())); |
| 42 EXPECT_EQ("(empty)", OpportunityToString(iterator.Next())); | 42 EXPECT_EQ("(empty)", OpportunityToString(iterator.Next())); |
| 43 } | 43 } |
| 44 | 44 |
| 45 TEST(NGConstraintSpaceTest, LayoutOpportunitiesTopRightExclusion) { | 45 TEST(NGConstraintSpaceTest, LayoutOpportunitiesTopRightExclusion) { |
| 46 NGLogicalSize size; | 46 NGLogicalSize size; |
| 47 size.inline_size = LayoutUnit(600); | 47 size.inline_size = LayoutUnit(600); |
| 48 size.block_size = LayoutUnit(400); | 48 size.block_size = LayoutUnit(400); |
| 49 // Create a space with a 100x100 exclusion in the top right corner. | 49 // Create a space with a 100x100 exclusion in the top right corner. |
| 50 auto* space = | 50 RefPtr<NGConstraintSpace> space = |
| 51 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); | 51 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); |
| 52 NGExclusion exclusion; | 52 NGExclusion exclusion; |
| 53 exclusion.rect.size = {/* inline_size */ LayoutUnit(100), | 53 exclusion.rect.size = {/* inline_size */ LayoutUnit(100), |
| 54 /* block_size */ LayoutUnit(100)}; | 54 /* block_size */ LayoutUnit(100)}; |
| 55 exclusion.rect.offset = {/* inline_offset */ LayoutUnit(500), | 55 exclusion.rect.offset = {/* inline_offset */ LayoutUnit(500), |
| 56 /* block_offset */ LayoutUnit(0)}; | 56 /* block_offset */ LayoutUnit(0)}; |
| 57 space->AddExclusion(exclusion); | 57 space->AddExclusion(exclusion); |
| 58 | 58 |
| 59 NGLayoutOpportunityIterator iterator(space); | 59 NGLayoutOpportunityIterator iterator(space.get()); |
| 60 // First opportunity should be to the left of the exclusion. | 60 // First opportunity should be to the left of the exclusion. |
| 61 EXPECT_EQ("0,0 500x400", OpportunityToString(iterator.Next())); | 61 EXPECT_EQ("0,0 500x400", OpportunityToString(iterator.Next())); |
| 62 // Second opportunity should be below the exclusion. | 62 // Second opportunity should be below the exclusion. |
| 63 EXPECT_EQ("0,100 600x300", OpportunityToString(iterator.Next())); | 63 EXPECT_EQ("0,100 600x300", OpportunityToString(iterator.Next())); |
| 64 // There should be no third opportunity. | 64 // There should be no third opportunity. |
| 65 EXPECT_EQ("(empty)", OpportunityToString(iterator.Next())); | 65 EXPECT_EQ("(empty)", OpportunityToString(iterator.Next())); |
| 66 } | 66 } |
| 67 | 67 |
| 68 TEST(NGConstraintSpaceTest, LayoutOpportunitiesTopLeftExclusion) { | 68 TEST(NGConstraintSpaceTest, LayoutOpportunitiesTopLeftExclusion) { |
| 69 NGLogicalSize size; | 69 NGLogicalSize size; |
| 70 size.inline_size = LayoutUnit(600); | 70 size.inline_size = LayoutUnit(600); |
| 71 size.block_size = LayoutUnit(400); | 71 size.block_size = LayoutUnit(400); |
| 72 // Create a space with a 100x100 exclusion in the top left corner. | 72 // Create a space with a 100x100 exclusion in the top left corner. |
| 73 auto* space = | 73 RefPtr<NGConstraintSpace> space = |
| 74 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); | 74 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); |
| 75 NGExclusion exclusion; | 75 NGExclusion exclusion; |
| 76 exclusion.rect.size = {/* inline_size */ LayoutUnit(100), | 76 exclusion.rect.size = {/* inline_size */ LayoutUnit(100), |
| 77 /* block_size */ LayoutUnit(100)}; | 77 /* block_size */ LayoutUnit(100)}; |
| 78 exclusion.rect.offset = {/* inline_offset */ LayoutUnit(0), | 78 exclusion.rect.offset = {/* inline_offset */ LayoutUnit(0), |
| 79 /* block_offset */ LayoutUnit(0)}; | 79 /* block_offset */ LayoutUnit(0)}; |
| 80 space->AddExclusion(exclusion); | 80 space->AddExclusion(exclusion); |
| 81 | 81 |
| 82 NGLayoutOpportunityIterator iterator(space); | 82 NGLayoutOpportunityIterator iterator(space.get()); |
| 83 // First opportunity should be to the right of the exclusion. | 83 // First opportunity should be to the right of the exclusion. |
| 84 EXPECT_EQ("100,0 500x400", OpportunityToString(iterator.Next())); | 84 EXPECT_EQ("100,0 500x400", OpportunityToString(iterator.Next())); |
| 85 // Second opportunity should be below the exclusion. | 85 // Second opportunity should be below the exclusion. |
| 86 EXPECT_EQ("0,100 600x300", OpportunityToString(iterator.Next())); | 86 EXPECT_EQ("0,100 600x300", OpportunityToString(iterator.Next())); |
| 87 // There should be no third opportunity. | 87 // There should be no third opportunity. |
| 88 EXPECT_EQ("(empty)", OpportunityToString(iterator.Next())); | 88 EXPECT_EQ("(empty)", OpportunityToString(iterator.Next())); |
| 89 } | 89 } |
| 90 | 90 |
| 91 // Verifies that Layout Opportunity iterator produces 7 layout opportunities | 91 // Verifies that Layout Opportunity iterator produces 7 layout opportunities |
| 92 // from 4 start points created by 2 CSS exclusions positioned in the middle of | 92 // from 4 start points created by 2 CSS exclusions positioned in the middle of |
| (...skipping 15 matching lines...) Expand all Loading... |
| 108 // Expected: | 108 // Expected: |
| 109 // Layout opportunity iterator generates the next opportunities: | 109 // Layout opportunity iterator generates the next opportunities: |
| 110 // - 1st Start Point: 0,0 600x200; 0,0 150x400 | 110 // - 1st Start Point: 0,0 600x200; 0,0 150x400 |
| 111 // - 2nd Start Point: 250,0 350x350; 250,0 250x400 | 111 // - 2nd Start Point: 250,0 350x350; 250,0 250x400 |
| 112 // - 3rd Start Point: 550,0 50x400 | 112 // - 3rd Start Point: 550,0 50x400 |
| 113 // - 4th Start Point: 0,300 600x50; 0,300 500x100 | 113 // - 4th Start Point: 0,300 600x50; 0,300 500x100 |
| 114 TEST(NGConstraintSpaceTest, LayoutOpportunitiesTwoInMiddle) { | 114 TEST(NGConstraintSpaceTest, LayoutOpportunitiesTwoInMiddle) { |
| 115 NGLogicalSize size; | 115 NGLogicalSize size; |
| 116 size.inline_size = LayoutUnit(600); | 116 size.inline_size = LayoutUnit(600); |
| 117 size.block_size = LayoutUnit(400); | 117 size.block_size = LayoutUnit(400); |
| 118 auto* space = | 118 RefPtr<NGConstraintSpace> space = |
| 119 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); | 119 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); |
| 120 // Add exclusions | 120 // Add exclusions |
| 121 NGExclusion exclusion1; | 121 NGExclusion exclusion1; |
| 122 exclusion1.rect.size = {/* inline_size */ LayoutUnit(100), | 122 exclusion1.rect.size = {/* inline_size */ LayoutUnit(100), |
| 123 /* block_size */ LayoutUnit(100)}; | 123 /* block_size */ LayoutUnit(100)}; |
| 124 exclusion1.rect.offset = {/* inline_offset */ LayoutUnit(150), | 124 exclusion1.rect.offset = {/* inline_offset */ LayoutUnit(150), |
| 125 /* block_offset */ LayoutUnit(200)}; | 125 /* block_offset */ LayoutUnit(200)}; |
| 126 space->AddExclusion(exclusion1); | 126 space->AddExclusion(exclusion1); |
| 127 NGExclusion exclusion2; | 127 NGExclusion exclusion2; |
| 128 exclusion2.rect.size = {/* inline_size */ LayoutUnit(50), | 128 exclusion2.rect.size = {/* inline_size */ LayoutUnit(50), |
| 129 /* block_size */ LayoutUnit(50)}; | 129 /* block_size */ LayoutUnit(50)}; |
| 130 exclusion2.rect.offset = {/* inline_offset */ LayoutUnit(500), | 130 exclusion2.rect.offset = {/* inline_offset */ LayoutUnit(500), |
| 131 /* block_offset */ LayoutUnit(350)}; | 131 /* block_offset */ LayoutUnit(350)}; |
| 132 space->AddExclusion(exclusion2); | 132 space->AddExclusion(exclusion2); |
| 133 | 133 |
| 134 NGLayoutOpportunityIterator iterator(space); | 134 NGLayoutOpportunityIterator iterator(space.get()); |
| 135 // 1st Start point | 135 // 1st Start point |
| 136 EXPECT_EQ("0,0 600x200", OpportunityToString(iterator.Next())); | 136 EXPECT_EQ("0,0 600x200", OpportunityToString(iterator.Next())); |
| 137 EXPECT_EQ("0,0 150x400", OpportunityToString(iterator.Next())); | 137 EXPECT_EQ("0,0 150x400", OpportunityToString(iterator.Next())); |
| 138 // 2nd Start point | 138 // 2nd Start point |
| 139 EXPECT_EQ("250,0 350x350", OpportunityToString(iterator.Next())); | 139 EXPECT_EQ("250,0 350x350", OpportunityToString(iterator.Next())); |
| 140 EXPECT_EQ("250,0 250x400", OpportunityToString(iterator.Next())); | 140 EXPECT_EQ("250,0 250x400", OpportunityToString(iterator.Next())); |
| 141 // 3rd Start point | 141 // 3rd Start point |
| 142 EXPECT_EQ("550,0 50x400", OpportunityToString(iterator.Next())); | 142 EXPECT_EQ("550,0 50x400", OpportunityToString(iterator.Next())); |
| 143 // 4th Start point | 143 // 4th Start point |
| 144 EXPECT_EQ("0,300 600x50", OpportunityToString(iterator.Next())); | 144 EXPECT_EQ("0,300 600x50", OpportunityToString(iterator.Next())); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 158 // - 1st Start Point (0, 200): 350x150, 250x400 | 158 // - 1st Start Point (0, 200): 350x150, 250x400 |
| 159 // - 3rd Start Point (550, 200): 50x400 | 159 // - 3rd Start Point (550, 200): 50x400 |
| 160 // - 4th Start Point (0, 300): 600x50, 500x300 | 160 // - 4th Start Point (0, 300): 600x50, 500x300 |
| 161 // - 5th Start Point (0, 400): 600x200 | 161 // - 5th Start Point (0, 400): 600x200 |
| 162 // All other opportunities that are located before the origin point should be | 162 // All other opportunities that are located before the origin point should be |
| 163 // filtered out. | 163 // filtered out. |
| 164 TEST(NGConstraintSpaceTest, LayoutOpportunitiesTwoInMiddleWithOriginAndLeader) { | 164 TEST(NGConstraintSpaceTest, LayoutOpportunitiesTwoInMiddleWithOriginAndLeader) { |
| 165 NGLogicalSize size; | 165 NGLogicalSize size; |
| 166 size.inline_size = LayoutUnit(600); | 166 size.inline_size = LayoutUnit(600); |
| 167 size.block_size = LayoutUnit(400); | 167 size.block_size = LayoutUnit(400); |
| 168 auto* space = | 168 RefPtr<NGConstraintSpace> space = |
| 169 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); | 169 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); |
| 170 // Add exclusions | 170 // Add exclusions |
| 171 NGExclusion exclusion1; | 171 NGExclusion exclusion1; |
| 172 exclusion1.rect.size = {/* inline_size */ LayoutUnit(100), | 172 exclusion1.rect.size = {/* inline_size */ LayoutUnit(100), |
| 173 /* block_size */ LayoutUnit(100)}; | 173 /* block_size */ LayoutUnit(100)}; |
| 174 exclusion1.rect.offset = {/* inline_offset */ LayoutUnit(150), | 174 exclusion1.rect.offset = {/* inline_offset */ LayoutUnit(150), |
| 175 /* block_offset */ LayoutUnit(200)}; | 175 /* block_offset */ LayoutUnit(200)}; |
| 176 space->AddExclusion(exclusion1); | 176 space->AddExclusion(exclusion1); |
| 177 NGExclusion exclusion2; | 177 NGExclusion exclusion2; |
| 178 exclusion2.rect.size = {/* inline_size */ LayoutUnit(50), | 178 exclusion2.rect.size = {/* inline_size */ LayoutUnit(50), |
| 179 /* block_size */ LayoutUnit(50)}; | 179 /* block_size */ LayoutUnit(50)}; |
| 180 exclusion2.rect.offset = {/* inline_offset */ LayoutUnit(500), | 180 exclusion2.rect.offset = {/* inline_offset */ LayoutUnit(500), |
| 181 /* block_offset */ LayoutUnit(350)}; | 181 /* block_offset */ LayoutUnit(350)}; |
| 182 space->AddExclusion(exclusion2); | 182 space->AddExclusion(exclusion2); |
| 183 | 183 |
| 184 const NGLogicalOffset origin_point = {LayoutUnit(0), LayoutUnit(200)}; | 184 const NGLogicalOffset origin_point = {LayoutUnit(0), LayoutUnit(200)}; |
| 185 const NGLogicalOffset leader_point = {LayoutUnit(250), LayoutUnit(300)}; | 185 const NGLogicalOffset leader_point = {LayoutUnit(250), LayoutUnit(300)}; |
| 186 NGLayoutOpportunityIterator iterator(space, origin_point, leader_point); | 186 NGLayoutOpportunityIterator iterator(space.get(), origin_point, leader_point); |
| 187 // 1st Start Point | 187 // 1st Start Point |
| 188 EXPECT_EQ("250,200 350x150", OpportunityToString(iterator.Next())); | 188 EXPECT_EQ("250,200 350x150", OpportunityToString(iterator.Next())); |
| 189 EXPECT_EQ("250,200 250x400", OpportunityToString(iterator.Next())); | 189 EXPECT_EQ("250,200 250x400", OpportunityToString(iterator.Next())); |
| 190 // 2nd Start Point | 190 // 2nd Start Point |
| 191 EXPECT_EQ("550,200 50x400", OpportunityToString(iterator.Next())); | 191 EXPECT_EQ("550,200 50x400", OpportunityToString(iterator.Next())); |
| 192 // 3rd Start Point | 192 // 3rd Start Point |
| 193 EXPECT_EQ("0,300 600x50", OpportunityToString(iterator.Next())); | 193 EXPECT_EQ("0,300 600x50", OpportunityToString(iterator.Next())); |
| 194 EXPECT_EQ("0,300 500x300", OpportunityToString(iterator.Next())); | 194 EXPECT_EQ("0,300 500x300", OpportunityToString(iterator.Next())); |
| 195 // 4th Start Point | 195 // 4th Start Point |
| 196 EXPECT_EQ("0,400 600x200", OpportunityToString(iterator.Next())); | 196 EXPECT_EQ("0,400 600x200", OpportunityToString(iterator.Next())); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 211 // +-----------------------------+ | 211 // +-----------------------------+ |
| 212 // *** <- Exclusion | 212 // *** <- Exclusion |
| 213 // | 213 // |
| 214 // Expected: | 214 // Expected: |
| 215 // Layout opportunity iterator generates only one opportunity that equals to | 215 // Layout opportunity iterator generates only one opportunity that equals to |
| 216 // available constraint space, i.e. 0,0 600x200 | 216 // available constraint space, i.e. 0,0 600x200 |
| 217 TEST(NGConstraintSpaceTest, LayoutOpportunitiesWithOutOfBoundsExclusions) { | 217 TEST(NGConstraintSpaceTest, LayoutOpportunitiesWithOutOfBoundsExclusions) { |
| 218 NGLogicalSize size; | 218 NGLogicalSize size; |
| 219 size.inline_size = LayoutUnit(600); | 219 size.inline_size = LayoutUnit(600); |
| 220 size.block_size = LayoutUnit(100); | 220 size.block_size = LayoutUnit(100); |
| 221 auto* space = | 221 RefPtr<NGConstraintSpace> space = |
| 222 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); | 222 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); |
| 223 NGExclusion exclusion; | 223 NGExclusion exclusion; |
| 224 exclusion.rect.size = {/* inline_size */ LayoutUnit(100), | 224 exclusion.rect.size = {/* inline_size */ LayoutUnit(100), |
| 225 /* block_size */ LayoutUnit(100)}; | 225 /* block_size */ LayoutUnit(100)}; |
| 226 exclusion.rect.offset = {/* inline_offset */ LayoutUnit(0), | 226 exclusion.rect.offset = {/* inline_offset */ LayoutUnit(0), |
| 227 /* block_offset */ LayoutUnit(150)}; | 227 /* block_offset */ LayoutUnit(150)}; |
| 228 space->AddExclusion(exclusion); | 228 space->AddExclusion(exclusion); |
| 229 | 229 |
| 230 NGLayoutOpportunityIterator iterator(space); | 230 NGLayoutOpportunityIterator iterator(space.get()); |
| 231 EXPECT_EQ("0,0 600x100", OpportunityToString(iterator.Next())); | 231 EXPECT_EQ("0,0 600x100", OpportunityToString(iterator.Next())); |
| 232 EXPECT_EQ("(empty)", OpportunityToString(iterator.Next())); | 232 EXPECT_EQ("(empty)", OpportunityToString(iterator.Next())); |
| 233 } | 233 } |
| 234 | 234 |
| 235 } // namespace | 235 } // namespace |
| 236 } // namespace blink | 236 } // namespace blink |
| OLD | NEW |