| 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" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 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 auto* space = |
| 38 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); | 38 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); |
| 39 auto* iterator = space->LayoutOpportunities(); | 39 |
| 40 EXPECT_EQ("0,0 600x400", OpportunityToString(iterator->Next())); | 40 NGLayoutOpportunityIterator iterator(space); |
| 41 EXPECT_EQ("(empty)", OpportunityToString(iterator->Next())); | 41 EXPECT_EQ("0,0 600x400", OpportunityToString(iterator.Next())); |
| 42 EXPECT_EQ("(empty)", OpportunityToString(iterator.Next())); |
| 42 } | 43 } |
| 43 | 44 |
| 44 TEST(NGConstraintSpaceTest, LayoutOpportunitiesTopRightExclusion) { | 45 TEST(NGConstraintSpaceTest, LayoutOpportunitiesTopRightExclusion) { |
| 45 NGLogicalSize size; | 46 NGLogicalSize size; |
| 46 size.inline_size = LayoutUnit(600); | 47 size.inline_size = LayoutUnit(600); |
| 47 size.block_size = LayoutUnit(400); | 48 size.block_size = LayoutUnit(400); |
| 48 // 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. |
| 49 auto* space = | 50 auto* space = |
| 50 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); | 51 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); |
| 51 NGExclusion exclusion; | 52 NGExclusion exclusion; |
| 52 exclusion.rect.size = {/* inline_size */ LayoutUnit(100), | 53 exclusion.rect.size = {/* inline_size */ LayoutUnit(100), |
| 53 /* block_size */ LayoutUnit(100)}; | 54 /* block_size */ LayoutUnit(100)}; |
| 54 exclusion.rect.offset = {/* inline_offset */ LayoutUnit(500), | 55 exclusion.rect.offset = {/* inline_offset */ LayoutUnit(500), |
| 55 /* block_offset */ LayoutUnit(0)}; | 56 /* block_offset */ LayoutUnit(0)}; |
| 56 space->AddExclusion(exclusion); | 57 space->AddExclusion(exclusion); |
| 57 auto* iterator = space->LayoutOpportunities(); | 58 |
| 59 NGLayoutOpportunityIterator iterator(space); |
| 58 // First opportunity should be to the left of the exclusion. | 60 // First opportunity should be to the left of the exclusion. |
| 59 EXPECT_EQ("0,0 500x400", OpportunityToString(iterator->Next())); | 61 EXPECT_EQ("0,0 500x400", OpportunityToString(iterator.Next())); |
| 60 // Second opportunity should be below the exclusion. | 62 // Second opportunity should be below the exclusion. |
| 61 EXPECT_EQ("0,100 600x300", OpportunityToString(iterator->Next())); | 63 EXPECT_EQ("0,100 600x300", OpportunityToString(iterator.Next())); |
| 62 // There should be no third opportunity. | 64 // There should be no third opportunity. |
| 63 EXPECT_EQ("(empty)", OpportunityToString(iterator->Next())); | 65 EXPECT_EQ("(empty)", OpportunityToString(iterator.Next())); |
| 64 } | 66 } |
| 65 | 67 |
| 66 TEST(NGConstraintSpaceTest, LayoutOpportunitiesTopLeftExclusion) { | 68 TEST(NGConstraintSpaceTest, LayoutOpportunitiesTopLeftExclusion) { |
| 67 NGLogicalSize size; | 69 NGLogicalSize size; |
| 68 size.inline_size = LayoutUnit(600); | 70 size.inline_size = LayoutUnit(600); |
| 69 size.block_size = LayoutUnit(400); | 71 size.block_size = LayoutUnit(400); |
| 70 // 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. |
| 71 auto* space = | 73 auto* space = |
| 72 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); | 74 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); |
| 73 NGExclusion exclusion; | 75 NGExclusion exclusion; |
| 74 exclusion.rect.size = {/* inline_size */ LayoutUnit(100), | 76 exclusion.rect.size = {/* inline_size */ LayoutUnit(100), |
| 75 /* block_size */ LayoutUnit(100)}; | 77 /* block_size */ LayoutUnit(100)}; |
| 76 exclusion.rect.offset = {/* inline_offset */ LayoutUnit(0), | 78 exclusion.rect.offset = {/* inline_offset */ LayoutUnit(0), |
| 77 /* block_offset */ LayoutUnit(0)}; | 79 /* block_offset */ LayoutUnit(0)}; |
| 78 space->AddExclusion(exclusion); | 80 space->AddExclusion(exclusion); |
| 79 auto* iterator = space->LayoutOpportunities(); | 81 |
| 82 NGLayoutOpportunityIterator iterator(space); |
| 80 // First opportunity should be to the right of the exclusion. | 83 // First opportunity should be to the right of the exclusion. |
| 81 EXPECT_EQ("100,0 500x400", OpportunityToString(iterator->Next())); | 84 EXPECT_EQ("100,0 500x400", OpportunityToString(iterator.Next())); |
| 82 // Second opportunity should be below the exclusion. | 85 // Second opportunity should be below the exclusion. |
| 83 EXPECT_EQ("0,100 600x300", OpportunityToString(iterator->Next())); | 86 EXPECT_EQ("0,100 600x300", OpportunityToString(iterator.Next())); |
| 84 // There should be no third opportunity. | 87 // There should be no third opportunity. |
| 85 EXPECT_EQ("(empty)", OpportunityToString(iterator->Next())); | 88 EXPECT_EQ("(empty)", OpportunityToString(iterator.Next())); |
| 86 } | 89 } |
| 87 | 90 |
| 88 // Verifies that Layout Opportunity iterator produces 7 layout opportunities | 91 // Verifies that Layout Opportunity iterator produces 7 layout opportunities |
| 89 // 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 |
| 90 // the main constraint space. | 93 // the main constraint space. |
| 91 // | 94 // |
| 92 // Test case visual representation: | 95 // Test case visual representation: |
| 93 // | 96 // |
| 94 // 100 200 300 400 500 | 97 // 100 200 300 400 500 |
| 95 // (1)--|----|-(2)-|----|----|-(3)-+ | 98 // (1)--|----|-(2)-|----|----|-(3)-+ |
| (...skipping 24 matching lines...) Expand all Loading... |
| 120 /* block_size */ LayoutUnit(100)}; | 123 /* block_size */ LayoutUnit(100)}; |
| 121 exclusion1.rect.offset = {/* inline_offset */ LayoutUnit(150), | 124 exclusion1.rect.offset = {/* inline_offset */ LayoutUnit(150), |
| 122 /* block_offset */ LayoutUnit(200)}; | 125 /* block_offset */ LayoutUnit(200)}; |
| 123 space->AddExclusion(exclusion1); | 126 space->AddExclusion(exclusion1); |
| 124 NGExclusion exclusion2; | 127 NGExclusion exclusion2; |
| 125 exclusion2.rect.size = {/* inline_size */ LayoutUnit(50), | 128 exclusion2.rect.size = {/* inline_size */ LayoutUnit(50), |
| 126 /* block_size */ LayoutUnit(50)}; | 129 /* block_size */ LayoutUnit(50)}; |
| 127 exclusion2.rect.offset = {/* inline_offset */ LayoutUnit(500), | 130 exclusion2.rect.offset = {/* inline_offset */ LayoutUnit(500), |
| 128 /* block_offset */ LayoutUnit(350)}; | 131 /* block_offset */ LayoutUnit(350)}; |
| 129 space->AddExclusion(exclusion2); | 132 space->AddExclusion(exclusion2); |
| 130 auto* iterator = space->LayoutOpportunities(); | 133 |
| 134 NGLayoutOpportunityIterator iterator(space); |
| 131 // 1st Start point | 135 // 1st Start point |
| 132 EXPECT_EQ("0,0 600x200", OpportunityToString(iterator->Next())); | 136 EXPECT_EQ("0,0 600x200", OpportunityToString(iterator.Next())); |
| 133 EXPECT_EQ("0,0 150x400", OpportunityToString(iterator->Next())); | 137 EXPECT_EQ("0,0 150x400", OpportunityToString(iterator.Next())); |
| 134 // 2nd Start point | 138 // 2nd Start point |
| 135 EXPECT_EQ("250,0 350x350", OpportunityToString(iterator->Next())); | 139 EXPECT_EQ("250,0 350x350", OpportunityToString(iterator.Next())); |
| 136 EXPECT_EQ("250,0 250x400", OpportunityToString(iterator->Next())); | 140 EXPECT_EQ("250,0 250x400", OpportunityToString(iterator.Next())); |
| 137 // 3rd Start point | 141 // 3rd Start point |
| 138 EXPECT_EQ("550,0 50x400", OpportunityToString(iterator->Next())); | 142 EXPECT_EQ("550,0 50x400", OpportunityToString(iterator.Next())); |
| 139 // 4th Start point | 143 // 4th Start point |
| 140 EXPECT_EQ("0,300 600x50", OpportunityToString(iterator->Next())); | 144 EXPECT_EQ("0,300 600x50", OpportunityToString(iterator.Next())); |
| 141 EXPECT_EQ("0,300 500x100", OpportunityToString(iterator->Next())); | 145 EXPECT_EQ("0,300 500x100", OpportunityToString(iterator.Next())); |
| 142 // Iterator is exhausted. | 146 // Iterator is exhausted. |
| 143 EXPECT_EQ("(empty)", OpportunityToString(iterator->Next())); | 147 EXPECT_EQ("(empty)", OpportunityToString(iterator.Next())); |
| 144 } | 148 } |
| 145 | 149 |
| 146 // This test is the same as LayoutOpportunitiesTwoInMiddle with the only | 150 // This test is the same as LayoutOpportunitiesTwoInMiddle with the only |
| 147 // difference that NGLayoutOpportunityIterator takes 2 additional arguments: | 151 // difference that NGLayoutOpportunityIterator takes 2 additional arguments: |
| 148 // - origin_point that changes the iterator to return Layout Opportunities that | 152 // - origin_point that changes the iterator to return Layout Opportunities that |
| 149 // lay after the origin point. | 153 // lay after the origin point. |
| 150 // - leader_point that together with origin_point creates a temporary exclusion | 154 // - leader_point that together with origin_point creates a temporary exclusion |
| 151 // | 155 // |
| 152 // Expected: | 156 // Expected: |
| 153 // Layout opportunity iterator generates the next opportunities: | 157 // Layout opportunity iterator generates the next opportunities: |
| (...skipping 15 matching lines...) Expand all Loading... |
| 169 /* block_size */ LayoutUnit(100)}; | 173 /* block_size */ LayoutUnit(100)}; |
| 170 exclusion1.rect.offset = {/* inline_offset */ LayoutUnit(150), | 174 exclusion1.rect.offset = {/* inline_offset */ LayoutUnit(150), |
| 171 /* block_offset */ LayoutUnit(200)}; | 175 /* block_offset */ LayoutUnit(200)}; |
| 172 space->AddExclusion(exclusion1); | 176 space->AddExclusion(exclusion1); |
| 173 NGExclusion exclusion2; | 177 NGExclusion exclusion2; |
| 174 exclusion2.rect.size = {/* inline_size */ LayoutUnit(50), | 178 exclusion2.rect.size = {/* inline_size */ LayoutUnit(50), |
| 175 /* block_size */ LayoutUnit(50)}; | 179 /* block_size */ LayoutUnit(50)}; |
| 176 exclusion2.rect.offset = {/* inline_offset */ LayoutUnit(500), | 180 exclusion2.rect.offset = {/* inline_offset */ LayoutUnit(500), |
| 177 /* block_offset */ LayoutUnit(350)}; | 181 /* block_offset */ LayoutUnit(350)}; |
| 178 space->AddExclusion(exclusion2); | 182 space->AddExclusion(exclusion2); |
| 183 |
| 179 const NGLogicalOffset origin_point = {LayoutUnit(0), LayoutUnit(200)}; | 184 const NGLogicalOffset origin_point = {LayoutUnit(0), LayoutUnit(200)}; |
| 180 const NGLogicalOffset leader_point = {LayoutUnit(250), LayoutUnit(300)}; | 185 const NGLogicalOffset leader_point = {LayoutUnit(250), LayoutUnit(300)}; |
| 181 auto* iterator = | 186 NGLayoutOpportunityIterator iterator(space, origin_point, leader_point); |
| 182 new NGLayoutOpportunityIterator(space, origin_point, leader_point); | |
| 183 // 1st Start Point | 187 // 1st Start Point |
| 184 EXPECT_EQ("250,200 350x150", OpportunityToString(iterator->Next())); | 188 EXPECT_EQ("250,200 350x150", OpportunityToString(iterator.Next())); |
| 185 EXPECT_EQ("250,200 250x400", OpportunityToString(iterator->Next())); | 189 EXPECT_EQ("250,200 250x400", OpportunityToString(iterator.Next())); |
| 186 // 2nd Start Point | 190 // 2nd Start Point |
| 187 EXPECT_EQ("550,200 50x400", OpportunityToString(iterator->Next())); | 191 EXPECT_EQ("550,200 50x400", OpportunityToString(iterator.Next())); |
| 188 // 3rd Start Point | 192 // 3rd Start Point |
| 189 EXPECT_EQ("0,300 600x50", OpportunityToString(iterator->Next())); | 193 EXPECT_EQ("0,300 600x50", OpportunityToString(iterator.Next())); |
| 190 EXPECT_EQ("0,300 500x300", OpportunityToString(iterator->Next())); | 194 EXPECT_EQ("0,300 500x300", OpportunityToString(iterator.Next())); |
| 191 // 4th Start Point | 195 // 4th Start Point |
| 192 EXPECT_EQ("0,400 600x200", OpportunityToString(iterator->Next())); | 196 EXPECT_EQ("0,400 600x200", OpportunityToString(iterator.Next())); |
| 193 // TODO(glebl): The opportunity below should not be generated. | 197 // TODO(glebl): The opportunity below should not be generated. |
| 194 EXPECT_EQ("250,400 350x200", OpportunityToString(iterator->Next())); | 198 EXPECT_EQ("250,400 350x200", OpportunityToString(iterator.Next())); |
| 195 // Iterator is exhausted. | 199 // Iterator is exhausted. |
| 196 EXPECT_EQ("(empty)", OpportunityToString(iterator->Next())); | 200 EXPECT_EQ("(empty)", OpportunityToString(iterator.Next())); |
| 197 } | 201 } |
| 198 // Verifies that Layout Opportunity iterator ignores the exclusion that is not | 202 // Verifies that Layout Opportunity iterator ignores the exclusion that is not |
| 199 // within constraint space. | 203 // within constraint space. |
| 200 // | 204 // |
| 201 // Test case visual representation: | 205 // Test case visual representation: |
| 202 // | 206 // |
| 203 // 100 200 300 400 500 | 207 // 100 200 300 400 500 |
| 204 // +----|----|----|----|----|----+ | 208 // +----|----|----|----|----|----+ |
| 205 // 50 | | | 209 // 50 | | |
| 206 // 100 | | | 210 // 100 | | |
| 207 // +-----------------------------+ | 211 // +-----------------------------+ |
| 208 // *** <- Exclusion | 212 // *** <- Exclusion |
| 209 // | 213 // |
| 210 // Expected: | 214 // Expected: |
| 211 // Layout opportunity iterator generates only one opportunity that equals to | 215 // Layout opportunity iterator generates only one opportunity that equals to |
| 212 // available constraint space, i.e. 0,0 600x200 | 216 // available constraint space, i.e. 0,0 600x200 |
| 213 TEST(NGConstraintSpaceTest, LayoutOpportunitiesWithOutOfBoundsExclusions) { | 217 TEST(NGConstraintSpaceTest, LayoutOpportunitiesWithOutOfBoundsExclusions) { |
| 214 NGLogicalSize size; | 218 NGLogicalSize size; |
| 215 size.inline_size = LayoutUnit(600); | 219 size.inline_size = LayoutUnit(600); |
| 216 size.block_size = LayoutUnit(100); | 220 size.block_size = LayoutUnit(100); |
| 217 auto* space = | 221 auto* space = |
| 218 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); | 222 ConstructConstraintSpace(kHorizontalTopBottom, TextDirection::kLtr, size); |
| 219 NGExclusion exclusion; | 223 NGExclusion exclusion; |
| 220 exclusion.rect.size = {/* inline_size */ LayoutUnit(100), | 224 exclusion.rect.size = {/* inline_size */ LayoutUnit(100), |
| 221 /* block_size */ LayoutUnit(100)}; | 225 /* block_size */ LayoutUnit(100)}; |
| 222 exclusion.rect.offset = {/* inline_offset */ LayoutUnit(0), | 226 exclusion.rect.offset = {/* inline_offset */ LayoutUnit(0), |
| 223 /* block_offset */ LayoutUnit(150)}; | 227 /* block_offset */ LayoutUnit(150)}; |
| 224 space->AddExclusion(exclusion); | 228 space->AddExclusion(exclusion); |
| 225 auto* iterator = space->LayoutOpportunities(); | 229 |
| 226 EXPECT_EQ("0,0 600x100", OpportunityToString(iterator->Next())); | 230 NGLayoutOpportunityIterator iterator(space); |
| 227 EXPECT_EQ("(empty)", OpportunityToString(iterator->Next())); | 231 EXPECT_EQ("0,0 600x100", OpportunityToString(iterator.Next())); |
| 232 EXPECT_EQ("(empty)", OpportunityToString(iterator.Next())); |
| 228 } | 233 } |
| 229 | 234 |
| 230 } // namespace | 235 } // namespace |
| 231 } // namespace blink | 236 } // namespace blink |
| OLD | NEW |