Chromium Code Reviews| 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_job_data.h" | |
| 6 | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "content/browser/background_fetch/background_fetch_job_info.h" | |
| 9 #include "content/browser/background_fetch/background_fetch_request_info.h" | |
| 10 #include "content/public/browser/download_interrupt_reasons.h" | |
| 11 #include "content/public/browser/download_item.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 const char kResource[] = "https://example.com/resource.html"; | |
| 17 const char kTag[] = "TestRequestTag"; | |
| 18 | |
| 19 } // namespace | |
|
Peter Beverloo
2017/03/16 12:43:50
nit: you can put this in the `content` namespace,
harkness
2017/03/19 18:20:44
Done.
| |
| 20 | |
| 21 namespace content { | |
| 22 | |
| 23 class BackgroundFetchJobDataTest : public testing::Test { | |
| 24 protected: | |
| 25 BackgroundFetchJobDataTest() {} | |
|
Peter Beverloo
2017/03/16 12:43:50
= default; (or delete if we can)
harkness
2017/03/19 18:20:45
Done.
| |
| 26 | |
| 27 BackgroundFetchJobData* CreateMinimalJobData() { | |
| 28 DCHECK(!job_data_); | |
| 29 // Create a JobData with a single entry. | |
| 30 request_infos_.emplace_back(GURL(kResource), kTag); | |
| 31 job_data_ = base::MakeUnique<BackgroundFetchJobData>(request_infos_); | |
| 32 return job_data_.get(); | |
| 33 } | |
| 34 | |
| 35 BackgroundFetchJobData* CreateSmallJobData() { | |
|
Peter Beverloo
2017/03/16 12:43:50
optional: Seems like it'd be nicer to just return
harkness
2017/03/19 18:20:44
Done.
| |
| 36 DCHECK(!job_data_); | |
| 37 // Create 10 BackgroundFetchRequestInfos. | |
| 38 for (int i = 0; i < 10; i++) { | |
| 39 request_infos_.emplace_back(GURL(kResource), kTag); | |
| 40 } | |
| 41 job_data_ = base::MakeUnique<BackgroundFetchJobData>(request_infos_); | |
| 42 return job_data_.get(); | |
| 43 } | |
| 44 | |
| 45 const BackgroundFetchRequestInfos& request_infos() const { | |
| 46 return request_infos_; | |
| 47 } | |
| 48 | |
| 49 private: | |
| 50 std::unique_ptr<BackgroundFetchJobData> job_data_; | |
| 51 BackgroundFetchRequestInfos request_infos_; | |
| 52 }; | |
| 53 | |
| 54 TEST_F(BackgroundFetchJobDataTest, CompleteJob) { | |
| 55 BackgroundFetchJobData* job_data = CreateSmallJobData(); | |
| 56 const BackgroundFetchRequestInfos& request_infos = | |
| 57 BackgroundFetchJobDataTest::request_infos(); | |
|
Peter Beverloo
2017/03/16 12:43:50
Here and elsewhere - drop the `BackgroundFetchJobD
harkness
2017/03/19 18:20:45
Unfortunately it's here because of a clash in the
| |
| 58 ASSERT_EQ(10U, request_infos.size()); | |
| 59 | |
| 60 // Get all of the fetch requests from the BackgroundFetchJobData. | |
| 61 for (int i = 0; i < 10; i++) { | |
| 62 EXPECT_FALSE(job_data->IsComplete()); | |
| 63 ASSERT_TRUE(job_data->HasRequestsRemaining()); | |
| 64 const BackgroundFetchRequestInfo& request_info = | |
| 65 job_data->GetNextBackgroundFetchRequestInfo(); | |
| 66 EXPECT_EQ(request_info.tag(), kTag); | |
| 67 EXPECT_EQ(request_info.state(), | |
| 68 DownloadItem::DownloadState::MAX_DOWNLOAD_STATE); | |
| 69 EXPECT_EQ(request_info.interrupt_reason(), | |
| 70 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE); | |
| 71 } | |
| 72 | |
| 73 // At this point, all the fetches have been started, but none finished. | |
| 74 EXPECT_FALSE(job_data->HasRequestsRemaining()); | |
| 75 | |
| 76 // Complete all of the fetch requests. | |
| 77 for (int i = 0; i < 10; i++) { | |
| 78 EXPECT_FALSE(job_data->IsComplete()); | |
| 79 EXPECT_FALSE(job_data->UpdateBackgroundFetchRequestState( | |
| 80 request_infos[i].guid(), DownloadItem::DownloadState::COMPLETE, | |
| 81 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE)); | |
| 82 } | |
| 83 | |
| 84 // All requests are complete now. | |
| 85 EXPECT_TRUE(job_data->IsComplete()); | |
| 86 } | |
| 87 | |
| 88 TEST_F(BackgroundFetchJobDataTest, OutOfOrderCompletion) { | |
| 89 BackgroundFetchJobData* job_data = CreateSmallJobData(); | |
| 90 const BackgroundFetchRequestInfos& request_infos = | |
| 91 BackgroundFetchJobDataTest::request_infos(); | |
| 92 ASSERT_EQ(10U, request_infos.size()); | |
| 93 | |
| 94 // Start half of the fetch requests. | |
| 95 for (int i = 0; i < 5; i++) { | |
| 96 ASSERT_TRUE(job_data->HasRequestsRemaining()); | |
| 97 job_data->GetNextBackgroundFetchRequestInfo(); | |
| 98 } | |
| 99 | |
| 100 // Complete all of the fetches out of order except for #1. | |
| 101 DownloadItem::DownloadState complete = DownloadItem::DownloadState::COMPLETE; | |
| 102 DownloadInterruptReason no_interrupt = | |
| 103 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE; | |
| 104 EXPECT_TRUE(job_data->UpdateBackgroundFetchRequestState( | |
| 105 request_infos[3].guid(), complete, no_interrupt)); | |
| 106 EXPECT_TRUE(job_data->UpdateBackgroundFetchRequestState( | |
| 107 request_infos[2].guid(), complete, no_interrupt)); | |
| 108 EXPECT_TRUE(job_data->UpdateBackgroundFetchRequestState( | |
| 109 request_infos[4].guid(), complete, no_interrupt)); | |
| 110 EXPECT_TRUE(job_data->UpdateBackgroundFetchRequestState( | |
| 111 request_infos[0].guid(), complete, no_interrupt)); | |
| 112 | |
| 113 EXPECT_TRUE(job_data->HasRequestsRemaining()); | |
| 114 EXPECT_FALSE(job_data->IsComplete()); | |
| 115 | |
| 116 // Start and complete the remaining requests. | |
| 117 for (int i = 5; i < 10; i++) { | |
| 118 job_data->GetNextBackgroundFetchRequestInfo(); | |
| 119 job_data->UpdateBackgroundFetchRequestState(request_infos[i].guid(), | |
| 120 complete, no_interrupt); | |
| 121 } | |
| 122 | |
| 123 EXPECT_FALSE(job_data->IsComplete()); | |
| 124 EXPECT_FALSE(job_data->HasRequestsRemaining()); | |
| 125 | |
| 126 // Complete the final request. | |
| 127 EXPECT_FALSE(job_data->UpdateBackgroundFetchRequestState( | |
| 128 request_infos[1].guid(), complete, no_interrupt)); | |
| 129 EXPECT_TRUE(job_data->IsComplete()); | |
| 130 } | |
| 131 | |
| 132 TEST_F(BackgroundFetchJobDataTest, PauseAndResume) { | |
| 133 BackgroundFetchJobData* job_data = CreateMinimalJobData(); | |
| 134 const BackgroundFetchRequestInfos& request_infos = | |
| 135 BackgroundFetchJobDataTest::request_infos(); | |
| 136 ASSERT_EQ(1U, request_infos.size()); | |
| 137 | |
| 138 // Start the request. | |
| 139 ASSERT_TRUE(job_data->HasRequestsRemaining()); | |
| 140 const BackgroundFetchRequestInfo& request_info = | |
| 141 job_data->GetNextBackgroundFetchRequestInfo(); | |
| 142 | |
| 143 EXPECT_FALSE(job_data->HasRequestsRemaining()); | |
| 144 EXPECT_FALSE(job_data->IsComplete()); | |
| 145 | |
| 146 // Set the request state to be paused. This should not complete the job. | |
| 147 EXPECT_FALSE(job_data->UpdateBackgroundFetchRequestState( | |
| 148 request_info.guid(), DownloadItem::DownloadState::INTERRUPTED, | |
| 149 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN)); | |
| 150 EXPECT_FALSE(job_data->IsComplete()); | |
| 151 | |
| 152 // Unpause the request. | |
| 153 EXPECT_FALSE(job_data->UpdateBackgroundFetchRequestState( | |
| 154 request_info.guid(), DownloadItem::DownloadState::IN_PROGRESS, | |
| 155 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE)); | |
| 156 EXPECT_FALSE(job_data->IsComplete()); | |
| 157 | |
| 158 // Complete the request. | |
| 159 EXPECT_FALSE(job_data->UpdateBackgroundFetchRequestState( | |
| 160 request_info.guid(), DownloadItem::DownloadState::COMPLETE, | |
| 161 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE)); | |
| 162 EXPECT_TRUE(job_data->IsComplete()); | |
| 163 } | |
| 164 | |
| 165 TEST_F(BackgroundFetchJobDataTest, CancelledRequest) { | |
| 166 BackgroundFetchJobData* job_data = CreateMinimalJobData(); | |
| 167 const BackgroundFetchRequestInfos& request_infos = | |
| 168 BackgroundFetchJobDataTest::request_infos(); | |
| 169 ASSERT_EQ(1U, request_infos.size()); | |
| 170 | |
| 171 // Start the request. | |
| 172 ASSERT_TRUE(job_data->HasRequestsRemaining()); | |
| 173 const BackgroundFetchRequestInfo& request_info = | |
| 174 job_data->GetNextBackgroundFetchRequestInfo(); | |
| 175 | |
| 176 EXPECT_FALSE(job_data->HasRequestsRemaining()); | |
| 177 EXPECT_FALSE(job_data->IsComplete()); | |
| 178 | |
| 179 // Cancel the request. | |
| 180 EXPECT_FALSE(job_data->UpdateBackgroundFetchRequestState( | |
| 181 request_info.guid(), DownloadItem::DownloadState::CANCELLED, | |
| 182 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE)); | |
| 183 EXPECT_TRUE(job_data->IsComplete()); | |
| 184 } | |
| 185 | |
| 186 } // namespace content | |
| OLD | NEW |