OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 // Unit tests for |Feedback| object. | 5 // Unit tests for |Feedback| object. |
6 | 6 |
7 #include "chrome/browser/spellchecker/feedback.h" | 7 #include "chrome/browser/spellchecker/feedback.h" |
8 | 8 |
| 9 #include <stddef.h> |
| 10 #include <stdint.h> |
| 11 |
9 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
11 | 14 |
12 using base::ASCIIToUTF16; | 15 using base::ASCIIToUTF16; |
13 | 16 |
14 namespace spellcheck { | 17 namespace spellcheck { |
15 | 18 |
16 namespace { | 19 namespace { |
17 | 20 |
18 // Maximum number of bytes of feedback that would be sent to the server at once. | 21 // Maximum number of bytes of feedback that would be sent to the server at once. |
19 const size_t kMaxFeedbackSize = 1024; | 22 const size_t kMaxFeedbackSize = 1024; |
20 | 23 |
21 // Identifier for a renderer process. | 24 // Identifier for a renderer process. |
22 const int kRendererProcessId = 7; | 25 const int kRendererProcessId = 7; |
23 | 26 |
24 // Hash identifier for a misspelling. | 27 // Hash identifier for a misspelling. |
25 const uint32 kMisspellingHash = 42; | 28 const uint32_t kMisspellingHash = 42; |
26 | 29 |
27 } // namespace | 30 } // namespace |
28 | 31 |
29 // A test fixture to help keep the tests simple. | 32 // A test fixture to help keep the tests simple. |
30 class FeedbackTest : public testing::Test { | 33 class FeedbackTest : public testing::Test { |
31 public: | 34 public: |
32 FeedbackTest() : feedback_(kMaxFeedbackSize) {} | 35 FeedbackTest() : feedback_(kMaxFeedbackSize) {} |
33 ~FeedbackTest() override {} | 36 ~FeedbackTest() override {} |
34 | 37 |
35 protected: | 38 protected: |
36 void AddMisspelling(int renderer_process_id, uint32 hash) { | 39 void AddMisspelling(int renderer_process_id, uint32_t hash) { |
37 feedback_.AddMisspelling(renderer_process_id, | 40 feedback_.AddMisspelling(renderer_process_id, |
38 Misspelling(base::string16(), 0, 0, | 41 Misspelling(base::string16(), 0, 0, |
39 std::vector<base::string16>(), hash)); | 42 std::vector<base::string16>(), hash)); |
40 } | 43 } |
41 | 44 |
42 spellcheck::Feedback feedback_; | 45 spellcheck::Feedback feedback_; |
43 }; | 46 }; |
44 | 47 |
45 TEST_F(FeedbackTest, LimitFeedbackSize) { | 48 TEST_F(FeedbackTest, LimitFeedbackSize) { |
46 // Adding too much feedback data should prevent adding more feedback. | 49 // Adding too much feedback data should prevent adding more feedback. |
(...skipping 23 matching lines...) Expand all Loading... |
70 feedback_.Clear(); | 73 feedback_.Clear(); |
71 | 74 |
72 // Erasing finalized misspellings should allow adding feedback again. | 75 // Erasing finalized misspellings should allow adding feedback again. |
73 feedback_.AddMisspelling( | 76 feedback_.AddMisspelling( |
74 kRendererProcessId, | 77 kRendererProcessId, |
75 Misspelling( | 78 Misspelling( |
76 base::ASCIIToUTF16("0123456789"), 0, 1, | 79 base::ASCIIToUTF16("0123456789"), 0, 1, |
77 std::vector<base::string16>(50, base::ASCIIToUTF16("9876543210")), | 80 std::vector<base::string16>(50, base::ASCIIToUTF16("9876543210")), |
78 0)); | 81 0)); |
79 feedback_.FinalizeRemovedMisspellings(kRendererProcessId, | 82 feedback_.FinalizeRemovedMisspellings(kRendererProcessId, |
80 std::vector<uint32>()); | 83 std::vector<uint32_t>()); |
81 feedback_.EraseFinalizedMisspellings(kRendererProcessId); | 84 feedback_.EraseFinalizedMisspellings(kRendererProcessId); |
82 feedback_.AddMisspelling( | 85 feedback_.AddMisspelling( |
83 kRendererProcessId, | 86 kRendererProcessId, |
84 Misspelling( | 87 Misspelling( |
85 base::ASCIIToUTF16("0123456789"), 0, 1, | 88 base::ASCIIToUTF16("0123456789"), 0, 1, |
86 std::vector<base::string16>(50, base::ASCIIToUTF16("9876543210")), | 89 std::vector<base::string16>(50, base::ASCIIToUTF16("9876543210")), |
87 kMisspellingHash)); | 90 kMisspellingHash)); |
88 EXPECT_NE(nullptr, feedback_.GetMisspelling(kMisspellingHash)); | 91 EXPECT_NE(nullptr, feedback_.GetMisspelling(kMisspellingHash)); |
89 } | 92 } |
90 | 93 |
91 // Should be able to retrieve misspelling after it's added. | 94 // Should be able to retrieve misspelling after it's added. |
92 TEST_F(FeedbackTest, RetreiveMisspelling) { | 95 TEST_F(FeedbackTest, RetreiveMisspelling) { |
93 EXPECT_EQ(nullptr, feedback_.GetMisspelling(kMisspellingHash)); | 96 EXPECT_EQ(nullptr, feedback_.GetMisspelling(kMisspellingHash)); |
94 AddMisspelling(kRendererProcessId, kMisspellingHash); | 97 AddMisspelling(kRendererProcessId, kMisspellingHash); |
95 Misspelling* result = feedback_.GetMisspelling(kMisspellingHash); | 98 Misspelling* result = feedback_.GetMisspelling(kMisspellingHash); |
96 EXPECT_NE(nullptr, result); | 99 EXPECT_NE(nullptr, result); |
97 EXPECT_EQ(kMisspellingHash, result->hash); | 100 EXPECT_EQ(kMisspellingHash, result->hash); |
98 } | 101 } |
99 | 102 |
100 // Removed misspellings should be finalized. | 103 // Removed misspellings should be finalized. |
101 TEST_F(FeedbackTest, FinalizeRemovedMisspellings) { | 104 TEST_F(FeedbackTest, FinalizeRemovedMisspellings) { |
102 static const int kRemovedMisspellingHash = 1; | 105 static const int kRemovedMisspellingHash = 1; |
103 static const int kRemainingMisspellingHash = 2; | 106 static const int kRemainingMisspellingHash = 2; |
104 AddMisspelling(kRendererProcessId, kRemovedMisspellingHash); | 107 AddMisspelling(kRendererProcessId, kRemovedMisspellingHash); |
105 AddMisspelling(kRendererProcessId, kRemainingMisspellingHash); | 108 AddMisspelling(kRendererProcessId, kRemainingMisspellingHash); |
106 std::vector<uint32> remaining_markers(1, kRemainingMisspellingHash); | 109 std::vector<uint32_t> remaining_markers(1, kRemainingMisspellingHash); |
107 feedback_.FinalizeRemovedMisspellings(kRendererProcessId, remaining_markers); | 110 feedback_.FinalizeRemovedMisspellings(kRendererProcessId, remaining_markers); |
108 Misspelling* removed_misspelling = | 111 Misspelling* removed_misspelling = |
109 feedback_.GetMisspelling(kRemovedMisspellingHash); | 112 feedback_.GetMisspelling(kRemovedMisspellingHash); |
110 EXPECT_NE(nullptr, removed_misspelling); | 113 EXPECT_NE(nullptr, removed_misspelling); |
111 EXPECT_TRUE(removed_misspelling->action.IsFinal()); | 114 EXPECT_TRUE(removed_misspelling->action.IsFinal()); |
112 Misspelling* remaining_misspelling = | 115 Misspelling* remaining_misspelling = |
113 feedback_.GetMisspelling(kRemainingMisspellingHash); | 116 feedback_.GetMisspelling(kRemainingMisspellingHash); |
114 EXPECT_NE(nullptr, remaining_misspelling); | 117 EXPECT_NE(nullptr, remaining_misspelling); |
115 EXPECT_FALSE(remaining_misspelling->action.IsFinal()); | 118 EXPECT_FALSE(remaining_misspelling->action.IsFinal()); |
116 } | 119 } |
117 | 120 |
118 // Duplicate misspellings should not be finalized. | 121 // Duplicate misspellings should not be finalized. |
119 TEST_F(FeedbackTest, DuplicateMisspellingFinalization) { | 122 TEST_F(FeedbackTest, DuplicateMisspellingFinalization) { |
120 AddMisspelling(kRendererProcessId, kMisspellingHash); | 123 AddMisspelling(kRendererProcessId, kMisspellingHash); |
121 AddMisspelling(kRendererProcessId, kMisspellingHash); | 124 AddMisspelling(kRendererProcessId, kMisspellingHash); |
122 std::vector<uint32> remaining_markers(1, kMisspellingHash); | 125 std::vector<uint32_t> remaining_markers(1, kMisspellingHash); |
123 feedback_.FinalizeRemovedMisspellings(kRendererProcessId, remaining_markers); | 126 feedback_.FinalizeRemovedMisspellings(kRendererProcessId, remaining_markers); |
124 std::vector<Misspelling> misspellings = feedback_.GetAllMisspellings(); | 127 std::vector<Misspelling> misspellings = feedback_.GetAllMisspellings(); |
125 EXPECT_EQ(static_cast<size_t>(1), misspellings.size()); | 128 EXPECT_EQ(static_cast<size_t>(1), misspellings.size()); |
126 EXPECT_FALSE(misspellings[0].action.IsFinal()); | 129 EXPECT_FALSE(misspellings[0].action.IsFinal()); |
127 } | 130 } |
128 | 131 |
129 // Misspellings should be associated with a renderer. | 132 // Misspellings should be associated with a renderer. |
130 TEST_F(FeedbackTest, RendererHasMisspellings) { | 133 TEST_F(FeedbackTest, RendererHasMisspellings) { |
131 EXPECT_FALSE(feedback_.RendererHasMisspellings(kRendererProcessId)); | 134 EXPECT_FALSE(feedback_.RendererHasMisspellings(kRendererProcessId)); |
132 AddMisspelling(kRendererProcessId, kMisspellingHash); | 135 AddMisspelling(kRendererProcessId, kMisspellingHash); |
133 EXPECT_TRUE(feedback_.RendererHasMisspellings(kRendererProcessId)); | 136 EXPECT_TRUE(feedback_.RendererHasMisspellings(kRendererProcessId)); |
134 } | 137 } |
135 | 138 |
136 // Should be able to retrieve misspellings in renderer. | 139 // Should be able to retrieve misspellings in renderer. |
137 TEST_F(FeedbackTest, GetMisspellingsInRenderer) { | 140 TEST_F(FeedbackTest, GetMisspellingsInRenderer) { |
138 AddMisspelling(kRendererProcessId, kMisspellingHash); | 141 AddMisspelling(kRendererProcessId, kMisspellingHash); |
139 const std::vector<Misspelling>& renderer_with_misspellings = | 142 const std::vector<Misspelling>& renderer_with_misspellings = |
140 feedback_.GetMisspellingsInRenderer(kRendererProcessId); | 143 feedback_.GetMisspellingsInRenderer(kRendererProcessId); |
141 EXPECT_EQ(static_cast<size_t>(1), renderer_with_misspellings.size()); | 144 EXPECT_EQ(static_cast<size_t>(1), renderer_with_misspellings.size()); |
142 EXPECT_EQ(kMisspellingHash, renderer_with_misspellings[0].hash); | 145 EXPECT_EQ(kMisspellingHash, renderer_with_misspellings[0].hash); |
143 const std::vector<Misspelling>& renderer_without_misspellings = | 146 const std::vector<Misspelling>& renderer_without_misspellings = |
144 feedback_.GetMisspellingsInRenderer(kRendererProcessId + 1); | 147 feedback_.GetMisspellingsInRenderer(kRendererProcessId + 1); |
145 EXPECT_EQ(static_cast<size_t>(0), renderer_without_misspellings.size()); | 148 EXPECT_EQ(static_cast<size_t>(0), renderer_without_misspellings.size()); |
146 } | 149 } |
147 | 150 |
148 // Finalized misspellings should be erased. | 151 // Finalized misspellings should be erased. |
149 TEST_F(FeedbackTest, EraseFinalizedMisspellings) { | 152 TEST_F(FeedbackTest, EraseFinalizedMisspellings) { |
150 AddMisspelling(kRendererProcessId, kMisspellingHash); | 153 AddMisspelling(kRendererProcessId, kMisspellingHash); |
151 feedback_.FinalizeRemovedMisspellings(kRendererProcessId, | 154 feedback_.FinalizeRemovedMisspellings(kRendererProcessId, |
152 std::vector<uint32>()); | 155 std::vector<uint32_t>()); |
153 EXPECT_TRUE(feedback_.RendererHasMisspellings(kRendererProcessId)); | 156 EXPECT_TRUE(feedback_.RendererHasMisspellings(kRendererProcessId)); |
154 feedback_.EraseFinalizedMisspellings(kRendererProcessId); | 157 feedback_.EraseFinalizedMisspellings(kRendererProcessId); |
155 EXPECT_FALSE(feedback_.RendererHasMisspellings(kRendererProcessId)); | 158 EXPECT_FALSE(feedback_.RendererHasMisspellings(kRendererProcessId)); |
156 EXPECT_TRUE(feedback_.GetMisspellingsInRenderer(kRendererProcessId).empty()); | 159 EXPECT_TRUE(feedback_.GetMisspellingsInRenderer(kRendererProcessId).empty()); |
157 } | 160 } |
158 | 161 |
159 // Should be able to check for misspelling existence. | 162 // Should be able to check for misspelling existence. |
160 TEST_F(FeedbackTest, HasMisspelling) { | 163 TEST_F(FeedbackTest, HasMisspelling) { |
161 EXPECT_FALSE(feedback_.HasMisspelling(kMisspellingHash)); | 164 EXPECT_FALSE(feedback_.HasMisspelling(kMisspellingHash)); |
162 AddMisspelling(kRendererProcessId, kMisspellingHash); | 165 AddMisspelling(kRendererProcessId, kMisspellingHash); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 // Should be able to find misspellings by misspelled word. | 234 // Should be able to find misspellings by misspelled word. |
232 TEST_F(FeedbackTest, FindMisspellingsByText) { | 235 TEST_F(FeedbackTest, FindMisspellingsByText) { |
233 static const base::string16 kMisspelledText = | 236 static const base::string16 kMisspelledText = |
234 ASCIIToUTF16("Helllo world. Helllo world"); | 237 ASCIIToUTF16("Helllo world. Helllo world"); |
235 static const base::string16 kSuggestion = ASCIIToUTF16("Hello"); | 238 static const base::string16 kSuggestion = ASCIIToUTF16("Hello"); |
236 static const int kMisspellingStart = 0; | 239 static const int kMisspellingStart = 0; |
237 static const int kMisspellingLength = 6; | 240 static const int kMisspellingLength = 6; |
238 static const int kSentenceLength = 14; | 241 static const int kSentenceLength = 14; |
239 static const int kNumberOfSentences = 2; | 242 static const int kNumberOfSentences = 2; |
240 static const int kNumberOfRenderers = 2; | 243 static const int kNumberOfRenderers = 2; |
241 uint32 hash = kMisspellingHash; | 244 uint32_t hash = kMisspellingHash; |
242 for (int renderer_process_id = kRendererProcessId; | 245 for (int renderer_process_id = kRendererProcessId; |
243 renderer_process_id < kRendererProcessId + kNumberOfRenderers; | 246 renderer_process_id < kRendererProcessId + kNumberOfRenderers; |
244 ++renderer_process_id) { | 247 ++renderer_process_id) { |
245 for (int j = 0; j < kNumberOfSentences; ++j) { | 248 for (int j = 0; j < kNumberOfSentences; ++j) { |
246 feedback_.AddMisspelling( | 249 feedback_.AddMisspelling( |
247 renderer_process_id, | 250 renderer_process_id, |
248 Misspelling(kMisspelledText, kMisspellingStart + j * kSentenceLength, | 251 Misspelling(kMisspelledText, kMisspellingStart + j * kSentenceLength, |
249 kMisspellingLength, | 252 kMisspellingLength, |
250 std::vector<base::string16>(1, kSuggestion), ++hash)); | 253 std::vector<base::string16>(1, kSuggestion), ++hash)); |
251 } | 254 } |
252 } | 255 } |
253 | 256 |
254 static const base::string16 kOtherMisspelledText = | 257 static const base::string16 kOtherMisspelledText = |
255 ASCIIToUTF16("Somethign else"); | 258 ASCIIToUTF16("Somethign else"); |
256 static const base::string16 kOtherSuggestion = ASCIIToUTF16("Something"); | 259 static const base::string16 kOtherSuggestion = ASCIIToUTF16("Something"); |
257 static const int kOtherMisspellingStart = 0; | 260 static const int kOtherMisspellingStart = 0; |
258 static const int kOtherMisspellingLength = 9; | 261 static const int kOtherMisspellingLength = 9; |
259 feedback_.AddMisspelling( | 262 feedback_.AddMisspelling( |
260 kRendererProcessId, | 263 kRendererProcessId, |
261 Misspelling(kOtherMisspelledText, kOtherMisspellingStart, | 264 Misspelling(kOtherMisspelledText, kOtherMisspellingStart, |
262 kOtherMisspellingLength, | 265 kOtherMisspellingLength, |
263 std::vector<base::string16>(1, kOtherSuggestion), hash + 1)); | 266 std::vector<base::string16>(1, kOtherSuggestion), hash + 1)); |
264 | 267 |
265 static const base::string16 kMisspelledWord = ASCIIToUTF16("Helllo"); | 268 static const base::string16 kMisspelledWord = ASCIIToUTF16("Helllo"); |
266 const std::set<uint32>& misspellings = | 269 const std::set<uint32_t>& misspellings = |
267 feedback_.FindMisspellings(kMisspelledWord); | 270 feedback_.FindMisspellings(kMisspelledWord); |
268 EXPECT_EQ(static_cast<size_t>(kNumberOfSentences * kNumberOfRenderers), | 271 EXPECT_EQ(static_cast<size_t>(kNumberOfSentences * kNumberOfRenderers), |
269 misspellings.size()); | 272 misspellings.size()); |
270 | 273 |
271 for (std::set<uint32>::const_iterator it = misspellings.begin(); | 274 for (std::set<uint32_t>::const_iterator it = misspellings.begin(); |
272 it != misspellings.end(); ++it) { | 275 it != misspellings.end(); ++it) { |
273 Misspelling* misspelling = feedback_.GetMisspelling(*it); | 276 Misspelling* misspelling = feedback_.GetMisspelling(*it); |
274 EXPECT_NE(nullptr, misspelling); | 277 EXPECT_NE(nullptr, misspelling); |
275 EXPECT_TRUE(misspelling->hash >= kMisspellingHash && | 278 EXPECT_TRUE(misspelling->hash >= kMisspellingHash && |
276 misspelling->hash <= hash); | 279 misspelling->hash <= hash); |
277 EXPECT_EQ(kMisspelledWord, GetMisspelledString(*misspelling)); | 280 EXPECT_EQ(kMisspelledWord, GetMisspelledString(*misspelling)); |
278 } | 281 } |
279 } | 282 } |
280 | 283 |
281 // Should not be able to find misspellings by misspelled word after they have | 284 // Should not be able to find misspellings by misspelled word after they have |
282 // been removed. | 285 // been removed. |
283 TEST_F(FeedbackTest, CannotFindMisspellingsByTextAfterErased) { | 286 TEST_F(FeedbackTest, CannotFindMisspellingsByTextAfterErased) { |
284 static const base::string16 kMisspelledText = ASCIIToUTF16("Helllo world"); | 287 static const base::string16 kMisspelledText = ASCIIToUTF16("Helllo world"); |
285 static const base::string16 kMisspelledWord = ASCIIToUTF16("Helllo"); | 288 static const base::string16 kMisspelledWord = ASCIIToUTF16("Helllo"); |
286 static const base::string16 kSuggestion = ASCIIToUTF16("Hello"); | 289 static const base::string16 kSuggestion = ASCIIToUTF16("Hello"); |
287 static const int kMisspellingStart = 0; | 290 static const int kMisspellingStart = 0; |
288 static const int kMisspellingLength = 6; | 291 static const int kMisspellingLength = 6; |
289 feedback_.AddMisspelling( | 292 feedback_.AddMisspelling( |
290 kRendererProcessId, | 293 kRendererProcessId, |
291 Misspelling(kMisspelledText, kMisspellingStart, kMisspellingLength, | 294 Misspelling(kMisspelledText, kMisspellingStart, kMisspellingLength, |
292 std::vector<base::string16>(1, kSuggestion), | 295 std::vector<base::string16>(1, kSuggestion), |
293 kMisspellingHash)); | 296 kMisspellingHash)); |
294 feedback_.GetMisspelling(kMisspellingHash)->action.Finalize(); | 297 feedback_.GetMisspelling(kMisspellingHash)->action.Finalize(); |
295 feedback_.EraseFinalizedMisspellings(kRendererProcessId); | 298 feedback_.EraseFinalizedMisspellings(kRendererProcessId); |
296 EXPECT_TRUE(feedback_.FindMisspellings(kMisspelledWord).empty()); | 299 EXPECT_TRUE(feedback_.FindMisspellings(kMisspelledWord).empty()); |
297 } | 300 } |
298 | 301 |
299 } // namespace spellcheck | 302 } // namespace spellcheck |
OLD | NEW |