OLD | NEW |
(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/background_fetch/background_fetch_data_manager.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/bind_helpers.h" |
| 12 #include "base/callback_helpers.h" |
| 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/run_loop.h" |
| 15 #include "content/browser/background_fetch/background_fetch_job_info.h" |
| 16 #include "content/browser/background_fetch/background_fetch_job_response_data.h" |
| 17 #include "content/browser/background_fetch/background_fetch_request_info.h" |
| 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/download_interrupt_reasons.h" |
| 20 #include "content/public/browser/download_item.h" |
| 21 #include "content/public/test/test_browser_context.h" |
| 22 #include "content/public/test/test_browser_thread_bundle.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 |
| 25 namespace content { |
| 26 namespace { |
| 27 |
| 28 const char kResource[] = "https://example.com/resource.html"; |
| 29 const char kTag[] = "TestRequestTag"; |
| 30 const char kJobOrigin[] = "https://example.com"; |
| 31 const int64_t kServiceWorkerRegistrationId = 9001; |
| 32 |
| 33 } // namespace |
| 34 |
| 35 class BackgroundFetchDataManagerTest : public ::testing::Test { |
| 36 public: |
| 37 BackgroundFetchDataManagerTest() |
| 38 : background_fetch_data_manager_( |
| 39 base::MakeUnique<BackgroundFetchDataManager>(&browser_context_)) {} |
| 40 ~BackgroundFetchDataManagerTest() override = default; |
| 41 |
| 42 protected: |
| 43 void CreateRequests(int num_requests) { |
| 44 DCHECK(num_requests > 0); |
| 45 // Create |num_requests| BackgroundFetchRequestInfo's. |
| 46 BackgroundFetchRequestInfos request_infos; |
| 47 for (int i = 0; i < num_requests; i++) { |
| 48 request_infos.push_back( |
| 49 base::MakeUnique<BackgroundFetchRequestInfo>(GURL(kResource), kTag)); |
| 50 } |
| 51 std::unique_ptr<BackgroundFetchJobInfo> job_info = |
| 52 base::MakeUnique<BackgroundFetchJobInfo>( |
| 53 "tag", url::Origin(GURL(kJobOrigin)), kServiceWorkerRegistrationId); |
| 54 job_guid_ = job_info->guid(); |
| 55 background_fetch_data_manager_->CreateRequest(std::move(job_info), |
| 56 std::move(request_infos)); |
| 57 } |
| 58 |
| 59 void GetResponse() { |
| 60 base::RunLoop run_loop; |
| 61 BackgroundFetchResponseCompleteCallback callback = |
| 62 base::Bind(&BackgroundFetchDataManagerTest::DidGetResponse, |
| 63 base::Unretained(this), run_loop.QuitClosure()); |
| 64 BrowserThread::PostTask( |
| 65 BrowserThread::IO, FROM_HERE, |
| 66 base::Bind(&BackgroundFetchDataManager::GetJobResponse, |
| 67 base::Unretained(background_fetch_data_manager()), job_guid_, |
| 68 base::Bind(&BackgroundFetchDataManagerTest::DidGetResponse, |
| 69 base::Unretained(this), run_loop.QuitClosure()))); |
| 70 run_loop.Run(); |
| 71 } |
| 72 |
| 73 void DidGetResponse(base::Closure closure, |
| 74 std::vector<std::unique_ptr<BlobHandle>> blobs) { |
| 75 blobs_ = std::move(blobs); |
| 76 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); |
| 77 } |
| 78 |
| 79 const std::string& job_guid() const { return job_guid_; } |
| 80 BackgroundFetchDataManager* background_fetch_data_manager() { |
| 81 return background_fetch_data_manager_.get(); |
| 82 } |
| 83 |
| 84 private: |
| 85 std::string job_guid_; |
| 86 TestBrowserContext browser_context_; |
| 87 std::unique_ptr<BackgroundFetchDataManager> background_fetch_data_manager_; |
| 88 TestBrowserThreadBundle thread_bundle_; |
| 89 std::vector<std::unique_ptr<BlobHandle>> blobs_; |
| 90 }; |
| 91 |
| 92 TEST_F(BackgroundFetchDataManagerTest, CompleteJob) { |
| 93 CreateRequests(10); |
| 94 BackgroundFetchDataManager* data_manager = background_fetch_data_manager(); |
| 95 std::vector<std::string> request_guids; |
| 96 |
| 97 // Get all of the fetch requests from the BackgroundFetchDataManager. |
| 98 for (int i = 0; i < 10; i++) { |
| 99 EXPECT_FALSE(data_manager->IsComplete(job_guid())); |
| 100 ASSERT_TRUE(data_manager->HasRequestsRemaining(job_guid())); |
| 101 const BackgroundFetchRequestInfo& request_info = |
| 102 data_manager->GetNextBackgroundFetchRequestInfo(job_guid()); |
| 103 request_guids.push_back(request_info.guid()); |
| 104 EXPECT_EQ(request_info.tag(), kTag); |
| 105 EXPECT_EQ(request_info.state(), |
| 106 DownloadItem::DownloadState::MAX_DOWNLOAD_STATE); |
| 107 EXPECT_EQ(request_info.interrupt_reason(), |
| 108 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE); |
| 109 } |
| 110 |
| 111 // At this point, all the fetches have been started, but none finished. |
| 112 EXPECT_FALSE(data_manager->HasRequestsRemaining(job_guid())); |
| 113 |
| 114 // Complete all of the fetch requests. |
| 115 for (int i = 0; i < 10; i++) { |
| 116 EXPECT_FALSE(data_manager->IsComplete(job_guid())); |
| 117 EXPECT_FALSE(data_manager->UpdateRequestState( |
| 118 job_guid(), request_guids[i], DownloadItem::DownloadState::COMPLETE, |
| 119 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE)); |
| 120 } |
| 121 |
| 122 // All requests are complete now. |
| 123 EXPECT_TRUE(data_manager->IsComplete(job_guid())); |
| 124 GetResponse(); |
| 125 } |
| 126 |
| 127 TEST_F(BackgroundFetchDataManagerTest, OutOfOrderCompletion) { |
| 128 CreateRequests(10); |
| 129 BackgroundFetchDataManager* data_manager = background_fetch_data_manager(); |
| 130 const std::string& job_guid = BackgroundFetchDataManagerTest::job_guid(); |
| 131 std::vector<std::string> request_guids; |
| 132 |
| 133 // Start half of the fetch requests. |
| 134 for (int i = 0; i < 5; i++) { |
| 135 ASSERT_TRUE(data_manager->HasRequestsRemaining(job_guid)); |
| 136 const BackgroundFetchRequestInfo& request_info = |
| 137 data_manager->GetNextBackgroundFetchRequestInfo(job_guid); |
| 138 request_guids.push_back(request_info.guid()); |
| 139 } |
| 140 |
| 141 // Complete all of the fetches out of order except for #1. |
| 142 DownloadItem::DownloadState complete = DownloadItem::DownloadState::COMPLETE; |
| 143 DownloadInterruptReason no_interrupt = |
| 144 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE; |
| 145 EXPECT_TRUE(data_manager->UpdateRequestState(job_guid, request_guids[3], |
| 146 complete, no_interrupt)); |
| 147 EXPECT_TRUE(data_manager->UpdateRequestState(job_guid, request_guids[2], |
| 148 complete, no_interrupt)); |
| 149 EXPECT_TRUE(data_manager->UpdateRequestState(job_guid, request_guids[4], |
| 150 complete, no_interrupt)); |
| 151 EXPECT_TRUE(data_manager->UpdateRequestState(job_guid, request_guids[0], |
| 152 complete, no_interrupt)); |
| 153 |
| 154 EXPECT_TRUE(data_manager->HasRequestsRemaining(job_guid)); |
| 155 EXPECT_FALSE(data_manager->IsComplete(job_guid)); |
| 156 |
| 157 // Start and complete the remaining requests. |
| 158 for (int i = 5; i < 10; i++) { |
| 159 const BackgroundFetchRequestInfo& request_info = |
| 160 data_manager->GetNextBackgroundFetchRequestInfo(job_guid); |
| 161 request_guids.push_back(request_info.guid()); |
| 162 data_manager->UpdateRequestState(job_guid, request_guids[i], complete, |
| 163 no_interrupt); |
| 164 } |
| 165 |
| 166 EXPECT_FALSE(data_manager->IsComplete(job_guid)); |
| 167 EXPECT_FALSE(data_manager->HasRequestsRemaining(job_guid)); |
| 168 |
| 169 // Complete the final request. |
| 170 EXPECT_FALSE(data_manager->UpdateRequestState(job_guid, request_guids[1], |
| 171 complete, no_interrupt)); |
| 172 EXPECT_TRUE(data_manager->IsComplete(job_guid)); |
| 173 } |
| 174 |
| 175 TEST_F(BackgroundFetchDataManagerTest, PauseAndResume) { |
| 176 CreateRequests(1); |
| 177 BackgroundFetchDataManager* data_manager = background_fetch_data_manager(); |
| 178 const std::string& job_guid = BackgroundFetchDataManagerTest::job_guid(); |
| 179 |
| 180 // Start the request. |
| 181 ASSERT_TRUE(data_manager->HasRequestsRemaining(job_guid)); |
| 182 const BackgroundFetchRequestInfo& request_info = |
| 183 data_manager->GetNextBackgroundFetchRequestInfo(job_guid); |
| 184 |
| 185 EXPECT_FALSE(data_manager->HasRequestsRemaining(job_guid)); |
| 186 EXPECT_FALSE(data_manager->IsComplete(job_guid)); |
| 187 |
| 188 // Set the request state to be paused. This should not complete the job. |
| 189 EXPECT_FALSE(data_manager->UpdateRequestState( |
| 190 job_guid, request_info.guid(), DownloadItem::DownloadState::INTERRUPTED, |
| 191 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN)); |
| 192 EXPECT_FALSE(data_manager->IsComplete(job_guid)); |
| 193 |
| 194 // Unpause the request. |
| 195 EXPECT_FALSE(data_manager->UpdateRequestState( |
| 196 job_guid, request_info.guid(), DownloadItem::DownloadState::IN_PROGRESS, |
| 197 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE)); |
| 198 EXPECT_FALSE(data_manager->IsComplete(job_guid)); |
| 199 |
| 200 // Complete the request. |
| 201 EXPECT_FALSE(data_manager->UpdateRequestState( |
| 202 job_guid, request_info.guid(), DownloadItem::DownloadState::COMPLETE, |
| 203 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE)); |
| 204 EXPECT_TRUE(data_manager->IsComplete(job_guid)); |
| 205 } |
| 206 |
| 207 TEST_F(BackgroundFetchDataManagerTest, CancelledRequest) { |
| 208 CreateRequests(1); |
| 209 BackgroundFetchDataManager* data_manager = background_fetch_data_manager(); |
| 210 const std::string& job_guid = BackgroundFetchDataManagerTest::job_guid(); |
| 211 |
| 212 // Start the request. |
| 213 ASSERT_TRUE(data_manager->HasRequestsRemaining(job_guid)); |
| 214 const BackgroundFetchRequestInfo& request_info = |
| 215 data_manager->GetNextBackgroundFetchRequestInfo(job_guid); |
| 216 |
| 217 EXPECT_FALSE(data_manager->HasRequestsRemaining(job_guid)); |
| 218 EXPECT_FALSE(data_manager->IsComplete(job_guid)); |
| 219 |
| 220 // Cancel the request. |
| 221 EXPECT_FALSE(data_manager->UpdateRequestState( |
| 222 job_guid, request_info.guid(), DownloadItem::DownloadState::CANCELLED, |
| 223 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE)); |
| 224 EXPECT_TRUE(data_manager->IsComplete(job_guid)); |
| 225 } |
| 226 |
| 227 TEST_F(BackgroundFetchDataManagerTest, PrepareResponse) { |
| 228 CreateRequests(1); |
| 229 BackgroundFetchDataManager* data_manager = background_fetch_data_manager(); |
| 230 const std::string& job_guid = BackgroundFetchDataManagerTest::job_guid(); |
| 231 |
| 232 const BackgroundFetchRequestInfo& request_info = |
| 233 data_manager->GetNextBackgroundFetchRequestInfo(job_guid); |
| 234 EXPECT_FALSE(data_manager->UpdateRequestState( |
| 235 job_guid, request_info.guid(), DownloadItem::DownloadState::COMPLETE, |
| 236 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE)); |
| 237 EXPECT_TRUE(data_manager->IsComplete(job_guid)); |
| 238 } |
| 239 |
| 240 } // namespace content |
OLD | NEW |