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