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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 } | 58 } |
59 return number_of_occurrences; | 59 return number_of_occurrences; |
60 } | 60 } |
61 | 61 |
62 } // namespace | 62 } // namespace |
63 | 63 |
64 // A test fixture to help keep tests simple. | 64 // A test fixture to help keep tests simple. |
65 class FeedbackSenderTest : public testing::Test { | 65 class FeedbackSenderTest : public testing::Test { |
66 public: | 66 public: |
67 FeedbackSenderTest() : ui_thread_(content::BrowserThread::UI, &loop_) { | 67 FeedbackSenderTest() : ui_thread_(content::BrowserThread::UI, &loop_) { |
68 feedback_.reset(new FeedbackSender(NULL, kLanguage, kCountry)); | 68 feedback_.reset(new FeedbackSender(nullptr, kLanguage, kCountry)); |
69 feedback_->StartFeedbackCollection(); | 69 feedback_->StartFeedbackCollection(); |
70 } | 70 } |
71 | 71 |
72 ~FeedbackSenderTest() override {} | 72 ~FeedbackSenderTest() override {} |
73 | 73 |
74 protected: | 74 protected: |
75 // Appends the "--enable-spelling-service-feedback" switch to the | 75 // Appends the "--enable-spelling-service-feedback" switch to the |
76 // command-line. | 76 // command-line. |
77 void AppendCommandLineSwitch() { | 77 void AppendCommandLineSwitch() { |
78 // The command-line switch is temporary. | 78 // The command-line switch is temporary. |
79 // TODO(rouslan): Remove the command-line switch. http://crbug.com/247726 | 79 // TODO(rouslan): Remove the command-line switch. http://crbug.com/247726 |
80 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 80 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
81 switches::kEnableSpellingFeedbackFieldTrial); | 81 switches::kEnableSpellingFeedbackFieldTrial); |
82 feedback_.reset(new FeedbackSender(NULL, kLanguage, kCountry)); | 82 feedback_.reset(new FeedbackSender(nullptr, kLanguage, kCountry)); |
83 feedback_->StartFeedbackCollection(); | 83 feedback_->StartFeedbackCollection(); |
84 } | 84 } |
85 | 85 |
86 // Enables the "SpellingServiceFeedback.Enabled" field trial. | 86 // Enables the "SpellingServiceFeedback.Enabled" field trial. |
87 void EnableFieldTrial() { | 87 void EnableFieldTrial() { |
88 // The field trial is temporary. | 88 // The field trial is temporary. |
89 // TODO(rouslan): Remove the field trial. http://crbug.com/247726 | 89 // TODO(rouslan): Remove the field trial. http://crbug.com/247726 |
90 field_trial_list_.reset( | 90 field_trial_list_.reset( |
91 new base::FieldTrialList(new metrics::SHA1EntropyProvider("foo"))); | 91 new base::FieldTrialList(new metrics::SHA1EntropyProvider("foo"))); |
92 field_trial_ = base::FieldTrialList::CreateFieldTrial( | 92 field_trial_ = base::FieldTrialList::CreateFieldTrial( |
93 kFeedbackFieldTrialName, kFeedbackFieldTrialEnabledGroupName); | 93 kFeedbackFieldTrialName, kFeedbackFieldTrialEnabledGroupName); |
94 field_trial_->group(); | 94 field_trial_->group(); |
95 feedback_.reset(new FeedbackSender(NULL, kLanguage, kCountry)); | 95 feedback_.reset(new FeedbackSender(nullptr, kLanguage, kCountry)); |
96 feedback_->StartFeedbackCollection(); | 96 feedback_->StartFeedbackCollection(); |
97 } | 97 } |
98 | 98 |
99 uint32_t AddPendingFeedback() { | 99 uint32_t AddPendingFeedback() { |
100 std::vector<SpellCheckResult> results(1, BuildSpellCheckResult()); | 100 std::vector<SpellCheckResult> results(1, BuildSpellCheckResult()); |
101 feedback_->OnSpellcheckResults(kRendererProcessId, | 101 feedback_->OnSpellcheckResults(kRendererProcessId, |
102 base::UTF8ToUTF16(kText), | 102 base::UTF8ToUTF16(kText), |
103 std::vector<SpellCheckMarker>(), | 103 std::vector<SpellCheckMarker>(), |
104 &results); | 104 &results); |
105 return results[0].hash; | 105 return results[0].hash; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 } | 189 } |
190 | 190 |
191 // Send SELECT feedback message if the user has selected a spelling suggestion. | 191 // Send SELECT feedback message if the user has selected a spelling suggestion. |
192 TEST_F(FeedbackSenderTest, SelectFeedback) { | 192 TEST_F(FeedbackSenderTest, SelectFeedback) { |
193 uint32_t hash = AddPendingFeedback(); | 193 uint32_t hash = AddPendingFeedback(); |
194 static const int kSuggestionIndex = 0; | 194 static const int kSuggestionIndex = 0; |
195 feedback_->SelectedSuggestion(hash, kSuggestionIndex); | 195 feedback_->SelectedSuggestion(hash, kSuggestionIndex); |
196 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | 196 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, |
197 std::vector<uint32_t>()); | 197 std::vector<uint32_t>()); |
198 EXPECT_TRUE(UploadDataContains("\"actionType\":\"SELECT\"")); | 198 EXPECT_TRUE(UploadDataContains("\"actionType\":\"SELECT\"")); |
199 EXPECT_TRUE(UploadDataContains("\"actionTargetIndex\":" + kSuggestionIndex)); | 199 EXPECT_TRUE(UploadDataContains("\"actionTargetIndex\":" + |
| 200 base::StringPrintf("%d", kSuggestionIndex))); |
200 } | 201 } |
201 | 202 |
202 // Send ADD_TO_DICT feedback message if the user has added the misspelled word | 203 // Send ADD_TO_DICT feedback message if the user has added the misspelled word |
203 // to the custom dictionary. | 204 // to the custom dictionary. |
204 TEST_F(FeedbackSenderTest, AddToDictFeedback) { | 205 TEST_F(FeedbackSenderTest, AddToDictFeedback) { |
205 uint32_t hash = AddPendingFeedback(); | 206 uint32_t hash = AddPendingFeedback(); |
206 feedback_->AddedToDictionary(hash); | 207 feedback_->AddedToDictionary(hash); |
207 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | 208 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, |
208 std::vector<uint32_t>()); | 209 std::vector<uint32_t>()); |
209 EXPECT_TRUE(UploadDataContains("\"actionType\":\"ADD_TO_DICT\"")); | 210 EXPECT_TRUE(UploadDataContains("\"actionType\":\"ADD_TO_DICT\"")); |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 | 416 |
416 // The field names and types should correspond to the API. | 417 // The field names and types should correspond to the API. |
417 TEST_F(FeedbackSenderTest, FeedbackAPI) { | 418 TEST_F(FeedbackSenderTest, FeedbackAPI) { |
418 AddPendingFeedback(); | 419 AddPendingFeedback(); |
419 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | 420 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, |
420 std::vector<uint32_t>()); | 421 std::vector<uint32_t>()); |
421 std::string actual_data = GetUploadData(); | 422 std::string actual_data = GetUploadData(); |
422 scoped_ptr<base::DictionaryValue> actual(static_cast<base::DictionaryValue*>( | 423 scoped_ptr<base::DictionaryValue> actual(static_cast<base::DictionaryValue*>( |
423 base::JSONReader::Read(actual_data).release())); | 424 base::JSONReader::Read(actual_data).release())); |
424 actual->SetString("params.key", "TestDummyKey"); | 425 actual->SetString("params.key", "TestDummyKey"); |
425 base::ListValue* suggestions = NULL; | 426 base::ListValue* suggestions = nullptr; |
426 actual->GetList("params.suggestionInfo", &suggestions); | 427 actual->GetList("params.suggestionInfo", &suggestions); |
427 base::DictionaryValue* suggestion = NULL; | 428 base::DictionaryValue* suggestion = nullptr; |
428 suggestions->GetDictionary(0, &suggestion); | 429 suggestions->GetDictionary(0, &suggestion); |
429 suggestion->SetString("suggestionId", "42"); | 430 suggestion->SetString("suggestionId", "42"); |
430 suggestion->SetString("timestamp", "9001"); | 431 suggestion->SetString("timestamp", "9001"); |
431 static const std::string expected_data = | 432 static const std::string expected_data = |
432 "{\"apiVersion\":\"v2\"," | 433 "{\"apiVersion\":\"v2\"," |
433 "\"method\":\"spelling.feedback\"," | 434 "\"method\":\"spelling.feedback\"," |
434 "\"params\":" | 435 "\"params\":" |
435 "{\"clientName\":\"Chrome\"," | 436 "{\"clientName\":\"Chrome\"," |
436 "\"originCountry\":\"USA\"," | 437 "\"originCountry\":\"USA\"," |
437 "\"key\":\"TestDummyKey\"," | 438 "\"key\":\"TestDummyKey\"," |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
636 "the chance to work hard at work worth doing."), | 637 "the chance to work hard at work worth doing."), |
637 std::vector<SpellCheckMarker>(), &results); | 638 std::vector<SpellCheckMarker>(), &results); |
638 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | 639 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, |
639 std::vector<uint32_t>()); | 640 std::vector<uint32_t>()); |
640 EXPECT_TRUE( | 641 EXPECT_TRUE( |
641 UploadDataContains(",\"originalText\":\"and away teh best prize\",")); | 642 UploadDataContains(",\"originalText\":\"and away teh best prize\",")); |
642 EXPECT_TRUE(UploadDataContains(",\"misspelledStart\":9,")); | 643 EXPECT_TRUE(UploadDataContains(",\"misspelledStart\":9,")); |
643 } | 644 } |
644 | 645 |
645 } // namespace spellcheck | 646 } // namespace spellcheck |
OLD | NEW |