blob: af6aa3cc09ba16609ec502c45fc22d28bf3e8f8f [file] [log] [blame]
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "core/layout/ng/ng_base_layout_algorithm_test.h"
#include "core/dom/TagCollection.h"
#include "core/layout/line/InlineTextBox.h"
#include "core/layout/ng/ng_inline_node.h"
#include "core/layout/ng/ng_physical_text_fragment.h"
#include "platform/geometry/LayoutPoint.h"
#include "platform/geometry/LayoutRect.h"
namespace blink {
namespace {
class NGTextLayoutAlgorithmTest : public NGBaseLayoutAlgorithmTest {};
// Verifies that text can flow correctly around floats that were positioned
// before the inline block.
TEST_F(NGTextLayoutAlgorithmTest, TextFloatsAroundFloatsBefore) {
setBodyInnerHTML(R"HTML(
<!DOCTYPE html>
<style>
* {
font-family: "Arial", sans-serif;
font-size: 19px;
}
#container {
height: 200px; width: 200px; outline: solid blue;
}
#left-float1 {
float: left; width: 30px; height: 30px; background-color: blue;
}
#left-float2 {
float: left; width: 10px; height: 10px;
background-color: purple;
}
#right-float {
float: right; width: 40px; height: 40px; background-color: yellow;
}
</style>
<div id="container">
<div id="left-float1"></div>
<div id="left-float2"></div>
<div id="right-float"></div>
<span id="text">The quick brown fox jumps over the lazy dog</span>
</div>
)HTML");
// ** Run LayoutNG algorithm **
RefPtr<NGConstraintSpace> space;
RefPtr<NGPhysicalBoxFragment> html_fragment;
std::tie(html_fragment, space) = RunBlockLayoutAlgorithmForElement(
document().getElementsByTagName("html")->item(0));
auto* body_fragment =
toNGPhysicalBoxFragment(html_fragment->Children()[0].get());
auto* container_fragment =
toNGPhysicalBoxFragment(body_fragment->Children()[0].get());
auto* text_fragments_wrapper =
toNGPhysicalBoxFragment(container_fragment->Children()[0].get());
LayoutText* layout_text =
toLayoutText(getLayoutObjectByElementId("text")->slowFirstChild());
ASSERT(layout_text->hasTextBoxes());
// TODO(glebl): Should have only 3 text fragments. For some reason we have a
// left over fragment with text == "dog".
ASSERT_EQ(4UL, text_fragments_wrapper->Children().size());
auto* text_fragment1 =
toNGPhysicalTextFragment(text_fragments_wrapper->Children()[0].get());
auto* text_node = text_fragment1->Node();
// 40 = #left-float1' width 30 + #left-float2 10
EXPECT_EQ(LayoutUnit(40), text_fragment1->LeftOffset());
EXPECT_EQ("The quick ", text_node->Text(text_fragment1->StartOffset(),
text_fragment1->EndOffset()));
InlineTextBox* inline_text_box1 = layout_text->firstTextBox();
EXPECT_EQ(LayoutUnit(40), inline_text_box1->x());
auto* text_fragment2 =
toNGPhysicalTextFragment(text_fragments_wrapper->Children()[1].get());
// 40 = #left-float1' width 30
EXPECT_EQ(LayoutUnit(30), text_fragment2->LeftOffset());
EXPECT_EQ("brown fox jumps over",
text_node->Text(text_fragment2->StartOffset(),
text_fragment2->EndOffset()));
InlineTextBox* inline_text_box2 = inline_text_box1->nextTextBox();
EXPECT_EQ(LayoutUnit(30), inline_text_box2->x());
auto* text_fragment3 =
toNGPhysicalTextFragment(text_fragments_wrapper->Children()[2].get());
EXPECT_EQ(LayoutUnit(), text_fragment3->LeftOffset());
EXPECT_EQ("jumps over the lazy dog",
text_node->Text(text_fragment3->StartOffset(),
text_fragment3->EndOffset()));
InlineTextBox* inline_text_box3 = inline_text_box2->nextTextBox();
EXPECT_EQ(LayoutUnit(), inline_text_box3->x());
}
// Verifies that text correctly flows around the inline float that fits on
// the same text line.
TEST_F(NGTextLayoutAlgorithmTest, TextFloatsAroundInlineFloatThatFitsOnLine) {
setBodyInnerHTML(R"HTML(
<!DOCTYPE html>
<style>
* {
font-family: "Arial", sans-serif;
font-size: 19px;
}
#container {
height: 200px; width: 200px; outline: solid orange;
}
#narrow-float {
float: left; width: 30px; height: 30px; background-color: blue;
}
</style>
<div id="container">
<span id="text">
The quick <div id="narrow-float"></div> brown fox jumps over the lazy
</span>
</div>
)HTML");
LayoutText* layout_text =
toLayoutText(getLayoutObjectByElementId("text")->slowFirstChild());
ASSERT(layout_text->hasTextBoxes());
InlineTextBox* inline_text_box1 = layout_text->firstTextBox();
// 30 == narrow-float's width.
EXPECT_EQ(LayoutUnit(30), inline_text_box1->x());
Element* narrow_float = document().getElementById("narrow-float");
// 8 == body's margin.
EXPECT_EQ(8, narrow_float->offsetLeft());
EXPECT_EQ(8, narrow_float->offsetTop());
}
// Verifies that the inline float got pushed to the next line if it doesn't
// fit the current line.
TEST_F(NGTextLayoutAlgorithmTest,
TextFloatsAroundInlineFloatThatDoesNotFitOnLine) {
setBodyInnerHTML(R"HTML(
<!DOCTYPE html>
<style>
* {
font-family: "Arial", sans-serif;
font-size: 19px;
}
#container {
height: 200px; width: 200px; outline: solid orange;
}
#wide-float {
float: left; width: 160px; height: 30px; background-color: red;
}
</style>
<div id="container">
<span id="text">
The quick <div id="wide-float"></div> brown fox jumps over the lazy dog
</span>
</div>
)HTML");
LayoutText* layout_text =
toLayoutText(getLayoutObjectByElementId("text")->slowFirstChild());
ASSERT(layout_text->hasTextBoxes());
InlineTextBox* inline_text_box1 = layout_text->firstTextBox();
EXPECT_EQ(LayoutUnit(), inline_text_box1->x());
Element* wide_float = document().getElementById("wide-float");
// 8 == body's margin.
EXPECT_EQ(8, wide_float->offsetLeft());
}
// Verifies that if an inline float pushed to the next line then all others
// following inline floats positioned with respect to the float's top edge
// alignment rule.
TEST_F(NGTextLayoutAlgorithmTest,
FloatsArePositionedWithRespectToTopEdgeAlignmentRule) {
setBodyInnerHTML(R"HTML(
<!DOCTYPE html>
<style>
* {
font-family: "Arial", sans-serif;
font-size: 19px;
}
#container {
height: 200px; width: 200px; outline: solid orange;
}
#left-narrow {
float: left; width: 5px; height: 30px; background-color: blue;
}
#left-wide {
float: left; width: 160px; height: 30px; background-color: red;
}
</style>
<div id="container">
<span id="text">
The quick <div id="left-wide"></div> brown <div id="left-narrow"></div>
fox jumps over the lazy dog
</span>
</div>
)HTML");
Element* wide_float = document().getElementById("left-wide");
// 8 == body's margin.
EXPECT_EQ(8, wide_float->offsetLeft());
Element* narrow_float = document().getElementById("left-narrow");
// 160 float-wide's width + 8 body's margin.
EXPECT_EQ(160 + 8, narrow_float->offsetLeft());
// On the same line.
EXPECT_EQ(wide_float->offsetTop(), narrow_float->offsetTop());
}
} // namespace
} // namespace blink