OLD | NEW |
---|---|
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 "base/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 14 matching lines...) Expand all Loading... | |
25 const char kTestUrl[] = "http://www.example.com/example.html"; | 25 const char kTestUrl[] = "http://www.example.com/example.html"; |
26 const char kTag[] = "testTag"; | 26 const char kTag[] = "testTag"; |
27 | 27 |
28 } // namespace | 28 } // namespace |
29 | 29 |
30 namespace content { | 30 namespace content { |
31 | 31 |
32 class BackgroundFetchJobControllerTest : public ::testing::Test { | 32 class BackgroundFetchJobControllerTest : public ::testing::Test { |
33 public: | 33 public: |
34 BackgroundFetchJobControllerTest() | 34 BackgroundFetchJobControllerTest() |
35 : job_controller_( | 35 : download_manager_(new MockDownloadManager()) {} |
36 &browser_context_, | |
37 BrowserContext::GetDefaultStoragePartition(&browser_context_)), | |
38 download_manager_(new MockDownloadManager()) {} | |
39 ~BackgroundFetchJobControllerTest() override = default; | 36 ~BackgroundFetchJobControllerTest() override = default; |
40 | 37 |
41 void SetUp() override { | 38 void SetUp() override { |
42 // The download_manager_ ownership is given to the BrowserContext, and the | 39 // The download_manager_ ownership is given to the BrowserContext, and the |
43 // BrowserContext will take care of deallocating it. | 40 // BrowserContext will take care of deallocating it. |
44 BrowserContext::SetDownloadManagerForTesting(&browser_context_, | 41 BrowserContext::SetDownloadManagerForTesting(&browser_context_, |
45 download_manager_); | 42 download_manager_); |
46 } | 43 } |
47 | 44 |
48 void ProcessJob(const std::string& job_guid, | 45 void InitializeJobController(BackgroundFetchJobData* job_data) { |
49 BackgroundFetchJobData* job_data) { | 46 job_controller_ = base::MakeUnique<BackgroundFetchJobController>( |
47 kJobGuid, &browser_context_, | |
48 BrowserContext::GetDefaultStoragePartition(&browser_context_), | |
49 base::WrapUnique(job_data)); | |
50 } | |
51 | |
52 void StartProcessing() { | |
50 base::RunLoop run_loop; | 53 base::RunLoop run_loop; |
51 BrowserThread::PostTask( | 54 BrowserThread::PostTask( |
52 BrowserThread::IO, FROM_HERE, | 55 BrowserThread::IO, FROM_HERE, |
53 base::Bind(&BackgroundFetchJobControllerTest::ProcessJobOnIO, | 56 base::Bind(&BackgroundFetchJobControllerTest::StartProcessingOnIO, |
54 base::Unretained(this), job_guid, job_data, | 57 base::Unretained(this), run_loop.QuitClosure())); |
55 run_loop.QuitClosure())); | |
56 run_loop.Run(); | 58 run_loop.Run(); |
57 } | 59 } |
58 | 60 |
59 void ProcessJobOnIO(const std::string& job_guid, | 61 void StartProcessingOnIO(const base::Closure& closure) { |
60 BackgroundFetchJobData* job_data, | 62 job_controller_->StartProcessing(); |
61 const base::Closure& closure) { | |
62 job_controller_.ProcessJob(job_guid, job_data); | |
63 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); | 63 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); |
64 } | 64 } |
65 | 65 |
66 BackgroundFetchJobController* job_controller() { return &job_controller_; } | 66 BackgroundFetchJobController* job_controller() { |
67 return job_controller_.get(); | |
68 } | |
67 MockDownloadManager* download_manager() { return download_manager_; } | 69 MockDownloadManager* download_manager() { return download_manager_; } |
68 | 70 |
69 private: | 71 private: |
70 TestBrowserThreadBundle thread_bundle_; | 72 TestBrowserThreadBundle thread_bundle_; |
71 TestBrowserContext browser_context_; | 73 TestBrowserContext browser_context_; |
72 BackgroundFetchJobController job_controller_; | 74 std::unique_ptr<BackgroundFetchJobController> job_controller_; |
73 MockDownloadManager* download_manager_; | 75 MockDownloadManager* download_manager_; |
74 }; | 76 }; |
75 | 77 |
76 TEST_F(BackgroundFetchJobControllerTest, StartDownload) { | 78 TEST_F(BackgroundFetchJobControllerTest, StartDownload) { |
77 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), | 79 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), |
78 kServiceWorkerRegistrationId); | 80 kServiceWorkerRegistrationId); |
79 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid); | 81 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid); |
80 std::vector<BackgroundFetchRequestInfo> request_infos{request_info}; | 82 std::vector<BackgroundFetchRequestInfo> request_infos{request_info}; |
81 | 83 |
82 // Get a JobData to give to the JobController. The JobController then gets | 84 // Get a JobData to give to the JobController. The JobController then gets |
83 // the BackgroundFetchRequestInfos from the JobData. | 85 // the BackgroundFetchRequestInfos from the JobData. The JobController will |
84 BackgroundFetchJobData job_data(request_infos); | 86 // take ownership of the JobData. |
87 BackgroundFetchJobData* job_data = new BackgroundFetchJobData(request_infos); | |
Peter Beverloo
2017/03/09 15:18:57
Store this in a std::unique_ptr<> and std::move()
harkness
2017/03/09 18:35:31
Done.
| |
88 InitializeJobController(job_data); | |
85 | 89 |
86 EXPECT_CALL(*(download_manager()), | 90 EXPECT_CALL(*(download_manager()), |
87 DownloadUrlMock(::testing::Pointee(::testing::Property( | 91 DownloadUrlMock(::testing::Pointee(::testing::Property( |
88 &DownloadUrlParameters::url, GURL(kTestUrl))))) | 92 &DownloadUrlParameters::url, GURL(kTestUrl))))) |
89 .Times(1); | 93 .Times(1); |
90 | 94 |
91 ProcessJob(kJobGuid, &job_data); | 95 StartProcessing(); |
92 } | 96 } |
93 | 97 |
94 } // namespace content | 98 } // namespace content |
OLD | NEW |