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

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

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