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

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

Issue 2774343002: Hook up BackgroundFetchServiceImpl::Fetch() to start a fetch (Closed)
Patch Set: rebase 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_data_manager.h" 5 #include "content/browser/background_fetch/background_fetch_data_manager.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/callback_helpers.h" 12 #include "base/callback_helpers.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/run_loop.h" 14 #include "base/run_loop.h"
15 #include "content/browser/background_fetch/background_fetch_job_info.h" 15 #include "content/browser/background_fetch/background_fetch_job_info.h"
16 #include "content/browser/background_fetch/background_fetch_job_response_data.h" 16 #include "content/browser/background_fetch/background_fetch_job_response_data.h"
17 #include "content/browser/background_fetch/background_fetch_request_info.h" 17 #include "content/browser/background_fetch/background_fetch_request_info.h"
18 #include "content/browser/background_fetch/background_fetch_test_base.h"
18 #include "content/common/service_worker/service_worker_types.h" 19 #include "content/common/service_worker/service_worker_types.h"
19 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/download_interrupt_reasons.h" 21 #include "content/public/browser/download_interrupt_reasons.h"
21 #include "content/public/browser/download_item.h" 22 #include "content/public/browser/download_item.h"
22 #include "content/public/test/test_browser_context.h" 23 #include "content/public/test/test_browser_context.h"
23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 24
26 namespace content { 25 namespace content {
27 namespace { 26 namespace {
28 27
29 const char kResource[] = "https://example.com/resource.html"; 28 const char kResource[] = "https://example.com/resource.html";
30 const char kTag[] = "TestRequestTag"; 29 const char kTag[] = "TestRequestTag";
31 const char kJobOrigin[] = "https://example.com"; 30 const char kJobOrigin[] = "https://example.com";
32 const int64_t kServiceWorkerRegistrationId = 9001; 31 const int64_t kServiceWorkerRegistrationId = 9001;
33 32
34 } // namespace 33 } // namespace
35 34
36 class BackgroundFetchDataManagerTest : public ::testing::Test { 35 class BackgroundFetchDataManagerTest : public BackgroundFetchTestBase {
37 public: 36 public:
38 BackgroundFetchDataManagerTest() 37 BackgroundFetchDataManagerTest()
39 : background_fetch_data_manager_( 38 : background_fetch_data_manager_(
40 base::MakeUnique<BackgroundFetchDataManager>(&browser_context_)) {} 39 base::MakeUnique<BackgroundFetchDataManager>(&browser_context_)) {}
41 ~BackgroundFetchDataManagerTest() override = default; 40 ~BackgroundFetchDataManagerTest() override = default;
42 41
43 protected: 42 protected:
43 // Synchronous version of BackgroundFetchDataManager::CreateRegistration().
44 void CreateRegistration(
45 const BackgroundFetchRegistrationId& registration_id,
46 const std::vector<ServiceWorkerFetchRequest>& requests,
47 const BackgroundFetchOptions& options,
48 blink::mojom::BackgroundFetchError* out_error) {
49 DCHECK(out_error);
50
51 base::RunLoop run_loop;
52 background_fetch_data_manager_->CreateRegistration(
53 registration_id, requests, options,
54 base::BindOnce(&BackgroundFetchDataManagerTest::DidCreateRegistration,
55 base::Unretained(this), run_loop.QuitClosure(),
56 out_error));
57 run_loop.Run();
58 }
59
60 // Synchronous version of BackgroundFetchDataManager::DeleteRegistration().
61 void DeleteRegistration(const BackgroundFetchRegistrationId& registration_id,
62 blink::mojom::BackgroundFetchError* out_error) {
63 DCHECK(out_error);
64
65 base::RunLoop run_loop;
66 background_fetch_data_manager_->DeleteRegistration(
67 registration_id,
68 base::BindOnce(&BackgroundFetchDataManagerTest::DidDeleteRegistration,
69 base::Unretained(this), run_loop.QuitClosure(),
70 out_error));
71 run_loop.Run();
72 }
73
44 void CreateRequests(int num_requests) { 74 void CreateRequests(int num_requests) {
45 DCHECK(num_requests > 0); 75 DCHECK_GT(num_requests, 0);
46 // Create |num_requests| BackgroundFetchRequestInfo's. 76 // Create |num_requests| BackgroundFetchRequestInfo's.
47 std::vector<std::unique_ptr<BackgroundFetchRequestInfo>> request_infos; 77 std::vector<std::unique_ptr<BackgroundFetchRequestInfo>> request_infos;
48 for (int i = 0; i < num_requests; i++) { 78 for (int i = 0; i < num_requests; i++) {
49 request_infos.push_back( 79 request_infos.push_back(
50 base::MakeUnique<BackgroundFetchRequestInfo>(GURL(kResource), kTag)); 80 base::MakeUnique<BackgroundFetchRequestInfo>(GURL(kResource), kTag));
51 } 81 }
52 std::unique_ptr<BackgroundFetchJobInfo> job_info = 82 std::unique_ptr<BackgroundFetchJobInfo> job_info =
53 base::MakeUnique<BackgroundFetchJobInfo>( 83 base::MakeUnique<BackgroundFetchJobInfo>(
54 "tag", url::Origin(GURL(kJobOrigin)), kServiceWorkerRegistrationId); 84 "tag", url::Origin(GURL(kJobOrigin)), kServiceWorkerRegistrationId);
85 job_info->set_num_requests(num_requests);
86
55 job_guid_ = job_info->guid(); 87 job_guid_ = job_info->guid();
56 background_fetch_data_manager_->CreateRequest(std::move(job_info), 88
57 std::move(request_infos)); 89 background_fetch_data_manager_->WriteJobToStorage(std::move(job_info),
90 std::move(request_infos));
58 } 91 }
59 92
60 void GetResponse() { 93 void GetResponse() {
61 base::RunLoop run_loop; 94 base::RunLoop run_loop;
62 BackgroundFetchResponseCompleteCallback callback = 95 BackgroundFetchResponseCompleteCallback callback =
63 base::Bind(&BackgroundFetchDataManagerTest::DidGetResponse, 96 base::Bind(&BackgroundFetchDataManagerTest::DidGetResponse,
64 base::Unretained(this), run_loop.QuitClosure()); 97 base::Unretained(this), run_loop.QuitClosure());
65 BrowserThread::PostTask( 98 BrowserThread::PostTask(
66 BrowserThread::IO, FROM_HERE, 99 BrowserThread::IO, FROM_HERE,
67 base::Bind(&BackgroundFetchDataManager::GetJobResponse, 100 base::Bind(&BackgroundFetchDataManager::GetJobResponse,
(...skipping 17 matching lines...) Expand all
85 } 118 }
86 119
87 const std::vector<ServiceWorkerResponse>& responses() const { 120 const std::vector<ServiceWorkerResponse>& responses() const {
88 return responses_; 121 return responses_;
89 } 122 }
90 const std::vector<std::unique_ptr<BlobHandle>>& blobs() const { 123 const std::vector<std::unique_ptr<BlobHandle>>& blobs() const {
91 return blobs_; 124 return blobs_;
92 } 125 }
93 126
94 private: 127 private:
128 void DidCreateRegistration(base::Closure quit_closure,
129 blink::mojom::BackgroundFetchError* out_error,
130 blink::mojom::BackgroundFetchError error) {
131 *out_error = error;
132
133 quit_closure.Run();
134 }
135
136 void DidDeleteRegistration(base::Closure quit_closure,
137 blink::mojom::BackgroundFetchError* out_error,
138 blink::mojom::BackgroundFetchError error) {
139 *out_error = error;
140
141 quit_closure.Run();
142 }
143
95 std::string job_guid_; 144 std::string job_guid_;
96 TestBrowserContext browser_context_; 145 TestBrowserContext browser_context_;
97 std::unique_ptr<BackgroundFetchDataManager> background_fetch_data_manager_; 146 std::unique_ptr<BackgroundFetchDataManager> background_fetch_data_manager_;
98 TestBrowserThreadBundle thread_bundle_;
99 std::vector<ServiceWorkerResponse> responses_; 147 std::vector<ServiceWorkerResponse> responses_;
100 std::vector<std::unique_ptr<BlobHandle>> blobs_; 148 std::vector<std::unique_ptr<BlobHandle>> blobs_;
101 }; 149 };
102 150
103 TEST_F(BackgroundFetchDataManagerTest, CompleteJob) { 151 TEST_F(BackgroundFetchDataManagerTest, NoDuplicateRegistrations) {
104 CreateRequests(10); 152 // Tests that the BackgroundFetchDataManager correctly rejects creating a
105 BackgroundFetchDataManager* data_manager = background_fetch_data_manager(); 153 // registration that's already known to the system.
106 std::vector<std::string> request_guids;
107 154
108 // Get all of the fetch requests from the BackgroundFetchDataManager. 155 BackgroundFetchRegistrationId registration_id;
109 for (int i = 0; i < 10; i++) { 156 ASSERT_TRUE(CreateRegistrationId(kTag, &registration_id));
110 EXPECT_FALSE(data_manager->IsComplete(job_guid()));
111 ASSERT_TRUE(data_manager->HasRequestsRemaining(job_guid()));
112 const BackgroundFetchRequestInfo& request_info =
113 data_manager->GetNextBackgroundFetchRequestInfo(job_guid());
114 request_guids.push_back(request_info.guid());
115 EXPECT_EQ(request_info.tag(), kTag);
116 EXPECT_EQ(request_info.state(),
117 DownloadItem::DownloadState::MAX_DOWNLOAD_STATE);
118 EXPECT_EQ(request_info.interrupt_reason(),
119 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE);
120 }
121 157
122 // At this point, all the fetches have been started, but none finished. 158 std::vector<ServiceWorkerFetchRequest> requests;
123 EXPECT_FALSE(data_manager->HasRequestsRemaining(job_guid())); 159 BackgroundFetchOptions options;
124 160
125 // Complete all of the fetch requests. 161 blink::mojom::BackgroundFetchError error;
126 for (int i = 0; i < 10; i++) {
127 EXPECT_FALSE(data_manager->IsComplete(job_guid()));
128 EXPECT_FALSE(data_manager->UpdateRequestState(
129 job_guid(), request_guids[i], DownloadItem::DownloadState::COMPLETE,
130 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE));
131 }
132 162
133 // All requests are complete now. 163 // Deleting the not-yet-created registration should fail.
134 EXPECT_TRUE(data_manager->IsComplete(job_guid())); 164 ASSERT_NO_FATAL_FAILURE(DeleteRegistration(registration_id, &error));
135 GetResponse(); 165 EXPECT_EQ(error, blink::mojom::BackgroundFetchError::INVALID_TAG);
136 166
137 EXPECT_EQ(10U, responses().size()); 167 // Creating the initial registration should succeed.
138 EXPECT_EQ(10U, blobs().size()); 168 ASSERT_NO_FATAL_FAILURE(
169 CreateRegistration(registration_id, requests, options, &error));
170 EXPECT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
171
172 // Attempting to create it again should yield an error.
173 ASSERT_NO_FATAL_FAILURE(
174 CreateRegistration(registration_id, requests, options, &error));
175 EXPECT_EQ(error, blink::mojom::BackgroundFetchError::DUPLICATED_TAG);
176
177 // Deleting the registration should succeed.
178 ASSERT_NO_FATAL_FAILURE(DeleteRegistration(registration_id, &error));
179 EXPECT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
180
181 // And then recreating the registration again should work fine.
182 ASSERT_NO_FATAL_FAILURE(
183 CreateRegistration(registration_id, requests, options, &error));
184 EXPECT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
139 } 185 }
140 186
141 TEST_F(BackgroundFetchDataManagerTest, OutOfOrderCompletion) { 187 TEST_F(BackgroundFetchDataManagerTest, OutOfOrderCompletion) {
142 CreateRequests(10); 188 CreateRequests(10);
143 BackgroundFetchDataManager* data_manager = background_fetch_data_manager(); 189 BackgroundFetchDataManager* data_manager = background_fetch_data_manager();
144 const std::string& job_guid = BackgroundFetchDataManagerTest::job_guid(); 190 const std::string& job_guid = BackgroundFetchDataManagerTest::job_guid();
145 std::vector<std::string> request_guids; 191 std::vector<std::string> request_guids;
146 192
147 // Start half of the fetch requests. 193 // Start half of the fetch requests.
148 for (int i = 0; i < 5; i++) { 194 for (int i = 0; i < 5; i++) {
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 291
246 const BackgroundFetchRequestInfo& request_info = 292 const BackgroundFetchRequestInfo& request_info =
247 data_manager->GetNextBackgroundFetchRequestInfo(job_guid); 293 data_manager->GetNextBackgroundFetchRequestInfo(job_guid);
248 EXPECT_FALSE(data_manager->UpdateRequestState( 294 EXPECT_FALSE(data_manager->UpdateRequestState(
249 job_guid, request_info.guid(), DownloadItem::DownloadState::COMPLETE, 295 job_guid, request_info.guid(), DownloadItem::DownloadState::COMPLETE,
250 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE)); 296 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE));
251 EXPECT_TRUE(data_manager->IsComplete(job_guid)); 297 EXPECT_TRUE(data_manager->IsComplete(job_guid));
252 } 298 }
253 299
254 } // namespace content 300 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698