Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: content/browser/loader/upload_progress_tracker_unittest.cc

Issue 2612903008: Add UploadProgressTracker unittest (Closed)
Patch Set: +expectation for unreported small progress. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/loader/upload_progress_tracker.cc ('k') | content/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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 "content/browser/loader/upload_progress_tracker.h"
6
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/test/test_simple_task_runner.h"
13 #include "net/base/upload_progress.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace content {
17 namespace {
18
19 class TestingUploadProgressTracker : public UploadProgressTracker {
20 public:
21 TestingUploadProgressTracker(
22 const tracked_objects::Location& location,
23 UploadProgressReportCallback report_callback,
24 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
25 : UploadProgressTracker(location,
26 std::move(report_callback),
27 nullptr,
28 std::move(task_runner)),
29 current_time_(base::TimeTicks::Now()) {}
30
31 void set_upload_progress(const net::UploadProgress& upload_progress) {
32 upload_progress_ = upload_progress;
33 }
34
35 void set_current_time(const base::TimeTicks& current_time) {
36 current_time_ = current_time;
37 }
38
39 private:
40 // UploadProgressTracker overrides.
41 base::TimeTicks GetCurrentTime() const override { return current_time_; }
42 net::UploadProgress GetUploadProgress() const override {
43 return upload_progress_;
44 }
45
46 base::TimeTicks current_time_;
47 net::UploadProgress upload_progress_;
48
49 DISALLOW_COPY_AND_ASSIGN(TestingUploadProgressTracker);
50 };
51
52 } // namespace
53
54 class UploadProgressTrackerTest : public ::testing::Test {
55 public:
56 UploadProgressTrackerTest()
57 : task_runner_(new base::TestSimpleTaskRunner),
58 upload_progress_tracker_(
59 FROM_HERE,
60 base::BindRepeating(
61 &UploadProgressTrackerTest::OnUploadProgressReported,
62 base::Unretained(this)),
63 task_runner_) {}
64
65 private:
66 void OnUploadProgressReported(const net::UploadProgress& progress) {
67 ++report_count_;
68 reported_position_ = progress.position();
69 reported_total_size_ = progress.size();
70 }
71
72 protected:
73 int report_count_ = 0;
74 int64_t reported_position_ = 0;
75 int64_t reported_total_size_ = 0;
76
77 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
78 TestingUploadProgressTracker upload_progress_tracker_;
79
80 DISALLOW_COPY_AND_ASSIGN(UploadProgressTrackerTest);
81 };
82
83 TEST_F(UploadProgressTrackerTest, NoACK) {
84 upload_progress_tracker_.set_upload_progress(net::UploadProgress(500, 1000));
85
86 // The first timer task calls ReportUploadProgress.
87 EXPECT_EQ(0, report_count_);
88 task_runner_->RunPendingTasks();
89 EXPECT_EQ(1, report_count_);
90 EXPECT_EQ(500, reported_position_);
91 EXPECT_EQ(1000, reported_total_size_);
92
93 upload_progress_tracker_.set_upload_progress(net::UploadProgress(750, 1000));
94
95 // The second timer task does nothing, since the first report didn't send the
96 // ACK.
97 task_runner_->RunPendingTasks();
98 EXPECT_EQ(1, report_count_);
99 }
100
101 TEST_F(UploadProgressTrackerTest, NoUpload) {
102 upload_progress_tracker_.set_upload_progress(net::UploadProgress(0, 0));
103
104 // UploadProgressTracker does nothing on the empty upload content.
105 EXPECT_EQ(0, report_count_);
106 task_runner_->RunPendingTasks();
107 EXPECT_EQ(0, report_count_);
108 }
109
110 TEST_F(UploadProgressTrackerTest, NoProgress) {
111 upload_progress_tracker_.set_upload_progress(net::UploadProgress(500, 1000));
112
113 // The first timer task calls ReportUploadProgress.
114 EXPECT_EQ(0, report_count_);
115 task_runner_->RunPendingTasks();
116 EXPECT_EQ(1, report_count_);
117 EXPECT_EQ(500, reported_position_);
118 EXPECT_EQ(1000, reported_total_size_);
119
120 upload_progress_tracker_.OnAckReceived();
121
122 // The second time doesn't call ReportUploadProgress since there's no
123 // progress.
124 EXPECT_EQ(1, report_count_);
125 task_runner_->RunPendingTasks();
126 EXPECT_EQ(1, report_count_);
127 }
128
129 TEST_F(UploadProgressTrackerTest, Finished) {
130 upload_progress_tracker_.set_upload_progress(net::UploadProgress(999, 1000));
131
132 // The first timer task calls ReportUploadProgress.
133 EXPECT_EQ(0, report_count_);
134 task_runner_->RunPendingTasks();
135 EXPECT_EQ(1, report_count_);
136 EXPECT_EQ(999, reported_position_);
137 EXPECT_EQ(1000, reported_total_size_);
138
139 upload_progress_tracker_.OnAckReceived();
140 upload_progress_tracker_.set_upload_progress(net::UploadProgress(1000, 1000));
141
142 // The second timer task calls ReportUploadProgress for reporting the
143 // completion.
144 EXPECT_EQ(1, report_count_);
145 task_runner_->RunPendingTasks();
146 EXPECT_EQ(2, report_count_);
147 EXPECT_EQ(1000, reported_position_);
148 EXPECT_EQ(1000, reported_total_size_);
149 }
150
151 TEST_F(UploadProgressTrackerTest, Progress) {
152 upload_progress_tracker_.set_upload_progress(net::UploadProgress(500, 1000));
153
154 // The first timer task calls ReportUploadProgress.
155 EXPECT_EQ(0, report_count_);
156 task_runner_->RunPendingTasks();
157 EXPECT_EQ(1, report_count_);
158 EXPECT_EQ(500, reported_position_);
159 EXPECT_EQ(1000, reported_total_size_);
160
161 upload_progress_tracker_.OnAckReceived();
162 upload_progress_tracker_.set_upload_progress(net::UploadProgress(750, 1000));
163
164 // The second timer task calls ReportUploadProgress since the progress is
165 // big enough to report.
166 EXPECT_EQ(1, report_count_);
167 task_runner_->RunPendingTasks();
168 EXPECT_EQ(2, report_count_);
169 EXPECT_EQ(750, reported_position_);
170 EXPECT_EQ(1000, reported_total_size_);
171 }
172
173 TEST_F(UploadProgressTrackerTest, TimePassed) {
174 upload_progress_tracker_.set_upload_progress(net::UploadProgress(500, 1000));
175
176 // The first timer task calls ReportUploadProgress.
177 EXPECT_EQ(0, report_count_);
178 task_runner_->RunPendingTasks();
179 EXPECT_EQ(1, report_count_);
180 EXPECT_EQ(500, reported_position_);
181 EXPECT_EQ(1000, reported_total_size_);
182
183 upload_progress_tracker_.OnAckReceived();
184 upload_progress_tracker_.set_upload_progress(net::UploadProgress(501, 1000));
185
186 // The second timer task doesn't call ReportUploadProgress since the progress
187 // is too small to report it.
188 EXPECT_EQ(1, report_count_);
189 task_runner_->RunPendingTasks();
190 EXPECT_EQ(1, report_count_);
191
192 upload_progress_tracker_.set_current_time(base::TimeTicks::Now() +
193 base::TimeDelta::FromSeconds(1));
194
195 // The third timer task calls ReportUploadProgress since it's been long time
196 // from the last report.
197 EXPECT_EQ(1, report_count_);
198 task_runner_->RunPendingTasks();
199 EXPECT_EQ(2, report_count_);
200 EXPECT_EQ(501, reported_position_);
201 EXPECT_EQ(1000, reported_total_size_);
202 }
203
204 TEST_F(UploadProgressTrackerTest, Rewound) {
205 upload_progress_tracker_.set_upload_progress(net::UploadProgress(500, 1000));
206
207 // The first timer task calls ReportUploadProgress.
208 EXPECT_EQ(0, report_count_);
209 task_runner_->RunPendingTasks();
210 EXPECT_EQ(1, report_count_);
211 EXPECT_EQ(500, reported_position_);
212 EXPECT_EQ(1000, reported_total_size_);
213
214 upload_progress_tracker_.OnAckReceived();
215 upload_progress_tracker_.set_upload_progress(net::UploadProgress(250, 1000));
216
217 // The second timer task doesn't call ReportUploadProgress since the progress
218 // was rewound.
219 EXPECT_EQ(1, report_count_);
220 task_runner_->RunPendingTasks();
221 EXPECT_EQ(1, report_count_);
222
223 upload_progress_tracker_.set_current_time(base::TimeTicks::Now() +
224 base::TimeDelta::FromSeconds(1));
225
226 // Even after a good amount of time passed, the rewound progress should not be
227 // reported.
228 EXPECT_EQ(1, report_count_);
229 task_runner_->RunPendingTasks();
230 EXPECT_EQ(1, report_count_);
231 }
232
233 TEST_F(UploadProgressTrackerTest, Completed) {
234 upload_progress_tracker_.set_upload_progress(net::UploadProgress(500, 1000));
235
236 // The first timer task calls ReportUploadProgress.
237 EXPECT_EQ(0, report_count_);
238 task_runner_->RunPendingTasks();
239 EXPECT_EQ(1, report_count_);
240 EXPECT_EQ(500, reported_position_);
241 EXPECT_EQ(1000, reported_total_size_);
242
243 upload_progress_tracker_.set_upload_progress(net::UploadProgress(1000, 1000));
244
245 // OnUploadCompleted runs ReportUploadProgress even without Ack nor timer.
246 upload_progress_tracker_.OnUploadCompleted();
247 EXPECT_EQ(2, report_count_);
248 EXPECT_EQ(1000, reported_position_);
249 EXPECT_EQ(1000, reported_total_size_);
250
251 task_runner_->RunPendingTasks();
252 EXPECT_EQ(2, report_count_);
253 EXPECT_FALSE(task_runner_->HasPendingTask());
254 }
255
256 } // namespace context
OLDNEW
« no previous file with comments | « content/browser/loader/upload_progress_tracker.cc ('k') | content/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698