OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" |
| 10 #include "chrome/browser/feedback/feedback_uploader_factory.h" |
| 11 #include "content/public/test/test_browser_context.h" |
| 12 #include "content/public/test/test_browser_thread.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 const char kReportOne[] = "one"; |
| 18 const char kReportTwo[] = "two"; |
| 19 const char kReportThree[] = "three"; |
| 20 const char kReportFour[] = "four"; |
| 21 const char kReportFive[] = "five"; |
| 22 |
| 23 const base::TimeDelta kRetryDelayForTest = |
| 24 base::TimeDelta::FromMilliseconds(100); |
| 25 |
| 26 } // namespace |
| 27 |
| 28 namespace feedback { |
| 29 |
| 30 class FeedbackUploaderTest : public testing::Test { |
| 31 protected: |
| 32 FeedbackUploaderTest() |
| 33 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 34 expected_reports_(0) { |
| 35 uploader_ = FeedbackUploaderFactory::GetForBrowserContext(&context_); |
| 36 uploader_->setup_for_test( |
| 37 base::Bind(&FeedbackUploaderTest::MockDispatchReport, |
| 38 base::Unretained(this)), |
| 39 kRetryDelayForTest); |
| 40 } |
| 41 |
| 42 virtual ~FeedbackUploaderTest() {} |
| 43 |
| 44 void QueueReport(const std::string& data) { |
| 45 uploader_->QueueReport(make_scoped_ptr(new std::string(data))); |
| 46 } |
| 47 |
| 48 void ReportFailure(const std::string& data) { |
| 49 uploader_->RetryReport(make_scoped_ptr(new std::string(data))); |
| 50 } |
| 51 |
| 52 void MockDispatchReport(scoped_ptr<std::string> report_data) { |
| 53 dispatched_reports_.push_back(*report_data.get()); |
| 54 |
| 55 // Dispatch will always update the timer, whether successful or not, |
| 56 // simulate the same behavior. |
| 57 uploader_->UpdateUploadTimer(); |
| 58 |
| 59 if (dispatched_reports_.size() >= expected_reports_) { |
| 60 if (run_loop_.get()) |
| 61 run_loop_->Quit(); |
| 62 } |
| 63 } |
| 64 |
| 65 void RunMessageLoop() { |
| 66 run_loop_.reset(new base::RunLoop()); |
| 67 run_loop_->Run(); |
| 68 } |
| 69 |
| 70 base::MessageLoop message_loop_; |
| 71 scoped_ptr<base::RunLoop> run_loop_; |
| 72 content::TestBrowserContext context_; |
| 73 FeedbackUploader* uploader_; |
| 74 content::TestBrowserThread ui_thread_; |
| 75 |
| 76 std::vector<std::string> dispatched_reports_; |
| 77 uint expected_reports_; |
| 78 }; |
| 79 |
| 80 TEST_F(FeedbackUploaderTest, QueueMultiple) { |
| 81 dispatched_reports_.clear(); |
| 82 QueueReport(kReportOne); |
| 83 QueueReport(kReportTwo); |
| 84 QueueReport(kReportThree); |
| 85 QueueReport(kReportFour); |
| 86 |
| 87 EXPECT_EQ(dispatched_reports_.size(), 4u); |
| 88 EXPECT_EQ(dispatched_reports_[0], kReportOne); |
| 89 EXPECT_EQ(dispatched_reports_[1], kReportTwo); |
| 90 EXPECT_EQ(dispatched_reports_[2], kReportThree); |
| 91 EXPECT_EQ(dispatched_reports_[3], kReportFour); |
| 92 } |
| 93 |
| 94 TEST_F(FeedbackUploaderTest, QueueMultipleWithFailures) { |
| 95 dispatched_reports_.clear(); |
| 96 QueueReport(kReportOne); |
| 97 QueueReport(kReportTwo); |
| 98 QueueReport(kReportThree); |
| 99 QueueReport(kReportFour); |
| 100 |
| 101 ReportFailure(kReportThree); |
| 102 ReportFailure(kReportTwo); |
| 103 QueueReport(kReportFive); |
| 104 |
| 105 expected_reports_ = 7; |
| 106 RunMessageLoop(); |
| 107 |
| 108 EXPECT_EQ(dispatched_reports_.size(), 7u); |
| 109 EXPECT_EQ(dispatched_reports_[0], kReportOne); |
| 110 EXPECT_EQ(dispatched_reports_[1], kReportTwo); |
| 111 EXPECT_EQ(dispatched_reports_[2], kReportThree); |
| 112 EXPECT_EQ(dispatched_reports_[3], kReportFour); |
| 113 EXPECT_EQ(dispatched_reports_[4], kReportFive); |
| 114 EXPECT_EQ(dispatched_reports_[5], kReportThree); |
| 115 EXPECT_EQ(dispatched_reports_[6], kReportTwo); |
| 116 } |
| 117 |
| 118 } // namespace feedback |
OLD | NEW |