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> |
| 9 #include <utility> |
8 | 10 |
9 #include "base/bind.h" | 11 #include "base/bind.h" |
10 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/files/file_util.h" |
| 14 #include "base/files/scoped_temp_dir.h" |
| 15 #include "base/guid.h" |
11 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/memory/weak_ptr.h" |
12 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 19 #include "base/time/time.h" |
13 #include "content/browser/background_fetch/background_fetch_registration_id.h" | 20 #include "content/browser/background_fetch/background_fetch_registration_id.h" |
14 #include "content/browser/service_worker/service_worker_context_core.h" | 21 #include "content/browser/service_worker/service_worker_context_core.h" |
15 #include "content/browser/service_worker/service_worker_registration.h" | 22 #include "content/browser/service_worker/service_worker_registration.h" |
16 #include "content/common/service_worker/service_worker_status_code.h" | 23 #include "content/common/service_worker/service_worker_status_code.h" |
| 24 #include "content/public/browser/browser_thread.h" |
| 25 #include "content/public/browser/download_item.h" |
| 26 #include "content/public/browser/download_url_parameters.h" |
| 27 #include "content/public/test/fake_download_item.h" |
| 28 #include "content/public/test/mock_download_manager.h" |
17 #include "url/gurl.h" | 29 #include "url/gurl.h" |
18 | 30 |
19 namespace content { | 31 namespace content { |
20 | 32 |
21 namespace { | 33 namespace { |
22 | 34 |
23 const char kTestOrigin[] = "https://example.com/"; | 35 const char kTestOrigin[] = "https://example.com/"; |
24 const char kTestScriptUrl[] = "https://example.com/sw.js"; | 36 const char kTestScriptUrl[] = "https://example.com/sw.js"; |
25 | 37 |
26 void DidRegisterServiceWorker(int64_t* out_service_worker_registration_id, | 38 void DidRegisterServiceWorker(int64_t* out_service_worker_registration_id, |
(...skipping 17 matching lines...) Expand all Loading... |
44 DCHECK(out_service_worker_registration); | 56 DCHECK(out_service_worker_registration); |
45 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | 57 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
46 | 58 |
47 *out_service_worker_registration = service_worker_registration; | 59 *out_service_worker_registration = service_worker_registration; |
48 | 60 |
49 quit_closure.Run(); | 61 quit_closure.Run(); |
50 } | 62 } |
51 | 63 |
52 } // namespace | 64 } // namespace |
53 | 65 |
| 66 // Faked download manager that will respond to known HTTP requests with a test- |
| 67 // defined response. See CreateRequestWithProvidedResponse(). |
| 68 class BackgroundFetchTestBase::RespondingDownloadManager |
| 69 : public MockDownloadManager { |
| 70 public: |
| 71 RespondingDownloadManager() : weak_ptr_factory_(this) {} |
| 72 ~RespondingDownloadManager() override = default; |
| 73 |
| 74 // Responds to requests to |url| with the |status_code| and |response_text|. |
| 75 void RegisterResponse(const GURL& url, |
| 76 int status_code, |
| 77 const std::string& response_text) { |
| 78 DCHECK_EQ(registered_responses_.count(url), 0u); |
| 79 registered_responses_[url] = std::make_pair(status_code, response_text); |
| 80 } |
| 81 |
| 82 // Called when the Background Fetch system starts a download, all information |
| 83 // for which is contained in the |params|. |
| 84 void DownloadUrl(std::unique_ptr<DownloadUrlParameters> params) override { |
| 85 auto iter = registered_responses_.find(params->url()); |
| 86 if (iter == registered_responses_.end()) |
| 87 return; |
| 88 |
| 89 std::unique_ptr<FakeDownloadItem> download_item = |
| 90 base::MakeUnique<FakeDownloadItem>(); |
| 91 |
| 92 download_item->SetURL(params->url()); |
| 93 download_item->SetUrlChain({params->url()}); |
| 94 download_item->SetState(DownloadItem::DownloadState::IN_PROGRESS); |
| 95 download_item->SetGuid(base::GenerateGUID()); |
| 96 download_item->SetStartTime(base::Time::Now()); |
| 97 |
| 98 // Asynchronously invoke the callback set on the |params|, and then continue |
| 99 // dealing with the response in this class. |
| 100 BrowserThread::PostTaskAndReply( |
| 101 BrowserThread::UI, FROM_HERE, |
| 102 base::Bind(params->callback(), download_item.get(), |
| 103 DOWNLOAD_INTERRUPT_REASON_NONE), |
| 104 base::Bind(&RespondingDownloadManager::DidStartDownload, |
| 105 weak_ptr_factory_.GetWeakPtr(), download_item.get())); |
| 106 |
| 107 download_items_.push_back(std::move(download_item)); |
| 108 } |
| 109 |
| 110 private: |
| 111 // Called when the download has been "started" by the download manager. This |
| 112 // is where we finish the download by sending a single update. |
| 113 void DidStartDownload(FakeDownloadItem* download_item) { |
| 114 auto iter = registered_responses_.find(download_item->GetURL()); |
| 115 DCHECK(iter != registered_responses_.end()); |
| 116 |
| 117 const ResponseInfo& response_info = iter->second; |
| 118 |
| 119 download_item->SetState(DownloadItem::DownloadState::COMPLETE); |
| 120 download_item->SetEndTime(base::Time::Now()); |
| 121 |
| 122 base::FilePath response_path; |
| 123 if (!temp_directory_.IsValid()) |
| 124 ASSERT_TRUE(temp_directory_.CreateUniqueTempDir()); |
| 125 |
| 126 // Write the |response_info|'s response_text to a temporary file. |
| 127 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory_.GetPath(), |
| 128 &response_path)); |
| 129 |
| 130 ASSERT_NE(-1 /* error */, |
| 131 base::WriteFile(response_path, response_info.second.c_str(), |
| 132 response_info.second.size())); |
| 133 |
| 134 download_item->SetTargetFilePath(response_path); |
| 135 download_item->SetReceivedBytes(response_info.second.size()); |
| 136 |
| 137 // Notify the Job Controller about the download having been updated. |
| 138 download_item->NotifyDownloadUpdated(); |
| 139 } |
| 140 |
| 141 using ResponseInfo = |
| 142 std::pair<int /* status_code */, std::string /* response_text */>; |
| 143 |
| 144 // Map of URL to the response information associated with that URL. |
| 145 std::map<GURL, ResponseInfo> registered_responses_; |
| 146 |
| 147 // Only used to guarantee the lifetime of the created FakeDownloadItems. |
| 148 std::vector<std::unique_ptr<FakeDownloadItem>> download_items_; |
| 149 |
| 150 // Temporary directory in which successfully downloaded files will be stored. |
| 151 base::ScopedTempDir temp_directory_; |
| 152 |
| 153 base::WeakPtrFactory<RespondingDownloadManager> weak_ptr_factory_; |
| 154 |
| 155 DISALLOW_COPY_AND_ASSIGN(RespondingDownloadManager); |
| 156 }; |
| 157 |
54 BackgroundFetchTestBase::BackgroundFetchTestBase() | 158 BackgroundFetchTestBase::BackgroundFetchTestBase() |
55 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 159 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
56 origin_(GURL(kTestOrigin)) {} | 160 origin_(GURL(kTestOrigin)) {} |
57 | 161 |
58 BackgroundFetchTestBase::~BackgroundFetchTestBase() { | 162 BackgroundFetchTestBase::~BackgroundFetchTestBase() { |
59 DCHECK(set_up_called_); | 163 DCHECK(set_up_called_); |
60 DCHECK(tear_down_called_); | 164 DCHECK(tear_down_called_); |
61 } | 165 } |
62 | 166 |
63 void BackgroundFetchTestBase::SetUp() { | 167 void BackgroundFetchTestBase::SetUp() { |
| 168 download_manager_ = new RespondingDownloadManager(); |
| 169 |
| 170 // The |download_manager_| ownership is given to the BrowserContext, and the |
| 171 // BrowserContext will take care of deallocating it. |
| 172 BrowserContext::SetDownloadManagerForTesting(browser_context(), |
| 173 download_manager_); |
| 174 |
64 set_up_called_ = true; | 175 set_up_called_ = true; |
65 } | 176 } |
66 | 177 |
67 void BackgroundFetchTestBase::TearDown() { | 178 void BackgroundFetchTestBase::TearDown() { |
| 179 EXPECT_CALL(*download_manager_, Shutdown()).Times(1); |
| 180 |
68 service_worker_registrations_.clear(); | 181 service_worker_registrations_.clear(); |
69 tear_down_called_ = true; | 182 tear_down_called_ = true; |
70 } | 183 } |
71 | 184 |
72 bool BackgroundFetchTestBase::CreateRegistrationId( | 185 bool BackgroundFetchTestBase::CreateRegistrationId( |
73 const std::string& tag, | 186 const std::string& tag, |
74 BackgroundFetchRegistrationId* registration_id) { | 187 BackgroundFetchRegistrationId* registration_id) { |
75 DCHECK(registration_id); | 188 DCHECK(registration_id); |
76 DCHECK(registration_id->is_null()); | 189 DCHECK(registration_id->is_null()); |
77 | 190 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 } | 228 } |
116 | 229 |
117 *registration_id = BackgroundFetchRegistrationId( | 230 *registration_id = BackgroundFetchRegistrationId( |
118 service_worker_registration->id(), origin_, tag); | 231 service_worker_registration->id(), origin_, tag); |
119 | 232 |
120 service_worker_registrations_.push_back( | 233 service_worker_registrations_.push_back( |
121 std::move(service_worker_registration)); | 234 std::move(service_worker_registration)); |
122 return true; | 235 return true; |
123 } | 236 } |
124 | 237 |
| 238 // Creates a ServiceWorkerFetchRequest instance for the given details and |
| 239 // provides a faked response with |status_code| and |response_text| to the |
| 240 // download manager, that will resolve the download with that information. |
| 241 ServiceWorkerFetchRequest |
| 242 BackgroundFetchTestBase::CreateRequestWithProvidedResponse( |
| 243 const std::string& method, |
| 244 const std::string& url_string, |
| 245 int status_code, |
| 246 const std::string& response_text) { |
| 247 GURL url(url_string); |
| 248 |
| 249 // Register the |status_code| and |response_text| with the download manager. |
| 250 download_manager_->RegisterResponse(GURL(url_string), status_code, |
| 251 response_text); |
| 252 |
| 253 // Create a ServiceWorkerFetchRequest request with the same information. |
| 254 return ServiceWorkerFetchRequest(url, method, ServiceWorkerHeaderMap(), |
| 255 Referrer(), false /* is_reload */); |
| 256 } |
| 257 |
| 258 MockDownloadManager* BackgroundFetchTestBase::download_manager() { |
| 259 return download_manager_; |
| 260 } |
| 261 |
125 } // namespace content | 262 } // namespace content |
OLD | NEW |