Index: content/browser/background_fetch/background_fetch_job_data_unittest.cc |
diff --git a/content/browser/background_fetch/background_fetch_job_data_unittest.cc b/content/browser/background_fetch/background_fetch_job_data_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..aea4ef2841108fced67b31d7e8edbf7681ccc45b |
--- /dev/null |
+++ b/content/browser/background_fetch/background_fetch_job_data_unittest.cc |
@@ -0,0 +1,186 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/background_fetch/background_fetch_job_data.h" |
+ |
+#include "base/memory/ptr_util.h" |
+#include "content/browser/background_fetch/background_fetch_job_info.h" |
+#include "content/browser/background_fetch/background_fetch_request_info.h" |
+#include "content/public/browser/download_interrupt_reasons.h" |
+#include "content/public/browser/download_item.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+ |
+const char kResource[] = "https://example.com/resource.html"; |
+const char kTag[] = "TestRequestTag"; |
+ |
+} // 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.
|
+ |
+namespace content { |
+ |
+class BackgroundFetchJobDataTest : public testing::Test { |
+ protected: |
+ BackgroundFetchJobDataTest() {} |
Peter Beverloo
2017/03/16 12:43:50
= default; (or delete if we can)
harkness
2017/03/19 18:20:45
Done.
|
+ |
+ BackgroundFetchJobData* CreateMinimalJobData() { |
+ DCHECK(!job_data_); |
+ // Create a JobData with a single entry. |
+ request_infos_.emplace_back(GURL(kResource), kTag); |
+ job_data_ = base::MakeUnique<BackgroundFetchJobData>(request_infos_); |
+ return job_data_.get(); |
+ } |
+ |
+ 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.
|
+ DCHECK(!job_data_); |
+ // Create 10 BackgroundFetchRequestInfos. |
+ for (int i = 0; i < 10; i++) { |
+ request_infos_.emplace_back(GURL(kResource), kTag); |
+ } |
+ job_data_ = base::MakeUnique<BackgroundFetchJobData>(request_infos_); |
+ return job_data_.get(); |
+ } |
+ |
+ const BackgroundFetchRequestInfos& request_infos() const { |
+ return request_infos_; |
+ } |
+ |
+ private: |
+ std::unique_ptr<BackgroundFetchJobData> job_data_; |
+ BackgroundFetchRequestInfos request_infos_; |
+}; |
+ |
+TEST_F(BackgroundFetchJobDataTest, CompleteJob) { |
+ BackgroundFetchJobData* job_data = CreateSmallJobData(); |
+ const BackgroundFetchRequestInfos& request_infos = |
+ 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
|
+ ASSERT_EQ(10U, request_infos.size()); |
+ |
+ // Get all of the fetch requests from the BackgroundFetchJobData. |
+ for (int i = 0; i < 10; i++) { |
+ EXPECT_FALSE(job_data->IsComplete()); |
+ ASSERT_TRUE(job_data->HasRequestsRemaining()); |
+ const BackgroundFetchRequestInfo& request_info = |
+ job_data->GetNextBackgroundFetchRequestInfo(); |
+ EXPECT_EQ(request_info.tag(), kTag); |
+ EXPECT_EQ(request_info.state(), |
+ DownloadItem::DownloadState::MAX_DOWNLOAD_STATE); |
+ EXPECT_EQ(request_info.interrupt_reason(), |
+ DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE); |
+ } |
+ |
+ // At this point, all the fetches have been started, but none finished. |
+ EXPECT_FALSE(job_data->HasRequestsRemaining()); |
+ |
+ // Complete all of the fetch requests. |
+ for (int i = 0; i < 10; i++) { |
+ EXPECT_FALSE(job_data->IsComplete()); |
+ EXPECT_FALSE(job_data->UpdateBackgroundFetchRequestState( |
+ request_infos[i].guid(), DownloadItem::DownloadState::COMPLETE, |
+ DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE)); |
+ } |
+ |
+ // All requests are complete now. |
+ EXPECT_TRUE(job_data->IsComplete()); |
+} |
+ |
+TEST_F(BackgroundFetchJobDataTest, OutOfOrderCompletion) { |
+ BackgroundFetchJobData* job_data = CreateSmallJobData(); |
+ const BackgroundFetchRequestInfos& request_infos = |
+ BackgroundFetchJobDataTest::request_infos(); |
+ ASSERT_EQ(10U, request_infos.size()); |
+ |
+ // Start half of the fetch requests. |
+ for (int i = 0; i < 5; i++) { |
+ ASSERT_TRUE(job_data->HasRequestsRemaining()); |
+ job_data->GetNextBackgroundFetchRequestInfo(); |
+ } |
+ |
+ // Complete all of the fetches out of order except for #1. |
+ DownloadItem::DownloadState complete = DownloadItem::DownloadState::COMPLETE; |
+ DownloadInterruptReason no_interrupt = |
+ DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE; |
+ EXPECT_TRUE(job_data->UpdateBackgroundFetchRequestState( |
+ request_infos[3].guid(), complete, no_interrupt)); |
+ EXPECT_TRUE(job_data->UpdateBackgroundFetchRequestState( |
+ request_infos[2].guid(), complete, no_interrupt)); |
+ EXPECT_TRUE(job_data->UpdateBackgroundFetchRequestState( |
+ request_infos[4].guid(), complete, no_interrupt)); |
+ EXPECT_TRUE(job_data->UpdateBackgroundFetchRequestState( |
+ request_infos[0].guid(), complete, no_interrupt)); |
+ |
+ EXPECT_TRUE(job_data->HasRequestsRemaining()); |
+ EXPECT_FALSE(job_data->IsComplete()); |
+ |
+ // Start and complete the remaining requests. |
+ for (int i = 5; i < 10; i++) { |
+ job_data->GetNextBackgroundFetchRequestInfo(); |
+ job_data->UpdateBackgroundFetchRequestState(request_infos[i].guid(), |
+ complete, no_interrupt); |
+ } |
+ |
+ EXPECT_FALSE(job_data->IsComplete()); |
+ EXPECT_FALSE(job_data->HasRequestsRemaining()); |
+ |
+ // Complete the final request. |
+ EXPECT_FALSE(job_data->UpdateBackgroundFetchRequestState( |
+ request_infos[1].guid(), complete, no_interrupt)); |
+ EXPECT_TRUE(job_data->IsComplete()); |
+} |
+ |
+TEST_F(BackgroundFetchJobDataTest, PauseAndResume) { |
+ BackgroundFetchJobData* job_data = CreateMinimalJobData(); |
+ const BackgroundFetchRequestInfos& request_infos = |
+ BackgroundFetchJobDataTest::request_infos(); |
+ ASSERT_EQ(1U, request_infos.size()); |
+ |
+ // Start the request. |
+ ASSERT_TRUE(job_data->HasRequestsRemaining()); |
+ const BackgroundFetchRequestInfo& request_info = |
+ job_data->GetNextBackgroundFetchRequestInfo(); |
+ |
+ EXPECT_FALSE(job_data->HasRequestsRemaining()); |
+ EXPECT_FALSE(job_data->IsComplete()); |
+ |
+ // Set the request state to be paused. This should not complete the job. |
+ EXPECT_FALSE(job_data->UpdateBackgroundFetchRequestState( |
+ request_info.guid(), DownloadItem::DownloadState::INTERRUPTED, |
+ DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN)); |
+ EXPECT_FALSE(job_data->IsComplete()); |
+ |
+ // Unpause the request. |
+ EXPECT_FALSE(job_data->UpdateBackgroundFetchRequestState( |
+ request_info.guid(), DownloadItem::DownloadState::IN_PROGRESS, |
+ DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE)); |
+ EXPECT_FALSE(job_data->IsComplete()); |
+ |
+ // Complete the request. |
+ EXPECT_FALSE(job_data->UpdateBackgroundFetchRequestState( |
+ request_info.guid(), DownloadItem::DownloadState::COMPLETE, |
+ DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE)); |
+ EXPECT_TRUE(job_data->IsComplete()); |
+} |
+ |
+TEST_F(BackgroundFetchJobDataTest, CancelledRequest) { |
+ BackgroundFetchJobData* job_data = CreateMinimalJobData(); |
+ const BackgroundFetchRequestInfos& request_infos = |
+ BackgroundFetchJobDataTest::request_infos(); |
+ ASSERT_EQ(1U, request_infos.size()); |
+ |
+ // Start the request. |
+ ASSERT_TRUE(job_data->HasRequestsRemaining()); |
+ const BackgroundFetchRequestInfo& request_info = |
+ job_data->GetNextBackgroundFetchRequestInfo(); |
+ |
+ EXPECT_FALSE(job_data->HasRequestsRemaining()); |
+ EXPECT_FALSE(job_data->IsComplete()); |
+ |
+ // Cancel the request. |
+ EXPECT_FALSE(job_data->UpdateBackgroundFetchRequestState( |
+ request_info.guid(), DownloadItem::DownloadState::CANCELLED, |
+ DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE)); |
+ EXPECT_TRUE(job_data->IsComplete()); |
+} |
+ |
+} // namespace content |