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

Side by Side Diff: Source/core/editing/FrameSelectionTest.cpp

Issue 1123563003: Improving direction-based selection strategy. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Addressing remaining review feedback. Created 5 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "config.h" 5 #include "config.h"
6 #include "core/editing/FrameSelection.h" 6 #include "core/editing/FrameSelection.h"
7 7
8 #include "bindings/core/v8/ExceptionStatePlaceholder.h" 8 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
9 #include "core/dom/Document.h" 9 #include "core/dom/Document.h"
10 #include "core/dom/Element.h" 10 #include "core/dom/Element.h"
11 #include "core/dom/Text.h" 11 #include "core/dom/Text.h"
12 #include "core/frame/FrameView.h" 12 #include "core/frame/FrameView.h"
13 #include "core/frame/Settings.h"
14 #include "core/html/HTMLBodyElement.h" 13 #include "core/html/HTMLBodyElement.h"
15 #include "core/html/HTMLDocument.h" 14 #include "core/html/HTMLDocument.h"
16 #include "core/testing/DummyPageHolder.h" 15 #include "core/testing/DummyPageHolder.h"
17 #include "wtf/OwnPtr.h" 16 #include "wtf/OwnPtr.h"
18 #include "wtf/PassRefPtr.h" 17 #include "wtf/PassRefPtr.h"
19 #include "wtf/RefPtr.h" 18 #include "wtf/RefPtr.h"
20 #include "wtf/StdLibExtras.h" 19 #include "wtf/StdLibExtras.h"
21 #include "wtf/testing/WTFTestHelpers.h" 20 #include "wtf/testing/WTFTestHelpers.h"
22 #include <gtest/gtest.h> 21 #include <gtest/gtest.h>
23 22
24 using namespace blink; 23 using namespace blink;
25 24
26 namespace { 25 namespace {
27 26
28 class FrameSelectionTest : public ::testing::Test { 27 class FrameSelectionTest : public ::testing::Test {
29 protected: 28 protected:
30 virtual void SetUp() override; 29 virtual void SetUp() override;
31 30
32 DummyPageHolder& dummyPageHolder() const { return *m_dummyPageHolder; } 31 DummyPageHolder& dummyPageHolder() const { return *m_dummyPageHolder; }
33 HTMLDocument& document() const; 32 HTMLDocument& document() const;
34 void setSelection(const VisibleSelection&); 33 void setSelection(const VisibleSelection&);
35 FrameSelection& selection() const; 34 FrameSelection& selection() const;
36 PassRefPtrWillBeRawPtr<Text> appendTextNode(const String& data); 35 PassRefPtrWillBeRawPtr<Text> appendTextNode(const String& data);
36 void setInnerHTML(const char*);
37 int layoutCount() const { return m_dummyPageHolder->frameView().layoutCount( ); } 37 int layoutCount() const { return m_dummyPageHolder->frameView().layoutCount( ); }
38 38
39 private: 39 private:
40 OwnPtr<DummyPageHolder> m_dummyPageHolder; 40 OwnPtr<DummyPageHolder> m_dummyPageHolder;
41 RawPtr<HTMLDocument> m_document; 41 RawPtr<HTMLDocument> m_document;
42 RefPtrWillBePersistent<Text> m_textNode; 42 RefPtrWillBePersistent<Text> m_textNode;
43 }; 43 };
44 44
45 void FrameSelectionTest::SetUp() 45 void FrameSelectionTest::SetUp()
46 { 46 {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 document().updateLayoutIgnorePendingStylesheets(); 115 document().updateLayoutIgnorePendingStylesheets();
116 selection().setCaretRectNeedsUpdate(); 116 selection().setCaretRectNeedsUpdate();
117 EXPECT_TRUE(selection().isCaretBoundsDirty()); 117 EXPECT_TRUE(selection().isCaretBoundsDirty());
118 selection().invalidateCaretRect(); 118 selection().invalidateCaretRect();
119 EXPECT_FALSE(selection().isCaretBoundsDirty()); 119 EXPECT_FALSE(selection().isCaretBoundsDirty());
120 } 120 }
121 121
122 TEST_F(FrameSelectionTest, PaintCaretShouldNotLayout) 122 TEST_F(FrameSelectionTest, PaintCaretShouldNotLayout)
123 { 123 {
124 RefPtrWillBeRawPtr<Text> text = appendTextNode("Hello, World!"); 124 RefPtrWillBeRawPtr<Text> text = appendTextNode("Hello, World!");
125 // TODO: Do I need this?
leviw_travelin_and_unemployed 2015/06/03 22:29:47 It'd be apparent quickly (at least on Debug) if yo
mfomitchev 2015/06/05 17:38:39 Sorry, this was a reminder for myself to check som
125 document().view()->updateLayoutAndStyleForPainting(); 126 document().view()->updateLayoutAndStyleForPainting();
126 127
127 document().body()->setContentEditable("true", ASSERT_NO_EXCEPTION); 128 document().body()->setContentEditable("true", ASSERT_NO_EXCEPTION);
128 document().body()->focus(); 129 document().body()->focus();
129 EXPECT_TRUE(document().body()->focused()); 130 EXPECT_TRUE(document().body()->focused());
130 131
131 VisibleSelection validSelection(Position(text, 0), Position(text, 0)); 132 VisibleSelection validSelection(Position(text, 0), Position(text, 0));
132 selection().setCaretVisible(true); 133 selection().setCaretVisible(true);
133 setSelection(validSelection); 134 setSelection(validSelection);
134 EXPECT_TRUE(selection().isCaret()); 135 EXPECT_TRUE(selection().isCaret());
(...skipping 25 matching lines...) Expand all
160 // "Foo| Bar Baz," 161 // "Foo| Bar Baz,"
161 EXPECT_TRUE(selection().selectWordAroundPosition(VisiblePosition(Position(te xt, 3)))); 162 EXPECT_TRUE(selection().selectWordAroundPosition(VisiblePosition(Position(te xt, 3))));
162 EXPECT_EQ_SELECTED_TEXT("Foo"); 163 EXPECT_EQ_SELECTED_TEXT("Foo");
163 // "Foo Bar | Baz," 164 // "Foo Bar | Baz,"
164 EXPECT_FALSE(selection().selectWordAroundPosition(VisiblePosition(Position(t ext, 13)))); 165 EXPECT_FALSE(selection().selectWordAroundPosition(VisiblePosition(Position(t ext, 13))));
165 // "Foo Bar Baz|," 166 // "Foo Bar Baz|,"
166 EXPECT_TRUE(selection().selectWordAroundPosition(VisiblePosition(Position(te xt, 22)))); 167 EXPECT_TRUE(selection().selectWordAroundPosition(VisiblePosition(Position(te xt, 22))));
167 EXPECT_EQ_SELECTED_TEXT("Baz"); 168 EXPECT_EQ_SELECTED_TEXT("Baz");
168 } 169 }
169 170
170 // Test for MoveRangeSelectionExtent with the default CharacterGranularityStrate gy
171 TEST_F(FrameSelectionTest, MoveRangeSelectionExtentCharacter)
172 {
173 // "Foo Bar Baz,"
174 RefPtrWillBeRawPtr<Text> text = appendTextNode("Foo Bar Baz,");
175 // "Foo B|a>r Baz," (| means start and > means end).
176 selection().setSelection(VisibleSelection(Position(text, 5), Position(text, 6)));
177 EXPECT_EQ_SELECTED_TEXT("a");
178 // "Foo B|ar B>az," with the Character granularity.
179 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 1)));
180 EXPECT_EQ_SELECTED_TEXT("oo B");
181 }
182
183 // Test for MoveRangeSelectionExtent with DirectionGranularityStrategy
184 TEST_F(FrameSelectionTest, MoveRangeSelectionExtentDirection)
185 {
186 RefPtrWillBeRawPtr<Text> text = document().createTextNode("abcdef ghij kl mn opqr stuvwx yzab,");
187 document().body()->appendChild(text);
188 dummyPageHolder().frame().settings()->setSelectionStrategy(SelectionStrategy ::Direction);
189
190 // "abcdef ghij kl mno|p>qr stuvwx yzab," (| means start and > means end).
191 selection().setSelection(VisibleSelection(Position(text, 18), Position(text, 19)));
192 EXPECT_EQ_SELECTED_TEXT("p");
193 // "abcdef ghij kl mno|pq>r stuvwx yzab," - expand selection using character granularity
194 // until the end of the word is reached.
195 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 20)));
196 EXPECT_EQ_SELECTED_TEXT("pq");
197 // "abcdef ghij kl mno|pqr> stuvwx yzab,"
198 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 21)));
199 EXPECT_EQ_SELECTED_TEXT("pqr");
200 // "abcdef ghij kl mno|pqr >stuvwx yzab," - confirm selection doesn't
201 // jump over the beginning of the word.
202 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 22)));
203 EXPECT_EQ_SELECTED_TEXT("pqr ");
204 // "abcdef ghij kl mno|pqr s>tuvwx yzab," - confirm selection switches to wo rd granularity.
205 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 23)));
206 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx");
207 // "abcdef ghij kl mno|pqr stu>vwx yzab," - selection stays the same.
208 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 25)));
209 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx");
210 // "abcdef ghij kl mno|pqr stuvwx yz>ab," - next word.
211 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 31)));
212 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx yzab");
213 // "abcdef ghij kl mno|pqr stuvwx y>zab," - move back one character -
214 // confirm switch to character granularity.
215 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 30)));
216 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx y");
217 // "abcdef ghij kl mno|pqr stuvwx yz>ab," - stay in character granularity
218 // if the user moves the position within the word.
219 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 31)));
220 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx yz");
221 // "abcdef ghij kl mno|pqr stuvwx >yzab,"
222 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 29)));
223 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx ");
224 // "abcdef ghij kl mno|pqr stuvwx >yzab," - it's possible to get a move when
225 // position doesn't change. It shouldn't affect anything.
226 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 29)));
227 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx ");
228 // "abcdef ghij kl mno|pqr stuvwx y>zab,"
229 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 30)));
230 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx y");
231 // "abcdef ghij kl mno|pqr stuv>wx yzab,"
232 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 26)));
233 EXPECT_EQ_SELECTED_TEXT("pqr stuv");
234 // "abcdef ghij kl mno|pqr stuvw>x yzab,"
235 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 27)));
236 EXPECT_EQ_SELECTED_TEXT("pqr stuvw");
237 // "abcdef ghij kl mno|pqr stuvwx y>zab," - switch to word granularity
238 // after expanding beyond the word boundary
239 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 30)));
240 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx yzab");
241 // "abcdef ghij kl mn<o|pqr stuvwx yzab," - over to the other side of the ba se
242 // - stay in character granularity until the beginning of the word is passed .
243 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 17)));
244 EXPECT_EQ_SELECTED_TEXT("o");
245 // "abcdef ghij kl m<no|pqr stuvwx yzab,"
246 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 16)));
247 EXPECT_EQ_SELECTED_TEXT("no");
248 // "abcdef ghij kl <mno|pqr stuvwx yzab,"
249 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 15)));
250 EXPECT_EQ_SELECTED_TEXT("mno");
251 // "abcdef ghij kl mn<o|pqr stuvwx yzab,"
252 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 17)));
253 EXPECT_EQ_SELECTED_TEXT("o");
254 // "abcdef ghij k<l mno|pqr stuvwx yzab," - switch to word granularity
255 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 13)));
256 EXPECT_EQ_SELECTED_TEXT("kl mno");
257 // "abcd<ef ghij kl mno|pqr stuvwx yzab,"
258 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 4)));
259 EXPECT_EQ_SELECTED_TEXT("abcdef ghij kl mno");
260 // "abcde<f ghij kl mno|pqr stuvwx yzab," - decrease selection -
261 // switch back to character granularity.
262 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 5)));
263 EXPECT_EQ_SELECTED_TEXT("f ghij kl mno");
264 // "abcdef ghij kl mn<o|pqr stuvwx yzab,"
265 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 17)));
266 EXPECT_EQ_SELECTED_TEXT("o");
267 // "abcdef ghij kl m<no|pqr stuvwx yzab,"
268 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 16)));
269 EXPECT_EQ_SELECTED_TEXT("no");
270 // "abcdef ghij k<l mno|pqr stuvwx yzab,"
271 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 13)));
272 EXPECT_EQ_SELECTED_TEXT("kl mno");
273
274 // Make sure we switch to word granularity right away when starting on a
275 // word boundary and extending.
276 // "abcdef ghij kl |mnopqr >stuvwx yzab," (| means start and > means end).
277 selection().setSelection(VisibleSelection(Position(text, 15), Position(text, 22)));
278 EXPECT_EQ_SELECTED_TEXT("mnopqr ");
279 // "abcdef ghij kl |mnopqr s>tuvwx yzab,"
280 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 23)));
281 EXPECT_EQ_SELECTED_TEXT("mnopqr stuvwx");
282
283 // Make sure we start in character granularity when moving extent over to th e other
284 // side of the base.
285 // "abcdef| ghij> kl mnopqr stuvwx yzab," (| means start and > means end).
286 selection().setSelection(VisibleSelection(Position(text, 6), Position(text, 11)));
287 EXPECT_EQ_SELECTED_TEXT(" ghij");
288 // "abcde<f| ghij kl mnopqr stuvwx yzab,"
289 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 5)));
290 EXPECT_EQ_SELECTED_TEXT("f");
291
292 // Make sure we switch to word granularity when moving over to the other
293 // side of the base and then passing over the word boundary.
294 // "abcdef |ghij> kl mnopqr stuvwx yzab,"
295 selection().setSelection(VisibleSelection(Position(text, 7), Position(text, 11)));
296 EXPECT_EQ_SELECTED_TEXT("ghij");
297 // "abcdef< |ghij kl mnopqr stuvwx yzab,"
298 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 6)));
299 EXPECT_EQ_SELECTED_TEXT(" ");
300 // "abcde<f |ghij kl mnopqr stuvwx yzab,"
301 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 5)));
302 EXPECT_EQ_SELECTED_TEXT("abcdef ");
303 }
304
305 TEST_F(FrameSelectionTest, MoveRangeSelectionTest) 171 TEST_F(FrameSelectionTest, MoveRangeSelectionTest)
306 { 172 {
307 // "Foo Bar Baz," 173 // "Foo Bar Baz,"
308 RefPtrWillBeRawPtr<Text> text = appendTextNode("Foo Bar Baz,"); 174 RefPtrWillBeRawPtr<Text> text = appendTextNode("Foo Bar Baz,");
309 // Itinitializes with "Foo B|a>r Baz," (| means start and > means end). 175 // Itinitializes with "Foo B|a>r Baz," (| means start and > means end).
310 selection().setSelection(VisibleSelection(Position(text, 5), Position(text, 6))); 176 selection().setSelection(VisibleSelection(Position(text, 5), Position(text, 6)));
311 EXPECT_EQ_SELECTED_TEXT("a"); 177 EXPECT_EQ_SELECTED_TEXT("a");
312 178
313 // "Foo B|ar B>az," with the Character granularity. 179 // "Foo B|ar B>az," with the Character granularity.
314 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 9)), CharacterGranularity); 180 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 9)), CharacterGranularity);
315 EXPECT_EQ_SELECTED_TEXT("ar B"); 181 EXPECT_EQ_SELECTED_TEXT("ar B");
316 // "Foo B|ar B>az," with the Word granularity. 182 // "Foo B|ar B>az," with the Word granularity.
317 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 9)), WordGranularity); 183 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 9)), WordGranularity);
318 EXPECT_EQ_SELECTED_TEXT("Bar Baz"); 184 EXPECT_EQ_SELECTED_TEXT("Bar Baz");
319 // "Fo<o B|ar Baz," with the Character granularity. 185 // "Fo<o B|ar Baz," with the Character granularity.
320 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 2)), CharacterGranularity); 186 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 2)), CharacterGranularity);
321 EXPECT_EQ_SELECTED_TEXT("o B"); 187 EXPECT_EQ_SELECTED_TEXT("o B");
322 // "Fo<o B|ar Baz," with the Word granularity. 188 // "Fo<o B|ar Baz," with the Word granularity.
323 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 2)), WordGranularity); 189 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 2)), WordGranularity);
324 EXPECT_EQ_SELECTED_TEXT("Foo Bar"); 190 EXPECT_EQ_SELECTED_TEXT("Foo Bar");
325 } 191 }
326 } 192 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698