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_box.h" | 7 #include "core/layout/ng/ng_box.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_physical_fragment.h" | 9 #include "core/layout/ng/ng_physical_fragment.h" |
10 #include "core/layout/ng/ng_length_utils.h" | 10 #include "core/layout/ng/ng_length_utils.h" |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
156 div1->SetFirstChild(div2); | 156 div1->SetFirstChild(div2); |
157 | 157 |
158 auto* space = | 158 auto* space = |
159 new NGConstraintSpace(HorizontalTopBottom, LeftToRight, | 159 new NGConstraintSpace(HorizontalTopBottom, LeftToRight, |
160 NGLogicalSize(LayoutUnit(100), NGSizeIndefinite)); | 160 NGLogicalSize(LayoutUnit(100), NGSizeIndefinite)); |
161 space->SetIsNewFormattingContext(true); | 161 space->SetIsNewFormattingContext(true); |
162 NGPhysicalFragment* frag = RunBlockLayoutAlgorithm(space, div1); | 162 NGPhysicalFragment* frag = RunBlockLayoutAlgorithm(space, div1); |
163 | 163 |
164 EXPECT_TRUE(frag->MarginStrut().IsEmpty()); | 164 EXPECT_TRUE(frag->MarginStrut().IsEmpty()); |
165 ASSERT_EQ(frag->Children().size(), 1UL); | 165 ASSERT_EQ(frag->Children().size(), 1UL); |
166 const NGPhysicalFragmentBase* div2_fragment = frag->Children()[0]; | 166 const NGPhysicalFragment* div2_fragment = |
167 static_cast<const NGPhysicalFragment*>(frag->Children()[0].get()); | |
167 EXPECT_EQ(NGMarginStrut({LayoutUnit(kDiv2MarginTop)}), | 168 EXPECT_EQ(NGMarginStrut({LayoutUnit(kDiv2MarginTop)}), |
168 div2_fragment->MarginStrut()); | 169 div2_fragment->MarginStrut()); |
169 EXPECT_EQ(kDiv1MarginTop, div2_fragment->TopOffset()); | 170 EXPECT_EQ(kDiv1MarginTop, div2_fragment->TopOffset()); |
170 } | 171 } |
171 | 172 |
172 // Verifies the collapsing margins case for the next pair: | 173 // Verifies the collapsing margins case for the next pair: |
173 // - bottom margin of box and top margin of its next in-flow following sibling. | 174 // - bottom margin of box and top margin of its next in-flow following sibling. |
174 // | 175 // |
175 // Test case's HTML representation: | 176 // Test case's HTML representation: |
176 // <div style="margin-bottom: 20px; height: 50px;"> <!-- DIV1 --> | 177 // <div style="margin-bottom: 20px; height: 50px;"> <!-- DIV1 --> |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
332 NGLogicalSize(LayoutUnit(100), NGSizeIndefinite)); | 333 NGLogicalSize(LayoutUnit(100), NGSizeIndefinite)); |
333 NGPhysicalFragment* frag = RunBlockLayoutAlgorithm(space, div1); | 334 NGPhysicalFragment* frag = RunBlockLayoutAlgorithm(space, div1); |
334 | 335 |
335 // Verify that margins do NOT collapse. | 336 // Verify that margins do NOT collapse. |
336 frag = RunBlockLayoutAlgorithm(space, div1); | 337 frag = RunBlockLayoutAlgorithm(space, div1); |
337 EXPECT_EQ(NGMarginStrut({LayoutUnit(kDiv1Margin), LayoutUnit(kDiv1Margin)}), | 338 EXPECT_EQ(NGMarginStrut({LayoutUnit(kDiv1Margin), LayoutUnit(kDiv1Margin)}), |
338 frag->MarginStrut()); | 339 frag->MarginStrut()); |
339 ASSERT_EQ(frag->Children().size(), 1UL); | 340 ASSERT_EQ(frag->Children().size(), 1UL); |
340 | 341 |
341 EXPECT_EQ(NGMarginStrut({LayoutUnit(kDiv2Margin), LayoutUnit(kDiv2Margin)}), | 342 EXPECT_EQ(NGMarginStrut({LayoutUnit(kDiv2Margin), LayoutUnit(kDiv2Margin)}), |
342 frag->Children()[0]->MarginStrut()); | 343 static_cast<const NGPhysicalFragment*>(frag->Children()[0].get()) |
344 ->MarginStrut()); | |
343 | 345 |
344 // Reset padding and verify that margins DO collapse. | 346 // Reset padding and verify that margins DO collapse. |
345 div1_style->setPaddingTop(Length(0, Fixed)); | 347 div1_style->setPaddingTop(Length(0, Fixed)); |
346 div1_style->setPaddingBottom(Length(0, Fixed)); | 348 div1_style->setPaddingBottom(Length(0, Fixed)); |
347 frag = RunBlockLayoutAlgorithm(space, div1); | 349 frag = RunBlockLayoutAlgorithm(space, div1); |
348 EXPECT_EQ(NGMarginStrut({LayoutUnit(kDiv2Margin), LayoutUnit(kDiv2Margin)}), | 350 EXPECT_EQ(NGMarginStrut({LayoutUnit(kDiv2Margin), LayoutUnit(kDiv2Margin)}), |
349 frag->MarginStrut()); | 351 frag->MarginStrut()); |
350 } | 352 } |
351 | 353 |
352 // Verifies that margins of 2 adjoining blocks with different writing modes | 354 // Verifies that margins of 2 adjoining blocks with different writing modes |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
387 NGLogicalSize(LayoutUnit(500), LayoutUnit(500))); | 389 NGLogicalSize(LayoutUnit(500), LayoutUnit(500))); |
388 NGPhysicalFragment* frag = RunBlockLayoutAlgorithm(space, vertical_div); | 390 NGPhysicalFragment* frag = RunBlockLayoutAlgorithm(space, vertical_div); |
389 | 391 |
390 ASSERT_EQ(frag->Children().size(), 2UL); | 392 ASSERT_EQ(frag->Children().size(), 2UL); |
391 const NGPhysicalFragmentBase* child = frag->Children()[1]; | 393 const NGPhysicalFragmentBase* child = frag->Children()[1]; |
392 // Horizontal div | 394 // Horizontal div |
393 EXPECT_EQ(0, child->TopOffset()); | 395 EXPECT_EQ(0, child->TopOffset()); |
394 EXPECT_EQ(kVerticalDivWidth + kHorizontalDivMarginLeft, child->LeftOffset()); | 396 EXPECT_EQ(kVerticalDivWidth + kHorizontalDivMarginLeft, child->LeftOffset()); |
395 } | 397 } |
396 | 398 |
399 // Verifies that the margin strut of a child with a different writing mode does | |
400 // not get used in the collapsing margins calculation. | |
401 // | |
402 // Test case's HTML representation: | |
403 // <style> | |
404 // #div1 { margin-bottom: 10px; height: 60px; writing-mode: vertical-rl; } | |
405 // #div2 { margin-left: -20px; width: 10px; } | |
406 // #div3 { margin-top: 40px; height: 60px; } | |
407 // </style> | |
408 // <div id="div1"> | |
409 // <div id="div2">vertical</div> | |
410 // </div> | |
411 // <div id="div3"></div> | |
412 TEST_F(NGBlockLayoutAlgorithmTest, CollapsingMarginsCase6) { | |
eae
2016/10/17 18:03:04
Great test, thanks for adding it!
| |
413 const int kHeight = 60; | |
414 const int kWidth = 10; | |
415 const int kMarginBottom = 10; | |
416 const int kMarginLeft = -20; | |
417 const int kMarginTop = 40; | |
418 | |
419 style_->setWidth(Length(500, Fixed)); | |
420 style_->setHeight(Length(500, Fixed)); | |
421 | |
422 // DIV1 | |
423 RefPtr<ComputedStyle> div1_style = ComputedStyle::create(); | |
424 div1_style->setWidth(Length(kWidth, Fixed)); | |
425 div1_style->setHeight(Length(kHeight, Fixed)); | |
426 div1_style->setWritingMode(RightToLeftWritingMode); | |
427 div1_style->setMarginBottom(Length(kMarginBottom, Fixed)); | |
428 NGBox* div1 = new NGBox(div1_style.get()); | |
429 | |
430 // DIV2 | |
431 RefPtr<ComputedStyle> div2_style = ComputedStyle::create(); | |
432 div2_style->setWidth(Length(kWidth, Fixed)); | |
433 div2_style->setMarginLeft(Length(kMarginLeft, Fixed)); | |
434 NGBox* div2 = new NGBox(div2_style.get()); | |
435 | |
436 // DIV3 | |
437 RefPtr<ComputedStyle> div3_style = ComputedStyle::create(); | |
438 div3_style->setHeight(Length(kHeight, Fixed)); | |
439 div3_style->setMarginTop(Length(kMarginTop, Fixed)); | |
440 NGBox* div3 = new NGBox(div3_style.get()); | |
441 | |
442 div1->SetFirstChild(div2); | |
443 div1->SetNextSibling(div3); | |
444 | |
445 auto* space = | |
446 new NGConstraintSpace(HorizontalTopBottom, LeftToRight, | |
447 NGLogicalSize(LayoutUnit(500), LayoutUnit(500))); | |
448 NGPhysicalFragment* frag = RunBlockLayoutAlgorithm(space, div1); | |
449 | |
450 ASSERT_EQ(frag->Children().size(), 2UL); | |
451 | |
452 const NGPhysicalFragmentBase* child1 = frag->Children()[0]; | |
453 EXPECT_EQ(0, child1->TopOffset()); | |
454 EXPECT_EQ(kHeight, child1->Height()); | |
455 | |
456 const NGPhysicalFragmentBase* child2 = frag->Children()[1]; | |
457 EXPECT_EQ(kHeight + std::max(kMarginBottom, kMarginTop), child2->TopOffset()); | |
458 } | |
459 | |
397 // Verifies that a box's size includes its borders and padding, and that | 460 // Verifies that a box's size includes its borders and padding, and that |
398 // children are positioned inside the content box. | 461 // children are positioned inside the content box. |
399 // | 462 // |
400 // Test case's HTML representation: | 463 // Test case's HTML representation: |
401 // <style> | 464 // <style> |
402 // #div1 { width:100px; height:100px; } | 465 // #div1 { width:100px; height:100px; } |
403 // #div1 { border-style:solid; border-width:1px 2px 3px 4px; } | 466 // #div1 { border-style:solid; border-width:1px 2px 3px 4px; } |
404 // #div1 { padding:5px 6px 7px 8px; } | 467 // #div1 { padding:5px 6px 7px 8px; } |
405 // </style> | 468 // </style> |
406 // <div id="div1"> | 469 // <div id="div1"> |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
514 EXPECT_EQ(LayoutUnit(kWidth + kPaddingLeft), frag->WidthOverflow()); | 577 EXPECT_EQ(LayoutUnit(kWidth + kPaddingLeft), frag->WidthOverflow()); |
515 ASSERT_EQ(1UL, frag->Children().size()); | 578 ASSERT_EQ(1UL, frag->Children().size()); |
516 | 579 |
517 const NGPhysicalFragmentBase* child = frag->Children()[0]; | 580 const NGPhysicalFragmentBase* child = frag->Children()[0]; |
518 EXPECT_EQ(LayoutUnit(kChildWidth), child->Width()); | 581 EXPECT_EQ(LayoutUnit(kChildWidth), child->Width()); |
519 EXPECT_EQ(LayoutUnit(kPaddingLeft + 10), child->LeftOffset()); | 582 EXPECT_EQ(LayoutUnit(kPaddingLeft + 10), child->LeftOffset()); |
520 EXPECT_EQ(LayoutUnit(0), child->TopOffset()); | 583 EXPECT_EQ(LayoutUnit(0), child->TopOffset()); |
521 } | 584 } |
522 } // namespace | 585 } // namespace |
523 } // namespace blink | 586 } // namespace blink |
OLD | NEW |