| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 1 // Copyright 2017 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/suggestion/TextSuggestionController.h" | 
|  | 6 | 
|  | 7 #include "core/editing/EditingTestBase.h" | 
|  | 8 #include "core/editing/FrameSelection.h" | 
|  | 9 #include "core/editing/markers/DocumentMarkerController.h" | 
|  | 10 #include "core/editing/spellcheck/SpellChecker.h" | 
|  | 11 | 
|  | 12 namespace blink { | 
|  | 13 | 
|  | 14 class TextSuggestionControllerTest : public EditingTestBase {}; | 
|  | 15 | 
|  | 16 TEST_F(TextSuggestionControllerTest, ApplySpellCheckSuggestion) { | 
|  | 17   SetBodyContent( | 
|  | 18       "<div contenteditable>" | 
|  | 19       "spllchck" | 
|  | 20       "</div>"); | 
|  | 21   Element* div = GetDocument().QuerySelector("div"); | 
|  | 22   Node* text = div->firstChild(); | 
|  | 23 | 
|  | 24   GetDocument().Markers().AddSpellingMarker( | 
|  | 25       EphemeralRange(Position(text, 0), Position(text, 8))); | 
|  | 26   // Select immediately before misspelling | 
|  | 27   GetDocument().GetFrame()->Selection().SetSelection( | 
|  | 28       SelectionInDOMTree::Builder() | 
|  | 29           .SetBaseAndExtent(Position(text, 0), Position(text, 0)) | 
|  | 30           .Build()); | 
|  | 31   GetDocument() | 
|  | 32       .GetFrame() | 
|  | 33       ->GetTextSuggestionController() | 
|  | 34       .ApplySpellCheckSuggestion("spellcheck"); | 
|  | 35 | 
|  | 36   EXPECT_EQ("spellcheck", text->textContent()); | 
|  | 37 } | 
|  | 38 | 
|  | 39 TEST_F(TextSuggestionControllerTest, DeleteActiveSuggestionRange_DeleteAtEnd) { | 
|  | 40   SetBodyContent( | 
|  | 41       "<div contenteditable>" | 
|  | 42       "word1 word2" | 
|  | 43       "</div>"); | 
|  | 44   Element* div = GetDocument().QuerySelector("div"); | 
|  | 45   Node* text = div->firstChild(); | 
|  | 46 | 
|  | 47   // Mark "word2" as misspelled | 
|  | 48   GetDocument().Markers().AddSpellingMarker( | 
|  | 49       EphemeralRange(Position(text, 6), Position(text, 11))); | 
|  | 50   // Select immediately before word2 | 
|  | 51   GetDocument().GetFrame()->Selection().SetSelection( | 
|  | 52       SelectionInDOMTree::Builder() | 
|  | 53           .SetBaseAndExtent(Position(text, 6), Position(text, 6)) | 
|  | 54           .Build()); | 
|  | 55   GetDocument() | 
|  | 56       .GetFrame() | 
|  | 57       ->GetTextSuggestionController() | 
|  | 58       .DeleteActiveSuggestionRange(); | 
|  | 59 | 
|  | 60   EXPECT_EQ("word1\xA0", text->textContent()); | 
|  | 61 } | 
|  | 62 | 
|  | 63 TEST_F(TextSuggestionControllerTest, | 
|  | 64        DeleteActiveSuggestionRange_DeleteInMiddle) { | 
|  | 65   SetBodyContent( | 
|  | 66       "<div contenteditable>" | 
|  | 67       "word1 word2 word3" | 
|  | 68       "</div>"); | 
|  | 69   Element* div = GetDocument().QuerySelector("div"); | 
|  | 70   Node* text = div->firstChild(); | 
|  | 71 | 
|  | 72   // Mark "word2" as misspelled | 
|  | 73   GetDocument().Markers().AddSpellingMarker( | 
|  | 74       EphemeralRange(Position(text, 6), Position(text, 11))); | 
|  | 75   // Select immediately before word2 | 
|  | 76   GetDocument().GetFrame()->Selection().SetSelection( | 
|  | 77       SelectionInDOMTree::Builder() | 
|  | 78           .SetBaseAndExtent(Position(text, 6), Position(text, 6)) | 
|  | 79           .Build()); | 
|  | 80   GetDocument() | 
|  | 81       .GetFrame() | 
|  | 82       ->GetTextSuggestionController() | 
|  | 83       .DeleteActiveSuggestionRange(); | 
|  | 84 | 
|  | 85   // One of the extra spaces around "word2" should have been removed | 
|  | 86   EXPECT_EQ("word1\xA0word3", text->textContent()); | 
|  | 87 } | 
|  | 88 | 
|  | 89 TEST_F(TextSuggestionControllerTest, | 
|  | 90        DeleteActiveSuggestionRange_DeleteAtBeginningWithSpaceAfter) { | 
|  | 91   SetBodyContent( | 
|  | 92       "<div contenteditable>" | 
|  | 93       "word1 word2" | 
|  | 94       "</div>"); | 
|  | 95   Element* div = GetDocument().QuerySelector("div"); | 
|  | 96   Node* text = div->firstChild(); | 
|  | 97 | 
|  | 98   // Mark "word1" as misspelled | 
|  | 99   GetDocument().Markers().AddSpellingMarker( | 
|  | 100       EphemeralRange(Position(text, 0), Position(text, 5))); | 
|  | 101   // Select immediately before word1 | 
|  | 102   GetDocument().GetFrame()->Selection().SetSelection( | 
|  | 103       SelectionInDOMTree::Builder() | 
|  | 104           .SetBaseAndExtent(Position(text, 0), Position(text, 0)) | 
|  | 105           .Build()); | 
|  | 106   GetDocument() | 
|  | 107       .GetFrame() | 
|  | 108       ->GetTextSuggestionController() | 
|  | 109       .DeleteActiveSuggestionRange(); | 
|  | 110 | 
|  | 111   // The space after "word1" should have been removed (to avoid leaving an | 
|  | 112   // empty space at the beginning of the composition) | 
|  | 113   EXPECT_EQ("word2", text->textContent()); | 
|  | 114 } | 
|  | 115 | 
|  | 116 TEST_F(TextSuggestionControllerTest, | 
|  | 117        DeleteActiveSuggestionRange_DeleteEntireRange) { | 
|  | 118   SetBodyContent( | 
|  | 119       "<div contenteditable>" | 
|  | 120       "word1" | 
|  | 121       "</div>"); | 
|  | 122   Element* div = GetDocument().QuerySelector("div"); | 
|  | 123   Node* text = div->firstChild(); | 
|  | 124 | 
|  | 125   // Mark "word1" as misspelled | 
|  | 126   GetDocument().Markers().AddSpellingMarker( | 
|  | 127       EphemeralRange(Position(text, 0), Position(text, 5))); | 
|  | 128   // Select immediately before word1 | 
|  | 129   GetDocument().GetFrame()->Selection().SetSelection( | 
|  | 130       SelectionInDOMTree::Builder() | 
|  | 131           .SetBaseAndExtent(Position(text, 0), Position(text, 0)) | 
|  | 132           .Build()); | 
|  | 133   GetDocument() | 
|  | 134       .GetFrame() | 
|  | 135       ->GetTextSuggestionController() | 
|  | 136       .DeleteActiveSuggestionRange(); | 
|  | 137 | 
|  | 138   EXPECT_EQ("", text->textContent()); | 
|  | 139 } | 
|  | 140 | 
|  | 141 // The following two cases test situations that shouldn't occur in normal use | 
|  | 142 // (spellcheck markers not spanning a whole word), but are included anyway to | 
|  | 143 // verify that DeleteActiveSuggestionRange() is well-behaved in these cases | 
|  | 144 | 
|  | 145 TEST_F(TextSuggestionControllerTest, | 
|  | 146        DeleteActiveSuggestionRange_DeleteRangeWithTextBeforeAndSpaceAfter) { | 
|  | 147   SetBodyContent( | 
|  | 148       "<div contenteditable>" | 
|  | 149       "word1word2 word3" | 
|  | 150       "</div>"); | 
|  | 151   Element* div = GetDocument().QuerySelector("div"); | 
|  | 152   Node* text = div->firstChild(); | 
|  | 153 | 
|  | 154   // Mark "word2" as misspelled | 
|  | 155   GetDocument().Markers().AddSpellingMarker( | 
|  | 156       EphemeralRange(Position(text, 5), Position(text, 10))); | 
|  | 157   // Select immediately before word2 | 
|  | 158   GetDocument().GetFrame()->Selection().SetSelection( | 
|  | 159       SelectionInDOMTree::Builder() | 
|  | 160           .SetBaseAndExtent(Position(text, 5), Position(text, 5)) | 
|  | 161           .Build()); | 
|  | 162   GetDocument() | 
|  | 163       .GetFrame() | 
|  | 164       ->GetTextSuggestionController() | 
|  | 165       .DeleteActiveSuggestionRange(); | 
|  | 166 | 
|  | 167   EXPECT_EQ("word1\xA0word3", text->textContent()); | 
|  | 168 } | 
|  | 169 | 
|  | 170 TEST_F(TextSuggestionControllerTest, | 
|  | 171        DeleteActiveSuggestionRange_DeleteRangeWithSpaceBeforeAndTextAfter) { | 
|  | 172   SetBodyContent( | 
|  | 173       "<div contenteditable>" | 
|  | 174       "word1 word2word3" | 
|  | 175       "</div>"); | 
|  | 176   Element* div = GetDocument().QuerySelector("div"); | 
|  | 177   Node* text = div->firstChild(); | 
|  | 178 | 
|  | 179   // Mark "word2" as misspelled | 
|  | 180   GetDocument().Markers().AddSpellingMarker( | 
|  | 181       EphemeralRange(Position(text, 6), Position(text, 11))); | 
|  | 182   // Select immediately before word2 | 
|  | 183   GetDocument().GetFrame()->Selection().SetSelection( | 
|  | 184       SelectionInDOMTree::Builder() | 
|  | 185           .SetBaseAndExtent(Position(text, 6), Position(text, 6)) | 
|  | 186           .Build()); | 
|  | 187   GetDocument() | 
|  | 188       .GetFrame() | 
|  | 189       ->GetTextSuggestionController() | 
|  | 190       .DeleteActiveSuggestionRange(); | 
|  | 191 | 
|  | 192   EXPECT_EQ("word1\xA0word3", text->textContent()); | 
|  | 193 } | 
|  | 194 | 
|  | 195 TEST_F(TextSuggestionControllerTest, | 
|  | 196        DeleteActiveSuggestionRange_DeleteAtBeginningWithTextAfter) { | 
|  | 197   SetBodyContent( | 
|  | 198       "<div contenteditable>" | 
|  | 199       "word1word2" | 
|  | 200       "</div>"); | 
|  | 201   Element* div = GetDocument().QuerySelector("div"); | 
|  | 202   Node* text = div->firstChild(); | 
|  | 203 | 
|  | 204   // Mark "word1" as misspelled | 
|  | 205   GetDocument().Markers().AddSpellingMarker( | 
|  | 206       EphemeralRange(Position(text, 0), Position(text, 5))); | 
|  | 207   // Select immediately before word1 | 
|  | 208   GetDocument().GetFrame()->Selection().SetSelection( | 
|  | 209       SelectionInDOMTree::Builder() | 
|  | 210           .SetBaseAndExtent(Position(text, 0), Position(text, 0)) | 
|  | 211           .Build()); | 
|  | 212   GetDocument() | 
|  | 213       .GetFrame() | 
|  | 214       ->GetTextSuggestionController() | 
|  | 215       .DeleteActiveSuggestionRange(); | 
|  | 216 | 
|  | 217   EXPECT_EQ("word2", text->textContent()); | 
|  | 218 } | 
|  | 219 | 
|  | 220 TEST_F(TextSuggestionControllerTest, | 
|  | 221        DeleteActiveSuggestionRange_NewWordAddedToDictionary) { | 
|  | 222   SetBodyContent( | 
|  | 223       "<div contenteditable>" | 
|  | 224       "embiggen" | 
|  | 225       "</div>"); | 
|  | 226   Element* div = GetDocument().QuerySelector("div"); | 
|  | 227   Node* text = div->firstChild(); | 
|  | 228 | 
|  | 229   // Mark "embiggen" as misspelled | 
|  | 230   GetDocument().Markers().AddSpellingMarker( | 
|  | 231       EphemeralRange(Position(text, 0), Position(text, 8))); | 
|  | 232   // Select immediately before embiggen | 
|  | 233   GetDocument().GetFrame()->Selection().SetSelection( | 
|  | 234       SelectionInDOMTree::Builder() | 
|  | 235           .SetBaseAndExtent(Position(text, 0), Position(text, 0)) | 
|  | 236           .Build()); | 
|  | 237   // Add some other word to the dictionary | 
|  | 238   GetDocument() | 
|  | 239       .GetFrame() | 
|  | 240       ->GetTextSuggestionController() | 
|  | 241       .NewWordAddedToDictionary("cromulent"); | 
|  | 242   // Verify the spelling marker is still present | 
|  | 243   EXPECT_TRUE(GetDocument() | 
|  | 244                   .GetFrame() | 
|  | 245                   ->GetSpellChecker() | 
|  | 246                   .GetSpellCheckMarkerTouchingSelection()); | 
|  | 247 | 
|  | 248   // Add "embiggen" to the dictionary | 
|  | 249   GetDocument() | 
|  | 250       .GetFrame() | 
|  | 251       ->GetTextSuggestionController() | 
|  | 252       .NewWordAddedToDictionary("embiggen"); | 
|  | 253   // Verify the spelling marker is gone | 
|  | 254   EXPECT_FALSE(GetDocument() | 
|  | 255                    .GetFrame() | 
|  | 256                    ->GetSpellChecker() | 
|  | 257                    .GetSpellCheckMarkerTouchingSelection()); | 
|  | 258 } | 
|  | 259 | 
|  | 260 }  // namespace blink | 
| OLD | NEW | 
|---|