[email protected] | 4837225 | 2013-12-20 12:18:01 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/feedback/feedback_uploader.h" |
| 6 | |
[email protected] | 90cc2a9a | 2014-02-28 22:05:55 | [diff] [blame] | 7 | #include <set> |
| 8 | |
[email protected] | 4837225 | 2013-12-20 12:18:01 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/message_loop/message_loop.h" |
| 11 | #include "base/run_loop.h" |
[email protected] | 77f7fe89 | 2014-03-18 00:00:06 | [diff] [blame] | 12 | #include "chrome/browser/feedback/feedback_uploader_chrome.h" |
[email protected] | 4837225 | 2013-12-20 12:18:01 | [diff] [blame] | 13 | #include "chrome/browser/feedback/feedback_uploader_factory.h" |
| 14 | #include "chrome/test/base/testing_profile.h" |
| 15 | #include "content/public/test/test_browser_thread.h" |
| 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | const char kReportOne[] = "one"; |
| 21 | const char kReportTwo[] = "two"; |
| 22 | const char kReportThree[] = "three"; |
| 23 | const char kReportFour[] = "four"; |
| 24 | const char kReportFive[] = "five"; |
| 25 | |
| 26 | const base::TimeDelta kRetryDelayForTest = |
| 27 | base::TimeDelta::FromMilliseconds(100); |
| 28 | |
[email protected] | 95003d52 | 2014-03-13 20:22:31 | [diff] [blame] | 29 | KeyedService* CreateFeedbackUploaderService(content::BrowserContext* context) { |
[email protected] | 77f7fe89 | 2014-03-18 00:00:06 | [diff] [blame] | 30 | return new feedback::FeedbackUploaderChrome( |
| 31 | Profile::FromBrowserContext(context)); |
[email protected] | 4837225 | 2013-12-20 12:18:01 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | } // namespace |
| 35 | |
| 36 | namespace feedback { |
| 37 | |
| 38 | class FeedbackUploaderTest : public testing::Test { |
| 39 | protected: |
| 40 | FeedbackUploaderTest() |
| 41 | : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 42 | profile_(new TestingProfile()), |
[email protected] | 90cc2a9a | 2014-02-28 22:05:55 | [diff] [blame] | 43 | dispatched_reports_count_(0), |
[email protected] | 4837225 | 2013-12-20 12:18:01 | [diff] [blame] | 44 | expected_reports_(0) { |
| 45 | FeedbackUploaderFactory::GetInstance()->SetTestingFactory( |
| 46 | profile_.get(), &CreateFeedbackUploaderService); |
| 47 | |
| 48 | uploader_ = FeedbackUploaderFactory::GetForBrowserContext(profile_.get()); |
| 49 | uploader_->setup_for_test( |
| 50 | base::Bind(&FeedbackUploaderTest::MockDispatchReport, |
| 51 | base::Unretained(this)), |
| 52 | kRetryDelayForTest); |
| 53 | } |
| 54 | |
| 55 | virtual ~FeedbackUploaderTest() { |
| 56 | FeedbackUploaderFactory::GetInstance()->SetTestingFactory( |
| 57 | profile_.get(), NULL); |
| 58 | } |
| 59 | |
| 60 | void QueueReport(const std::string& data) { |
[email protected] | 90cc2a9a | 2014-02-28 22:05:55 | [diff] [blame] | 61 | uploader_->QueueReport(data); |
[email protected] | 4837225 | 2013-12-20 12:18:01 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void ReportFailure(const std::string& data) { |
[email protected] | 90cc2a9a | 2014-02-28 22:05:55 | [diff] [blame] | 65 | uploader_->RetryReport(data); |
[email protected] | 4837225 | 2013-12-20 12:18:01 | [diff] [blame] | 66 | } |
| 67 | |
[email protected] | 90cc2a9a | 2014-02-28 22:05:55 | [diff] [blame] | 68 | void MockDispatchReport(const std::string& report_data) { |
| 69 | if (ContainsKey(dispatched_reports_, report_data)) { |
| 70 | dispatched_reports_[report_data]++; |
| 71 | } else { |
| 72 | dispatched_reports_[report_data] = 1; |
| 73 | } |
| 74 | dispatched_reports_count_++; |
[email protected] | 4837225 | 2013-12-20 12:18:01 | [diff] [blame] | 75 | |
| 76 | // Dispatch will always update the timer, whether successful or not, |
| 77 | // simulate the same behavior. |
| 78 | uploader_->UpdateUploadTimer(); |
| 79 | |
[email protected] | 90cc2a9a | 2014-02-28 22:05:55 | [diff] [blame] | 80 | if (ProcessingComplete()) { |
[email protected] | 4837225 | 2013-12-20 12:18:01 | [diff] [blame] | 81 | if (run_loop_.get()) |
| 82 | run_loop_->Quit(); |
| 83 | } |
| 84 | } |
| 85 | |
[email protected] | 90cc2a9a | 2014-02-28 22:05:55 | [diff] [blame] | 86 | bool ProcessingComplete() { |
| 87 | return (dispatched_reports_count_ >= expected_reports_); |
| 88 | } |
| 89 | |
[email protected] | 4837225 | 2013-12-20 12:18:01 | [diff] [blame] | 90 | void RunMessageLoop() { |
[email protected] | 90cc2a9a | 2014-02-28 22:05:55 | [diff] [blame] | 91 | if (ProcessingComplete()) |
| 92 | return; |
[email protected] | 4837225 | 2013-12-20 12:18:01 | [diff] [blame] | 93 | run_loop_.reset(new base::RunLoop()); |
| 94 | run_loop_->Run(); |
| 95 | } |
| 96 | |
| 97 | base::MessageLoop message_loop_; |
| 98 | scoped_ptr<base::RunLoop> run_loop_; |
| 99 | content::TestBrowserThread ui_thread_; |
| 100 | scoped_ptr<TestingProfile> profile_; |
| 101 | |
| 102 | FeedbackUploader* uploader_; |
| 103 | |
[email protected] | 90cc2a9a | 2014-02-28 22:05:55 | [diff] [blame] | 104 | std::map<std::string, unsigned int> dispatched_reports_; |
| 105 | size_t dispatched_reports_count_; |
[email protected] | 4837225 | 2013-12-20 12:18:01 | [diff] [blame] | 106 | size_t expected_reports_; |
| 107 | }; |
| 108 | |
[email protected] | 90cc2a9a | 2014-02-28 22:05:55 | [diff] [blame] | 109 | #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_MACOSX) |
| 110 | #define MAYBE_QueueMultiple QueueMultiple |
| 111 | #else |
| 112 | // crbug.com/330547 |
| 113 | #define MAYBE_QueueMultiple DISABLED_QueueMultiple |
| 114 | #endif |
| 115 | TEST_F(FeedbackUploaderTest, MAYBE_QueueMultiple) { |
[email protected] | 4837225 | 2013-12-20 12:18:01 | [diff] [blame] | 116 | dispatched_reports_.clear(); |
| 117 | QueueReport(kReportOne); |
| 118 | QueueReport(kReportTwo); |
| 119 | QueueReport(kReportThree); |
| 120 | QueueReport(kReportFour); |
| 121 | |
| 122 | EXPECT_EQ(dispatched_reports_.size(), 4u); |
[email protected] | 90cc2a9a | 2014-02-28 22:05:55 | [diff] [blame] | 123 | EXPECT_EQ(dispatched_reports_[kReportOne], 1u); |
| 124 | EXPECT_EQ(dispatched_reports_[kReportTwo], 1u); |
| 125 | EXPECT_EQ(dispatched_reports_[kReportThree], 1u); |
| 126 | EXPECT_EQ(dispatched_reports_[kReportFour], 1u); |
[email protected] | 4837225 | 2013-12-20 12:18:01 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | 9bd534e | 2014-02-24 18:48:47 | [diff] [blame] | 129 | #if defined(OS_WIN) || defined(OS_ANDROID) |
[email protected] | f100711 | 2013-12-23 19:04:16 | [diff] [blame] | 130 | // crbug.com/330547 |
[email protected] | 3f2e9bb | 2014-01-18 08:48:34 | [diff] [blame] | 131 | #define MAYBE_QueueMultipleWithFailures DISABLED_QueueMultipleWithFailures |
[email protected] | 4e0ebba | 2014-02-21 22:32:22 | [diff] [blame] | 132 | #else |
| 133 | #define MAYBE_QueueMultipleWithFailures QueueMultipleWithFailures |
[email protected] | 3f2e9bb | 2014-01-18 08:48:34 | [diff] [blame] | 134 | #endif |
| 135 | TEST_F(FeedbackUploaderTest, MAYBE_QueueMultipleWithFailures) { |
[email protected] | 4837225 | 2013-12-20 12:18:01 | [diff] [blame] | 136 | dispatched_reports_.clear(); |
[email protected] | 90cc2a9a | 2014-02-28 22:05:55 | [diff] [blame] | 137 | |
[email protected] | 4837225 | 2013-12-20 12:18:01 | [diff] [blame] | 138 | QueueReport(kReportOne); |
| 139 | QueueReport(kReportTwo); |
| 140 | QueueReport(kReportThree); |
| 141 | QueueReport(kReportFour); |
| 142 | |
| 143 | ReportFailure(kReportThree); |
| 144 | ReportFailure(kReportTwo); |
| 145 | QueueReport(kReportFive); |
| 146 | |
| 147 | expected_reports_ = 7; |
| 148 | RunMessageLoop(); |
| 149 | |
[email protected] | 90cc2a9a | 2014-02-28 22:05:55 | [diff] [blame] | 150 | EXPECT_EQ(dispatched_reports_.size(), 5u); |
| 151 | EXPECT_EQ(dispatched_reports_[kReportOne], 1u); |
| 152 | EXPECT_EQ(dispatched_reports_[kReportTwo], 2u); |
| 153 | EXPECT_EQ(dispatched_reports_[kReportThree], 2u); |
| 154 | EXPECT_EQ(dispatched_reports_[kReportFour], 1u); |
| 155 | EXPECT_EQ(dispatched_reports_[kReportFive], 1u); |
[email protected] | 4837225 | 2013-12-20 12:18:01 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | } // namespace feedback |