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

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: 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/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "content/browser/background_fetch/background_fetch_job_info.h" 14 #include "content/browser/background_fetch/background_fetch_job_info.h"
15 #include "content/browser/background_fetch/background_fetch_job_response_data.h" 15 #include "content/browser/background_fetch/background_fetch_job_response_data.h"
16 #include "content/browser/background_fetch/background_fetch_request_info.h" 16 #include "content/browser/background_fetch/background_fetch_request_info.h"
17 #include "content/browser/background_fetch/background_fetch_test_base.h"
17 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/download_interrupt_reasons.h" 19 #include "content/public/browser/download_interrupt_reasons.h"
19 #include "content/public/browser/download_item.h" 20 #include "content/public/browser/download_item.h"
20 #include "content/public/test/test_browser_context.h" 21 #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 22
24 namespace content { 23 namespace content {
25 namespace { 24 namespace {
26 25
27 const char kResource[] = "https://example.com/resource.html"; 26 const char kResource[] = "https://example.com/resource.html";
28 const char kTag[] = "TestRequestTag"; 27 const char kTag[] = "TestRequestTag";
29 const char kJobOrigin[] = "https://example.com"; 28 const char kJobOrigin[] = "https://example.com";
30 const int64_t kServiceWorkerRegistrationId = 9001; 29 const int64_t kServiceWorkerRegistrationId = 9001;
31 30
32 } // namespace 31 } // namespace
33 32
34 class BackgroundFetchDataManagerTest : public ::testing::Test { 33 class BackgroundFetchDataManagerTest : public BackgroundFetchTestBase {
35 public: 34 public:
36 BackgroundFetchDataManagerTest() 35 BackgroundFetchDataManagerTest()
37 : background_fetch_data_manager_( 36 : background_fetch_data_manager_(
38 base::MakeUnique<BackgroundFetchDataManager>(&browser_context_)) {} 37 base::MakeUnique<BackgroundFetchDataManager>(&browser_context_)) {}
39 ~BackgroundFetchDataManagerTest() override = default; 38 ~BackgroundFetchDataManagerTest() override = default;
40 39
41 protected: 40 protected:
41 // Synchronous version of BackgroundFetchDataManager::CreateRegistration().
42 void CreateRegistration(
43 const BackgroundFetchRegistrationId& registration_id,
44 const std::vector<ServiceWorkerFetchRequest>& requests,
45 const BackgroundFetchOptions& options,
46 blink::mojom::BackgroundFetchError* out_error) {
47 DCHECK(out_error);
48
49 base::RunLoop run_loop;
50 background_fetch_data_manager_->CreateRegistration(
51 registration_id, requests, options,
52 base::BindOnce(&BackgroundFetchDataManagerTest::DidCreateRegistration,
53 base::Unretained(this), run_loop.QuitClosure(),
54 out_error));
55 run_loop.Run();
56 }
57
58 // Synchronous version of BackgroundFetchDataManager::DeleteRegistration().
59 void DeleteRegistration(const BackgroundFetchRegistrationId& registration_id,
60 blink::mojom::BackgroundFetchError* out_error) {
61 DCHECK(out_error);
62
63 base::RunLoop run_loop;
64 background_fetch_data_manager_->DeleteRegistration(
65 registration_id,
66 base::BindOnce(&BackgroundFetchDataManagerTest::DidDeleteRegistration,
67 base::Unretained(this), run_loop.QuitClosure(),
68 out_error));
69 run_loop.Run();
70 }
71
42 void CreateRequests(int num_requests) { 72 void CreateRequests(int num_requests) {
43 DCHECK(num_requests > 0); 73 DCHECK_GT(num_requests, 0);
44 // Create |num_requests| BackgroundFetchRequestInfo's. 74 // Create |num_requests| BackgroundFetchRequestInfo's.
45 BackgroundFetchRequestInfos request_infos; 75 BackgroundFetchRequestInfos request_infos;
46 for (int i = 0; i < num_requests; i++) { 76 for (int i = 0; i < num_requests; i++) {
47 request_infos.push_back( 77 request_infos.push_back(
48 base::MakeUnique<BackgroundFetchRequestInfo>(GURL(kResource), kTag)); 78 base::MakeUnique<BackgroundFetchRequestInfo>(GURL(kResource), kTag));
49 } 79 }
50 std::unique_ptr<BackgroundFetchJobInfo> job_info = 80 std::unique_ptr<BackgroundFetchJobInfo> job_info =
51 base::MakeUnique<BackgroundFetchJobInfo>( 81 base::MakeUnique<BackgroundFetchJobInfo>(
52 "tag", url::Origin(GURL(kJobOrigin)), kServiceWorkerRegistrationId); 82 "tag", url::Origin(GURL(kJobOrigin)), kServiceWorkerRegistrationId);
83 job_info->set_num_requests(num_requests);
84
53 job_guid_ = job_info->guid(); 85 job_guid_ = job_info->guid();
54 background_fetch_data_manager_->CreateRequest(std::move(job_info), 86
55 std::move(request_infos)); 87 background_fetch_data_manager_->WriteJobToStorage(std::move(job_info),
88 std::move(request_infos));
56 } 89 }
57 90
58 const std::string& job_guid() const { return job_guid_; } 91 const std::string& job_guid() const { return job_guid_; }
59 BackgroundFetchDataManager* background_fetch_data_manager() { 92 BackgroundFetchDataManager* background_fetch_data_manager() {
60 return background_fetch_data_manager_.get(); 93 return background_fetch_data_manager_.get();
61 } 94 }
62 95
63 private: 96 private:
97 void DidCreateRegistration(base::Closure quit_closure,
98 blink::mojom::BackgroundFetchError* out_error,
99 blink::mojom::BackgroundFetchError error) {
100 *out_error = error;
101
102 quit_closure.Run();
103 }
104
105 void DidDeleteRegistration(base::Closure quit_closure,
106 blink::mojom::BackgroundFetchError* out_error,
107 blink::mojom::BackgroundFetchError error) {
108 *out_error = error;
109
110 quit_closure.Run();
111 }
112
64 std::string job_guid_; 113 std::string job_guid_;
65 TestBrowserContext browser_context_; 114 TestBrowserContext browser_context_;
66 std::unique_ptr<BackgroundFetchDataManager> background_fetch_data_manager_; 115 std::unique_ptr<BackgroundFetchDataManager> background_fetch_data_manager_;
67 TestBrowserThreadBundle thread_bundle_;
68 }; 116 };
69 117
118 TEST_F(BackgroundFetchDataManagerTest, NoDuplicateRegistrations) {
119 // Tests that the BackgroundFetchDataManager correctly rejects creating a
120 // registration that's already known to the system.
121
122 BackgroundFetchRegistrationId registration_id;
123 ASSERT_TRUE(CreateRegistrationId(kTag, &registration_id));
124
125 std::vector<ServiceWorkerFetchRequest> requests;
126 BackgroundFetchOptions options;
127
128 blink::mojom::BackgroundFetchError error;
129
130 // Deleting the not-yet-created registration should fail.
131 ASSERT_NO_FATAL_FAILURE(DeleteRegistration(registration_id, &error));
132 EXPECT_EQ(error, blink::mojom::BackgroundFetchError::INVALID_TAG);
133
134 // Creating the initial registration should succeed.
135 ASSERT_NO_FATAL_FAILURE(
136 CreateRegistration(registration_id, requests, options, &error));
137 EXPECT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
138
139 // Attempting to create it again should yield an error.
140 ASSERT_NO_FATAL_FAILURE(
141 CreateRegistration(registration_id, requests, options, &error));
142 EXPECT_EQ(error, blink::mojom::BackgroundFetchError::DUPLICATED_TAG);
143
144 // Deleting the registration should succeed.
145 ASSERT_NO_FATAL_FAILURE(DeleteRegistration(registration_id, &error));
146 EXPECT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
147
148 // And then recreating the registration again should work fine.
149 ASSERT_NO_FATAL_FAILURE(
150 CreateRegistration(registration_id, requests, options, &error));
151 EXPECT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
152 }
153
70 TEST_F(BackgroundFetchDataManagerTest, OutOfOrderCompletion) { 154 TEST_F(BackgroundFetchDataManagerTest, OutOfOrderCompletion) {
71 CreateRequests(10); 155 CreateRequests(10);
72 BackgroundFetchDataManager* data_manager = background_fetch_data_manager(); 156 BackgroundFetchDataManager* data_manager = background_fetch_data_manager();
73 const std::string& job_guid = BackgroundFetchDataManagerTest::job_guid(); 157 const std::string& job_guid = BackgroundFetchDataManagerTest::job_guid();
74 std::vector<std::string> request_guids; 158 std::vector<std::string> request_guids;
75 159
76 // Start half of the fetch requests. 160 // Start half of the fetch requests.
77 for (int i = 0; i < 5; i++) { 161 for (int i = 0; i < 5; i++) {
78 ASSERT_TRUE(data_manager->HasRequestsRemaining(job_guid)); 162 ASSERT_TRUE(data_manager->HasRequestsRemaining(job_guid));
79 const BackgroundFetchRequestInfo& request_info = 163 const BackgroundFetchRequestInfo& request_info =
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 258
175 const BackgroundFetchRequestInfo& request_info = 259 const BackgroundFetchRequestInfo& request_info =
176 data_manager->GetNextBackgroundFetchRequestInfo(job_guid); 260 data_manager->GetNextBackgroundFetchRequestInfo(job_guid);
177 EXPECT_FALSE(data_manager->UpdateRequestState( 261 EXPECT_FALSE(data_manager->UpdateRequestState(
178 job_guid, request_info.guid(), DownloadItem::DownloadState::COMPLETE, 262 job_guid, request_info.guid(), DownloadItem::DownloadState::COMPLETE,
179 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE)); 263 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE));
180 EXPECT_TRUE(data_manager->IsComplete(job_guid)); 264 EXPECT_TRUE(data_manager->IsComplete(job_guid));
181 } 265 }
182 266
183 } // namespace content 267 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698