Index: third_party/WebKit/Source/core/editing/LayoutSelectionTest.cpp |
diff --git a/third_party/WebKit/Source/core/editing/LayoutSelectionTest.cpp b/third_party/WebKit/Source/core/editing/LayoutSelectionTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..08054daf5d7ff745b960ff7dd5bd511f34306eb8 |
--- /dev/null |
+++ b/third_party/WebKit/Source/core/editing/LayoutSelectionTest.cpp |
@@ -0,0 +1,136 @@ |
+// Copyright 2015 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/editing/LayoutSelection.h" |
+ |
+#include "core/editing/EditingTestBase.h" |
+#include "core/layout/LayoutTestHelper.h" |
+#include "core/layout/LayoutText.h" |
+#include "core/layout/ng/inline/ng_inline_node.h" |
+#include "core/layout/ng/layout_ng_block_flow.h" |
+#include "core/layout/ng/ng_block_layout_algorithm.h" |
+#include "core/layout/ng/ng_constraint_space_builder.h" |
+#include "core/layout/ng/ng_layout_result.h" |
+ |
+#include "platform/heap/HeapAllocator.h" |
+ |
+namespace blink { |
+ |
+class LayoutSelectionTest : public EditingTestBase { |
+ public: |
+ NGInlineNode LayoutFirstInlineNode() { |
+ Node* body = GetDocument().body(); |
+ LayoutObject* layout_object = body->firstChild()->GetLayoutObject(); |
+ LayoutNGBlockFlow* block_flow = ToLayoutNGBlockFlow(layout_object); |
+ NGInlineNode inline_node(block_flow, block_flow->FirstChild()); |
+ RefPtr<NGConstraintSpace> constraint_space = |
+ NGConstraintSpaceBuilder(NGWritingMode::kHorizontalTopBottom) |
+ .SetAvailableSize({LayoutUnit(200), LayoutUnit(200)}) |
+ .ToConstraintSpace(NGWritingMode::kHorizontalTopBottom); |
+ RefPtr<NGLayoutResult> layout_result = |
+ inline_node.Layout(constraint_space.Get(), nullptr); |
+ return inline_node; |
+ } |
+ |
+ ShadowRoot* CreateShadowRootForElementAndSetInnerHTMLV1( |
+ const char* host_element_id, |
+ const char* shadow_root_content) { |
+ ShadowRoot* shadow_root = |
+ GetDocument() |
+ .getElementById(AtomicString::FromUTF8(host_element_id)) |
+ ->CreateShadowRootInternal(ShadowRootType::kOpen, |
+ ASSERT_NO_EXCEPTION); |
+ shadow_root->setInnerHTML(String::FromUTF8(shadow_root_content), |
+ ASSERT_NO_EXCEPTION); |
+ GetDocument().View()->UpdateAllLifecyclePhases(); |
+ return shadow_root; |
+ } |
+ |
+ protected: |
+ void SetUp() override { |
+ EditingTestBase::SetUp(); |
+ RuntimeEnabledFeatures::SetLayoutNGEnabled(true); |
+ } |
+}; |
+ |
+TEST_F(LayoutSelectionTest, LayoutNGOffsetTest) { |
+ SetBodyContent("<div id=target>foo<span id=span>bar</span>baz</div>"); |
+ Element* target = GetDocument().getElementById("target"); |
+ NGTextOffsetMap::Builder builder; |
+ builder.Add(target, 0, 1); |
+ builder.Add(target, 1, 3); |
+ NGTextOffsetMap offset_map = builder.Build(); |
+ EXPECT_EQ(1, offset_map.Get(target, 0)); |
+ EXPECT_EQ(3, offset_map.Get(target, 1)); |
+ EXPECT_FALSE(offset_map.Get(target, 2).has_value()); |
+ EXPECT_FALSE(offset_map.Get(nullptr, 0).has_value()); |
+} |
+ |
+TEST_F(LayoutSelectionTest, Shadow) { |
+ SetBodyContent( |
+ "<div id='target'>foo<span slot='s1'>s1</span><span " |
+ "slot='s2'>s2</span></div>"); |
+ CreateShadowRootForElementAndSetInnerHTMLV1( |
+ "target", "hoge<slot name='s2'></slot><slot name='s1'></slot>"); |
+ NGInlineNode inline_node = LayoutFirstInlineNode(); |
+ EXPECT_EQ("foobabazz", inline_node.Text()); |
+} |
+ |
+TEST_F(LayoutSelectionTest, BasicMapping) { |
+ SetBodyContent("<div>foo<span>ba</span>bazz</div>"); |
+ NGInlineNode inline_node = LayoutFirstInlineNode(); |
+ EXPECT_EQ("foobabazz", inline_node.Text()); |
+ |
+ NGTextOffsetMap ngoffset = NGTextOffsetMap::Create( |
+ *ToLayoutNGBlockFlow(inline_node.GetLayoutBlockFlow())); |
+ |
+ Element* div = GetDocument().QuerySelector("div"); |
+ Text* foo = ToText(div->firstChild()); |
+ Text* ba = ToText(GetDocument().QuerySelector("span")->firstChild()); |
+ Text* bazz = ToText(div->firstChild()->nextSibling()->nextSibling()); |
+ EXPECT_FALSE(ngoffset.Get(div, 0).has_value()); |
+ EXPECT_FALSE(ngoffset.Get(div, 1).has_value()); |
+ EXPECT_EQ(0, ngoffset.Get(foo, 0)); |
+ EXPECT_EQ(1, ngoffset.Get(foo, 1)); |
+ EXPECT_EQ(2, ngoffset.Get(foo, 2)); |
+ EXPECT_EQ(3, ngoffset.Get(foo, 3)); |
+ EXPECT_EQ(3, ngoffset.Get(ba, 0)); |
+ EXPECT_EQ(4, ngoffset.Get(ba, 1)); |
+ EXPECT_EQ(5, ngoffset.Get(ba, 2)); |
+ EXPECT_EQ(5, ngoffset.Get(bazz, 0)); |
+ EXPECT_EQ(6, ngoffset.Get(bazz, 1)); |
+ EXPECT_EQ(7, ngoffset.Get(bazz, 2)); |
+ EXPECT_EQ(8, ngoffset.Get(bazz, 3)); |
+ EXPECT_EQ(9, ngoffset.Get(bazz, 4)); |
+} |
+ |
+TEST_F(LayoutSelectionTest, WhiteSpaceCollapsing) { |
+ SetBodyContent("<div>foo baz</div>"); |
+ NGInlineNode inline_node = LayoutFirstInlineNode(); |
+ EXPECT_EQ("foo baz", inline_node.Text()); |
+ |
+ NGTextOffsetMap ngoffset = NGTextOffsetMap::Create( |
+ *ToLayoutNGBlockFlow(inline_node.GetLayoutBlockFlow())); |
+ |
+ Element* div = GetDocument().QuerySelector("div"); |
+ Text* text = ToText(div->firstChild()); |
+ EXPECT_EQ(0, ngoffset.Get(text, 0)); // |foo baz to |foo baz |
+ EXPECT_EQ(1, ngoffset.Get(text, 1)); // f|oo baz to f|oo baz |
+ EXPECT_EQ(2, ngoffset.Get(text, 2)); // fo|o baz to fo|o baz |
+ EXPECT_EQ(3, ngoffset.Get(text, 3)); // foo| baz to foo| baz |
+ EXPECT_EQ(4, ngoffset.Get(text, 4)); // foo | baz to foo |baz |
+ EXPECT_EQ(4, ngoffset.Get(text, 5)); // foo | baz to foo |baz |
+ EXPECT_EQ(4, ngoffset.Get(text, 6)); // foo | baz to foo |baz |
+ EXPECT_EQ(4, ngoffset.Get(text, 7)); // foo |baz to foo |baz |
+ EXPECT_EQ(5, ngoffset.Get(text, 8)); // foo b|az to foo b|az |
+ EXPECT_EQ(6, ngoffset.Get(text, 9)); // foo ba|z to foo ba|z |
+ EXPECT_EQ(7, ngoffset.Get(text, 10)); // foo baz| to foo baz| |
+} |
+ |
+// TODO(yoichio): We should have |
+// TEST_F(LayoutSelectionTest, TextTranceFormation) {} |
+// TEST_F(LayoutSelectionTest, LineEndWrapping) {} |
+// TEST_F(LayoutSelectionTest, LineEndHyphenation) {} |
+ |
+} // namespace blink |