OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/command_line.h" |
5 #include "base/macros.h" | 6 #include "base/macros.h" |
6 #include "base/time/time.h" | 7 #include "base/time/time.h" |
7 #include "content/browser/frame_host/navigation_controller_impl.h" | 8 #include "content/browser/frame_host/navigation_controller_impl.h" |
8 #include "content/browser/frame_host/navigation_entry_impl.h" | 9 #include "content/browser/frame_host/navigation_entry_impl.h" |
9 #include "content/browser/frame_host/navigation_request.h" | 10 #include "content/browser/frame_host/navigation_request.h" |
10 #include "content/browser/frame_host/navigation_request_info.h" | 11 #include "content/browser/frame_host/navigation_request_info.h" |
11 #include "content/browser/frame_host/navigator.h" | 12 #include "content/browser/frame_host/navigator.h" |
12 #include "content/browser/frame_host/navigator_impl.h" | 13 #include "content/browser/frame_host/navigator_impl.h" |
13 #include "content/browser/frame_host/render_frame_host_manager.h" | 14 #include "content/browser/frame_host/render_frame_host_manager.h" |
14 #include "content/browser/site_instance_impl.h" | 15 #include "content/browser/site_instance_impl.h" |
15 #include "content/browser/streams/stream.h" | 16 #include "content/browser/streams/stream.h" |
16 #include "content/common/navigation_params.h" | 17 #include "content/common/navigation_params.h" |
17 #include "content/public/browser/stream_handle.h" | 18 #include "content/public/browser/stream_handle.h" |
| 19 #include "content/public/common/content_switches.h" |
18 #include "content/public/common/url_constants.h" | 20 #include "content/public/common/url_constants.h" |
19 #include "content/public/common/url_utils.h" | 21 #include "content/public/common/url_utils.h" |
| 22 #include "content/public/test/mock_render_process_host.h" |
20 #include "content/test/browser_side_navigation_test_utils.h" | 23 #include "content/test/browser_side_navigation_test_utils.h" |
21 #include "content/test/test_navigation_url_loader.h" | 24 #include "content/test/test_navigation_url_loader.h" |
22 #include "content/test/test_render_frame_host.h" | 25 #include "content/test/test_render_frame_host.h" |
23 #include "content/test/test_web_contents.h" | 26 #include "content/test/test_web_contents.h" |
24 #include "net/base/load_flags.h" | 27 #include "net/base/load_flags.h" |
25 #include "net/http/http_response_headers.h" | 28 #include "net/http/http_response_headers.h" |
26 #include "net/url_request/redirect_info.h" | 29 #include "net/url_request/redirect_info.h" |
27 #include "ui/base/page_transition_types.h" | 30 #include "ui/base/page_transition_types.h" |
28 #include "url/url_constants.h" | 31 #include "url/url_constants.h" |
29 | 32 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 controller().GetBrowserContext()))); | 69 controller().GetBrowserContext()))); |
67 static_cast<NavigatorImpl*>(node->navigator())->RequestNavigation( | 70 static_cast<NavigatorImpl*>(node->navigator())->RequestNavigation( |
68 node, *entry, reload_type, base::TimeTicks::Now()); | 71 node, *entry, reload_type, base::TimeTicks::Now()); |
69 } | 72 } |
70 | 73 |
71 NavigationRequest* GetNavigationRequestForFrameTreeNode( | 74 NavigationRequest* GetNavigationRequestForFrameTreeNode( |
72 FrameTreeNode* frame_tree_node) { | 75 FrameTreeNode* frame_tree_node) { |
73 return static_cast<NavigatorImpl*>(frame_tree_node->navigator()) | 76 return static_cast<NavigatorImpl*>(frame_tree_node->navigator()) |
74 ->GetNavigationRequestForNodeForTesting(frame_tree_node); | 77 ->GetNavigationRequestForNodeForTesting(frame_tree_node); |
75 } | 78 } |
| 79 |
| 80 TestRenderFrameHost* GetSpeculativeRenderFrameHost(FrameTreeNode* node) { |
| 81 return static_cast<TestRenderFrameHost*>( |
| 82 node->render_manager()->speculative_render_frame_host_.get()); |
| 83 } |
| 84 |
| 85 // Checks if this RenderFrameHost sent a single FrameMsg_CommitNavigation |
| 86 // since the last clearing of the sink. |
| 87 // Note: caller must invoke ClearMessages on the sink at some point before |
| 88 // the tracked commit happens to clear up commit messages from previous |
| 89 // navigations. |
| 90 bool DidRenderFrameHostRequestCommit(RenderFrameHostImpl* rfh) { |
| 91 MockRenderProcessHost* rph = |
| 92 static_cast<MockRenderProcessHost*>(rfh->GetProcess()); |
| 93 const FrameMsg_CommitNavigation* commit_message = |
| 94 static_cast<const FrameMsg_CommitNavigation*>( |
| 95 rph->sink().GetUniqueMessageMatching( |
| 96 FrameMsg_CommitNavigation::ID)); |
| 97 return commit_message && |
| 98 rfh->GetRoutingID() == commit_message->routing_id(); |
| 99 } |
76 }; | 100 }; |
77 | 101 |
| 102 // PlzNavigate: Test final state after a complete navigation (to avoid repeating |
| 103 // these checks in other tests). |
| 104 TEST_F(NavigatorTestWithBrowserSideNavigation, NavigationFinishedState) { |
| 105 const GURL kUrl("http://chromium.org/"); |
| 106 contents()->NavigateAndCommit(kUrl); |
| 107 ASSERT_TRUE(main_test_rfh()); |
| 108 EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, main_test_rfh()->rfh_state()); |
| 109 EXPECT_EQ(SiteInstanceImpl::GetSiteForURL(browser_context(), kUrl), |
| 110 main_test_rfh()->GetSiteInstance()->GetSiteURL()); |
| 111 EXPECT_EQ(kUrl, contents()->GetLastCommittedURL()); |
| 112 |
| 113 // After a navigation is finished no speculative RenderFrameHost should |
| 114 // exist. |
| 115 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); |
| 116 EXPECT_FALSE(GetSpeculativeRenderFrameHost(node)); |
| 117 |
| 118 // With PlzNavigate enabled a pending RenderFrameHost should never exist. |
| 119 EXPECT_FALSE(node->render_manager()->pending_frame_host()); |
| 120 } |
| 121 |
78 // PlzNavigate: Test that a proper NavigationRequest is created by | 122 // PlzNavigate: Test that a proper NavigationRequest is created by |
79 // BeginNavigation. | 123 // BeginNavigation. |
80 // Note that all PlzNavigate methods on the browser side require the use of the | 124 // Note that all PlzNavigate methods on the browser side require the use of the |
81 // flag kEnableBrowserSideNavigation. | 125 // flag kEnableBrowserSideNavigation. |
82 TEST_F(NavigatorTestWithBrowserSideNavigation, BeginNavigation) { | 126 TEST_F(NavigatorTestWithBrowserSideNavigation, BeginNavigation) { |
83 const GURL kUrl1("http://www.google.com/"); | 127 const GURL kUrl1("http://www.google.com/"); |
84 const GURL kUrl2("http://www.chromium.org/"); | 128 const GURL kUrl2("http://www.chromium.org/"); |
85 const GURL kUrl3("http://www.gmail.com/"); | 129 const GURL kUrl3("http://www.gmail.com/"); |
86 | 130 |
87 contents()->NavigateAndCommit(kUrl1); | 131 contents()->NavigateAndCommit(kUrl1); |
88 | 132 |
89 // Add a subframe. | 133 // Add a subframe. |
90 FrameTreeNode* root = contents()->GetFrameTree()->root(); | 134 FrameTreeNode* root_node = contents()->GetFrameTree()->root(); |
91 TestRenderFrameHost* subframe_rfh = static_cast<TestRenderFrameHost*>( | 135 TestRenderFrameHost* subframe_rfh = |
92 contents()->GetFrameTree()->AddFrame( | 136 static_cast<TestRenderFrameHost*>(contents()->GetFrameTree()->AddFrame( |
93 root, root->current_frame_host()->GetProcess()->GetID(), 14, | 137 root_node, root_node->current_frame_host()->GetProcess()->GetID(), 14, |
94 "Child")); | 138 "Child")); |
95 EXPECT_TRUE(subframe_rfh); | 139 ASSERT_TRUE(subframe_rfh); |
96 | 140 |
97 FrameTreeNode* subframe_node = subframe_rfh->frame_tree_node(); | 141 FrameTreeNode* subframe_node = subframe_rfh->frame_tree_node(); |
98 SendRequestNavigation(subframe_rfh->frame_tree_node(), kUrl2); | 142 SendRequestNavigation(subframe_node, kUrl2); |
99 // There is no previous renderer in the subframe, so BeginNavigation is | 143 // There is no previous renderer in the subframe, so BeginNavigation is |
100 // handled already. | 144 // handled already. |
101 NavigationRequest* subframe_request = | 145 NavigationRequest* subframe_request = |
102 GetNavigationRequestForFrameTreeNode(subframe_node); | 146 GetNavigationRequestForFrameTreeNode(subframe_node); |
103 TestNavigationURLLoader* subframe_loader = | 147 TestNavigationURLLoader* subframe_loader = |
104 GetLoaderForNavigationRequest(subframe_request); | 148 GetLoaderForNavigationRequest(subframe_request); |
105 ASSERT_TRUE(subframe_request); | 149 ASSERT_TRUE(subframe_request); |
106 EXPECT_EQ(kUrl2, subframe_request->common_params().url); | 150 EXPECT_EQ(kUrl2, subframe_request->common_params().url); |
107 EXPECT_EQ(kUrl2, subframe_loader->common_params().url); | 151 EXPECT_EQ(kUrl2, subframe_loader->common_params().url); |
108 // First party for cookies url should be that of the main frame. | 152 // First party for cookies url should be that of the main frame. |
109 EXPECT_EQ(kUrl1, subframe_loader->request_info()->first_party_for_cookies); | 153 EXPECT_EQ(kUrl1, subframe_loader->request_info()->first_party_for_cookies); |
110 EXPECT_FALSE(subframe_loader->request_info()->is_main_frame); | 154 EXPECT_FALSE(subframe_loader->request_info()->is_main_frame); |
111 EXPECT_TRUE(subframe_loader->request_info()->parent_is_main_frame); | 155 EXPECT_TRUE(subframe_loader->request_info()->parent_is_main_frame); |
| 156 EXPECT_FALSE(GetSpeculativeRenderFrameHost(root_node)); |
112 | 157 |
113 SendRequestNavigation(root, kUrl3); | 158 // Subframe navigations should never create a speculative RenderFrameHost, |
| 159 // unless site-per-process is enabled. In that case, as the subframe |
| 160 // navigation is to a different site and is still ongoing, it should have one. |
| 161 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 162 switches::kSitePerProcess)) { |
| 163 EXPECT_TRUE(GetSpeculativeRenderFrameHost(subframe_node)); |
| 164 } else { |
| 165 EXPECT_FALSE(GetSpeculativeRenderFrameHost(subframe_node)); |
| 166 } |
| 167 |
| 168 SendRequestNavigation(root_node, kUrl3); |
114 // Simulate a BeginNavigation IPC on the main frame. | 169 // Simulate a BeginNavigation IPC on the main frame. |
115 contents()->GetMainFrame()->SendBeginNavigationWithURL(kUrl3); | 170 main_test_rfh()->SendBeginNavigationWithURL(kUrl3); |
116 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(root); | 171 NavigationRequest* main_request = |
| 172 GetNavigationRequestForFrameTreeNode(root_node); |
117 TestNavigationURLLoader* main_loader = | 173 TestNavigationURLLoader* main_loader = |
118 GetLoaderForNavigationRequest(main_request); | 174 GetLoaderForNavigationRequest(main_request); |
119 ASSERT_TRUE(main_request); | 175 ASSERT_TRUE(main_request); |
120 EXPECT_EQ(kUrl3, main_request->common_params().url); | 176 EXPECT_EQ(kUrl3, main_request->common_params().url); |
121 EXPECT_EQ(kUrl3, main_loader->common_params().url); | 177 EXPECT_EQ(kUrl3, main_loader->common_params().url); |
122 EXPECT_EQ(kUrl3, main_loader->request_info()->first_party_for_cookies); | 178 EXPECT_EQ(kUrl3, main_loader->request_info()->first_party_for_cookies); |
123 EXPECT_TRUE(main_loader->request_info()->is_main_frame); | 179 EXPECT_TRUE(main_loader->request_info()->is_main_frame); |
124 EXPECT_FALSE(main_loader->request_info()->parent_is_main_frame); | 180 EXPECT_FALSE(main_loader->request_info()->parent_is_main_frame); |
| 181 |
| 182 // Main frame navigation to a different site should use a speculative |
| 183 // RenderFrameHost. |
| 184 EXPECT_TRUE(GetSpeculativeRenderFrameHost(root_node)); |
| 185 |
| 186 // As the main frame hasn't yet committed the subframe still exists. Thus, the |
| 187 // above situation regarding subframe navigations is valid here. |
| 188 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 189 switches::kSitePerProcess)) { |
| 190 EXPECT_TRUE(GetSpeculativeRenderFrameHost(subframe_node)); |
| 191 } else { |
| 192 EXPECT_FALSE(GetSpeculativeRenderFrameHost(subframe_node)); |
| 193 } |
125 } | 194 } |
126 | 195 |
127 // PlzNavigate: Test that RequestNavigation creates a NavigationRequest and that | 196 // PlzNavigate: Test that RequestNavigation creates a NavigationRequest and that |
128 // RenderFrameHost is not modified when the navigation commits. | 197 // RenderFrameHost is not modified when the navigation commits. |
129 TEST_F(NavigatorTestWithBrowserSideNavigation, NoLiveRenderer) { | 198 TEST_F(NavigatorTestWithBrowserSideNavigation, NoLiveRenderer) { |
130 const GURL kUrl("http://www.google.com/"); | 199 const GURL kUrl("http://www.google.com/"); |
131 | 200 |
132 EXPECT_FALSE(main_test_rfh()->render_view_host()->IsRenderViewLive()); | 201 EXPECT_FALSE(main_test_rfh()->render_view_host()->IsRenderViewLive()); |
133 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); | 202 RenderFrameHostImpl* rfh = main_test_rfh(); |
| 203 FrameTreeNode* node = rfh->frame_tree_node(); |
134 SendRequestNavigation(node, kUrl); | 204 SendRequestNavigation(node, kUrl); |
| 205 |
| 206 // A NavigationRequest should have been generated. |
135 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node); | 207 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node); |
136 // A NavigationRequest should have been generated. | |
137 EXPECT_TRUE(main_request != NULL); | 208 EXPECT_TRUE(main_request != NULL); |
138 RenderFrameHostImpl* rfh = main_test_rfh(); | 209 |
| 210 // Since we're re-using the current RenderFrameHost, no speculative one should |
| 211 // be created. |
| 212 EXPECT_FALSE(GetSpeculativeRenderFrameHost(node)); |
139 | 213 |
140 // Now return the response without any redirects. This will cause the | 214 // Now return the response without any redirects. This will cause the |
141 // navigation to commit at the same URL. | 215 // navigation to commit at the same URL. |
142 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 216 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
143 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted( | 217 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted( |
144 response, MakeEmptyStream()); | 218 response, MakeEmptyStream()); |
145 main_request = GetNavigationRequestForFrameTreeNode(node); | 219 main_request = GetNavigationRequestForFrameTreeNode(node); |
146 | 220 |
147 // The main RFH should not have been changed, and the renderer should have | 221 // The main RFH should not have been changed, and the renderer should have |
148 // been initialized. | 222 // been initialized. |
149 EXPECT_EQ(rfh, main_test_rfh()); | 223 EXPECT_EQ(rfh, main_test_rfh()); |
150 EXPECT_TRUE(main_test_rfh()->IsRenderFrameLive()); | 224 EXPECT_TRUE(main_test_rfh()->IsRenderFrameLive()); |
151 EXPECT_TRUE(main_test_rfh()->render_view_host()->IsRenderViewLive()); | 225 EXPECT_TRUE(main_test_rfh()->render_view_host()->IsRenderViewLive()); |
152 } | 226 } |
153 | 227 |
154 // PlzNavigate: Test that commiting an HTTP 204 or HTTP 205 response cancels the | 228 // PlzNavigate: Test that committing an HTTP 204 or HTTP 205 response cancels |
155 // navigation. | 229 // the navigation. |
156 TEST_F(NavigatorTestWithBrowserSideNavigation, NoContent) { | 230 TEST_F(NavigatorTestWithBrowserSideNavigation, NoContent) { |
157 const GURL kUrl1("http://www.chromium.org/"); | 231 const GURL kUrl1("http://www.chromium.org/"); |
158 const GURL kUrl2("http://www.google.com/"); | 232 const GURL kUrl2("http://www.google.com/"); |
159 | 233 |
160 // Load a URL. | 234 // Load a URL. |
161 contents()->NavigateAndCommit(kUrl1); | 235 contents()->NavigateAndCommit(kUrl1); |
162 RenderFrameHostImpl* rfh = main_test_rfh(); | |
163 EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, rfh->rfh_state()); | |
164 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); | 236 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); |
165 | 237 |
166 // Navigate to a different site. | 238 // Navigate to a different site. |
| 239 process()->sink().ClearMessages(); |
167 SendRequestNavigation(node, kUrl2); | 240 SendRequestNavigation(node, kUrl2); |
168 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); | 241 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); |
| 242 |
169 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node); | 243 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node); |
170 ASSERT_TRUE(main_request); | 244 ASSERT_TRUE(main_request); |
171 | 245 |
| 246 // Navigations to a different site do create a speculative RenderFrameHost. |
| 247 EXPECT_TRUE(GetSpeculativeRenderFrameHost(node)); |
| 248 |
172 // Commit an HTTP 204 response. | 249 // Commit an HTTP 204 response. |
173 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 250 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
174 const char kNoContentHeaders[] = "HTTP/1.1 204 No Content\0\0"; | 251 const char kNoContentHeaders[] = "HTTP/1.1 204 No Content\0\0"; |
175 response->head.headers = new net::HttpResponseHeaders( | 252 response->head.headers = new net::HttpResponseHeaders( |
176 std::string(kNoContentHeaders, arraysize(kNoContentHeaders))); | 253 std::string(kNoContentHeaders, arraysize(kNoContentHeaders))); |
177 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted( | 254 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted( |
178 response, MakeEmptyStream()); | 255 response, MakeEmptyStream()); |
179 | 256 |
180 // There should be no pending RenderFrameHost; the navigation was aborted. | 257 // There should be no pending nor speculative RenderFrameHost; the navigation |
| 258 // was aborted. |
| 259 EXPECT_FALSE(DidRenderFrameHostRequestCommit(main_test_rfh())); |
181 EXPECT_FALSE(GetNavigationRequestForFrameTreeNode(node)); | 260 EXPECT_FALSE(GetNavigationRequestForFrameTreeNode(node)); |
182 EXPECT_FALSE(node->render_manager()->pending_frame_host()); | 261 EXPECT_FALSE(node->render_manager()->pending_frame_host()); |
| 262 EXPECT_FALSE(GetSpeculativeRenderFrameHost(node)); |
183 | 263 |
184 // Now, repeat the test with 205 Reset Content. | 264 // Now, repeat the test with 205 Reset Content. |
185 | 265 |
186 // Navigate to a different site again. | 266 // Navigate to a different site again. |
| 267 process()->sink().ClearMessages(); |
187 SendRequestNavigation(node, kUrl2); | 268 SendRequestNavigation(node, kUrl2); |
188 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); | 269 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); |
| 270 |
189 main_request = GetNavigationRequestForFrameTreeNode(node); | 271 main_request = GetNavigationRequestForFrameTreeNode(node); |
190 ASSERT_TRUE(main_request); | 272 ASSERT_TRUE(main_request); |
| 273 EXPECT_TRUE(GetSpeculativeRenderFrameHost(node)); |
191 | 274 |
192 // Commit an HTTP 205 response. | 275 // Commit an HTTP 205 response. |
193 response = new ResourceResponse; | 276 response = new ResourceResponse; |
194 const char kResetContentHeaders[] = "HTTP/1.1 205 Reset Content\0\0"; | 277 const char kResetContentHeaders[] = "HTTP/1.1 205 Reset Content\0\0"; |
195 response->head.headers = new net::HttpResponseHeaders( | 278 response->head.headers = new net::HttpResponseHeaders( |
196 std::string(kResetContentHeaders, arraysize(kResetContentHeaders))); | 279 std::string(kResetContentHeaders, arraysize(kResetContentHeaders))); |
197 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted( | 280 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted( |
198 response, MakeEmptyStream()); | 281 response, MakeEmptyStream()); |
199 | 282 |
200 // There should be no pending RenderFrameHost; the navigation was aborted. | 283 // There should be no pending nor speculative RenderFrameHost; the navigation |
| 284 // was aborted. |
| 285 EXPECT_FALSE(DidRenderFrameHostRequestCommit(main_test_rfh())); |
201 EXPECT_FALSE(GetNavigationRequestForFrameTreeNode(node)); | 286 EXPECT_FALSE(GetNavigationRequestForFrameTreeNode(node)); |
202 EXPECT_FALSE(node->render_manager()->pending_frame_host()); | 287 EXPECT_FALSE(node->render_manager()->pending_frame_host()); |
| 288 EXPECT_FALSE(GetSpeculativeRenderFrameHost(node)); |
203 } | 289 } |
204 | 290 |
205 // PlzNavigate: Test that a new RenderFrameHost is created when doing a cross | 291 // PlzNavigate: Test that a new RenderFrameHost is created when doing a cross |
206 // site navigation. | 292 // site navigation. |
207 TEST_F(NavigatorTestWithBrowserSideNavigation, CrossSiteNavigation) { | 293 TEST_F(NavigatorTestWithBrowserSideNavigation, CrossSiteNavigation) { |
208 const GURL kUrl1("http://www.chromium.org/"); | 294 const GURL kUrl1("http://www.chromium.org/"); |
209 const GURL kUrl2("http://www.google.com/"); | 295 const GURL kUrl2("http://www.google.com/"); |
210 | 296 |
211 contents()->NavigateAndCommit(kUrl1); | 297 contents()->NavigateAndCommit(kUrl1); |
212 RenderFrameHostImpl* rfh = main_test_rfh(); | 298 RenderFrameHostImpl* initial_rfh = main_test_rfh(); |
213 EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, rfh->rfh_state()); | |
214 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); | 299 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); |
215 | 300 |
216 // Navigate to a different site. | 301 // Navigate to a different site. |
| 302 process()->sink().ClearMessages(); |
217 SendRequestNavigation(node, kUrl2); | 303 SendRequestNavigation(node, kUrl2); |
218 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); | 304 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); |
219 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node); | 305 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node); |
220 ASSERT_TRUE(main_request); | 306 ASSERT_TRUE(main_request); |
| 307 EXPECT_TRUE(GetSpeculativeRenderFrameHost(node)); |
221 | 308 |
222 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 309 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
223 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted( | 310 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted( |
224 response, MakeEmptyStream()); | 311 response, MakeEmptyStream()); |
225 RenderFrameHostImpl* pending_rfh = | 312 TestRenderFrameHost* speculative_rfh = GetSpeculativeRenderFrameHost(node); |
226 node->render_manager()->pending_frame_host(); | 313 ASSERT_TRUE(speculative_rfh); |
227 ASSERT_TRUE(pending_rfh); | 314 EXPECT_TRUE(DidRenderFrameHostRequestCommit(speculative_rfh)); |
228 EXPECT_NE(pending_rfh, rfh); | 315 EXPECT_FALSE(DidRenderFrameHostRequestCommit(main_test_rfh())); |
229 EXPECT_TRUE(pending_rfh->IsRenderFrameLive()); | 316 |
230 EXPECT_TRUE(pending_rfh->render_view_host()->IsRenderViewLive()); | 317 speculative_rfh->SendNavigate(0, kUrl2); |
| 318 |
| 319 RenderFrameHostImpl* final_rfh = main_test_rfh(); |
| 320 EXPECT_EQ(speculative_rfh, final_rfh); |
| 321 EXPECT_NE(initial_rfh, final_rfh); |
| 322 EXPECT_TRUE(final_rfh->IsRenderFrameLive()); |
| 323 EXPECT_TRUE(final_rfh->render_view_host()->IsRenderViewLive()); |
| 324 EXPECT_FALSE(GetSpeculativeRenderFrameHost(node)); |
231 } | 325 } |
232 | 326 |
233 // PlzNavigate: Test that redirects are followed. | 327 // PlzNavigate: Test that redirects are followed and the speculative |
| 328 // RenderFrameHost logic behaves as expected. |
234 TEST_F(NavigatorTestWithBrowserSideNavigation, RedirectCrossSite) { | 329 TEST_F(NavigatorTestWithBrowserSideNavigation, RedirectCrossSite) { |
235 const GURL kUrl1("http://www.chromium.org/"); | 330 const GURL kUrl1("http://www.chromium.org/"); |
236 const GURL kUrl2("http://www.google.com/"); | 331 const GURL kUrl2("http://www.google.com/"); |
237 | 332 |
238 contents()->NavigateAndCommit(kUrl1); | 333 contents()->NavigateAndCommit(kUrl1); |
239 RenderFrameHostImpl* rfh = main_test_rfh(); | 334 RenderFrameHostImpl* rfh = main_test_rfh(); |
240 EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, rfh->rfh_state()); | |
241 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); | 335 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); |
242 | 336 |
243 // Navigate to a URL on the same site. | 337 // Navigate to a URL on the same site. |
| 338 process()->sink().ClearMessages(); |
244 SendRequestNavigation(node, kUrl1); | 339 SendRequestNavigation(node, kUrl1); |
245 main_test_rfh()->SendBeginNavigationWithURL(kUrl1); | 340 main_test_rfh()->SendBeginNavigationWithURL(kUrl1); |
246 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node); | 341 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node); |
247 ASSERT_TRUE(main_request); | 342 ASSERT_TRUE(main_request); |
| 343 EXPECT_FALSE(GetSpeculativeRenderFrameHost(node)); |
248 | 344 |
249 // It then redirects to another site. | 345 // It then redirects to another site. |
250 net::RedirectInfo redirect_info; | 346 net::RedirectInfo redirect_info; |
251 redirect_info.status_code = 302; | 347 redirect_info.status_code = 302; |
252 redirect_info.new_method = "GET"; | 348 redirect_info.new_method = "GET"; |
253 redirect_info.new_url = kUrl2; | 349 redirect_info.new_url = kUrl2; |
254 redirect_info.new_first_party_for_cookies = kUrl2; | 350 redirect_info.new_first_party_for_cookies = kUrl2; |
255 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 351 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
256 GetLoaderForNavigationRequest(main_request)->CallOnRequestRedirected( | 352 GetLoaderForNavigationRequest(main_request)->CallOnRequestRedirected( |
257 redirect_info, response); | 353 redirect_info, response); |
258 | 354 |
259 // The redirect should have been followed. | 355 // The redirect should have been followed. |
260 EXPECT_EQ(1, GetLoaderForNavigationRequest(main_request)->redirect_count()); | 356 EXPECT_EQ(1, GetLoaderForNavigationRequest(main_request)->redirect_count()); |
| 357 EXPECT_FALSE(GetSpeculativeRenderFrameHost(node)); |
261 | 358 |
262 // Then it commits. | 359 // Request the RenderFrameHost to commit. |
263 response = new ResourceResponse; | 360 response = new ResourceResponse; |
264 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted( | 361 GetLoaderForNavigationRequest(main_request)->CallOnResponseStarted( |
265 response, MakeEmptyStream()); | 362 response, MakeEmptyStream()); |
266 RenderFrameHostImpl* pending_rfh = | 363 TestRenderFrameHost* final_speculative_rfh = |
267 node->render_manager()->pending_frame_host(); | 364 GetSpeculativeRenderFrameHost(node); |
268 ASSERT_TRUE(pending_rfh); | 365 EXPECT_TRUE(final_speculative_rfh); |
269 EXPECT_NE(pending_rfh, rfh); | 366 EXPECT_TRUE(DidRenderFrameHostRequestCommit(final_speculative_rfh)); |
270 EXPECT_TRUE(pending_rfh->IsRenderFrameLive()); | 367 |
271 EXPECT_TRUE(pending_rfh->render_view_host()->IsRenderViewLive()); | 368 // And commit provisional load. |
| 369 final_speculative_rfh->SendNavigate(0, kUrl2); |
| 370 RenderFrameHostImpl* final_rfh = main_test_rfh(); |
| 371 ASSERT_TRUE(final_rfh); |
| 372 EXPECT_NE(rfh, final_rfh); |
| 373 EXPECT_EQ(final_speculative_rfh, final_rfh); |
| 374 EXPECT_TRUE(final_rfh->IsRenderFrameLive()); |
| 375 EXPECT_TRUE(final_rfh->render_view_host()->IsRenderViewLive()); |
| 376 EXPECT_FALSE(GetSpeculativeRenderFrameHost(node)); |
272 } | 377 } |
273 | 378 |
274 // PlzNavigate: Test that a navigation is cancelled if another request has been | 379 // PlzNavigate: Test that a navigation is canceled if another request has been |
275 // issued in the meantime. | 380 // issued in the meantime. Also confirms that the speculative RenderFrameHost is |
| 381 // correctly updated in the process. |
276 TEST_F(NavigatorTestWithBrowserSideNavigation, ReplacePendingNavigation) { | 382 TEST_F(NavigatorTestWithBrowserSideNavigation, ReplacePendingNavigation) { |
277 const GURL kUrl0("http://www.wikipedia.org/"); | 383 const GURL kUrl0("http://www.wikipedia.org/"); |
278 const GURL kUrl0_site = SiteInstance::GetSiteForURL(browser_context(), kUrl0); | |
279 const GURL kUrl1("http://www.chromium.org/"); | 384 const GURL kUrl1("http://www.chromium.org/"); |
| 385 const GURL kUrl1_site = SiteInstance::GetSiteForURL(browser_context(), kUrl1); |
280 const GURL kUrl2("http://www.google.com/"); | 386 const GURL kUrl2("http://www.google.com/"); |
281 const GURL kUrl2_site = SiteInstance::GetSiteForURL(browser_context(), kUrl2); | 387 const GURL kUrl2_site = SiteInstance::GetSiteForURL(browser_context(), kUrl2); |
282 | 388 |
283 // Initialization. | 389 // Initialization. |
284 contents()->NavigateAndCommit(kUrl0); | 390 contents()->NavigateAndCommit(kUrl0); |
285 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); | 391 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); |
286 EXPECT_EQ(kUrl0_site, main_test_rfh()->GetSiteInstance()->GetSiteURL()); | |
287 | 392 |
288 // Request navigation to the 1st URL. | 393 // Request navigation to the 1st URL. |
| 394 process()->sink().ClearMessages(); |
289 SendRequestNavigation(node, kUrl1); | 395 SendRequestNavigation(node, kUrl1); |
290 main_test_rfh()->SendBeginNavigationWithURL(kUrl1); | 396 main_test_rfh()->SendBeginNavigationWithURL(kUrl1); |
291 NavigationRequest* request1 = GetNavigationRequestForFrameTreeNode(node); | 397 NavigationRequest* request1 = GetNavigationRequestForFrameTreeNode(node); |
292 ASSERT_TRUE(request1); | 398 ASSERT_TRUE(request1); |
293 EXPECT_EQ(kUrl1, request1->common_params().url); | 399 EXPECT_EQ(kUrl1, request1->common_params().url); |
294 base::WeakPtr<TestNavigationURLLoader> loader1 = | 400 base::WeakPtr<TestNavigationURLLoader> loader1 = |
295 GetLoaderForNavigationRequest(request1)->AsWeakPtr(); | 401 GetLoaderForNavigationRequest(request1)->AsWeakPtr(); |
296 | 402 |
| 403 // Confirm a speculative RFH was created. |
| 404 TestRenderFrameHost* speculative_rfh = GetSpeculativeRenderFrameHost(node); |
| 405 ASSERT_TRUE(speculative_rfh); |
| 406 int32 site_instance_id_1 = speculative_rfh->GetSiteInstance()->GetId(); |
| 407 EXPECT_EQ(kUrl1_site, speculative_rfh->GetSiteInstance()->GetSiteURL()); |
| 408 |
297 // Request navigation to the 2nd URL; the NavigationRequest must have been | 409 // Request navigation to the 2nd URL; the NavigationRequest must have been |
298 // replaced by a new one with a different URL. | 410 // replaced by a new one with a different URL. |
299 SendRequestNavigation(node, kUrl2); | 411 SendRequestNavigation(node, kUrl2); |
300 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); | 412 main_test_rfh()->SendBeginNavigationWithURL(kUrl2); |
301 NavigationRequest* request2 = GetNavigationRequestForFrameTreeNode(node); | 413 NavigationRequest* request2 = GetNavigationRequestForFrameTreeNode(node); |
302 ASSERT_TRUE(request2); | 414 ASSERT_TRUE(request2); |
303 EXPECT_EQ(kUrl2, request2->common_params().url); | 415 EXPECT_EQ(kUrl2, request2->common_params().url); |
304 | 416 |
305 // Confirm that the first loader got destroyed. | 417 // Confirm that the first loader got destroyed. |
306 EXPECT_FALSE(loader1); | 418 EXPECT_FALSE(loader1); |
307 | 419 |
308 // Confirm that the commit corresponds to the new request. | 420 // Confirm that a new speculative RFH was created. |
| 421 speculative_rfh = GetSpeculativeRenderFrameHost(node); |
| 422 ASSERT_TRUE(speculative_rfh); |
| 423 int32 site_instance_id_2 = speculative_rfh->GetSiteInstance()->GetId(); |
| 424 EXPECT_NE(site_instance_id_1, site_instance_id_2); |
| 425 |
| 426 // Request the RenderFrameHost to commit. |
309 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 427 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
310 GetLoaderForNavigationRequest(request2)->CallOnResponseStarted( | 428 GetLoaderForNavigationRequest(request2)->CallOnResponseStarted( |
311 response, MakeEmptyStream()); | 429 response, MakeEmptyStream()); |
312 RenderFrameHostImpl* pending_rfh = | 430 EXPECT_TRUE(DidRenderFrameHostRequestCommit(speculative_rfh)); |
313 node->render_manager()->pending_frame_host(); | 431 EXPECT_FALSE(DidRenderFrameHostRequestCommit(main_test_rfh())); |
314 ASSERT_TRUE(pending_rfh); | 432 |
315 EXPECT_EQ(kUrl2_site, pending_rfh->GetSiteInstance()->GetSiteURL()); | 433 // And commit provisional load. |
| 434 speculative_rfh->SendNavigate(0, kUrl2); |
| 435 |
| 436 // Confirm that the commit corresponds to the new request. |
| 437 ASSERT_TRUE(main_test_rfh()); |
| 438 EXPECT_EQ(kUrl2_site, main_test_rfh()->GetSiteInstance()->GetSiteURL()); |
| 439 EXPECT_EQ(kUrl2, contents()->GetLastCommittedURL()); |
| 440 |
| 441 // Confirm that the committed RFH is the latest speculative one. |
| 442 EXPECT_EQ(site_instance_id_2, main_test_rfh()->GetSiteInstance()->GetId()); |
316 } | 443 } |
317 | 444 |
318 // PlzNavigate: Test that a reload navigation is properly signaled to the | 445 // PlzNavigate: Test that a reload navigation is properly signaled to the |
319 // renderer when the navigation can commit. | 446 // RenderFrame when the navigation can commit. A speculative RenderFrameHost |
| 447 // should not be created at any step. |
320 TEST_F(NavigatorTestWithBrowserSideNavigation, Reload) { | 448 TEST_F(NavigatorTestWithBrowserSideNavigation, Reload) { |
321 const GURL kUrl("http://www.google.com/"); | 449 const GURL kUrl("http://www.google.com/"); |
322 contents()->NavigateAndCommit(kUrl); | 450 contents()->NavigateAndCommit(kUrl); |
323 | 451 |
324 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); | 452 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); |
325 SendRequestNavigationWithParameters( | 453 SendRequestNavigationWithParameters( |
326 node, kUrl, Referrer(), ui::PAGE_TRANSITION_LINK, | 454 node, kUrl, Referrer(), ui::PAGE_TRANSITION_LINK, |
327 NavigationController::RELOAD); | 455 NavigationController::RELOAD); |
328 contents()->GetMainFrame()->SendBeginNavigationWithURL(kUrl); | 456 main_test_rfh()->SendBeginNavigationWithURL(kUrl); |
329 // A NavigationRequest should have been generated. | 457 // A NavigationRequest should have been generated. |
330 NavigationRequest* main_request = | 458 NavigationRequest* main_request = |
331 GetNavigationRequestForFrameTreeNode(node); | 459 GetNavigationRequestForFrameTreeNode(node); |
332 ASSERT_TRUE(main_request != NULL); | 460 ASSERT_TRUE(main_request != NULL); |
333 EXPECT_EQ(FrameMsg_Navigate_Type::RELOAD, | 461 EXPECT_EQ(FrameMsg_Navigate_Type::RELOAD, |
334 main_request->common_params().navigation_type); | 462 main_request->common_params().navigation_type); |
335 int page_id = contents()->GetMaxPageIDForSiteInstance( | 463 EXPECT_FALSE(GetSpeculativeRenderFrameHost(node)); |
336 main_test_rfh()->GetSiteInstance()) + 1; | 464 |
337 main_test_rfh()->SendNavigate(page_id, kUrl); | 465 main_test_rfh()->SendNavigate(0, kUrl); |
| 466 EXPECT_FALSE(GetSpeculativeRenderFrameHost(node)); |
338 | 467 |
339 // Now do a shift+reload. | 468 // Now do a shift+reload. |
340 SendRequestNavigationWithParameters( | 469 SendRequestNavigationWithParameters( |
341 node, kUrl, Referrer(), ui::PAGE_TRANSITION_LINK, | 470 node, kUrl, Referrer(), ui::PAGE_TRANSITION_LINK, |
342 NavigationController::RELOAD_IGNORING_CACHE); | 471 NavigationController::RELOAD_IGNORING_CACHE); |
343 contents()->GetMainFrame()->SendBeginNavigationWithURL(kUrl); | 472 main_test_rfh()->SendBeginNavigationWithURL(kUrl); |
344 // A NavigationRequest should have been generated. | 473 // A NavigationRequest should have been generated. |
345 main_request = GetNavigationRequestForFrameTreeNode(node); | 474 main_request = GetNavigationRequestForFrameTreeNode(node); |
346 ASSERT_TRUE(main_request != NULL); | 475 ASSERT_TRUE(main_request != NULL); |
347 EXPECT_EQ(FrameMsg_Navigate_Type::RELOAD_IGNORING_CACHE, | 476 EXPECT_EQ(FrameMsg_Navigate_Type::RELOAD_IGNORING_CACHE, |
348 main_request->common_params().navigation_type); | 477 main_request->common_params().navigation_type); |
| 478 EXPECT_FALSE(GetSpeculativeRenderFrameHost(node)); |
| 479 } |
| 480 |
| 481 // PlzNavigate: Confirm that a speculative RenderFrameHost is used when |
| 482 // navigating from one site to another. |
| 483 TEST_F(NavigatorTestWithBrowserSideNavigation, |
| 484 SpeculativeRendererWorksBaseCase) { |
| 485 // Navigate to an initial site. |
| 486 const GURL kUrlInit("http://wikipedia.org/"); |
| 487 contents()->NavigateAndCommit(kUrlInit); |
| 488 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); |
| 489 |
| 490 // Begin navigating to another site. |
| 491 const GURL kUrl("http://google.com/"); |
| 492 process()->sink().ClearMessages(); |
| 493 SendRequestNavigation(node, kUrl); |
| 494 main_test_rfh()->SendBeginNavigationWithURL(kUrl); |
| 495 TestRenderFrameHost* speculative_rfh = GetSpeculativeRenderFrameHost(node); |
| 496 ASSERT_TRUE(speculative_rfh); |
| 497 EXPECT_NE(speculative_rfh, main_test_rfh()); |
| 498 EXPECT_EQ(SiteInstanceImpl::GetSiteForURL(browser_context(), kUrl), |
| 499 speculative_rfh->GetSiteInstance()->GetSiteURL()); |
| 500 EXPECT_FALSE(node->render_manager()->pending_frame_host()); |
| 501 int32 site_instance_id = speculative_rfh->GetSiteInstance()->GetId(); |
| 502 |
| 503 // Ask Navigator to commit the navigation by simulating a call to |
| 504 // OnResponseStarted. |
| 505 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 506 GetLoaderForNavigationRequest(GetNavigationRequestForFrameTreeNode(node)) |
| 507 ->CallOnResponseStarted(response, MakeEmptyStream()); |
| 508 speculative_rfh = GetSpeculativeRenderFrameHost(node); |
| 509 ASSERT_TRUE(speculative_rfh); |
| 510 EXPECT_TRUE(DidRenderFrameHostRequestCommit(speculative_rfh)); |
| 511 EXPECT_EQ(site_instance_id, speculative_rfh->GetSiteInstance()->GetId()); |
| 512 EXPECT_FALSE(node->render_manager()->pending_frame_host()); |
| 513 |
| 514 // Invoke OnDidCommitProvisionalLoad. |
| 515 speculative_rfh->SendNavigate(0, kUrl); |
| 516 EXPECT_EQ(site_instance_id, main_test_rfh()->GetSiteInstance()->GetId()); |
| 517 EXPECT_FALSE(GetSpeculativeRenderFrameHost(node)); |
| 518 EXPECT_FALSE(node->render_manager()->pending_frame_host()); |
| 519 } |
| 520 |
| 521 // PlzNavigate: Confirm that a speculative RenderFrameHost is thrown away when |
| 522 // the final URL's site differs from the initial one due to redirects. |
| 523 TEST_F(NavigatorTestWithBrowserSideNavigation, |
| 524 SpeculativeRendererDiscardedAfterRedirectToAnotherSite) { |
| 525 // Navigate to an initial site. |
| 526 const GURL kUrlInit("http://wikipedia.org/"); |
| 527 contents()->NavigateAndCommit(kUrlInit); |
| 528 FrameTreeNode* node = main_test_rfh()->frame_tree_node(); |
| 529 int32 init_site_instance_id = main_test_rfh()->GetSiteInstance()->GetId(); |
| 530 |
| 531 // Begin navigating to another site. |
| 532 const GURL kUrl("http://google.com/"); |
| 533 process()->sink().ClearMessages(); |
| 534 SendRequestNavigation(node, kUrl); |
| 535 main_test_rfh()->SendBeginNavigationWithURL(kUrl); |
| 536 TestRenderFrameHost* speculative_rfh = GetSpeculativeRenderFrameHost(node); |
| 537 int32 site_instance_id = speculative_rfh->GetSiteInstance()->GetId(); |
| 538 EXPECT_NE(init_site_instance_id, site_instance_id); |
| 539 EXPECT_EQ(init_site_instance_id, main_test_rfh()->GetSiteInstance()->GetId()); |
| 540 ASSERT_TRUE(speculative_rfh); |
| 541 EXPECT_NE(speculative_rfh, main_test_rfh()); |
| 542 EXPECT_EQ(SiteInstanceImpl::GetSiteForURL(browser_context(), kUrl), |
| 543 speculative_rfh->GetSiteInstance()->GetSiteURL()); |
| 544 |
| 545 // It then redirects to yet another site. |
| 546 NavigationRequest* main_request = GetNavigationRequestForFrameTreeNode(node); |
| 547 ASSERT_TRUE(main_request); |
| 548 const GURL kUrlRedirect("https://www.google.com/"); |
| 549 net::RedirectInfo redirect_info; |
| 550 redirect_info.status_code = 302; |
| 551 redirect_info.new_method = "GET"; |
| 552 redirect_info.new_url = kUrlRedirect; |
| 553 redirect_info.new_first_party_for_cookies = kUrlRedirect; |
| 554 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 555 GetLoaderForNavigationRequest(main_request) |
| 556 ->CallOnRequestRedirected(redirect_info, response); |
| 557 EXPECT_EQ(init_site_instance_id, main_test_rfh()->GetSiteInstance()->GetId()); |
| 558 speculative_rfh = GetSpeculativeRenderFrameHost(node); |
| 559 ASSERT_TRUE(speculative_rfh); |
| 560 |
| 561 // For now, ensure that the speculative RFH does not change after the |
| 562 // redirect. |
| 563 // TODO(carlosk): once the speculative RenderFrameHost updates with redirects |
| 564 // this next check will be changed to verify that it actually happens. |
| 565 EXPECT_EQ(site_instance_id, speculative_rfh->GetSiteInstance()->GetId()); |
| 566 |
| 567 // Commit the navigation with Navigator by simulating the call to |
| 568 // OnResponseStarted. |
| 569 response = new ResourceResponse; |
| 570 GetLoaderForNavigationRequest(main_request) |
| 571 ->CallOnResponseStarted(response, MakeEmptyStream()); |
| 572 speculative_rfh = GetSpeculativeRenderFrameHost(node); |
| 573 EXPECT_TRUE(DidRenderFrameHostRequestCommit(speculative_rfh)); |
| 574 EXPECT_EQ(init_site_instance_id, main_test_rfh()->GetSiteInstance()->GetId()); |
| 575 |
| 576 // Once commit happens the speculative RenderFrameHost is updated to match the |
| 577 // known final SiteInstance. |
| 578 ASSERT_TRUE(speculative_rfh); |
| 579 EXPECT_EQ(SiteInstanceImpl::GetSiteForURL(browser_context(), kUrlRedirect), |
| 580 speculative_rfh->GetSiteInstance()->GetSiteURL()); |
| 581 int32 redirect_site_instance_id = speculative_rfh->GetSiteInstance()->GetId(); |
| 582 EXPECT_NE(init_site_instance_id, redirect_site_instance_id); |
| 583 EXPECT_NE(site_instance_id, redirect_site_instance_id); |
| 584 |
| 585 // Invoke OnDidCommitProvisionalLoad. |
| 586 speculative_rfh->SendNavigate(0, kUrlRedirect); |
| 587 |
| 588 // Check that the speculative RenderFrameHost was swapped in. |
| 589 EXPECT_EQ(redirect_site_instance_id, |
| 590 main_test_rfh()->GetSiteInstance()->GetId()); |
| 591 EXPECT_FALSE(GetSpeculativeRenderFrameHost(node)); |
| 592 } |
| 593 |
| 594 // PlzNavigate: Verify that a previously swapped out RenderFrameHost is |
| 595 // correctly reused when spawning a speculative RenderFrameHost in a navigation |
| 596 // using the same SiteInstance. |
| 597 TEST_F(NavigatorTestWithBrowserSideNavigation, |
| 598 SpeculativeRendererReuseSwappedOutRFH) { |
| 599 // Navigate to an initial site. |
| 600 const GURL kUrl1("http://wikipedia.org/"); |
| 601 contents()->NavigateAndCommit(kUrl1); |
| 602 TestRenderFrameHost* rfh1 = main_test_rfh(); |
| 603 FrameTreeNode* node = rfh1->frame_tree_node(); |
| 604 RenderFrameHostManager* rfhm = node->render_manager(); |
| 605 |
| 606 // Increment active frame count to cause the RenderFrameHost to be swapped out |
| 607 // (instead of immediately destroyed). |
| 608 rfh1->GetSiteInstance()->increment_active_frame_count(); |
| 609 |
| 610 // Navigate to another site to swap out the initial RenderFrameHost. |
| 611 const GURL kUrl2("http://chromium.org/"); |
| 612 contents()->NavigateAndCommit(kUrl2); |
| 613 ASSERT_NE(rfh1, main_test_rfh()); |
| 614 EXPECT_NE(RenderFrameHostImpl::STATE_DEFAULT, rfh1->rfh_state()); |
| 615 EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, main_test_rfh()->rfh_state()); |
| 616 EXPECT_TRUE(rfhm->IsOnSwappedOutList(rfh1)); |
| 617 |
| 618 // Now go back to the initial site so that the swapped out RenderFrameHost |
| 619 // should be reused. |
| 620 process()->sink().ClearMessages(); |
| 621 static_cast<MockRenderProcessHost*>(rfh1->GetProcess()) |
| 622 ->sink() |
| 623 .ClearMessages(); |
| 624 SendRequestNavigation(node, kUrl1); |
| 625 main_test_rfh()->SendBeginNavigationWithURL(kUrl1); |
| 626 EXPECT_EQ(rfh1, GetSpeculativeRenderFrameHost(node)); |
| 627 EXPECT_NE(RenderFrameHostImpl::STATE_DEFAULT, |
| 628 GetSpeculativeRenderFrameHost(node)->rfh_state()); |
| 629 |
| 630 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 631 GetLoaderForNavigationRequest(GetNavigationRequestForFrameTreeNode(node)) |
| 632 ->CallOnResponseStarted(response, MakeEmptyStream()); |
| 633 EXPECT_EQ(rfh1, GetSpeculativeRenderFrameHost(node)); |
| 634 EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, |
| 635 GetSpeculativeRenderFrameHost(node)->rfh_state()); |
| 636 EXPECT_TRUE(DidRenderFrameHostRequestCommit(rfh1)); |
| 637 EXPECT_FALSE(DidRenderFrameHostRequestCommit(main_test_rfh())); |
| 638 |
| 639 rfh1->SendNavigate(1, kUrl1); |
| 640 EXPECT_EQ(rfh1, main_test_rfh()); |
| 641 EXPECT_EQ(RenderFrameHostImpl::STATE_DEFAULT, rfh1->rfh_state()); |
| 642 EXPECT_FALSE(rfhm->IsOnSwappedOutList(rfh1)); |
349 } | 643 } |
350 | 644 |
351 } // namespace content | 645 } // namespace content |
OLD | NEW |