OLD | NEW |
| (Empty) |
1 // 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 | |
7 #include <set> | |
8 | |
9 #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_chrome.h" | |
13 #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 | |
18 namespace { | |
19 | |
20 const char kReportOne[] = "one"; | |
21 const char kReportTwo[] = "two"; | |
22 const char kReportThree[] = "three"; | |
23 const char kReportFour[] = "four"; | |
24 const char kReportFive[] = "five"; | |
25 | |
26 const base::TimeDelta kRetryDelayForTest = | |
27 base::TimeDelta::FromMilliseconds(100); | |
28 | |
29 KeyedService* CreateFeedbackUploaderService(content::BrowserContext* context) { | |
30 return new feedback::FeedbackUploaderChrome( | |
31 Profile::FromBrowserContext(context)); | |
32 } | |
33 | |
34 } // namespace | |
35 | |
36 namespace feedback { | |
37 | |
38 class FeedbackUploaderTest : public testing::Test { | |
39 protected: | |
40 FeedbackUploaderTest() | |
41 : ui_thread_(content::BrowserThread::UI, &message_loop_), | |
42 profile_(new TestingProfile()), | |
43 dispatched_reports_count_(0), | |
44 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) { | |
61 uploader_->QueueReport(data); | |
62 } | |
63 | |
64 void ReportFailure(const std::string& data) { | |
65 uploader_->RetryReport(data); | |
66 } | |
67 | |
68 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_++; | |
75 | |
76 // Dispatch will always update the timer, whether successful or not, | |
77 // simulate the same behavior. | |
78 uploader_->UpdateUploadTimer(); | |
79 | |
80 if (ProcessingComplete()) { | |
81 if (run_loop_.get()) | |
82 run_loop_->Quit(); | |
83 } | |
84 } | |
85 | |
86 bool ProcessingComplete() { | |
87 return (dispatched_reports_count_ >= expected_reports_); | |
88 } | |
89 | |
90 void RunMessageLoop() { | |
91 if (ProcessingComplete()) | |
92 return; | |
93 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 | |
104 std::map<std::string, unsigned int> dispatched_reports_; | |
105 size_t dispatched_reports_count_; | |
106 size_t expected_reports_; | |
107 }; | |
108 | |
109 #if defined(OS_LINUX) || defined(OS_MACOSX) | |
110 #define MAYBE_QueueMultiple QueueMultiple | |
111 #else | |
112 // crbug.com/330547 | |
113 #define MAYBE_QueueMultiple DISABLED_QueueMultiple | |
114 #endif | |
115 TEST_F(FeedbackUploaderTest, MAYBE_QueueMultiple) { | |
116 dispatched_reports_.clear(); | |
117 QueueReport(kReportOne); | |
118 QueueReport(kReportTwo); | |
119 QueueReport(kReportThree); | |
120 QueueReport(kReportFour); | |
121 | |
122 EXPECT_EQ(dispatched_reports_.size(), 4u); | |
123 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); | |
127 } | |
128 | |
129 #if defined(OS_WIN) || defined(OS_ANDROID) | |
130 // crbug.com/330547 | |
131 #define MAYBE_QueueMultipleWithFailures DISABLED_QueueMultipleWithFailures | |
132 #else | |
133 #define MAYBE_QueueMultipleWithFailures QueueMultipleWithFailures | |
134 #endif | |
135 TEST_F(FeedbackUploaderTest, MAYBE_QueueMultipleWithFailures) { | |
136 dispatched_reports_.clear(); | |
137 | |
138 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 | |
150 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); | |
156 } | |
157 | |
158 } // namespace feedback | |
OLD | NEW |