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

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

Issue 2774263003: Added ServiceWorkerResponses to GetJobResponse callback (Closed)
Patch Set: Move PostTask to correct thread 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_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/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
13 #include "base/run_loop.h" 14 #include "base/run_loop.h"
14 #include "content/browser/background_fetch/background_fetch_job_info.h" 15 #include "content/browser/background_fetch/background_fetch_job_info.h"
15 #include "content/browser/background_fetch/background_fetch_job_response_data.h" 16 #include "content/browser/background_fetch/background_fetch_job_response_data.h"
16 #include "content/browser/background_fetch/background_fetch_request_info.h" 17 #include "content/browser/background_fetch/background_fetch_request_info.h"
18 #include "content/common/service_worker/service_worker_types.h"
17 #include "content/public/browser/browser_thread.h" 19 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/download_interrupt_reasons.h" 20 #include "content/public/browser/download_interrupt_reasons.h"
19 #include "content/public/browser/download_item.h" 21 #include "content/public/browser/download_item.h"
20 #include "content/public/test/test_browser_context.h" 22 #include "content/public/test/test_browser_context.h"
21 #include "content/public/test/test_browser_thread_bundle.h" 23 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
23 25
24 namespace content { 26 namespace content {
25 namespace { 27 namespace {
26 28
(...skipping 21 matching lines...) Expand all
48 base::MakeUnique<BackgroundFetchRequestInfo>(GURL(kResource), kTag)); 50 base::MakeUnique<BackgroundFetchRequestInfo>(GURL(kResource), kTag));
49 } 51 }
50 std::unique_ptr<BackgroundFetchJobInfo> job_info = 52 std::unique_ptr<BackgroundFetchJobInfo> job_info =
51 base::MakeUnique<BackgroundFetchJobInfo>( 53 base::MakeUnique<BackgroundFetchJobInfo>(
52 "tag", url::Origin(GURL(kJobOrigin)), kServiceWorkerRegistrationId); 54 "tag", url::Origin(GURL(kJobOrigin)), kServiceWorkerRegistrationId);
53 job_guid_ = job_info->guid(); 55 job_guid_ = job_info->guid();
54 background_fetch_data_manager_->CreateRequest(std::move(job_info), 56 background_fetch_data_manager_->CreateRequest(std::move(job_info),
55 std::move(request_infos)); 57 std::move(request_infos));
56 } 58 }
57 59
60 void GetResponse() {
61 base::RunLoop run_loop;
62 BackgroundFetchResponseCompleteCallback callback =
63 base::Bind(&BackgroundFetchDataManagerTest::DidGetResponse,
64 base::Unretained(this), run_loop.QuitClosure());
65 BrowserThread::PostTask(
66 BrowserThread::IO, FROM_HERE,
67 base::Bind(&BackgroundFetchDataManager::GetJobResponse,
68 base::Unretained(background_fetch_data_manager()), job_guid_,
69 base::Bind(&BackgroundFetchDataManagerTest::DidGetResponse,
70 base::Unretained(this), run_loop.QuitClosure())));
71 run_loop.Run();
72 }
73
74 void DidGetResponse(base::Closure closure,
75 std::vector<ServiceWorkerResponse> responses,
76 std::vector<std::unique_ptr<BlobHandle>> blobs) {
77 responses_ = std::move(responses);
78 blobs_ = std::move(blobs);
79 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure);
80 }
81
58 const std::string& job_guid() const { return job_guid_; } 82 const std::string& job_guid() const { return job_guid_; }
59 BackgroundFetchDataManager* background_fetch_data_manager() { 83 BackgroundFetchDataManager* background_fetch_data_manager() {
60 return background_fetch_data_manager_.get(); 84 return background_fetch_data_manager_.get();
61 } 85 }
62 86
87 const std::vector<ServiceWorkerResponse>& responses() const {
88 return responses_;
89 }
90 const std::vector<std::unique_ptr<BlobHandle>>& blobs() const {
91 return blobs_;
92 }
93
63 private: 94 private:
64 std::string job_guid_; 95 std::string job_guid_;
65 TestBrowserContext browser_context_; 96 TestBrowserContext browser_context_;
66 std::unique_ptr<BackgroundFetchDataManager> background_fetch_data_manager_; 97 std::unique_ptr<BackgroundFetchDataManager> background_fetch_data_manager_;
67 TestBrowserThreadBundle thread_bundle_; 98 TestBrowserThreadBundle thread_bundle_;
99 std::vector<ServiceWorkerResponse> responses_;
100 std::vector<std::unique_ptr<BlobHandle>> blobs_;
68 }; 101 };
69 102
103 TEST_F(BackgroundFetchDataManagerTest, CompleteJob) {
104 CreateRequests(10);
105 BackgroundFetchDataManager* data_manager = background_fetch_data_manager();
106 std::vector<std::string> request_guids;
107
108 // Get all of the fetch requests from the BackgroundFetchDataManager.
109 for (int i = 0; i < 10; i++) {
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
122 // At this point, all the fetches have been started, but none finished.
123 EXPECT_FALSE(data_manager->HasRequestsRemaining(job_guid()));
124
125 // Complete all of the fetch requests.
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
133 // All requests are complete now.
134 EXPECT_TRUE(data_manager->IsComplete(job_guid()));
135 GetResponse();
136
137 EXPECT_EQ(10U, responses().size());
138 EXPECT_EQ(10U, blobs().size());
139 }
140
70 TEST_F(BackgroundFetchDataManagerTest, OutOfOrderCompletion) { 141 TEST_F(BackgroundFetchDataManagerTest, OutOfOrderCompletion) {
71 CreateRequests(10); 142 CreateRequests(10);
72 BackgroundFetchDataManager* data_manager = background_fetch_data_manager(); 143 BackgroundFetchDataManager* data_manager = background_fetch_data_manager();
73 const std::string& job_guid = BackgroundFetchDataManagerTest::job_guid(); 144 const std::string& job_guid = BackgroundFetchDataManagerTest::job_guid();
74 std::vector<std::string> request_guids; 145 std::vector<std::string> request_guids;
75 146
76 // Start half of the fetch requests. 147 // Start half of the fetch requests.
77 for (int i = 0; i < 5; i++) { 148 for (int i = 0; i < 5; i++) {
78 ASSERT_TRUE(data_manager->HasRequestsRemaining(job_guid)); 149 ASSERT_TRUE(data_manager->HasRequestsRemaining(job_guid));
79 const BackgroundFetchRequestInfo& request_info = 150 const BackgroundFetchRequestInfo& request_info =
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 245
175 const BackgroundFetchRequestInfo& request_info = 246 const BackgroundFetchRequestInfo& request_info =
176 data_manager->GetNextBackgroundFetchRequestInfo(job_guid); 247 data_manager->GetNextBackgroundFetchRequestInfo(job_guid);
177 EXPECT_FALSE(data_manager->UpdateRequestState( 248 EXPECT_FALSE(data_manager->UpdateRequestState(
178 job_guid, request_info.guid(), DownloadItem::DownloadState::COMPLETE, 249 job_guid, request_info.guid(), DownloadItem::DownloadState::COMPLETE,
179 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE)); 250 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE));
180 EXPECT_TRUE(data_manager->IsComplete(job_guid)); 251 EXPECT_TRUE(data_manager->IsComplete(job_guid));
181 } 252 }
182 253
183 } // namespace content 254 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698