Revert of Revert of Update V8 to version 4.5.61. (patchset #1 id:1 of https://coderev...
[chromium-blink-merge.git] / components / test_runner / mock_spell_check.cc
blob883ca5e400f952a67043bbfa86a377b4b6c7946b
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.
5 #include "components/test_runner/mock_spell_check.h"
7 #include "base/logging.h"
8 #include "components/test_runner/test_common.h"
9 #include "third_party/WebKit/public/platform/WebCString.h"
11 namespace test_runner {
13 namespace {
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);
24 } // namespace
26 MockSpellCheck::MockSpellCheck() : initialized_(false) {
29 MockSpellCheck::~MockSpellCheck() {
32 bool MockSpellCheck::SpellCheckWord(const blink::WebString& text,
33 int* misspelled_offset,
34 int* misspelled_length) {
35 DCHECK(misspelled_offset);
36 DCHECK(misspelled_length);
38 // Initialize this spellchecker.
39 InitializeIfNeeded();
41 // Reset the result values as our spellchecker does.
42 *misspelled_offset = 0;
43 *misspelled_length = 0;
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;
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;
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()) ==
80 word_offset + word_length ||
81 IsNotASCIIAlpha(string_text[word_offset + word_length]))) {
82 *misspelled_offset = word_offset + skipped_length;
83 *misspelled_length = word_length;
84 break;
88 if (*misspelled_length > 0)
89 break;
91 base::string16::iterator last_char = std::find_if(
92 string_text.begin() + word_offset, string_text.end(), IsNotASCIIAlpha);
93 if (last_char == string_text.end())
94 word_length = static_cast<int>(string_text.length()) - word_offset;
95 else
96 word_length = std::distance(first_char, last_char);
98 DCHECK_LT(0, word_offset + word_length);
99 string_text = string_text.substr(word_offset + word_length);
100 skipped_length += word_offset + word_length;
103 return false;
106 bool MockSpellCheck::HasInCache(const blink::WebString& word) {
107 return word == blink::WebString::fromUTF8("Spell wellcome. Is it broken?") ||
108 word == blink::WebString::fromUTF8("Spell wellcome.\x007F");
111 bool MockSpellCheck::IsMultiWordMisspelling(
112 const blink::WebString& text,
113 std::vector<blink::WebTextCheckingResult>* results) {
114 if (text == blink::WebString::fromUTF8("Helllo wordl.")) {
115 results->push_back(blink::WebTextCheckingResult(
116 blink::WebTextDecorationTypeSpelling, 0, 6, blink::WebString("Hello")));
117 results->push_back(blink::WebTextCheckingResult(
118 blink::WebTextDecorationTypeSpelling, 7, 5, blink::WebString("world")));
119 return true;
121 return false;
124 void MockSpellCheck::FillSuggestionList(
125 const blink::WebString& word,
126 blink::WebVector<blink::WebString>* suggestions) {
127 if (word == blink::WebString::fromUTF8("wellcome"))
128 Append(suggestions, blink::WebString::fromUTF8("welcome"));
129 else if (word == blink::WebString::fromUTF8("upper case"))
130 Append(suggestions, blink::WebString::fromUTF8("uppercase"));
131 else if (word == blink::WebString::fromUTF8("Helllo"))
132 Append(suggestions, blink::WebString::fromUTF8("Hello"));
133 else if (word == blink::WebString::fromUTF8("wordl"))
134 Append(suggestions, blink::WebString::fromUTF8("world"));
137 bool MockSpellCheck::InitializeIfNeeded() {
138 // Exit if we have already initialized this object.
139 if (initialized_)
140 return false;
142 // Create a table that consists of misspelled words used in WebKit layout
143 // tests.
144 // Since WebKit layout tests don't have so many misspelled words as
145 // well-spelled words, it is easier to compare the given word with misspelled
146 // ones than to compare with well-spelled ones.
147 static const char* misspelled_words[] = {
148 // These words are known misspelled words in webkit tests.
149 // If there are other misspelled words in webkit tests, please add them in
150 // this array.
151 "foo", "Foo", "baz", "fo", "LibertyF",
152 "chello", "xxxtestxxx", "XXxxx", "Textx", "blockquoted",
153 "asd", "Lorem", "Nunc", "Curabitur", "eu",
154 "adlj", "adaasj", "sdklj", "jlkds", "jsaada",
155 "jlda", "zz", "contentEditable",
156 // The following words are used by unit tests.
157 "ifmmp", "qwertyuiopasd", "qwertyuiopasdf", "upper case", "wellcome"};
159 misspelled_words_.clear();
160 for (size_t i = 0; i < arraysize(misspelled_words); ++i)
161 misspelled_words_.push_back(
162 base::string16(misspelled_words[i],
163 misspelled_words[i] + strlen(misspelled_words[i])));
165 // Mark as initialized to prevent this object from being initialized twice
166 // or more.
167 initialized_ = true;
169 // Since this MockSpellCheck class doesn't download dictionaries, this
170 // function always returns false.
171 return false;
174 } // namespace test_runner