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

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: Removing anonymous namespace and marking functions static instead. 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
« no previous file with comments | « Source/core/editing/FrameSelection.cpp ('k') | Source/core/editing/GranularityStrategy.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 // "Foo| Bar Baz," 159 // "Foo| Bar Baz,"
161 EXPECT_TRUE(selection().selectWordAroundPosition(VisiblePosition(Position(te xt, 3)))); 160 EXPECT_TRUE(selection().selectWordAroundPosition(VisiblePosition(Position(te xt, 3))));
162 EXPECT_EQ_SELECTED_TEXT("Foo"); 161 EXPECT_EQ_SELECTED_TEXT("Foo");
163 // "Foo Bar | Baz," 162 // "Foo Bar | Baz,"
164 EXPECT_FALSE(selection().selectWordAroundPosition(VisiblePosition(Position(t ext, 13)))); 163 EXPECT_FALSE(selection().selectWordAroundPosition(VisiblePosition(Position(t ext, 13))));
165 // "Foo Bar Baz|," 164 // "Foo Bar Baz|,"
166 EXPECT_TRUE(selection().selectWordAroundPosition(VisiblePosition(Position(te xt, 22)))); 165 EXPECT_TRUE(selection().selectWordAroundPosition(VisiblePosition(Position(te xt, 22))));
167 EXPECT_EQ_SELECTED_TEXT("Baz"); 166 EXPECT_EQ_SELECTED_TEXT("Baz");
168 } 167 }
169 168
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) 169 TEST_F(FrameSelectionTest, MoveRangeSelectionTest)
306 { 170 {
307 // "Foo Bar Baz," 171 // "Foo Bar Baz,"
308 RefPtrWillBeRawPtr<Text> text = appendTextNode("Foo Bar Baz,"); 172 RefPtrWillBeRawPtr<Text> text = appendTextNode("Foo Bar Baz,");
309 // Itinitializes with "Foo B|a>r Baz," (| means start and > means end). 173 // Itinitializes with "Foo B|a>r Baz," (| means start and > means end).
310 selection().setSelection(VisibleSelection(Position(text, 5), Position(text, 6))); 174 selection().setSelection(VisibleSelection(Position(text, 5), Position(text, 6)));
311 EXPECT_EQ_SELECTED_TEXT("a"); 175 EXPECT_EQ_SELECTED_TEXT("a");
312 176
313 // "Foo B|ar B>az," with the Character granularity. 177 // "Foo B|ar B>az," with the Character granularity.
314 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 9)), CharacterGranularity); 178 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 9)), CharacterGranularity);
315 EXPECT_EQ_SELECTED_TEXT("ar B"); 179 EXPECT_EQ_SELECTED_TEXT("ar B");
316 // "Foo B|ar B>az," with the Word granularity. 180 // "Foo B|ar B>az," with the Word granularity.
317 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 9)), WordGranularity); 181 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 9)), WordGranularity);
318 EXPECT_EQ_SELECTED_TEXT("Bar Baz"); 182 EXPECT_EQ_SELECTED_TEXT("Bar Baz");
319 // "Fo<o B|ar Baz," with the Character granularity. 183 // "Fo<o B|ar Baz," with the Character granularity.
320 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 2)), CharacterGranularity); 184 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 2)), CharacterGranularity);
321 EXPECT_EQ_SELECTED_TEXT("o B"); 185 EXPECT_EQ_SELECTED_TEXT("o B");
322 // "Fo<o B|ar Baz," with the Word granularity. 186 // "Fo<o B|ar Baz," with the Word granularity.
323 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 2)), WordGranularity); 187 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 2)), WordGranularity);
324 EXPECT_EQ_SELECTED_TEXT("Foo Bar"); 188 EXPECT_EQ_SELECTED_TEXT("Foo Bar");
325 } 189 }
326 } 190 }
OLDNEW
« no previous file with comments | « Source/core/editing/FrameSelection.cpp ('k') | Source/core/editing/GranularityStrategy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698