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

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

Issue 988023005: Implementing directional selection strategy in Blink. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Addressing review feedback. Created 5 years, 9 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"
13 #include "core/html/HTMLBodyElement.h" 14 #include "core/html/HTMLBodyElement.h"
14 #include "core/html/HTMLDocument.h" 15 #include "core/html/HTMLDocument.h"
15 #include "core/testing/DummyPageHolder.h" 16 #include "core/testing/DummyPageHolder.h"
16 #include "wtf/OwnPtr.h" 17 #include "wtf/OwnPtr.h"
17 #include "wtf/PassRefPtr.h" 18 #include "wtf/PassRefPtr.h"
18 #include "wtf/RefPtr.h" 19 #include "wtf/RefPtr.h"
19 #include "wtf/StdLibExtras.h" 20 #include "wtf/StdLibExtras.h"
20 #include "wtf/testing/WTFTestHelpers.h" 21 #include "wtf/testing/WTFTestHelpers.h"
21 #include <gtest/gtest.h> 22 #include <gtest/gtest.h>
22 23
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 // "Foo| Bar Baz," 153 // "Foo| Bar Baz,"
153 EXPECT_TRUE(selection().selectWordAroundPosition(VisiblePosition(Position(te xt, 3)))); 154 EXPECT_TRUE(selection().selectWordAroundPosition(VisiblePosition(Position(te xt, 3))));
154 EXPECT_EQ_SELECTED_TEXT("Foo"); 155 EXPECT_EQ_SELECTED_TEXT("Foo");
155 // "Foo Bar | Baz," 156 // "Foo Bar | Baz,"
156 EXPECT_FALSE(selection().selectWordAroundPosition(VisiblePosition(Position(t ext, 13)))); 157 EXPECT_FALSE(selection().selectWordAroundPosition(VisiblePosition(Position(t ext, 13))));
157 // "Foo Bar Baz|," 158 // "Foo Bar Baz|,"
158 EXPECT_TRUE(selection().selectWordAroundPosition(VisiblePosition(Position(te xt, 22)))); 159 EXPECT_TRUE(selection().selectWordAroundPosition(VisiblePosition(Position(te xt, 22))));
159 EXPECT_EQ_SELECTED_TEXT("Baz"); 160 EXPECT_EQ_SELECTED_TEXT("Baz");
160 } 161 }
161 162
162 TEST_F(FrameSelectionTest, MoveRangeSelectionExtent) 163 // Test for MoveRangeSelectionExtent with the DefaultGranularityStrategy
164 TEST_F(FrameSelectionTest, MoveRangeSelectionExtentDefault)
163 { 165 {
164 // "Foo Bar Baz," 166 // "Foo Bar Baz,"
165 RefPtrWillBeRawPtr<Text> text = document().createTextNode("Foo Bar Baz,"); 167 RefPtrWillBeRawPtr<Text> text = document().createTextNode("Foo Bar Baz,");
166 document().body()->appendChild(text); 168 document().body()->appendChild(text);
167 // "Foo B|a>r Baz," (| means start and > means end). 169 // "Foo B|a>r Baz," (| means start and > means end).
168 selection().setSelection(VisibleSelection(Position(text, 5), Position(text, 6))); 170 selection().setSelection(VisibleSelection(Position(text, 5), Position(text, 6)));
169 EXPECT_EQ_SELECTED_TEXT("a"); 171 EXPECT_EQ_SELECTED_TEXT("a");
170 // "Foo B|ar B>az," with the Character granularity. 172 // "Foo B|ar B>az," with the Character granularity.
171 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 9)), Cha racterGranularity); 173 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 1)));
172 EXPECT_EQ_SELECTED_TEXT("ar B"); 174 EXPECT_EQ_SELECTED_TEXT("oo B");
173 // "Foo B|ar B>az," with the Word granularity. 175 }
174 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 9)), Wor dGranularity); 176
175 EXPECT_EQ_SELECTED_TEXT("ar Baz"); 177 // Test for MoveRangeSelectionExtent with DirectionGranularityStrategy
176 // "Fo<o B|ar Baz," with the Character granularity. 178 TEST_F(FrameSelectionTest, MoveRangeSelectionExtentDirection)
177 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 2)), Cha racterGranularity); 179 {
178 EXPECT_EQ_SELECTED_TEXT("o B"); 180 RefPtrWillBeRawPtr<Text> text = document().createTextNode("abcdef ghij kl mn opqr stuvwx yzab,");
179 // "Fo<o B|ar Baz," with the Word granularity. 181 document().body()->appendChild(text);
180 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 2)), Wor dGranularity); 182 dummyPageHolder().frame().settings()->setSelectionStrategy(StrategyDirection );
181 EXPECT_EQ_SELECTED_TEXT("Foo B"); 183
184 // "abcdef ghij kl mno|p>qr stuvwx yzab," (| means start and > means end).
185 selection().setSelection(VisibleSelection(Position(text, 18), Position(text, 19)));
186 EXPECT_EQ_SELECTED_TEXT("p");
187 // "abcdef ghij kl mno|pq>r stuvwx yzab," - expand selection using character granularity
188 // until the end of the word is reached.
189 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 20)));
190 EXPECT_EQ_SELECTED_TEXT("pq");
191 // "abcdef ghij kl mno|pqr> stuvwx yzab,"
192 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 21)));
193 EXPECT_EQ_SELECTED_TEXT("pqr");
194 // "abcdef ghij kl mno|pqr >stuvwx yzab," - confirm selection doesn't
195 // jump over the beginning of the word.
196 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 22)));
197 EXPECT_EQ_SELECTED_TEXT("pqr ");
198 // "abcdef ghij kl mno|pqr s>tuvwx yzab," - confirm selection switches to wo rd granularity.
199 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 23)));
200 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx");
201 // "abcdef ghij kl mno|pqr stu>vwx yzab," - selection stays the same.
202 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 25)));
203 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx");
204 // "abcdef ghij kl mno|pqr stuvwx yz>ab," - next word.
205 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 31)));
206 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx yzab");
207 // "abcdef ghij kl mno|pqr stuvwx y>zab," - move back one character -
208 // confirm switch to character granularity.
209 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 30)));
210 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx y");
211 // "abcdef ghij kl mno|pqr stuvwx yz>ab," - stay in character granularity
212 // if the user moves the position within the word.
213 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 31)));
214 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx yz");
215 // "abcdef ghij kl mno|pqr stuvwx >yzab,"
216 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 29)));
217 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx ");
218 // "abcdef ghij kl mno|pqr stuvwx y>zab,"
219 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 30)));
220 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx y");
221 // "abcdef ghij kl mno|pqr stuv>wx yzab,"
222 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 26)));
223 EXPECT_EQ_SELECTED_TEXT("pqr stuv");
224 // "abcdef ghij kl mno|pqr stuvw>x yzab,"
225 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 27)));
226 EXPECT_EQ_SELECTED_TEXT("pqr stuvw");
227 // "abcdef ghij kl mno|pqr stuvwx y>zab," - switch to word granularity
228 // after expanding beyond the word boundary
229 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 30)));
230 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx yzab");
231 // "abcdef ghij kl mn<o|pqr stuvwx yzab," - over to the other side of the ba se
232 // - stay in character granularity until the beginning of the word is passed .
233 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 17)));
234 EXPECT_EQ_SELECTED_TEXT("o");
235 // "abcdef ghij kl m<no|pqr stuvwx yzab,"
236 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 16)));
237 EXPECT_EQ_SELECTED_TEXT("no");
238 // "abcdef ghij kl <mno|pqr stuvwx yzab,"
239 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 15)));
240 EXPECT_EQ_SELECTED_TEXT("mno");
241 // "abcdef ghij kl mn<o|pqr stuvwx yzab,"
242 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 17)));
243 EXPECT_EQ_SELECTED_TEXT("o");
244 // "abcdef ghij k<l mno|pqr stuvwx yzab," - switch to word granularity
245 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 13)));
246 EXPECT_EQ_SELECTED_TEXT("kl mno");
247 // "abcd<ef ghij kl mno|pqr stuvwx yzab,"
248 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 4)));
249 EXPECT_EQ_SELECTED_TEXT("abcdef ghij kl mno");
250 // "abcde<f ghij kl mno|pqr stuvwx yzab," - decrease selection -
251 // switch back to character granularity.
252 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 5)));
253 EXPECT_EQ_SELECTED_TEXT("f ghij kl mno");
254 // "abcdef ghij kl mn<o|pqr stuvwx yzab,"
255 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 17)));
256 EXPECT_EQ_SELECTED_TEXT("o");
257 // "abcdef ghij kl m<no|pqr stuvwx yzab,"
258 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 16)));
259 EXPECT_EQ_SELECTED_TEXT("no");
260 // "abcdef ghij k<l mno|pqr stuvwx yzab,"
261 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 13)));
262 EXPECT_EQ_SELECTED_TEXT("kl mno");
263
264 // Make sure we switch to word granularity right away when starting on a
265 // word boundary and extending.
266 // "abcdef ghij kl |mnopqr >stuvwx yzab," (| means start and > means end).
267 selection().setSelection(VisibleSelection(Position(text, 15), Position(text, 22)));
268 EXPECT_EQ_SELECTED_TEXT("mnopqr ");
269 // "abcdef ghij kl |mnopqr s>tuvwx yzab,"
270 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 23)));
271 EXPECT_EQ_SELECTED_TEXT("mnopqr stuvwx");
272
273 // Make sure we start in character granularity when moving extent over to th e other
274 // side of the base.
275 // "abcdef| ghij> kl mnopqr stuvwx yzab," (| means start and > means end).
276 selection().setSelection(VisibleSelection(Position(text, 6), Position(text, 11)));
277 EXPECT_EQ_SELECTED_TEXT(" ghij");
278 // "abcde<f| ghij kl mnopqr stuvwx yzab,"
279 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 5)));
280 EXPECT_EQ_SELECTED_TEXT("f");
281
282 // Make sure we switch to word granularity when moving over to the other
283 // side of the base and then passing over the word boundary.
284 // "abcdef |ghij> kl mnopqr stuvwx yzab,"
285 selection().setSelection(VisibleSelection(Position(text, 7), Position(text, 11)));
286 EXPECT_EQ_SELECTED_TEXT("ghij");
287 // "abcdef< |ghij kl mnopqr stuvwx yzab,"
288 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 6)));
289 EXPECT_EQ_SELECTED_TEXT(" ");
290 // "abcde<f |ghij kl mnopqr stuvwx yzab,"
291 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 5)));
292 EXPECT_EQ_SELECTED_TEXT("abcdef ");
182 } 293 }
183 294
184 TEST_F(FrameSelectionTest, MoveRangeSelectionTest) 295 TEST_F(FrameSelectionTest, MoveRangeSelectionTest)
185 { 296 {
186 // "Foo Bar Baz," 297 // "Foo Bar Baz,"
187 RefPtrWillBeRawPtr<Text> text = document().createTextNode("Foo Bar Baz,"); 298 RefPtrWillBeRawPtr<Text> text = document().createTextNode("Foo Bar Baz,");
188 document().body()->appendChild(text); 299 document().body()->appendChild(text);
189 // Itinitializes with "Foo B|a>r Baz," (| means start and > means end). 300 // Itinitializes with "Foo B|a>r Baz," (| means start and > means end).
190 selection().setSelection(VisibleSelection(Position(text, 5), Position(text, 6))); 301 selection().setSelection(VisibleSelection(Position(text, 5), Position(text, 6)));
191 EXPECT_EQ_SELECTED_TEXT("a"); 302 EXPECT_EQ_SELECTED_TEXT("a");
192 303
193 // "Foo B|ar B>az," with the Character granularity. 304 // "Foo B|ar B>az," with the Character granularity.
194 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 9)), CharacterGranularity); 305 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 9)), CharacterGranularity);
195 EXPECT_EQ_SELECTED_TEXT("ar B"); 306 EXPECT_EQ_SELECTED_TEXT("ar B");
196 // "Foo B|ar B>az," with the Word granularity. 307 // "Foo B|ar B>az," with the Word granularity.
197 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 9)), WordGranularity); 308 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 9)), WordGranularity);
198 EXPECT_EQ_SELECTED_TEXT("Bar Baz"); 309 EXPECT_EQ_SELECTED_TEXT("Bar Baz");
199 // "Fo<o B|ar Baz," with the Character granularity. 310 // "Fo<o B|ar Baz," with the Character granularity.
200 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 2)), CharacterGranularity); 311 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 2)), CharacterGranularity);
201 EXPECT_EQ_SELECTED_TEXT("o B"); 312 EXPECT_EQ_SELECTED_TEXT("o B");
202 // "Fo<o B|ar Baz," with the Word granularity. 313 // "Fo<o B|ar Baz," with the Word granularity.
203 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 2)), WordGranularity); 314 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo sition(Position(text, 2)), WordGranularity);
204 EXPECT_EQ_SELECTED_TEXT("Foo Bar"); 315 EXPECT_EQ_SELECTED_TEXT("Foo Bar");
205 } 316 }
206
207 } 317 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698