| 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 (ContainsKey(dispatched_reports_, report_data)) { |
| 69 dispatched_reports_[report_data]++; |
| 70 } else { |
| 71 dispatched_reports_[report_data] = 1; |
| 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 return (dispatched_reports_count_ >= expected_reports_); |
| 87 } |
| 88 |
| 77 void RunMessageLoop() { | 89 void RunMessageLoop() { |
| 90 if (ProcessingComplete()) |
| 91 return; |
| 78 run_loop_.reset(new base::RunLoop()); | 92 run_loop_.reset(new base::RunLoop()); |
| 79 run_loop_->Run(); | 93 run_loop_->Run(); |
| 80 } | 94 } |
| 81 | 95 |
| 82 base::MessageLoop message_loop_; | 96 base::MessageLoop message_loop_; |
| 83 scoped_ptr<base::RunLoop> run_loop_; | 97 scoped_ptr<base::RunLoop> run_loop_; |
| 84 content::TestBrowserThread ui_thread_; | 98 content::TestBrowserThread ui_thread_; |
| 85 scoped_ptr<TestingProfile> profile_; | 99 scoped_ptr<TestingProfile> profile_; |
| 86 | 100 |
| 87 FeedbackUploader* uploader_; | 101 FeedbackUploader* uploader_; |
| 88 | 102 |
| 89 std::vector<std::string> dispatched_reports_; | 103 std::map<std::string, unsigned int> dispatched_reports_; |
| 104 size_t dispatched_reports_count_; |
| 90 size_t expected_reports_; | 105 size_t expected_reports_; |
| 91 }; | 106 }; |
| 92 | 107 |
| 93 TEST_F(FeedbackUploaderTest, QueueMultiple) { | 108 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_MACOSX) |
| 109 #define MAYBE_QueueMultiple QueueMultiple |
| 110 #else |
| 111 // crbug.com/330547 |
| 112 #define MAYBE_QueueMultiple DISABLED_QueueMultiple |
| 113 #endif |
| 114 TEST_F(FeedbackUploaderTest, MAYBE_QueueMultiple) { |
| 94 dispatched_reports_.clear(); | 115 dispatched_reports_.clear(); |
| 95 QueueReport(kReportOne); | 116 QueueReport(kReportOne); |
| 96 QueueReport(kReportTwo); | 117 QueueReport(kReportTwo); |
| 97 QueueReport(kReportThree); | 118 QueueReport(kReportThree); |
| 98 QueueReport(kReportFour); | 119 QueueReport(kReportFour); |
| 99 | 120 |
| 100 EXPECT_EQ(dispatched_reports_.size(), 4u); | 121 EXPECT_EQ(dispatched_reports_.size(), 4u); |
| 101 EXPECT_EQ(dispatched_reports_[0], kReportOne); | 122 EXPECT_EQ(dispatched_reports_[kReportOne], 1u); |
| 102 EXPECT_EQ(dispatched_reports_[1], kReportTwo); | 123 EXPECT_EQ(dispatched_reports_[kReportTwo], 1u); |
| 103 EXPECT_EQ(dispatched_reports_[2], kReportThree); | 124 EXPECT_EQ(dispatched_reports_[kReportThree], 1u); |
| 104 EXPECT_EQ(dispatched_reports_[3], kReportFour); | 125 EXPECT_EQ(dispatched_reports_[kReportFour], 1u); |
| 105 } | 126 } |
| 106 | 127 |
| 107 #if defined(OS_WIN) | 128 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_MACOSX) |
| 129 #define MAYBE_QueueMultipleWithFailures QueueMultipleWithFailures |
| 130 #else |
| 108 // crbug.com/330547 | 131 // crbug.com/330547 |
| 109 #define MAYBE_QueueMultipleWithFailures DISABLED_QueueMultipleWithFailures | 132 #define MAYBE_QueueMultipleWithFailures DISABLED_QueueMultipleWithFailures |
| 110 #else | |
| 111 #define MAYBE_QueueMultipleWithFailures QueueMultipleWithFailures | |
| 112 #endif | 133 #endif |
| 113 TEST_F(FeedbackUploaderTest, MAYBE_QueueMultipleWithFailures) { | 134 TEST_F(FeedbackUploaderTest, MAYBE_QueueMultipleWithFailures) { |
| 114 dispatched_reports_.clear(); | 135 dispatched_reports_.clear(); |
| 136 |
| 115 QueueReport(kReportOne); | 137 QueueReport(kReportOne); |
| 116 QueueReport(kReportTwo); | 138 QueueReport(kReportTwo); |
| 117 QueueReport(kReportThree); | 139 QueueReport(kReportThree); |
| 118 QueueReport(kReportFour); | 140 QueueReport(kReportFour); |
| 119 | 141 |
| 120 ReportFailure(kReportThree); | 142 ReportFailure(kReportThree); |
| 121 ReportFailure(kReportTwo); | 143 ReportFailure(kReportTwo); |
| 122 QueueReport(kReportFive); | 144 QueueReport(kReportFive); |
| 123 | 145 |
| 124 expected_reports_ = 7; | 146 expected_reports_ = 7; |
| 125 RunMessageLoop(); | 147 RunMessageLoop(); |
| 126 | 148 |
| 127 EXPECT_EQ(dispatched_reports_.size(), 7u); | 149 EXPECT_EQ(dispatched_reports_.size(), 5u); |
| 128 EXPECT_EQ(dispatched_reports_[0], kReportOne); | 150 EXPECT_EQ(dispatched_reports_[kReportOne], 1u); |
| 129 EXPECT_EQ(dispatched_reports_[1], kReportTwo); | 151 EXPECT_EQ(dispatched_reports_[kReportTwo], 2u); |
| 130 EXPECT_EQ(dispatched_reports_[2], kReportThree); | 152 EXPECT_EQ(dispatched_reports_[kReportThree], 2u); |
| 131 EXPECT_EQ(dispatched_reports_[3], kReportFour); | 153 EXPECT_EQ(dispatched_reports_[kReportFour], 1u); |
| 132 EXPECT_EQ(dispatched_reports_[4], kReportFive); | 154 EXPECT_EQ(dispatched_reports_[kReportFive], 1u); |
| 133 EXPECT_EQ(dispatched_reports_[5], kReportThree); | |
| 134 EXPECT_EQ(dispatched_reports_[6], kReportTwo); | |
| 135 } | 155 } |
| 136 | 156 |
| 137 } // namespace feedback | 157 } // namespace feedback |
| OLD | NEW |