Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: content/shell/renderer/test_runner/mock_spell_check.cc

Issue 410283003: test_runner: Migrate MockSpellCheck to Chromium C++ style. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: variable names corrected Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/mock_spell_check.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 namespace content {
12
13 namespace {
14
15 void Append(blink::WebVector<blink::WebString>* data,
16 const blink::WebString& item) {
17 blink::WebVector<blink::WebString> result(data->size() + 1);
18 for (size_t i = 0; i < data->size(); ++i)
19 result[i] = (*data)[i];
20 result[data->size()] = item;
21 data->swap(result);
22 }
23
24 } // namespace
25
26 MockSpellCheck::MockSpellCheck() : initialized_(false) {
27 }
28
29 MockSpellCheck::~MockSpellCheck() {
30 }
31
32 bool MockSpellCheck::SpellCheckWord(const blink::WebString& text,
33 int* misspelled_offset,
34 int* misspelled_length) {
35 DCHECK(misspelled_offset);
36 DCHECK(misspelled_length);
37
38 // Initialize this spellchecker.
39 InitializeIfNeeded();
40
41 // Reset the result values as our spellchecker does.
42 *misspelled_offset = 0;
43 *misspelled_length = 0;
44
45 // Convert to a base::string16 because we store base::string16 instances in
46 // misspelled_words_ and blink::WebString has no find().
47 base::string16 string_text = text;
48 int skipped_length = 0;
49
50 while (!string_text.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 treat
57 // the string as valid one.
58 base::string16::iterator first_char =
59 std::find_if(string_text.begin(), string_text.end(), isASCIIAlpha);
60 if (first_char == string_text.end())
61 return true;
62 int word_offset = std::distance(string_text.begin(), first_char);
63 int max_word_length = static_cast<int>(string_text.length()) - word_offset;
64 int word_length;
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 < misspelled_words_.size(); ++i) {
73 word_length =
74 static_cast<int>(misspelled_words_.at(i).length()) > max_word_length
75 ? max_word_length
76 : static_cast<int>(misspelled_words_.at(i).length());
77 word = string_text.substr(word_offset, word_length);
78 if (word == misspelled_words_.at(i) &&
79 (static_cast<int>(string_text.length()) == word_offset + word_length | |
80 isNotASCIIAlpha(string_text[word_offset + word_length]))) {
81 *misspelled_offset = word_offset + skipped_length;
82 *misspelled_length = word_length;
83 break;
84 }
85 }
86
87 if (*misspelled_length > 0)
88 break;
89
90 base::string16::iterator last_char = std::find_if(
91 string_text.begin() + word_offset, string_text.end(), isNotASCIIAlpha);
92 if (last_char == string_text.end())
93 word_length = static_cast<int>(string_text.length()) - word_offset;
94 else
95 word_length = std::distance(first_char, last_char);
96
97 DCHECK_LT(0, word_offset + word_length);
98 string_text = string_text.substr(word_offset + word_length);
99 skipped_length += word_offset + word_length;
100 }
101
102 return false;
103 }
104
105 bool MockSpellCheck::HasInCache(const blink::WebString& word) {
106 return word == blink::WebString::fromUTF8("Spell wellcome. Is it broken?") ||
107 word == blink::WebString::fromUTF8("Spell wellcome.\x007F");
108 }
109
110 bool MockSpellCheck::IsMultiWordMisspelling(
111 const blink::WebString& text,
112 std::vector<blink::WebTextCheckingResult>* results) {
113 if (text == blink::WebString::fromUTF8("Helllo wordl.")) {
114 results->push_back(blink::WebTextCheckingResult(
115 blink::WebTextDecorationTypeSpelling, 0, 6, blink::WebString("Hello")));
116 results->push_back(blink::WebTextCheckingResult(
117 blink::WebTextDecorationTypeSpelling, 7, 5, blink::WebString("world")));
118 return true;
119 }
120 return false;
121 }
122
123 void MockSpellCheck::FillSuggestionList(
124 const blink::WebString& word,
125 blink::WebVector<blink::WebString>* suggestions) {
126 if (word == blink::WebString::fromUTF8("wellcome"))
127 Append(suggestions, blink::WebString::fromUTF8("welcome"));
128 else if (word == blink::WebString::fromUTF8("upper case"))
129 Append(suggestions, blink::WebString::fromUTF8("uppercase"));
130 else if (word == blink::WebString::fromUTF8("Helllo"))
131 Append(suggestions, blink::WebString::fromUTF8("Hello"));
132 else if (word == blink::WebString::fromUTF8("wordl"))
133 Append(suggestions, blink::WebString::fromUTF8("world"));
134 }
135
136 bool MockSpellCheck::InitializeIfNeeded() {
137 // Exit if we have already initialized this object.
138 if (initialized_)
139 return false;
140
141 // Create a table that consists of misspelled words used in WebKit layout
142 // tests.
143 // Since WebKit layout tests don't have so many misspelled words as
144 // well-spelled words, it is easier to compare the given word with misspelled
145 // ones than to compare with well-spelled ones.
146 static const char* misspelled_words[] = {
147 // These words are known misspelled words in webkit tests.
148 // If there are other misspelled words in webkit tests, please add them in
149 // this array.
150 "foo", "Foo", "baz", "fo", "LibertyF",
151 "chello", "xxxtestxxx", "XXxxx", "Textx", "blockquoted",
152 "asd", "Lorem", "Nunc", "Curabitur", "eu",
153 "adlj", "adaasj", "sdklj", "jlkds", "jsaada",
154 "jlda", "zz", "contentEditable",
155 // The following words are used by unit tests.
156 "ifmmp", "qwertyuiopasd", "qwertyuiopasdf", "upper case", "wellcome"};
157
158 misspelled_words_.clear();
159 for (size_t i = 0; i < arraysize(misspelled_words); ++i)
160 misspelled_words_.push_back(base::string16(
161 misspelled_words[i], misspelled_words[i] + strlen(misspelled_words[i]))) ;
162
163 // Mark as initialized to prevent this object from being initialized twice
164 // or more.
165 initialized_ = true;
166
167 // Since this MockSpellCheck class doesn't download dictionaries, this
168 // function always returns false.
169 return false;
170 }
171
172 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698