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 |FeedbackSender| object. | 5 // Unit tests for |FeedbackSender| object. |
6 | 6 |
7 #include "chrome/browser/spellchecker/feedback_sender.h" | 7 #include "chrome/browser/spellchecker/feedback_sender.h" |
8 | 8 |
9 #include <stddef.h> | 9 #include <stddef.h> |
10 #include <stdint.h> | 10 #include <stdint.h> |
(...skipping 23 matching lines...) Expand all Loading... | |
34 const char kCountry[] = "USA"; | 34 const char kCountry[] = "USA"; |
35 const char kLanguage[] = "en"; | 35 const char kLanguage[] = "en"; |
36 const char kText[] = "Helllo world."; | 36 const char kText[] = "Helllo world."; |
37 const int kMisspellingLength = 6; | 37 const int kMisspellingLength = 6; |
38 const int kMisspellingStart = 0; | 38 const int kMisspellingStart = 0; |
39 const int kRendererProcessId = 0; | 39 const int kRendererProcessId = 0; |
40 const int kUrlFetcherId = 0; | 40 const int kUrlFetcherId = 0; |
41 | 41 |
42 // Builds a simple spellcheck result. | 42 // Builds a simple spellcheck result. |
43 SpellCheckResult BuildSpellCheckResult() { | 43 SpellCheckResult BuildSpellCheckResult() { |
44 return SpellCheckResult(SpellCheckResult::SPELLING, | 44 return SpellCheckResult(SpellCheckResult::SPELLING, kMisspellingStart, |
45 kMisspellingStart, | 45 kMisspellingLength, base::UTF8ToUTF16("Hello")); |
46 kMisspellingLength, | |
47 base::UTF8ToUTF16("Hello")); | |
48 } | 46 } |
49 | 47 |
50 // Returns the number of times that |needle| appears in |haystack| without | 48 // Returns the number of times that |needle| appears in |haystack| without |
51 // overlaps. For example, CountOccurences("bananana", "nana") returns 1. | 49 // overlaps. For example, CountOccurences("bananana", "nana") returns 1. |
52 int CountOccurences(const std::string& haystack, const std::string& needle) { | 50 int CountOccurences(const std::string& haystack, const std::string& needle) { |
53 int number_of_occurrences = 0; | 51 int number_of_occurrences = 0; |
54 for (size_t pos = haystack.find(needle); | 52 for (size_t pos = haystack.find(needle); pos != std::string::npos; |
55 pos != std::string::npos; | |
56 pos = haystack.find(needle, pos + needle.length())) { | 53 pos = haystack.find(needle, pos + needle.length())) { |
57 ++number_of_occurrences; | 54 ++number_of_occurrences; |
58 } | 55 } |
59 return number_of_occurrences; | 56 return number_of_occurrences; |
60 } | 57 } |
61 | 58 |
62 } // namespace | 59 } // namespace |
63 | 60 |
64 // A test fixture to help keep tests simple. | 61 // A test fixture to help keep tests simple. |
65 class FeedbackSenderTest : public testing::Test { | 62 class FeedbackSenderTest : public testing::Test { |
66 public: | 63 public: |
67 FeedbackSenderTest() : ui_thread_(content::BrowserThread::UI, &loop_) { | 64 FeedbackSenderTest() : ui_thread_(content::BrowserThread::UI, &loop_) { |
68 feedback_.reset(new FeedbackSender(NULL, kLanguage, kCountry)); | 65 feedback_.reset(new FeedbackSender(nullptr, kLanguage, kCountry)); |
69 feedback_->StartFeedbackCollection(); | 66 feedback_->StartFeedbackCollection(); |
70 } | 67 } |
71 | 68 |
72 ~FeedbackSenderTest() override {} | 69 ~FeedbackSenderTest() override {} |
73 | 70 |
74 protected: | 71 protected: |
75 // Appends the "--enable-spelling-service-feedback" switch to the | 72 // Appends the "--enable-spelling-service-feedback" switch to the |
76 // command-line. | 73 // command-line. |
77 void AppendCommandLineSwitch() { | 74 void AppendCommandLineSwitch() { |
78 // The command-line switch is temporary. | 75 // The command-line switch is temporary. |
79 // TODO(rouslan): Remove the command-line switch. http://crbug.com/247726 | 76 // TODO(rouslan): Remove the command-line switch. http://crbug.com/247726 |
80 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 77 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
81 switches::kEnableSpellingFeedbackFieldTrial); | 78 switches::kEnableSpellingFeedbackFieldTrial); |
82 feedback_.reset(new FeedbackSender(NULL, kLanguage, kCountry)); | 79 feedback_.reset(new FeedbackSender(nullptr, kLanguage, kCountry)); |
83 feedback_->StartFeedbackCollection(); | 80 feedback_->StartFeedbackCollection(); |
84 } | 81 } |
85 | 82 |
86 // Enables the "SpellingServiceFeedback.Enabled" field trial. | 83 // Enables the "SpellingServiceFeedback.Enabled" field trial. |
87 void EnableFieldTrial() { | 84 void EnableFieldTrial() { |
88 // The field trial is temporary. | 85 // The field trial is temporary. |
89 // TODO(rouslan): Remove the field trial. http://crbug.com/247726 | 86 // TODO(rouslan): Remove the field trial. http://crbug.com/247726 |
90 field_trial_list_.reset( | 87 field_trial_list_.reset( |
91 new base::FieldTrialList(new metrics::SHA1EntropyProvider("foo"))); | 88 new base::FieldTrialList(new metrics::SHA1EntropyProvider("foo"))); |
92 field_trial_ = base::FieldTrialList::CreateFieldTrial( | 89 field_trial_ = base::FieldTrialList::CreateFieldTrial( |
93 kFeedbackFieldTrialName, kFeedbackFieldTrialEnabledGroupName); | 90 kFeedbackFieldTrialName, kFeedbackFieldTrialEnabledGroupName); |
94 field_trial_->group(); | 91 field_trial_->group(); |
95 feedback_.reset(new FeedbackSender(NULL, kLanguage, kCountry)); | 92 feedback_.reset(new FeedbackSender(nullptr, kLanguage, kCountry)); |
96 feedback_->StartFeedbackCollection(); | 93 feedback_->StartFeedbackCollection(); |
97 } | 94 } |
98 | 95 |
99 uint32_t AddPendingFeedback() { | 96 uint32_t AddPendingFeedback() { |
100 std::vector<SpellCheckResult> results(1, BuildSpellCheckResult()); | 97 std::vector<SpellCheckResult> results(1, BuildSpellCheckResult()); |
101 feedback_->OnSpellcheckResults(kRendererProcessId, | 98 feedback_->OnSpellcheckResults(kRendererProcessId, base::UTF8ToUTF16(kText), |
102 base::UTF8ToUTF16(kText), | 99 std::vector<SpellCheckMarker>(), &results); |
103 std::vector<SpellCheckMarker>(), | |
104 &results); | |
105 return results[0].hash; | 100 return results[0].hash; |
106 } | 101 } |
107 | 102 |
108 void ExpireSession() { | 103 void ExpireSession() { |
109 feedback_->session_start_ = | 104 feedback_->session_start_ = |
110 base::Time::Now() - | 105 base::Time::Now() - |
111 base::TimeDelta::FromHours(chrome::spellcheck_common::kSessionHours); | 106 base::TimeDelta::FromHours(chrome::spellcheck_common::kSessionHours); |
112 } | 107 } |
113 | 108 |
114 bool UploadDataContains(const std::string& data) const { | 109 bool UploadDataContains(const std::string& data) const { |
115 const net::TestURLFetcher* fetcher = | 110 const net::TestURLFetcher* fetcher = |
116 fetchers_.GetFetcherByID(kUrlFetcherId); | 111 fetchers_.GetFetcherByID(kUrlFetcherId); |
117 return fetcher && CountOccurences(fetcher->upload_data(), data) > 0; | 112 return fetcher && CountOccurences(fetcher->upload_data(), data) > 0; |
118 } | 113 } |
119 | 114 |
120 bool UploadDataContains(const std::string& data, | 115 bool UploadDataContains(const std::string& data, |
121 int number_of_occurrences) const { | 116 int number_of_occurrences) const { |
122 const net::TestURLFetcher* fetcher = | 117 const net::TestURLFetcher* fetcher = |
123 fetchers_.GetFetcherByID(kUrlFetcherId); | 118 fetchers_.GetFetcherByID(kUrlFetcherId); |
124 return fetcher && CountOccurences(fetcher->upload_data(), data) == | 119 return fetcher && |
125 number_of_occurrences; | 120 CountOccurences(fetcher->upload_data(), data) == |
121 number_of_occurrences; | |
126 } | 122 } |
127 | 123 |
128 // Returns true if the feedback sender would be uploading data now. The test | 124 // Returns true if the feedback sender would be uploading data now. The test |
129 // does not open network connections. | 125 // does not open network connections. |
130 bool IsUploadingData() const { | 126 bool IsUploadingData() const { |
131 return !!fetchers_.GetFetcherByID(kUrlFetcherId); | 127 return !!fetchers_.GetFetcherByID(kUrlFetcherId); |
132 } | 128 } |
133 | 129 |
134 void ClearUploadData() { | 130 void ClearUploadData() { fetchers_.RemoveFetcherFromMap(kUrlFetcherId); } |
135 fetchers_.RemoveFetcherFromMap(kUrlFetcherId); | |
136 } | |
137 | 131 |
138 std::string GetUploadData() const { | 132 std::string GetUploadData() const { |
139 const net::TestURLFetcher* fetcher = | 133 const net::TestURLFetcher* fetcher = |
140 fetchers_.GetFetcherByID(kUrlFetcherId); | 134 fetchers_.GetFetcherByID(kUrlFetcherId); |
141 return fetcher ? fetcher->upload_data() : std::string(); | 135 return fetcher ? fetcher->upload_data() : std::string(); |
142 } | 136 } |
143 | 137 |
144 scoped_ptr<spellcheck::FeedbackSender> feedback_; | 138 scoped_ptr<spellcheck::FeedbackSender> feedback_; |
145 | 139 |
146 private: | 140 private: |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 | 175 |
182 // Send NO_ACTION feedback message if the marker has been removed from the | 176 // Send NO_ACTION feedback message if the marker has been removed from the |
183 // document. | 177 // document. |
184 TEST_F(FeedbackSenderTest, NoActionFeedback) { | 178 TEST_F(FeedbackSenderTest, NoActionFeedback) { |
185 AddPendingFeedback(); | 179 AddPendingFeedback(); |
186 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | 180 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, |
187 std::vector<uint32_t>()); | 181 std::vector<uint32_t>()); |
188 EXPECT_TRUE(UploadDataContains("\"actionType\":\"NO_ACTION\"")); | 182 EXPECT_TRUE(UploadDataContains("\"actionType\":\"NO_ACTION\"")); |
189 } | 183 } |
190 | 184 |
185 #define SUGGESTIONINDEX 0 | |
please use gerrit instead
2016/02/03 23:59:10
const int kSuggestionIndex = 0;
if possible.
Kevin Bailey
2016/02/04 16:34:11
Rewritten.
| |
186 #define STR(x) #x | |
187 #define STREVAL(x) STR(x) | |
please use gerrit instead
2016/02/03 23:59:10
Let's put the "#define" statements before all of t
Kevin Bailey
2016/02/04 16:34:11
They're gone.
| |
188 | |
groby-ooo-7-16
2016/02/04 03:43:15
Let's just not do this. I can't see any value in t
Kevin Bailey
2016/02/04 16:34:11
There was an honest to goodness bug in the origina
| |
191 // Send SELECT feedback message if the user has selected a spelling suggestion. | 189 // Send SELECT feedback message if the user has selected a spelling suggestion. |
192 TEST_F(FeedbackSenderTest, SelectFeedback) { | 190 TEST_F(FeedbackSenderTest, SelectFeedback) { |
193 uint32_t hash = AddPendingFeedback(); | 191 uint32_t hash = AddPendingFeedback(); |
194 static const int kSuggestionIndex = 0; | 192 feedback_->SelectedSuggestion(hash, SUGGESTIONINDEX); |
195 feedback_->SelectedSuggestion(hash, kSuggestionIndex); | |
196 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | 193 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, |
197 std::vector<uint32_t>()); | 194 std::vector<uint32_t>()); |
198 EXPECT_TRUE(UploadDataContains("\"actionType\":\"SELECT\"")); | 195 EXPECT_TRUE(UploadDataContains("\"actionType\":\"SELECT\"")); |
199 EXPECT_TRUE(UploadDataContains("\"actionTargetIndex\":" + kSuggestionIndex)); | 196 EXPECT_TRUE( |
197 UploadDataContains("\"actionTargetIndex\":" STREVAL(SUGGESTIONINDEX))); | |
200 } | 198 } |
201 | 199 |
202 // Send ADD_TO_DICT feedback message if the user has added the misspelled word | 200 // Send ADD_TO_DICT feedback message if the user has added the misspelled word |
203 // to the custom dictionary. | 201 // to the custom dictionary. |
204 TEST_F(FeedbackSenderTest, AddToDictFeedback) { | 202 TEST_F(FeedbackSenderTest, AddToDictFeedback) { |
205 uint32_t hash = AddPendingFeedback(); | 203 uint32_t hash = AddPendingFeedback(); |
206 feedback_->AddedToDictionary(hash); | 204 feedback_->AddedToDictionary(hash); |
207 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | 205 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, |
208 std::vector<uint32_t>()); | 206 std::vector<uint32_t>()); |
209 EXPECT_TRUE(UploadDataContains("\"actionType\":\"ADD_TO_DICT\"")); | 207 EXPECT_TRUE(UploadDataContains("\"actionType\":\"ADD_TO_DICT\"")); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
249 std::vector<uint32_t>()); | 247 std::vector<uint32_t>()); |
250 EXPECT_TRUE(UploadDataContains("\"actionType\":\"MANUALLY_CORRECTED\"")); | 248 EXPECT_TRUE(UploadDataContains("\"actionType\":\"MANUALLY_CORRECTED\"")); |
251 EXPECT_TRUE(UploadDataContains("\"actionTargetValue\":\"" + | 249 EXPECT_TRUE(UploadDataContains("\"actionTargetValue\":\"" + |
252 kManualCorrection + "\"")); | 250 kManualCorrection + "\"")); |
253 } | 251 } |
254 | 252 |
255 // Send feedback messages in batch. | 253 // Send feedback messages in batch. |
256 TEST_F(FeedbackSenderTest, BatchFeedback) { | 254 TEST_F(FeedbackSenderTest, BatchFeedback) { |
257 std::vector<SpellCheckResult> results; | 255 std::vector<SpellCheckResult> results; |
258 results.push_back(SpellCheckResult(SpellCheckResult::SPELLING, | 256 results.push_back(SpellCheckResult(SpellCheckResult::SPELLING, |
259 kMisspellingStart, | 257 kMisspellingStart, kMisspellingLength, |
260 kMisspellingLength, | |
261 base::ASCIIToUTF16("Hello"))); | 258 base::ASCIIToUTF16("Hello"))); |
262 static const int kSecondMisspellingStart = 7; | 259 static const int kSecondMisspellingStart = 7; |
263 static const int kSecondMisspellingLength = 5; | 260 static const int kSecondMisspellingLength = 5; |
264 results.push_back(SpellCheckResult(SpellCheckResult::SPELLING, | 261 results.push_back( |
265 kSecondMisspellingStart, | 262 SpellCheckResult(SpellCheckResult::SPELLING, kSecondMisspellingStart, |
266 kSecondMisspellingLength, | 263 kSecondMisspellingLength, base::ASCIIToUTF16("world"))); |
267 base::ASCIIToUTF16("world"))); | 264 feedback_->OnSpellcheckResults(kRendererProcessId, base::UTF8ToUTF16(kText), |
268 feedback_->OnSpellcheckResults(kRendererProcessId, | 265 std::vector<SpellCheckMarker>(), &results); |
269 base::UTF8ToUTF16(kText), | |
270 std::vector<SpellCheckMarker>(), | |
271 &results); | |
272 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | 266 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, |
273 std::vector<uint32_t>()); | 267 std::vector<uint32_t>()); |
274 EXPECT_TRUE(UploadDataContains("\"actionType\":\"NO_ACTION\"", 2)); | 268 EXPECT_TRUE(UploadDataContains("\"actionType\":\"NO_ACTION\"", 2)); |
275 } | 269 } |
276 | 270 |
277 // Send a series of PENDING feedback messages and one final NO_ACTION feedback | 271 // Send a series of PENDING feedback messages and one final NO_ACTION feedback |
278 // message with the same hash identifier for a single misspelling. | 272 // message with the same hash identifier for a single misspelling. |
279 TEST_F(FeedbackSenderTest, SameHashFeedback) { | 273 TEST_F(FeedbackSenderTest, SameHashFeedback) { |
280 uint32_t hash = AddPendingFeedback(); | 274 uint32_t hash = AddPendingFeedback(); |
281 std::vector<uint32_t> remaining_markers(1, hash); | 275 std::vector<uint32_t> remaining_markers(1, hash); |
(...skipping 20 matching lines...) Expand all Loading... | |
302 EXPECT_FALSE(IsUploadingData()); | 296 EXPECT_FALSE(IsUploadingData()); |
303 } | 297 } |
304 | 298 |
305 // When a session expires: | 299 // When a session expires: |
306 // 1) Pending feedback is finalized and sent to the server in the last message | 300 // 1) Pending feedback is finalized and sent to the server in the last message |
307 // batch in the session. | 301 // batch in the session. |
308 // 2) No feedback is sent until a spellcheck request happens. | 302 // 2) No feedback is sent until a spellcheck request happens. |
309 // 3) Existing markers get new hash identifiers. | 303 // 3) Existing markers get new hash identifiers. |
310 TEST_F(FeedbackSenderTest, SessionExpirationFeedback) { | 304 TEST_F(FeedbackSenderTest, SessionExpirationFeedback) { |
311 std::vector<SpellCheckResult> results( | 305 std::vector<SpellCheckResult> results( |
312 1, | 306 1, SpellCheckResult(SpellCheckResult::SPELLING, kMisspellingStart, |
groby-ooo-7-16
2016/02/04 03:43:15
If we're only reformatting all this code to make c
| |
313 SpellCheckResult(SpellCheckResult::SPELLING, | 307 kMisspellingLength, base::ASCIIToUTF16("Hello"))); |
314 kMisspellingStart, | 308 feedback_->OnSpellcheckResults(kRendererProcessId, base::UTF8ToUTF16(kText), |
315 kMisspellingLength, | 309 std::vector<SpellCheckMarker>(), &results); |
316 base::ASCIIToUTF16("Hello"))); | |
317 feedback_->OnSpellcheckResults(kRendererProcessId, | |
318 base::UTF8ToUTF16(kText), | |
319 std::vector<SpellCheckMarker>(), | |
320 &results); | |
321 uint32_t original_hash = results[0].hash; | 310 uint32_t original_hash = results[0].hash; |
322 std::vector<uint32_t> remaining_markers(1, original_hash); | 311 std::vector<uint32_t> remaining_markers(1, original_hash); |
323 | 312 |
324 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, remaining_markers); | 313 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, remaining_markers); |
325 EXPECT_FALSE(UploadDataContains("\"actionType\":\"NO_ACTION\"")); | 314 EXPECT_FALSE(UploadDataContains("\"actionType\":\"NO_ACTION\"")); |
326 EXPECT_TRUE(UploadDataContains("\"actionType\":\"PENDING\"")); | 315 EXPECT_TRUE(UploadDataContains("\"actionType\":\"PENDING\"")); |
327 std::string original_hash_string = | 316 std::string original_hash_string = |
328 base::StringPrintf("\"suggestionId\":\"%u\"", original_hash); | 317 base::StringPrintf("\"suggestionId\":\"%u\"", original_hash); |
329 EXPECT_TRUE(UploadDataContains(original_hash_string)); | 318 EXPECT_TRUE(UploadDataContains(original_hash_string)); |
330 ClearUploadData(); | 319 ClearUploadData(); |
331 | 320 |
332 ExpireSession(); | 321 ExpireSession(); |
333 | 322 |
334 // Last message batch in the current session has only finalized messages. | 323 // Last message batch in the current session has only finalized messages. |
335 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, remaining_markers); | 324 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, remaining_markers); |
336 EXPECT_TRUE(UploadDataContains("\"actionType\":\"NO_ACTION\"")); | 325 EXPECT_TRUE(UploadDataContains("\"actionType\":\"NO_ACTION\"")); |
337 EXPECT_FALSE(UploadDataContains("\"actionType\":\"PENDING\"")); | 326 EXPECT_FALSE(UploadDataContains("\"actionType\":\"PENDING\"")); |
338 EXPECT_TRUE(UploadDataContains(original_hash_string)); | 327 EXPECT_TRUE(UploadDataContains(original_hash_string)); |
339 ClearUploadData(); | 328 ClearUploadData(); |
340 | 329 |
341 // The next session starts on the next spellchecker request. Until then, | 330 // The next session starts on the next spellchecker request. Until then, |
342 // there's no more feedback sent. | 331 // there's no more feedback sent. |
343 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, remaining_markers); | 332 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, remaining_markers); |
344 EXPECT_FALSE(IsUploadingData()); | 333 EXPECT_FALSE(IsUploadingData()); |
345 | 334 |
346 // The first spellcheck request after session expiration creates different | 335 // The first spellcheck request after session expiration creates different |
347 // document marker hash identifiers. | 336 // document marker hash identifiers. |
348 std::vector<SpellCheckMarker> original_markers( | 337 std::vector<SpellCheckMarker> original_markers( |
349 1, SpellCheckMarker(results[0].hash, results[0].location)); | 338 1, SpellCheckMarker(results[0].hash, results[0].location)); |
350 results[0] = SpellCheckResult(SpellCheckResult::SPELLING, | 339 results[0] = |
351 kMisspellingStart, | 340 SpellCheckResult(SpellCheckResult::SPELLING, kMisspellingStart, |
352 kMisspellingLength, | 341 kMisspellingLength, base::ASCIIToUTF16("Hello")); |
353 base::ASCIIToUTF16("Hello")); | 342 feedback_->OnSpellcheckResults(kRendererProcessId, base::UTF8ToUTF16(kText), |
354 feedback_->OnSpellcheckResults( | 343 original_markers, &results); |
355 kRendererProcessId, base::UTF8ToUTF16(kText), original_markers, &results); | |
356 uint32_t updated_hash = results[0].hash; | 344 uint32_t updated_hash = results[0].hash; |
357 EXPECT_NE(updated_hash, original_hash); | 345 EXPECT_NE(updated_hash, original_hash); |
358 remaining_markers[0] = updated_hash; | 346 remaining_markers[0] = updated_hash; |
359 | 347 |
360 // The first feedback message batch in session |i + 1| has the new document | 348 // The first feedback message batch in session |i + 1| has the new document |
361 // marker hash identifiers. | 349 // marker hash identifiers. |
362 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, remaining_markers); | 350 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, remaining_markers); |
363 EXPECT_FALSE(UploadDataContains("\"actionType\":\"NO_ACTION\"")); | 351 EXPECT_FALSE(UploadDataContains("\"actionType\":\"NO_ACTION\"")); |
364 EXPECT_TRUE(UploadDataContains("\"actionType\":\"PENDING\"")); | 352 EXPECT_TRUE(UploadDataContains("\"actionType\":\"PENDING\"")); |
365 EXPECT_FALSE(UploadDataContains(original_hash_string)); | 353 EXPECT_FALSE(UploadDataContains(original_hash_string)); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
415 | 403 |
416 // The field names and types should correspond to the API. | 404 // The field names and types should correspond to the API. |
417 TEST_F(FeedbackSenderTest, FeedbackAPI) { | 405 TEST_F(FeedbackSenderTest, FeedbackAPI) { |
418 AddPendingFeedback(); | 406 AddPendingFeedback(); |
419 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | 407 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, |
420 std::vector<uint32_t>()); | 408 std::vector<uint32_t>()); |
421 std::string actual_data = GetUploadData(); | 409 std::string actual_data = GetUploadData(); |
422 scoped_ptr<base::DictionaryValue> actual(static_cast<base::DictionaryValue*>( | 410 scoped_ptr<base::DictionaryValue> actual(static_cast<base::DictionaryValue*>( |
423 base::JSONReader::Read(actual_data).release())); | 411 base::JSONReader::Read(actual_data).release())); |
424 actual->SetString("params.key", "TestDummyKey"); | 412 actual->SetString("params.key", "TestDummyKey"); |
425 base::ListValue* suggestions = NULL; | 413 base::ListValue* suggestions = nullptr; |
426 actual->GetList("params.suggestionInfo", &suggestions); | 414 actual->GetList("params.suggestionInfo", &suggestions); |
427 base::DictionaryValue* suggestion = NULL; | 415 base::DictionaryValue* suggestion = nullptr; |
428 suggestions->GetDictionary(0, &suggestion); | 416 suggestions->GetDictionary(0, &suggestion); |
429 suggestion->SetString("suggestionId", "42"); | 417 suggestion->SetString("suggestionId", "42"); |
430 suggestion->SetString("timestamp", "9001"); | 418 suggestion->SetString("timestamp", "9001"); |
431 static const std::string expected_data = | 419 static const std::string expected_data = |
432 "{\"apiVersion\":\"v2\"," | 420 "{\"apiVersion\":\"v2\"," |
433 "\"method\":\"spelling.feedback\"," | 421 "\"method\":\"spelling.feedback\"," |
434 "\"params\":" | 422 "\"params\":" |
435 "{\"clientName\":\"Chrome\"," | 423 "{\"clientName\":\"Chrome\"," |
436 "\"originCountry\":\"USA\"," | 424 "\"originCountry\":\"USA\"," |
437 "\"key\":\"TestDummyKey\"," | 425 "\"key\":\"TestDummyKey\"," |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
498 std::vector<uint32_t>()); | 486 std::vector<uint32_t>()); |
499 | 487 |
500 EXPECT_FALSE(UploadDataContains("\"apiVersion\":\"v2\"")); | 488 EXPECT_FALSE(UploadDataContains("\"apiVersion\":\"v2\"")); |
501 EXPECT_TRUE(UploadDataContains("\"apiVersion\":\"v2-internal\"")); | 489 EXPECT_TRUE(UploadDataContains("\"apiVersion\":\"v2-internal\"")); |
502 } | 490 } |
503 | 491 |
504 // Duplicate spellcheck results should be matched to the existing markers. | 492 // Duplicate spellcheck results should be matched to the existing markers. |
505 TEST_F(FeedbackSenderTest, MatchDupliateResultsWithExistingMarkers) { | 493 TEST_F(FeedbackSenderTest, MatchDupliateResultsWithExistingMarkers) { |
506 uint32_t hash = AddPendingFeedback(); | 494 uint32_t hash = AddPendingFeedback(); |
507 std::vector<SpellCheckResult> results( | 495 std::vector<SpellCheckResult> results( |
508 1, | 496 1, SpellCheckResult(SpellCheckResult::SPELLING, kMisspellingStart, |
509 SpellCheckResult(SpellCheckResult::SPELLING, | 497 kMisspellingLength, base::ASCIIToUTF16("Hello"))); |
510 kMisspellingStart, | |
511 kMisspellingLength, | |
512 base::ASCIIToUTF16("Hello"))); | |
513 std::vector<SpellCheckMarker> markers( | 498 std::vector<SpellCheckMarker> markers( |
514 1, SpellCheckMarker(hash, results[0].location)); | 499 1, SpellCheckMarker(hash, results[0].location)); |
515 EXPECT_EQ(static_cast<uint32_t>(0), results[0].hash); | 500 EXPECT_EQ(static_cast<uint32_t>(0), results[0].hash); |
516 feedback_->OnSpellcheckResults( | 501 feedback_->OnSpellcheckResults(kRendererProcessId, base::UTF8ToUTF16(kText), |
517 kRendererProcessId, base::UTF8ToUTF16(kText), markers, &results); | 502 markers, &results); |
518 EXPECT_EQ(hash, results[0].hash); | 503 EXPECT_EQ(hash, results[0].hash); |
519 } | 504 } |
520 | 505 |
521 // Adding a word to dictionary should trigger ADD_TO_DICT feedback for every | 506 // Adding a word to dictionary should trigger ADD_TO_DICT feedback for every |
522 // occurrence of that word. | 507 // occurrence of that word. |
523 TEST_F(FeedbackSenderTest, MultipleAddToDictFeedback) { | 508 TEST_F(FeedbackSenderTest, MultipleAddToDictFeedback) { |
524 std::vector<SpellCheckResult> results; | 509 std::vector<SpellCheckResult> results; |
525 static const int kSentenceLength = 14; | 510 static const int kSentenceLength = 14; |
526 static const int kNumberOfSentences = 2; | 511 static const int kNumberOfSentences = 2; |
527 static const base::string16 kTextWithDuplicates = | 512 static const base::string16 kTextWithDuplicates = |
528 base::ASCIIToUTF16("Helllo world. Helllo world."); | 513 base::ASCIIToUTF16("Helllo world. Helllo world."); |
529 for (int i = 0; i < kNumberOfSentences; ++i) { | 514 for (int i = 0; i < kNumberOfSentences; ++i) { |
530 results.push_back(SpellCheckResult(SpellCheckResult::SPELLING, | 515 results.push_back(SpellCheckResult( |
531 kMisspellingStart + i * kSentenceLength, | 516 SpellCheckResult::SPELLING, kMisspellingStart + i * kSentenceLength, |
532 kMisspellingLength, | 517 kMisspellingLength, base::ASCIIToUTF16("Hello"))); |
533 base::ASCIIToUTF16("Hello"))); | |
534 } | 518 } |
535 static const int kNumberOfRenderers = 2; | 519 static const int kNumberOfRenderers = 2; |
536 int last_renderer_process_id = -1; | 520 int last_renderer_process_id = -1; |
537 for (int i = 0; i < kNumberOfRenderers; ++i) { | 521 for (int i = 0; i < kNumberOfRenderers; ++i) { |
538 feedback_->OnSpellcheckResults(kRendererProcessId + i, | 522 feedback_->OnSpellcheckResults(kRendererProcessId + i, kTextWithDuplicates, |
539 kTextWithDuplicates, | 523 std::vector<SpellCheckMarker>(), &results); |
540 std::vector<SpellCheckMarker>(), | |
541 &results); | |
542 last_renderer_process_id = kRendererProcessId + i; | 524 last_renderer_process_id = kRendererProcessId + i; |
543 } | 525 } |
544 std::vector<uint32_t> remaining_markers; | 526 std::vector<uint32_t> remaining_markers; |
545 for (size_t i = 0; i < results.size(); ++i) | 527 for (size_t i = 0; i < results.size(); ++i) |
546 remaining_markers.push_back(results[i].hash); | 528 remaining_markers.push_back(results[i].hash); |
547 feedback_->OnReceiveDocumentMarkers(last_renderer_process_id, | 529 feedback_->OnReceiveDocumentMarkers(last_renderer_process_id, |
548 remaining_markers); | 530 remaining_markers); |
549 EXPECT_TRUE(UploadDataContains("PENDING", 2)); | 531 EXPECT_TRUE(UploadDataContains("PENDING", 2)); |
550 EXPECT_FALSE(UploadDataContains("ADD_TO_DICT")); | 532 EXPECT_FALSE(UploadDataContains("ADD_TO_DICT")); |
551 | 533 |
(...skipping 14 matching lines...) Expand all Loading... | |
566 feedback_->AddedToDictionary(add_to_dict_hash); | 548 feedback_->AddedToDictionary(add_to_dict_hash); |
567 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | 549 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, |
568 std::vector<uint32_t>()); | 550 std::vector<uint32_t>()); |
569 EXPECT_TRUE(UploadDataContains("SELECT", 1)); | 551 EXPECT_TRUE(UploadDataContains("SELECT", 1)); |
570 EXPECT_TRUE(UploadDataContains("ADD_TO_DICT", 2)); | 552 EXPECT_TRUE(UploadDataContains("ADD_TO_DICT", 2)); |
571 } | 553 } |
572 | 554 |
573 // Spellcheck results that are out-of-bounds are not added to feedback. | 555 // Spellcheck results that are out-of-bounds are not added to feedback. |
574 TEST_F(FeedbackSenderTest, IgnoreOutOfBounds) { | 556 TEST_F(FeedbackSenderTest, IgnoreOutOfBounds) { |
575 std::vector<SpellCheckResult> results; | 557 std::vector<SpellCheckResult> results; |
576 results.push_back(SpellCheckResult( | 558 results.push_back(SpellCheckResult(SpellCheckResult::SPELLING, 0, 100, |
577 SpellCheckResult::SPELLING, 0, 100, base::UTF8ToUTF16("Hello"))); | 559 base::UTF8ToUTF16("Hello"))); |
578 results.push_back(SpellCheckResult( | 560 results.push_back(SpellCheckResult(SpellCheckResult::SPELLING, 100, 3, |
579 SpellCheckResult::SPELLING, 100, 3, base::UTF8ToUTF16("world"))); | 561 base::UTF8ToUTF16("world"))); |
580 results.push_back(SpellCheckResult( | 562 results.push_back(SpellCheckResult(SpellCheckResult::SPELLING, -1, 3, |
581 SpellCheckResult::SPELLING, -1, 3, base::UTF8ToUTF16("how"))); | 563 base::UTF8ToUTF16("how"))); |
582 results.push_back(SpellCheckResult( | 564 results.push_back(SpellCheckResult(SpellCheckResult::SPELLING, 0, 0, |
583 SpellCheckResult::SPELLING, 0, 0, base::UTF8ToUTF16("are"))); | 565 base::UTF8ToUTF16("are"))); |
584 results.push_back(SpellCheckResult( | 566 results.push_back(SpellCheckResult(SpellCheckResult::SPELLING, 2, -1, |
585 SpellCheckResult::SPELLING, 2, -1, base::UTF8ToUTF16("you"))); | 567 base::UTF8ToUTF16("you"))); |
586 feedback_->OnSpellcheckResults(kRendererProcessId, | 568 feedback_->OnSpellcheckResults(kRendererProcessId, base::UTF8ToUTF16(kText), |
587 base::UTF8ToUTF16(kText), | 569 std::vector<SpellCheckMarker>(), &results); |
588 std::vector<SpellCheckMarker>(), | |
589 &results); | |
590 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | 570 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, |
591 std::vector<uint32_t>()); | 571 std::vector<uint32_t>()); |
592 EXPECT_FALSE(IsUploadingData()); | 572 EXPECT_FALSE(IsUploadingData()); |
593 } | 573 } |
594 | 574 |
595 // FeedbackSender does not collect and upload feedback when instructed to stop. | 575 // FeedbackSender does not collect and upload feedback when instructed to stop. |
596 TEST_F(FeedbackSenderTest, CanStopFeedbackCollection) { | 576 TEST_F(FeedbackSenderTest, CanStopFeedbackCollection) { |
597 feedback_->StopFeedbackCollection(); | 577 feedback_->StopFeedbackCollection(); |
598 AddPendingFeedback(); | 578 AddPendingFeedback(); |
599 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | 579 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
636 "the chance to work hard at work worth doing."), | 616 "the chance to work hard at work worth doing."), |
637 std::vector<SpellCheckMarker>(), &results); | 617 std::vector<SpellCheckMarker>(), &results); |
638 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | 618 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, |
639 std::vector<uint32_t>()); | 619 std::vector<uint32_t>()); |
640 EXPECT_TRUE( | 620 EXPECT_TRUE( |
641 UploadDataContains(",\"originalText\":\"and away teh best prize\",")); | 621 UploadDataContains(",\"originalText\":\"and away teh best prize\",")); |
642 EXPECT_TRUE(UploadDataContains(",\"misspelledStart\":9,")); | 622 EXPECT_TRUE(UploadDataContains(",\"misspelledStart\":9,")); |
643 } | 623 } |
644 | 624 |
645 } // namespace spellcheck | 625 } // namespace spellcheck |
OLD | NEW |