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

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

Issue 138583003: Add back the code to PrerenderManager::PendingSwap to handle RenderViewCreated being called for a... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix clang Created 6 years, 11 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 | Annotate | Revision Log
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 <deque> 5 #include <deque>
6 #include <vector> 6 #include <vector>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 687 matching lines...) Expand 10 before | Expand all | Expand 10 after
698 // |file| afterwards. 698 // |file| afterwards.
699 void CreateHangingFirstRequestProtocolHandlerOnIO(const GURL& url, 699 void CreateHangingFirstRequestProtocolHandlerOnIO(const GURL& url,
700 const base::FilePath& file) { 700 const base::FilePath& file) {
701 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 701 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
702 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> never_respond_handler( 702 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> never_respond_handler(
703 new HangingFirstRequestProtocolHandler(file)); 703 new HangingFirstRequestProtocolHandler(file));
704 net::URLRequestFilter::GetInstance()->AddUrlProtocolHandler( 704 net::URLRequestFilter::GetInstance()->AddUrlProtocolHandler(
705 url, never_respond_handler.Pass()); 705 url, never_respond_handler.Pass());
706 } 706 }
707 707
708 // Wrapper over URLRequestMockHTTPJob that exposes extra callbacks.
709 class MockHTTPJob : public content::URLRequestMockHTTPJob {
710 public:
711 MockHTTPJob(net::URLRequest* request,
712 net::NetworkDelegate* delegate,
713 const base::FilePath& file)
714 : content::URLRequestMockHTTPJob(request, delegate, file) {
715 }
716
717 void set_start_callback(const base::Closure& start_callback) {
718 start_callback_ = start_callback;
719 }
720
721 virtual void Start() OVERRIDE {
722 if (!start_callback_.is_null())
723 start_callback_.Run();
724 content::URLRequestMockHTTPJob::Start();
725 }
726
727 private:
728 virtual ~MockHTTPJob() {}
729
730 base::Closure start_callback_;
731 };
732
733 // Dummy counter class to live on the UI thread for counting requests.
734 class RequestCounter : public base::SupportsWeakPtr<RequestCounter> {
735 public:
736 RequestCounter() : count_(0) {}
737 int count() const { return count_; }
738 void RequestStarted() { count_++; }
739 private:
740 int count_;
741 };
742
743 // Protocol handler which counts the number of requests that start.
744 class CountingProtocolHandler
745 : public net::URLRequestJobFactory::ProtocolHandler {
746 public:
747 CountingProtocolHandler(const base::FilePath& file,
748 const base::WeakPtr<RequestCounter>& counter)
749 : file_(file),
750 counter_(counter),
751 weak_factory_(this) {
752 }
753 virtual ~CountingProtocolHandler() {}
754
755 virtual net::URLRequestJob* MaybeCreateJob(
756 net::URLRequest* request,
757 net::NetworkDelegate* network_delegate) const OVERRIDE {
758 MockHTTPJob* job = new MockHTTPJob(request, network_delegate, file_);
759 job->set_start_callback(base::Bind(&CountingProtocolHandler::RequestStarted,
760 weak_factory_.GetWeakPtr()));
761 return job;
762 }
763
764 void RequestStarted() {
765 BrowserThread::PostTask(
766 BrowserThread::UI, FROM_HERE,
767 base::Bind(&RequestCounter::RequestStarted, counter_));
768 }
769
770 private:
771 base::FilePath file_;
772 base::WeakPtr<RequestCounter> counter_;
773 mutable base::WeakPtrFactory<CountingProtocolHandler> weak_factory_;
774 };
775
776 // Makes |url| respond to requests with the contents of |file|, counting the
777 // number that start in |counter|.
778 void CreateCountingProtocolHandlerOnIO(
779 const GURL& url,
780 const base::FilePath& file,
781 const base::WeakPtr<RequestCounter>& counter) {
782 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
783 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> protocol_handler(
784 new CountingProtocolHandler(file, counter));
785 net::URLRequestFilter::GetInstance()->AddUrlProtocolHandler(
786 url, protocol_handler.Pass());
787 }
788
708 // Makes |url| respond to requests with the contents of |file|. 789 // Makes |url| respond to requests with the contents of |file|.
709 void CreateMockProtocolHandlerOnIO(const GURL& url, 790 void CreateMockProtocolHandlerOnIO(const GURL& url,
710 const base::FilePath& file) { 791 const base::FilePath& file) {
711 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 792 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
712 net::URLRequestFilter::GetInstance()->AddUrlProtocolHandler( 793 net::URLRequestFilter::GetInstance()->AddUrlProtocolHandler(
713 url, content::URLRequestMockHTTPJob::CreateProtocolHandlerForSingleFile( 794 url, content::URLRequestMockHTTPJob::CreateProtocolHandlerForSingleFile(
714 file)); 795 file));
715 } 796 }
716 797
717 // A ContentBrowserClient that cancels all prerenderers on OpenURL. 798 // A ContentBrowserClient that cancels all prerenderers on OpenURL.
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
780 NOTREACHED(); 861 NOTREACHED();
781 } 862 }
782 virtual void LaunchUrlWithoutSecurityCheck(const GURL& url) OVERRIDE { 863 virtual void LaunchUrlWithoutSecurityCheck(const GURL& url) OVERRIDE {
783 NOTREACHED(); 864 NOTREACHED();
784 } 865 }
785 virtual void FinishedProcessingCheck() OVERRIDE { 866 virtual void FinishedProcessingCheck() OVERRIDE {
786 NOTREACHED(); 867 NOTREACHED();
787 } 868 }
788 }; 869 };
789 870
871 base::FilePath GetTestPath(const std::string& file_name) {
872 return ui_test_utils::GetTestFilePath(
873 base::FilePath(FILE_PATH_LITERAL("prerender")),
874 base::FilePath().AppendASCII(file_name));
875 }
876
790 } // namespace 877 } // namespace
791 878
792 // Many of these tests are flaky. See http://crbug.com/249179 879 // Many of these tests are flaky. See http://crbug.com/249179
793 class PrerenderBrowserTest : virtual public InProcessBrowserTest { 880 class PrerenderBrowserTest : virtual public InProcessBrowserTest {
794 public: 881 public:
795 PrerenderBrowserTest() 882 PrerenderBrowserTest()
796 : autostart_test_server_(true), 883 : autostart_test_server_(true),
797 prerender_contents_factory_(NULL), 884 prerender_contents_factory_(NULL),
798 #if defined(FULL_SAFE_BROWSING) 885 #if defined(FULL_SAFE_BROWSING)
799 safe_browsing_factory_(new TestSafeBrowsingServiceFactory()), 886 safe_browsing_factory_(new TestSafeBrowsingServiceFactory()),
(...skipping 849 matching lines...) Expand 10 before | Expand all | Expand 10 after
1649 FINAL_STATUS_USED, 1736 FINAL_STATUS_USED,
1650 1); 1737 1);
1651 NavigateToDestURL(); 1738 NavigateToDestURL();
1652 } 1739 }
1653 1740
1654 // Checks that the prerendering of a page is canceled correctly if we try to 1741 // Checks that the prerendering of a page is canceled correctly if we try to
1655 // swap it in before it commits. 1742 // swap it in before it commits.
1656 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNoCommitNoSwap) { 1743 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNoCommitNoSwap) {
1657 // Navigate to a page that triggers a prerender for a URL that never commits. 1744 // Navigate to a page that triggers a prerender for a URL that never commits.
1658 const GURL kNoCommitUrl("http://never-respond.example.com"); 1745 const GURL kNoCommitUrl("http://never-respond.example.com");
1659 base::FilePath file(FILE_PATH_LITERAL( 1746 base::FilePath file(GetTestPath("prerender_page.html"));
1660 "chrome/test/data/prerender/prerender_page.html"));
1661 BrowserThread::PostTask( 1747 BrowserThread::PostTask(
1662 BrowserThread::IO, FROM_HERE, 1748 BrowserThread::IO, FROM_HERE,
1663 base::Bind(&CreateHangingFirstRequestProtocolHandlerOnIO, 1749 base::Bind(&CreateHangingFirstRequestProtocolHandlerOnIO,
1664 kNoCommitUrl, file)); 1750 kNoCommitUrl, file));
1665 DisableJavascriptCalls(); 1751 DisableJavascriptCalls();
1666 PrerenderTestURL(kNoCommitUrl, 1752 PrerenderTestURL(kNoCommitUrl,
1667 FINAL_STATUS_NAVIGATION_UNCOMMITTED, 1753 FINAL_STATUS_NAVIGATION_UNCOMMITTED,
1668 0); 1754 0);
1669 1755
1670 // Navigate to the URL, but assume the contents won't be swapped in. 1756 // Navigate to the URL, but assume the contents won't be swapped in.
1671 NavigateToDestURLWithDisposition(CURRENT_TAB, false); 1757 NavigateToDestURLWithDisposition(CURRENT_TAB, false);
1672 } 1758 }
1673 1759
1674 // Checks that client redirects don't add alias URLs until after they commit. 1760 // Checks that client redirects don't add alias URLs until after they commit.
1675 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNoCommitNoSwap2) { 1761 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNoCommitNoSwap2) {
1676 // Navigate to a page that then navigates to a URL that never commits. 1762 // Navigate to a page that then navigates to a URL that never commits.
1677 const GURL kNoCommitUrl("http://never-respond.example.com"); 1763 const GURL kNoCommitUrl("http://never-respond.example.com");
1678 base::FilePath file(FILE_PATH_LITERAL( 1764 base::FilePath file(GetTestPath("prerender_page.html"));
1679 "chrome/test/data/prerender/prerender_page.html"));
1680 BrowserThread::PostTask( 1765 BrowserThread::PostTask(
1681 BrowserThread::IO, FROM_HERE, 1766 BrowserThread::IO, FROM_HERE,
1682 base::Bind(&CreateHangingFirstRequestProtocolHandlerOnIO, 1767 base::Bind(&CreateHangingFirstRequestProtocolHandlerOnIO,
1683 kNoCommitUrl, file)); 1768 kNoCommitUrl, file));
1684 DisableJavascriptCalls(); 1769 DisableJavascriptCalls();
1685 PrerenderTestURL(CreateClientRedirect(kNoCommitUrl.spec()), 1770 PrerenderTestURL(CreateClientRedirect(kNoCommitUrl.spec()),
1686 FINAL_STATUS_APP_TERMINATING, 1); 1771 FINAL_STATUS_APP_TERMINATING, 1);
1687 1772
1688 // Navigating to the second URL should not swap. 1773 // Navigating to the second URL should not swap.
1689 NavigateToURLWithDisposition(kNoCommitUrl, CURRENT_TAB, false); 1774 NavigateToURLWithDisposition(kNoCommitUrl, CURRENT_TAB, false);
(...skipping 1650 matching lines...) Expand 10 before | Expand all | Expand 10 after
3340 // a client redirect. 3425 // a client redirect.
3341 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, 3426 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest,
3342 PrerenderCrossProcessClientRedirect) { 3427 PrerenderCrossProcessClientRedirect) {
3343 // Cross-process navigation logic for renderer-initiated navigations 3428 // Cross-process navigation logic for renderer-initiated navigations
3344 // is partially controlled by the renderer, namely 3429 // is partially controlled by the renderer, namely
3345 // ChromeContentRendererClient. This test instead relies on the Web 3430 // ChromeContentRendererClient. This test instead relies on the Web
3346 // Store triggering such navigations. 3431 // Store triggering such navigations.
3347 std::string webstore_url = extension_urls::GetWebstoreLaunchURL(); 3432 std::string webstore_url = extension_urls::GetWebstoreLaunchURL();
3348 3433
3349 // Mock out requests to the Web Store. 3434 // Mock out requests to the Web Store.
3350 base::FilePath file(FILE_PATH_LITERAL( 3435 base::FilePath file(GetTestPath("prerender_page.html"));
3351 "chrome/test/data/prerender/prerender_page.html"));
3352 BrowserThread::PostTask( 3436 BrowserThread::PostTask(
3353 BrowserThread::IO, FROM_HERE, 3437 BrowserThread::IO, FROM_HERE,
3354 base::Bind(&CreateMockProtocolHandlerOnIO, 3438 base::Bind(&CreateMockProtocolHandlerOnIO,
3355 GURL(webstore_url), file)); 3439 GURL(webstore_url), file));
3356 3440
3357 PrerenderTestURL(CreateClientRedirect(webstore_url), 3441 PrerenderTestURL(CreateClientRedirect(webstore_url),
3358 FINAL_STATUS_OPEN_URL, 1); 3442 FINAL_STATUS_OPEN_URL, 1);
3359 } 3443 }
3360 3444
3361 // Checks that canceling a MatchComplete dummy doesn't result in two 3445 // Checks that canceling a MatchComplete dummy doesn't result in two
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
3457 // prerendered page tries to make a second navigation entry. 3541 // prerendered page tries to make a second navigation entry.
3458 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNewNavigationEntry) { 3542 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderNewNavigationEntry) {
3459 PrerenderTestURL("files/prerender/prerender_new_entry.html", 3543 PrerenderTestURL("files/prerender/prerender_new_entry.html",
3460 FINAL_STATUS_NEW_NAVIGATION_ENTRY, 3544 FINAL_STATUS_NEW_NAVIGATION_ENTRY,
3461 1); 3545 1);
3462 } 3546 }
3463 3547
3464 // Attempt a swap-in in a new tab, verifying that session storage namespace 3548 // Attempt a swap-in in a new tab, verifying that session storage namespace
3465 // merging works. 3549 // merging works.
3466 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPageNewTab) { 3550 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPageNewTab) {
3467 PrerenderTestURL("files/prerender/prerender_session_storage.html", 3551 // Mock out some URLs and count the number of requests to one of them. Both
3468 FINAL_STATUS_USED, 1); 3552 // prerender_session_storage.html and init_session_storage.html need to be
3553 // mocked so they are same-origin.
3554 const GURL kInitURL("http://prerender.test/init_session_storage.html");
3555 base::FilePath init_file = GetTestPath("init_session_storage.html");
3556 BrowserThread::PostTask(
3557 BrowserThread::IO, FROM_HERE,
3558 base::Bind(&CreateMockProtocolHandlerOnIO, kInitURL, init_file));
3559
3560 const GURL kTestURL("http://prerender.test/prerender_session_storage.html");
3561 base::FilePath test_file = GetTestPath("prerender_session_storage.html");
3562 RequestCounter counter;
3563 BrowserThread::PostTask(
3564 BrowserThread::IO, FROM_HERE,
3565 base::Bind(&CreateCountingProtocolHandlerOnIO,
3566 kTestURL, test_file, counter.AsWeakPtr()));
3567
3568 PrerenderTestURL(kTestURL, FINAL_STATUS_USED, 1);
3469 3569
3470 // Open a new tab to navigate in. 3570 // Open a new tab to navigate in.
3471 ui_test_utils::NavigateToURLWithDisposition( 3571 ui_test_utils::NavigateToURLWithDisposition(
3472 current_browser(), 3572 current_browser(), kInitURL, NEW_FOREGROUND_TAB,
3473 test_server()->GetURL("files/prerender/init_session_storage.html"),
3474 NEW_FOREGROUND_TAB,
3475 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); 3573 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
3476 3574
3477 // Now navigate in the new tab. Set expect_swap_to_succeed to false because 3575 // Now navigate in the new tab. Set expect_swap_to_succeed to false because
3478 // the swap does not occur synchronously. 3576 // the swap does not occur synchronously.
3479 // 3577 //
3480 // TODO(davidben): When all swaps become asynchronous, remove the OpenURL 3578 // TODO(davidben): When all swaps become asynchronous, remove the OpenURL
3481 // return value assertion and let this go through the usual successful-swap 3579 // return value assertion and let this go through the usual successful-swap
3482 // codepath. 3580 // codepath.
3483 NavigateToDestURLWithDisposition(CURRENT_TAB, false); 3581 NavigateToDestURLWithDisposition(CURRENT_TAB, false);
3484 3582
3485 // Verify DidDisplayPass manually since the previous call skipped it. 3583 // Verify DidDisplayPass manually since the previous call skipped it.
3486 EXPECT_TRUE(DidDisplayPass( 3584 EXPECT_TRUE(DidDisplayPass(
3487 current_browser()->tab_strip_model()->GetActiveWebContents())); 3585 current_browser()->tab_strip_model()->GetActiveWebContents()));
3586
3587 // Only one request to the test URL started.
3588 EXPECT_EQ(1, counter.count());
3589 }
3590
3591 // Attempt a swap-in in a new tab, verifying that session storage namespace
3592 // merging works. Unlike the above test, the swap is for a navigation that would
3593 // normally be cross-process.
3594 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderPageNewTabCrossProcess) {
3595 base::FilePath test_data_dir;
3596 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
3597
3598 // Mock out some URLs and count the number of requests to one of them. Both
3599 // prerender_session_storage.html and init_session_storage.html need to be
3600 // mocked so they are same-origin.
3601 const GURL kInitURL("http://prerender.test/init_session_storage.html");
3602 base::FilePath init_file = GetTestPath("init_session_storage.html");
3603 BrowserThread::PostTask(
3604 BrowserThread::IO, FROM_HERE,
3605 base::Bind(&CreateMockProtocolHandlerOnIO, kInitURL, init_file));
3606
3607 const GURL kTestURL("http://prerender.test/prerender_session_storage.html");
3608 base::FilePath test_file = GetTestPath("prerender_session_storage.html");
3609 RequestCounter counter;
3610 BrowserThread::PostTask(
3611 BrowserThread::IO, FROM_HERE,
3612 base::Bind(&CreateCountingProtocolHandlerOnIO,
3613 kTestURL, test_file, counter.AsWeakPtr()));
3614
3615 PrerenderTestURL(kTestURL, FINAL_STATUS_USED, 1);
3616
3617 // Open a new tab to navigate in.
3618 ui_test_utils::NavigateToURLWithDisposition(
3619 current_browser(), kInitURL, NEW_FOREGROUND_TAB,
3620 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
3621
3622 // Navigate to about:blank so the next navigation is cross-process.
3623 ui_test_utils::NavigateToURL(current_browser(),
3624 GURL(content::kAboutBlankURL));
3625
3626 // Now navigate in the new tab. Set expect_swap_to_succeed to false because
3627 // the swap does not occur synchronously.
3628 //
3629 // TODO(davidben): When all swaps become asynchronous, remove the OpenURL
3630 // return value assertion and let this go through the usual successful-swap
3631 // codepath.
3632 NavigateToDestURLWithDisposition(CURRENT_TAB, false);
3633
3634 // Verify DidDisplayPass manually since the previous call skipped it.
3635 EXPECT_TRUE(DidDisplayPass(
3636 current_browser()->tab_strip_model()->GetActiveWebContents()));
3637
3638 // Only one request to the test URL started.
3639 EXPECT_EQ(1, counter.count());
3488 } 3640 }
3489 3641
3490 // Verify that session storage conflicts don't merge. 3642 // Verify that session storage conflicts don't merge.
3491 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSessionStorageConflict) { 3643 IN_PROC_BROWSER_TEST_F(PrerenderBrowserTest, PrerenderSessionStorageConflict) {
3492 PrerenderTestURL("files/prerender/prerender_session_storage_conflict.html", 3644 PrerenderTestURL("files/prerender/prerender_session_storage_conflict.html",
3493 FINAL_STATUS_APP_TERMINATING, 1); 3645 FINAL_STATUS_APP_TERMINATING, 1);
3494 3646
3495 // Open a new tab to navigate in. 3647 // Open a new tab to navigate in.
3496 ui_test_utils::NavigateToURLWithDisposition( 3648 ui_test_utils::NavigateToURLWithDisposition(
3497 current_browser(), 3649 current_browser(),
(...skipping 23 matching lines...) Expand all
3521 const NavigationController& controller = web_contents->GetController(); 3673 const NavigationController& controller = web_contents->GetController();
3522 // First entry is about:blank, second is prerender_page.html. 3674 // First entry is about:blank, second is prerender_page.html.
3523 EXPECT_TRUE(controller.GetPendingEntry() == NULL); 3675 EXPECT_TRUE(controller.GetPendingEntry() == NULL);
3524 EXPECT_EQ(2, controller.GetEntryCount()); 3676 EXPECT_EQ(2, controller.GetEntryCount());
3525 EXPECT_EQ(GURL(content::kAboutBlankURL), 3677 EXPECT_EQ(GURL(content::kAboutBlankURL),
3526 controller.GetEntryAtIndex(0)->GetURL()); 3678 controller.GetEntryAtIndex(0)->GetURL());
3527 EXPECT_EQ(dest_url(), controller.GetEntryAtIndex(1)->GetURL()); 3679 EXPECT_EQ(dest_url(), controller.GetEntryAtIndex(1)->GetURL());
3528 } 3680 }
3529 3681
3530 } // namespace prerender 3682 } // namespace prerender
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/prerender/prerender_manager.h » ('j') | chrome/browser/prerender/prerender_manager.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698