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

Side by Side Diff: content/test/layout_tests/runner/MockSpellCheck.cpp

Issue 110533009: Import TestRunner library into chromium. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: updates Created 7 years 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 | Annotate | Revision Log
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 /*
6 * Copyright (C) 2010 Google Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 * * Neither the name of Google Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include "content/test/layout_tests/runner/MockSpellCheck.h"
36
37 #include "content/test/layout_tests/runner/TestCommon.h"
38 #include "third_party/WebKit/public/platform/WebCString.h"
39
40 using namespace blink;
41 using namespace std;
42
43 namespace WebTestRunner {
44
45 namespace {
46
47 void append(WebVector<WebString>* data, const WebString& item)
48 {
49 WebVector<WebString> result(data->size() + 1);
50 for (size_t i = 0; i < data->size(); ++i)
51 result[i] = (*data)[i];
52 result[data->size()] = item;
53 data->swap(result);
54 }
55
56 }
57
58 MockSpellCheck::MockSpellCheck()
59 : m_initialized(false) { }
60
61 MockSpellCheck::~MockSpellCheck() { }
62
63 bool MockSpellCheck::spellCheckWord(const WebString& text, int* misspelledOffset , int* misspelledLength)
64 {
65 BLINK_ASSERT(misspelledOffset);
66 BLINK_ASSERT(misspelledLength);
67
68 // Initialize this spellchecker.
69 initializeIfNeeded();
70
71 // Reset the result values as our spellchecker does.
72 *misspelledOffset = 0;
73 *misspelledLength = 0;
74
75 // Convert to a string16 because we store string16 instances in
76 // m_misspelledWords and WebString has no find().
77 string16 stringText = text;
78 int skippedLength = 0;
79
80 while (!stringText.empty()) {
81 // Extract the first possible English word from the given string.
82 // The given string may include non-ASCII characters or numbers. So, we
83 // should filter out such characters before start looking up our
84 // misspelled-word table.
85 // (This is a simple version of our SpellCheckWordIterator class.)
86 // If the given string doesn't include any ASCII characters, we can trea t the
87 // string as valid one.
88 string16::iterator firstChar = find_if(stringText.begin(), stringText.en d(), isASCIIAlpha);
89 if (firstChar == stringText.end())
90 return true;
91 int wordOffset = distance(stringText.begin(), firstChar);
92 int maxWordLength = static_cast<int>(stringText.length()) - wordOffset;
93 int wordLength;
94 string16 word;
95
96 // Look up our misspelled-word table to check if the extracted word is a
97 // known misspelled word, and return the offset and the length of the
98 // extracted word if this word is a known misspelled word.
99 // (See the comment in MockSpellCheck::initializeIfNeeded() why we use a
100 // misspelled-word table.)
101 for (size_t i = 0; i < m_misspelledWords.size(); ++i) {
102 wordLength = static_cast<int>(m_misspelledWords.at(i).length()) > ma xWordLength ? maxWordLength : static_cast<int>(m_misspelledWords.at(i).length()) ;
103 word = stringText.substr(wordOffset, wordLength);
104 if (word == m_misspelledWords.at(i) && (static_cast<int>(stringText. length()) == wordOffset + wordLength || isNotASCIIAlpha(stringText[wordOffset + wordLength]))) {
105 *misspelledOffset = wordOffset + skippedLength;
106 *misspelledLength = wordLength;
107 break;
108 }
109 }
110
111 if (*misspelledLength > 0)
112 break;
113
114 string16::iterator lastChar = find_if(stringText.begin() + wordOffset, s tringText.end(), isNotASCIIAlpha);
115 if (lastChar == stringText.end())
116 wordLength = static_cast<int>(stringText.length()) - wordOffset;
117 else
118 wordLength = distance(firstChar, lastChar);
119
120 BLINK_ASSERT(0 < wordOffset + wordLength);
121 stringText = stringText.substr(wordOffset + wordLength);
122 skippedLength += wordOffset + wordLength;
123 }
124
125 return false;
126 }
127
128 bool MockSpellCheck::hasInCache(const WebString& word)
129 {
130 return word == WebString::fromUTF8("Spell wellcome. Is it broken?") || word == WebString::fromUTF8("Spell wellcome.\x007F");
131 }
132
133 bool MockSpellCheck::isMultiWordMisspelling(const WebString& text, vector<WebTex tCheckingResult>* results)
134 {
135 if (text == WebString::fromUTF8("Helllo wordl.")) {
136 results->push_back(WebTextCheckingResult(WebTextDecorationTypeSpelling, 0, 6, WebString("Hello")));
137 results->push_back(WebTextCheckingResult(WebTextDecorationTypeSpelling, 7, 5, WebString("world")));
138 return true;
139 }
140 return false;
141 }
142
143 void MockSpellCheck::fillSuggestionList(const WebString& word, WebVector<WebStri ng>* suggestions)
144 {
145 if (word == WebString::fromUTF8("wellcome"))
146 append(suggestions, WebString::fromUTF8("welcome"));
147 else if (word == WebString::fromUTF8("upper case"))
148 append(suggestions, WebString::fromUTF8("uppercase"));
149 else if (word == WebString::fromUTF8("Helllo"))
150 append(suggestions, WebString::fromUTF8("Hello"));
151 else if (word == WebString::fromUTF8("wordl"))
152 append(suggestions, WebString::fromUTF8("world"));
153 }
154
155 bool MockSpellCheck::initializeIfNeeded()
156 {
157 // Exit if we have already initialized this object.
158 if (m_initialized)
159 return false;
160
161 // Create a table that consists of misspelled words used in WebKit layout
162 // tests.
163 // Since WebKit layout tests don't have so many misspelled words as
164 // well-spelled words, it is easier to compare the given word with misspelle d
165 // ones than to compare with well-spelled ones.
166 static const char* misspelledWords[] = {
167 // These words are known misspelled words in webkit tests.
168 // If there are other misspelled words in webkit tests, please add them in
169 // this array.
170 "foo",
171 "Foo",
172 "baz",
173 "fo",
174 "LibertyF",
175 "chello",
176 "xxxtestxxx",
177 "XXxxx",
178 "Textx",
179 "blockquoted",
180 "asd",
181 "Lorem",
182 "Nunc",
183 "Curabitur",
184 "eu",
185 "adlj",
186 "adaasj",
187 "sdklj",
188 "jlkds",
189 "jsaada",
190 "jlda",
191 "zz",
192 "contentEditable",
193 // The following words are used by unit tests.
194 "ifmmp",
195 "qwertyuiopasd",
196 "qwertyuiopasdf",
197 "upper case",
198 "wellcome"
199 };
200
201 m_misspelledWords.clear();
202 for (size_t i = 0; i < arraysize(misspelledWords); ++i)
203 m_misspelledWords.push_back(string16(misspelledWords[i], misspelledWords [i] + strlen(misspelledWords[i])));
204
205 // Mark as initialized to prevent this object from being initialized twice
206 // or more.
207 m_initialized = true;
208
209 // Since this MockSpellCheck class doesn't download dictionaries, this
210 // function always returns false.
211 return false;
212 }
213
214 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698