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

Side by Side Diff: chrome/browser/android/offline_pages/background_loader_offliner_unittest.cc

Issue 2744553005: [Offline Pages] Turn Offliner::Cancel into an async operation to resolve conflicting assumptions be… (Closed)
Patch Set: 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "chrome/browser/android/offline_pages/background_loader_offliner.h" 5 #include "chrome/browser/android/offline_pages/background_loader_offliner.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/test/histogram_tester.h" 9 #include "base/test/histogram_tester.h"
10 #include "base/test/scoped_mock_time_message_loop_task_runner.h" 10 #include "base/test/scoped_mock_time_message_loop_task_runner.h"
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 127
128 TestBackgroundLoaderOffliner* offliner() const { return offliner_.get(); } 128 TestBackgroundLoaderOffliner* offliner() const { return offliner_.get(); }
129 Offliner::CompletionCallback const completion_callback() { 129 Offliner::CompletionCallback const completion_callback() {
130 return base::Bind(&BackgroundLoaderOfflinerTest::OnCompletion, 130 return base::Bind(&BackgroundLoaderOfflinerTest::OnCompletion,
131 base::Unretained(this)); 131 base::Unretained(this));
132 } 132 }
133 Offliner::ProgressCallback const progress_callback() { 133 Offliner::ProgressCallback const progress_callback() {
134 return base::Bind(&BackgroundLoaderOfflinerTest::OnProgress, 134 return base::Bind(&BackgroundLoaderOfflinerTest::OnProgress,
135 base::Unretained(this)); 135 base::Unretained(this));
136 } 136 }
137 Offliner::CancelCallback const cancel_callback() {
138 return base::Bind(&BackgroundLoaderOfflinerTest::OnCancel,
139 base::Unretained(this));
140 }
137 Profile* profile() { return &profile_; } 141 Profile* profile() { return &profile_; }
138 bool completion_callback_called() { return completion_callback_called_; } 142 bool completion_callback_called() { return completion_callback_called_; }
139 Offliner::RequestStatus request_status() { return request_status_; } 143 Offliner::RequestStatus request_status() { return request_status_; }
144 bool cancel_callback_called() { return cancel_callback_called_; }
140 bool SaveInProgress() const { return model_->mock_saving(); } 145 bool SaveInProgress() const { return model_->mock_saving(); }
141 MockOfflinePageModel* model() const { return model_; } 146 MockOfflinePageModel* model() const { return model_; }
142 const base::HistogramTester& histograms() const { return histogram_tester_; } 147 const base::HistogramTester& histograms() const { return histogram_tester_; }
143 148
144 void CompleteLoading() { 149 void CompleteLoading() {
145 // For some reason, setting loading to True will call DidStopLoading 150 // For some reason, setting loading to True will call DidStopLoading
146 // on the observers. 151 // on the observers.
147 offliner()->web_contents_tester()->TestSetIsLoading(true); 152 offliner()->web_contents_tester()->TestSetIsLoading(true);
148 } 153 }
149 154
150 void PumpLoop() { base::RunLoop().RunUntilIdle(); } 155 void PumpLoop() { base::RunLoop().RunUntilIdle(); }
151 156
152 private: 157 private:
153 void OnCompletion(const SavePageRequest& request, 158 void OnCompletion(const SavePageRequest& request,
154 Offliner::RequestStatus status); 159 Offliner::RequestStatus status);
155 void OnProgress(const SavePageRequest& request, int64_t bytes); 160 void OnProgress(const SavePageRequest& request, int64_t bytes);
161 void OnCancel(int64_t offline_id);
156 content::TestBrowserThreadBundle thread_bundle_; 162 content::TestBrowserThreadBundle thread_bundle_;
157 TestingProfile profile_; 163 TestingProfile profile_;
158 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; 164 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_;
159 MockOfflinePageModel* model_; 165 MockOfflinePageModel* model_;
160 bool completion_callback_called_; 166 bool completion_callback_called_;
167 bool cancel_callback_called_;
161 Offliner::RequestStatus request_status_; 168 Offliner::RequestStatus request_status_;
162 base::HistogramTester histogram_tester_; 169 base::HistogramTester histogram_tester_;
163 170
164 DISALLOW_COPY_AND_ASSIGN(BackgroundLoaderOfflinerTest); 171 DISALLOW_COPY_AND_ASSIGN(BackgroundLoaderOfflinerTest);
165 }; 172 };
166 173
167 BackgroundLoaderOfflinerTest::BackgroundLoaderOfflinerTest() 174 BackgroundLoaderOfflinerTest::BackgroundLoaderOfflinerTest()
168 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), 175 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
169 completion_callback_called_(false), 176 completion_callback_called_(false),
177 cancel_callback_called_(false),
170 request_status_(Offliner::RequestStatus::UNKNOWN) {} 178 request_status_(Offliner::RequestStatus::UNKNOWN) {}
171 179
172 BackgroundLoaderOfflinerTest::~BackgroundLoaderOfflinerTest() {} 180 BackgroundLoaderOfflinerTest::~BackgroundLoaderOfflinerTest() {}
173 181
174 void BackgroundLoaderOfflinerTest::SetUp() { 182 void BackgroundLoaderOfflinerTest::SetUp() {
175 model_ = new MockOfflinePageModel(); 183 model_ = new MockOfflinePageModel();
176 offliner_.reset(new TestBackgroundLoaderOffliner(profile(), nullptr, model_)); 184 offliner_.reset(new TestBackgroundLoaderOffliner(profile(), nullptr, model_));
177 offliner_->SetPageDelayForTest(0L); 185 offliner_->SetPageDelayForTest(0L);
178 } 186 }
179 187
180 void BackgroundLoaderOfflinerTest::OnCompletion( 188 void BackgroundLoaderOfflinerTest::OnCompletion(
181 const SavePageRequest& request, 189 const SavePageRequest& request,
182 Offliner::RequestStatus status) { 190 Offliner::RequestStatus status) {
183 DCHECK(!completion_callback_called_); // Expect 1 callback per request. 191 DCHECK(!completion_callback_called_); // Expect 1 callback per request.
184 completion_callback_called_ = true; 192 completion_callback_called_ = true;
185 request_status_ = status; 193 request_status_ = status;
186 } 194 }
187 195
188 void BackgroundLoaderOfflinerTest::OnProgress(const SavePageRequest& request, 196 void BackgroundLoaderOfflinerTest::OnProgress(const SavePageRequest& request,
189 int64_t bytes) {} 197 int64_t bytes) {}
198 void BackgroundLoaderOfflinerTest::OnCancel(int64_t offline_id) {
199 DCHECK(!cancel_callback_called_);
200 cancel_callback_called_ = true;
201 }
190 202
191 TEST_F(BackgroundLoaderOfflinerTest, 203 TEST_F(BackgroundLoaderOfflinerTest,
192 LoadAndSaveBlockThirdPartyCookiesForCustomTabs) { 204 LoadAndSaveBlockThirdPartyCookiesForCustomTabs) {
193 base::Time creation_time = base::Time::Now(); 205 base::Time creation_time = base::Time::Now();
194 ClientId custom_tabs_client_id("custom_tabs", "88"); 206 ClientId custom_tabs_client_id("custom_tabs", "88");
195 SavePageRequest request(kRequestId, kHttpUrl, custom_tabs_client_id, 207 SavePageRequest request(kRequestId, kHttpUrl, custom_tabs_client_id,
196 creation_time, kUserRequested); 208 creation_time, kUserRequested);
197 209
198 profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true); 210 profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true);
199 EXPECT_FALSE(offliner()->LoadAndSave(request, completion_callback(), 211 EXPECT_FALSE(offliner()->LoadAndSave(request, completion_callback(),
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 EXPECT_TRUE(SaveInProgress()); 267 EXPECT_TRUE(SaveInProgress());
256 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); 268 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status());
257 } 269 }
258 270
259 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoading) { 271 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoading) {
260 base::Time creation_time = base::Time::Now(); 272 base::Time creation_time = base::Time::Now();
261 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, 273 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time,
262 kUserRequested); 274 kUserRequested);
263 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), 275 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(),
264 progress_callback())); 276 progress_callback()));
265 offliner()->Cancel(); 277 offliner()->Cancel(cancel_callback());
278 PumpLoop();
279 EXPECT_TRUE(cancel_callback_called());
266 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. 280 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset.
267 } 281 }
268 282
269 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoaded) { 283 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoaded) {
270 base::Time creation_time = base::Time::Now(); 284 base::Time creation_time = base::Time::Now();
271 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, 285 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time,
272 kUserRequested); 286 kUserRequested);
273 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), 287 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(),
274 progress_callback())); 288 progress_callback()));
275 CompleteLoading(); 289 CompleteLoading();
276 PumpLoop(); 290 PumpLoop();
277 offliner()->Cancel(); 291 offliner()->Cancel(cancel_callback());
292 PumpLoop();
278 293
279 // Subsequent save callback cause no crash. 294 // Subsequent save callback cause no crash.
280 model()->CompleteSavingAsArchiveCreationFailed(); 295 model()->CompleteSavingAsArchiveCreationFailed();
281 PumpLoop(); 296 PumpLoop();
297 EXPECT_TRUE(cancel_callback_called());
282 EXPECT_FALSE(completion_callback_called()); 298 EXPECT_FALSE(completion_callback_called());
283 EXPECT_FALSE(SaveInProgress()); 299 EXPECT_FALSE(SaveInProgress());
284 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. 300 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset.
285 } 301 }
286 302
287 TEST_F(BackgroundLoaderOfflinerTest, LoadedButSaveFails) { 303 TEST_F(BackgroundLoaderOfflinerTest, LoadedButSaveFails) {
288 base::Time creation_time = base::Time::Now(); 304 base::Time creation_time = base::Time::Now();
289 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, 305 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time,
290 kUserRequested); 306 kUserRequested);
291 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(), 307 EXPECT_TRUE(offliner()->LoadAndSave(request, completion_callback(),
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 model()->CompleteSavingAsSuccess(); 420 model()->CompleteSavingAsSuccess();
405 PumpLoop(); 421 PumpLoop();
406 422
407 EXPECT_TRUE(completion_callback_called()); 423 EXPECT_TRUE(completion_callback_called());
408 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); 424 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status());
409 EXPECT_FALSE(offliner()->is_loading()); 425 EXPECT_FALSE(offliner()->is_loading());
410 EXPECT_FALSE(SaveInProgress()); 426 EXPECT_FALSE(SaveInProgress());
411 } 427 }
412 428
413 } // namespace offline_pages 429 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698