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

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

Powered by Google App Engine
This is Rietveld 408576698