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

Side by Side Diff: chrome/browser/prerender/prerender_test_utils.cc

Issue 2452313002: Revert of [NoStatePrefetch] Kill renderer after preload scanning (Closed)
Patch Set: Created 4 years, 1 month 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
« no previous file with comments | « chrome/browser/prerender/prerender_test_utils.h ('k') | chrome/common/prerender_messages.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/prerender/prerender_test_utils.h" 5 #include "chrome/browser/prerender/prerender_test_utils.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <set> 8 #include <set>
9 #include <string> 9 #include <string>
10 #include <utility>
11 #include <vector> 10 #include <vector>
12 11
13 #include "base/command_line.h" 12 #include "base/command_line.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/browser/loader/chrome_resource_dispatcher_host_delegate.h" 14 #include "chrome/browser/loader/chrome_resource_dispatcher_host_delegate.h"
17 #include "chrome/browser/prerender/prerender_manager.h" 15 #include "chrome/browser/prerender/prerender_manager.h"
18 #include "chrome/browser/prerender/prerender_manager_factory.h" 16 #include "chrome/browser/prerender/prerender_manager_factory.h"
19 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/safe_browsing/local_database_manager.h" 18 #include "chrome/browser/safe_browsing/local_database_manager.h"
21 #include "chrome/browser/ui/browser.h" 19 #include "chrome/browser/ui/browser.h"
22 #include "chrome/browser/ui/tabs/tab_strip_model.h" 20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
23 #include "chrome/common/pref_names.h" 21 #include "chrome/common/pref_names.h"
24 #include "chrome/grit/generated_resources.h" 22 #include "chrome/grit/generated_resources.h"
25 #include "chrome/test/base/ui_test_utils.h" 23 #include "chrome/test/base/ui_test_utils.h"
26 #include "components/prefs/pref_service.h" 24 #include "components/prefs/pref_service.h"
27 #include "content/public/browser/notification_details.h" 25 #include "content/public/browser/notification_details.h"
28 #include "content/public/browser/notification_source.h" 26 #include "content/public/browser/notification_source.h"
29 #include "content/public/browser/notification_types.h" 27 #include "content/public/browser/notification_types.h"
30 #include "content/public/browser/render_view_host.h" 28 #include "content/public/browser/render_view_host.h"
31 #include "content/public/common/content_switches.h" 29 #include "content/public/common/content_switches.h"
32 #include "content/public/common/url_constants.h" 30 #include "content/public/common/url_constants.h"
33 #include "content/public/test/ppapi_test_utils.h" 31 #include "content/public/test/ppapi_test_utils.h"
34 #include "net/test/embedded_test_server/request_handler_util.h"
35 #include "net/url_request/url_request_filter.h" 32 #include "net/url_request/url_request_filter.h"
36 #include "ppapi/shared_impl/ppapi_switches.h" 33 #include "ppapi/shared_impl/ppapi_switches.h"
37 #include "testing/gtest/include/gtest/gtest.h" 34 #include "testing/gtest/include/gtest/gtest.h"
38 #include "ui/base/l10n/l10n_util.h" 35 #include "ui/base/l10n/l10n_util.h"
39 36
40 using content::BrowserThread; 37 using content::BrowserThread;
41 using content::RenderViewHost; 38 using content::RenderViewHost;
42 39
43 namespace prerender { 40 namespace prerender {
44 41
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 339
343 DestructionWaiter::DestructionMarker::~DestructionMarker() {} 340 DestructionWaiter::DestructionMarker::~DestructionMarker() {}
344 341
345 void DestructionWaiter::DestructionMarker::OnPrerenderStop( 342 void DestructionWaiter::DestructionMarker::OnPrerenderStop(
346 PrerenderContents* contents) { 343 PrerenderContents* contents) {
347 waiter_->MarkDestruction(contents->final_status()); 344 waiter_->MarkDestruction(contents->final_status());
348 } 345 }
349 346
350 TestPrerender::TestPrerender() 347 TestPrerender::TestPrerender()
351 : contents_(nullptr), 348 : contents_(nullptr),
352 final_status_(FINAL_STATUS_MAX),
353 number_of_loads_(0), 349 number_of_loads_(0),
354 expected_number_of_loads_(0), 350 expected_number_of_loads_(0),
355 started_(false), 351 started_(false),
356 stopped_(false) {} 352 stopped_(false) {}
357 353
358 TestPrerender::~TestPrerender() { 354 TestPrerender::~TestPrerender() {
359 if (contents_) 355 if (contents_)
360 contents_->RemoveObserver(this); 356 contents_->RemoveObserver(this);
361 } 357 }
362 358
363 FinalStatus TestPrerender::GetFinalStatus() const {
364 if (contents_)
365 return contents_->final_status();
366 return final_status_;
367 }
368
369 void TestPrerender::WaitForCreate() { 359 void TestPrerender::WaitForCreate() {
370 if (contents_) 360 if (contents_)
371 return; 361 return;
372 create_loop_.Run(); 362 create_loop_.Run();
373 } 363 }
374 364
375 void TestPrerender::WaitForStart() { 365 void TestPrerender::WaitForStart() {
376 if (started_) 366 if (started_)
377 return; 367 return;
378 start_loop_.Run(); 368 start_loop_.Run();
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 401
412 void TestPrerender::OnPrerenderStopLoading(PrerenderContents* contents) { 402 void TestPrerender::OnPrerenderStopLoading(PrerenderContents* contents) {
413 number_of_loads_++; 403 number_of_loads_++;
414 if (load_waiter_ && number_of_loads_ >= expected_number_of_loads_) 404 if (load_waiter_ && number_of_loads_ >= expected_number_of_loads_)
415 load_waiter_->Quit(); 405 load_waiter_->Quit();
416 } 406 }
417 407
418 void TestPrerender::OnPrerenderStop(PrerenderContents* contents) { 408 void TestPrerender::OnPrerenderStop(PrerenderContents* contents) {
419 DCHECK(contents_); 409 DCHECK(contents_);
420 contents_ = nullptr; 410 contents_ = nullptr;
421 final_status_ = contents->final_status();
422 stopped_ = true; 411 stopped_ = true;
423 stop_loop_.Quit(); 412 stop_loop_.Quit();
424 // If there is a WaitForLoads call and it has yet to see the expected number 413 // If there is a WaitForLoads call and it has yet to see the expected number
425 // of loads, stop the loop so the test fails instead of timing out. 414 // of loads, stop the loop so the test fails instead of timing out.
426 if (load_waiter_) 415 if (load_waiter_)
427 load_waiter_->Quit(); 416 load_waiter_->Quit();
428 } 417 }
429 418
430 TestPrerenderContentsFactory::TestPrerenderContentsFactory() {} 419 TestPrerenderContentsFactory::TestPrerenderContentsFactory() {}
431 420
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 } 521 }
533 522
534 TestPrerenderContents* PrerenderInProcessBrowserTest::GetPrerenderContentsFor( 523 TestPrerenderContents* PrerenderInProcessBrowserTest::GetPrerenderContentsFor(
535 const GURL& url) const { 524 const GURL& url) const {
536 PrerenderManager::PrerenderData* prerender_data = 525 PrerenderManager::PrerenderData* prerender_data =
537 GetPrerenderManager()->FindPrerenderData(url, nullptr); 526 GetPrerenderManager()->FindPrerenderData(url, nullptr);
538 return static_cast<TestPrerenderContents*>( 527 return static_cast<TestPrerenderContents*>(
539 prerender_data ? prerender_data->contents() : nullptr); 528 prerender_data ? prerender_data->contents() : nullptr);
540 } 529 }
541 530
531 std::unique_ptr<TestPrerender> PrerenderInProcessBrowserTest::PrerenderTestURL(
532 const std::string& html_file,
533 FinalStatus expected_final_status,
534 int expected_number_of_loads) {
535 GURL url = src_server()->GetURL(MakeAbsolute(html_file));
536 return PrerenderTestURL(url, expected_final_status, expected_number_of_loads);
537 }
538
539 std::unique_ptr<TestPrerender> PrerenderInProcessBrowserTest::PrerenderTestURL(
540 const GURL& url,
541 FinalStatus expected_final_status,
542 int expected_number_of_loads) {
543 std::vector<FinalStatus> expected_final_status_queue(1,
544 expected_final_status);
545 auto prerenders = PrerenderTestURLImpl(url, expected_final_status_queue,
546 expected_number_of_loads);
547 CHECK_EQ(1u, prerenders.size());
548 return std::move(prerenders[0]);
549 }
550
551 std::vector<std::unique_ptr<TestPrerender>>
552 PrerenderInProcessBrowserTest::PrerenderTestURL(
553 const std::string& html_file,
554 const std::vector<FinalStatus>& expected_final_status_queue,
555 int expected_number_of_loads) {
556 GURL url = src_server()->GetURL(MakeAbsolute(html_file));
557 return PrerenderTestURLImpl(url, expected_final_status_queue,
558 expected_number_of_loads);
559 }
560
542 net::EmbeddedTestServer* PrerenderInProcessBrowserTest::src_server() { 561 net::EmbeddedTestServer* PrerenderInProcessBrowserTest::src_server() {
543 if (https_src_server_) 562 if (https_src_server_)
544 return https_src_server_.get(); 563 return https_src_server_.get();
545 return embedded_test_server(); 564 return embedded_test_server();
546 } 565 }
547 566
548 test_utils::FakeSafeBrowsingDatabaseManager* 567 test_utils::FakeSafeBrowsingDatabaseManager*
549 PrerenderInProcessBrowserTest::GetFakeSafeBrowsingDatabaseManager() { 568 PrerenderInProcessBrowserTest::GetFakeSafeBrowsingDatabaseManager() {
550 return static_cast<test_utils::FakeSafeBrowsingDatabaseManager*>( 569 return static_cast<test_utils::FakeSafeBrowsingDatabaseManager*>(
551 safe_browsing_factory() 570 safe_browsing_factory()
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
603 622
604 base::string16 PrerenderInProcessBrowserTest::MatchTaskManagerPrerender( 623 base::string16 PrerenderInProcessBrowserTest::MatchTaskManagerPrerender(
605 const char* page_title) { 624 const char* page_title) {
606 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PRERENDER_PREFIX, 625 return l10n_util::GetStringFUTF16(IDS_TASK_MANAGER_PRERENDER_PREFIX,
607 base::ASCIIToUTF16(page_title)); 626 base::ASCIIToUTF16(page_title));
608 } 627 }
609 628
610 std::vector<std::unique_ptr<TestPrerender>> 629 std::vector<std::unique_ptr<TestPrerender>>
611 PrerenderInProcessBrowserTest::NavigateWithPrerenders( 630 PrerenderInProcessBrowserTest::NavigateWithPrerenders(
612 const GURL& loader_url, 631 const GURL& loader_url,
613 const std::vector<FinalStatus>& expected_final_status_queue) { 632 const std::vector<FinalStatus>& expected_final_status_queue,
633 int expected_number_of_loads) {
614 CHECK(!expected_final_status_queue.empty()); 634 CHECK(!expected_final_status_queue.empty());
615 std::vector<std::unique_ptr<TestPrerender>> prerenders; 635 std::vector<std::unique_ptr<TestPrerender>> prerenders;
616 for (size_t i = 0; i < expected_final_status_queue.size(); i++) { 636 for (size_t i = 0; i < expected_final_status_queue.size(); i++) {
617 prerenders.push_back(prerender_contents_factory() 637 prerenders.push_back(prerender_contents_factory()
618 ->ExpectPrerenderContents(expected_final_status_queue[i])); 638 ->ExpectPrerenderContents(expected_final_status_queue[i]));
619 } 639 }
620 640
621 // Navigate to the loader URL and then wait for the first prerender to be 641 // Navigate to the loader URL and then wait for the first prerender to be
622 // created. 642 // created.
623 ui_test_utils::NavigateToURL(current_browser(), loader_url); 643 ui_test_utils::NavigateToURL(current_browser(), loader_url);
624 prerenders[0]->WaitForCreate(); 644 prerenders.at(0)->WaitForCreate();
645 prerenders.at(0)->WaitForLoads(expected_number_of_loads);
625 646
626 return prerenders; 647 return prerenders;
627 } 648 }
628 649
629 GURL PrerenderInProcessBrowserTest::ServeLoaderURL(
630 const std::string& loader_path,
631 const std::string& replacement_variable,
632 const GURL& url_to_prerender,
633 const std::string& loader_query) {
634 base::StringPairs replacement_text;
635 replacement_text.push_back(
636 make_pair(replacement_variable, url_to_prerender.spec()));
637 std::string replacement_path;
638 net::test_server::GetFilePathWithReplacements(loader_path, replacement_text,
639 &replacement_path);
640 return src_server()->GetURL(replacement_path + loader_query);
641 }
642
643 void CreateCountingInterceptorOnIO( 650 void CreateCountingInterceptorOnIO(
644 const GURL& url, 651 const GURL& url,
645 const base::FilePath& file, 652 const base::FilePath& file,
646 const base::WeakPtr<RequestCounter>& counter) { 653 const base::WeakPtr<RequestCounter>& counter) {
647 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 654 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
648 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( 655 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
649 url, base::MakeUnique<CountingInterceptor>(file, counter)); 656 url, base::MakeUnique<CountingInterceptor>(file, counter));
650 } 657 }
651 658
652 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file) { 659 void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file) {
653 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); 660 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
654 net::URLRequestFilter::GetInstance()->AddUrlInterceptor( 661 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
655 url, net::URLRequestMockHTTPJob::CreateInterceptorForSingleFile( 662 url, net::URLRequestMockHTTPJob::CreateInterceptorForSingleFile(
656 file, content::BrowserThread::GetBlockingPool())); 663 file, content::BrowserThread::GetBlockingPool()));
657 } 664 }
658 665
659 } // namespace test_utils 666 } // namespace test_utils
660 667
661 } // namespace prerender 668 } // namespace prerender
OLDNEW
« no previous file with comments | « chrome/browser/prerender/prerender_test_utils.h ('k') | chrome/common/prerender_messages.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698