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

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

Issue 2113383002: More detailed implementation of the RequestPicker (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: second round of CR comments Created 4 years, 5 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/prerendering_offliner.h" 5 #include "chrome/browser/android/offline_pages/prerendering_offliner.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/threading/thread_task_runner_handle.h" 11 #include "base/threading/thread_task_runner_handle.h"
12 #include "chrome/browser/android/offline_pages/prerendering_loader.h" 12 #include "chrome/browser/android/offline_pages/prerendering_loader.h"
13 #include "chrome/test/base/testing_profile.h" 13 #include "chrome/test/base/testing_profile.h"
14 #include "components/offline_pages/background/offliner.h" 14 #include "components/offline_pages/background/offliner.h"
15 #include "components/offline_pages/background/save_page_request.h" 15 #include "components/offline_pages/background/save_page_request.h"
16 #include "components/offline_pages/stub_offline_page_model.h" 16 #include "components/offline_pages/stub_offline_page_model.h"
17 #include "content/public/test/test_browser_thread_bundle.h" 17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "content/public/test/web_contents_tester.h" 18 #include "content/public/test/web_contents_tester.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 20
21 namespace offline_pages { 21 namespace offline_pages {
22 22
23 namespace { 23 namespace {
24 const int64_t kRequestId = 7; 24 const int64_t kRequestId = 7;
25 const GURL kHttpUrl("http://tunafish.com"); 25 const GURL kHttpUrl("http://tunafish.com");
26 const GURL kFileUrl("file://sailfish.png"); 26 const GURL kFileUrl("file://sailfish.png");
27 const ClientId kClientId("AsyncLoading", "88"); 27 const ClientId kClientId("AsyncLoading", "88");
28 const bool kUserRequested = true;
28 29
29 // Mock Loader for testing the Offliner calls. 30 // Mock Loader for testing the Offliner calls.
30 class MockPrerenderingLoader : public PrerenderingLoader { 31 class MockPrerenderingLoader : public PrerenderingLoader {
31 public: 32 public:
32 explicit MockPrerenderingLoader(content::BrowserContext* browser_context) 33 explicit MockPrerenderingLoader(content::BrowserContext* browser_context)
33 : PrerenderingLoader(browser_context), 34 : PrerenderingLoader(browser_context),
34 can_prerender_(true), 35 can_prerender_(true),
35 mock_loading_(false), 36 mock_loading_(false),
36 mock_loaded_(false) {} 37 mock_loaded_(false) {}
37 ~MockPrerenderingLoader() override {} 38 ~MockPrerenderingLoader() override {}
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 192
192 void PrerenderingOfflinerTest::OnCompletion(const SavePageRequest& request, 193 void PrerenderingOfflinerTest::OnCompletion(const SavePageRequest& request,
193 Offliner::RequestStatus status) { 194 Offliner::RequestStatus status) {
194 DCHECK(!completion_callback_called_); // Expect single callback per request. 195 DCHECK(!completion_callback_called_); // Expect single callback per request.
195 completion_callback_called_ = true; 196 completion_callback_called_ = true;
196 request_status_ = status; 197 request_status_ = status;
197 } 198 }
198 199
199 TEST_F(PrerenderingOfflinerTest, LoadAndSaveBadUrl) { 200 TEST_F(PrerenderingOfflinerTest, LoadAndSaveBadUrl) {
200 base::Time creation_time = base::Time::Now(); 201 base::Time creation_time = base::Time::Now();
201 SavePageRequest request(kRequestId, kFileUrl, kClientId, creation_time); 202 SavePageRequest request(
203 kRequestId, kFileUrl, kClientId, creation_time, kUserRequested);
202 EXPECT_FALSE(offliner()->LoadAndSave(request, callback())); 204 EXPECT_FALSE(offliner()->LoadAndSave(request, callback()));
203 EXPECT_TRUE(loader()->IsIdle()); 205 EXPECT_TRUE(loader()->IsIdle());
204 } 206 }
205 207
206 TEST_F(PrerenderingOfflinerTest, LoadAndSavePrerenderingDisabled) { 208 TEST_F(PrerenderingOfflinerTest, LoadAndSavePrerenderingDisabled) {
207 base::Time creation_time = base::Time::Now(); 209 base::Time creation_time = base::Time::Now();
208 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time); 210 SavePageRequest request(
211 kRequestId, kHttpUrl, kClientId, creation_time, kUserRequested);
209 loader()->DisablePrerendering(); 212 loader()->DisablePrerendering();
210 EXPECT_FALSE(offliner()->LoadAndSave(request, callback())); 213 EXPECT_FALSE(offliner()->LoadAndSave(request, callback()));
211 EXPECT_TRUE(loader()->IsIdle()); 214 EXPECT_TRUE(loader()->IsIdle());
212 } 215 }
213 216
214 TEST_F(PrerenderingOfflinerTest, LoadAndSaveLoadStartedButFails) { 217 TEST_F(PrerenderingOfflinerTest, LoadAndSaveLoadStartedButFails) {
215 base::Time creation_time = base::Time::Now(); 218 base::Time creation_time = base::Time::Now();
216 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time); 219 SavePageRequest request(
220 kRequestId, kHttpUrl, kClientId, creation_time, kUserRequested);
217 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); 221 EXPECT_TRUE(offliner()->LoadAndSave(request, callback()));
218 EXPECT_FALSE(loader()->IsIdle()); 222 EXPECT_FALSE(loader()->IsIdle());
219 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); 223 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status());
220 224
221 loader()->CompleteLoadingAsFailed(); 225 loader()->CompleteLoadingAsFailed();
222 PumpLoop(); 226 PumpLoop();
223 EXPECT_TRUE(completion_callback_called()); 227 EXPECT_TRUE(completion_callback_called());
224 EXPECT_EQ(Offliner::RequestStatus::PRERENDERING_FAILED, request_status()); 228 EXPECT_EQ(Offliner::RequestStatus::PRERENDERING_FAILED, request_status());
225 EXPECT_TRUE(loader()->IsIdle()); 229 EXPECT_TRUE(loader()->IsIdle());
226 EXPECT_FALSE(SaveInProgress()); 230 EXPECT_FALSE(SaveInProgress());
227 } 231 }
228 232
229 TEST_F(PrerenderingOfflinerTest, CancelWhenLoading) { 233 TEST_F(PrerenderingOfflinerTest, CancelWhenLoading) {
230 base::Time creation_time = base::Time::Now(); 234 base::Time creation_time = base::Time::Now();
231 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time); 235 SavePageRequest request(
236 kRequestId, kHttpUrl, kClientId, creation_time, kUserRequested);
232 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); 237 EXPECT_TRUE(offliner()->LoadAndSave(request, callback()));
233 EXPECT_FALSE(loader()->IsIdle()); 238 EXPECT_FALSE(loader()->IsIdle());
234 239
235 offliner()->Cancel(); 240 offliner()->Cancel();
236 EXPECT_TRUE(loader()->IsIdle()); 241 EXPECT_TRUE(loader()->IsIdle());
237 } 242 }
238 243
239 TEST_F(PrerenderingOfflinerTest, CancelWhenLoaded) { 244 TEST_F(PrerenderingOfflinerTest, CancelWhenLoaded) {
240 base::Time creation_time = base::Time::Now(); 245 base::Time creation_time = base::Time::Now();
241 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time); 246 SavePageRequest request(
247 kRequestId, kHttpUrl, kClientId, creation_time, kUserRequested);
242 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); 248 EXPECT_TRUE(offliner()->LoadAndSave(request, callback()));
243 EXPECT_FALSE(loader()->IsIdle()); 249 EXPECT_FALSE(loader()->IsIdle());
244 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); 250 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status());
245 251
246 loader()->CompleteLoadingAsLoaded(); 252 loader()->CompleteLoadingAsLoaded();
247 PumpLoop(); 253 PumpLoop();
248 EXPECT_FALSE(completion_callback_called()); 254 EXPECT_FALSE(completion_callback_called());
249 EXPECT_TRUE(loader()->IsLoaded()); 255 EXPECT_TRUE(loader()->IsLoaded());
250 EXPECT_TRUE(SaveInProgress()); 256 EXPECT_TRUE(SaveInProgress());
251 257
252 offliner()->Cancel(); 258 offliner()->Cancel();
253 PumpLoop(); 259 PumpLoop();
254 EXPECT_FALSE(completion_callback_called()); 260 EXPECT_FALSE(completion_callback_called());
255 EXPECT_FALSE(loader()->IsLoaded()); 261 EXPECT_FALSE(loader()->IsLoaded());
256 // Note: save still in progress since it does not support canceling. 262 // Note: save still in progress since it does not support canceling.
257 EXPECT_TRUE(SaveInProgress()); 263 EXPECT_TRUE(SaveInProgress());
258 264
259 // Subsequent save callback causes no harm (no crash and no callback). 265 // Subsequent save callback causes no harm (no crash and no callback).
260 model()->CompleteSavingAsArchiveCreationFailed(); 266 model()->CompleteSavingAsArchiveCreationFailed();
261 PumpLoop(); 267 PumpLoop();
262 EXPECT_FALSE(completion_callback_called()); 268 EXPECT_FALSE(completion_callback_called());
263 EXPECT_TRUE(loader()->IsIdle()); 269 EXPECT_TRUE(loader()->IsIdle());
264 EXPECT_FALSE(SaveInProgress()); 270 EXPECT_FALSE(SaveInProgress());
265 } 271 }
266 272
267 TEST_F(PrerenderingOfflinerTest, LoadAndSaveLoadedButSaveFails) { 273 TEST_F(PrerenderingOfflinerTest, LoadAndSaveLoadedButSaveFails) {
268 base::Time creation_time = base::Time::Now(); 274 base::Time creation_time = base::Time::Now();
269 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time); 275 SavePageRequest request(
276 kRequestId, kHttpUrl, kClientId, creation_time, kUserRequested);
270 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); 277 EXPECT_TRUE(offliner()->LoadAndSave(request, callback()));
271 EXPECT_FALSE(loader()->IsIdle()); 278 EXPECT_FALSE(loader()->IsIdle());
272 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); 279 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status());
273 280
274 loader()->CompleteLoadingAsLoaded(); 281 loader()->CompleteLoadingAsLoaded();
275 PumpLoop(); 282 PumpLoop();
276 EXPECT_FALSE(completion_callback_called()); 283 EXPECT_FALSE(completion_callback_called());
277 EXPECT_TRUE(loader()->IsLoaded()); 284 EXPECT_TRUE(loader()->IsLoaded());
278 EXPECT_TRUE(SaveInProgress()); 285 EXPECT_TRUE(SaveInProgress());
279 286
280 model()->CompleteSavingAsArchiveCreationFailed(); 287 model()->CompleteSavingAsArchiveCreationFailed();
281 PumpLoop(); 288 PumpLoop();
282 EXPECT_TRUE(completion_callback_called()); 289 EXPECT_TRUE(completion_callback_called());
283 EXPECT_EQ(Offliner::RequestStatus::SAVE_FAILED, request_status()); 290 EXPECT_EQ(Offliner::RequestStatus::SAVE_FAILED, request_status());
284 EXPECT_FALSE(loader()->IsLoaded()); 291 EXPECT_FALSE(loader()->IsLoaded());
285 EXPECT_FALSE(SaveInProgress()); 292 EXPECT_FALSE(SaveInProgress());
286 } 293 }
287 294
288 TEST_F(PrerenderingOfflinerTest, LoadAndSaveSuccessful) { 295 TEST_F(PrerenderingOfflinerTest, LoadAndSaveSuccessful) {
289 base::Time creation_time = base::Time::Now(); 296 base::Time creation_time = base::Time::Now();
290 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time); 297 SavePageRequest request(
298 kRequestId, kHttpUrl, kClientId, creation_time, kUserRequested);
291 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); 299 EXPECT_TRUE(offliner()->LoadAndSave(request, callback()));
292 EXPECT_FALSE(loader()->IsIdle()); 300 EXPECT_FALSE(loader()->IsIdle());
293 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); 301 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status());
294 302
295 loader()->CompleteLoadingAsLoaded(); 303 loader()->CompleteLoadingAsLoaded();
296 PumpLoop(); 304 PumpLoop();
297 EXPECT_FALSE(completion_callback_called()); 305 EXPECT_FALSE(completion_callback_called());
298 EXPECT_TRUE(loader()->IsLoaded()); 306 EXPECT_TRUE(loader()->IsLoaded());
299 EXPECT_TRUE(SaveInProgress()); 307 EXPECT_TRUE(SaveInProgress());
300 308
301 model()->CompleteSavingAsSuccess(); 309 model()->CompleteSavingAsSuccess();
302 PumpLoop(); 310 PumpLoop();
303 EXPECT_TRUE(completion_callback_called()); 311 EXPECT_TRUE(completion_callback_called());
304 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status()); 312 EXPECT_EQ(Offliner::RequestStatus::SAVED, request_status());
305 EXPECT_FALSE(loader()->IsLoaded()); 313 EXPECT_FALSE(loader()->IsLoaded());
306 EXPECT_FALSE(SaveInProgress()); 314 EXPECT_FALSE(SaveInProgress());
307 } 315 }
308 316
309 TEST_F(PrerenderingOfflinerTest, LoadAndSaveLoadedButThenCanceledFromLoader) { 317 TEST_F(PrerenderingOfflinerTest, LoadAndSaveLoadedButThenCanceledFromLoader) {
310 base::Time creation_time = base::Time::Now(); 318 base::Time creation_time = base::Time::Now();
311 SavePageRequest request(kRequestId, kHttpUrl, kClientId, creation_time); 319 SavePageRequest request(
320 kRequestId, kHttpUrl, kClientId, creation_time, kUserRequested);
312 EXPECT_TRUE(offliner()->LoadAndSave(request, callback())); 321 EXPECT_TRUE(offliner()->LoadAndSave(request, callback()));
313 EXPECT_FALSE(loader()->IsIdle()); 322 EXPECT_FALSE(loader()->IsIdle());
314 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status()); 323 EXPECT_EQ(Offliner::RequestStatus::UNKNOWN, request_status());
315 324
316 loader()->CompleteLoadingAsLoaded(); 325 loader()->CompleteLoadingAsLoaded();
317 PumpLoop(); 326 PumpLoop();
318 EXPECT_FALSE(completion_callback_called()); 327 EXPECT_FALSE(completion_callback_called());
319 EXPECT_TRUE(loader()->IsLoaded()); 328 EXPECT_TRUE(loader()->IsLoaded());
320 EXPECT_TRUE(SaveInProgress()); 329 EXPECT_TRUE(SaveInProgress());
321 330
322 loader()->CompleteLoadingAsCanceled(); 331 loader()->CompleteLoadingAsCanceled();
323 PumpLoop(); 332 PumpLoop();
324 EXPECT_TRUE(completion_callback_called()); 333 EXPECT_TRUE(completion_callback_called());
325 EXPECT_EQ(Offliner::RequestStatus::PRERENDERING_CANCELED, request_status()); 334 EXPECT_EQ(Offliner::RequestStatus::PRERENDERING_CANCELED, request_status());
326 EXPECT_FALSE(loader()->IsLoaded()); 335 EXPECT_FALSE(loader()->IsLoaded());
327 // Note: save still in progress since it does not support canceling. 336 // Note: save still in progress since it does not support canceling.
328 EXPECT_TRUE(SaveInProgress()); 337 EXPECT_TRUE(SaveInProgress());
329 } 338 }
330 339
331 } // namespace offline_pages 340 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698