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 "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
55 } | 55 } |
56 return number_of_occurrences; | 56 return number_of_occurrences; |
57 } | 57 } |
58 | 58 |
59 } // namespace | 59 } // namespace |
60 | 60 |
61 // A test fixture to help keep tests simple. | 61 // A test fixture to help keep tests simple. |
62 class FeedbackSenderTest : public testing::Test { | 62 class FeedbackSenderTest : public testing::Test { |
63 public: | 63 public: |
64 FeedbackSenderTest() : ui_thread_(content::BrowserThread::UI, &loop_) { | 64 FeedbackSenderTest() : ui_thread_(content::BrowserThread::UI, &loop_) { |
65 // The command-line switch and the field trial are temporary. | 65 feedback_.reset(new FeedbackSender(NULL, kLanguage, kCountry)); |
66 // TODO(rouslan): Remove the command-line switch and the field trial. | 66 feedback_->StartFeedbackCollection(); |
67 // http://crbug.com/247726 | 67 } |
68 | |
69 virtual ~FeedbackSenderTest() {} | |
70 | |
71 protected: | |
72 // Appends the "--enable-spelling-service-feedback" switch to the | |
73 // command-line. | |
74 void AppendCommandLineSwitch() { | |
75 // The command-line switch is temporary. | |
76 // TODO(rouslan): Remove the command-line switch. http://crbug.com/247726 | |
68 CommandLine::ForCurrentProcess()->AppendSwitch( | 77 CommandLine::ForCurrentProcess()->AppendSwitch( |
69 switches::kEnableSpellingServiceFeedback); | 78 switches::kEnableSpellingServiceFeedback); |
79 feedback_.reset(new FeedbackSender(NULL, kLanguage, kCountry)); | |
80 feedback_->StartFeedbackCollection(); | |
81 } | |
82 | |
83 // Enables the "SpellingServiceFeedback.Enabled" field trial. | |
84 void EnableFieldTrial() { | |
85 // The field trial is temporary. | |
86 // TODO(rouslan): Remove the field trial. http://crbug.com/247726 | |
70 field_trial_list_.reset( | 87 field_trial_list_.reset( |
71 new base::FieldTrialList(new metrics::SHA1EntropyProvider("foo"))); | 88 new base::FieldTrialList(new metrics::SHA1EntropyProvider("foo"))); |
72 field_trial_ = base::FieldTrialList::CreateFieldTrial( | 89 field_trial_ = base::FieldTrialList::CreateFieldTrial( |
73 kFeedbackFieldTrialName, kFeedbackFieldTrialEnabledGroupName); | 90 kFeedbackFieldTrialName, kFeedbackFieldTrialEnabledGroupName); |
74 field_trial_->group(); | 91 field_trial_->group(); |
75 feedback_.reset(new FeedbackSender(NULL, kLanguage, kCountry)); | 92 feedback_.reset(new FeedbackSender(NULL, kLanguage, kCountry)); |
93 feedback_->StartFeedbackCollection(); | |
76 } | 94 } |
77 virtual ~FeedbackSenderTest() {} | |
78 | 95 |
79 protected: | |
80 uint32 AddPendingFeedback() { | 96 uint32 AddPendingFeedback() { |
81 std::vector<SpellCheckResult> results(1, BuildSpellCheckResult()); | 97 std::vector<SpellCheckResult> results(1, BuildSpellCheckResult()); |
82 feedback_->OnSpellcheckResults(kRendererProcessId, | 98 feedback_->OnSpellcheckResults(kRendererProcessId, |
83 UTF8ToUTF16(kText), | 99 UTF8ToUTF16(kText), |
84 std::vector<SpellCheckMarker>(), | 100 std::vector<SpellCheckMarker>(), |
85 &results); | 101 &results); |
86 return results[0].hash; | 102 return results[0].hash; |
87 } | 103 } |
88 | 104 |
89 void ExpireSession() { | 105 void ExpireSession() { |
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
426 "\"suggestionId\":\"42\"," | 442 "\"suggestionId\":\"42\"," |
427 "\"suggestions\":[\"Hello\"]," | 443 "\"suggestions\":[\"Hello\"]," |
428 "\"timestamp\":\"9001\"," | 444 "\"timestamp\":\"9001\"," |
429 "\"userActions\":[{\"actionType\":\"NO_ACTION\"}]}]}}"; | 445 "\"userActions\":[{\"actionType\":\"NO_ACTION\"}]}]}}"; |
430 scoped_ptr<base::Value> expected(base::JSONReader::Read(expected_data)); | 446 scoped_ptr<base::Value> expected(base::JSONReader::Read(expected_data)); |
431 EXPECT_TRUE(expected->Equals(actual.get())) | 447 EXPECT_TRUE(expected->Equals(actual.get())) |
432 << "Expected data: " << expected_data | 448 << "Expected data: " << expected_data |
433 << "\nActual data: " << actual_data; | 449 << "\nActual data: " << actual_data; |
434 } | 450 } |
435 | 451 |
452 // The default API version is "v2". | |
453 TEST_F(FeedbackSenderTest, DefaultApiVersion) { | |
454 AddPendingFeedback(); | |
455 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | |
456 std::vector<uint32>()); | |
457 EXPECT_TRUE(UploadDataContains("\"apiVersion\":\"v2\"")); | |
458 EXPECT_FALSE(UploadDataContains("\"apiVersion\":\"v2-internal\"")); | |
459 } | |
460 | |
461 // The API version should not change for field-trial participants that do not | |
462 // append the command-line switch. | |
463 TEST_F(FeedbackSenderTest, FieldTrialAloneHasSameApiVersion) { | |
464 EnableFieldTrial(); | |
465 | |
466 AddPendingFeedback(); | |
467 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | |
468 std::vector<uint32>()); | |
469 | |
470 EXPECT_TRUE(UploadDataContains("\"apiVersion\":\"v2\"")); | |
471 EXPECT_FALSE(UploadDataContains("\"apiVersion\":\"v2-internal\"")); | |
472 } | |
473 | |
474 // The API version should not change if the command-line switch is appended, but | |
475 // the user is not participating in the field-trial. | |
476 TEST_F(FeedbackSenderTest, CommandLineSwitchAloneHasSameApiVersion) { | |
477 AppendCommandLineSwitch(); | |
478 | |
479 AddPendingFeedback(); | |
480 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | |
481 std::vector<uint32>()); | |
482 | |
483 EXPECT_TRUE(UploadDataContains("\"apiVersion\":\"v2\"")); | |
484 EXPECT_FALSE(UploadDataContains("\"apiVersion\":\"v2-internal\"")); | |
485 } | |
486 | |
487 // The API version should be different for field-trial participants that also | |
488 // append the command-line switch. | |
489 TEST_F(FeedbackSenderTest, InternalApiVersion) { | |
490 AppendCommandLineSwitch(); | |
491 EnableFieldTrial(); | |
492 | |
493 AddPendingFeedback(); | |
494 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | |
495 std::vector<uint32>()); | |
496 | |
497 EXPECT_FALSE(UploadDataContains("\"apiVersion\":\"v2\"")); | |
498 EXPECT_TRUE(UploadDataContains("\"apiVersion\":\"v2-internal\"")); | |
499 } | |
500 | |
436 // Duplicate spellcheck results should be matched to the existing markers. | 501 // Duplicate spellcheck results should be matched to the existing markers. |
437 TEST_F(FeedbackSenderTest, MatchDupliateResultsWithExistingMarkers) { | 502 TEST_F(FeedbackSenderTest, MatchDupliateResultsWithExistingMarkers) { |
438 uint32 hash = AddPendingFeedback(); | 503 uint32 hash = AddPendingFeedback(); |
439 std::vector<SpellCheckResult> results( | 504 std::vector<SpellCheckResult> results( |
440 1, | 505 1, |
441 SpellCheckResult(SpellCheckResult::SPELLING, | 506 SpellCheckResult(SpellCheckResult::SPELLING, |
442 kMisspellingStart, | 507 kMisspellingStart, |
443 kMisspellingLength, | 508 kMisspellingLength, |
444 ASCIIToUTF16("Hello"))); | 509 ASCIIToUTF16("Hello"))); |
445 std::vector<SpellCheckMarker> markers( | 510 std::vector<SpellCheckMarker> markers( |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
517 SpellCheckResult(SpellCheckResult::SPELLING, 2, -1, UTF8ToUTF16("you"))); | 582 SpellCheckResult(SpellCheckResult::SPELLING, 2, -1, UTF8ToUTF16("you"))); |
518 feedback_->OnSpellcheckResults(kRendererProcessId, | 583 feedback_->OnSpellcheckResults(kRendererProcessId, |
519 UTF8ToUTF16(kText), | 584 UTF8ToUTF16(kText), |
520 std::vector<SpellCheckMarker>(), | 585 std::vector<SpellCheckMarker>(), |
521 &results); | 586 &results); |
522 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | 587 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, |
523 std::vector<uint32>()); | 588 std::vector<uint32>()); |
524 EXPECT_FALSE(IsUploadingData()); | 589 EXPECT_FALSE(IsUploadingData()); |
525 } | 590 } |
526 | 591 |
592 // FeedbackSender does not collect and upload feedback when instructed to stop. | |
593 TEST_F(FeedbackSenderTest, CanStopFeedbackCollection) { | |
594 feedback_->StopFeedbackCollection(); | |
595 AddPendingFeedback(); | |
596 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | |
597 std::vector<uint32>()); | |
598 EXPECT_FALSE(IsUploadingData()); | |
599 } | |
600 | |
601 // FeedbackSender resumes collecting and uploading feedback when instructed to | |
602 // start after stopping. | |
603 TEST_F(FeedbackSenderTest, CanReumseFeedbackCollection) { | |
groby-ooo-7-16
2013/11/21 02:48:41
CanResume
please use gerrit instead
2013/11/22 22:06:31
Done.
| |
604 feedback_->StopFeedbackCollection(); | |
605 feedback_->StartFeedbackCollection(); | |
606 AddPendingFeedback(); | |
607 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | |
608 std::vector<uint32>()); | |
609 EXPECT_TRUE(IsUploadingData()); | |
610 } | |
611 | |
612 // FeedbackSender does not collect data while being stopped and upload it later. | |
613 TEST_F(FeedbackSenderTest, NoFeedbackCollectionWhenStopped) { | |
614 feedback_->StopFeedbackCollection(); | |
615 AddPendingFeedback(); | |
616 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | |
617 std::vector<uint32>()); | |
618 feedback_->StartFeedbackCollection(); | |
619 feedback_->OnReceiveDocumentMarkers(kRendererProcessId, | |
620 std::vector<uint32>()); | |
621 EXPECT_FALSE(IsUploadingData()); | |
622 } | |
623 | |
527 } // namespace spellcheck | 624 } // namespace spellcheck |
OLD | NEW |