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

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

Issue 2767373002: Implement GetJobResponse and merge JobData into DataManager. (Closed)
Patch Set: Removed typedef and added DISALLOW_COPY_AND_ASSIGN 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/browser/background_fetch/background_fetch_data_manager.h"
6
7 #include <memory>
8 #include <string>
9 #include <vector>
10
11 #include "base/bind_helpers.h"
12 #include "base/memory/ptr_util.h"
13 #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_response_data.h"
16 #include "content/browser/background_fetch/background_fetch_request_info.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/download_interrupt_reasons.h"
19 #include "content/public/browser/download_item.h"
20 #include "content/public/test/test_browser_context.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 namespace content {
25 namespace {
26
27 const char kResource[] = "https://example.com/resource.html";
28 const char kTag[] = "TestRequestTag";
29 const char kJobOrigin[] = "https://example.com";
30 const int64_t kServiceWorkerRegistrationId = 9001;
31
32 } // namespace
33
34 class BackgroundFetchDataManagerTest : public ::testing::Test {
35 public:
36 BackgroundFetchDataManagerTest()
37 : background_fetch_data_manager_(
38 base::MakeUnique<BackgroundFetchDataManager>(&browser_context_)) {}
39 ~BackgroundFetchDataManagerTest() override = default;
40
41 protected:
42 void CreateRequests(int num_requests) {
43 DCHECK(num_requests > 0);
44 // Create |num_requests| BackgroundFetchRequestInfo's.
45 std::vector<std::unique_ptr<BackgroundFetchRequestInfo>> request_infos;
46 for (int i = 0; i < num_requests; i++) {
47 request_infos.push_back(
48 base::MakeUnique<BackgroundFetchRequestInfo>(GURL(kResource), kTag));
49 }
50 std::unique_ptr<BackgroundFetchJobInfo> job_info =
51 base::MakeUnique<BackgroundFetchJobInfo>(
52 "tag", url::Origin(GURL(kJobOrigin)), kServiceWorkerRegistrationId);
53 job_guid_ = job_info->guid();
54 background_fetch_data_manager_->CreateRequest(std::move(job_info),
55 std::move(request_infos));
56 }
57
58 const std::string& job_guid() const { return job_guid_; }
59 BackgroundFetchDataManager* background_fetch_data_manager() {
60 return background_fetch_data_manager_.get();
61 }
62
63 private:
64 std::string job_guid_;
65 TestBrowserContext browser_context_;
66 std::unique_ptr<BackgroundFetchDataManager> background_fetch_data_manager_;
67 TestBrowserThreadBundle thread_bundle_;
68 };
69
70 TEST_F(BackgroundFetchDataManagerTest, OutOfOrderCompletion) {
71 CreateRequests(10);
72 BackgroundFetchDataManager* data_manager = background_fetch_data_manager();
73 const std::string& job_guid = BackgroundFetchDataManagerTest::job_guid();
74 std::vector<std::string> request_guids;
75
76 // Start half of the fetch requests.
77 for (int i = 0; i < 5; i++) {
78 ASSERT_TRUE(data_manager->HasRequestsRemaining(job_guid));
79 const BackgroundFetchRequestInfo& request_info =
80 data_manager->GetNextBackgroundFetchRequestInfo(job_guid);
81 request_guids.push_back(request_info.guid());
82 }
83
84 // Complete all of the fetches out of order except for #1.
85 DownloadItem::DownloadState complete = DownloadItem::DownloadState::COMPLETE;
86 DownloadInterruptReason no_interrupt =
87 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE;
88 EXPECT_TRUE(data_manager->UpdateRequestState(job_guid, request_guids[3],
89 complete, no_interrupt));
90 EXPECT_TRUE(data_manager->UpdateRequestState(job_guid, request_guids[2],
91 complete, no_interrupt));
92 EXPECT_TRUE(data_manager->UpdateRequestState(job_guid, request_guids[4],
93 complete, no_interrupt));
94 EXPECT_TRUE(data_manager->UpdateRequestState(job_guid, request_guids[0],
95 complete, no_interrupt));
96
97 EXPECT_TRUE(data_manager->HasRequestsRemaining(job_guid));
98 EXPECT_FALSE(data_manager->IsComplete(job_guid));
99
100 // Start and complete the remaining requests.
101 for (int i = 5; i < 10; i++) {
102 const BackgroundFetchRequestInfo& request_info =
103 data_manager->GetNextBackgroundFetchRequestInfo(job_guid);
104 request_guids.push_back(request_info.guid());
105 data_manager->UpdateRequestState(job_guid, request_guids[i], complete,
106 no_interrupt);
107 }
108
109 EXPECT_FALSE(data_manager->IsComplete(job_guid));
110 EXPECT_FALSE(data_manager->HasRequestsRemaining(job_guid));
111
112 // Complete the final request.
113 EXPECT_FALSE(data_manager->UpdateRequestState(job_guid, request_guids[1],
114 complete, no_interrupt));
115 EXPECT_TRUE(data_manager->IsComplete(job_guid));
116 }
117
118 TEST_F(BackgroundFetchDataManagerTest, PauseAndResume) {
119 CreateRequests(1);
120 BackgroundFetchDataManager* data_manager = background_fetch_data_manager();
121 const std::string& job_guid = BackgroundFetchDataManagerTest::job_guid();
122
123 // Start the request.
124 ASSERT_TRUE(data_manager->HasRequestsRemaining(job_guid));
125 const BackgroundFetchRequestInfo& request_info =
126 data_manager->GetNextBackgroundFetchRequestInfo(job_guid);
127
128 EXPECT_FALSE(data_manager->HasRequestsRemaining(job_guid));
129 EXPECT_FALSE(data_manager->IsComplete(job_guid));
130
131 // Set the request state to be paused. This should not complete the job.
132 EXPECT_FALSE(data_manager->UpdateRequestState(
133 job_guid, request_info.guid(), DownloadItem::DownloadState::INTERRUPTED,
134 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN));
135 EXPECT_FALSE(data_manager->IsComplete(job_guid));
136
137 // Unpause the request.
138 EXPECT_FALSE(data_manager->UpdateRequestState(
139 job_guid, request_info.guid(), DownloadItem::DownloadState::IN_PROGRESS,
140 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE));
141 EXPECT_FALSE(data_manager->IsComplete(job_guid));
142
143 // Complete the request.
144 EXPECT_FALSE(data_manager->UpdateRequestState(
145 job_guid, request_info.guid(), DownloadItem::DownloadState::COMPLETE,
146 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE));
147 EXPECT_TRUE(data_manager->IsComplete(job_guid));
148 }
149
150 TEST_F(BackgroundFetchDataManagerTest, CancelledRequest) {
151 CreateRequests(1);
152 BackgroundFetchDataManager* data_manager = background_fetch_data_manager();
153 const std::string& job_guid = BackgroundFetchDataManagerTest::job_guid();
154
155 // Start the request.
156 ASSERT_TRUE(data_manager->HasRequestsRemaining(job_guid));
157 const BackgroundFetchRequestInfo& request_info =
158 data_manager->GetNextBackgroundFetchRequestInfo(job_guid);
159
160 EXPECT_FALSE(data_manager->HasRequestsRemaining(job_guid));
161 EXPECT_FALSE(data_manager->IsComplete(job_guid));
162
163 // Cancel the request.
164 EXPECT_FALSE(data_manager->UpdateRequestState(
165 job_guid, request_info.guid(), DownloadItem::DownloadState::CANCELLED,
166 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE));
167 EXPECT_TRUE(data_manager->IsComplete(job_guid));
168 }
169
170 TEST_F(BackgroundFetchDataManagerTest, PrepareResponse) {
171 CreateRequests(1);
172 BackgroundFetchDataManager* data_manager = background_fetch_data_manager();
173 const std::string& job_guid = BackgroundFetchDataManagerTest::job_guid();
174
175 const BackgroundFetchRequestInfo& request_info =
176 data_manager->GetNextBackgroundFetchRequestInfo(job_guid);
177 EXPECT_FALSE(data_manager->UpdateRequestState(
178 job_guid, request_info.guid(), DownloadItem::DownloadState::COMPLETE,
179 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE));
180 EXPECT_TRUE(data_manager->IsComplete(job_guid));
181 }
182
183 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698