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

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: 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
« no previous file with comments | « content/shell/renderer/test_runner/mock_spell_check.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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* misspelledOffset,
jochen (gone - plz use gerrit) 2014/07/24 08:47:04 misspelled_offset
Abhishek 2014/07/24 10:08:39 Done.
34 int* misspelledLength) {
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 // misspelled_words_ and blink::WebString has no find().
47 base::string16 stringText = text;
jochen (gone - plz use gerrit) 2014/07/24 08:47:04 string_text (also all other variables)
Abhishek 2014/07/24 10:08:39 Done.
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 treat
57 // the string as valid one.
58 base::string16::iterator firstChar =
59 std::find_if(stringText.begin(), stringText.end(), isASCIIAlpha);
60 if (firstChar == stringText.end())
61 return true;
62 int wordOffset = std::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 < misspelled_words_.size(); ++i) {
73 wordLength =
74 static_cast<int>(misspelled_words_.at(i).length()) > maxWordLength
75 ? maxWordLength
76 : static_cast<int>(misspelled_words_.at(i).length());
77 word = stringText.substr(wordOffset, wordLength);
78 if (word == misspelled_words_.at(i) &&
79 (static_cast<int>(stringText.length()) == wordOffset + wordLength ||
80 isNotASCIIAlpha(stringText[wordOffset + wordLength]))) {
81 *misspelledOffset = wordOffset + skippedLength;
82 *misspelledLength = wordLength;
83 break;
84 }
85 }
86
87 if (*misspelledLength > 0)
88 break;
89
90 base::string16::iterator lastChar = std::find_if(
91 stringText.begin() + wordOffset, stringText.end(), isNotASCIIAlpha);
92 if (lastChar == stringText.end())
93 wordLength = static_cast<int>(stringText.length()) - wordOffset;
94 else
95 wordLength = std::distance(firstChar, lastChar);
96
97 DCHECK_LT(0, wordOffset + wordLength);
98 stringText = stringText.substr(wordOffset + wordLength);
99 skippedLength += wordOffset + wordLength;
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* misspelledWords[] = {
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(misspelledWords); ++i)
160 misspelled_words_.push_back(base::string16(
161 misspelledWords[i], misspelledWords[i] + strlen(misspelledWords[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
« no previous file with comments | « content/shell/renderer/test_runner/mock_spell_check.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698