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

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

Issue 2612903008: Add UploadProgressTracker unittest (Closed)
Patch Set: +#include 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
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
30 void set_upload_progress(const net::UploadProgress& upload_progress) {
31 upload_progress_ = upload_progress;
32 }
33
34 void set_current_time(const base::TimeTicks& current_time) {
35 current_time_ = current_time;
36 }
37
38 private:
39 // UploadProgressTracker overrides.
40 base::TimeTicks GetCurrentTime() const override { return current_time_; }
41 net::UploadProgress GetUploadProgress() const override {
42 return upload_progress_;
43 }
44
45 base::TimeTicks current_time_;
46 net::UploadProgress upload_progress_;
47
48 DISALLOW_COPY_AND_ASSIGN(TestingUploadProgressTracker);
49 };
50
51 } // namespace
52
53 class UploadProgressTrackerTest : public ::testing::Test {
54 public:
55 UploadProgressTrackerTest()
56 : task_runner_(new base::TestSimpleTaskRunner),
57 upload_progress_tracker_(
58 FROM_HERE,
59 base::BindRepeating(
60 &UploadProgressTrackerTest::OnUploadProgressReported,
61 base::Unretained(this)),
62 task_runner_) {}
63
64 void SetUp() override {
65 upload_progress_tracker_.set_current_time(base::TimeTicks::Now());
mmenke 2017/01/12 16:06:54 Just move this into UploadProgressTracker's constr
tzik 2017/01/13 05:26:40 Done.
66 }
67
68 private:
69 void OnUploadProgressReported(const net::UploadProgress& progress) {
70 ++report_count_;
71 reported_position_ = progress.position();
72 reported_total_size_ = progress.size();
73 }
74
75 protected:
76 int report_count_ = 0;
77 int64_t reported_position_ = 0;
78 int64_t reported_total_size_ = 0;
79
80 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
81 TestingUploadProgressTracker upload_progress_tracker_;
82
83 DISALLOW_COPY_AND_ASSIGN(UploadProgressTrackerTest);
84 };
85
86 TEST_F(UploadProgressTrackerTest, NoACK) {
87 upload_progress_tracker_.set_upload_progress(net::UploadProgress(500, 1000));
88
89 // The first timer task calls ReportUploadProgress.
90 EXPECT_EQ(0, report_count_);
91 task_runner_->RunPendingTasks();
92 EXPECT_EQ(1, report_count_);
93 EXPECT_EQ(500, reported_position_);
94 EXPECT_EQ(1000, reported_total_size_);
95
96 upload_progress_tracker_.set_upload_progress(net::UploadProgress(750, 1000));
97
98 // The second timer task does nothing, since the first report didn't send the
99 // ACK.
100 task_runner_->RunPendingTasks();
101 EXPECT_EQ(1, report_count_);
102 }
103
104 TEST_F(UploadProgressTrackerTest, NoUpload) {
105 upload_progress_tracker_.set_upload_progress(net::UploadProgress(0, 0));
106
107 // UploadProgressTracker does nothing on the empty upload content.
108 EXPECT_EQ(0, report_count_);
109 task_runner_->RunPendingTasks();
110 EXPECT_EQ(0, report_count_);
111 }
112
113 TEST_F(UploadProgressTrackerTest, NoProgress) {
114 upload_progress_tracker_.set_upload_progress(net::UploadProgress(500, 1000));
115
116 // The first timer task calls ReportUploadProgress.
117 EXPECT_EQ(0, report_count_);
118 task_runner_->RunPendingTasks();
119 EXPECT_EQ(1, report_count_);
120 EXPECT_EQ(500, reported_position_);
121 EXPECT_EQ(1000, reported_total_size_);
122
123 upload_progress_tracker_.OnAckReceived();
124
125 // The second time doesn't call ReportUploadProgress since there's no
126 // progress.
127 EXPECT_EQ(1, report_count_);
128 task_runner_->RunPendingTasks();
129 EXPECT_EQ(1, report_count_);
130 }
131
132 TEST_F(UploadProgressTrackerTest, Finished) {
133 upload_progress_tracker_.set_upload_progress(net::UploadProgress(999, 1000));
134
135 // The first timer task calls ReportUploadProgress.
136 EXPECT_EQ(0, report_count_);
137 task_runner_->RunPendingTasks();
138 EXPECT_EQ(1, report_count_);
139 EXPECT_EQ(999, reported_position_);
140 EXPECT_EQ(1000, reported_total_size_);
141
142 upload_progress_tracker_.OnAckReceived();
143 upload_progress_tracker_.set_upload_progress(net::UploadProgress(1000, 1000));
144
145 // The second timer task calls ReportUploadProgress for reporting the
146 // completion.
147 EXPECT_EQ(1, report_count_);
148 task_runner_->RunPendingTasks();
149 EXPECT_EQ(2, report_count_);
150 EXPECT_EQ(1000, reported_position_);
151 EXPECT_EQ(1000, reported_total_size_);
152 }
153
154 TEST_F(UploadProgressTrackerTest, Progress) {
155 upload_progress_tracker_.set_upload_progress(net::UploadProgress(500, 1000));
156
157 // The first timer task calls ReportUploadProgress.
158 EXPECT_EQ(0, report_count_);
159 task_runner_->RunPendingTasks();
160 EXPECT_EQ(1, report_count_);
161 EXPECT_EQ(500, reported_position_);
162 EXPECT_EQ(1000, reported_total_size_);
163
164 upload_progress_tracker_.OnAckReceived();
165 upload_progress_tracker_.set_upload_progress(net::UploadProgress(750, 1000));
166
167 // The second timer task calls ReportUploadProgress since the progress is
168 // big enough to report.
169 EXPECT_EQ(1, report_count_);
170 task_runner_->RunPendingTasks();
171 EXPECT_EQ(2, report_count_);
172 EXPECT_EQ(750, reported_position_);
173 EXPECT_EQ(1000, reported_total_size_);
174 }
175
176 TEST_F(UploadProgressTrackerTest, TimePassed) {
177 upload_progress_tracker_.set_upload_progress(net::UploadProgress(500, 1000));
178
179 // The first timer task calls ReportUploadProgress.
180 EXPECT_EQ(0, report_count_);
181 task_runner_->RunPendingTasks();
182 EXPECT_EQ(1, report_count_);
183 EXPECT_EQ(500, reported_position_);
184 EXPECT_EQ(1000, reported_total_size_);
185
186 upload_progress_tracker_.OnAckReceived();
187 upload_progress_tracker_.set_upload_progress(net::UploadProgress(501, 1000));
yhirano 2017/01/13 03:30:37 Can you call task_runner_->RunPendingTasks() and c
tzik 2017/01/13 05:26:41 Done.
188 upload_progress_tracker_.set_current_time(base::TimeTicks::Now() +
189 base::TimeDelta::FromSeconds(1));
190
191 // The second timer task calls ReportUploadProgress since it's been long time
192 // from the last report.
193 EXPECT_EQ(1, report_count_);
194 task_runner_->RunPendingTasks();
195 EXPECT_EQ(2, report_count_);
196 EXPECT_EQ(501, reported_position_);
197 EXPECT_EQ(1000, reported_total_size_);
198 }
199
200 TEST_F(UploadProgressTrackerTest, Rewound) {
201 upload_progress_tracker_.set_upload_progress(net::UploadProgress(500, 1000));
202
203 // The first timer task calls ReportUploadProgress.
204 EXPECT_EQ(0, report_count_);
205 task_runner_->RunPendingTasks();
206 EXPECT_EQ(1, report_count_);
207 EXPECT_EQ(500, reported_position_);
208 EXPECT_EQ(1000, reported_total_size_);
209
210 upload_progress_tracker_.OnAckReceived();
211 upload_progress_tracker_.set_upload_progress(net::UploadProgress(250, 1000));
212
213 // The second timer task doesn't call ReportUploadProgress since the progress
214 // was rewound.
215 EXPECT_EQ(1, report_count_);
216 task_runner_->RunPendingTasks();
217 EXPECT_EQ(1, report_count_);
218
219 upload_progress_tracker_.set_current_time(base::TimeTicks::Now() +
220 base::TimeDelta::FromSeconds(1));
221
222 // Even after a good amount of time passed, the rewound progress should not be
223 // reported.
224 EXPECT_EQ(1, report_count_);
225 task_runner_->RunPendingTasks();
226 EXPECT_EQ(1, report_count_);
227 }
228
229 TEST_F(UploadProgressTrackerTest, Completed) {
230 upload_progress_tracker_.set_upload_progress(net::UploadProgress(500, 1000));
231
232 // The first timer task calls ReportUploadProgress.
233 EXPECT_EQ(0, report_count_);
234 task_runner_->RunPendingTasks();
235 EXPECT_EQ(1, report_count_);
236 EXPECT_EQ(500, reported_position_);
237 EXPECT_EQ(1000, reported_total_size_);
238
239 upload_progress_tracker_.set_upload_progress(net::UploadProgress(1000, 1000));
240
241 // OnUploadCompleted runs ReportUploadProgress even without Ack nor timer.
242 upload_progress_tracker_.OnUploadCompleted();
243 EXPECT_EQ(2, report_count_);
244 EXPECT_EQ(1000, reported_position_);
245 EXPECT_EQ(1000, reported_total_size_);
246
247 task_runner_->RunPendingTasks();
248 EXPECT_EQ(2, report_count_);
249 EXPECT_FALSE(task_runner_->HasPendingTask());
250 }
251
252 } // namespace context
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698