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

Unified Diff: content/browser/loader/upload_progress_tracker_unittest.cc

Issue 2491613004: Make base::Timer sequence-friendly. (Closed)
Patch Set: add back commented out DCHECK in Stop() Created 3 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/loader/upload_progress_tracker_unittest.cc
diff --git a/content/browser/loader/upload_progress_tracker_unittest.cc b/content/browser/loader/upload_progress_tracker_unittest.cc
index cf556db2c94ec8b2366a01218c5d16530662715a..363ae990eb1231c564c59167c90100a869ef61cf 100644
--- a/content/browser/loader/upload_progress_tracker_unittest.cc
+++ b/content/browser/loader/upload_progress_tracker_unittest.cc
@@ -9,7 +9,8 @@
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h"
-#include "base/test/test_simple_task_runner.h"
+#include "base/test/test_mock_time_task_runner.h"
+#include "base/threading/thread_task_runner_handle.h"
#include "net/base/upload_progress.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -54,13 +55,13 @@ class TestingUploadProgressTracker : public UploadProgressTracker {
class UploadProgressTrackerTest : public ::testing::Test {
public:
UploadProgressTrackerTest()
- : task_runner_(new base::TestSimpleTaskRunner),
+ : task_runner_handle_(mock_task_runner_),
upload_progress_tracker_(
FROM_HERE,
base::BindRepeating(
&UploadProgressTrackerTest::OnUploadProgressReported,
base::Unretained(this)),
- task_runner_) {}
+ mock_task_runner_) {}
private:
void OnUploadProgressReported(const net::UploadProgress& progress) {
@@ -74,7 +75,12 @@ class UploadProgressTrackerTest : public ::testing::Test {
int64_t reported_position_ = 0;
int64_t reported_total_size_ = 0;
- scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
+ // Mocks the current thread's task runner which will also be used as the
+ // UploadProgressTracker's task runner.
+ scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_ =
+ new base::TestMockTimeTaskRunner;
+ base::ThreadTaskRunnerHandle task_runner_handle_;
+
TestingUploadProgressTracker upload_progress_tracker_;
DISALLOW_COPY_AND_ASSIGN(UploadProgressTrackerTest);
@@ -85,7 +91,8 @@ TEST_F(UploadProgressTrackerTest, NoACK) {
// The first timer task calls ReportUploadProgress.
EXPECT_EQ(0, report_count_);
- task_runner_->RunPendingTasks();
+ mock_task_runner_->FastForwardBy(
+ UploadProgressTracker::GetUploadProgressIntervalForTesting());
EXPECT_EQ(1, report_count_);
EXPECT_EQ(500, reported_position_);
EXPECT_EQ(1000, reported_total_size_);
@@ -94,7 +101,8 @@ TEST_F(UploadProgressTrackerTest, NoACK) {
// The second timer task does nothing, since the first report didn't send the
// ACK.
- task_runner_->RunPendingTasks();
+ mock_task_runner_->FastForwardBy(
+ UploadProgressTracker::GetUploadProgressIntervalForTesting());
EXPECT_EQ(1, report_count_);
}
@@ -103,7 +111,8 @@ TEST_F(UploadProgressTrackerTest, NoUpload) {
// UploadProgressTracker does nothing on the empty upload content.
EXPECT_EQ(0, report_count_);
- task_runner_->RunPendingTasks();
+ mock_task_runner_->FastForwardBy(
+ UploadProgressTracker::GetUploadProgressIntervalForTesting());
EXPECT_EQ(0, report_count_);
}
@@ -112,7 +121,8 @@ TEST_F(UploadProgressTrackerTest, NoProgress) {
// The first timer task calls ReportUploadProgress.
EXPECT_EQ(0, report_count_);
- task_runner_->RunPendingTasks();
+ mock_task_runner_->FastForwardBy(
+ UploadProgressTracker::GetUploadProgressIntervalForTesting());
EXPECT_EQ(1, report_count_);
EXPECT_EQ(500, reported_position_);
EXPECT_EQ(1000, reported_total_size_);
@@ -122,7 +132,8 @@ TEST_F(UploadProgressTrackerTest, NoProgress) {
// The second time doesn't call ReportUploadProgress since there's no
// progress.
EXPECT_EQ(1, report_count_);
- task_runner_->RunPendingTasks();
+ mock_task_runner_->FastForwardBy(
+ UploadProgressTracker::GetUploadProgressIntervalForTesting());
EXPECT_EQ(1, report_count_);
}
@@ -131,7 +142,8 @@ TEST_F(UploadProgressTrackerTest, Finished) {
// The first timer task calls ReportUploadProgress.
EXPECT_EQ(0, report_count_);
- task_runner_->RunPendingTasks();
+ mock_task_runner_->FastForwardBy(
+ UploadProgressTracker::GetUploadProgressIntervalForTesting());
EXPECT_EQ(1, report_count_);
EXPECT_EQ(999, reported_position_);
EXPECT_EQ(1000, reported_total_size_);
@@ -142,7 +154,8 @@ TEST_F(UploadProgressTrackerTest, Finished) {
// The second timer task calls ReportUploadProgress for reporting the
// completion.
EXPECT_EQ(1, report_count_);
- task_runner_->RunPendingTasks();
+ mock_task_runner_->FastForwardBy(
+ UploadProgressTracker::GetUploadProgressIntervalForTesting());
EXPECT_EQ(2, report_count_);
EXPECT_EQ(1000, reported_position_);
EXPECT_EQ(1000, reported_total_size_);
@@ -153,7 +166,8 @@ TEST_F(UploadProgressTrackerTest, Progress) {
// The first timer task calls ReportUploadProgress.
EXPECT_EQ(0, report_count_);
- task_runner_->RunPendingTasks();
+ mock_task_runner_->FastForwardBy(
+ UploadProgressTracker::GetUploadProgressIntervalForTesting());
EXPECT_EQ(1, report_count_);
EXPECT_EQ(500, reported_position_);
EXPECT_EQ(1000, reported_total_size_);
@@ -164,7 +178,8 @@ TEST_F(UploadProgressTrackerTest, Progress) {
// The second timer task calls ReportUploadProgress since the progress is
// big enough to report.
EXPECT_EQ(1, report_count_);
- task_runner_->RunPendingTasks();
+ mock_task_runner_->FastForwardBy(
+ UploadProgressTracker::GetUploadProgressIntervalForTesting());
EXPECT_EQ(2, report_count_);
EXPECT_EQ(750, reported_position_);
EXPECT_EQ(1000, reported_total_size_);
@@ -175,7 +190,8 @@ TEST_F(UploadProgressTrackerTest, TimePassed) {
// The first timer task calls ReportUploadProgress.
EXPECT_EQ(0, report_count_);
- task_runner_->RunPendingTasks();
+ mock_task_runner_->FastForwardBy(
+ UploadProgressTracker::GetUploadProgressIntervalForTesting());
EXPECT_EQ(1, report_count_);
EXPECT_EQ(500, reported_position_);
EXPECT_EQ(1000, reported_total_size_);
@@ -186,7 +202,8 @@ TEST_F(UploadProgressTrackerTest, TimePassed) {
// The second timer task doesn't call ReportUploadProgress since the progress
// is too small to report it.
EXPECT_EQ(1, report_count_);
- task_runner_->RunPendingTasks();
+ mock_task_runner_->FastForwardBy(
+ UploadProgressTracker::GetUploadProgressIntervalForTesting());
EXPECT_EQ(1, report_count_);
upload_progress_tracker_.set_current_time(base::TimeTicks::Now() +
@@ -195,7 +212,8 @@ TEST_F(UploadProgressTrackerTest, TimePassed) {
// The third timer task calls ReportUploadProgress since it's been long time
// from the last report.
EXPECT_EQ(1, report_count_);
- task_runner_->RunPendingTasks();
+ mock_task_runner_->FastForwardBy(
+ UploadProgressTracker::GetUploadProgressIntervalForTesting());
EXPECT_EQ(2, report_count_);
EXPECT_EQ(501, reported_position_);
EXPECT_EQ(1000, reported_total_size_);
@@ -206,7 +224,8 @@ TEST_F(UploadProgressTrackerTest, Rewound) {
// The first timer task calls ReportUploadProgress.
EXPECT_EQ(0, report_count_);
- task_runner_->RunPendingTasks();
+ mock_task_runner_->FastForwardBy(
+ UploadProgressTracker::GetUploadProgressIntervalForTesting());
EXPECT_EQ(1, report_count_);
EXPECT_EQ(500, reported_position_);
EXPECT_EQ(1000, reported_total_size_);
@@ -217,7 +236,8 @@ TEST_F(UploadProgressTrackerTest, Rewound) {
// The second timer task doesn't call ReportUploadProgress since the progress
// was rewound.
EXPECT_EQ(1, report_count_);
- task_runner_->RunPendingTasks();
+ mock_task_runner_->FastForwardBy(
+ UploadProgressTracker::GetUploadProgressIntervalForTesting());
EXPECT_EQ(1, report_count_);
upload_progress_tracker_.set_current_time(base::TimeTicks::Now() +
@@ -226,7 +246,8 @@ TEST_F(UploadProgressTrackerTest, Rewound) {
// Even after a good amount of time passed, the rewound progress should not be
// reported.
EXPECT_EQ(1, report_count_);
- task_runner_->RunPendingTasks();
+ mock_task_runner_->FastForwardBy(
+ UploadProgressTracker::GetUploadProgressIntervalForTesting());
EXPECT_EQ(1, report_count_);
}
@@ -235,7 +256,8 @@ TEST_F(UploadProgressTrackerTest, Completed) {
// The first timer task calls ReportUploadProgress.
EXPECT_EQ(0, report_count_);
- task_runner_->RunPendingTasks();
+ mock_task_runner_->FastForwardBy(
+ UploadProgressTracker::GetUploadProgressIntervalForTesting());
EXPECT_EQ(1, report_count_);
EXPECT_EQ(500, reported_position_);
EXPECT_EQ(1000, reported_total_size_);
@@ -248,9 +270,10 @@ TEST_F(UploadProgressTrackerTest, Completed) {
EXPECT_EQ(1000, reported_position_);
EXPECT_EQ(1000, reported_total_size_);
- task_runner_->RunPendingTasks();
+ mock_task_runner_->FastForwardBy(
+ UploadProgressTracker::GetUploadProgressIntervalForTesting());
EXPECT_EQ(2, report_count_);
- EXPECT_FALSE(task_runner_->HasPendingTask());
+ EXPECT_FALSE(mock_task_runner_->HasPendingTask());
}
} // namespace context
« no previous file with comments | « content/browser/loader/upload_progress_tracker.cc ('k') | content/browser/service_worker/service_worker_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698