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

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

Issue 2322833002: Support serving offline page by offline ID (Closed)
Patch Set: Remove unused variable to fix trybot Created 4 years, 3 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/offline_page_request_job.h" 5 #include "chrome/browser/android/offline_pages/offline_page_request_job.h"
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/feature_list.h" 8 #include "base/feature_list.h"
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/metrics/field_trial.h" 12 #include "base/metrics/field_trial.h"
13 #include "base/path_service.h" 13 #include "base/path_service.h"
14 #include "base/run_loop.h" 14 #include "base/run_loop.h"
15 #include "base/strings/string_number_conversions.h"
15 #include "base/test/histogram_tester.h" 16 #include "base/test/histogram_tester.h"
17 #include "base/test/simple_test_clock.h"
16 #include "base/threading/thread_task_runner_handle.h" 18 #include "base/threading/thread_task_runner_handle.h"
17 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" 19 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
18 #include "chrome/browser/android/offline_pages/offline_page_request_interceptor. h" 20 #include "chrome/browser/android/offline_pages/offline_page_request_interceptor. h"
19 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" 21 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h"
20 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h " 22 #include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h "
21 #include "chrome/common/chrome_paths.h" 23 #include "chrome/common/chrome_paths.h"
22 #include "chrome/test/base/testing_browser_process.h" 24 #include "chrome/test/base/testing_browser_process.h"
23 #include "chrome/test/base/testing_profile.h" 25 #include "chrome/test/base/testing_profile.h"
24 #include "chrome/test/base/testing_profile_manager.h" 26 #include "chrome/test/base/testing_profile_manager.h"
25 #include "components/offline_pages/client_namespace_constants.h" 27 #include "components/offline_pages/client_namespace_constants.h"
26 #include "components/offline_pages/offline_page_model.h" 28 #include "components/offline_pages/offline_page_model_impl.h"
27 #include "components/previews/previews_experiments.h" 29 #include "components/previews/previews_experiments.h"
28 #include "components/variations/variations_associated_data.h" 30 #include "components/variations/variations_associated_data.h"
29 #include "content/public/browser/browser_thread.h" 31 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/resource_request_info.h" 32 #include "content/public/browser/resource_request_info.h"
31 #include "content/public/browser/web_contents.h" 33 #include "content/public/browser/web_contents.h"
32 #include "content/public/common/resource_type.h" 34 #include "content/public/common/resource_type.h"
33 #include "content/public/test/test_browser_thread_bundle.h" 35 #include "content/public/test/test_browser_thread_bundle.h"
34 #include "net/nqe/network_quality_estimator.h" 36 #include "net/nqe/network_quality_estimator.h"
35 #include "net/url_request/url_request.h" 37 #include "net/url_request/url_request.h"
36 #include "net/url_request/url_request_context.h" 38 #include "net/url_request/url_request_context.h"
37 #include "net/url_request/url_request_intercepting_job_factory.h" 39 #include "net/url_request/url_request_intercepting_job_factory.h"
38 #include "net/url_request/url_request_job_factory_impl.h" 40 #include "net/url_request/url_request_job_factory_impl.h"
39 #include "net/url_request/url_request_test_util.h" 41 #include "net/url_request/url_request_test_util.h"
40 #include "testing/gtest/include/gtest/gtest.h" 42 #include "testing/gtest/include/gtest/gtest.h"
41 43
42 namespace offline_pages { 44 namespace offline_pages {
43 45
44 namespace { 46 namespace {
45 47
46 const GURL kTestUrl("http://test.org/page1"); 48 const GURL kTestUrl("http://test.org/page1");
47 const GURL kTestUrl2("http://test.org/page2"); 49 const GURL kTestUrl2("http://test.org/page2");
48 const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234"); 50 const ClientId kTestClientId = ClientId(kBookmarkNamespace, "1234");
51 const ClientId kTestClientId2 = ClientId(kDownloadNamespace, "1a2b3c4d");
49 const int kTestFileSize = 444; 52 const int kTestFileSize = 444;
53 const int kTestFileSize2 = 450;
50 const int kTabId = 1; 54 const int kTabId = 1;
51 const int kBufSize = 1024; 55 const int kBufSize = 1024;
52 const char kAggregatedRequestResultHistogram[] = 56 const char kAggregatedRequestResultHistogram[] =
53 "OfflinePages.AggregatedRequestResult"; 57 "OfflinePages.AggregatedRequestResult";
54 58
55 class OfflinePageRequestJobTestDelegate : 59 class OfflinePageRequestJobTestDelegate :
56 public OfflinePageRequestJob::Delegate { 60 public OfflinePageRequestJob::Delegate {
57 public: 61 public:
58 OfflinePageRequestJobTestDelegate(content::WebContents* web_content, 62 OfflinePageRequestJobTestDelegate(content::WebContents* web_content,
59 int tab_id) 63 int tab_id)
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 private: 223 private:
220 base::FieldTrialList field_trial_list_; 224 base::FieldTrialList field_trial_list_;
221 std::map<std::string, std::string> network_quality_estimator_params_; 225 std::map<std::string, std::string> network_quality_estimator_params_;
222 std::unique_ptr<TestNetworkQualityEstimator> test_network_quality_estimator_; 226 std::unique_ptr<TestNetworkQualityEstimator> test_network_quality_estimator_;
223 net::URLRequestContext* url_request_context_; 227 net::URLRequestContext* url_request_context_;
224 }; 228 };
225 229
226 class TestOfflinePageArchiver : public OfflinePageArchiver { 230 class TestOfflinePageArchiver : public OfflinePageArchiver {
227 public: 231 public:
228 TestOfflinePageArchiver(const GURL& url, 232 TestOfflinePageArchiver(const GURL& url,
229 const base::FilePath& archive_file_path) 233 const base::FilePath& archive_file_path,
234 int archive_file_size)
230 : url_(url), 235 : url_(url),
231 archive_file_path_(archive_file_path) {} 236 archive_file_path_(archive_file_path),
237 archive_file_size_(archive_file_size) {}
232 ~TestOfflinePageArchiver() override {} 238 ~TestOfflinePageArchiver() override {}
233 239
234 void CreateArchive(const base::FilePath& archives_dir, 240 void CreateArchive(const base::FilePath& archives_dir,
235 int64_t archive_id, 241 int64_t archive_id,
236 const CreateArchiveCallback& callback) override { 242 const CreateArchiveCallback& callback) override {
237 base::ThreadTaskRunnerHandle::Get()->PostTask( 243 base::ThreadTaskRunnerHandle::Get()->PostTask(
238 FROM_HERE, 244 FROM_HERE,
239 base::Bind(callback, this, ArchiverResult::SUCCESSFULLY_CREATED, 245 base::Bind(callback, this, ArchiverResult::SUCCESSFULLY_CREATED,
240 url_, archive_file_path_, base::string16(), kTestFileSize)); 246 url_, archive_file_path_, base::string16(),
247 archive_file_size_));
241 } 248 }
242 249
243 private: 250 private:
244 const GURL url_; 251 const GURL url_;
245 const base::FilePath archive_file_path_; 252 const base::FilePath archive_file_path_;
253 const int archive_file_size_;
246 254
247 DISALLOW_COPY_AND_ASSIGN(TestOfflinePageArchiver); 255 DISALLOW_COPY_AND_ASSIGN(TestOfflinePageArchiver);
248 }; 256 };
249 257
250 } // namespace 258 } // namespace
251 259
252 class OfflinePageRequestJobTest : public testing::Test { 260 class OfflinePageRequestJobTest : public testing::Test {
253 public: 261 public:
254 OfflinePageRequestJobTest(); 262 OfflinePageRequestJobTest();
255 ~OfflinePageRequestJobTest() override {} 263 ~OfflinePageRequestJobTest() override {}
256 264
257 void SetUp() override; 265 void SetUp() override;
266 void TearDown() override;
258 267
259 void SimulateHasNetworkConnectivity(bool has_connectivity); 268 void SimulateHasNetworkConnectivity(bool has_connectivity);
260 void RunUntilIdle(); 269 void RunUntilIdle();
261 270
262 void InterceptRequest(const GURL& url, 271 void InterceptRequest(const GURL& url,
263 const std::string& method, 272 const std::string& method,
264 const std::string& extra_header_name, 273 const std::string& extra_header_name,
265 const std::string& extra_header_value, 274 const std::string& extra_header_value,
266 content::ResourceType resource_type); 275 content::ResourceType resource_type);
267 276
268 void ExpectAggregatedRequestResultHistogram( 277 void ExpectAggregatedRequestResultHistogram(
269 OfflinePageRequestJob::AggregatedRequestResult result); 278 OfflinePageRequestJob::AggregatedRequestResult result);
270 279
271 net::TestURLRequestContext* url_request_context() { 280 net::TestURLRequestContext* url_request_context() {
272 return test_url_request_context_.get(); 281 return test_url_request_context_.get();
273 } 282 }
274 Profile* profile() { return profile_; } 283 Profile* profile() { return profile_; }
275 OfflinePageTabHelper* offline_page_tab_helper() const { 284 OfflinePageTabHelper* offline_page_tab_helper() const {
276 return offline_page_tab_helper_; 285 return offline_page_tab_helper_;
277 } 286 }
278 int64_t offline_id() const { return offline_id_; } 287 int64_t offline_id() const { return offline_id_; }
288 int64_t offline_id2() const { return offline_id2_; }
279 int bytes_read() const { return bytes_read_; } 289 int bytes_read() const { return bytes_read_; }
280 290
281 private: 291 private:
282 void OnSavePageDone(SavePageResult result, int64_t offline_id); 292 void OnSavePageDone(SavePageResult result, int64_t offline_id);
283 std::unique_ptr<net::URLRequest> CreateRequest( 293 std::unique_ptr<net::URLRequest> CreateRequest(
284 const GURL& url, 294 const GURL& url,
285 const std::string& method, 295 const std::string& method,
286 content::ResourceType resource_type); 296 content::ResourceType resource_type);
287 void ReadCompleted(int bytes_read); 297 void ReadCompleted(int bytes_read);
288 298
289 // Runs on IO thread. 299 // Runs on IO thread.
290 void InterceptRequestOnIO(const GURL& url, 300 void InterceptRequestOnIO(const GURL& url,
291 const std::string& method, 301 const std::string& method,
292 const std::string& extra_header_name, 302 const std::string& extra_header_name,
293 const std::string& extra_header_value, 303 const std::string& extra_header_value,
294 content::ResourceType resource_type, 304 content::ResourceType resource_type);
295 void* profile_id);
296 void ReadCompletedOnIO(int bytes_read); 305 void ReadCompletedOnIO(int bytes_read);
297 306
298 content::TestBrowserThreadBundle thread_bundle_; 307 content::TestBrowserThreadBundle thread_bundle_;
308 base::SimpleTestClock clock_;
299 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_; 309 std::unique_ptr<TestNetworkChangeNotifier> network_change_notifier_;
300 std::unique_ptr<net::TestURLRequestContext> test_url_request_context_; 310 std::unique_ptr<net::TestURLRequestContext> test_url_request_context_;
301 net::URLRequestJobFactoryImpl url_request_job_factory_; 311 net::URLRequestJobFactoryImpl url_request_job_factory_;
302 std::unique_ptr<net::URLRequestInterceptingJobFactory> 312 std::unique_ptr<net::URLRequestInterceptingJobFactory>
303 intercepting_job_factory_; 313 intercepting_job_factory_;
304 std::unique_ptr<TestURLRequestDelegate> url_request_delegate_; 314 std::unique_ptr<TestURLRequestDelegate> url_request_delegate_;
305 net::TestNetworkDelegate network_delegate_; 315 net::TestNetworkDelegate network_delegate_;
306 TestingProfileManager profile_manager_; 316 TestingProfileManager profile_manager_;
307 TestingProfile* profile_; 317 TestingProfile* profile_;
308 std::unique_ptr<content::WebContents> web_contents_; 318 std::unique_ptr<content::WebContents> web_contents_;
309 base::HistogramTester histogram_tester_; 319 base::HistogramTester histogram_tester_;
310 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned. 320 OfflinePageTabHelper* offline_page_tab_helper_; // Not owned.
311 std::unique_ptr<net::URLRequest> request_; 321 std::unique_ptr<net::URLRequest> request_;
312 int64_t offline_id_; 322 int64_t offline_id_;
323 int64_t offline_id2_;
313 int bytes_read_; 324 int bytes_read_;
314 325
315 DISALLOW_COPY_AND_ASSIGN(OfflinePageRequestJobTest); 326 DISALLOW_COPY_AND_ASSIGN(OfflinePageRequestJobTest);
316 }; 327 };
317 328
318 OfflinePageRequestJobTest::OfflinePageRequestJobTest() 329 OfflinePageRequestJobTest::OfflinePageRequestJobTest()
319 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), 330 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
320 network_change_notifier_(new TestNetworkChangeNotifier()), 331 network_change_notifier_(new TestNetworkChangeNotifier()),
321 profile_manager_(TestingBrowserProcess::GetGlobal()), 332 profile_manager_(TestingBrowserProcess::GetGlobal()),
322 offline_id_(-1), 333 offline_id_(-1),
334 offline_id2_(-1),
323 bytes_read_(0) { 335 bytes_read_(0) {
324 } 336 }
325 337
326 void OfflinePageRequestJobTest::SetUp() { 338 void OfflinePageRequestJobTest::SetUp() {
327 // Create a test profile. 339 // Create a test profile.
328 ASSERT_TRUE(profile_manager_.SetUp()); 340 ASSERT_TRUE(profile_manager_.SetUp());
329 profile_ = profile_manager_.CreateTestingProfile("Profile 1"); 341 profile_ = profile_manager_.CreateTestingProfile("Profile 1");
330 342
331 // Create a test web contents. 343 // Create a test web contents.
332 web_contents_.reset(content::WebContents::Create( 344 web_contents_.reset(content::WebContents::Create(
333 content::WebContents::CreateParams(profile_))); 345 content::WebContents::CreateParams(profile_)));
334 OfflinePageTabHelper::CreateForWebContents(web_contents_.get()); 346 OfflinePageTabHelper::CreateForWebContents(web_contents_.get());
335 offline_page_tab_helper_ = 347 offline_page_tab_helper_ =
336 OfflinePageTabHelper::FromWebContents(web_contents_.get()); 348 OfflinePageTabHelper::FromWebContents(web_contents_.get());
337 349
338 // Set up the factory for testing. 350 // Set up the factory for testing.
339 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse( 351 OfflinePageModelFactory::GetInstance()->SetTestingFactoryAndUse(
340 profile(), BuildTestOfflinePageModel); 352 profile(), BuildTestOfflinePageModel);
341 RunUntilIdle(); 353 RunUntilIdle();
342 354
343 // Use a test archive file. 355 OfflinePageModel* model =
344 base::FilePath archive_file_path; 356 OfflinePageModelFactory::GetForBrowserContext(profile());
345 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &archive_file_path)); 357
346 archive_file_path = 358 // Hook up a test clock such that we can control the time when the offline
347 archive_file_path.AppendASCII("offline_pages").AppendASCII("test.mhtml"); 359 // page is created.
348 std::unique_ptr<TestOfflinePageArchiver> archiver( 360 clock_.SetNow(base::Time::Now());
349 new TestOfflinePageArchiver(kTestUrl, archive_file_path)); 361 static_cast<OfflinePageModelImpl*>(model)->set_testing_clock(&clock_);
362
363 // All offline pages being created below will point to real archive files
364 // residing in test data directory.
365 base::FilePath test_data_dir_path;
366 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_path));
350 367
351 // Save an offline page. 368 // Save an offline page.
352 OfflinePageModel* model = 369 base::FilePath archive_file_path =
353 OfflinePageModelFactory::GetForBrowserContext(profile()); 370 test_data_dir_path.AppendASCII("offline_pages").AppendASCII("test.mhtml");
371 std::unique_ptr<TestOfflinePageArchiver> archiver(
372 new TestOfflinePageArchiver(kTestUrl, archive_file_path, kTestFileSize));
373
354 model->SavePage( 374 model->SavePage(
355 kTestUrl, kTestClientId, 0, std::move(archiver), 375 kTestUrl, kTestClientId, 0, std::move(archiver),
356 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone, 376 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone,
357 base::Unretained(this))); 377 base::Unretained(this)));
358 RunUntilIdle(); 378 RunUntilIdle();
359 379
380 // Save another offline page associated with same online URL as above, but
381 // pointing to different archive file.
382 base::FilePath archive_file_path2 =
383 test_data_dir_path.AppendASCII("offline_pages").
384 AppendASCII("hello.mhtml");
385 std::unique_ptr<TestOfflinePageArchiver> archiver2(
386 new TestOfflinePageArchiver(
387 kTestUrl, archive_file_path2, kTestFileSize2));
388
389 // Make sure that the creation time of 2nd offline file is later.
390 clock_.Advance(base::TimeDelta::FromMinutes(10));
391
392 model->SavePage(
393 kTestUrl, kTestClientId2, 0, std::move(archiver2),
394 base::Bind(&OfflinePageRequestJobTest::OnSavePageDone,
395 base::Unretained(this)));
396 RunUntilIdle();
397
360 // Create a context with delayed initialization. 398 // Create a context with delayed initialization.
361 test_url_request_context_.reset(new net::TestURLRequestContext(true)); 399 test_url_request_context_.reset(new net::TestURLRequestContext(true));
362 400
363 // Install the interceptor. 401 // Install the interceptor.
364 std::unique_ptr<net::URLRequestInterceptor> interceptor( 402 std::unique_ptr<net::URLRequestInterceptor> interceptor(
365 new OfflinePageRequestInterceptor(profile_)); 403 new OfflinePageRequestInterceptor());
366 std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory_impl( 404 std::unique_ptr<net::URLRequestJobFactoryImpl> job_factory_impl(
367 new net::URLRequestJobFactoryImpl()); 405 new net::URLRequestJobFactoryImpl());
368 intercepting_job_factory_.reset(new TestURLRequestInterceptingJobFactory( 406 intercepting_job_factory_.reset(new TestURLRequestInterceptingJobFactory(
369 std::move(job_factory_impl), 407 std::move(job_factory_impl),
370 std::move(interceptor), 408 std::move(interceptor),
371 web_contents_.get())); 409 web_contents_.get()));
372 410
373 test_url_request_context_->set_job_factory(intercepting_job_factory_.get()); 411 test_url_request_context_->set_job_factory(intercepting_job_factory_.get());
374 test_url_request_context_->Init(); 412 test_url_request_context_->Init();
375 } 413 }
376 414
415 void OfflinePageRequestJobTest::TearDown() {
416 OfflinePageModel* model =
417 OfflinePageModelFactory::GetForBrowserContext(profile());
418 static_cast<OfflinePageModelImpl*>(model)->set_testing_clock(nullptr);
419 }
420
377 void OfflinePageRequestJobTest::SimulateHasNetworkConnectivity( 421 void OfflinePageRequestJobTest::SimulateHasNetworkConnectivity(
378 bool online) { 422 bool online) {
379 network_change_notifier_->set_online(online); 423 network_change_notifier_->set_online(online);
380 } 424 }
381 425
382 void OfflinePageRequestJobTest::RunUntilIdle() { 426 void OfflinePageRequestJobTest::RunUntilIdle() {
383 base::RunLoop().RunUntilIdle(); 427 base::RunLoop().RunUntilIdle();
384 } 428 }
385 429
386 std::unique_ptr<net::URLRequest> OfflinePageRequestJobTest::CreateRequest( 430 std::unique_ptr<net::URLRequest> OfflinePageRequestJobTest::CreateRequest(
(...skipping 25 matching lines...) Expand all
412 return request; 456 return request;
413 } 457 }
414 458
415 void OfflinePageRequestJobTest::ExpectAggregatedRequestResultHistogram( 459 void OfflinePageRequestJobTest::ExpectAggregatedRequestResultHistogram(
416 OfflinePageRequestJob::AggregatedRequestResult result) { 460 OfflinePageRequestJob::AggregatedRequestResult result) {
417 histogram_tester_.ExpectUniqueSample( 461 histogram_tester_.ExpectUniqueSample(
418 kAggregatedRequestResultHistogram, static_cast<int>(result), 1); 462 kAggregatedRequestResultHistogram, static_cast<int>(result), 1);
419 } 463 }
420 464
421 void OfflinePageRequestJobTest::OnSavePageDone(SavePageResult result, 465 void OfflinePageRequestJobTest::OnSavePageDone(SavePageResult result,
422 int64_t offline_id) { 466 int64_t offline_id) {
423 ASSERT_EQ(SavePageResult::SUCCESS, result); 467 ASSERT_EQ(SavePageResult::SUCCESS, result);
424 offline_id_ = offline_id; 468 if (offline_id_ == -1)
469 offline_id_ = offline_id;
470 else if (offline_id2_ == -1)
471 offline_id2_ = offline_id;
425 } 472 }
426 473
427 void OfflinePageRequestJobTest::InterceptRequestOnIO( 474 void OfflinePageRequestJobTest::InterceptRequestOnIO(
428 const GURL& url, 475 const GURL& url,
429 const std::string& method, 476 const std::string& method,
430 const std::string& extra_header_name, 477 const std::string& extra_header_name,
431 const std::string& extra_header_value, 478 const std::string& extra_header_value,
432 content::ResourceType resource_type, 479 content::ResourceType resource_type) {
433 void* profile_id) {
434 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 480 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
435 481
436 request_ = CreateRequest(url, method, resource_type); 482 request_ = CreateRequest(url, method, resource_type);
437 if (!extra_header_name.empty()) { 483 if (!extra_header_name.empty()) {
438 request_->SetExtraRequestHeaderByName( 484 request_->SetExtraRequestHeaderByName(
439 extra_header_name, extra_header_value, true); 485 extra_header_name, extra_header_value, true);
440 } 486 }
441 request_->Start(); 487 request_->Start();
442 } 488 }
443 489
444 void OfflinePageRequestJobTest::InterceptRequest( 490 void OfflinePageRequestJobTest::InterceptRequest(
445 const GURL& url, 491 const GURL& url,
446 const std::string& method, 492 const std::string& method,
447 const std::string& extra_header_name, 493 const std::string& extra_header_name,
448 const std::string& extra_header_value, 494 const std::string& extra_header_value,
449 content::ResourceType resource_type) { 495 content::ResourceType resource_type) {
450 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 496 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
451 497
452 content::BrowserThread::PostTask( 498 content::BrowserThread::PostTask(
453 content::BrowserThread::IO, FROM_HERE, 499 content::BrowserThread::IO, FROM_HERE,
454 base::Bind(&OfflinePageRequestJobTest::InterceptRequestOnIO, 500 base::Bind(&OfflinePageRequestJobTest::InterceptRequestOnIO,
455 base::Unretained(this), url, method, extra_header_name, 501 base::Unretained(this), url, method, extra_header_name,
456 extra_header_value, resource_type, profile())); 502 extra_header_value, resource_type));
457 } 503 }
458 504
459 void OfflinePageRequestJobTest::ReadCompletedOnIO(int bytes_read) { 505 void OfflinePageRequestJobTest::ReadCompletedOnIO(int bytes_read) {
460 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 506 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
461 507
462 content::BrowserThread::PostTask( 508 content::BrowserThread::PostTask(
463 content::BrowserThread::UI, FROM_HERE, 509 content::BrowserThread::UI, FROM_HERE,
464 base::Bind(&OfflinePageRequestJobTest::ReadCompleted, 510 base::Bind(&OfflinePageRequestJobTest::ReadCompleted,
465 base::Unretained(this), bytes_read)); 511 base::Unretained(this), bytes_read));
466 } 512 }
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 EXPECT_EQ(0, bytes_read()); 561 EXPECT_EQ(0, bytes_read());
516 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); 562 EXPECT_FALSE(offline_page_tab_helper()->offline_page());
517 } 563 }
518 564
519 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnDisconnectedNetwork) { 565 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnDisconnectedNetwork) {
520 SimulateHasNetworkConnectivity(false); 566 SimulateHasNetworkConnectivity(false);
521 567
522 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); 568 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME);
523 base::RunLoop().Run(); 569 base::RunLoop().Run();
524 570
525 EXPECT_EQ(kTestFileSize, bytes_read()); 571 EXPECT_EQ(kTestFileSize2, bytes_read());
526 ASSERT_TRUE(offline_page_tab_helper()->offline_page()); 572 ASSERT_TRUE(offline_page_tab_helper()->offline_page());
527 EXPECT_EQ(offline_id(), 573 EXPECT_EQ(offline_id2(),
528 offline_page_tab_helper()->offline_page()->offline_id); 574 offline_page_tab_helper()->offline_page()->offline_id);
529 ExpectAggregatedRequestResultHistogram( 575 ExpectAggregatedRequestResultHistogram(
530 OfflinePageRequestJob::AggregatedRequestResult:: 576 OfflinePageRequestJob::AggregatedRequestResult::
531 SHOW_OFFLINE_ON_DISCONNECTED_NETWORK); 577 SHOW_OFFLINE_ON_DISCONNECTED_NETWORK);
532 } 578 }
533 579
534 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnDisconnectedNetwork) { 580 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnDisconnectedNetwork) {
535 SimulateHasNetworkConnectivity(false); 581 SimulateHasNetworkConnectivity(false);
536 582
537 InterceptRequest(kTestUrl2, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); 583 InterceptRequest(kTestUrl2, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME);
538 base::RunLoop().Run(); 584 base::RunLoop().Run();
539 585
540 EXPECT_EQ(0, bytes_read()); 586 EXPECT_EQ(0, bytes_read());
541 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); 587 EXPECT_FALSE(offline_page_tab_helper()->offline_page());
542 ExpectAggregatedRequestResultHistogram( 588 ExpectAggregatedRequestResultHistogram(
543 OfflinePageRequestJob::AggregatedRequestResult:: 589 OfflinePageRequestJob::AggregatedRequestResult::
544 PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK); 590 PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK);
545 } 591 }
546 592
547 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnProhibitivelySlowNetwork) { 593 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnProhibitivelySlowNetwork) {
548 SimulateHasNetworkConnectivity(true); 594 SimulateHasNetworkConnectivity(true);
549 595
550 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context()); 596 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context());
551 597
552 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); 598 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME);
553 base::RunLoop().Run(); 599 base::RunLoop().Run();
554 600
555 EXPECT_EQ(kTestFileSize, bytes_read()); 601 EXPECT_EQ(kTestFileSize2, bytes_read());
556 ASSERT_TRUE(offline_page_tab_helper()->offline_page()); 602 ASSERT_TRUE(offline_page_tab_helper()->offline_page());
557 EXPECT_EQ(offline_id(), 603 EXPECT_EQ(offline_id2(),
558 offline_page_tab_helper()->offline_page()->offline_id); 604 offline_page_tab_helper()->offline_page()->offline_id);
559 ExpectAggregatedRequestResultHistogram( 605 ExpectAggregatedRequestResultHistogram(
560 OfflinePageRequestJob::AggregatedRequestResult:: 606 OfflinePageRequestJob::AggregatedRequestResult::
561 SHOW_OFFLINE_ON_PROHIBITIVELY_SLOW_NETWORK); 607 SHOW_OFFLINE_ON_PROHIBITIVELY_SLOW_NETWORK);
562 } 608 }
563 609
564 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnProhibitivelySlowNetwork) { 610 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnProhibitivelySlowNetwork) {
565 SimulateHasNetworkConnectivity(true); 611 SimulateHasNetworkConnectivity(true);
566 612
567 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context()); 613 ScopedEnableProbihibitivelySlowNetwork scoped(url_request_context());
568 614
569 InterceptRequest(kTestUrl2, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); 615 InterceptRequest(kTestUrl2, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME);
570 base::RunLoop().Run(); 616 base::RunLoop().Run();
571 617
572 EXPECT_EQ(0, bytes_read()); 618 EXPECT_EQ(0, bytes_read());
573 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); 619 EXPECT_FALSE(offline_page_tab_helper()->offline_page());
574 ExpectAggregatedRequestResultHistogram( 620 ExpectAggregatedRequestResultHistogram(
575 OfflinePageRequestJob::AggregatedRequestResult:: 621 OfflinePageRequestJob::AggregatedRequestResult::
576 PAGE_NOT_FOUND_ON_PROHIBITIVELY_SLOW_NETWORK); 622 PAGE_NOT_FOUND_ON_PROHIBITIVELY_SLOW_NETWORK);
577 } 623 }
578 624
579 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnFlakyNetwork) { 625 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageOnFlakyNetwork) {
580 SimulateHasNetworkConnectivity(true); 626 SimulateHasNetworkConnectivity(true);
581 627
582 // When custom offline header exists and contains "reason=error", it means 628 // When custom offline header exists and contains "reason=error", it means
583 // that net error is hit in last request due to flaky network. 629 // that net error is hit in last request due to flaky network.
584 InterceptRequest( 630 InterceptRequest(
585 kTestUrl, 631 kTestUrl,
586 "GET", 632 "GET",
587 kLoadingOfflinePageHeader, 633 kOfflinePageHeader,
588 std::string(kLoadingOfflinePageReason) + kLoadingOfflinePageDueToNetError, 634 std::string(kOfflinePageHeaderReasonKey) + "=" +
635 kOfflinePageHeaderReasonValueDueToNetError,
589 content::RESOURCE_TYPE_MAIN_FRAME); 636 content::RESOURCE_TYPE_MAIN_FRAME);
590 base::RunLoop().Run(); 637 base::RunLoop().Run();
591 638
592 EXPECT_EQ(kTestFileSize, bytes_read()); 639 EXPECT_EQ(kTestFileSize2, bytes_read());
593 ASSERT_TRUE(offline_page_tab_helper()->offline_page()); 640 ASSERT_TRUE(offline_page_tab_helper()->offline_page());
594 EXPECT_EQ(offline_id(), 641 EXPECT_EQ(offline_id2(),
595 offline_page_tab_helper()->offline_page()->offline_id); 642 offline_page_tab_helper()->offline_page()->offline_id);
596 ExpectAggregatedRequestResultHistogram( 643 ExpectAggregatedRequestResultHistogram(
597 OfflinePageRequestJob::AggregatedRequestResult:: 644 OfflinePageRequestJob::AggregatedRequestResult::
598 SHOW_OFFLINE_ON_FLAKY_NETWORK); 645 SHOW_OFFLINE_ON_FLAKY_NETWORK);
599 } 646 }
600 647
601 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnFlakyNetwork) { 648 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnFlakyNetwork) {
602 SimulateHasNetworkConnectivity(true); 649 SimulateHasNetworkConnectivity(true);
603 650
604 // When custom offline header exists and contains "reason=error", it means 651 // When custom offline header exists and contains "reason=error", it means
605 // that net error is hit in last request due to flaky network. 652 // that net error is hit in last request due to flaky network.
606 InterceptRequest( 653 InterceptRequest(
607 kTestUrl2, 654 kTestUrl2,
608 "GET", 655 "GET",
609 kLoadingOfflinePageHeader, 656 kOfflinePageHeader,
610 std::string(kLoadingOfflinePageReason) + kLoadingOfflinePageDueToNetError, 657 std::string(kOfflinePageHeaderReasonKey) + "=" +
658 kOfflinePageHeaderReasonValueDueToNetError,
611 content::RESOURCE_TYPE_MAIN_FRAME); 659 content::RESOURCE_TYPE_MAIN_FRAME);
612 base::RunLoop().Run(); 660 base::RunLoop().Run();
613 661
614 EXPECT_EQ(0, bytes_read()); 662 EXPECT_EQ(0, bytes_read());
615 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); 663 EXPECT_FALSE(offline_page_tab_helper()->offline_page());
616 ExpectAggregatedRequestResultHistogram( 664 ExpectAggregatedRequestResultHistogram(
617 OfflinePageRequestJob::AggregatedRequestResult:: 665 OfflinePageRequestJob::AggregatedRequestResult::
618 PAGE_NOT_FOUND_ON_FLAKY_NETWORK); 666 PAGE_NOT_FOUND_ON_FLAKY_NETWORK);
619 } 667 }
620 668
621 TEST_F(OfflinePageRequestJobTest, ForceLoadOfflinePageOnConnectedNetwork) { 669 TEST_F(OfflinePageRequestJobTest, ForceLoadOfflinePageOnConnectedNetwork) {
622 SimulateHasNetworkConnectivity(true); 670 SimulateHasNetworkConnectivity(true);
623 671
624 // When custom offline header exists and contains value other than 672 // When custom offline header exists and contains value other than
625 // "reason=error", it means that offline page is forced to load. 673 // "reason=error", it means that offline page is forced to load.
626 InterceptRequest( 674 InterceptRequest(
627 kTestUrl, 675 kTestUrl,
628 "GET", 676 "GET",
629 kLoadingOfflinePageHeader, 677 kOfflinePageHeader,
630 std::string(kLoadingOfflinePageReason) + "download", 678 std::string(kOfflinePageHeaderReasonKey) + "=download",
631 content::RESOURCE_TYPE_MAIN_FRAME); 679 content::RESOURCE_TYPE_MAIN_FRAME);
632 base::RunLoop().Run(); 680 base::RunLoop().Run();
633 681
634 EXPECT_EQ(kTestFileSize, bytes_read()); 682 EXPECT_EQ(kTestFileSize2, bytes_read());
635 ASSERT_TRUE(offline_page_tab_helper()->offline_page()); 683 ASSERT_TRUE(offline_page_tab_helper()->offline_page());
636 EXPECT_EQ(offline_id(), 684 EXPECT_EQ(offline_id2(),
637 offline_page_tab_helper()->offline_page()->offline_id); 685 offline_page_tab_helper()->offline_page()->offline_id);
638 ExpectAggregatedRequestResultHistogram( 686 ExpectAggregatedRequestResultHistogram(
639 OfflinePageRequestJob::AggregatedRequestResult:: 687 OfflinePageRequestJob::AggregatedRequestResult::
640 SHOW_OFFLINE_ON_CONNECTED_NETWORK); 688 SHOW_OFFLINE_ON_CONNECTED_NETWORK);
641 } 689 }
642 690
643 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnConnectedNetwork) { 691 TEST_F(OfflinePageRequestJobTest, PageNotFoundOnConnectedNetwork) {
644 SimulateHasNetworkConnectivity(true); 692 SimulateHasNetworkConnectivity(true);
645 693
646 // When custom offline header exists and contains value other than 694 // When custom offline header exists and contains value other than
647 // "reason=error", it means that offline page is forced to load. 695 // "reason=error", it means that offline page is forced to load.
648 InterceptRequest( 696 InterceptRequest(
649 kTestUrl2, 697 kTestUrl2,
650 "GET", 698 "GET",
651 kLoadingOfflinePageHeader, 699 kOfflinePageHeader,
652 std::string(kLoadingOfflinePageReason) + "download", 700 std::string(kOfflinePageHeaderReasonKey) + "=download",
653 content::RESOURCE_TYPE_MAIN_FRAME); 701 content::RESOURCE_TYPE_MAIN_FRAME);
654 base::RunLoop().Run(); 702 base::RunLoop().Run();
655 703
656 EXPECT_EQ(0, bytes_read()); 704 EXPECT_EQ(0, bytes_read());
657 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); 705 EXPECT_FALSE(offline_page_tab_helper()->offline_page());
658 ExpectAggregatedRequestResultHistogram( 706 ExpectAggregatedRequestResultHistogram(
659 OfflinePageRequestJob::AggregatedRequestResult:: 707 OfflinePageRequestJob::AggregatedRequestResult::
660 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK); 708 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK);
661 } 709 }
662 710
663 TEST_F(OfflinePageRequestJobTest, DoNotLoadOfflinePageOnConnectedNetwork) { 711 TEST_F(OfflinePageRequestJobTest, DoNotLoadOfflinePageOnConnectedNetwork) {
664 SimulateHasNetworkConnectivity(true); 712 SimulateHasNetworkConnectivity(true);
665 713
666 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME); 714 InterceptRequest(kTestUrl, "GET", "", "", content::RESOURCE_TYPE_MAIN_FRAME);
667 base::RunLoop().Run(); 715 base::RunLoop().Run();
668 716
669 EXPECT_EQ(0, bytes_read()); 717 EXPECT_EQ(0, bytes_read());
670 EXPECT_FALSE(offline_page_tab_helper()->offline_page()); 718 EXPECT_FALSE(offline_page_tab_helper()->offline_page());
671 } 719 }
672 720
721 TEST_F(OfflinePageRequestJobTest, LoadOfflinePageByOfflineID) {
722 SimulateHasNetworkConnectivity(true);
723
724 InterceptRequest(
725 kTestUrl,
726 "GET",
727 kOfflinePageHeader,
728 std::string(kOfflinePageHeaderReasonKey) + "=download " +
729 kOfflinePageHeaderIDKey + "=" + base::Int64ToString(offline_id()),
730 content::RESOURCE_TYPE_MAIN_FRAME);
731 base::RunLoop().Run();
732
733 EXPECT_EQ(kTestFileSize, bytes_read());
734 ASSERT_TRUE(offline_page_tab_helper()->offline_page());
735 EXPECT_EQ(offline_id(),
736 offline_page_tab_helper()->offline_page()->offline_id);
737 ExpectAggregatedRequestResultHistogram(
738 OfflinePageRequestJob::AggregatedRequestResult::
739 SHOW_OFFLINE_ON_CONNECTED_NETWORK);
740 }
741
742 TEST_F(OfflinePageRequestJobTest,
743 LoadOfflinePageByOfflineIDAndFallbackToOnlineURL) {
744 SimulateHasNetworkConnectivity(true);
745
746 // The offline page found with specific offline ID does not match the passed
747 // online URL. Should fall back to find the offline page based on the online
748 // URL.
749 InterceptRequest(
750 kTestUrl2,
751 "GET",
752 kOfflinePageHeader,
753 std::string(kOfflinePageHeaderReasonKey) + "=download " +
754 kOfflinePageHeaderIDKey + "=" + base::Int64ToString(offline_id()),
755 content::RESOURCE_TYPE_MAIN_FRAME);
756 base::RunLoop().Run();
757
758 EXPECT_EQ(0, bytes_read());
759 EXPECT_FALSE(offline_page_tab_helper()->offline_page());
760 ExpectAggregatedRequestResultHistogram(
761 OfflinePageRequestJob::AggregatedRequestResult::
762 PAGE_NOT_FOUND_ON_CONNECTED_NETWORK);
763 }
764
673 } // namespace offline_pages 765 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698