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