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..b098ee7c88bfa48d9a0c013bd449ef152624ff67 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 { |
| 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,29 @@ 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(); |
| + std::unique_ptr<MockDownloadItemWithValues> item = |
| + base::MakeUnique<MockDownloadItemWithValues>(guid); |
| + params->callback().Run(item.get(), DOWNLOAD_INTERRUPT_REASON_NONE); |
| + download_items_.push_back(std::move(item)); |
| } |
| + // This is very inefficient, but is only called during shutdown. |
|
Peter Beverloo
2017/03/13 14:05:27
nit: it would be good to clarify why. O(n) is not
harkness
2017/03/13 15:53:13
Updated. Based on our discussion last week, if we'
|
| 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_; } |
| + const std::vector<std::unique_ptr<MockDownloadItemWithValues>>& |
| + download_items() const { |
| + return download_items_; |
| + } |
| private: |
| - MockDownloadItemWithValues download_item_; |
| + std::vector<std::unique_ptr<MockDownloadItemWithValues>> download_items_; |
| }; |
| class BackgroundFetchJobControllerTest : public ::testing::Test { |
| @@ -111,16 +134,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 +152,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(); |
| + 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()); |
| + |
| + // 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))); |
|
Peter Beverloo
2017/03/13 14:05:27
This is not how you'd use emplace_back, right? Sho
harkness
2017/03/13 15:53:13
Ack, of course you're right. Updated.
|
| + } |
| + |
| + // 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()); |
| + ASSERT_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 |