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

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

Issue 2689373003: Introduce ParallelDownloadJob. (Closed)
Patch Set: nits. Created 3 years, 9 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 DownloadItemImpl* download_item,
39 std::unique_ptr<DownloadRequestHandleInterface> request_handle)
40 : ParallelDownloadJob(download_item, std::move(request_handle)) {}
41
42 void CreateRequest(int64_t offset, int64_t length) override {
43 fake_tasks_.push_back(std::pair<int64_t, int64_t>(offset, length));
44 }
45
46 std::vector<std::pair<int64_t, int64_t>> fake_tasks_;
47
48 private:
49 DISALLOW_COPY_AND_ASSIGN(ParallelDownloadJobForTest);
50 };
51
52 class ParallelDownloadJobTest : public testing::Test {
53 public:
54 void SetUp() override {
55 item_delegate_ = base::MakeUnique<DownloadItemImplDelegate>();
56 download_item_ =
57 base::MakeUnique<NiceMock<MockDownloadItemImpl>>(item_delegate_.get());
58 job_ = base::MakeUnique<ParallelDownloadJobForTest>(
59 download_item_.get(), base::MakeUnique<MockDownloadRequestHandle>());
60 }
61
62 void CreateNewDownloadRequests(int64_t total_bytes,
63 int64_t bytes_received,
64 int request_num) {
65 job_->request_num_ = request_num;
66 job_->ForkRequestsForNewDownload(bytes_received, total_bytes);
67 }
68
69 content::TestBrowserThreadBundle browser_threads_;
70 std::unique_ptr<DownloadItemImplDelegate> item_delegate_;
71 std::unique_ptr<MockDownloadItemImpl> download_item_;
72 std::unique_ptr<ParallelDownloadJobForTest> job_;
73 };
74
75 // Test if sub tasks are created correctly.
76 TEST_F(ParallelDownloadJobTest, CreateNewDownloadRequests) {
77 EXPECT_TRUE(job_->fake_tasks_.empty());
78
79 // Totally 2 requests for 100 bytes.
80 // Original request: Range:0-49, for 50 bytes.
81 // Task 1: Range:50-99, for 50 bytes.
82 CreateNewDownloadRequests(100, 0, 2);
83 EXPECT_EQ(1, static_cast<int>(job_->fake_tasks_.size()));
84 EXPECT_EQ(50, job_->fake_tasks_[0].first);
85 EXPECT_EQ(50, job_->fake_tasks_[0].second);
86 job_->fake_tasks_.clear();
87
88 // Totally 3 requests for 100 bytes.
89 // Original request: Range:0-32, for 33 bytes.
90 // Task 1: Range:33-65, for 33 bytes.
91 // Task 2: Range:66-99, for 34 bytes.
92 CreateNewDownloadRequests(100, 0, 3);
93 EXPECT_EQ(2, static_cast<int>(job_->fake_tasks_.size()));
94 EXPECT_EQ(33, job_->fake_tasks_[0].first);
95 EXPECT_EQ(33, job_->fake_tasks_[0].second);
96 EXPECT_EQ(66, job_->fake_tasks_[1].first);
97 EXPECT_EQ(34, job_->fake_tasks_[1].second);
98 job_->fake_tasks_.clear();
99
100 // Totally 3 requests for 100 bytes. Start from the 17th byte.
101 // Original request: Range:17-43, for 27 bytes.
102 // Task 1: Range:44-70, for 27 bytes.
103 // Task 2: Range:71-99, for 29 bytes.
104 CreateNewDownloadRequests(100, 17, 3);
105 EXPECT_EQ(2, static_cast<int>(job_->fake_tasks_.size()));
106 EXPECT_EQ(44, job_->fake_tasks_[0].first);
107 EXPECT_EQ(27, job_->fake_tasks_[0].second);
108 EXPECT_EQ(71, job_->fake_tasks_[1].first);
109 EXPECT_EQ(29, job_->fake_tasks_[1].second);
110 job_->fake_tasks_.clear();
111
112 // Less than 2 requests, do nothing.
113 CreateNewDownloadRequests(100, 17, 1);
114 EXPECT_TRUE(job_->fake_tasks_.empty());
115 CreateNewDownloadRequests(100, 17, 0);
116 EXPECT_TRUE(job_->fake_tasks_.empty());
117
118 // Received bytes are no less than total bytes, do nothing.
119 CreateNewDownloadRequests(100, 100, 3);
120 EXPECT_TRUE(job_->fake_tasks_.empty());
121 CreateNewDownloadRequests(100, 255, 3);
122 EXPECT_TRUE(job_->fake_tasks_.empty());
123
124 // Edge cases for 0 bytes.
125 CreateNewDownloadRequests(0, 0, 3);
126 EXPECT_TRUE(job_->fake_tasks_.empty());
127
128 // 2 bytes left for 3 additional requests. Only 1 are built.
129 // Original request: Range:98-98, for 1 byte.
130 // Task 1: Range:99-99, for 1 byte.
131 CreateNewDownloadRequests(100, 98, 4);
132 EXPECT_EQ(1, static_cast<int>(job_->fake_tasks_.size()));
133 EXPECT_EQ(99, job_->fake_tasks_[0].first);
134 EXPECT_EQ(1, job_->fake_tasks_[0].second);
135 job_->fake_tasks_.clear();
136 }
137
138 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/download/parallel_download_job.cc ('k') | content/browser/download/url_downloader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698