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

Side by Side Diff: content/browser/frame_host/render_frame_host_manager_unittest.cc

Issue 88503002: Have the unload event execute in background on cross-site navigations (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added unit tests 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/strings/utf_string_conversions.h" 5 #include "base/strings/utf_string_conversions.h"
6 #include "content/browser/frame_host/navigation_controller_impl.h" 6 #include "content/browser/frame_host/navigation_controller_impl.h"
7 #include "content/browser/frame_host/navigation_entry_impl.h" 7 #include "content/browser/frame_host/navigation_entry_impl.h"
8 #include "content/browser/frame_host/render_frame_host_manager.h" 8 #include "content/browser/frame_host/render_frame_host_manager.h"
9 #include "content/browser/site_instance_impl.h" 9 #include "content/browser/site_instance_impl.h"
10 #include "content/browser/webui/web_ui_controller_factory_registry.h" 10 #include "content/browser/webui/web_ui_controller_factory_registry.h"
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 increment_active_view_count(); 162 increment_active_view_count();
163 163
164 TestRenderViewHost* dest_rvh = static_cast<TestRenderViewHost*>( 164 TestRenderViewHost* dest_rvh = static_cast<TestRenderViewHost*>(
165 contents()->GetRenderManagerForTesting()->pending_render_view_host()); 165 contents()->GetRenderManagerForTesting()->pending_render_view_host());
166 CHECK(dest_rvh); 166 CHECK(dest_rvh);
167 EXPECT_NE(ntp_rvh, dest_rvh); 167 EXPECT_NE(ntp_rvh, dest_rvh);
168 168
169 // BeforeUnload finishes. 169 // BeforeUnload finishes.
170 ntp_rvh->SendShouldCloseACK(true); 170 ntp_rvh->SendShouldCloseACK(true);
171 171
172 // Assume SwapOutACK times out, so the dest_rvh proceeds and commits.
173 dest_rvh->SendNavigate(101, kDestUrl); 172 dest_rvh->SendNavigate(101, kDestUrl);
173 ntp_rvh->OnSwappedOut(false);
174 174
175 EXPECT_TRUE(ntp_rvh->is_swapped_out()); 175 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_SWAPPED_OUT, ntp_rvh->rvh_state());
176 return ntp_rvh; 176 return ntp_rvh;
177 } 177 }
178 178
179 private: 179 private:
180 RenderFrameHostManagerTestWebUIControllerFactory factory_; 180 RenderFrameHostManagerTestWebUIControllerFactory factory_;
181 }; 181 };
182 182
183 // Tests that when you navigate from a chrome:// url to another page, and 183 // Tests that when you navigate from a chrome:// url to another page, and
184 // then do that same thing in another tab, that the two resulting pages have 184 // then do that same thing in another tab, that the two resulting pages have
185 // different SiteInstances, BrowsingInstances, and RenderProcessHosts. This is 185 // different SiteInstances, BrowsingInstances, and RenderProcessHosts. This is
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 289
290 // The new RVH should be able to update its title. 290 // The new RVH should be able to update its title.
291 const base::string16 dest_title = base::ASCIIToUTF16("Google"); 291 const base::string16 dest_title = base::ASCIIToUTF16("Google");
292 EXPECT_TRUE(dest_rvh->OnMessageReceived( 292 EXPECT_TRUE(dest_rvh->OnMessageReceived(
293 ViewHostMsg_UpdateTitle(rvh()->GetRoutingID(), 101, dest_title, 293 ViewHostMsg_UpdateTitle(rvh()->GetRoutingID(), 101, dest_title,
294 direction))); 294 direction)));
295 EXPECT_EQ(dest_title, contents()->GetTitle()); 295 EXPECT_EQ(dest_title, contents()->GetTitle());
296 296
297 // The old renderer, being slow, now updates the title. It should be filtered 297 // The old renderer, being slow, now updates the title. It should be filtered
298 // out and not take effect. 298 // out and not take effect.
299 EXPECT_TRUE(ntp_rvh->is_swapped_out()); 299 EXPECT_NE(RenderViewHostImpl::RVH_STATE_LIVE, ntp_rvh->rvh_state());
300 EXPECT_TRUE(ntp_rvh->OnMessageReceived( 300 EXPECT_TRUE(ntp_rvh->OnMessageReceived(
301 ViewHostMsg_UpdateTitle(rvh()->GetRoutingID(), 0, ntp_title, direction))); 301 ViewHostMsg_UpdateTitle(rvh()->GetRoutingID(), 0, ntp_title, direction)));
302 EXPECT_EQ(dest_title, contents()->GetTitle()); 302 EXPECT_EQ(dest_title, contents()->GetTitle());
303 303
304 // We cannot filter out synchronous IPC messages, because the renderer would 304 // We cannot filter out synchronous IPC messages, because the renderer would
305 // be left waiting for a reply. We pick RunBeforeUnloadConfirm as an example 305 // be left waiting for a reply. We pick RunBeforeUnloadConfirm as an example
306 // that can run easily within a unit test, and that needs to receive a reply 306 // that can run easily within a unit test, and that needs to receive a reply
307 // without showing an actual dialog. 307 // without showing an actual dialog.
308 MockRenderProcessHost* ntp_process_host = 308 MockRenderProcessHost* ntp_process_host =
309 static_cast<MockRenderProcessHost*>(ntp_rvh->GetProcess()); 309 static_cast<MockRenderProcessHost*>(ntp_rvh->GetProcess());
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 ViewHostMsg_DidActivateAcceleratedCompositing msg( 354 ViewHostMsg_DidActivateAcceleratedCompositing msg(
355 rvh()->GetRoutingID(), true); 355 rvh()->GetRoutingID(), true);
356 EXPECT_TRUE(swapped_out_rvh->OnMessageReceived(msg)); 356 EXPECT_TRUE(swapped_out_rvh->OnMessageReceived(msg));
357 EXPECT_TRUE(swapped_out_rvh->is_accelerated_compositing_active()); 357 EXPECT_TRUE(swapped_out_rvh->is_accelerated_compositing_active());
358 } 358 }
359 359
360 // Test if RenderViewHost::GetRenderWidgetHosts() only returns active 360 // Test if RenderViewHost::GetRenderWidgetHosts() only returns active
361 // widgets. 361 // widgets.
362 TEST_F(RenderFrameHostManagerTest, GetRenderWidgetHostsReturnsActiveViews) { 362 TEST_F(RenderFrameHostManagerTest, GetRenderWidgetHostsReturnsActiveViews) {
363 TestRenderViewHost* swapped_out_rvh = CreateSwappedOutRenderViewHost(); 363 TestRenderViewHost* swapped_out_rvh = CreateSwappedOutRenderViewHost();
364 EXPECT_TRUE(swapped_out_rvh->is_swapped_out()); 364 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_SWAPPED_OUT,
365 swapped_out_rvh->rvh_state());
365 366
366 scoped_ptr<RenderWidgetHostIterator> widgets( 367 scoped_ptr<RenderWidgetHostIterator> widgets(
367 RenderWidgetHost::GetRenderWidgetHosts()); 368 RenderWidgetHost::GetRenderWidgetHosts());
368 // We know that there is the only one active widget. Another view is 369 // We know that there is the only one active widget. Another view is
369 // now swapped out, so the swapped out view is not included in the 370 // now swapped out, so the swapped out view is not included in the
370 // list. 371 // list.
371 RenderWidgetHost* widget = widgets->GetNextHost(); 372 RenderWidgetHost* widget = widgets->GetNextHost();
372 EXPECT_FALSE(widgets->GetNextHost()); 373 EXPECT_FALSE(widgets->GetNextHost());
373 RenderViewHost* rvh = RenderViewHost::From(widget); 374 RenderViewHost* rvh = RenderViewHost::From(widget);
374 EXPECT_FALSE(static_cast<RenderViewHostImpl*>(rvh)->is_swapped_out()); 375 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_LIVE,
376 static_cast<RenderViewHostImpl*>(rvh)->rvh_state());
375 } 377 }
376 378
377 // Test if RenderViewHost::GetRenderWidgetHosts() returns a subset of 379 // Test if RenderViewHost::GetRenderWidgetHosts() returns a subset of
378 // RenderViewHostImpl::GetAllRenderWidgetHosts(). 380 // RenderViewHostImpl::GetAllRenderWidgetHosts().
379 // RenderViewHost::GetRenderWidgetHosts() returns only active widgets, but 381 // RenderViewHost::GetRenderWidgetHosts() returns only active widgets, but
380 // RenderViewHostImpl::GetAllRenderWidgetHosts() returns everything 382 // RenderViewHostImpl::GetAllRenderWidgetHosts() returns everything
381 // including swapped out ones. 383 // including swapped out ones.
382 TEST_F(RenderFrameHostManagerTest, 384 TEST_F(RenderFrameHostManagerTest,
383 GetRenderWidgetHostsWithinGetAllRenderWidgetHosts) { 385 GetRenderWidgetHostsWithinGetAllRenderWidgetHosts) {
384 TestRenderViewHost* swapped_out_rvh = CreateSwappedOutRenderViewHost(); 386 TestRenderViewHost* swapped_out_rvh = CreateSwappedOutRenderViewHost();
385 EXPECT_TRUE(swapped_out_rvh->is_swapped_out()); 387 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_SWAPPED_OUT,
388 swapped_out_rvh->rvh_state());
386 389
387 scoped_ptr<RenderWidgetHostIterator> widgets( 390 scoped_ptr<RenderWidgetHostIterator> widgets(
388 RenderWidgetHost::GetRenderWidgetHosts()); 391 RenderWidgetHost::GetRenderWidgetHosts());
389 392
390 while (RenderWidgetHost* w = widgets->GetNextHost()) { 393 while (RenderWidgetHost* w = widgets->GetNextHost()) {
391 bool found = false; 394 bool found = false;
392 scoped_ptr<RenderWidgetHostIterator> all_widgets( 395 scoped_ptr<RenderWidgetHostIterator> all_widgets(
393 RenderWidgetHostImpl::GetAllRenderWidgetHosts()); 396 RenderWidgetHostImpl::GetAllRenderWidgetHosts());
394 while (RenderWidgetHost* widget = all_widgets->GetNextHost()) { 397 while (RenderWidgetHost* widget = all_widgets->GetNextHost()) {
395 if (w == widget) { 398 if (w == widget) {
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after
761 static_cast<TestRenderViewHost*>(host->render_view_host()); 764 static_cast<TestRenderViewHost*>(host->render_view_host());
762 MockRenderProcessHost* test_process_host = 765 MockRenderProcessHost* test_process_host =
763 static_cast<MockRenderProcessHost*>(test_host->GetProcess()); 766 static_cast<MockRenderProcessHost*>(test_host->GetProcess());
764 EXPECT_TRUE(test_process_host->sink().GetUniqueMessageMatching( 767 EXPECT_TRUE(test_process_host->sink().GetUniqueMessageMatching(
765 ViewMsg_ShouldClose::ID)); 768 ViewMsg_ShouldClose::ID));
766 test_host->SendShouldCloseACK(true); 769 test_host->SendShouldCloseACK(true);
767 770
768 // CrossSiteResourceHandler::StartCrossSiteTransition triggers a 771 // CrossSiteResourceHandler::StartCrossSiteTransition triggers a
769 // call of RenderFrameHostManager::SwapOutOldPage before 772 // call of RenderFrameHostManager::SwapOutOldPage before
770 // RenderFrameHostManager::DidNavigateMainFrame is called. 773 // RenderFrameHostManager::DidNavigateMainFrame is called.
771 // The RVH is not swapped out until the commit. 774 // The RVH is swapped out after receiving the unload ack.
772 manager->SwapOutOldPage(); 775 manager->SwapOutOldPage();
773 EXPECT_TRUE(test_process_host->sink().GetUniqueMessageMatching( 776 EXPECT_TRUE(test_process_host->sink().GetUniqueMessageMatching(
774 ViewMsg_SwapOut::ID)); 777 ViewMsg_SwapOut::ID));
775 test_host->OnSwappedOut(false); 778 test_host->OnSwappedOut(false);
776 779
777 EXPECT_EQ(host, manager->current_frame_host()); 780 EXPECT_EQ(host, manager->current_frame_host());
778 EXPECT_FALSE(manager->current_frame_host()->is_swapped_out()); 781 EXPECT_FALSE(manager->current_frame_host()->is_swapped_out());
779 EXPECT_EQ(host2, manager->pending_frame_host()); 782 EXPECT_EQ(host2, manager->pending_frame_host());
780 // There should be still no navigation messages being sent. 783 // There should be still no navigation messages being sent.
781 EXPECT_FALSE(test_process_host2->sink().GetUniqueMessageMatching( 784 EXPECT_FALSE(test_process_host2->sink().GetUniqueMessageMatching(
782 ViewMsg_Navigate::ID)); 785 ViewMsg_Navigate::ID));
783 786
784 // 3) Cross-site navigate to next site before 2) has committed. -------------- 787 // 3) Cross-site navigate to next site before 2) has committed. --------------
785 const GURL kUrl3("http://webkit.org/"); 788 const GURL kUrl3("http://webkit.org/");
786 NavigationEntryImpl entry3(NULL /* instance */, -1 /* page_id */, kUrl3, 789 NavigationEntryImpl entry3(NULL /* instance */, -1 /* page_id */, kUrl3,
787 Referrer(), base::string16() /* title */, 790 Referrer(), base::string16() /* title */,
788 PAGE_TRANSITION_TYPED, 791 PAGE_TRANSITION_TYPED,
789 false /* is_renderer_init */); 792 false /* is_renderer_init */);
790 test_process_host->sink().ClearMessages(); 793 test_process_host->sink().ClearMessages();
791 RenderFrameHostImpl* host3 = manager->Navigate(entry3); 794 RenderFrameHostImpl* host3 = manager->Navigate(entry3);
792 795
793 // A new RenderFrameHost should be created. host2 is now deleted. 796 // A new RenderFrameHost should be created. host2 is now deleted.
794 EXPECT_TRUE(manager->pending_frame_host()); 797 EXPECT_TRUE(manager->pending_frame_host());
795 ASSERT_EQ(host3, manager->pending_frame_host()); 798 ASSERT_EQ(host3, manager->pending_frame_host());
796 EXPECT_NE(host3, host); 799 EXPECT_NE(host3, host);
797 EXPECT_NE(host3->GetProcess()->GetID(), host2_process_id); 800 EXPECT_NE(host3->GetProcess()->GetID(), host2_process_id);
798 801
799 // Navigations in the new RVH should be suspended, which is ok because the 802 // Navigations in the new RVH should be suspended.
800 // old RVH is not yet swapped out and can respond to a second beforeunload
801 // request.
802 EXPECT_TRUE(static_cast<RenderViewHostImpl*>( 803 EXPECT_TRUE(static_cast<RenderViewHostImpl*>(
803 host3->render_view_host())->are_navigations_suspended()); 804 host3->render_view_host())->are_navigations_suspended());
804 EXPECT_EQ(host, manager->current_frame_host()); 805 EXPECT_EQ(host, manager->current_frame_host());
805 EXPECT_FALSE(manager->current_frame_host()->is_swapped_out()); 806 EXPECT_FALSE(manager->current_frame_host()->is_swapped_out());
806 807
807 // Simulate a response to the second beforeunload request. 808 // Simulate a response to the second beforeunload request.
808 EXPECT_TRUE(test_process_host->sink().GetUniqueMessageMatching( 809 EXPECT_TRUE(test_process_host->sink().GetUniqueMessageMatching(
809 ViewMsg_ShouldClose::ID)); 810 ViewMsg_ShouldClose::ID));
810 test_host->SendShouldCloseACK(true); 811 test_host->SendShouldCloseACK(true);
811 812
812 // CrossSiteResourceHandler::StartCrossSiteTransition triggers a 813 // CrossSiteResourceHandler::StartCrossSiteTransition triggers a
813 // call of RenderFrameHostManager::SwapOutOldPage before 814 // call of RenderFrameHostManager::SwapOutOldPage before
814 // RenderFrameHostManager::DidNavigateMainFrame is called. 815 // RenderFrameHostManager::DidNavigateMainFrame is called.
815 // The RVH is not swapped out until the commit.
816 manager->SwapOutOldPage(); 816 manager->SwapOutOldPage();
817 EXPECT_TRUE(test_process_host->sink().GetUniqueMessageMatching( 817 EXPECT_TRUE(test_process_host->sink().GetUniqueMessageMatching(
818 ViewMsg_SwapOut::ID)); 818 ViewMsg_SwapOut::ID));
819 test_host->OnSwappedOut(false); 819 test_host->OnSwappedOut(false);
820 820
821 // Commit. 821 // Commit.
822 manager->DidNavigateMainFrame(host3->render_view_host()); 822 manager->DidNavigateMainFrame(host3->render_view_host());
823 EXPECT_TRUE(host3 == manager->current_frame_host()); 823 EXPECT_TRUE(host3 == manager->current_frame_host());
824 ASSERT_TRUE(host3); 824 ASSERT_TRUE(host3);
825 EXPECT_TRUE(static_cast<SiteInstanceImpl*>(host3->GetSiteInstance())-> 825 EXPECT_TRUE(static_cast<SiteInstanceImpl*>(host3->GetSiteInstance())->
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
1018 increment_active_view_count(); 1018 increment_active_view_count();
1019 1019
1020 // Now go back, but suppose the SwapOut_ACK isn't received. This shouldn't 1020 // Now go back, but suppose the SwapOut_ACK isn't received. This shouldn't
1021 // happen, but we have seen it when going back quickly across many entries 1021 // happen, but we have seen it when going back quickly across many entries
1022 // (http://crbug.com/93427). 1022 // (http://crbug.com/93427).
1023 contents()->GetController().GoBack(); 1023 contents()->GetController().GoBack();
1024 EXPECT_TRUE(rvh2->is_waiting_for_beforeunload_ack()); 1024 EXPECT_TRUE(rvh2->is_waiting_for_beforeunload_ack());
1025 contents()->ProceedWithCrossSiteNavigation(); 1025 contents()->ProceedWithCrossSiteNavigation();
1026 EXPECT_FALSE(rvh2->is_waiting_for_beforeunload_ack()); 1026 EXPECT_FALSE(rvh2->is_waiting_for_beforeunload_ack());
1027 rvh2->SwapOut(); 1027 rvh2->SwapOut();
1028 EXPECT_TRUE(rvh2->is_waiting_for_unload_ack()); 1028 EXPECT_TRUE(rvh2->IsWaitingForUnloadACK());
1029 1029
1030 // The back navigation commits. We should proactively clear the 1030 // The back navigation commits.
1031 // is_waiting_for_unload_ack state to be safe.
1032 const NavigationEntry* entry1 = contents()->GetController().GetPendingEntry(); 1031 const NavigationEntry* entry1 = contents()->GetController().GetPendingEntry();
1033 rvh1->SendNavigate(entry1->GetPageID(), entry1->GetURL()); 1032 rvh1->SendNavigate(entry1->GetPageID(), entry1->GetURL());
1034 EXPECT_TRUE(rvh2->is_swapped_out()); 1033 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_PENDING_SWAP_OUT, rvh2->rvh_state());
1035 EXPECT_FALSE(rvh2->is_waiting_for_unload_ack());
1036 1034
1037 // We should be able to navigate forward. 1035 // We should be able to navigate forward.
1038 contents()->GetController().GoForward(); 1036 contents()->GetController().GoForward();
1039 contents()->ProceedWithCrossSiteNavigation(); 1037 contents()->ProceedWithCrossSiteNavigation();
1040 const NavigationEntry* entry2 = contents()->GetController().GetPendingEntry(); 1038 const NavigationEntry* entry2 = contents()->GetController().GetPendingEntry();
1041 rvh2->SendNavigate(entry2->GetPageID(), entry2->GetURL()); 1039 rvh2->SendNavigate(entry2->GetPageID(), entry2->GetURL());
1042 EXPECT_EQ(rvh2, rvh()); 1040 EXPECT_EQ(rvh2, rvh());
1043 EXPECT_FALSE(rvh2->is_swapped_out()); 1041 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_LIVE, rvh2->rvh_state());
1044 EXPECT_TRUE(rvh1->is_swapped_out()); 1042 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_PENDING_SWAP_OUT, rvh1->rvh_state());
1045 } 1043 }
1046 1044
1047 // Test that we create swapped out RVHs for the opener chain when navigating an 1045 // Test that we create swapped out RVHs for the opener chain when navigating an
1048 // opened tab cross-process. This allows us to support certain cross-process 1046 // opened tab cross-process. This allows us to support certain cross-process
1049 // JavaScript calls (http://crbug.com/99202). 1047 // JavaScript calls (http://crbug.com/99202).
1050 TEST_F(RenderFrameHostManagerTest, CreateSwappedOutOpenerRVHs) { 1048 TEST_F(RenderFrameHostManagerTest, CreateSwappedOutOpenerRVHs) {
1051 const GURL kUrl1("http://www.google.com/"); 1049 const GURL kUrl1("http://www.google.com/");
1052 const GURL kUrl2("http://www.chromium.org/"); 1050 const GURL kUrl2("http://www.chromium.org/");
1053 const GURL kChromeUrl("chrome://foo"); 1051 const GURL kChromeUrl("chrome://foo");
1054 1052
(...skipping 26 matching lines...) Expand all
1081 1079
1082 // Ensure rvh1 is placed on swapped out list of the current tab. 1080 // Ensure rvh1 is placed on swapped out list of the current tab.
1083 EXPECT_TRUE(manager->IsRVHOnSwappedOutList(rvh1)); 1081 EXPECT_TRUE(manager->IsRVHOnSwappedOutList(rvh1));
1084 EXPECT_EQ(rvh1, 1082 EXPECT_EQ(rvh1,
1085 manager->GetSwappedOutRenderViewHost(rvh1->GetSiteInstance())); 1083 manager->GetSwappedOutRenderViewHost(rvh1->GetSiteInstance()));
1086 1084
1087 // Ensure a swapped out RVH is created in the first opener tab. 1085 // Ensure a swapped out RVH is created in the first opener tab.
1088 TestRenderViewHost* opener1_rvh = static_cast<TestRenderViewHost*>( 1086 TestRenderViewHost* opener1_rvh = static_cast<TestRenderViewHost*>(
1089 opener1_manager->GetSwappedOutRenderViewHost(rvh2->GetSiteInstance())); 1087 opener1_manager->GetSwappedOutRenderViewHost(rvh2->GetSiteInstance()));
1090 EXPECT_TRUE(opener1_manager->IsRVHOnSwappedOutList(opener1_rvh)); 1088 EXPECT_TRUE(opener1_manager->IsRVHOnSwappedOutList(opener1_rvh));
1091 EXPECT_TRUE(opener1_rvh->is_swapped_out()); 1089 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_SWAPPED_OUT,
1090 opener1_rvh->rvh_state());
1092 1091
1093 // Ensure a swapped out RVH is created in the second opener tab. 1092 // Ensure a swapped out RVH is created in the second opener tab.
1094 TestRenderViewHost* opener2_rvh = static_cast<TestRenderViewHost*>( 1093 TestRenderViewHost* opener2_rvh = static_cast<TestRenderViewHost*>(
1095 opener2_manager->GetSwappedOutRenderViewHost(rvh2->GetSiteInstance())); 1094 opener2_manager->GetSwappedOutRenderViewHost(rvh2->GetSiteInstance()));
1096 EXPECT_TRUE(opener2_manager->IsRVHOnSwappedOutList(opener2_rvh)); 1095 EXPECT_TRUE(opener2_manager->IsRVHOnSwappedOutList(opener2_rvh));
1097 EXPECT_TRUE(opener2_rvh->is_swapped_out()); 1096 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_SWAPPED_OUT,
1097 opener2_rvh->rvh_state());
1098 1098
1099 // Navigate to a cross-BrowsingInstance URL. 1099 // Navigate to a cross-BrowsingInstance URL.
1100 contents()->NavigateAndCommit(kChromeUrl); 1100 contents()->NavigateAndCommit(kChromeUrl);
1101 TestRenderViewHost* rvh3 = test_rvh(); 1101 TestRenderViewHost* rvh3 = test_rvh();
1102 EXPECT_NE(rvh1->GetSiteInstance(), rvh3->GetSiteInstance()); 1102 EXPECT_NE(rvh1->GetSiteInstance(), rvh3->GetSiteInstance());
1103 EXPECT_FALSE(rvh1->GetSiteInstance()->IsRelatedSiteInstance( 1103 EXPECT_FALSE(rvh1->GetSiteInstance()->IsRelatedSiteInstance(
1104 rvh3->GetSiteInstance())); 1104 rvh3->GetSiteInstance()));
1105 1105
1106 // No scripting is allowed across BrowsingInstances, so we should not create 1106 // No scripting is allowed across BrowsingInstances, so we should not create
1107 // swapped out RVHs for the opener chain in this case. 1107 // swapped out RVHs for the opener chain in this case.
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
1189 contents()->NavigateAndCommit(kPluginUrl); 1189 contents()->NavigateAndCommit(kPluginUrl);
1190 TestRenderViewHost* rvh2 = test_rvh(); 1190 TestRenderViewHost* rvh2 = test_rvh();
1191 EXPECT_NE(rvh1->GetSiteInstance(), rvh2->GetSiteInstance()); 1191 EXPECT_NE(rvh1->GetSiteInstance(), rvh2->GetSiteInstance());
1192 EXPECT_TRUE(rvh1->GetSiteInstance()->IsRelatedSiteInstance( 1192 EXPECT_TRUE(rvh1->GetSiteInstance()->IsRelatedSiteInstance(
1193 rvh2->GetSiteInstance())); 1193 rvh2->GetSiteInstance()));
1194 1194
1195 // Ensure a swapped out RVH is created in the first opener tab. 1195 // Ensure a swapped out RVH is created in the first opener tab.
1196 TestRenderViewHost* opener1_rvh = static_cast<TestRenderViewHost*>( 1196 TestRenderViewHost* opener1_rvh = static_cast<TestRenderViewHost*>(
1197 opener1_manager->GetSwappedOutRenderViewHost(rvh2->GetSiteInstance())); 1197 opener1_manager->GetSwappedOutRenderViewHost(rvh2->GetSiteInstance()));
1198 EXPECT_TRUE(opener1_manager->IsRVHOnSwappedOutList(opener1_rvh)); 1198 EXPECT_TRUE(opener1_manager->IsRVHOnSwappedOutList(opener1_rvh));
1199 EXPECT_TRUE(opener1_rvh->is_swapped_out()); 1199 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_SWAPPED_OUT,
1200 opener1_rvh->rvh_state());
1200 1201
1201 // Ensure the new RVH has WebUI bindings. 1202 // Ensure the new RVH has WebUI bindings.
1202 EXPECT_TRUE(rvh2->GetEnabledBindings() & BINDINGS_POLICY_WEB_UI); 1203 EXPECT_TRUE(rvh2->GetEnabledBindings() & BINDINGS_POLICY_WEB_UI);
1203 } 1204 }
1204 1205
1205 // Test that we reuse the same guest SiteInstance if we navigate across sites. 1206 // Test that we reuse the same guest SiteInstance if we navigate across sites.
1206 TEST_F(RenderFrameHostManagerTest, NoSwapOnGuestNavigations) { 1207 TEST_F(RenderFrameHostManagerTest, NoSwapOnGuestNavigations) {
1207 TestNotificationTracker notifications; 1208 TestNotificationTracker notifications;
1208 1209
1209 GURL guest_url(std::string(kGuestScheme).append("://abc123")); 1210 GURL guest_url(std::string(kGuestScheme).append("://abc123"));
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
1335 notifications.ListenFor(NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, 1336 notifications.ListenFor(NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
1336 Source<RenderWidgetHost>(host2->render_view_host())); 1337 Source<RenderWidgetHost>(host2->render_view_host()));
1337 manager->ShouldClosePage(false, true, base::TimeTicks()); 1338 manager->ShouldClosePage(false, true, base::TimeTicks());
1338 1339
1339 EXPECT_TRUE( 1340 EXPECT_TRUE(
1340 notifications.Check1AndReset(NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED)); 1341 notifications.Check1AndReset(NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED));
1341 EXPECT_FALSE(manager->pending_frame_host()); 1342 EXPECT_FALSE(manager->pending_frame_host());
1342 EXPECT_EQ(host, manager->current_frame_host()); 1343 EXPECT_EQ(host, manager->current_frame_host());
1343 } 1344 }
1344 1345
1346 // This checks that the given RVH has been properly deleted.
1347 class RenderViewHostDestructionChecker : public WebContentsObserver {
1348 public:
1349 RenderViewHostDestructionChecker(RenderViewHost* render_view_host)
1350 : WebContentsObserver(WebContents::FromRenderViewHost(render_view_host)),
1351 render_view_host_(render_view_host),
1352 rvh_deleted_(false) {}
1353
1354 bool rvh_deleted() { return rvh_deleted_; }
1355
1356 virtual void RenderViewDeleted(RenderViewHost* render_view_host) OVERRIDE {
1357 if (render_view_host == render_view_host_)
1358 rvh_deleted_ = true;
1359 }
1360
1361 private:
1362 RenderViewHost* render_view_host_;
1363 bool rvh_deleted_;
1364
1365 DISALLOW_COPY_AND_ASSIGN(RenderViewHostDestructionChecker);
1366 };
1367
1368 // Tests that the RenderViewHost is properly swapped out or deleted when the
1369 // SwapOutACK is received before the new page commits.
1370 TEST_F(RenderFrameHostManagerTest, SwapOutACKBeforeNewPageCommits) {
1371 const GURL kUrl1("http://www.google.com/");
1372 const GURL kUrl2("http://www.chromium.org/");
1373
1374 // Navigate to the first page.
1375 contents()->NavigateAndCommit(kUrl1);
1376 TestRenderViewHost* rvh1 = test_rvh();
1377 RenderViewHostDestructionChecker destruction_checker1(rvh1);
1378 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_LIVE, rvh1->rvh_state());
1379
1380 // Navigate to new site, simulating onbeforeunload approval.
1381 controller().LoadURL(kUrl2, Referrer(), PAGE_TRANSITION_LINK, std::string());
1382 base::TimeTicks now = base::TimeTicks::Now();
1383 rvh1->OnMessageReceived(ViewHostMsg_ShouldClose_ACK(0, true, now, now));
1384 EXPECT_TRUE(contents()->cross_navigation_pending());
1385 TestRenderViewHost* rvh2 =
1386 static_cast<TestRenderViewHost*>(contents()->GetPendingRenderViewHost());
1387
1388 // Simulate rvh2's response, which leads to an unload request being sent to
1389 // rvh1.
1390 std::vector<GURL> url_chain1;
1391 url_chain1.push_back(GURL());
1392 contents()->GetRenderManagerForTesting()->OnCrossSiteResponse(
1393 rvh2,
1394 GlobalRequestID(0, 0),
1395 false,
1396 url_chain1,
1397 Referrer(),
1398 PAGE_TRANSITION_TYPED,
1399 1,
1400 false);
1401 EXPECT_TRUE(contents()->cross_navigation_pending());
1402 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_WAITING_FOR_UNLOAD_ACK,
1403 rvh1->rvh_state());
1404
1405 // Simulate the swap out ack.
1406 rvh1->OnSwappedOut(false);
1407 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_WAITING_FOR_COMMIT,
1408 rvh1->rvh_state());
1409
1410 // The new page commits.
1411 contents()->TestDidNavigate(rvh2, 1, kUrl2, PAGE_TRANSITION_TYPED);
1412 EXPECT_FALSE(contents()->cross_navigation_pending());
1413 EXPECT_EQ(rvh2, rvh());
1414 EXPECT_TRUE(contents()->GetPendingRenderViewHost() == NULL);
1415 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_LIVE, rvh2->rvh_state());
1416
1417 // rvh1 should have been deleted.
1418 EXPECT_TRUE(destruction_checker1.rvh_deleted());
1419 rvh1 = NULL;
1420
1421 // Increment the number of active views in SiteInstanceImpl so that rvh2 is
1422 // not deleted on swap out.
1423 static_cast<SiteInstanceImpl*>(
1424 rvh2->GetSiteInstance())->increment_active_view_count();
1425 RenderViewHostDestructionChecker destruction_checker2(rvh2);
1426
1427 // Navigate to new site, simulating onbeforeunload approval.
1428 controller().LoadURL(kUrl1, Referrer(), PAGE_TRANSITION_LINK, std::string());
1429 now = base::TimeTicks::Now();
1430 rvh2->OnMessageReceived(ViewHostMsg_ShouldClose_ACK(0, true, now, now));
1431 EXPECT_TRUE(contents()->cross_navigation_pending());
1432 TestRenderViewHost* rvh3 =
1433 static_cast<TestRenderViewHost*>(contents()->GetPendingRenderViewHost());
1434
1435 // Simulate rvh3's response, which leads to an unload request being sent to
1436 // rvh2.
1437 std::vector<GURL> url_chain2;
1438 url_chain2.push_back(GURL());
1439 contents()->GetRenderManagerForTesting()->OnCrossSiteResponse(
1440 rvh3,
1441 GlobalRequestID(0, 0),
1442 false,
1443 url_chain2,
1444 Referrer(),
1445 PAGE_TRANSITION_TYPED,
1446 1,
1447 false);
1448 EXPECT_TRUE(contents()->cross_navigation_pending());
1449 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_WAITING_FOR_UNLOAD_ACK,
1450 rvh2->rvh_state());
1451
1452 // Simulate the swap out ack.
1453 rvh2->OnSwappedOut(false);
1454 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_WAITING_FOR_COMMIT,
1455 rvh2->rvh_state());
1456
1457 // The new page commits.
1458 contents()->TestDidNavigate(rvh3, 1, kUrl2, PAGE_TRANSITION_TYPED);
1459 EXPECT_FALSE(contents()->cross_navigation_pending());
1460 EXPECT_EQ(rvh3, rvh());
1461 EXPECT_TRUE(contents()->GetPendingRenderViewHost() == NULL);
1462 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_LIVE, rvh3->rvh_state());
1463
1464 // rvh2 should be swapped out.
1465 EXPECT_FALSE(destruction_checker2.rvh_deleted());
1466 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_SWAPPED_OUT, rvh2->rvh_state());
1467 }
1468
1469 // Tests that the RenderViewHost is properly swapped out or deleted when the new
1470 // page commits befoer the swap out ack is received.
1471 TEST_F(RenderFrameHostManagerTest, NewPageCommitsBeforeSwapOutACK) {
1472 const GURL kUrl1("http://www.google.com/");
1473 const GURL kUrl2("http://www.chromium.org/");
1474
1475 // Navigate to the first page.
1476 contents()->NavigateAndCommit(kUrl1);
1477 TestRenderViewHost* rvh1 = test_rvh();
1478 RenderViewHostDestructionChecker destruction_checker1(rvh1);
1479 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_LIVE, rvh1->rvh_state());
1480
1481 // Navigate to new site, simulating onbeforeunload approval.
1482 controller().LoadURL(kUrl2, Referrer(), PAGE_TRANSITION_LINK, std::string());
1483 base::TimeTicks now = base::TimeTicks::Now();
1484 rvh1->OnMessageReceived(ViewHostMsg_ShouldClose_ACK(0, true, now, now));
1485 EXPECT_TRUE(contents()->cross_navigation_pending());
1486 TestRenderViewHost* rvh2 =
1487 static_cast<TestRenderViewHost*>(contents()->GetPendingRenderViewHost());
1488
1489 // Simulate rvh2's response, which leads to an unload request being sent to
1490 // rvh1.
1491 std::vector<GURL> url_chain1;
1492 url_chain1.push_back(GURL());
1493 contents()->GetRenderManagerForTesting()->OnCrossSiteResponse(
1494 rvh2,
1495 GlobalRequestID(0, 0),
1496 false,
1497 url_chain1,
1498 Referrer(),
1499 PAGE_TRANSITION_TYPED,
1500 1,
1501 false);
1502 EXPECT_TRUE(contents()->cross_navigation_pending());
1503 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_WAITING_FOR_UNLOAD_ACK,
1504 rvh1->rvh_state());
1505
1506 // The new page commits.
1507 contents()->TestDidNavigate(rvh2, 1, kUrl2, PAGE_TRANSITION_TYPED);
1508 EXPECT_FALSE(contents()->cross_navigation_pending());
1509 EXPECT_EQ(rvh2, rvh());
1510 EXPECT_TRUE(contents()->GetPendingRenderViewHost() == NULL);
1511 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_LIVE, rvh2->rvh_state());
1512 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_PENDING_SHUTDOWN, rvh1->rvh_state());
1513
1514 // Simulate the swap out ack.
1515 rvh1->OnSwappedOut(false);
1516
1517 // rvh1 should have been deleted.
1518 EXPECT_TRUE(destruction_checker1.rvh_deleted());
1519 rvh1 = NULL;
1520
1521 // Increment the number of active views in SiteInstanceImpl so that rvh2 is
1522 // not deleted on swap out.
1523 static_cast<SiteInstanceImpl*>(
1524 rvh2->GetSiteInstance())->increment_active_view_count();
1525 RenderViewHostDestructionChecker destruction_checker2(rvh2);
1526
1527 // Navigate to new site, simulating onbeforeunload approval.
1528 controller().LoadURL(kUrl1, Referrer(), PAGE_TRANSITION_LINK, std::string());
1529 now = base::TimeTicks::Now();
1530 rvh2->OnMessageReceived(ViewHostMsg_ShouldClose_ACK(0, true, now, now));
1531 EXPECT_TRUE(contents()->cross_navigation_pending());
1532 TestRenderViewHost* rvh3 =
1533 static_cast<TestRenderViewHost*>(contents()->GetPendingRenderViewHost());
1534
1535 // Simulate rvh3's response, which leads to an unload request being sent to
1536 // rvh2.
1537 std::vector<GURL> url_chain2;
1538 url_chain2.push_back(GURL());
1539 contents()->GetRenderManagerForTesting()->OnCrossSiteResponse(
1540 rvh3,
1541 GlobalRequestID(0, 0),
1542 false,
1543 url_chain2,
1544 Referrer(),
1545 PAGE_TRANSITION_TYPED,
1546 1,
1547 false);
1548 EXPECT_TRUE(contents()->cross_navigation_pending());
1549 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_WAITING_FOR_UNLOAD_ACK,
1550 rvh2->rvh_state());
1551
1552 // The new page commits.
1553 contents()->TestDidNavigate(rvh3, 1, kUrl2, PAGE_TRANSITION_TYPED);
1554 EXPECT_FALSE(contents()->cross_navigation_pending());
1555 EXPECT_EQ(rvh3, rvh());
1556 EXPECT_TRUE(contents()->GetPendingRenderViewHost() == NULL);
1557 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_LIVE, rvh3->rvh_state());
1558 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_PENDING_SWAP_OUT, rvh2->rvh_state());
1559
1560 // Simulate the swap out ack.
1561 rvh2->OnSwappedOut(false);
1562
1563 // rvh2 should be swapped out.
1564 EXPECT_FALSE(destruction_checker2.rvh_deleted());
1565 EXPECT_EQ(RenderViewHostImpl::RVH_STATE_SWAPPED_OUT, rvh2->rvh_state());
1566 }
1567
1345 } // namespace content 1568 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698