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 "base/files/file_path.h" | |
8 #include "content/browser/background_fetch/background_fetch_context.h" | |
9 #include "content/browser/background_fetch/background_fetch_job_info.h" | |
10 #include "content/browser/background_fetch/background_fetch_request_info.h" | |
11 #include "content/browser/service_worker/embedded_worker_test_helper.h" | |
12 #include "content/browser/service_worker/service_worker_context_wrapper.h" | |
13 #include "content/public/browser/download_interrupt_reasons.h" | |
14 #include "content/public/browser/download_item.h" | |
15 #include "content/public/test/test_browser_context.h" | |
16 #include "content/public/test/test_browser_thread_bundle.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 namespace { | |
20 | |
21 const char kOrigin[] = "https://example.com/"; | |
22 const char kResource[] = "https://example.com/resource.html"; | |
23 const char kTag[] = "TestRequestTag"; | |
24 constexpr int64_t kServiceWorkerRegistrationId = 9001; | |
25 | |
26 } // namespace | |
27 | |
28 namespace content { | |
29 | |
30 class BackgroundFetchDataManagerTest : public testing::Test { | |
31 protected: | |
32 BackgroundFetchDataManagerTest() | |
33 : helper_(base::FilePath()), | |
34 background_fetch_context_(new BackgroundFetchContext( | |
35 &browser_context_, | |
36 BrowserContext::GetDefaultStoragePartition(&browser_context_), | |
37 helper_.context_wrapper())) {} | |
38 | |
39 BackgroundFetchDataManager* GetDataManager() const { | |
40 return background_fetch_context_->GetDataManagerForTesting(); | |
41 } | |
42 | |
43 private: | |
44 TestBrowserThreadBundle browser_thread_bundle_; | |
45 TestBrowserContext browser_context_; | |
46 EmbeddedWorkerTestHelper helper_; | |
47 | |
48 scoped_refptr<BackgroundFetchContext> background_fetch_context_; | |
49 }; | |
50 | |
51 TEST_F(BackgroundFetchDataManagerTest, AddRequest) { | |
52 // Create a BackgroundFetchJobInfo and a set of BackgroundFetchRequestInfos. | |
53 std::vector<BackgroundFetchRequestInfo> request_infos; | |
54 std::vector<std::string> request_guids; | |
55 for (int i = 0; i < 10; i++) { | |
56 BackgroundFetchRequestInfo request_info(GURL(kResource), kTag); | |
57 request_guids.push_back(request_info.guid()); | |
58 request_infos.push_back(std::move(request_info)); | |
59 } | |
60 std::unique_ptr<BackgroundFetchJobInfo> job_info = | |
61 base::MakeUnique<BackgroundFetchJobInfo>(kTag, url::Origin(GURL(kOrigin)), | |
62 kServiceWorkerRegistrationId); | |
63 | |
64 // Initialize a BackgroundFetchJobData with the constructed requests. | |
65 BackgroundFetchDataManager* data_manager = GetDataManager(); | |
66 std::unique_ptr<BackgroundFetchJobData> job_data = | |
67 data_manager->CreateRequest(std::move(job_info), request_infos); | |
68 | |
69 // Get all of the fetch requests from the BackgroundFetchJobData. | |
70 for (int i = 0; i < 10; i++) { | |
71 EXPECT_FALSE(job_data->IsComplete()); | |
72 ASSERT_TRUE(job_data->HasRequestsRemaining()); | |
73 const BackgroundFetchRequestInfo& request_info = | |
74 job_data->GetNextBackgroundFetchRequestInfo(); | |
75 EXPECT_EQ(request_info.tag(), kTag); | |
76 EXPECT_EQ(request_info.state(), | |
77 DownloadItem::DownloadState::MAX_DOWNLOAD_STATE); | |
78 EXPECT_EQ(request_info.interrupt_reason(), | |
79 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE); | |
80 } | |
81 | |
82 // At this point, all the fetches have been started, but none finished. | |
83 EXPECT_FALSE(job_data->HasRequestsRemaining()); | |
84 EXPECT_FALSE(job_data->IsComplete()); | |
85 | |
86 // Complete all but one of the fetch requests. | |
87 for (int i = 0; i < 9; i++) { | |
88 EXPECT_FALSE(job_data->UpdateBackgroundFetchRequestState( | |
89 request_guids[i], DownloadItem::DownloadState::COMPLETE, | |
90 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE)); | |
91 EXPECT_FALSE(job_data->IsComplete()); | |
92 } | |
93 | |
94 // Set the state of the last task to be interrupted. This should not complete | |
95 // the job. | |
96 EXPECT_FALSE(job_data->UpdateBackgroundFetchRequestState( | |
97 request_guids[9], DownloadItem::DownloadState::INTERRUPTED, | |
98 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN)); | |
99 EXPECT_FALSE(job_data->IsComplete()); | |
100 | |
101 // Complete the final fetch request. | |
102 EXPECT_FALSE(job_data->UpdateBackgroundFetchRequestState( | |
103 request_guids[9], DownloadItem::DownloadState::COMPLETE, | |
104 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE)); | |
105 EXPECT_TRUE(job_data->IsComplete()); | |
106 } | |
107 | |
108 } // namespace content | |
OLD | NEW |