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

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

Issue 2727253002: Added DownloadItem::Observer to JobController. (Closed)
Patch Set: Added TODO Created 3 years, 9 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 "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 {
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 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
50 base::Bind(params->callback(), &download_item_,
51 DOWNLOAD_INTERRUPT_REASON_NONE));
52 }
53
54 DownloadItem* GetDownloadByGuid(const std::string& guid) override {
55 DCHECK_EQ(download_item_.GetGuid(), guid);
56 return &download_item_;
57 }
58
59 MockDownloadItemWithValues* download_item() { return &download_item_; }
60
61 private:
62 MockDownloadItemWithValues download_item_;
63 };
64
32 class BackgroundFetchJobControllerTest : public ::testing::Test { 65 class BackgroundFetchJobControllerTest : public ::testing::Test {
33 public: 66 public:
34 BackgroundFetchJobControllerTest() 67 BackgroundFetchJobControllerTest()
35 : download_manager_(new MockDownloadManager()) {} 68 : download_manager_(new MockDownloadManagerWithCallback()) {}
36 ~BackgroundFetchJobControllerTest() override = default; 69 ~BackgroundFetchJobControllerTest() override = default;
37 70
38 void SetUp() override { 71 void SetUp() override {
39 // The download_manager_ ownership is given to the BrowserContext, and the 72 // The download_manager_ ownership is given to the BrowserContext, and the
40 // BrowserContext will take care of deallocating it. 73 // BrowserContext will take care of deallocating it.
41 BrowserContext::SetDownloadManagerForTesting(&browser_context_, 74 BrowserContext::SetDownloadManagerForTesting(&browser_context_,
42 download_manager_); 75 download_manager_);
43 } 76 }
44 77
78 void TearDown() override { job_controller_->Shutdown(); }
79
45 void InitializeJobController( 80 void InitializeJobController(
46 std::unique_ptr<BackgroundFetchJobData> job_data) { 81 std::unique_ptr<BackgroundFetchJobData> job_data) {
47 job_controller_ = base::MakeUnique<BackgroundFetchJobController>( 82 job_controller_ = base::MakeUnique<BackgroundFetchJobController>(
48 kJobGuid, &browser_context_, 83 kJobGuid, &browser_context_,
49 BrowserContext::GetDefaultStoragePartition(&browser_context_), 84 BrowserContext::GetDefaultStoragePartition(&browser_context_),
50 std::move(job_data)); 85 std::move(job_data));
51 } 86 }
52 87
53 void StartProcessing() { 88 void StartProcessing() {
54 base::RunLoop run_loop; 89 base::RunLoop run_loop;
55 BrowserThread::PostTask( 90 BrowserThread::PostTask(
56 BrowserThread::IO, FROM_HERE, 91 BrowserThread::IO, FROM_HERE,
57 base::Bind(&BackgroundFetchJobControllerTest::StartProcessingOnIO, 92 base::Bind(&BackgroundFetchJobControllerTest::StartProcessingOnIO,
58 base::Unretained(this), run_loop.QuitClosure())); 93 base::Unretained(this), run_loop.QuitClosure()));
59 run_loop.Run(); 94 run_loop.Run();
60 } 95 }
61 96
62 void StartProcessingOnIO(const base::Closure& closure) { 97 void StartProcessingOnIO(const base::Closure& closure) {
63 job_controller_->StartProcessing(); 98 job_controller_->StartProcessing();
64 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); 99 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure);
65 } 100 }
66 101
67 BackgroundFetchJobController* job_controller() { 102 BackgroundFetchJobController* job_controller() {
68 return job_controller_.get(); 103 return job_controller_.get();
69 } 104 }
70 MockDownloadManager* download_manager() { return download_manager_; } 105 MockDownloadManagerWithCallback* download_manager() {
106 return download_manager_;
107 }
108
109 DownloadItem::Observer* ItemObserver() const { return job_controller_.get(); }
71 110
72 private: 111 private:
73 TestBrowserThreadBundle thread_bundle_; 112 TestBrowserThreadBundle thread_bundle_;
74 TestBrowserContext browser_context_; 113 TestBrowserContext browser_context_;
75 std::unique_ptr<BackgroundFetchJobController> job_controller_; 114 std::unique_ptr<BackgroundFetchJobController> job_controller_;
76 MockDownloadManager* download_manager_; 115 MockDownloadManagerWithCallback* download_manager_;
77 }; 116 };
78 117
79 TEST_F(BackgroundFetchJobControllerTest, StartDownload) { 118 TEST_F(BackgroundFetchJobControllerTest, StartDownload) {
80 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), 119 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)),
81 kServiceWorkerRegistrationId); 120 kServiceWorkerRegistrationId);
82 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid); 121 BackgroundFetchRequestInfo request_info(GURL(kTestUrl), kJobGuid);
83 std::vector<BackgroundFetchRequestInfo> request_infos{request_info}; 122 std::vector<BackgroundFetchRequestInfo> request_infos{request_info};
84 123
124 // Create a MockDownloadItem that the test can manipulate.
125 MockDownloadItemWithValues* item = download_manager()->download_item();
126 item->SetGuid("foo");
127
85 // Get a JobData to give to the JobController. The JobController then gets 128 // Get a JobData to give to the JobController. The JobController then gets
86 // the BackgroundFetchRequestInfos from the JobData. 129 // the BackgroundFetchRequestInfos from the JobData.
87 std::unique_ptr<BackgroundFetchJobData> job_data = 130 std::unique_ptr<BackgroundFetchJobData> job_data =
88 base::MakeUnique<BackgroundFetchJobData>(request_infos); 131 base::MakeUnique<BackgroundFetchJobData>(request_infos);
89 InitializeJobController(std::move(job_data)); 132 InitializeJobController(std::move(job_data));
90 133
91 EXPECT_CALL(*(download_manager()), 134 EXPECT_CALL(*(download_manager()),
92 DownloadUrlMock(::testing::Pointee(::testing::Property( 135 DownloadUrlMock(::testing::Pointee(::testing::Property(
93 &DownloadUrlParameters::url, GURL(kTestUrl))))) 136 &DownloadUrlParameters::url, GURL(kTestUrl)))))
94 .Times(1); 137 .Times(1);
95 138
96 StartProcessing(); 139 StartProcessing();
97 } 140 }
98 141
99 } // namespace content 142 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698