blob: ab5806559a02f7a618f9eb63f16377fe48e7614e [file] [log] [blame]
[email protected]48372252013-12-20 12:18:011// 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]90cc2a9a2014-02-28 22:05:557#include <set>
8
[email protected]48372252013-12-20 12:18:019#include "base/bind.h"
10#include "base/message_loop/message_loop.h"
11#include "base/run_loop.h"
12#include "chrome/browser/feedback/feedback_uploader_factory.h"
13#include "chrome/test/base/testing_profile.h"
14#include "content/public/test/test_browser_thread.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17namespace {
18
19const char kReportOne[] = "one";
20const char kReportTwo[] = "two";
21const char kReportThree[] = "three";
22const char kReportFour[] = "four";
23const char kReportFive[] = "five";
24
25const base::TimeDelta kRetryDelayForTest =
26 base::TimeDelta::FromMilliseconds(100);
27
28BrowserContextKeyedService* CreateFeedbackUploaderService(
29 content::BrowserContext* context) {
30 return new feedback::FeedbackUploader(Profile::FromBrowserContext(context));
31}
32
33} // namespace
34
35namespace feedback {
36
37class FeedbackUploaderTest : public testing::Test {
38 protected:
39 FeedbackUploaderTest()
40 : ui_thread_(content::BrowserThread::UI, &message_loop_),
41 profile_(new TestingProfile()),
[email protected]90cc2a9a2014-02-28 22:05:5542 dispatched_reports_count_(0),
[email protected]48372252013-12-20 12:18:0143 expected_reports_(0) {
44 FeedbackUploaderFactory::GetInstance()->SetTestingFactory(
45 profile_.get(), &CreateFeedbackUploaderService);
46
47 uploader_ = FeedbackUploaderFactory::GetForBrowserContext(profile_.get());
48 uploader_->setup_for_test(
49 base::Bind(&FeedbackUploaderTest::MockDispatchReport,
50 base::Unretained(this)),
51 kRetryDelayForTest);
52 }
53
54 virtual ~FeedbackUploaderTest() {
55 FeedbackUploaderFactory::GetInstance()->SetTestingFactory(
56 profile_.get(), NULL);
57 }
58
59 void QueueReport(const std::string& data) {
[email protected]90cc2a9a2014-02-28 22:05:5560 uploader_->QueueReport(data);
[email protected]48372252013-12-20 12:18:0161 }
62
63 void ReportFailure(const std::string& data) {
[email protected]90cc2a9a2014-02-28 22:05:5564 uploader_->RetryReport(data);
[email protected]48372252013-12-20 12:18:0165 }
66
[email protected]90cc2a9a2014-02-28 22:05:5567 void MockDispatchReport(const std::string& report_data) {
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_++;
[email protected]48372252013-12-20 12:18:0174
75 // Dispatch will always update the timer, whether successful or not,
76 // simulate the same behavior.
77 uploader_->UpdateUploadTimer();
78
[email protected]90cc2a9a2014-02-28 22:05:5579 if (ProcessingComplete()) {
[email protected]48372252013-12-20 12:18:0180 if (run_loop_.get())
81 run_loop_->Quit();
82 }
83 }
84
[email protected]90cc2a9a2014-02-28 22:05:5585 bool ProcessingComplete() {
86 return (dispatched_reports_count_ >= expected_reports_);
87 }
88
[email protected]48372252013-12-20 12:18:0189 void RunMessageLoop() {
[email protected]90cc2a9a2014-02-28 22:05:5590 if (ProcessingComplete())
91 return;
[email protected]48372252013-12-20 12:18:0192 run_loop_.reset(new base::RunLoop());
93 run_loop_->Run();
94 }
95
96 base::MessageLoop message_loop_;
97 scoped_ptr<base::RunLoop> run_loop_;
98 content::TestBrowserThread ui_thread_;
99 scoped_ptr<TestingProfile> profile_;
100
101 FeedbackUploader* uploader_;
102
[email protected]90cc2a9a2014-02-28 22:05:55103 std::map<std::string, unsigned int> dispatched_reports_;
104 size_t dispatched_reports_count_;
[email protected]48372252013-12-20 12:18:01105 size_t expected_reports_;
106};
107
[email protected]90cc2a9a2014-02-28 22:05:55108#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
114TEST_F(FeedbackUploaderTest, MAYBE_QueueMultiple) {
[email protected]48372252013-12-20 12:18:01115 dispatched_reports_.clear();
116 QueueReport(kReportOne);
117 QueueReport(kReportTwo);
118 QueueReport(kReportThree);
119 QueueReport(kReportFour);
120
121 EXPECT_EQ(dispatched_reports_.size(), 4u);
[email protected]90cc2a9a2014-02-28 22:05:55122 EXPECT_EQ(dispatched_reports_[kReportOne], 1u);
123 EXPECT_EQ(dispatched_reports_[kReportTwo], 1u);
124 EXPECT_EQ(dispatched_reports_[kReportThree], 1u);
125 EXPECT_EQ(dispatched_reports_[kReportFour], 1u);
[email protected]48372252013-12-20 12:18:01126}
127
[email protected]9bd534e2014-02-24 18:48:47128#if defined(OS_WIN) || defined(OS_ANDROID)
[email protected]f1007112013-12-23 19:04:16129// crbug.com/330547
[email protected]3f2e9bb2014-01-18 08:48:34130#define MAYBE_QueueMultipleWithFailures DISABLED_QueueMultipleWithFailures
[email protected]4e0ebba2014-02-21 22:32:22131#else
132#define MAYBE_QueueMultipleWithFailures QueueMultipleWithFailures
[email protected]3f2e9bb2014-01-18 08:48:34133#endif
134TEST_F(FeedbackUploaderTest, MAYBE_QueueMultipleWithFailures) {
[email protected]48372252013-12-20 12:18:01135 dispatched_reports_.clear();
[email protected]90cc2a9a2014-02-28 22:05:55136
[email protected]48372252013-12-20 12:18:01137 QueueReport(kReportOne);
138 QueueReport(kReportTwo);
139 QueueReport(kReportThree);
140 QueueReport(kReportFour);
141
142 ReportFailure(kReportThree);
143 ReportFailure(kReportTwo);
144 QueueReport(kReportFive);
145
146 expected_reports_ = 7;
147 RunMessageLoop();
148
[email protected]90cc2a9a2014-02-28 22:05:55149 EXPECT_EQ(dispatched_reports_.size(), 5u);
150 EXPECT_EQ(dispatched_reports_[kReportOne], 1u);
151 EXPECT_EQ(dispatched_reports_[kReportTwo], 2u);
152 EXPECT_EQ(dispatched_reports_[kReportThree], 2u);
153 EXPECT_EQ(dispatched_reports_[kReportFour], 1u);
154 EXPECT_EQ(dispatched_reports_[kReportFive], 1u);
[email protected]48372252013-12-20 12:18:01155}
156
157} // namespace feedback