| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/background_fetch/background_fetch_test_base.h" | 5 #include "content/browser/background_fetch/background_fetch_test_base.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 DCHECK(out_service_worker_registration); | 56 DCHECK(out_service_worker_registration); |
| 57 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | 57 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
| 58 | 58 |
| 59 *out_service_worker_registration = service_worker_registration; | 59 *out_service_worker_registration = service_worker_registration; |
| 60 | 60 |
| 61 quit_closure.Run(); | 61 quit_closure.Run(); |
| 62 } | 62 } |
| 63 | 63 |
| 64 } // namespace | 64 } // namespace |
| 65 | 65 |
| 66 // ----------------------------------------------------------------------------- |
| 67 // TestResponse |
| 68 |
| 69 BackgroundFetchTestBase::TestResponse::TestResponse() = default; |
| 70 |
| 71 BackgroundFetchTestBase::TestResponse::~TestResponse() = default; |
| 72 |
| 73 // ----------------------------------------------------------------------------- |
| 74 // TestResponseBuilder |
| 75 |
| 76 BackgroundFetchTestBase::TestResponseBuilder::TestResponseBuilder( |
| 77 int response_code) |
| 78 : response_(base::MakeUnique<BackgroundFetchTestBase::TestResponse>()) { |
| 79 response_->headers = make_scoped_refptr(new net::HttpResponseHeaders( |
| 80 "HTTP/1.1 " + std::to_string(response_code))); |
| 81 } |
| 82 |
| 83 BackgroundFetchTestBase::TestResponseBuilder::~TestResponseBuilder() = default; |
| 84 |
| 85 BackgroundFetchTestBase::TestResponseBuilder& |
| 86 BackgroundFetchTestBase::TestResponseBuilder::AddResponseHeader( |
| 87 const std::string& name, |
| 88 const std::string& value) { |
| 89 DCHECK(response_); |
| 90 response_->headers->AddHeader(name + ": " + value); |
| 91 return *this; |
| 92 } |
| 93 |
| 94 BackgroundFetchTestBase::TestResponseBuilder& |
| 95 BackgroundFetchTestBase::TestResponseBuilder::SetResponseData( |
| 96 std::string data) { |
| 97 DCHECK(response_); |
| 98 response_->data.swap(data); |
| 99 return *this; |
| 100 } |
| 101 |
| 102 std::unique_ptr<BackgroundFetchTestBase::TestResponse> |
| 103 BackgroundFetchTestBase::TestResponseBuilder::Build() { |
| 104 return std::move(response_); |
| 105 } |
| 106 |
| 107 // ----------------------------------------------------------------------------- |
| 108 // RespondingDownloadManager |
| 109 |
| 66 // Faked download manager that will respond to known HTTP requests with a test- | 110 // Faked download manager that will respond to known HTTP requests with a test- |
| 67 // defined response. See CreateRequestWithProvidedResponse(). | 111 // defined response. See CreateRequestWithProvidedResponse(). |
| 68 class BackgroundFetchTestBase::RespondingDownloadManager | 112 class BackgroundFetchTestBase::RespondingDownloadManager |
| 69 : public MockDownloadManager { | 113 : public MockDownloadManager { |
| 70 public: | 114 public: |
| 71 RespondingDownloadManager() : weak_ptr_factory_(this) {} | 115 RespondingDownloadManager() : weak_ptr_factory_(this) {} |
| 72 ~RespondingDownloadManager() override = default; | 116 ~RespondingDownloadManager() override = default; |
| 73 | 117 |
| 74 // Responds to requests to |url| with the |status_code| and |response_text|. | 118 // Responds to requests to |url| with the given |response|. |
| 75 void RegisterResponse(const GURL& url, | 119 void RegisterResponse(const GURL& url, |
| 76 int status_code, | 120 std::unique_ptr<TestResponse> response) { |
| 77 const std::string& response_text) { | |
| 78 DCHECK_EQ(registered_responses_.count(url), 0u); | 121 DCHECK_EQ(registered_responses_.count(url), 0u); |
| 79 registered_responses_[url] = std::make_pair(status_code, response_text); | 122 registered_responses_[url] = std::move(response); |
| 80 } | 123 } |
| 81 | 124 |
| 82 // Called when the Background Fetch system starts a download, all information | 125 // Called when the Background Fetch system starts a download, all information |
| 83 // for which is contained in the |params|. | 126 // for which is contained in the |params|. |
| 84 void DownloadUrl(std::unique_ptr<DownloadUrlParameters> params) override { | 127 void DownloadUrl(std::unique_ptr<DownloadUrlParameters> params) override { |
| 85 auto iter = registered_responses_.find(params->url()); | 128 auto iter = registered_responses_.find(params->url()); |
| 86 if (iter == registered_responses_.end()) | 129 if (iter == registered_responses_.end()) |
| 87 return; | 130 return; |
| 88 | 131 |
| 132 TestResponse* response = iter->second.get(); |
| 133 |
| 89 std::unique_ptr<FakeDownloadItem> download_item = | 134 std::unique_ptr<FakeDownloadItem> download_item = |
| 90 base::MakeUnique<FakeDownloadItem>(); | 135 base::MakeUnique<FakeDownloadItem>(); |
| 91 | 136 |
| 92 download_item->SetURL(params->url()); | 137 download_item->SetURL(params->url()); |
| 93 download_item->SetUrlChain({params->url()}); | 138 download_item->SetUrlChain({params->url()}); |
| 94 download_item->SetState(DownloadItem::DownloadState::IN_PROGRESS); | 139 download_item->SetState(DownloadItem::DownloadState::IN_PROGRESS); |
| 95 download_item->SetGuid(base::GenerateGUID()); | 140 download_item->SetGuid(base::GenerateGUID()); |
| 96 download_item->SetStartTime(base::Time::Now()); | 141 download_item->SetStartTime(base::Time::Now()); |
| 142 download_item->SetResponseHeaders(response->headers); |
| 97 | 143 |
| 98 // Asynchronously invoke the callback set on the |params|, and then continue | 144 // Asynchronously invoke the callback set on the |params|, and then continue |
| 99 // dealing with the response in this class. | 145 // dealing with the response in this class. |
| 100 BrowserThread::PostTaskAndReply( | 146 BrowserThread::PostTaskAndReply( |
| 101 BrowserThread::UI, FROM_HERE, | 147 BrowserThread::UI, FROM_HERE, |
| 102 base::Bind(params->callback(), download_item.get(), | 148 base::Bind(params->callback(), download_item.get(), |
| 103 DOWNLOAD_INTERRUPT_REASON_NONE), | 149 DOWNLOAD_INTERRUPT_REASON_NONE), |
| 104 base::Bind(&RespondingDownloadManager::DidStartDownload, | 150 base::Bind(&RespondingDownloadManager::DidStartDownload, |
| 105 weak_ptr_factory_.GetWeakPtr(), download_item.get())); | 151 weak_ptr_factory_.GetWeakPtr(), download_item.get())); |
| 106 | 152 |
| 107 download_items_.push_back(std::move(download_item)); | 153 download_items_.push_back(std::move(download_item)); |
| 108 } | 154 } |
| 109 | 155 |
| 110 private: | 156 private: |
| 111 // Called when the download has been "started" by the download manager. This | 157 // Called when the download has been "started" by the download manager. This |
| 112 // is where we finish the download by sending a single update. | 158 // is where we finish the download by sending a single update. |
| 113 void DidStartDownload(FakeDownloadItem* download_item) { | 159 void DidStartDownload(FakeDownloadItem* download_item) { |
| 114 auto iter = registered_responses_.find(download_item->GetURL()); | 160 auto iter = registered_responses_.find(download_item->GetURL()); |
| 115 DCHECK(iter != registered_responses_.end()); | 161 DCHECK(iter != registered_responses_.end()); |
| 116 | 162 |
| 117 const ResponseInfo& response_info = iter->second; | 163 TestResponse* response = iter->second.get(); |
| 118 | 164 |
| 119 download_item->SetState(DownloadItem::DownloadState::COMPLETE); | 165 download_item->SetState(DownloadItem::DownloadState::COMPLETE); |
| 120 download_item->SetEndTime(base::Time::Now()); | 166 download_item->SetEndTime(base::Time::Now()); |
| 121 | 167 |
| 122 base::FilePath response_path; | 168 base::FilePath response_path; |
| 123 if (!temp_directory_.IsValid()) | 169 if (!temp_directory_.IsValid()) |
| 124 ASSERT_TRUE(temp_directory_.CreateUniqueTempDir()); | 170 ASSERT_TRUE(temp_directory_.CreateUniqueTempDir()); |
| 125 | 171 |
| 126 // Write the |response_info|'s response_text to a temporary file. | 172 // Write the |response|'s data to a temporary file. |
| 127 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory_.GetPath(), | 173 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory_.GetPath(), |
| 128 &response_path)); | 174 &response_path)); |
| 129 | 175 |
| 130 ASSERT_NE(-1 /* error */, | 176 ASSERT_NE(-1 /* error */, |
| 131 base::WriteFile(response_path, response_info.second.c_str(), | 177 base::WriteFile(response_path, response->data.c_str(), |
| 132 response_info.second.size())); | 178 response->data.size())); |
| 133 | 179 |
| 134 download_item->SetTargetFilePath(response_path); | 180 download_item->SetTargetFilePath(response_path); |
| 135 download_item->SetReceivedBytes(response_info.second.size()); | 181 download_item->SetReceivedBytes(response->data.size()); |
| 136 download_item->SetMimeType("text/plain"); | 182 download_item->SetMimeType("text/plain"); |
| 137 | 183 |
| 138 // Notify the Job Controller about the download having been updated. | 184 // Notify the Job Controller about the download having been updated. |
| 139 download_item->NotifyDownloadUpdated(); | 185 download_item->NotifyDownloadUpdated(); |
| 140 } | 186 } |
| 141 | 187 |
| 142 using ResponseInfo = | |
| 143 std::pair<int /* status_code */, std::string /* response_text */>; | |
| 144 | |
| 145 // Map of URL to the response information associated with that URL. | 188 // Map of URL to the response information associated with that URL. |
| 146 std::map<GURL, ResponseInfo> registered_responses_; | 189 std::map<GURL, std::unique_ptr<TestResponse>> registered_responses_; |
| 147 | 190 |
| 148 // Only used to guarantee the lifetime of the created FakeDownloadItems. | 191 // Only used to guarantee the lifetime of the created FakeDownloadItems. |
| 149 std::vector<std::unique_ptr<FakeDownloadItem>> download_items_; | 192 std::vector<std::unique_ptr<FakeDownloadItem>> download_items_; |
| 150 | 193 |
| 151 // Temporary directory in which successfully downloaded files will be stored. | 194 // Temporary directory in which successfully downloaded files will be stored. |
| 152 base::ScopedTempDir temp_directory_; | 195 base::ScopedTempDir temp_directory_; |
| 153 | 196 |
| 154 base::WeakPtrFactory<RespondingDownloadManager> weak_ptr_factory_; | 197 base::WeakPtrFactory<RespondingDownloadManager> weak_ptr_factory_; |
| 155 | 198 |
| 156 DISALLOW_COPY_AND_ASSIGN(RespondingDownloadManager); | 199 DISALLOW_COPY_AND_ASSIGN(RespondingDownloadManager); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 } | 272 } |
| 230 | 273 |
| 231 *registration_id = BackgroundFetchRegistrationId( | 274 *registration_id = BackgroundFetchRegistrationId( |
| 232 service_worker_registration->id(), origin_, tag); | 275 service_worker_registration->id(), origin_, tag); |
| 233 | 276 |
| 234 service_worker_registrations_.push_back( | 277 service_worker_registrations_.push_back( |
| 235 std::move(service_worker_registration)); | 278 std::move(service_worker_registration)); |
| 236 return true; | 279 return true; |
| 237 } | 280 } |
| 238 | 281 |
| 239 // Creates a ServiceWorkerFetchRequest instance for the given details and | |
| 240 // provides a faked response with |status_code| and |response_text| to the | |
| 241 // download manager, that will resolve the download with that information. | |
| 242 ServiceWorkerFetchRequest | 282 ServiceWorkerFetchRequest |
| 243 BackgroundFetchTestBase::CreateRequestWithProvidedResponse( | 283 BackgroundFetchTestBase::CreateRequestWithProvidedResponse( |
| 244 const std::string& method, | 284 const std::string& method, |
| 245 const std::string& url_string, | 285 const std::string& url, |
| 246 int status_code, | 286 std::unique_ptr<TestResponse> response) { |
| 247 const std::string& response_text) { | 287 GURL gurl(url); |
| 248 GURL url(url_string); | |
| 249 | 288 |
| 250 // Register the |status_code| and |response_text| with the download manager. | 289 // Register the |response| with the faked download manager. |
| 251 download_manager_->RegisterResponse(GURL(url_string), status_code, | 290 download_manager_->RegisterResponse(gurl, std::move(response)); |
| 252 response_text); | |
| 253 | 291 |
| 254 // Create a ServiceWorkerFetchRequest request with the same information. | 292 // Create a ServiceWorkerFetchRequest request with the same information. |
| 255 return ServiceWorkerFetchRequest(url, method, ServiceWorkerHeaderMap(), | 293 return ServiceWorkerFetchRequest(gurl, method, ServiceWorkerHeaderMap(), |
| 256 Referrer(), false /* is_reload */); | 294 Referrer(), false /* is_reload */); |
| 257 } | 295 } |
| 258 | 296 |
| 259 MockDownloadManager* BackgroundFetchTestBase::download_manager() { | 297 MockDownloadManager* BackgroundFetchTestBase::download_manager() { |
| 260 return download_manager_; | 298 return download_manager_; |
| 261 } | 299 } |
| 262 | 300 |
| 263 } // namespace content | 301 } // namespace content |
| OLD | NEW |