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" |
11 #include "content/browser/background_fetch/background_fetch_data_manager.h" | 11 #include "content/browser/background_fetch/background_fetch_data_manager.h" |
12 #include "content/browser/background_fetch/background_fetch_job_info.h" | 12 #include "content/browser/background_fetch/background_fetch_job_info.h" |
13 #include "content/browser/background_fetch/background_fetch_request_info.h" | 13 #include "content/browser/background_fetch/background_fetch_request_info.h" |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/test/mock_download_item.h" | |
15 #include "content/public/test/mock_download_manager.h" | 16 #include "content/public/test/mock_download_manager.h" |
16 #include "content/public/test/test_browser_context.h" | 17 #include "content/public/test/test_browser_context.h" |
17 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 const char kOrigin[] = "https://example.com/"; | 23 const char kOrigin[] = "https://example.com/"; |
23 const char kJobGuid[] = "TestRequestGuid"; | 24 const char kJobGuid[] = "TestRequestGuid"; |
24 constexpr int64_t kServiceWorkerRegistrationId = 9001; | 25 constexpr int64_t kServiceWorkerRegistrationId = 9001; |
25 const char kTestUrl[] = "http://www.example.com/example.html"; | 26 const char kTestUrl[] = "http://www.example.com/example.html"; |
26 const char kTag[] = "testTag"; | 27 const char kTag[] = "testTag"; |
27 | 28 |
28 } // namespace | 29 } // namespace |
29 | 30 |
30 namespace content { | 31 namespace content { |
31 | 32 |
33 // Use the basic MockDownloadItem, but override it to provide a valid GUID. | |
34 class MockDownloadItemWithValues : public MockDownloadItem { | |
Peter Beverloo
2017/03/08 14:58:19
WithValues -> WithGuid?
harkness
2017/03/10 13:33:54
We'll be adding more state to this item, so I'd ra
| |
35 public: | |
36 const std::string& GetGuid() const override { return guid_; } | |
37 void SetGuid(const std::string& guid) { guid_ = guid; } | |
38 | |
39 private: | |
40 std::string guid_; | |
41 }; | |
42 | |
43 // Use the basic MockDownloadManager, but override it so that it implements the | |
44 // functionality that the JobController requires. | |
45 class MockDownloadManagerWithCallback : public MockDownloadManager { | |
46 public: | |
47 void DownloadUrl(std::unique_ptr<DownloadUrlParameters> params) override { | |
48 DownloadUrlMock(params.get()); | |
49 params->callback().Run(&download_item_, DOWNLOAD_INTERRUPT_REASON_NONE); | |
Peter Beverloo
2017/03/08 14:58:19
Should this perhaps be a PostTask() to make sure w
harkness
2017/03/10 13:33:54
Good idea.
| |
50 } | |
51 | |
52 DownloadItem* GetDownloadByGuid(const std::string& guid) override { | |
53 return &download_item_; | |
Peter Beverloo
2017/03/08 14:58:19
DCHECK on the `guid`?
harkness
2017/03/10 13:33:54
Done.
| |
54 } | |
55 | |
56 MockDownloadItemWithValues* download_item() { return &download_item_; } | |
57 | |
58 private: | |
59 MockDownloadItemWithValues download_item_; | |
60 }; | |
61 | |
32 class BackgroundFetchJobControllerTest : public ::testing::Test { | 62 class BackgroundFetchJobControllerTest : public ::testing::Test { |
33 public: | 63 public: |
34 BackgroundFetchJobControllerTest() | 64 BackgroundFetchJobControllerTest() |
35 : download_manager_(new MockDownloadManager()) {} | 65 : download_manager_(new MockDownloadManagerWithCallback()) {} |
36 ~BackgroundFetchJobControllerTest() override = default; | 66 ~BackgroundFetchJobControllerTest() override = default; |
37 | 67 |
38 void SetUp() override { | 68 void SetUp() override { |
39 // The download_manager_ ownership is given to the BrowserContext, and the | 69 // The download_manager_ ownership is given to the BrowserContext, and the |
40 // BrowserContext will take care of deallocating it. | 70 // BrowserContext will take care of deallocating it. |
41 BrowserContext::SetDownloadManagerForTesting(&browser_context_, | 71 BrowserContext::SetDownloadManagerForTesting(&browser_context_, |
42 download_manager_); | 72 download_manager_); |
43 } | 73 } |
44 | 74 |
75 void TearDown() override { job_controller_->Shutdown(); } | |
76 | |
45 void InitializeJobController(BackgroundFetchJobData* job_data) { | 77 void InitializeJobController(BackgroundFetchJobData* job_data) { |
46 job_controller_.reset(new BackgroundFetchJobController( | 78 job_controller_.reset(new BackgroundFetchJobController( |
47 kJobGuid, &browser_context_, | 79 kJobGuid, &browser_context_, |
48 BrowserContext::GetDefaultStoragePartition(&browser_context_), | 80 BrowserContext::GetDefaultStoragePartition(&browser_context_), |
49 job_data)); | 81 job_data)); |
50 } | 82 } |
51 | 83 |
52 void StartProcessing() { | 84 void StartProcessing() { |
53 base::RunLoop run_loop; | 85 base::RunLoop run_loop; |
54 BrowserThread::PostTask( | 86 BrowserThread::PostTask( |
55 BrowserThread::IO, FROM_HERE, | 87 BrowserThread::IO, FROM_HERE, |
56 base::Bind(&BackgroundFetchJobControllerTest::StartProcessingOnIO, | 88 base::Bind(&BackgroundFetchJobControllerTest::StartProcessingOnIO, |
57 base::Unretained(this), run_loop.QuitClosure())); | 89 base::Unretained(this), run_loop.QuitClosure())); |
58 run_loop.Run(); | 90 run_loop.Run(); |
59 } | 91 } |
60 | 92 |
61 void StartProcessingOnIO(const base::Closure& closure) { | 93 void StartProcessingOnIO(const base::Closure& closure) { |
62 job_controller_->StartProcessing(); | 94 job_controller_->StartProcessing(); |
63 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); | 95 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); |
64 } | 96 } |
65 | 97 |
66 BackgroundFetchJobController* job_controller() { | 98 BackgroundFetchJobController* job_controller() { |
67 return job_controller_.get(); | 99 return job_controller_.get(); |
68 } | 100 } |
69 MockDownloadManager* download_manager() { return download_manager_; } | 101 MockDownloadManagerWithCallback* download_manager() { |
102 return download_manager_; | |
103 } | |
104 | |
105 DownloadItem::Observer* ItemObserver() const { return job_controller_.get(); } | |
70 | 106 |
71 private: | 107 private: |
72 TestBrowserThreadBundle thread_bundle_; | 108 TestBrowserThreadBundle thread_bundle_; |
73 TestBrowserContext browser_context_; | 109 TestBrowserContext browser_context_; |
74 std::unique_ptr<BackgroundFetchJobController> job_controller_; | 110 std::unique_ptr<BackgroundFetchJobController> job_controller_; |
75 MockDownloadManager* download_manager_; | 111 MockDownloadManagerWithCallback* download_manager_; |
76 }; | 112 }; |
77 | 113 |
78 TEST_F(BackgroundFetchJobControllerTest, StartDownload) { | 114 TEST_F(BackgroundFetchJobControllerTest, StartDownload) { |
79 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), | 115 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), |
80 kServiceWorkerRegistrationId); | 116 kServiceWorkerRegistrationId); |
81 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid); | 117 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid); |
82 std::vector<BackgroundFetchRequestInfo> request_infos{request_info}; | 118 std::vector<BackgroundFetchRequestInfo> request_infos{request_info}; |
83 | 119 |
120 // Create a MockDownloadItem that the test can manipulate. | |
121 MockDownloadItemWithValues* item = download_manager()->download_item(); | |
122 item->SetGuid("foo"); | |
123 | |
84 // Get a JobData to give to the JobController. The JobController then gets | 124 // Get a JobData to give to the JobController. The JobController then gets |
85 // the BackgroundFetchRequestInfos from the JobData. The JobController will | 125 // the BackgroundFetchRequestInfos from the JobData. The JobController will |
86 // take ownership of the JobData. | 126 // take ownership of the JobData. |
87 BackgroundFetchJobData* job_data = new BackgroundFetchJobData(request_infos); | 127 BackgroundFetchJobData* job_data = new BackgroundFetchJobData(request_infos); |
88 InitializeJobController(job_data); | 128 InitializeJobController(job_data); |
89 | 129 |
90 EXPECT_CALL(*(download_manager()), | 130 EXPECT_CALL(*(download_manager()), |
91 DownloadUrlMock(::testing::Pointee(::testing::Property( | 131 DownloadUrlMock(::testing::Pointee(::testing::Property( |
92 &DownloadUrlParameters::url, GURL(kTestUrl))))) | 132 &DownloadUrlParameters::url, GURL(kTestUrl))))) |
93 .Times(1); | 133 .Times(1); |
94 | 134 |
95 StartProcessing(); | 135 StartProcessing(); |
96 } | 136 } |
97 | 137 |
98 } // namespace content | 138 } // namespace content |
OLD | NEW |