1 // Copyright 2018 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 "components/offline_pages/core/prefetch/test_download_service.h"
6
7 #include "base/bind.h"
8 #include "base/files/file.h"
9 #include "base/files/file_util.h"
10 #include "base/strings/strcat.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/test/bind_test_util.h"
13 #include "base/threading/thread_task_runner_handle.h"
14 #include "components/download/public/background_service/download_metadata.h"
15 #include "components/download/public/background_service/service_config.h"
16 #include "components/offline_pages/core/prefetch/test_download_client.h"
17
18 namespace offline_pages {
19
20 namespace {
21
22 // Implementation of ServiceConfig used for testing. This is never actually
23 // constructed.
24 class TestServiceConfig : public download::ServiceConfig {
25 public:
26 TestServiceConfig() = default;
27 ~TestServiceConfig() override = default;
28
29 // ServiceConfig implementation.
GetMaxScheduledDownloadsPerClient() const30 uint32_t GetMaxScheduledDownloadsPerClient() const override { return 0; }
GetMaxConcurrentDownloads() const31 uint32_t GetMaxConcurrentDownloads() const override { return 0; }
GetFileKeepAliveTime() const32 const base::TimeDelta& GetFileKeepAliveTime() const override {
33 return time_delta_;
34 }
35
36 private:
37 base::TimeDelta time_delta_;
38
39 DISALLOW_COPY_AND_ASSIGN(TestServiceConfig);
40 };
41
42 } // namespace
43
44 TestDownloadService::TestDownloadService() = default;
45 TestDownloadService::~TestDownloadService() = default;
46
GetConfig()47 const download::ServiceConfig& TestDownloadService::GetConfig() {
48 NOTIMPLEMENTED();
49 static TestServiceConfig config;
50 return config;
51 }
52
StartDownload(const download::DownloadParams & download_params)53 void TestDownloadService::StartDownload(
54 const download::DownloadParams& download_params) {
55 if (!download_dir_.IsValid())
56 CHECK(download_dir_.CreateUniqueTempDir());
57 base::ThreadTaskRunnerHandle::Get()->PostTask(
58 FROM_HERE,
59 base::BindRepeating(download_params.callback, download_params.guid,
60 download::DownloadParams::ACCEPTED));
61 base::ThreadTaskRunnerHandle::Get()->PostTask(
62 FROM_HERE, base::BindOnce(&TestDownloadService::FinishDownload,
63 base::Unretained(this), download_params.guid));
64 }
65
FinishDownload(const std::string & guid)66 void TestDownloadService::FinishDownload(const std::string& guid) {
67 base::FilePath path = download_dir_.GetPath().AppendASCII(
68 base::StrCat({"dl_", base::NumberToString(next_file_id_++)}));
69 const int file_size = static_cast<int>(test_file_data_.size());
70 CHECK_EQ(file_size, base::WriteFile(path, test_file_data_.data(), file_size));
71 client_->OnDownloadSucceeded(
72 guid, download::CompletionInfo(path, test_file_data_.size(),
73 std::vector<GURL>(), nullptr));
74 }
75
SetTestFileData(const std::string & data)76 void TestDownloadService::SetTestFileData(const std::string& data) {
77 test_file_data_ = data;
78 }
79
OnStartScheduledTask(download::DownloadTaskType task_type,download::TaskFinishedCallback callback)80 void TestDownloadService::OnStartScheduledTask(
81 download::DownloadTaskType task_type,
82 download::TaskFinishedCallback callback) {
83 NOTIMPLEMENTED();
84 }
OnStopScheduledTask(download::DownloadTaskType task_type)85 bool TestDownloadService::OnStopScheduledTask(
86 download::DownloadTaskType task_type) {
87 NOTIMPLEMENTED();
88 return false;
89 }
GetStatus()90 download::DownloadService::ServiceStatus TestDownloadService::GetStatus() {
91 NOTIMPLEMENTED();
92 return DownloadService::ServiceStatus();
93 }
PauseDownload(const std::string & guid)94 void TestDownloadService::PauseDownload(const std::string& guid) {
95 NOTIMPLEMENTED();
96 }
ResumeDownload(const std::string & guid)97 void TestDownloadService::ResumeDownload(const std::string& guid) {
98 NOTIMPLEMENTED();
99 }
CancelDownload(const std::string & guid)100 void TestDownloadService::CancelDownload(const std::string& guid) {
101 NOTIMPLEMENTED();
102 }
ChangeDownloadCriteria(const std::string & guid,const download::SchedulingParams & params)103 void TestDownloadService::ChangeDownloadCriteria(
104 const std::string& guid,
105 const download::SchedulingParams& params) {
106 NOTIMPLEMENTED();
107 }
GetLogger()108 download::Logger* TestDownloadService::GetLogger() {
109 NOTIMPLEMENTED();
110 return nullptr;
111 }
112
113 } // namespace offline_pages
114