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

Side by Side Diff: content/browser/download/parallel_download_job_unittest.cc

Issue 2689373003: Introduce ParallelDownloadJob. (Closed)
Patch Set: Make windows compiler happy. Created 3 years, 10 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/download/parallel_download_job.h"
6
7 #include <utility>
8 #include <vector>
9
10 #include "base/memory/ptr_util.h"
11 #include "content/browser/download/download_item_impl_delegate.h"
12 #include "content/browser/download/mock_download_item_impl.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 using ::testing::NiceMock;
18
19 namespace content {
20
21 namespace {
22
23 class MockDownloadRequestHandle : public DownloadRequestHandleInterface {
24 public:
25 MOCK_CONST_METHOD0(GetWebContents, WebContents*());
26 MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*());
27 MOCK_CONST_METHOD0(PauseRequest, void());
28 MOCK_CONST_METHOD0(ResumeRequest, void());
29 MOCK_CONST_METHOD0(CancelRequest, void());
30 MOCK_CONST_METHOD0(DebugString, std::string());
31 };
32
33 } // namespace
34
35 class ParallelDownloadJobForTest : public ParallelDownloadJob {
36 public:
37 ParallelDownloadJobForTest(
38 std::unique_ptr<DownloadRequestHandleInterface> request_handle)
39 : ParallelDownloadJob(std::move(request_handle)) {}
40
41 void CreateRequest(int64_t offset, int64_t length) override {
42 fake_tasks_.push_back(std::pair<int64_t, int64_t>(offset, length));
43 }
44
45 std::vector<std::pair<int64_t, int64_t>> fake_tasks_;
46
47 private:
48 DISALLOW_COPY_AND_ASSIGN(ParallelDownloadJobForTest);
49 };
50
51 class ParallelDownloadJobTest : public testing::Test {
52 public:
53 void SetUp() override {
54 item_delegate_ = base::MakeUnique<DownloadItemImplDelegate>();
55 download_item_ =
56 base::MakeUnique<NiceMock<MockDownloadItemImpl>>(item_delegate_.get());
57 job_ = base::MakeUnique<ParallelDownloadJobForTest>(
58 base::MakeUnique<MockDownloadRequestHandle>());
59 job_->Attach(download_item_.get());
60 }
61
62 void CreateRangeRequests(int64_t total_bytes,
63 int64_t bytes_received,
64 int request_num) {
65 job_->request_num_ = request_num;
66 job_->ForkParallelRequests(bytes_received, total_bytes);
67 }
68
69 void set_min_length(int64_t min_length) { job_->min_length_ = min_length; }
70
71 content::TestBrowserThreadBundle browser_threads_;
72 std::unique_ptr<DownloadItemImplDelegate> item_delegate_;
73 std::unique_ptr<MockDownloadItemImpl> download_item_;
74 std::unique_ptr<ParallelDownloadJobForTest> job_;
75 };
76
77 // Test if sub tasks are created correctly.
78 TEST_F(ParallelDownloadJobTest, CreateRangeRequests) {
79 EXPECT_TRUE(job_->fake_tasks_.empty());
80
81 // Make minimum length of each range requests extremely small.
82 set_min_length(1);
83
84 // Totally 2 requests for 100 bytes.
85 // Original request: Range:0-49, for 50 bytes.
86 // Task 1: Range:50-99, for 50 bytes.
87 CreateRangeRequests(100, 0, 2);
88 EXPECT_EQ(1, static_cast<int>(job_->fake_tasks_.size()));
89 EXPECT_EQ(50, job_->fake_tasks_[0].first);
90 EXPECT_EQ(50, job_->fake_tasks_[0].second);
91 job_->fake_tasks_.clear();
92
93 // Totally 3 requests for 100 bytes.
94 // Original request: Range:0-32, for 33 bytes.
95 // Task 1: Range:33-65, for 33 bytes.
96 // Task 2: Range:66-99, for 34 bytes.
97 CreateRangeRequests(100, 0, 3);
98 EXPECT_EQ(2, static_cast<int>(job_->fake_tasks_.size()));
99 EXPECT_EQ(33, job_->fake_tasks_[0].first);
100 EXPECT_EQ(33, job_->fake_tasks_[0].second);
101 EXPECT_EQ(66, job_->fake_tasks_[1].first);
102 EXPECT_EQ(34, job_->fake_tasks_[1].second);
103 job_->fake_tasks_.clear();
104
105 // Totally 3 requests for 100 bytes. Start from the 17th byte.
106 // Original request: Range:17-43, for 27 bytes.
107 // Task 1: Range:44-70, for 27 bytes.
108 // Task 2: Range:71-99, for 29 bytes.
109 CreateRangeRequests(100, 17, 3);
110 EXPECT_EQ(2, static_cast<int>(job_->fake_tasks_.size()));
111 EXPECT_EQ(44, job_->fake_tasks_[0].first);
112 EXPECT_EQ(27, job_->fake_tasks_[0].second);
113 EXPECT_EQ(71, job_->fake_tasks_[1].first);
114 EXPECT_EQ(29, job_->fake_tasks_[1].second);
115 job_->fake_tasks_.clear();
116
117 // Less than 2 requests, do nothing.
118 CreateRangeRequests(100, 17, 1);
119 EXPECT_TRUE(job_->fake_tasks_.empty());
120 CreateRangeRequests(100, 17, 0);
121 EXPECT_TRUE(job_->fake_tasks_.empty());
122
123 // Received bytes are no less than total bytes, do nothing.
124 CreateRangeRequests(100, 100, 3);
125 EXPECT_TRUE(job_->fake_tasks_.empty());
126 CreateRangeRequests(100, 255, 3);
127 EXPECT_TRUE(job_->fake_tasks_.empty());
128
129 // Edge cases for 0 bytes.
130 CreateRangeRequests(0, 0, 3);
131 EXPECT_TRUE(job_->fake_tasks_.empty());
132
133 // 2 bytes left for 2 additional requests. Only 1 are built.
134 CreateRangeRequests(100, 98, 3);
135 EXPECT_EQ(1, static_cast<int>(job_->fake_tasks_.size()));
136 EXPECT_EQ(99, job_->fake_tasks_[0].first);
137 EXPECT_EQ(1, job_->fake_tasks_[0].second);
138 job_->fake_tasks_.clear();
139
140 // Since minimum length is no less than bytes left, no additional requests.
141 set_min_length(1000);
142 CreateRangeRequests(100, 0, 4);
143 EXPECT_TRUE(job_->fake_tasks_.empty());
144 CreateRangeRequests(1000, 0, 4);
145 EXPECT_TRUE(job_->fake_tasks_.empty());
146
147 // Totally 4 requests for 1001 bytes. Minimum length for each request is
148 // 1000, no additional requests are created.
149 // Original request: Range:0-1000, for 1001 bytes. Since another new request
150 // won't meet the minimum length requirement.
151 set_min_length(1000);
152 CreateRangeRequests(1001, 0, 4);
153 EXPECT_TRUE(job_->fake_tasks_.empty());
154
155 // Totally 4 requests for 2000 bytes. Minimum length for each request is
156 // 1000. 1 additional request is created.
157 // Original request: Range:1000-1999 for 1000 bytes.
158 // Task 1: Range:2000-2999 for 1000 bytes.
159 set_min_length(1000);
160 CreateRangeRequests(3000, 1000, 4);
161 EXPECT_EQ(1, static_cast<int>(job_->fake_tasks_.size()));
162 EXPECT_EQ(2000, job_->fake_tasks_[0].first);
163 EXPECT_EQ(1000, job_->fake_tasks_[0].second);
164 job_->fake_tasks_.clear();
165
166 // Totally 4 requests for 4000 bytes. Start from 100 bytes. The last request
167 // takes more data.
168 // Original request: Range:100-1099 for 1000 bytes.
169 // Task 1: Range:1100-1999 for 1000 bytes.
170 // Task 2: Range:2100-3999 for 1900 bytes.
171 set_min_length(1000);
172 CreateRangeRequests(4000, 100, 4);
173 EXPECT_EQ(2, static_cast<int>(job_->fake_tasks_.size()));
174 EXPECT_EQ(1100, job_->fake_tasks_[0].first);
175 EXPECT_EQ(1000, job_->fake_tasks_[0].second);
176 EXPECT_EQ(2100, job_->fake_tasks_[1].first);
177 EXPECT_EQ(1900, job_->fake_tasks_[1].second);
178 job_->fake_tasks_.clear();
179 }
180
181 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698