Adds shutdown when FrameTreeServer goes away
[chromium-blink-merge.git] / components / feedback / feedback_uploader_unittest.cc
blob80bbb21f0c5117318e2eed017e7718864b92ae2c
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"
7 #include <set>
9 #include "base/bind.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/stl_util.h"
14 #include "components/feedback/feedback_uploader_chrome.h"
15 #include "components/feedback/feedback_uploader_factory.h"
16 #include "components/pref_registry/testing_pref_service_syncable.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"
23 namespace {
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));
39 } // namespace
41 namespace feedback {
43 class FeedbackUploaderTest : public testing::Test {
44 protected:
45 FeedbackUploaderTest()
46 : ui_thread_(content::BrowserThread::UI, &message_loop_),
47 context_(new content::TestBrowserContext()),
48 prefs_(new user_prefs::TestingPrefServiceSyncable()),
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)),
59 kRetryDelayForTest);
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]++;
78 } else {
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()) {
88 if (run_loop_.get())
89 run_loop_->Quit();
93 bool ProcessingComplete() {
94 return (dispatched_reports_count_ >= expected_reports_);
97 void RunMessageLoop() {
98 if (ProcessingComplete())
99 return;
100 run_loop_.reset(new base::RunLoop());
101 run_loop_->Run();
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
119 #else
120 // crbug.com/330547
121 #define MAYBE_QueueMultiple DISABLED_QueueMultiple
122 #endif
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)
138 // crbug.com/330547
139 #define MAYBE_QueueMultipleWithFailures DISABLED_QueueMultipleWithFailures
140 #else
141 #define MAYBE_QueueMultipleWithFailures QueueMultipleWithFailures
142 #endif
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;
156 RunMessageLoop();
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