Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(223)

Side by Side Diff: content/browser/background_fetch/background_fetch_job_controller_unittest.cc

Issue 2777183002: Hook up the other Background Fetch Mojo methods with the system (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/background_fetch/background_fetch_job_controller.h" 5 #include "content/browser/background_fetch/background_fetch_job_controller.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 // BrowserContext will take care of deallocating it. 148 // BrowserContext will take care of deallocating it.
149 BrowserContext::SetDownloadManagerForTesting(&browser_context_, 149 BrowserContext::SetDownloadManagerForTesting(&browser_context_,
150 download_manager_); 150 download_manager_);
151 } 151 }
152 152
153 void TearDown() override { job_controller_->Shutdown(); } 153 void TearDown() override { job_controller_->Shutdown(); }
154 154
155 void InitializeJobController( 155 void InitializeJobController(
156 const BackgroundFetchRegistrationId& registration_id) { 156 const BackgroundFetchRegistrationId& registration_id) {
157 job_controller_ = base::MakeUnique<BackgroundFetchJobController>( 157 job_controller_ = base::MakeUnique<BackgroundFetchJobController>(
158 registration_id, &browser_context_, 158 registration_id, BackgroundFetchOptions(), &browser_context_,
159 BrowserContext::GetDefaultStoragePartition(&browser_context_), 159 BrowserContext::GetDefaultStoragePartition(&browser_context_),
160 data_manager_.get(), 160 data_manager_.get(),
161 base::BindOnce(&BackgroundFetchJobControllerTest::DidCompleteJob, 161 base::BindOnce(&BackgroundFetchJobControllerTest::DidCompleteJob,
162 base::Unretained(this), registration_id)); 162 base::Unretained(this), registration_id));
163 } 163 }
164 164
165 void DidCompleteJob( 165 void DidCompleteJob(
166 const BackgroundFetchRegistrationId& original_registration_id,
166 const BackgroundFetchRegistrationId& registration_id, 167 const BackgroundFetchRegistrationId& registration_id,
167 const BackgroundFetchRegistrationId& original_registration_id) { 168 bool aborted_by_developer) {
168 ASSERT_EQ(registration_id, original_registration_id); 169 ASSERT_EQ(registration_id, original_registration_id);
169 did_complete_job_ = true; 170 did_complete_job_ = true;
171 did_abort_by_developer_ = true;
harkness 2017/03/27 16:34:12 Shouldn't this be did_abort_by_developer_ = aborte
Peter Beverloo 2017/03/28 13:34:22 Done.
170 } 172 }
171 173
172 void StartProcessing() { 174 void StartProcessing() {
173 base::RunLoop run_loop; 175 base::RunLoop run_loop;
174 BrowserThread::PostTask( 176 BrowserThread::PostTask(
175 BrowserThread::IO, FROM_HERE, 177 BrowserThread::IO, FROM_HERE,
176 base::Bind(&BackgroundFetchJobControllerTest::StartProcessingOnIO, 178 base::Bind(&BackgroundFetchJobControllerTest::StartProcessingOnIO,
177 base::Unretained(this), run_loop.QuitClosure())); 179 base::Unretained(this), run_loop.QuitClosure()));
178 run_loop.Run(); 180 run_loop.Run();
179 } 181 }
180 182
181 void StartProcessingOnIO(const base::Closure& closure) { 183 void StartProcessingOnIO(const base::Closure& closure) {
182 job_controller_->StartProcessing(); 184 job_controller_->StartProcessing();
183 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); 185 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure);
184 } 186 }
185 187
186 BackgroundFetchJobController* job_controller() { 188 BackgroundFetchJobController* job_controller() {
187 return job_controller_.get(); 189 return job_controller_.get();
188 } 190 }
189 MockDownloadManagerWithCallback* download_manager() { 191 MockDownloadManagerWithCallback* download_manager() {
190 return download_manager_; 192 return download_manager_;
191 } 193 }
192 194
193 DownloadItem::Observer* ItemObserver() const { return job_controller_.get(); } 195 DownloadItem::Observer* ItemObserver() const { return job_controller_.get(); }
194 196
195 bool did_complete_job() const { return did_complete_job_; } 197 bool did_complete_job() const { return did_complete_job_; }
198 bool did_abort_by_developer() const { return did_abort_by_developer_; }
196 199
197 FakeBackgroundFetchDataManager* data_manager() const { 200 FakeBackgroundFetchDataManager* data_manager() const {
198 return data_manager_.get(); 201 return data_manager_.get();
199 } 202 }
200 203
201 private: 204 private:
202 bool did_complete_job_ = false; 205 bool did_complete_job_ = false;
206 bool did_abort_by_developer_ = false;
203 TestBrowserThreadBundle thread_bundle_; 207 TestBrowserThreadBundle thread_bundle_;
204 TestBrowserContext browser_context_; 208 TestBrowserContext browser_context_;
205 std::unique_ptr<FakeBackgroundFetchDataManager> data_manager_; 209 std::unique_ptr<FakeBackgroundFetchDataManager> data_manager_;
206 std::unique_ptr<BackgroundFetchJobController> job_controller_; 210 std::unique_ptr<BackgroundFetchJobController> job_controller_;
207 MockDownloadManagerWithCallback* download_manager_; 211 MockDownloadManagerWithCallback* download_manager_;
208 }; 212 };
209 213
210 TEST_F(BackgroundFetchJobControllerTest, SingleRequestJob) { 214 TEST_F(BackgroundFetchJobControllerTest, SingleRequestJob) {
211 BackgroundFetchRegistrationId registration_id( 215 BackgroundFetchRegistrationId registration_id(
212 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag); 216 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag);
(...skipping 20 matching lines...) Expand all
233 EXPECT_EQ(DownloadItem::DownloadState::IN_PROGRESS, request_info.state()); 237 EXPECT_EQ(DownloadItem::DownloadState::IN_PROGRESS, request_info.state());
234 EXPECT_FALSE(did_complete_job()); 238 EXPECT_FALSE(did_complete_job());
235 239
236 // Update the item to be completed then update the observer. The JobController 240 // Update the item to be completed then update the observer. The JobController
237 // should update the JobData that the request is complete. 241 // should update the JobData that the request is complete.
238 item->SetState(DownloadItem::DownloadState::COMPLETE); 242 item->SetState(DownloadItem::DownloadState::COMPLETE);
239 ItemObserver()->OnDownloadUpdated(item); 243 ItemObserver()->OnDownloadUpdated(item);
240 244
241 EXPECT_EQ(DownloadItem::DownloadState::COMPLETE, request_info.state()); 245 EXPECT_EQ(DownloadItem::DownloadState::COMPLETE, request_info.state());
242 EXPECT_TRUE(did_complete_job()); 246 EXPECT_TRUE(did_complete_job());
247 EXPECT_FALSE(did_abort_by_developer());
243 } 248 }
244 249
245 TEST_F(BackgroundFetchJobControllerTest, MultipleRequestJob) { 250 TEST_F(BackgroundFetchJobControllerTest, MultipleRequestJob) {
246 BackgroundFetchRegistrationId registration_id( 251 BackgroundFetchRegistrationId registration_id(
247 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag); 252 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag);
248 253
249 std::vector<BackgroundFetchRequestInfo> request_infos; 254 std::vector<BackgroundFetchRequestInfo> request_infos;
250 for (int i = 0; i < 10; i++) { 255 for (int i = 0; i < 10; i++) {
251 request_infos.emplace_back(GURL(kTestUrl), base::IntToString(i)); 256 request_infos.emplace_back(GURL(kTestUrl), base::IntToString(i));
252 } 257 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 EXPECT_FALSE(did_complete_job()); 290 EXPECT_FALSE(did_complete_job());
286 291
287 // Finally, update the last request to be complete. The JobController should 292 // Finally, update the last request to be complete. The JobController should
288 // see that there are no more requests and mark the job as done. 293 // see that there are no more requests and mark the job as done.
289 ASSERT_EQ(10U, download_items.size()); 294 ASSERT_EQ(10U, download_items.size());
290 item = download_items[9].get(); 295 item = download_items[9].get();
291 item->SetState(DownloadItem::DownloadState::COMPLETE); 296 item->SetState(DownloadItem::DownloadState::COMPLETE);
292 ItemObserver()->OnDownloadUpdated(item); 297 ItemObserver()->OnDownloadUpdated(item);
293 298
294 EXPECT_TRUE(did_complete_job()); 299 EXPECT_TRUE(did_complete_job());
300 EXPECT_FALSE(did_abort_by_developer());
295 } 301 }
296 302
297 TEST_F(BackgroundFetchJobControllerTest, UpdateStorageState) { 303 TEST_F(BackgroundFetchJobControllerTest, UpdateStorageState) {
298 BackgroundFetchRegistrationId registration_id( 304 BackgroundFetchRegistrationId registration_id(
299 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag); 305 kServiceWorkerRegistrationId, url::Origin(GURL(kOrigin)), kTag);
300 306
301 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid); 307 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid);
302 request_info.set_state(DownloadItem::DownloadState::IN_PROGRESS); 308 request_info.set_state(DownloadItem::DownloadState::IN_PROGRESS);
303 data_manager()->set_next_request(&request_info); 309 data_manager()->set_next_request(&request_info);
304 InitializeJobController(registration_id); 310 InitializeJobController(registration_id);
(...skipping 14 matching lines...) Expand all
319 item->SetReceivedBytes(123); 325 item->SetReceivedBytes(123);
320 item->SetState(DownloadItem::DownloadState::COMPLETE); 326 item->SetState(DownloadItem::DownloadState::COMPLETE);
321 327
322 // Trigger the observer. The JobController should update the JobData that the 328 // Trigger the observer. The JobController should update the JobData that the
323 // request is complete and should fill in storage state. 329 // request is complete and should fill in storage state.
324 ItemObserver()->OnDownloadUpdated(item); 330 ItemObserver()->OnDownloadUpdated(item);
325 331
326 EXPECT_EQ(123, request_info.received_bytes()); 332 EXPECT_EQ(123, request_info.received_bytes());
327 EXPECT_TRUE(data_manager()->IsComplete(kJobGuid)); 333 EXPECT_TRUE(data_manager()->IsComplete(kJobGuid));
328 EXPECT_TRUE(did_complete_job()); 334 EXPECT_TRUE(did_complete_job());
335 EXPECT_FALSE(did_abort_by_developer());
329 } 336 }
330 337
331 } // namespace content 338 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698