Chromium Code Reviews| Index: content/browser/background_fetch/background_fetch_job_controller_unittest.cc |
| diff --git a/content/browser/background_fetch/background_fetch_job_controller_unittest.cc b/content/browser/background_fetch/background_fetch_job_controller_unittest.cc |
| index 537e61041d6294b4f000857d03e627fb95274e7a..3f01938a12f7e5e6d5effba404428b57dc51ed5b 100644 |
| --- a/content/browser/background_fetch/background_fetch_job_controller_unittest.cc |
| +++ b/content/browser/background_fetch/background_fetch_job_controller_unittest.cc |
| @@ -4,10 +4,15 @@ |
| #include "content/browser/background_fetch/background_fetch_job_controller.h" |
| +#include <string> |
| +#include <vector> |
| + |
| #include "base/bind_helpers.h" |
| #include "base/callback_helpers.h" |
| +#include "base/guid.h" |
| #include "base/memory/ptr_util.h" |
| #include "base/run_loop.h" |
| +#include "base/strings/string_number_conversions.h" |
| #include "content/browser/background_fetch/background_fetch_data_manager.h" |
| #include "content/browser/background_fetch/background_fetch_job_info.h" |
| #include "content/browser/background_fetch/background_fetch_request_info.h" |
| @@ -33,11 +38,17 @@ namespace content { |
| // Use the basic MockDownloadItem, but override it to provide a valid GUID. |
| class MockDownloadItemWithValues : public MockDownloadItem { |
|
Peter Beverloo
2017/03/10 00:55:14
How about moving //chrome/browser/ntp_snippets/fak
harkness
2017/03/10 13:50:57
Ah, good find, I hadn't seen that. I'll move it to
|
| public: |
| + MockDownloadItemWithValues(const std::string& guid) : guid_(guid) {} |
| + |
| const std::string& GetGuid() const override { return guid_; } |
| void SetGuid(const std::string& guid) { guid_ = guid; } |
| + DownloadState GetState() const override { return state_; } |
| + void SetState(DownloadState state) { state_ = state; } |
| + |
| private: |
| std::string guid_; |
| + DownloadState state_ = DownloadItem::DownloadState::IN_PROGRESS; |
| }; |
| // Use the basic MockDownloadManager, but override it so that it implements the |
| @@ -46,17 +57,28 @@ class MockDownloadManagerWithCallback : public MockDownloadManager { |
| public: |
| void DownloadUrl(std::unique_ptr<DownloadUrlParameters> params) override { |
| DownloadUrlMock(params.get()); |
| - params->callback().Run(&download_item_, DOWNLOAD_INTERRUPT_REASON_NONE); |
| + std::string guid = base::GenerateGUID(); |
| + download_items_.emplace_back( |
| + base::MakeUnique<MockDownloadItemWithValues>(guid)); |
| + params->callback().Run(download_items_.back().get(), |
| + DOWNLOAD_INTERRUPT_REASON_NONE); |
|
Peter Beverloo
2017/03/10 00:55:14
The `.back()` is a bit odd. What about:
std::
harkness
2017/03/10 13:50:57
Done.
|
| } |
| + // This is very inefficient, but is only called during shutdown. |
| DownloadItem* GetDownloadByGuid(const std::string& guid) override { |
| - return &download_item_; |
| + for (const auto& item : download_items_) { |
| + if (item->GetGuid() == guid) |
| + return item.get(); |
| + } |
| + return nullptr; |
| } |
| - MockDownloadItemWithValues* download_item() { return &download_item_; } |
| + std::vector<std::unique_ptr<MockDownloadItemWithValues>>& download_items() { |
|
Peter Beverloo
2017/03/10 00:55:14
Two thoughts:
(1) This is a test, so it'd be OK
harkness
2017/03/10 13:50:57
If the property was on BackgroundFetchJObControlle
|
| + return download_items_; |
| + } |
| private: |
| - MockDownloadItemWithValues download_item_; |
| + std::vector<std::unique_ptr<MockDownloadItemWithValues>> download_items_; |
| }; |
| class BackgroundFetchJobControllerTest : public ::testing::Test { |
| @@ -111,16 +133,12 @@ class BackgroundFetchJobControllerTest : public ::testing::Test { |
| MockDownloadManagerWithCallback* download_manager_; |
| }; |
| -TEST_F(BackgroundFetchJobControllerTest, StartDownload) { |
| +TEST_F(BackgroundFetchJobControllerTest, SingleRequestJob) { |
| BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), |
| kServiceWorkerRegistrationId); |
| BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid); |
| std::vector<BackgroundFetchRequestInfo> request_infos{request_info}; |
| - // Create a MockDownloadItem that the test can manipulate. |
| - MockDownloadItemWithValues* item = download_manager()->download_item(); |
| - item->SetGuid("foo"); |
| - |
| // Get a JobData to give to the JobController. The JobController then gets |
| // the BackgroundFetchRequestInfos from the JobData. The JobController will |
| // take ownership of the JobData. |
| @@ -133,6 +151,72 @@ TEST_F(BackgroundFetchJobControllerTest, StartDownload) { |
| .Times(1); |
| StartProcessing(); |
| + |
| + // Get one of the pending downloads from the download manager. |
| + auto& download_items = download_manager()->download_items(); |
| + EXPECT_EQ(1U, download_items.size()); |
| + MockDownloadItemWithValues* item = download_items[0].get(); |
|
Peter Beverloo
2017/03/10 00:55:14
Since we'd be accessing an invalid entry here if |
harkness
2017/03/10 13:50:57
Done.
|
| + |
| + // Update the observer with no actual change. |
| + ItemObserver()->OnDownloadUpdated(item); |
| + EXPECT_FALSE(job_data->IsComplete()); |
| + |
| + // Update the item to be completed then update the observer. The JobController |
| + // should update the JobData that the request is complete. |
| + item->SetState(DownloadItem::DownloadState::COMPLETE); |
| + ItemObserver()->OnDownloadUpdated(item); |
| + EXPECT_TRUE(job_data->IsComplete()); |
| +} |
| + |
| +TEST_F(BackgroundFetchJobControllerTest, MultipleRequestJob) { |
| + BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), |
| + kServiceWorkerRegistrationId); |
| + std::vector<BackgroundFetchRequestInfo> request_infos; |
| + for (int i = 0; i < 10; i++) { |
| + request_infos.emplace_back( |
| + BackgroundFetchRequestInfo(GURL(kTestUrl), base::IntToString(i))); |
| + } |
| + |
| + // Get a JobData to give to the JobController. The JobController then gets |
| + // the BackgroundFetchRequestInfos from the JobData. The JobController will |
| + // take ownership of the JobData. |
| + BackgroundFetchJobData* job_data = new BackgroundFetchJobData(request_infos); |
| + InitializeJobController(job_data); |
| + |
| + EXPECT_CALL(*(download_manager()), |
| + DownloadUrlMock(::testing::Pointee(::testing::Property( |
| + &DownloadUrlParameters::url, GURL(kTestUrl))))) |
| + .Times(10); |
| + |
| + StartProcessing(); |
| + |
| + // Get one of the pending downloads from the download manager. |
| + auto& download_items = download_manager()->download_items(); |
| + ASSERT_EQ(1U, download_items.size()); |
| + MockDownloadItemWithValues* item = download_items[0].get(); |
| + |
| + // Update the observer with no actual change. |
| + ItemObserver()->OnDownloadUpdated(item); |
| + EXPECT_FALSE(job_data->IsComplete()); |
| + EXPECT_EQ(1U, download_items.size()); |
| + |
| + for (size_t i = 0; i < 9; i++) { |
| + // Update the next item to be completed then update the observer. |
| + ASSERT_EQ(i + 1, download_items.size()); |
| + item = download_items[i].get(); |
| + item->SetState(DownloadItem::DownloadState::COMPLETE); |
| + ItemObserver()->OnDownloadUpdated(item); |
| + EXPECT_FALSE(job_data->IsComplete()); |
| + } |
| + EXPECT_FALSE(job_data->HasRequestsRemaining()); |
| + |
| + // Finally, update the last request to be complete. The JobController should |
| + // see that there are no more requests and mark the job as done. |
| + ASSERT_EQ(10U, download_items.size()); |
| + item = download_items[9].get(); |
| + item->SetState(DownloadItem::DownloadState::COMPLETE); |
| + ItemObserver()->OnDownloadUpdated(item); |
| + EXPECT_TRUE(job_data->IsComplete()); |
| } |
| } // namespace content |