| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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 "content/shell/renderer/test_runner/MockSpellCheck.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "content/shell/renderer/test_runner/TestCommon.h" | |
| 9 #include "third_party/WebKit/public/platform/WebCString.h" | |
| 10 | |
| 11 using namespace blink; | |
| 12 | |
| 13 namespace content { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 void append(WebVector<WebString>* data, const WebString& item) | |
| 18 { | |
| 19 WebVector<WebString> result(data->size() + 1); | |
| 20 for (size_t i = 0; i < data->size(); ++i) | |
| 21 result[i] = (*data)[i]; | |
| 22 result[data->size()] = item; | |
| 23 data->swap(result); | |
| 24 } | |
| 25 | |
| 26 } | |
| 27 | |
| 28 MockSpellCheck::MockSpellCheck() | |
| 29 : m_initialized(false) { } | |
| 30 | |
| 31 MockSpellCheck::~MockSpellCheck() { } | |
| 32 | |
| 33 bool MockSpellCheck::spellCheckWord(const WebString& text, int* misspelledOffset
, int* misspelledLength) | |
| 34 { | |
| 35 DCHECK(misspelledOffset); | |
| 36 DCHECK(misspelledLength); | |
| 37 | |
| 38 // Initialize this spellchecker. | |
| 39 initializeIfNeeded(); | |
| 40 | |
| 41 // Reset the result values as our spellchecker does. | |
| 42 *misspelledOffset = 0; | |
| 43 *misspelledLength = 0; | |
| 44 | |
| 45 // Convert to a base::string16 because we store base::string16 instances in | |
| 46 // m_misspelledWords and WebString has no find(). | |
| 47 base::string16 stringText = text; | |
| 48 int skippedLength = 0; | |
| 49 | |
| 50 while (!stringText.empty()) { | |
| 51 // Extract the first possible English word from the given string. | |
| 52 // The given string may include non-ASCII characters or numbers. So, we | |
| 53 // should filter out such characters before start looking up our | |
| 54 // misspelled-word table. | |
| 55 // (This is a simple version of our SpellCheckWordIterator class.) | |
| 56 // If the given string doesn't include any ASCII characters, we can trea
t the | |
| 57 // string as valid one. | |
| 58 base::string16::iterator firstChar = std::find_if(stringText.begin(), st
ringText.end(), isASCIIAlpha); | |
| 59 if (firstChar == stringText.end()) | |
| 60 return true; | |
| 61 int wordOffset = std::distance(stringText.begin(), firstChar); | |
| 62 int maxWordLength = static_cast<int>(stringText.length()) - wordOffset; | |
| 63 int wordLength; | |
| 64 base::string16 word; | |
| 65 | |
| 66 // Look up our misspelled-word table to check if the extracted word is a | |
| 67 // known misspelled word, and return the offset and the length of the | |
| 68 // extracted word if this word is a known misspelled word. | |
| 69 // (See the comment in MockSpellCheck::initializeIfNeeded() why we use a | |
| 70 // misspelled-word table.) | |
| 71 for (size_t i = 0; i < m_misspelledWords.size(); ++i) { | |
| 72 wordLength = static_cast<int>(m_misspelledWords.at(i).length()) > ma
xWordLength ? maxWordLength : static_cast<int>(m_misspelledWords.at(i).length())
; | |
| 73 word = stringText.substr(wordOffset, wordLength); | |
| 74 if (word == m_misspelledWords.at(i) && (static_cast<int>(stringText.
length()) == wordOffset + wordLength || isNotASCIIAlpha(stringText[wordOffset +
wordLength]))) { | |
| 75 *misspelledOffset = wordOffset + skippedLength; | |
| 76 *misspelledLength = wordLength; | |
| 77 break; | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 if (*misspelledLength > 0) | |
| 82 break; | |
| 83 | |
| 84 base::string16::iterator lastChar = std::find_if(stringText.begin() + wo
rdOffset, stringText.end(), isNotASCIIAlpha); | |
| 85 if (lastChar == stringText.end()) | |
| 86 wordLength = static_cast<int>(stringText.length()) - wordOffset; | |
| 87 else | |
| 88 wordLength = std::distance(firstChar, lastChar); | |
| 89 | |
| 90 DCHECK_LT(0, wordOffset + wordLength); | |
| 91 stringText = stringText.substr(wordOffset + wordLength); | |
| 92 skippedLength += wordOffset + wordLength; | |
| 93 } | |
| 94 | |
| 95 return false; | |
| 96 } | |
| 97 | |
| 98 bool MockSpellCheck::hasInCache(const WebString& word) | |
| 99 { | |
| 100 return word == WebString::fromUTF8("Spell wellcome. Is it broken?") || word
== WebString::fromUTF8("Spell wellcome.\x007F"); | |
| 101 } | |
| 102 | |
| 103 bool MockSpellCheck::isMultiWordMisspelling(const WebString& text, std::vector<W
ebTextCheckingResult>* results) | |
| 104 { | |
| 105 if (text == WebString::fromUTF8("Helllo wordl.")) { | |
| 106 results->push_back(WebTextCheckingResult(WebTextDecorationTypeSpelling,
0, 6, WebString("Hello"))); | |
| 107 results->push_back(WebTextCheckingResult(WebTextDecorationTypeSpelling,
7, 5, WebString("world"))); | |
| 108 return true; | |
| 109 } | |
| 110 return false; | |
| 111 } | |
| 112 | |
| 113 void MockSpellCheck::fillSuggestionList(const WebString& word, WebVector<WebStri
ng>* suggestions) | |
| 114 { | |
| 115 if (word == WebString::fromUTF8("wellcome")) | |
| 116 append(suggestions, WebString::fromUTF8("welcome")); | |
| 117 else if (word == WebString::fromUTF8("upper case")) | |
| 118 append(suggestions, WebString::fromUTF8("uppercase")); | |
| 119 else if (word == WebString::fromUTF8("Helllo")) | |
| 120 append(suggestions, WebString::fromUTF8("Hello")); | |
| 121 else if (word == WebString::fromUTF8("wordl")) | |
| 122 append(suggestions, WebString::fromUTF8("world")); | |
| 123 } | |
| 124 | |
| 125 bool MockSpellCheck::initializeIfNeeded() | |
| 126 { | |
| 127 // Exit if we have already initialized this object. | |
| 128 if (m_initialized) | |
| 129 return false; | |
| 130 | |
| 131 // Create a table that consists of misspelled words used in WebKit layout | |
| 132 // tests. | |
| 133 // Since WebKit layout tests don't have so many misspelled words as | |
| 134 // well-spelled words, it is easier to compare the given word with misspelle
d | |
| 135 // ones than to compare with well-spelled ones. | |
| 136 static const char* misspelledWords[] = { | |
| 137 // These words are known misspelled words in webkit tests. | |
| 138 // If there are other misspelled words in webkit tests, please add them
in | |
| 139 // this array. | |
| 140 "foo", | |
| 141 "Foo", | |
| 142 "baz", | |
| 143 "fo", | |
| 144 "LibertyF", | |
| 145 "chello", | |
| 146 "xxxtestxxx", | |
| 147 "XXxxx", | |
| 148 "Textx", | |
| 149 "blockquoted", | |
| 150 "asd", | |
| 151 "Lorem", | |
| 152 "Nunc", | |
| 153 "Curabitur", | |
| 154 "eu", | |
| 155 "adlj", | |
| 156 "adaasj", | |
| 157 "sdklj", | |
| 158 "jlkds", | |
| 159 "jsaada", | |
| 160 "jlda", | |
| 161 "zz", | |
| 162 "contentEditable", | |
| 163 // The following words are used by unit tests. | |
| 164 "ifmmp", | |
| 165 "qwertyuiopasd", | |
| 166 "qwertyuiopasdf", | |
| 167 "upper case", | |
| 168 "wellcome" | |
| 169 }; | |
| 170 | |
| 171 m_misspelledWords.clear(); | |
| 172 for (size_t i = 0; i < arraysize(misspelledWords); ++i) | |
| 173 m_misspelledWords.push_back(base::string16(misspelledWords[i], misspelle
dWords[i] + strlen(misspelledWords[i]))); | |
| 174 | |
| 175 // Mark as initialized to prevent this object from being initialized twice | |
| 176 // or more. | |
| 177 m_initialized = true; | |
| 178 | |
| 179 // Since this MockSpellCheck class doesn't download dictionaries, this | |
| 180 // function always returns false. | |
| 181 return false; | |
| 182 } | |
| 183 | |
| 184 } // namespace content | |
| OLD | NEW |