OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/tools/test_shell/mock_spellcheck.h" | 5 #include "webkit/tools/test_shell/mock_spellcheck.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/string16.h" | 11 #include "base/string16.h.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 | 14 |
15 MockSpellCheck::MockSpellCheck() | 15 MockSpellCheck::MockSpellCheck() |
16 : initialized_(false) { | 16 : initialized_(false) { |
17 } | 17 } |
18 | 18 |
19 MockSpellCheck::~MockSpellCheck() { | 19 MockSpellCheck::~MockSpellCheck() { |
20 } | 20 } |
21 | 21 |
22 bool MockSpellCheck::SpellCheckWord(const string16& text, | 22 bool MockSpellCheck::SpellCheckWord(const base::string16& text, |
23 int* misspelledOffset, | 23 int* misspelledOffset, |
24 int* misspelledLength) { | 24 int* misspelledLength) { |
25 DCHECK(misspelledOffset && misspelledLength); | 25 DCHECK(misspelledOffset && misspelledLength); |
26 | 26 |
27 // Initialize this spellchecker. | 27 // Initialize this spellchecker. |
28 InitializeIfNeeded(); | 28 InitializeIfNeeded(); |
29 | 29 |
30 // Reset the result values as our spellchecker does. | 30 // Reset the result values as our spellchecker does. |
31 *misspelledOffset = 0; | 31 *misspelledOffset = 0; |
32 *misspelledLength = 0; | 32 *misspelledLength = 0; |
33 | 33 |
34 // Extract the first possible English word from the given string. | 34 // Extract the first possible English word from the given string. |
35 // The given string may include non-ASCII characters or numbers. So, we | 35 // The given string may include non-ASCII characters or numbers. So, we |
36 // should filter out such characters before start looking up our | 36 // should filter out such characters before start looking up our |
37 // misspelled-word table. | 37 // misspelled-word table. |
38 // (This is a simple version of our SpellCheckWordIterator class.) | 38 // (This is a simple version of our SpellCheckWordIterator class.) |
39 // If the given string doesn't include any ASCII characters, we can treat the | 39 // If the given string doesn't include any ASCII characters, we can treat the |
40 // string as valid one. | 40 // string as valid one. |
41 // Unfortunately, This implementation splits a contraction, i.e. "isn't" is | 41 // Unfortunately, This implementation splits a contraction, i.e. "isn't" is |
42 // split into two pieces "isn" and "t". This is OK because webkit tests | 42 // split into two pieces "isn" and "t". This is OK because webkit tests |
43 // don't have misspelled contractions. | 43 // don't have misspelled contractions. |
44 static const char* kWordCharacters = | 44 static const char* kWordCharacters = |
45 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | 45 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
46 string16 word_characters(ASCIIToUTF16(kWordCharacters)); | 46 base::string16 word_characters(ASCIIToUTF16(kWordCharacters)); |
47 | 47 |
48 size_t word_offset = text.find_first_of(word_characters); | 48 size_t word_offset = text.find_first_of(word_characters); |
49 if (word_offset == std::string::npos) | 49 if (word_offset == std::string::npos) |
50 return true; | 50 return true; |
51 | 51 |
52 size_t word_end = text.find_first_not_of(word_characters, word_offset); | 52 size_t word_end = text.find_first_not_of(word_characters, word_offset); |
53 size_t word_length = word_end == std::string::npos ? | 53 size_t word_length = word_end == std::string::npos ? |
54 text.length() - word_offset : word_end - word_offset; | 54 text.length() - word_offset : word_end - word_offset; |
55 | 55 |
56 // Look up our misspelled-word table to check if the extracted word is a | 56 // Look up our misspelled-word table to check if the extracted word is a |
57 // known misspelled word, and return the offset and the length of the | 57 // known misspelled word, and return the offset and the length of the |
58 // extracted word if this word is a known misspelled word. | 58 // extracted word if this word is a known misspelled word. |
59 // (See the comment in MockSpellCheck::InitializeIfNeeded() why we use a | 59 // (See the comment in MockSpellCheck::InitializeIfNeeded() why we use a |
60 // misspelled-word table.) | 60 // misspelled-word table.) |
61 string16 word(text, word_offset, word_length); | 61 base::string16 word(text, word_offset, word_length); |
62 std::map<string16, bool>::iterator it = misspelled_words_.find(word); | 62 std::map<base::string16, bool>::iterator it = misspelled_words_.find(word); |
63 if (it == misspelled_words_.end()) | 63 if (it == misspelled_words_.end()) |
64 return true; | 64 return true; |
65 | 65 |
66 *misspelledOffset = static_cast<int>(word_offset); | 66 *misspelledOffset = static_cast<int>(word_offset); |
67 *misspelledLength = static_cast<int>(word_length); | 67 *misspelledLength = static_cast<int>(word_length); |
68 return false; | 68 return false; |
69 } | 69 } |
70 | 70 |
71 bool MockSpellCheck::InitializeIfNeeded() { | 71 bool MockSpellCheck::InitializeIfNeeded() { |
72 // Exit if we have already initialized this object. | 72 // Exit if we have already initialized this object. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 "zz", | 106 "zz", |
107 "contentEditable", | 107 "contentEditable", |
108 // The following words are used by unit tests. | 108 // The following words are used by unit tests. |
109 "ifmmp", | 109 "ifmmp", |
110 "qwertyuiopasd", | 110 "qwertyuiopasd", |
111 "qwertyuiopasdf", | 111 "qwertyuiopasdf", |
112 }; | 112 }; |
113 | 113 |
114 misspelled_words_.clear(); | 114 misspelled_words_.clear(); |
115 for (size_t i = 0; i < arraysize(kMisspelledWords); ++i) { | 115 for (size_t i = 0; i < arraysize(kMisspelledWords); ++i) { |
116 misspelled_words_.insert(std::make_pair<string16, bool>( | 116 misspelled_words_.insert(std::make_pair<base::string16, bool>( |
117 ASCIIToUTF16(kMisspelledWords[i]), false)); | 117 ASCIIToUTF16(kMisspelledWords[i]), false)); |
118 } | 118 } |
119 | 119 |
120 // Mark as initialized to prevent this object from being initialized twice | 120 // Mark as initialized to prevent this object from being initialized twice |
121 // or more. | 121 // or more. |
122 initialized_ = true; | 122 initialized_ = true; |
123 | 123 |
124 // Since this MockSpellCheck class doesn't download dictionaries, this | 124 // Since this MockSpellCheck class doesn't download dictionaries, this |
125 // function always returns false. | 125 // function always returns false. |
126 return false; | 126 return false; |
127 } | 127 } |
128 | 128 |
129 void MockSpellCheck::FillSuggestions(const string16& word, | 129 void MockSpellCheck::FillSuggestions(const base::string16& word, |
130 std::vector<string16>* suggestions) { | 130 std::vector<base::string16>* suggestions) { |
131 if (word == ASCIIToUTF16("wellcome")) | 131 if (word == ASCIIToUTF16("wellcome")) |
132 suggestions->push_back(ASCIIToUTF16("welcome")); | 132 suggestions->push_back(ASCIIToUTF16("welcome")); |
133 } | 133 } |
OLD | NEW |