| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 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 "webkit/tools/test_shell/mock_spellcheck.h" | |
| 6 | |
| 7 #include <map> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "base/string16.h" | |
| 12 #include "base/string_util.h" | |
| 13 #include "base/utf_string_conversions.h" | |
| 14 | |
| 15 MockSpellCheck::MockSpellCheck() | |
| 16 : initialized_(false) { | |
| 17 } | |
| 18 | |
| 19 MockSpellCheck::~MockSpellCheck() { | |
| 20 } | |
| 21 | |
| 22 bool MockSpellCheck::SpellCheckWord(const base::string16& text, | |
| 23 int* misspelledOffset, | |
| 24 int* misspelledLength) { | |
| 25 DCHECK(misspelledOffset && misspelledLength); | |
| 26 | |
| 27 // Initialize this spellchecker. | |
| 28 InitializeIfNeeded(); | |
| 29 | |
| 30 // Reset the result values as our spellchecker does. | |
| 31 *misspelledOffset = 0; | |
| 32 *misspelledLength = 0; | |
| 33 | |
| 34 // Extract the first possible English word from the given string. | |
| 35 // The given string may include non-ASCII characters or numbers. So, we | |
| 36 // should filter out such characters before start looking up our | |
| 37 // misspelled-word table. | |
| 38 // (This is a simple version of our SpellCheckWordIterator class.) | |
| 39 // If the given string doesn't include any ASCII characters, we can treat the | |
| 40 // string as valid one. | |
| 41 // Unfortunately, This implementation splits a contraction, i.e. "isn't" is | |
| 42 // split into two pieces "isn" and "t". This is OK because webkit tests | |
| 43 // don't have misspelled contractions. | |
| 44 static const char* kWordCharacters = | |
| 45 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | |
| 46 base::string16 word_characters(ASCIIToUTF16(kWordCharacters)); | |
| 47 | |
| 48 size_t word_offset = text.find_first_of(word_characters); | |
| 49 if (word_offset == std::string::npos) | |
| 50 return true; | |
| 51 | |
| 52 size_t word_end = text.find_first_not_of(word_characters, word_offset); | |
| 53 size_t word_length = word_end == std::string::npos ? | |
| 54 text.length() - word_offset : word_end - word_offset; | |
| 55 | |
| 56 // Look up our misspelled-word table to check if the extracted word is a | |
| 57 // known misspelled word, and return the offset and the length of the | |
| 58 // extracted word if this word is a known misspelled word. | |
| 59 // (See the comment in MockSpellCheck::InitializeIfNeeded() why we use a | |
| 60 // misspelled-word table.) | |
| 61 base::string16 word(text, word_offset, word_length); | |
| 62 std::map<base::string16, bool>::iterator it = misspelled_words_.find(word); | |
| 63 if (it == misspelled_words_.end()) | |
| 64 return true; | |
| 65 | |
| 66 *misspelledOffset = static_cast<int>(word_offset); | |
| 67 *misspelledLength = static_cast<int>(word_length); | |
| 68 return false; | |
| 69 } | |
| 70 | |
| 71 bool MockSpellCheck::InitializeIfNeeded() { | |
| 72 // Exit if we have already initialized this object. | |
| 73 if (initialized_) | |
| 74 return false; | |
| 75 | |
| 76 // Create a table that consists of misspelled words used in WebKit layout | |
| 77 // tests. | |
| 78 // Since WebKit layout tests don't have so many misspelled words as | |
| 79 // well-spelled words, it is easier to compare the given word with misspelled | |
| 80 // ones than to compare with well-spelled ones. | |
| 81 static const char* kMisspelledWords[] = { | |
| 82 // These words are known misspelled words in webkit tests. | |
| 83 // If there are other misspelled words in webkit tests, please add them in | |
| 84 // this array. | |
| 85 "foo", | |
| 86 "Foo", | |
| 87 "baz", | |
| 88 "fo", | |
| 89 "LibertyF", | |
| 90 "chello", | |
| 91 "xxxtestxxx", | |
| 92 "XXxxx", | |
| 93 "Textx", | |
| 94 "blockquoted", | |
| 95 "asd", | |
| 96 "Lorem", | |
| 97 "Nunc", | |
| 98 "Curabitur", | |
| 99 "eu", | |
| 100 "adlj", | |
| 101 "adaasj", | |
| 102 "sdklj", | |
| 103 "jlkds", | |
| 104 "jsaada", | |
| 105 "jlda", | |
| 106 "zz", | |
| 107 "contentEditable", | |
| 108 // The following words are used by unit tests. | |
| 109 "ifmmp", | |
| 110 "qwertyuiopasd", | |
| 111 "qwertyuiopasdf", | |
| 112 }; | |
| 113 | |
| 114 misspelled_words_.clear(); | |
| 115 for (size_t i = 0; i < arraysize(kMisspelledWords); ++i) { | |
| 116 misspelled_words_.insert(std::make_pair<base::string16, bool>( | |
| 117 ASCIIToUTF16(kMisspelledWords[i]), false)); | |
| 118 } | |
| 119 | |
| 120 // Mark as initialized to prevent this object from being initialized twice | |
| 121 // or more. | |
| 122 initialized_ = true; | |
| 123 | |
| 124 // Since this MockSpellCheck class doesn't download dictionaries, this | |
| 125 // function always returns false. | |
| 126 return false; | |
| 127 } | |
| 128 | |
| 129 void MockSpellCheck::FillSuggestions(const base::string16& word, | |
| 130 std::vector<base::string16>* suggestions) { | |
| 131 if (word == ASCIIToUTF16("wellcome")) | |
| 132 suggestions->push_back(ASCIIToUTF16("welcome")); | |
| 133 } | |
| OLD | NEW |