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