Chromium Code Reviews| OLD | NEW | 
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 #include "chrome/browser/feedback/feedback_uploader.h" | 5 #include "chrome/browser/feedback/feedback_uploader.h" | 
| 6 | 6 | 
| 7 #include <set> | |
| 8 | |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" | 
| 8 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" | 
| 9 #include "base/run_loop.h" | 11 #include "base/run_loop.h" | 
| 10 #include "chrome/browser/feedback/feedback_uploader_factory.h" | 12 #include "chrome/browser/feedback/feedback_uploader_factory.h" | 
| 11 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" | 
| 12 #include "content/public/test/test_browser_thread.h" | 14 #include "content/public/test/test_browser_thread.h" | 
| 13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" | 
| 14 | 16 | 
| 15 namespace { | 17 namespace { | 
| 16 | 18 | 
| (...skipping 13 matching lines...) Expand all Loading... | |
| 30 | 32 | 
| 31 } // namespace | 33 } // namespace | 
| 32 | 34 | 
| 33 namespace feedback { | 35 namespace feedback { | 
| 34 | 36 | 
| 35 class FeedbackUploaderTest : public testing::Test { | 37 class FeedbackUploaderTest : public testing::Test { | 
| 36 protected: | 38 protected: | 
| 37 FeedbackUploaderTest() | 39 FeedbackUploaderTest() | 
| 38 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 40 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 
| 39 profile_(new TestingProfile()), | 41 profile_(new TestingProfile()), | 
| 42 dispatched_reports_count_(0), | |
| 40 expected_reports_(0) { | 43 expected_reports_(0) { | 
| 41 FeedbackUploaderFactory::GetInstance()->SetTestingFactory( | 44 FeedbackUploaderFactory::GetInstance()->SetTestingFactory( | 
| 42 profile_.get(), &CreateFeedbackUploaderService); | 45 profile_.get(), &CreateFeedbackUploaderService); | 
| 43 | 46 | 
| 44 uploader_ = FeedbackUploaderFactory::GetForBrowserContext(profile_.get()); | 47 uploader_ = FeedbackUploaderFactory::GetForBrowserContext(profile_.get()); | 
| 45 uploader_->setup_for_test( | 48 uploader_->setup_for_test( | 
| 46 base::Bind(&FeedbackUploaderTest::MockDispatchReport, | 49 base::Bind(&FeedbackUploaderTest::MockDispatchReport, | 
| 47 base::Unretained(this)), | 50 base::Unretained(this)), | 
| 48 kRetryDelayForTest); | 51 kRetryDelayForTest); | 
| 49 } | 52 } | 
| 50 | 53 | 
| 51 virtual ~FeedbackUploaderTest() { | 54 virtual ~FeedbackUploaderTest() { | 
| 52 FeedbackUploaderFactory::GetInstance()->SetTestingFactory( | 55 FeedbackUploaderFactory::GetInstance()->SetTestingFactory( | 
| 53 profile_.get(), NULL); | 56 profile_.get(), NULL); | 
| 54 } | 57 } | 
| 55 | 58 | 
| 56 void QueueReport(const std::string& data) { | 59 void QueueReport(const std::string& data) { | 
| 57 uploader_->QueueReport(make_scoped_ptr(new std::string(data))); | 60 uploader_->QueueReport(data); | 
| 58 } | 61 } | 
| 59 | 62 | 
| 60 void ReportFailure(const std::string& data) { | 63 void ReportFailure(const std::string& data) { | 
| 61 uploader_->RetryReport(make_scoped_ptr(new std::string(data))); | 64 uploader_->RetryReport(data); | 
| 62 } | 65 } | 
| 63 | 66 | 
| 64 void MockDispatchReport(scoped_ptr<std::string> report_data) { | 67 void MockDispatchReport(const std::string& report_data) { | 
| 65 dispatched_reports_.push_back(*report_data.get()); | 68 if (dispatched_reports_.count(report_data) == 0) { | 
| 
 
Lei Zhang
2014/02/03 22:29:41
if (ContainsKey(dispatched_reports_, report_data))
 
rkc
2014/02/03 22:41:27
Done.
 
 | |
| 69 dispatched_reports_[report_data] = 1; | |
| 70 } else { | |
| 71 dispatched_reports_[report_data]++; | |
| 72 } | |
| 73 dispatched_reports_count_++; | |
| 66 | 74 | 
| 67 // Dispatch will always update the timer, whether successful or not, | 75 // Dispatch will always update the timer, whether successful or not, | 
| 68 // simulate the same behavior. | 76 // simulate the same behavior. | 
| 69 uploader_->UpdateUploadTimer(); | 77 uploader_->UpdateUploadTimer(); | 
| 70 | 78 | 
| 71 if (dispatched_reports_.size() >= expected_reports_) { | 79 if (ProcessingComplete()) { | 
| 72 if (run_loop_.get()) | 80 if (run_loop_.get()) | 
| 73 run_loop_->Quit(); | 81 run_loop_->Quit(); | 
| 74 } | 82 } | 
| 75 } | 83 } | 
| 76 | 84 | 
| 85 bool ProcessingComplete() { | |
| 86 if (dispatched_reports_count_ >= expected_reports_) | |
| 
 
Lei Zhang
2014/02/03 22:29:41
return (dispatched_reports_count_ >= expected_repo
 
rkc
2014/02/03 22:41:27
Done.
 
 | |
| 87 return true; | |
| 88 return false; | |
| 89 } | |
| 90 | |
| 77 void RunMessageLoop() { | 91 void RunMessageLoop() { | 
| 92 if (ProcessingComplete()) | |
| 93 return; | |
| 78 run_loop_.reset(new base::RunLoop()); | 94 run_loop_.reset(new base::RunLoop()); | 
| 79 run_loop_->Run(); | 95 run_loop_->Run(); | 
| 80 } | 96 } | 
| 81 | 97 | 
| 82 base::MessageLoop message_loop_; | 98 base::MessageLoop message_loop_; | 
| 83 scoped_ptr<base::RunLoop> run_loop_; | 99 scoped_ptr<base::RunLoop> run_loop_; | 
| 84 content::TestBrowserThread ui_thread_; | 100 content::TestBrowserThread ui_thread_; | 
| 85 scoped_ptr<TestingProfile> profile_; | 101 scoped_ptr<TestingProfile> profile_; | 
| 86 | 102 | 
| 87 FeedbackUploader* uploader_; | 103 FeedbackUploader* uploader_; | 
| 88 | 104 | 
| 89 std::vector<std::string> dispatched_reports_; | 105 std::map<std::string, unsigned int> dispatched_reports_; | 
| 106 size_t dispatched_reports_count_; | |
| 90 size_t expected_reports_; | 107 size_t expected_reports_; | 
| 91 }; | 108 }; | 
| 92 | 109 | 
| 93 TEST_F(FeedbackUploaderTest, QueueMultiple) { | 110 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_MACOSX) | 
| 111 #define MAYBE_QueueMultiple QueueMultiple | |
| 112 #else | |
| 113 // crbug.com/330547 | |
| 114 #define MAYBE_QueueMultiple DISABLED_QueueMultiple | |
| 115 #endif | |
| 116 TEST_F(FeedbackUploaderTest, MAYBE_QueueMultiple) { | |
| 94 dispatched_reports_.clear(); | 117 dispatched_reports_.clear(); | 
| 95 QueueReport(kReportOne); | 118 QueueReport(kReportOne); | 
| 96 QueueReport(kReportTwo); | 119 QueueReport(kReportTwo); | 
| 97 QueueReport(kReportThree); | 120 QueueReport(kReportThree); | 
| 98 QueueReport(kReportFour); | 121 QueueReport(kReportFour); | 
| 99 | 122 | 
| 100 EXPECT_EQ(dispatched_reports_.size(), 4u); | 123 EXPECT_EQ(dispatched_reports_.size(), 4u); | 
| 101 EXPECT_EQ(dispatched_reports_[0], kReportOne); | 124 EXPECT_EQ(dispatched_reports_[kReportOne], 1u); | 
| 102 EXPECT_EQ(dispatched_reports_[1], kReportTwo); | 125 EXPECT_EQ(dispatched_reports_[kReportTwo], 1u); | 
| 103 EXPECT_EQ(dispatched_reports_[2], kReportThree); | 126 EXPECT_EQ(dispatched_reports_[kReportThree], 1u); | 
| 104 EXPECT_EQ(dispatched_reports_[3], kReportFour); | 127 EXPECT_EQ(dispatched_reports_[kReportFour], 1u); | 
| 105 } | 128 } | 
| 106 | 129 | 
| 107 #if defined(OS_WIN) | 130 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_MACOSX) | 
| 131 #define MAYBE_QueueMultipleWithFailures QueueMultipleWithFailures | |
| 132 #else | |
| 108 // crbug.com/330547 | 133 // crbug.com/330547 | 
| 109 #define MAYBE_QueueMultipleWithFailures DISABLED_QueueMultipleWithFailures | 134 #define MAYBE_QueueMultipleWithFailures DISABLED_QueueMultipleWithFailures | 
| 110 #else | |
| 111 #define MAYBE_QueueMultipleWithFailures QueueMultipleWithFailures | |
| 112 #endif | 135 #endif | 
| 113 TEST_F(FeedbackUploaderTest, MAYBE_QueueMultipleWithFailures) { | 136 TEST_F(FeedbackUploaderTest, MAYBE_QueueMultipleWithFailures) { | 
| 114 dispatched_reports_.clear(); | 137 dispatched_reports_.clear(); | 
| 138 | |
| 115 QueueReport(kReportOne); | 139 QueueReport(kReportOne); | 
| 116 QueueReport(kReportTwo); | 140 QueueReport(kReportTwo); | 
| 117 QueueReport(kReportThree); | 141 QueueReport(kReportThree); | 
| 118 QueueReport(kReportFour); | 142 QueueReport(kReportFour); | 
| 119 | 143 | 
| 120 ReportFailure(kReportThree); | 144 ReportFailure(kReportThree); | 
| 121 ReportFailure(kReportTwo); | 145 ReportFailure(kReportTwo); | 
| 122 QueueReport(kReportFive); | 146 QueueReport(kReportFive); | 
| 123 | 147 | 
| 124 expected_reports_ = 7; | 148 expected_reports_ = 7; | 
| 125 RunMessageLoop(); | 149 RunMessageLoop(); | 
| 126 | 150 | 
| 127 EXPECT_EQ(dispatched_reports_.size(), 7u); | 151 EXPECT_EQ(dispatched_reports_.size(), 5u); | 
| 128 EXPECT_EQ(dispatched_reports_[0], kReportOne); | 152 EXPECT_EQ(dispatched_reports_[kReportOne], 1u); | 
| 129 EXPECT_EQ(dispatched_reports_[1], kReportTwo); | 153 EXPECT_EQ(dispatched_reports_[kReportTwo], 2u); | 
| 130 EXPECT_EQ(dispatched_reports_[2], kReportThree); | 154 EXPECT_EQ(dispatched_reports_[kReportThree], 2u); | 
| 131 EXPECT_EQ(dispatched_reports_[3], kReportFour); | 155 EXPECT_EQ(dispatched_reports_[kReportFour], 1u); | 
| 132 EXPECT_EQ(dispatched_reports_[4], kReportFive); | 156 EXPECT_EQ(dispatched_reports_[kReportFive], 1u); | 
| 133 EXPECT_EQ(dispatched_reports_[5], kReportThree); | |
| 134 EXPECT_EQ(dispatched_reports_[6], kReportTwo); | |
| 135 } | 157 } | 
| 136 | 158 | 
| 137 } // namespace feedback | 159 } // namespace feedback | 
| OLD | NEW |