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

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

Issue 1847583003: Fix setComposingText when newCursorPosition != 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add some C++ unit tests. Created 4 years, 8 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 2016 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/PlainTextRange.h"
6
7 #include "core/dom/Element.h"
8 #include "core/dom/Range.h"
9 #include "core/editing/FrameSelection.h"
10 #include "core/editing/InputMethodController.h"
11 #include "core/frame/LocalFrame.h"
12 #include "core/html/HTMLDocument.h"
13 #include "core/html/HTMLInputElement.h"
14 #include "core/testing/DummyPageHolder.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace blink {
18
19 class PlainTextRangeTest : public ::testing::Test {
20 protected:
21 HTMLDocument& document() const { return *m_document; }
22 LocalFrame& frame() const { return m_dummyPageHolder->frame(); }
23 Element* insertHTMLElement(const char* elementCode, const char* elementId);
24
25 private:
26 void SetUp() override;
27
28 OwnPtr<DummyPageHolder> m_dummyPageHolder;
29 Persistent<HTMLDocument> m_document;
30 };
31
32 void PlainTextRangeTest::SetUp()
33 {
34 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600));
35 m_document = toHTMLDocument(&m_dummyPageHolder->document());
36 ASSERT(m_document);
37 }
38
39 Element* PlainTextRangeTest::insertHTMLElement(
40 const char* elementCode, const char* elementId)
41 {
42 document().write(elementCode);
43 document().updateLayout();
44 Element* element = document().getElementById(elementId);
45 element->focus();
46 return element;
47 }
48
49 TEST_F(PlainTextRangeTest, CreateRangeForSelection)
50 {
51 // Since the cursor never exceeds the left boundary, our test should focus o n right boundary cases.
52 HTMLInputElement* input = toHTMLInputElement(
53 insertHTMLElement("<input id='sample'>", "sample"));
54
55 input->setValue("hello");
56 Element* rootEditableElement = frame().selection().rootEditableElement();
57 EXPECT_TRUE(rootEditableElement);
58
59 // start and end are on the right boundary.
Changwan Ryu 2016/04/07 11:46:19 what happens when start / end are within boundary?
60 EphemeralRange range = PlainTextRange(5, 5).createRangeForSelection(*rootEdi tableElement);
61 EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode());
62 EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode());
63
64 // start is on the right boundary, and end exceeds right boundary.
65 range = PlainTextRange(5, 10).createRangeForSelection(*rootEditableElement);
66 EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode());
67 EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode());
68
69 // start and end exceed the right boundary.
70 range = PlainTextRange(10, 10).createRangeForSelection(*rootEditableElement) ;
71 EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode());
72 EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode());
73
74 input->setValue("");
75 rootEditableElement = frame().selection().rootEditableElement();
76 EXPECT_TRUE(rootEditableElement);
77
78 // start and end are on the right boundary.
79 range = PlainTextRange(0, 0).createRangeForSelection(*rootEditableElement);
80 EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode());
81 EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode());
82
83 // start is on the right boundary, and end exceeds right boundary.
84 range = PlainTextRange(0, 10).createRangeForSelection(*rootEditableElement);
85 EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode());
86 EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode());
87
88 // start and end exceed the right boundary.
89 range = PlainTextRange(10, 10).createRangeForSelection(*rootEditableElement) ;
90 EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode());
91 EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode());
92 }
93
94 TEST_F(PlainTextRangeTest, CreateRangeForInput)
95 {
96 // Since the cursor never exceeds the left boundary, our test should focus o n right boundary cases.
Changwan Ryu 2016/04/07 11:46:19 this line is not needed
97 HTMLInputElement* input = toHTMLInputElement(
98 insertHTMLElement("<input id='sample'>", "sample"));
99
100 input->setValue("hello");
101 Element* rootEditableElement = frame().selection().rootEditableElement();
102 EXPECT_TRUE(rootEditableElement);
103
104 // start and end are on the right boundary.
105 EphemeralRange range = PlainTextRange(5, 5).createRange(*rootEditableElement );
106 EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode());
107 EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode());
108
109 // start is on the right boundary, and end exceeds right boundary.
110 range = PlainTextRange(5, 10).createRange(*rootEditableElement);
111 EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode());
112 EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode());
113
114 // start and end exceed the right boundary.
115 range = PlainTextRange(10, 10).createRange(*rootEditableElement);
116 EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode());
117 EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode());
118
119 input->setValue("");
120 rootEditableElement = frame().selection().rootEditableElement();
121 EXPECT_TRUE(rootEditableElement);
122
123 // start and end are on the right boundary.
124 range = PlainTextRange(0, 0).createRange(*rootEditableElement);
125 EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode());
126 EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode());
127
128 // start is on the right boundary, and end exceeds right boundary.
129 range = PlainTextRange(0, 10).createRange(*rootEditableElement);
130 EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode());
131 EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode());
132
133 // start and end exceed the right boundary.
134 range = PlainTextRange(10, 10).createRange(*rootEditableElement);
135 EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode());
136 EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode());
137 }
138
139 TEST_F(PlainTextRangeTest, CreateRangeForEditableWithEmptyNode)
Changwan Ryu 2016/04/07 11:46:19 s/Editable/ContentEditable/ here and other places
140 {
141 // Since the cursor never exceeds the left boundary, our test should focus o n right boundary cases.
Changwan Ryu 2016/04/07 11:46:19 This line is probably not needed.
142 // There is only 1 empty node.
143 Element* div = insertHTMLElement(
144 "<div id='sample' contenteditable='true'></div>", "sample");
145
146 // start and end are on right boundary.
147 EphemeralRange range = PlainTextRange(0, 0).createRange(*div);
148 EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode());
149 EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode());
150
151 // start is on right boundary, and end exceeds right boundary.
152 range = PlainTextRange(0, 10).createRange(*div);
153 EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode());
154 EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode());
155
156 // start and end exceed right boundary.
157 range = PlainTextRange(10, 10).createRange(*div);
158 EXPECT_EQ(0, range.startPosition().computeOffsetInContainerNode());
159 EXPECT_EQ(0, range.endPosition().computeOffsetInContainerNode());
160 }
161
162 TEST_F(PlainTextRangeTest, CreateRangeForEditableWithLineBreakAtTheEnd)
163 {
164 // Since the cursor never exceeds the left boundary, our test should focus o n right boundary cases.
165 // There are 2 nodes and 5+1 characters: "hello", '\n'.
166 Element* div = insertHTMLElement(
167 "<div id='sample' contenteditable='true'>"
168 "hello"
169 "<p id='sample2' contenteditable='true'></p>",
170 "sample");
171
172 // start and end are on right boundary of the 1st node.
173 EphemeralRange range = PlainTextRange(5, 5).createRange(*div);
174 EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode());
175 EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode());
176
177 // start is on right boundary of the 1st node, and end is on right boundary of the 2nd node.
178 range = PlainTextRange(5, 6).createRange(*div);
179 EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode());
180 EXPECT_EQ(1, range.endPosition().computeOffsetInContainerNode());
181
182 // start and end are on right boundary of the 2nd node.
183 range = PlainTextRange(6, 6).createRange(*div);
184 EXPECT_EQ(1, range.startPosition().computeOffsetInContainerNode());
185 EXPECT_EQ(1, range.endPosition().computeOffsetInContainerNode());
186
187 // start is on right boundary of the 2nd node, and end exceeds right boundar y.
188 range = PlainTextRange(6, 10).createRange(*div);
189 EXPECT_EQ(1, range.startPosition().computeOffsetInContainerNode());
190 EXPECT_EQ(1, range.endPosition().computeOffsetInContainerNode());
191
192 // start and end exceed right boundary.
193 range = PlainTextRange(10, 10).createRange(*div);
194 EXPECT_EQ(1, range.startPosition().computeOffsetInContainerNode());
195 EXPECT_EQ(1, range.endPosition().computeOffsetInContainerNode());
196 }
197
198 TEST_F(PlainTextRangeTest, CreateRangeForEditableWithLineBreakInTheMiddle)
Changwan Ryu 2016/04/07 11:46:19 could you also check the cases where start and end
199 {
200 // Since the cursor never exceeds the left boundary, our test should focus o n right boundary cases.
Changwan Ryu 2016/04/07 11:46:19 this line is not needed
201 // There are 3 nodes and 5+1+6 characters: "hello", '\n', "world!".
202 Element* div = insertHTMLElement(
203 "<div id='sample' contenteditable='true'>"
204 "hello"
205 "<p id='sample2' contenteditable='true'>world!</p>"
206 "</div>",
207 "sample");
208
209 // start and end are on right boundary of the 1st node.
210 EphemeralRange range = PlainTextRange(5, 5).createRange(*div);
211 EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode());
212 EXPECT_EQ(5, range.endPosition().computeOffsetInContainerNode());
213
214 // start is on right boundary of the 1st node, and end is on right boundary of the 3rd node.
215 range = PlainTextRange(5, 12).createRange(*div);
216 EXPECT_EQ(5, range.startPosition().computeOffsetInContainerNode());
217 EXPECT_EQ(6, range.endPosition().computeOffsetInContainerNode());
218
219 // start is on right boundary of the 2nd node, and end is on right boundary of the 3rd node.
220 range = PlainTextRange(6, 12).createRange(*div);
221 EXPECT_EQ(1, range.startPosition().computeOffsetInContainerNode());
222 EXPECT_EQ(6, range.endPosition().computeOffsetInContainerNode());
223
224 // start and end are on right boundary of the 3rd node.
225 range = PlainTextRange(12, 12).createRange(*div);
226 EXPECT_EQ(6, range.startPosition().computeOffsetInContainerNode());
227 EXPECT_EQ(6, range.endPosition().computeOffsetInContainerNode());
228
229 // start and end exceed right boundary.
230 range = PlainTextRange(100, 200).createRange(*div);
231 EXPECT_EQ(6, range.startPosition().computeOffsetInContainerNode());
232 EXPECT_EQ(6, range.endPosition().computeOffsetInContainerNode());
233 }
234 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698