1 // Copyright 2014 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.
5 #include "components/feedback/feedback_uploader.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/prefs/testing_pref_service.h"
13 #include "base/run_loop.h"
14 #include "base/stl_util.h"
15 #include "components/feedback/feedback_uploader_chrome.h"
16 #include "components/feedback/feedback_uploader_factory.h"
17 #include "components/user_prefs/user_prefs.h"
18 #include "content/public/test/test_browser_context.h"
19 #include "content/public/test/test_browser_thread.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
25 const char kReportOne
[] = "one";
26 const char kReportTwo
[] = "two";
27 const char kReportThree
[] = "three";
28 const char kReportFour
[] = "four";
29 const char kReportFive
[] = "five";
31 const base::TimeDelta kRetryDelayForTest
=
32 base::TimeDelta::FromMilliseconds(100);
34 scoped_ptr
<KeyedService
> CreateFeedbackUploaderService(
35 content::BrowserContext
* context
) {
36 return make_scoped_ptr(new feedback::FeedbackUploaderChrome(context
));
43 class FeedbackUploaderTest
: public testing::Test
{
45 FeedbackUploaderTest()
46 : ui_thread_(content::BrowserThread::UI
, &message_loop_
),
47 context_(new content::TestBrowserContext()),
48 prefs_(new TestingPrefServiceSimple()),
49 dispatched_reports_count_(0),
50 expected_reports_(0) {
51 user_prefs::UserPrefs::Set(context_
.get(), prefs_
.get());
52 FeedbackUploaderFactory::GetInstance()->SetTestingFactory(
53 context_
.get(), &CreateFeedbackUploaderService
);
55 uploader_
= FeedbackUploaderFactory::GetForBrowserContext(context_
.get());
56 uploader_
->setup_for_test(
57 base::Bind(&FeedbackUploaderTest::MockDispatchReport
,
58 base::Unretained(this)),
62 ~FeedbackUploaderTest() override
{
63 FeedbackUploaderFactory::GetInstance()->SetTestingFactory(
64 context_
.get(), NULL
);
67 void QueueReport(const std::string
& data
) {
68 uploader_
->QueueReport(data
);
71 void ReportFailure(const std::string
& data
) {
72 uploader_
->RetryReport(data
);
75 void MockDispatchReport(const std::string
& report_data
) {
76 if (ContainsKey(dispatched_reports_
, report_data
)) {
77 dispatched_reports_
[report_data
]++;
79 dispatched_reports_
[report_data
] = 1;
81 dispatched_reports_count_
++;
83 // Dispatch will always update the timer, whether successful or not,
84 // simulate the same behavior.
85 uploader_
->UpdateUploadTimer();
87 if (ProcessingComplete()) {
93 bool ProcessingComplete() {
94 return (dispatched_reports_count_
>= expected_reports_
);
97 void RunMessageLoop() {
98 if (ProcessingComplete())
100 run_loop_
.reset(new base::RunLoop());
104 base::MessageLoop message_loop_
;
105 scoped_ptr
<base::RunLoop
> run_loop_
;
106 content::TestBrowserThread ui_thread_
;
107 scoped_ptr
<content::TestBrowserContext
> context_
;
108 scoped_ptr
<PrefService
> prefs_
;
110 FeedbackUploader
* uploader_
;
112 std::map
<std::string
, unsigned int> dispatched_reports_
;
113 size_t dispatched_reports_count_
;
114 size_t expected_reports_
;
117 #if defined(OS_LINUX) || defined(OS_MACOSX)
118 #define MAYBE_QueueMultiple QueueMultiple
121 #define MAYBE_QueueMultiple DISABLED_QueueMultiple
123 TEST_F(FeedbackUploaderTest
, MAYBE_QueueMultiple
) {
124 dispatched_reports_
.clear();
125 QueueReport(kReportOne
);
126 QueueReport(kReportTwo
);
127 QueueReport(kReportThree
);
128 QueueReport(kReportFour
);
130 EXPECT_EQ(dispatched_reports_
.size(), 4u);
131 EXPECT_EQ(dispatched_reports_
[kReportOne
], 1u);
132 EXPECT_EQ(dispatched_reports_
[kReportTwo
], 1u);
133 EXPECT_EQ(dispatched_reports_
[kReportThree
], 1u);
134 EXPECT_EQ(dispatched_reports_
[kReportFour
], 1u);
137 #if defined(OS_WIN) || defined(OS_ANDROID)
139 #define MAYBE_QueueMultipleWithFailures DISABLED_QueueMultipleWithFailures
141 #define MAYBE_QueueMultipleWithFailures QueueMultipleWithFailures
143 TEST_F(FeedbackUploaderTest
, MAYBE_QueueMultipleWithFailures
) {
144 dispatched_reports_
.clear();
146 QueueReport(kReportOne
);
147 QueueReport(kReportTwo
);
148 QueueReport(kReportThree
);
149 QueueReport(kReportFour
);
151 ReportFailure(kReportThree
);
152 ReportFailure(kReportTwo
);
153 QueueReport(kReportFive
);
155 expected_reports_
= 7;
158 EXPECT_EQ(dispatched_reports_
.size(), 5u);
159 EXPECT_EQ(dispatched_reports_
[kReportOne
], 1u);
160 EXPECT_EQ(dispatched_reports_
[kReportTwo
], 2u);
161 EXPECT_EQ(dispatched_reports_
[kReportThree
], 2u);
162 EXPECT_EQ(dispatched_reports_
[kReportFour
], 1u);
163 EXPECT_EQ(dispatched_reports_
[kReportFive
], 1u);
166 } // namespace feedback