OLD | NEW |
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 Loading... |
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 default CharacterGranularityStrate
gy |
| 164 TEST_F(FrameSelectionTest, MoveRangeSelectionExtentCharacter) |
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 >yzab," - it's possible to get a move when |
| 219 // position doesn't change. It shouldn't affect anything. |
| 220 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 29))); |
| 221 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx "); |
| 222 // "abcdef ghij kl mno|pqr stuvwx y>zab," |
| 223 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 30))); |
| 224 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx y"); |
| 225 // "abcdef ghij kl mno|pqr stuv>wx yzab," |
| 226 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 26))); |
| 227 EXPECT_EQ_SELECTED_TEXT("pqr stuv"); |
| 228 // "abcdef ghij kl mno|pqr stuvw>x yzab," |
| 229 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 27))); |
| 230 EXPECT_EQ_SELECTED_TEXT("pqr stuvw"); |
| 231 // "abcdef ghij kl mno|pqr stuvwx y>zab," - switch to word granularity |
| 232 // after expanding beyond the word boundary |
| 233 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 30))); |
| 234 EXPECT_EQ_SELECTED_TEXT("pqr stuvwx yzab"); |
| 235 // "abcdef ghij kl mn<o|pqr stuvwx yzab," - over to the other side of the ba
se |
| 236 // - stay in character granularity until the beginning of the word is passed
. |
| 237 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 17))); |
| 238 EXPECT_EQ_SELECTED_TEXT("o"); |
| 239 // "abcdef ghij kl m<no|pqr stuvwx yzab," |
| 240 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 16))); |
| 241 EXPECT_EQ_SELECTED_TEXT("no"); |
| 242 // "abcdef ghij kl <mno|pqr stuvwx yzab," |
| 243 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 15))); |
| 244 EXPECT_EQ_SELECTED_TEXT("mno"); |
| 245 // "abcdef ghij kl mn<o|pqr stuvwx yzab," |
| 246 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 17))); |
| 247 EXPECT_EQ_SELECTED_TEXT("o"); |
| 248 // "abcdef ghij k<l mno|pqr stuvwx yzab," - switch to word granularity |
| 249 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 13))); |
| 250 EXPECT_EQ_SELECTED_TEXT("kl mno"); |
| 251 // "abcd<ef ghij kl mno|pqr stuvwx yzab," |
| 252 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 4))); |
| 253 EXPECT_EQ_SELECTED_TEXT("abcdef ghij kl mno"); |
| 254 // "abcde<f ghij kl mno|pqr stuvwx yzab," - decrease selection - |
| 255 // switch back to character granularity. |
| 256 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 5))); |
| 257 EXPECT_EQ_SELECTED_TEXT("f ghij kl mno"); |
| 258 // "abcdef ghij kl mn<o|pqr stuvwx yzab," |
| 259 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 17))); |
| 260 EXPECT_EQ_SELECTED_TEXT("o"); |
| 261 // "abcdef ghij kl m<no|pqr stuvwx yzab," |
| 262 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 16))); |
| 263 EXPECT_EQ_SELECTED_TEXT("no"); |
| 264 // "abcdef ghij k<l mno|pqr stuvwx yzab," |
| 265 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 13))); |
| 266 EXPECT_EQ_SELECTED_TEXT("kl mno"); |
| 267 |
| 268 // Make sure we switch to word granularity right away when starting on a |
| 269 // word boundary and extending. |
| 270 // "abcdef ghij kl |mnopqr >stuvwx yzab," (| means start and > means end). |
| 271 selection().setSelection(VisibleSelection(Position(text, 15), Position(text,
22))); |
| 272 EXPECT_EQ_SELECTED_TEXT("mnopqr "); |
| 273 // "abcdef ghij kl |mnopqr s>tuvwx yzab," |
| 274 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 23))); |
| 275 EXPECT_EQ_SELECTED_TEXT("mnopqr stuvwx"); |
| 276 |
| 277 // Make sure we start in character granularity when moving extent over to th
e other |
| 278 // side of the base. |
| 279 // "abcdef| ghij> kl mnopqr stuvwx yzab," (| means start and > means end). |
| 280 selection().setSelection(VisibleSelection(Position(text, 6), Position(text,
11))); |
| 281 EXPECT_EQ_SELECTED_TEXT(" ghij"); |
| 282 // "abcde<f| ghij kl mnopqr stuvwx yzab," |
| 283 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 5))); |
| 284 EXPECT_EQ_SELECTED_TEXT("f"); |
| 285 |
| 286 // Make sure we switch to word granularity when moving over to the other |
| 287 // side of the base and then passing over the word boundary. |
| 288 // "abcdef |ghij> kl mnopqr stuvwx yzab," |
| 289 selection().setSelection(VisibleSelection(Position(text, 7), Position(text,
11))); |
| 290 EXPECT_EQ_SELECTED_TEXT("ghij"); |
| 291 // "abcdef< |ghij kl mnopqr stuvwx yzab," |
| 292 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 6))); |
| 293 EXPECT_EQ_SELECTED_TEXT(" "); |
| 294 // "abcde<f |ghij kl mnopqr stuvwx yzab," |
| 295 selection().moveRangeSelectionExtent(VisiblePosition(Position(text, 5))); |
| 296 EXPECT_EQ_SELECTED_TEXT("abcdef "); |
182 } | 297 } |
183 | 298 |
184 TEST_F(FrameSelectionTest, MoveRangeSelectionTest) | 299 TEST_F(FrameSelectionTest, MoveRangeSelectionTest) |
185 { | 300 { |
186 // "Foo Bar Baz," | 301 // "Foo Bar Baz," |
187 RefPtrWillBeRawPtr<Text> text = document().createTextNode("Foo Bar Baz,"); | 302 RefPtrWillBeRawPtr<Text> text = document().createTextNode("Foo Bar Baz,"); |
188 document().body()->appendChild(text); | 303 document().body()->appendChild(text); |
189 // Itinitializes with "Foo B|a>r Baz," (| means start and > means end). | 304 // Itinitializes with "Foo B|a>r Baz," (| means start and > means end). |
190 selection().setSelection(VisibleSelection(Position(text, 5), Position(text,
6))); | 305 selection().setSelection(VisibleSelection(Position(text, 5), Position(text,
6))); |
191 EXPECT_EQ_SELECTED_TEXT("a"); | 306 EXPECT_EQ_SELECTED_TEXT("a"); |
192 | 307 |
193 // "Foo B|ar B>az," with the Character granularity. | 308 // "Foo B|ar B>az," with the Character granularity. |
194 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo
sition(Position(text, 9)), CharacterGranularity); | 309 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo
sition(Position(text, 9)), CharacterGranularity); |
195 EXPECT_EQ_SELECTED_TEXT("ar B"); | 310 EXPECT_EQ_SELECTED_TEXT("ar B"); |
196 // "Foo B|ar B>az," with the Word granularity. | 311 // "Foo B|ar B>az," with the Word granularity. |
197 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo
sition(Position(text, 9)), WordGranularity); | 312 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo
sition(Position(text, 9)), WordGranularity); |
198 EXPECT_EQ_SELECTED_TEXT("Bar Baz"); | 313 EXPECT_EQ_SELECTED_TEXT("Bar Baz"); |
199 // "Fo<o B|ar Baz," with the Character granularity. | 314 // "Fo<o B|ar Baz," with the Character granularity. |
200 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo
sition(Position(text, 2)), CharacterGranularity); | 315 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo
sition(Position(text, 2)), CharacterGranularity); |
201 EXPECT_EQ_SELECTED_TEXT("o B"); | 316 EXPECT_EQ_SELECTED_TEXT("o B"); |
202 // "Fo<o B|ar Baz," with the Word granularity. | 317 // "Fo<o B|ar Baz," with the Word granularity. |
203 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo
sition(Position(text, 2)), WordGranularity); | 318 selection().moveRangeSelection(VisiblePosition(Position(text, 5)), VisiblePo
sition(Position(text, 2)), WordGranularity); |
204 EXPECT_EQ_SELECTED_TEXT("Foo Bar"); | 319 EXPECT_EQ_SELECTED_TEXT("Foo Bar"); |
205 } | 320 } |
206 | |
207 } | 321 } |
OLD | NEW |