Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(418)

Side by Side Diff: third_party/WebKit/Source/core/editing/LayoutSelectionTest.cpp

Issue 2913773002: [WIP][b:eae_mywip_paint] Paint Selection NG.
Patch Set: update Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "core/editing/LayoutSelection.h"
6
7 #include "core/editing/EditingTestBase.h"
8 #include "core/layout/LayoutTestHelper.h"
9 #include "core/layout/LayoutText.h"
10 #include "core/layout/ng/inline/ng_inline_node.h"
11 #include "core/layout/ng/layout_ng_block_flow.h"
12 #include "core/layout/ng/ng_block_layout_algorithm.h"
13 #include "core/layout/ng/ng_constraint_space_builder.h"
14 #include "core/layout/ng/ng_layout_result.h"
15
16 #include "platform/heap/HeapAllocator.h"
17
18 namespace blink {
19
20 class LayoutSelectionTest : public EditingTestBase {
21 public:
22 NGInlineNode LayoutFirstInlineNode() {
23 Node* body = GetDocument().body();
24 LayoutObject* layout_object = body->firstChild()->GetLayoutObject();
25 LayoutNGBlockFlow* block_flow = ToLayoutNGBlockFlow(layout_object);
26 NGInlineNode inline_node(block_flow, block_flow->FirstChild());
27 RefPtr<NGConstraintSpace> constraint_space =
28 NGConstraintSpaceBuilder(NGWritingMode::kHorizontalTopBottom)
29 .SetAvailableSize({LayoutUnit(200), LayoutUnit(200)})
30 .ToConstraintSpace(NGWritingMode::kHorizontalTopBottom);
31 RefPtr<NGLayoutResult> layout_result =
32 inline_node.Layout(constraint_space.Get(), nullptr);
33 return inline_node;
34 }
35
36 ShadowRoot* CreateShadowRootForElementAndSetInnerHTMLV1(
37 const char* host_element_id,
38 const char* shadow_root_content) {
39 ShadowRoot* shadow_root =
40 GetDocument()
41 .getElementById(AtomicString::FromUTF8(host_element_id))
42 ->CreateShadowRootInternal(ShadowRootType::kOpen,
43 ASSERT_NO_EXCEPTION);
44 shadow_root->setInnerHTML(String::FromUTF8(shadow_root_content),
45 ASSERT_NO_EXCEPTION);
46 GetDocument().View()->UpdateAllLifecyclePhases();
47 return shadow_root;
48 }
49
50 protected:
51 void SetUp() override {
52 EditingTestBase::SetUp();
53 RuntimeEnabledFeatures::SetLayoutNGEnabled(true);
54 }
55 };
56
57 TEST_F(LayoutSelectionTest, LayoutNGOffsetTest) {
58 SetBodyContent("<div id=target>foo<span id=span>bar</span>baz</div>");
59 Element* target = GetDocument().getElementById("target");
60 NGTextOffsetMap::Builder builder;
61 builder.Add(target, 0, 1);
62 builder.Add(target, 1, 3);
63 NGTextOffsetMap offset_map = builder.Build();
64 EXPECT_EQ(1, offset_map.Get(target, 0));
65 EXPECT_EQ(3, offset_map.Get(target, 1));
66 EXPECT_FALSE(offset_map.Get(target, 2).has_value());
67 EXPECT_FALSE(offset_map.Get(nullptr, 0).has_value());
68 }
69
70 TEST_F(LayoutSelectionTest, Shadow) {
71 SetBodyContent(
72 "<div id='target'>foo<span slot='s1'>s1</span><span "
73 "slot='s2'>s2</span></div>");
74 CreateShadowRootForElementAndSetInnerHTMLV1(
75 "target", "hoge<slot name='s2'></slot><slot name='s1'></slot>");
76 NGInlineNode inline_node = LayoutFirstInlineNode();
77 EXPECT_EQ("foobabazz", inline_node.Text());
78 }
79
80 TEST_F(LayoutSelectionTest, BasicMapping) {
81 SetBodyContent("<div>foo<span>ba</span>bazz</div>");
82 NGInlineNode inline_node = LayoutFirstInlineNode();
83 EXPECT_EQ("foobabazz", inline_node.Text());
84
85 NGTextOffsetMap ngoffset = NGTextOffsetMap::Create(
86 *ToLayoutNGBlockFlow(inline_node.GetLayoutBlockFlow()));
87
88 Element* div = GetDocument().QuerySelector("div");
89 Text* foo = ToText(div->firstChild());
90 Text* ba = ToText(GetDocument().QuerySelector("span")->firstChild());
91 Text* bazz = ToText(div->firstChild()->nextSibling()->nextSibling());
92 EXPECT_FALSE(ngoffset.Get(div, 0).has_value());
93 EXPECT_FALSE(ngoffset.Get(div, 1).has_value());
94 EXPECT_EQ(0, ngoffset.Get(foo, 0));
95 EXPECT_EQ(1, ngoffset.Get(foo, 1));
96 EXPECT_EQ(2, ngoffset.Get(foo, 2));
97 EXPECT_EQ(3, ngoffset.Get(foo, 3));
98 EXPECT_EQ(3, ngoffset.Get(ba, 0));
99 EXPECT_EQ(4, ngoffset.Get(ba, 1));
100 EXPECT_EQ(5, ngoffset.Get(ba, 2));
101 EXPECT_EQ(5, ngoffset.Get(bazz, 0));
102 EXPECT_EQ(6, ngoffset.Get(bazz, 1));
103 EXPECT_EQ(7, ngoffset.Get(bazz, 2));
104 EXPECT_EQ(8, ngoffset.Get(bazz, 3));
105 EXPECT_EQ(9, ngoffset.Get(bazz, 4));
106 }
107
108 TEST_F(LayoutSelectionTest, WhiteSpaceCollapsing) {
109 SetBodyContent("<div>foo baz</div>");
110 NGInlineNode inline_node = LayoutFirstInlineNode();
111 EXPECT_EQ("foo baz", inline_node.Text());
112
113 NGTextOffsetMap ngoffset = NGTextOffsetMap::Create(
114 *ToLayoutNGBlockFlow(inline_node.GetLayoutBlockFlow()));
115
116 Element* div = GetDocument().QuerySelector("div");
117 Text* text = ToText(div->firstChild());
118 EXPECT_EQ(0, ngoffset.Get(text, 0)); // |foo baz to |foo baz
119 EXPECT_EQ(1, ngoffset.Get(text, 1)); // f|oo baz to f|oo baz
120 EXPECT_EQ(2, ngoffset.Get(text, 2)); // fo|o baz to fo|o baz
121 EXPECT_EQ(3, ngoffset.Get(text, 3)); // foo| baz to foo| baz
122 EXPECT_EQ(4, ngoffset.Get(text, 4)); // foo | baz to foo |baz
123 EXPECT_EQ(4, ngoffset.Get(text, 5)); // foo | baz to foo |baz
124 EXPECT_EQ(4, ngoffset.Get(text, 6)); // foo | baz to foo |baz
125 EXPECT_EQ(4, ngoffset.Get(text, 7)); // foo |baz to foo |baz
126 EXPECT_EQ(5, ngoffset.Get(text, 8)); // foo b|az to foo b|az
127 EXPECT_EQ(6, ngoffset.Get(text, 9)); // foo ba|z to foo ba|z
128 EXPECT_EQ(7, ngoffset.Get(text, 10)); // foo baz| to foo baz|
129 }
130
131 // TODO(yoichio): We should have
132 // TEST_F(LayoutSelectionTest, TextTranceFormation) {}
133 // TEST_F(LayoutSelectionTest, LineEndWrapping) {}
134 // TEST_F(LayoutSelectionTest, LineEndHyphenation) {}
135
136 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698