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

Unified Diff: content/browser/frame_host/navigator_impl_unittest.cc

Issue 701953006: PlzNavigate: Speculatively spawns a renderer process for navigations. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase and one more round of CR work. Created 6 years 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/frame_host/navigator_impl_unittest.cc
diff --git a/content/browser/frame_host/navigator_impl_unittest.cc b/content/browser/frame_host/navigator_impl_unittest.cc
index 2258b258559dae276adc48b300c29e3e03836a22..39fad350f0945867a9686fffd954b7614e676f3a 100644
--- a/content/browser/frame_host/navigator_impl_unittest.cc
+++ b/content/browser/frame_host/navigator_impl_unittest.cc
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/command_line.h"
#include "base/macros.h"
#include "base/time/time.h"
#include "content/browser/frame_host/navigation_controller_impl.h"
@@ -15,6 +16,7 @@
#include "content/browser/streams/stream.h"
#include "content/common/navigation_params.h"
#include "content/public/browser/stream_handle.h"
+#include "content/public/common/content_switches.h"
#include "content/public/common/url_constants.h"
#include "content/public/common/url_utils.h"
#include "content/test/browser_side_navigation_test_utils.h"
@@ -73,8 +75,36 @@ class NavigatorTestWithBrowserSideNavigation
return static_cast<NavigatorImpl*>(frame_tree_node->navigator())
->GetNavigationRequestForNodeForTesting(frame_tree_node);
}
+
+ TestRenderFrameHost* GetSpeculativeRenderFrameHost(
+ RenderFrameHostManager* rfhm) {
+ return static_cast<TestRenderFrameHost*>(
+ rfhm->speculative_render_frame_host_.get());
+ }
+
+ void LoadURLAndBeginNavigationOnMainFrame(const GURL& url) {
+ contents()->GetController().LoadURL(
+ url, Referrer(), ui::PAGE_TRANSITION_LINK, std::string());
+ main_test_rfh()->SendBeginNavigationWithURL(url);
+ }
};
+// PlzNavigate: Test final state after a complete navigation (to avoid repeating
+// these checks).
+TEST_F(NavigatorTestWithBrowserSideNavigation, NavigationFinishedState) {
+ const GURL kUrl("http://chromium.org/");
+ contents()->NavigateAndCommit(kUrl);
+ ASSERT_TRUE(main_test_rfh());
+ EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, main_test_rfh()->rfh_state());
+ EXPECT_EQ(SiteInstanceImpl::GetSiteForURL(browser_context(), kUrl),
+ main_test_rfh()->GetSiteInstance()->GetSiteURL());
+ // After a navigation is finished no speculative RenderFrameHost should
+ // exist.
+ EXPECT_FALSE(contents()->GetSpeculativeMainFrame());
nasko 2014/12/20 00:09:09 I'd rather not add more code to contents(), as peo
carlosk 2014/12/29 16:40:16 I complied with what nasko@ and clamy@ asked for b
+ // With PlzNavigate enabled a pending RenderFrameHost should never exist.
+ EXPECT_FALSE(contents()->GetPendingMainFrame());
+}
+
// PlzNavigate: Test that a proper NavigationRequest is created by
// BeginNavigation.
// Note that all PlzNavigate methods on the browser side require the use of the
@@ -85,9 +115,10 @@ TEST_F(NavigatorTestWithBrowserSideNavigation, BeginNavigation) {
const GURL kUrl3("http://www.gmail.com/");
contents()->NavigateAndCommit(kUrl1);
+ FrameTreeNode* root = contents()->GetFrameTree()->root();
+ RenderFrameHostManager* root_rfhm = root->render_manager();
// Add a subframe.
- FrameTreeNode* root = contents()->GetFrameTree()->root();
TestRenderFrameHost* subframe_rfh = static_cast<TestRenderFrameHost*>(
contents()->GetFrameTree()->AddFrame(
root, root->current_frame_host()->GetProcess()->GetID(), 14,
@@ -95,6 +126,9 @@ TEST_F(NavigatorTestWithBrowserSideNavigation, BeginNavigation) {
EXPECT_TRUE(subframe_rfh);
FrameTreeNode* subframe_node = subframe_rfh->frame_tree_node();
+ RenderFrameHostManager* subframe_rfhm = subframe_node->render_manager();
+ EXPECT_NE(root_rfhm, subframe_rfhm);
+
SendRequestNavigation(subframe_rfh->frame_tree_node(), kUrl2);
// There is no previous renderer in the subframe, so BeginNavigation is
// handled already.
@@ -109,10 +143,20 @@ TEST_F(NavigatorTestWithBrowserSideNavigation, BeginNavigation) {
EXPECT_EQ(kUrl1, subframe_loader->request_info()->first_party_for_cookies);
EXPECT_FALSE(subframe_loader->request_info()->is_main_frame);
EXPECT_TRUE(subframe_loader->request_info()->parent_is_main_frame);
+ EXPECT_FALSE(GetSpeculativeRenderFrameHost(root_rfhm));
+
+ // Subframe navigations should not create a speculative RenderFrameHost.
+ // (unless site-per-process is enabled).
+ if (base::CommandLine::ForCurrentProcess()->HasSwitch(
+ switches::kSitePerProcess)) {
+ EXPECT_TRUE(GetSpeculativeRenderFrameHost(subframe_rfhm));
+ } else {
+ EXPECT_FALSE(GetSpeculativeRenderFrameHost(subframe_rfhm));
+ }
SendRequestNavigation(root, kUrl3);
// Simulate a BeginNavigation IPC on the main frame.
- contents()->GetMainFrame()->SendBeginNavigationWithURL(kUrl3);
+ main_test_rfh()->SendBeginNavigationWithURL(kUrl3);
NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(root);
TestNavigationURLLoader* main_loader =
GetLoaderForNavigationRequest(main_request);
@@ -122,6 +166,16 @@ TEST_F(NavigatorTestWithBrowserSideNavigation, BeginNavigation) {
EXPECT_EQ(kUrl3, main_loader->request_info()->first_party_for_cookies);
EXPECT_TRUE(main_loader->request_info()->is_main_frame);
EXPECT_FALSE(main_loader->request_info()->parent_is_main_frame);
+
+ // Main frame navigations to different sites should use a speculative
+ // RenderFrameHost.
+ EXPECT_TRUE(GetSpeculativeRenderFrameHost(root_rfhm));
+ if (base::CommandLine::ForCurrentProcess()->HasSwitch(
+ switches::kSitePerProcess)) {
+ EXPECT_TRUE(GetSpeculativeRenderFrameHost(subframe_rfhm));
+ } else {
+ EXPECT_FALSE(GetSpeculativeRenderFrameHost(subframe_rfhm));
+ }
}
// PlzNavigate: Test that RequestNavigation creates a NavigationRequest and that
@@ -130,12 +184,17 @@ TEST_F(NavigatorTestWithBrowserSideNavigation, NoLiveRenderer) {
const GURL kUrl("http://www.google.com/");
EXPECT_FALSE(main_test_rfh()->render_view_host()->IsRenderViewLive());
- FrameTreeNode* node = main_test_rfh()->frame_tree_node();
+ RenderFrameHostImpl* rfh = main_test_rfh();
+ FrameTreeNode* node = rfh->frame_tree_node();
SendRequestNavigation(node, kUrl);
- NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node);
+
// A NavigationRequest should have been generated.
+ NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node);
EXPECT_TRUE(main_request != NULL);
- RenderFrameHostImpl* rfh = main_test_rfh();
+
+ // As we're re-using the current RenderFrameHost, no speculative one should be
+ // created.
+ EXPECT_FALSE(contents()->GetSpeculativeMainFrame());
// Now return the response without any redirects. This will cause the
// navigation to commit at the same URL.
@@ -151,24 +210,24 @@ TEST_F(NavigatorTestWithBrowserSideNavigation, NoLiveRenderer) {
EXPECT_TRUE(main_test_rfh()->render_view_host()->IsRenderViewLive());
}
-// PlzNavigate: Test that commiting an HTTP 204 or HTTP 205 response cancels the
-// navigation.
+// PlzNavigate: Test that committing an HTTP 204 or HTTP 205 response cancels
+// the navigation.
TEST_F(NavigatorTestWithBrowserSideNavigation, NoContent) {
const GURL kUrl1("http://www.chromium.org/");
const GURL kUrl2("http://www.google.com/");
// Load a URL.
contents()->NavigateAndCommit(kUrl1);
- RenderFrameHostImpl* rfh = main_test_rfh();
- EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, rfh->rfh_state());
FrameTreeNode* node = main_test_rfh()->frame_tree_node();
// Navigate to a different site.
- SendRequestNavigation(node, kUrl2);
- main_test_rfh()->SendBeginNavigationWithURL(kUrl2);
+ LoadURLAndBeginNavigationOnMainFrame(kUrl2);
clamy 2014/12/19 13:35:57 Considering that the unit tests are for NavigatorI
nasko 2014/12/20 00:09:09 I'd second that, mainly since the abstraction is j
carlosk 2014/12/29 16:40:16 They were 2 repetitive calls that apart from the U
NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node);
ASSERT_TRUE(main_request);
+ // Navigations to a different site do create a speculative RenderFrameHost.
+ EXPECT_TRUE(contents()->GetSpeculativeMainFrame());
+
// Commit an HTTP 204 response.
scoped_refptr<ResourceResponse> response(new ResourceResponse);
const char kNoContentHeaders[] = "HTTP/1.1 204 No Content\0\0";
@@ -177,17 +236,19 @@ TEST_F(NavigatorTestWithBrowserSideNavigation, NoContent) {
GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted(
response, MakeEmptyStream());
- // There should be no pending RenderFrameHost; the navigation was aborted.
+ // There should be no pending nor speculative RenderFrameHost; the navigation
+ // was aborted.
EXPECT_FALSE(GetNavigationRequestForFrameTreeNode(node));
- EXPECT_FALSE(node->render_manager()->pending_frame_host());
+ EXPECT_FALSE(contents()->GetPendingMainFrame());
+ EXPECT_FALSE(contents()->GetSpeculativeMainFrame());
// Now, repeat the test with 205 Reset Content.
// Navigate to a different site again.
- SendRequestNavigation(node, kUrl2);
- main_test_rfh()->SendBeginNavigationWithURL(kUrl2);
+ LoadURLAndBeginNavigationOnMainFrame(kUrl2);
main_request = GetNavigationRequestForFrameTreeNode(node);
ASSERT_TRUE(main_request);
+ EXPECT_TRUE(contents()->GetSpeculativeMainFrame());
// Commit an HTTP 205 response.
response = new ResourceResponse;
@@ -197,9 +258,11 @@ TEST_F(NavigatorTestWithBrowserSideNavigation, NoContent) {
GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted(
response, MakeEmptyStream());
- // There should be no pending RenderFrameHost; the navigation was aborted.
+ // There should be no pending nor speculative RenderFrameHost; the navigation
+ // was aborted.
EXPECT_FALSE(GetNavigationRequestForFrameTreeNode(node));
- EXPECT_FALSE(node->render_manager()->pending_frame_host());
+ EXPECT_FALSE(contents()->GetPendingMainFrame());
+ EXPECT_FALSE(contents()->GetSpeculativeMainFrame());
}
// PlzNavigate: Test that a new RenderFrameHost is created when doing a cross
@@ -210,41 +273,43 @@ TEST_F(NavigatorTestWithBrowserSideNavigation, CrossSiteNavigation) {
contents()->NavigateAndCommit(kUrl1);
RenderFrameHostImpl* rfh = main_test_rfh();
- EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, rfh->rfh_state());
FrameTreeNode* node = main_test_rfh()->frame_tree_node();
// Navigate to a different site.
- SendRequestNavigation(node, kUrl2);
- main_test_rfh()->SendBeginNavigationWithURL(kUrl2);
+ LoadURLAndBeginNavigationOnMainFrame(kUrl2);
NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node);
ASSERT_TRUE(main_request);
+ EXPECT_TRUE(contents()->GetSpeculativeMainFrame());
scoped_refptr<ResourceResponse> response(new ResourceResponse);
GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted(
response, MakeEmptyStream());
- RenderFrameHostImpl* pending_rfh =
- node->render_manager()->pending_frame_host();
- ASSERT_TRUE(pending_rfh);
- EXPECT_NE(pending_rfh, rfh);
- EXPECT_TRUE(pending_rfh->IsRenderFrameLive());
- EXPECT_TRUE(pending_rfh->render_view_host()->IsRenderViewLive());
+ EXPECT_TRUE(contents()->GetSpeculativeMainFrame());
+
+ contents()->CommitPendingNavigation();
+ RenderFrameHostImpl* final_rfh = main_test_rfh();
+ ASSERT_TRUE(final_rfh);
+ EXPECT_NE(rfh, final_rfh);
+ EXPECT_TRUE(final_rfh->IsRenderFrameLive());
+ EXPECT_TRUE(final_rfh->render_view_host()->IsRenderViewLive());
+ EXPECT_FALSE(contents()->GetSpeculativeMainFrame());
}
-// PlzNavigate: Test that redirects are followed.
+// PlzNavigate: Test that redirects are followed and the speculative renderer
+// logic behaves as expected.
TEST_F(NavigatorTestWithBrowserSideNavigation, RedirectCrossSite) {
const GURL kUrl1("http://www.chromium.org/");
const GURL kUrl2("http://www.google.com/");
contents()->NavigateAndCommit(kUrl1);
RenderFrameHostImpl* rfh = main_test_rfh();
- EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, rfh->rfh_state());
FrameTreeNode* node = main_test_rfh()->frame_tree_node();
// Navigate to a URL on the same site.
- SendRequestNavigation(node, kUrl1);
- main_test_rfh()->SendBeginNavigationWithURL(kUrl1);
+ LoadURLAndBeginNavigationOnMainFrame(kUrl1);
NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node);
ASSERT_TRUE(main_request);
+ EXPECT_FALSE(contents()->GetSpeculativeMainFrame());
// It then redirects to another site.
net::RedirectInfo redirect_info;
@@ -252,52 +317,65 @@ TEST_F(NavigatorTestWithBrowserSideNavigation, RedirectCrossSite) {
redirect_info.new_method = "GET";
redirect_info.new_url = kUrl2;
redirect_info.new_first_party_for_cookies = kUrl2;
- scoped_refptr<ResourceResponse> response(new ResourceResponse);
+ scoped_refptr<ResourceResponse> response(new ResourceResponse);
GetLoaderForNavigationRequest(main_request)->CallOnRequestRedirected(
redirect_info, response);
// The redirect should have been followed.
EXPECT_EQ(1, GetLoaderForNavigationRequest(main_request)->redirect_count());
+ EXPECT_FALSE(contents()->GetSpeculativeMainFrame());
// Then it commits.
response = new ResourceResponse;
GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted(
response, MakeEmptyStream());
- RenderFrameHostImpl* pending_rfh =
- node->render_manager()->pending_frame_host();
- ASSERT_TRUE(pending_rfh);
- EXPECT_NE(pending_rfh, rfh);
- EXPECT_TRUE(pending_rfh->IsRenderFrameLive());
- EXPECT_TRUE(pending_rfh->render_view_host()->IsRenderViewLive());
+ RenderFrameHost* last_speculative_rfh = contents()->GetSpeculativeMainFrame();
+ EXPECT_TRUE(last_speculative_rfh);
+
+ // And commits provisional load.
+ contents()->CommitPendingNavigation();
+ RenderFrameHostImpl* final_rfh = main_test_rfh();
+ ASSERT_TRUE(final_rfh);
+ EXPECT_NE(rfh, final_rfh);
+ EXPECT_EQ(last_speculative_rfh, final_rfh);
+ EXPECT_TRUE(final_rfh->IsRenderFrameLive());
+ EXPECT_TRUE(final_rfh->render_view_host()->IsRenderViewLive());
+ EXPECT_FALSE(contents()->GetSpeculativeMainFrame());
}
-// PlzNavigate: Test that a navigation is cancelled if another request has been
-// issued in the meantime.
+// PlzNavigate: Test that a navigation is canceled if another request has been
+// issued in the meantime. Also confirms that the speculative renderer is
+// correctly updated in the process.
TEST_F(NavigatorTestWithBrowserSideNavigation, ReplacePendingNavigation) {
const GURL kUrl0("http://www.wikipedia.org/");
- const GURL kUrl0_site = SiteInstance::GetSiteForURL(browser_context(), kUrl0);
const GURL kUrl1("http://www.chromium.org/");
+ const GURL kUrl1_site = SiteInstance::GetSiteForURL(browser_context(), kUrl1);
const GURL kUrl2("http://www.google.com/");
const GURL kUrl2_site = SiteInstance::GetSiteForURL(browser_context(), kUrl2);
// Initialization.
contents()->NavigateAndCommit(kUrl0);
FrameTreeNode* node = main_test_rfh()->frame_tree_node();
- EXPECT_EQ(kUrl0_site, main_test_rfh()->GetSiteInstance()->GetSiteURL());
// Request navigation to the 1st URL.
- SendRequestNavigation(node, kUrl1);
- main_test_rfh()->SendBeginNavigationWithURL(kUrl1);
+ LoadURLAndBeginNavigationOnMainFrame(kUrl1);
NavigationRequest* request1 = GetNavigationRequestForFrameTreeNode(node);
ASSERT_TRUE(request1);
EXPECT_EQ(kUrl1, request1->common_params().url);
base::WeakPtr<TestNavigationURLLoader> loader1 =
GetLoaderForNavigationRequest(request1)->AsWeakPtr();
+ // Confirm a speculative RFH was created.
+ ASSERT_TRUE(contents()->GetSpeculativeMainFrame());
+ int32 site_instance_id_1 =
+ contents()->GetSpeculativeMainFrame()->GetSiteInstance()->GetId();
+ EXPECT_EQ(
+ kUrl1_site,
+ contents()->GetSpeculativeMainFrame()->GetSiteInstance()->GetSiteURL());
+
// Request navigation to the 2nd URL; the NavigationRequest must have been
// replaced by a new one with a different URL.
- SendRequestNavigation(node, kUrl2);
- main_test_rfh()->SendBeginNavigationWithURL(kUrl2);
+ LoadURLAndBeginNavigationOnMainFrame(kUrl2);
NavigationRequest* request2 = GetNavigationRequestForFrameTreeNode(node);
ASSERT_TRUE(request2);
EXPECT_EQ(kUrl2, request2->common_params().url);
@@ -305,18 +383,31 @@ TEST_F(NavigatorTestWithBrowserSideNavigation, ReplacePendingNavigation) {
// Confirm that the first loader got destroyed.
EXPECT_FALSE(loader1);
- // Confirm that the commit corresponds to the new request.
+ // Confirm that a new speculative RFH was created.
+ ASSERT_TRUE(contents()->GetSpeculativeMainFrame());
+ int32 site_instance_id_2 =
+ contents()->GetSpeculativeMainFrame()->GetSiteInstance()->GetId();
+ EXPECT_NE(site_instance_id_1, site_instance_id_2);
+
+ // Commit.
scoped_refptr<ResourceResponse> response(new ResourceResponse);
GetLoaderForNavigationRequest(request2)->CallOnResponseStarted(
response, MakeEmptyStream());
- RenderFrameHostImpl* pending_rfh =
- node->render_manager()->pending_frame_host();
- ASSERT_TRUE(pending_rfh);
- EXPECT_EQ(kUrl2_site, pending_rfh->GetSiteInstance()->GetSiteURL());
+
+ // And commit provisional load.
+ contents()->CommitPendingNavigation();
+
+ // Confirm that the commit corresponds to the new request.
+ ASSERT_TRUE(main_test_rfh());
+ EXPECT_EQ(kUrl2_site, main_test_rfh()->GetSiteInstance()->GetSiteURL());
+
+ // Confirm that the committed RFH is the new speculative one.
+ EXPECT_EQ(site_instance_id_2, main_test_rfh()->GetSiteInstance()->GetId());
}
// PlzNavigate: Test that a reload navigation is properly signaled to the
-// renderer when the navigation can commit.
+// renderer when the navigation can commit. Speculative renderers should not be
+// created at any step.
TEST_F(NavigatorTestWithBrowserSideNavigation, Reload) {
const GURL kUrl("http://www.google.com/");
contents()->NavigateAndCommit(kUrl);
@@ -325,27 +416,179 @@ TEST_F(NavigatorTestWithBrowserSideNavigation, Reload) {
SendRequestNavigationWithParameters(
node, kUrl, Referrer(), ui::PAGE_TRANSITION_LINK,
NavigationController::RELOAD);
- contents()->GetMainFrame()->SendBeginNavigationWithURL(kUrl);
+ main_test_rfh()->SendBeginNavigationWithURL(kUrl);
// A NavigationRequest should have been generated.
NavigationRequest* main_request =
GetNavigationRequestForFrameTreeNode(node);
ASSERT_TRUE(main_request != NULL);
EXPECT_EQ(FrameMsg_Navigate_Type::RELOAD,
main_request->common_params().navigation_type);
- int page_id = contents()->GetMaxPageIDForSiteInstance(
- main_test_rfh()->GetSiteInstance()) + 1;
- main_test_rfh()->SendNavigate(page_id, kUrl);
+ EXPECT_FALSE(contents()->GetSpeculativeMainFrame());
+
+ main_test_rfh()->SendNavigate(10, kUrl);
clamy 2014/12/19 13:35:57 Why 10?
carlosk 2014/12/29 16:40:16 I was just setting a value high enough. I fixed th
+ EXPECT_FALSE(contents()->GetSpeculativeMainFrame());
// Now do a shift+reload.
SendRequestNavigationWithParameters(
node, kUrl, Referrer(), ui::PAGE_TRANSITION_LINK,
NavigationController::RELOAD_IGNORING_CACHE);
- contents()->GetMainFrame()->SendBeginNavigationWithURL(kUrl);
+ main_test_rfh()->SendBeginNavigationWithURL(kUrl);
// A NavigationRequest should have been generated.
main_request = GetNavigationRequestForFrameTreeNode(node);
ASSERT_TRUE(main_request != NULL);
EXPECT_EQ(FrameMsg_Navigate_Type::RELOAD_IGNORING_CACHE,
main_request->common_params().navigation_type);
+ EXPECT_FALSE(contents()->GetSpeculativeMainFrame());
+}
+
+// PlzNavigate: Confirm that a speculative RenderFrameHost is used when
+// navigating from one site to the another.
+TEST_F(NavigatorTestWithBrowserSideNavigation,
+ SpeculativeRendererWorksBaseCase) {
+ // Navigate to an initial site.
+ const GURL kUrlInit("http://wikipedia.org/");
+ contents()->NavigateAndCommit(kUrlInit);
+ FrameTreeNode* node = main_test_rfh()->frame_tree_node();
+
+ // Begin navigating to another site.
+ const GURL kUrl("http://google.com/");
+ LoadURLAndBeginNavigationOnMainFrame(kUrl);
+ ASSERT_TRUE(contents()->GetSpeculativeMainFrame());
+ EXPECT_NE(contents()->GetSpeculativeMainFrame(), main_test_rfh());
+ EXPECT_EQ(
+ SiteInstanceImpl::GetSiteForURL(browser_context(), kUrl),
+ contents()->GetSpeculativeMainFrame()->GetSiteInstance()->GetSiteURL());
+ EXPECT_FALSE(contents()->GetPendingMainFrame());
+ int32 site_instance_id =
+ contents()->GetSpeculativeMainFrame()->GetSiteInstance()->GetId();
+
+ // Ask Navigator to commit the navigation by simulating a call to
+ // OnResponseStarted.
+ scoped_refptr<ResourceResponse> response(new ResourceResponse);
+ GetLoaderForNavigationRequest(GetNavigationRequestForFrameTreeNode(node))
+ ->CallOnResponseStarted(response, MakeEmptyStream());
+ ASSERT_TRUE(contents()->GetSpeculativeMainFrame());
+ EXPECT_EQ(site_instance_id,
+ contents()->GetSpeculativeMainFrame()->GetSiteInstance()->GetId());
+ EXPECT_FALSE(contents()->GetPendingMainFrame());
+
+ // Invoke OnDidCommitProvisionalLoad.
+ contents()->CommitPendingNavigation();
+ EXPECT_EQ(site_instance_id, main_test_rfh()->GetSiteInstance()->GetId());
+ EXPECT_FALSE(contents()->GetSpeculativeMainFrame());
+ EXPECT_FALSE(contents()->GetPendingMainFrame());
+}
+
+// PlzNavigate: Confirm that a speculative RenderFrameHost is thrown away when
+// the final URL's site differs from the initial one due to redirects.
+TEST_F(NavigatorTestWithBrowserSideNavigation,
+ SpeculativeRendererDiscardedAfterRedirectToAnotherSite) {
+ // Navigate to an initial site.
+ const GURL kUrlInit("http://wikipedia.org/");
+ contents()->NavigateAndCommit(kUrlInit);
+ FrameTreeNode* node = main_test_rfh()->frame_tree_node();
+ int32 init_site_instance_id = main_test_rfh()->GetSiteInstance()->GetId();
+
+ // Begin navigating to another site.
+ const GURL kUrl("http://google.com/");
+ LoadURLAndBeginNavigationOnMainFrame(kUrl);
+ int32 site_instance_id =
+ contents()->GetSpeculativeMainFrame()->GetSiteInstance()->GetId();
+ EXPECT_EQ(init_site_instance_id, main_test_rfh()->GetSiteInstance()->GetId());
+ ASSERT_TRUE(contents()->GetSpeculativeMainFrame());
+ EXPECT_NE(contents()->GetSpeculativeMainFrame(), main_test_rfh());
+ EXPECT_EQ(
+ SiteInstanceImpl::GetSiteForURL(browser_context(), kUrl),
+ contents()->GetSpeculativeMainFrame()->GetSiteInstance()->GetSiteURL());
+
+ // It then redirects to yet another site.
+ NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node);
+ ASSERT_TRUE(main_request);
+ const GURL kUrlRedirect("https://www.google.com/");
+ net::RedirectInfo redirect_info;
+ redirect_info.status_code = 302;
+ redirect_info.new_method = "GET";
+ redirect_info.new_url = kUrlRedirect;
+ redirect_info.new_first_party_for_cookies = kUrlRedirect;
+ scoped_refptr<ResourceResponse> response(new ResourceResponse);
+ GetLoaderForNavigationRequest(main_request)
+ ->CallOnRequestRedirected(redirect_info, response);
+ EXPECT_EQ(init_site_instance_id, main_test_rfh()->GetSiteInstance()->GetId());
+ ASSERT_TRUE(contents()->GetSpeculativeMainFrame());
+
+ // TODO(carlosk): once the speculative RenderFrameHost updates with redirects
+ // this next check will be changed to verify that it actually happens.
+ EXPECT_EQ(site_instance_id,
+ contents()->GetSpeculativeMainFrame()->GetSiteInstance()->GetId());
+
+ // Commit the navigation with Navigator by simulating the call to
+ // OnResponseStarted.
+ response = new ResourceResponse;
+ GetLoaderForNavigationRequest(main_request)
+ ->CallOnResponseStarted(response, MakeEmptyStream());
+ EXPECT_EQ(init_site_instance_id, main_test_rfh()->GetSiteInstance()->GetId());
+
+ // Once commit happens the speculative RenderFrameHost is already updated to
+ // match the known final SiteInstance.
+ ASSERT_TRUE(contents()->GetSpeculativeMainFrame());
+ EXPECT_EQ(
+ SiteInstanceImpl::GetSiteForURL(browser_context(), kUrlRedirect),
+ contents()->GetSpeculativeMainFrame()->GetSiteInstance()->GetSiteURL());
+ int32 redirect_site_instance_id =
+ contents()->GetSpeculativeMainFrame()->GetSiteInstance()->GetId();
+
+ // Invoke OnDidCommitProvisionalLoad.
+ contents()->CommitPendingNavigation();
+
+ // And finally on commit-provisional-load the already created RenderFrameHost
+ // is made active.
+ EXPECT_EQ(redirect_site_instance_id,
+ main_test_rfh()->GetSiteInstance()->GetId());
+ EXPECT_FALSE(contents()->GetSpeculativeMainFrame());
+}
+
+// PlzNavigate: Verify that a previously swapped-out RenderFrameHost is
+// correctly reused when spawning a speculative RenderFrameHost in a navigation
+// using the same SiteInstance.
+TEST_F(NavigatorTestWithBrowserSideNavigation,
+ SpeculativeRendererReuseSwappedOutRFH) {
+ // Navigate to an initial site.
+ const GURL kUrl1("http://wikipedia.org/");
+ contents()->NavigateAndCommit(kUrl1);
+ RenderFrameHostImpl* rfh1 = main_test_rfh();
+ FrameTreeNode* node = rfh1->frame_tree_node();
+ RenderFrameHostManager* rfhm = node->render_manager();
+
+ // Increment active frame count to cause the RenderFrameHost to be swapped out
+ // (instead of immediately destroyed).
+ rfh1->GetSiteInstance()->increment_active_frame_count();
+
+ // Navigate to another site to swap-out the initial RenderFrameHost.
+ const GURL kUrl2("http://chromium.org/");
+ contents()->NavigateAndCommit(kUrl2);
+ ASSERT_NE(rfh1, main_test_rfh());
+ EXPECT_NE(RenderFrameHostImpl::STATE_DEFAULT, rfh1->rfh_state());
+ EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, main_test_rfh()->rfh_state());
+ EXPECT_TRUE(rfhm->IsOnSwappedOutList(rfh1));
+
+ // Now go back to the initial site so the swapped-out RenderFrameHost should
+ // be reused.
+ LoadURLAndBeginNavigationOnMainFrame(kUrl1);
+ EXPECT_EQ(rfh1, contents()->GetSpeculativeMainFrame());
+ EXPECT_NE(RenderFrameHostImpl::STATE_DEFAULT,
+ contents()->GetSpeculativeMainFrame()->rfh_state());
+
+ scoped_refptr<ResourceResponse> response(new ResourceResponse);
+ GetLoaderForNavigationRequest(GetNavigationRequestForFrameTreeNode(node))
+ ->CallOnResponseStarted(response, MakeEmptyStream());
+ EXPECT_EQ(rfh1, contents()->GetSpeculativeMainFrame());
+ EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT,
+ contents()->GetSpeculativeMainFrame()->rfh_state());
+
+ contents()->CommitPendingNavigation();
+ EXPECT_EQ(rfh1, main_test_rfh());
+ EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, rfh1->rfh_state());
+ EXPECT_FALSE(rfhm->IsOnSwappedOutList(rfh1));
}
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698