blob: c795c18bf5c0df77924b4aaa7f2e4c867afce4d5 [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"
[email protected]77f7fe892014-03-18 00:00:0612#include "chrome/browser/feedback/feedback_uploader_chrome.h"
[email protected]48372252013-12-20 12:18:0113#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
18namespace {
19
20const char kReportOne[] = "one";
21const char kReportTwo[] = "two";
22const char kReportThree[] = "three";
23const char kReportFour[] = "four";
24const char kReportFive[] = "five";
25
26const base::TimeDelta kRetryDelayForTest =
27 base::TimeDelta::FromMilliseconds(100);
28
[email protected]95003d522014-03-13 20:22:3129KeyedService* CreateFeedbackUploaderService(content::BrowserContext* context) {
[email protected]77f7fe892014-03-18 00:00:0630 return new feedback::FeedbackUploaderChrome(
31 Profile::FromBrowserContext(context));
[email protected]48372252013-12-20 12:18:0132}
33
34} // namespace
35
36namespace feedback {
37
38class FeedbackUploaderTest : public testing::Test {
39 protected:
40 FeedbackUploaderTest()
41 : ui_thread_(content::BrowserThread::UI, &message_loop_),
42 profile_(new TestingProfile()),
[email protected]90cc2a9a2014-02-28 22:05:5543 dispatched_reports_count_(0),
[email protected]48372252013-12-20 12:18:0144 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]90cc2a9a2014-02-28 22:05:5561 uploader_->QueueReport(data);
[email protected]48372252013-12-20 12:18:0162 }
63
64 void ReportFailure(const std::string& data) {
[email protected]90cc2a9a2014-02-28 22:05:5565 uploader_->RetryReport(data);
[email protected]48372252013-12-20 12:18:0166 }
67
[email protected]90cc2a9a2014-02-28 22:05:5568 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]48372252013-12-20 12:18:0175
76 // Dispatch will always update the timer, whether successful or not,
77 // simulate the same behavior.
78 uploader_->UpdateUploadTimer();
79
[email protected]90cc2a9a2014-02-28 22:05:5580 if (ProcessingComplete()) {
[email protected]48372252013-12-20 12:18:0181 if (run_loop_.get())
82 run_loop_->Quit();
83 }
84 }
85
[email protected]90cc2a9a2014-02-28 22:05:5586 bool ProcessingComplete() {
87 return (dispatched_reports_count_ >= expected_reports_);
88 }
89
[email protected]48372252013-12-20 12:18:0190 void RunMessageLoop() {
[email protected]90cc2a9a2014-02-28 22:05:5591 if (ProcessingComplete())
92 return;
[email protected]48372252013-12-20 12:18:0193 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]90cc2a9a2014-02-28 22:05:55104 std::map<std::string, unsigned int> dispatched_reports_;
105 size_t dispatched_reports_count_;
[email protected]48372252013-12-20 12:18:01106 size_t expected_reports_;
107};
108
[email protected]90cc2a9a2014-02-28 22:05:55109#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
115TEST_F(FeedbackUploaderTest, MAYBE_QueueMultiple) {
[email protected]48372252013-12-20 12:18:01116 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]90cc2a9a2014-02-28 22:05:55123 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]48372252013-12-20 12:18:01127}
128
[email protected]9bd534e2014-02-24 18:48:47129#if defined(OS_WIN) || defined(OS_ANDROID)
[email protected]f1007112013-12-23 19:04:16130// crbug.com/330547
[email protected]3f2e9bb2014-01-18 08:48:34131#define MAYBE_QueueMultipleWithFailures DISABLED_QueueMultipleWithFailures
[email protected]4e0ebba2014-02-21 22:32:22132#else
133#define MAYBE_QueueMultipleWithFailures QueueMultipleWithFailures
[email protected]3f2e9bb2014-01-18 08:48:34134#endif
135TEST_F(FeedbackUploaderTest, MAYBE_QueueMultipleWithFailures) {
[email protected]48372252013-12-20 12:18:01136 dispatched_reports_.clear();
[email protected]90cc2a9a2014-02-28 22:05:55137
[email protected]48372252013-12-20 12:18:01138 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]90cc2a9a2014-02-28 22:05:55150 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]48372252013-12-20 12:18:01156}
157
158} // namespace feedback