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

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

Issue 2715433006: [Offline Pages] Turn Offliner::Cancel into an async operation to resolve conflicting assumptions (Closed)
Patch Set: forgot to init 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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 BackgroundLoaderOfflinerTest(); 123 BackgroundLoaderOfflinerTest();
124 ~BackgroundLoaderOfflinerTest() override; 124 ~BackgroundLoaderOfflinerTest() override;
125 125
126 void SetUp() override; 126 void SetUp() override;
127 127
128 TestBackgroundLoaderOffliner* offliner() const { return offliner_.get(); } 128 TestBackgroundLoaderOffliner* offliner() const { return offliner_.get(); }
129 Offliner::CompletionCallback const callback() { 129 Offliner::CompletionCallback const 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::CancelCallback const cancel_callback() {
134 return base::Bind(&BackgroundLoaderOfflinerTest::OnCancel,
135 base::Unretained(this));
136 }
133 Profile* profile() { return &profile_; } 137 Profile* profile() { return &profile_; }
134 bool completion_callback_called() { return completion_callback_called_; } 138 bool completion_callback_called() { return completion_callback_called_; }
135 Offliner::RequestStatus request_status() { return request_status_; } 139 Offliner::RequestStatus request_status() { return request_status_; }
140 bool cancel_callback_called() { return cancel_callback_called_; }
136 bool SaveInProgress() const { return model_->mock_saving(); } 141 bool SaveInProgress() const { return model_->mock_saving(); }
137 MockOfflinePageModel* model() const { return model_; } 142 MockOfflinePageModel* model() const { return model_; }
138 const base::HistogramTester& histograms() const { return histogram_tester_; } 143 const base::HistogramTester& histograms() const { return histogram_tester_; }
139 144
140 void CompleteLoading() { 145 void CompleteLoading() {
141 // For some reason, setting loading to True will call DidStopLoading 146 // For some reason, setting loading to True will call DidStopLoading
142 // on the observers. 147 // on the observers.
143 offliner()->web_contents_tester()->TestSetIsLoading(true); 148 offliner()->web_contents_tester()->TestSetIsLoading(true);
144 } 149 }
145 150
146 void PumpLoop() { base::RunLoop().RunUntilIdle(); } 151 void PumpLoop() { base::RunLoop().RunUntilIdle(); }
147 152
148 private: 153 private:
149 void OnCompletion(const SavePageRequest& request, 154 void OnCompletion(const SavePageRequest& request,
150 Offliner::RequestStatus status); 155 Offliner::RequestStatus status);
156 void OnCancel(int64_t offline_id);
151 content::TestBrowserThreadBundle thread_bundle_; 157 content::TestBrowserThreadBundle thread_bundle_;
152 TestingProfile profile_; 158 TestingProfile profile_;
153 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_; 159 std::unique_ptr<TestBackgroundLoaderOffliner> offliner_;
154 MockOfflinePageModel* model_; 160 MockOfflinePageModel* model_;
155 bool completion_callback_called_; 161 bool completion_callback_called_;
162 bool cancel_callback_called_;
156 Offliner::RequestStatus request_status_; 163 Offliner::RequestStatus request_status_;
157 base::HistogramTester histogram_tester_; 164 base::HistogramTester histogram_tester_;
158 165
159 DISALLOW_COPY_AND_ASSIGN(BackgroundLoaderOfflinerTest); 166 DISALLOW_COPY_AND_ASSIGN(BackgroundLoaderOfflinerTest);
160 }; 167 };
161 168
162 BackgroundLoaderOfflinerTest::BackgroundLoaderOfflinerTest() 169 BackgroundLoaderOfflinerTest::BackgroundLoaderOfflinerTest()
163 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), 170 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
164 completion_callback_called_(false), 171 completion_callback_called_(false),
172 cancel_callback_called_(false),
165 request_status_(Offliner::RequestStatus::UNKNOWN) {} 173 request_status_(Offliner::RequestStatus::UNKNOWN) {}
166 174
167 BackgroundLoaderOfflinerTest::~BackgroundLoaderOfflinerTest() {} 175 BackgroundLoaderOfflinerTest::~BackgroundLoaderOfflinerTest() {}
168 176
169 void BackgroundLoaderOfflinerTest::SetUp() { 177 void BackgroundLoaderOfflinerTest::SetUp() {
170 model_ = new MockOfflinePageModel(); 178 model_ = new MockOfflinePageModel();
171 offliner_.reset(new TestBackgroundLoaderOffliner(profile(), nullptr, model_)); 179 offliner_.reset(new TestBackgroundLoaderOffliner(profile(), nullptr, model_));
172 offliner_->SetPageDelayForTest(0L); 180 offliner_->SetPageDelayForTest(0L);
173 } 181 }
174 182
175 void BackgroundLoaderOfflinerTest::OnCompletion( 183 void BackgroundLoaderOfflinerTest::OnCompletion(
176 const SavePageRequest& request, 184 const SavePageRequest& request,
177 Offliner::RequestStatus status) { 185 Offliner::RequestStatus status) {
178 DCHECK(!completion_callback_called_); // Expect 1 callback per request. 186 DCHECK(!completion_callback_called_); // Expect 1 callback per request.
179 completion_callback_called_ = true; 187 completion_callback_called_ = true;
180 request_status_ = status; 188 request_status_ = status;
181 } 189 }
182 190
191 void BackgroundLoaderOfflinerTest::OnCancel(int64_t offline_id) {
192 DCHECK(!cancel_callback_called_);
193 cancel_callback_called_ = true;
194 }
195
183 TEST_F(BackgroundLoaderOfflinerTest, 196 TEST_F(BackgroundLoaderOfflinerTest,
184 LoadAndSaveBlockThirdPartyCookiesForCustomTabs) { 197 LoadAndSaveBlockThirdPartyCookiesForCustomTabs) {
185 base::Time creation_time = base::Time::Now(); 198 base::Time creation_time = base::Time::Now();
186 ClientId custom_tabs_client_id("custom_tabs", "88"); 199 ClientId custom_tabs_client_id("custom_tabs", "88");
187 SavePageRequest request(kRequestId, kHttpUrl, custom_tabs_client_id, 200 SavePageRequest request(kRequestId, kHttpUrl, custom_tabs_client_id,
188 creation_time, kUserRequested); 201 creation_time, kUserRequested);
189 202
190 profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true); 203 profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true);
191 EXPECT_FALSE(offliner()->LoadAndSave(request, callback())); 204 EXPECT_FALSE(offliner()->LoadAndSave(request, callback()));
192 histograms().ExpectBucketCount( 205 histograms().ExpectBucketCount(
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 EXPECT_FALSE(completion_callback_called()); 255 EXPECT_FALSE(completion_callback_called());
243 EXPECT_TRUE(SaveInProgress()); 256 EXPECT_TRUE(SaveInProgress());
244 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); 257 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status());
245 } 258 }
246 259
247 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoading) { 260 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoading) {
248 base::Time creation_time = base::Time::Now(); 261 base::Time creation_time = base::Time::Now();
249 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, 262 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time,
250 kUserRequested); 263 kUserRequested);
251 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); 264 EXPECT_TRUE(offliner()->LoadAndSave(request, callback()));
252 offliner()->Cancel(); 265 offliner()->Cancel(cancel_callback());
266 PumpLoop();
267 EXPECT_TRUE(cancel_callback_called());
253 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. 268 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset.
254 } 269 }
255 270
256 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoaded) { 271 TEST_F(BackgroundLoaderOfflinerTest, CancelWhenLoaded) {
257 base::Time creation_time = base::Time::Now(); 272 base::Time creation_time = base::Time::Now();
258 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, 273 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time,
259 kUserRequested); 274 kUserRequested);
260 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); 275 EXPECT_TRUE(offliner()->LoadAndSave(request, callback()));
261 CompleteLoading(); 276 CompleteLoading();
262 PumpLoop(); 277 PumpLoop();
263 offliner()->Cancel(); 278 offliner()->Cancel(cancel_callback());
279 PumpLoop();
264 280
265 // Subsequent save callback cause no crash. 281 // Subsequent save callback cause no crash.
266 model()->CompleteSavingAsArchiveCreationFailed(); 282 model()->CompleteSavingAsArchiveCreationFailed();
267 PumpLoop(); 283 PumpLoop();
284 EXPECT_TRUE(cancel_callback_called());
268 EXPECT_FALSE(completion_callback_called()); 285 EXPECT_FALSE(completion_callback_called());
269 EXPECT_FALSE(SaveInProgress()); 286 EXPECT_FALSE(SaveInProgress());
270 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset. 287 EXPECT_FALSE(offliner()->is_loading()); // Offliner reset.
271 } 288 }
272 289
273 TEST_F(BackgroundLoaderOfflinerTest, LoadedButSaveFails) { 290 TEST_F(BackgroundLoaderOfflinerTest, LoadedButSaveFails) {
274 base::Time creation_time = base::Time::Now(); 291 base::Time creation_time = base::Time::Now();
275 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time, 292 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time,
276 kUserRequested); 293 kUserRequested);
277 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); 294 EXPECT_TRUE(offliner()->LoadAndSave(request, callback()));
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 model()->CompleteSavingAsSuccess(); 399 model()->CompleteSavingAsSuccess();
383 PumpLoop(); 400 PumpLoop();
384 401
385 EXPECT_TRUE(completion_callback_called()); 402 EXPECT_TRUE(completion_callback_called());
386 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); 403 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status());
387 EXPECT_FALSE(offliner()->is_loading()); 404 EXPECT_FALSE(offliner()->is_loading());
388 EXPECT_FALSE(SaveInProgress()); 405 EXPECT_FALSE(SaveInProgress());
389 } 406 }
390 407
391 } // namespace offline_pages 408 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698