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

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: Addressed Charlie's comments Created 6 years, 10 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_TRUE(ntp_rvh->IsSwappedOut());
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_EQ(RenderViewHostImpl::STATE_PENDING_SWAP_OUT, 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_TRUE(swapped_out_rvh->IsSwappedOut());
365 365
366 scoped_ptr<RenderWidgetHostIterator> widgets( 366 scoped_ptr<RenderWidgetHostIterator> widgets(
367 RenderWidgetHost::GetRenderWidgetHosts()); 367 RenderWidgetHost::GetRenderWidgetHosts());
368 // We know that there is the only one active widget. Another view is 368 // 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 369 // now swapped out, so the swapped out view is not included in the
370 // list. 370 // list.
371 RenderWidgetHost* widget = widgets->GetNextHost(); 371 RenderWidgetHost* widget = widgets->GetNextHost();
372 EXPECT_FALSE(widgets->GetNextHost()); 372 EXPECT_FALSE(widgets->GetNextHost());
373 RenderViewHost* rvh = RenderViewHost::From(widget); 373 RenderViewHost* rvh = RenderViewHost::From(widget);
374 EXPECT_FALSE(static_cast<RenderViewHostImpl*>(rvh)->is_swapped_out()); 374 EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT,
375 static_cast<RenderViewHostImpl*>(rvh)->rvh_state());
375 } 376 }
376 377
377 // Test if RenderViewHost::GetRenderWidgetHosts() returns a subset of 378 // Test if RenderViewHost::GetRenderWidgetHosts() returns a subset of
378 // RenderViewHostImpl::GetAllRenderWidgetHosts(). 379 // RenderViewHostImpl::GetAllRenderWidgetHosts().
379 // RenderViewHost::GetRenderWidgetHosts() returns only active widgets, but 380 // RenderViewHost::GetRenderWidgetHosts() returns only active widgets, but
380 // RenderViewHostImpl::GetAllRenderWidgetHosts() returns everything 381 // RenderViewHostImpl::GetAllRenderWidgetHosts() returns everything
381 // including swapped out ones. 382 // including swapped out ones.
382 TEST_F(RenderFrameHostManagerTest, 383 TEST_F(RenderFrameHostManagerTest,
383 GetRenderWidgetHostsWithinGetAllRenderWidgetHosts) { 384 GetRenderWidgetHostsWithinGetAllRenderWidgetHosts) {
384 TestRenderViewHost* swapped_out_rvh = CreateSwappedOutRenderViewHost(); 385 TestRenderViewHost* swapped_out_rvh = CreateSwappedOutRenderViewHost();
385 EXPECT_TRUE(swapped_out_rvh->is_swapped_out()); 386 EXPECT_TRUE(swapped_out_rvh->IsSwappedOut());
386 387
387 scoped_ptr<RenderWidgetHostIterator> widgets( 388 scoped_ptr<RenderWidgetHostIterator> widgets(
388 RenderWidgetHost::GetRenderWidgetHosts()); 389 RenderWidgetHost::GetRenderWidgetHosts());
389 390
390 while (RenderWidgetHost* w = widgets->GetNextHost()) { 391 while (RenderWidgetHost* w = widgets->GetNextHost()) {
391 bool found = false; 392 bool found = false;
392 scoped_ptr<RenderWidgetHostIterator> all_widgets( 393 scoped_ptr<RenderWidgetHostIterator> all_widgets(
393 RenderWidgetHostImpl::GetAllRenderWidgetHosts()); 394 RenderWidgetHostImpl::GetAllRenderWidgetHosts());
394 while (RenderWidgetHost* widget = all_widgets->GetNextHost()) { 395 while (RenderWidgetHost* widget = all_widgets->GetNextHost()) {
395 if (w == widget) { 396 if (w == widget) {
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after
761 static_cast<TestRenderViewHost*>(host->render_view_host()); 762 static_cast<TestRenderViewHost*>(host->render_view_host());
762 MockRenderProcessHost* test_process_host = 763 MockRenderProcessHost* test_process_host =
763 static_cast<MockRenderProcessHost*>(test_host->GetProcess()); 764 static_cast<MockRenderProcessHost*>(test_host->GetProcess());
764 EXPECT_TRUE(test_process_host->sink().GetUniqueMessageMatching( 765 EXPECT_TRUE(test_process_host->sink().GetUniqueMessageMatching(
765 ViewMsg_ShouldClose::ID)); 766 ViewMsg_ShouldClose::ID));
766 test_host->SendShouldCloseACK(true); 767 test_host->SendShouldCloseACK(true);
767 768
768 // CrossSiteResourceHandler::StartCrossSiteTransition triggers a 769 // CrossSiteResourceHandler::StartCrossSiteTransition triggers a
769 // call of RenderFrameHostManager::SwapOutOldPage before 770 // call of RenderFrameHostManager::SwapOutOldPage before
770 // RenderFrameHostManager::DidNavigateMainFrame is called. 771 // RenderFrameHostManager::DidNavigateMainFrame is called.
771 // The RVH is not swapped out until the commit. 772 // The RVH is swapped out after receiving the unload ack.
772 manager->SwapOutOldPage(); 773 manager->SwapOutOldPage();
773 EXPECT_TRUE(test_process_host->sink().GetUniqueMessageMatching( 774 EXPECT_TRUE(test_process_host->sink().GetUniqueMessageMatching(
774 ViewMsg_SwapOut::ID)); 775 ViewMsg_SwapOut::ID));
775 test_host->OnSwappedOut(false); 776 test_host->OnSwappedOut(false);
776 777
777 EXPECT_EQ(host, manager->current_frame_host()); 778 EXPECT_EQ(host, manager->current_frame_host());
778 EXPECT_FALSE(manager->current_frame_host()->is_swapped_out()); 779 EXPECT_FALSE(manager->current_frame_host()->is_swapped_out());
779 EXPECT_EQ(host2, manager->pending_frame_host()); 780 EXPECT_EQ(host2, manager->pending_frame_host());
780 // There should be still no navigation messages being sent. 781 // There should be still no navigation messages being sent.
781 EXPECT_FALSE(test_process_host2->sink().GetUniqueMessageMatching( 782 EXPECT_FALSE(test_process_host2->sink().GetUniqueMessageMatching(
782 ViewMsg_Navigate::ID)); 783 ViewMsg_Navigate::ID));
783 784
784 // 3) Cross-site navigate to next site before 2) has committed. -------------- 785 // 3) Cross-site navigate to next site before 2) has committed. --------------
785 const GURL kUrl3("http://webkit.org/"); 786 const GURL kUrl3("http://webkit.org/");
786 NavigationEntryImpl entry3(NULL /* instance */, -1 /* page_id */, kUrl3, 787 NavigationEntryImpl entry3(NULL /* instance */, -1 /* page_id */, kUrl3,
787 Referrer(), base::string16() /* title */, 788 Referrer(), base::string16() /* title */,
788 PAGE_TRANSITION_TYPED, 789 PAGE_TRANSITION_TYPED,
789 false /* is_renderer_init */); 790 false /* is_renderer_init */);
790 test_process_host->sink().ClearMessages(); 791 test_process_host->sink().ClearMessages();
791 RenderFrameHostImpl* host3 = manager->Navigate(entry3); 792 RenderFrameHostImpl* host3 = manager->Navigate(entry3);
792 793
793 // A new RenderFrameHost should be created. host2 is now deleted. 794 // A new RenderFrameHost should be created. host2 is now deleted.
794 EXPECT_TRUE(manager->pending_frame_host()); 795 EXPECT_TRUE(manager->pending_frame_host());
795 ASSERT_EQ(host3, manager->pending_frame_host()); 796 ASSERT_EQ(host3, manager->pending_frame_host());
796 EXPECT_NE(host3, host); 797 EXPECT_NE(host3, host);
797 EXPECT_NE(host3->GetProcess()->GetID(), host2_process_id); 798 EXPECT_NE(host3->GetProcess()->GetID(), host2_process_id);
798 799
799 // Navigations in the new RVH should be suspended, which is ok because the 800 // 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*>( 801 EXPECT_TRUE(static_cast<RenderViewHostImpl*>(
803 host3->render_view_host())->are_navigations_suspended()); 802 host3->render_view_host())->are_navigations_suspended());
804 EXPECT_EQ(host, manager->current_frame_host()); 803 EXPECT_EQ(host, manager->current_frame_host());
805 EXPECT_FALSE(manager->current_frame_host()->is_swapped_out()); 804 EXPECT_FALSE(manager->current_frame_host()->is_swapped_out());
806 805
807 // Simulate a response to the second beforeunload request. 806 // Simulate a response to the second beforeunload request.
808 EXPECT_TRUE(test_process_host->sink().GetUniqueMessageMatching( 807 EXPECT_TRUE(test_process_host->sink().GetUniqueMessageMatching(
809 ViewMsg_ShouldClose::ID)); 808 ViewMsg_ShouldClose::ID));
810 test_host->SendShouldCloseACK(true); 809 test_host->SendShouldCloseACK(true);
811 810
812 // CrossSiteResourceHandler::StartCrossSiteTransition triggers a 811 // CrossSiteResourceHandler::StartCrossSiteTransition triggers a
813 // call of RenderFrameHostManager::SwapOutOldPage before 812 // call of RenderFrameHostManager::SwapOutOldPage before
814 // RenderFrameHostManager::DidNavigateMainFrame is called. 813 // RenderFrameHostManager::DidNavigateMainFrame is called.
815 // The RVH is not swapped out until the commit.
816 manager->SwapOutOldPage(); 814 manager->SwapOutOldPage();
817 EXPECT_TRUE(test_process_host->sink().GetUniqueMessageMatching( 815 EXPECT_TRUE(test_process_host->sink().GetUniqueMessageMatching(
818 ViewMsg_SwapOut::ID)); 816 ViewMsg_SwapOut::ID));
819 test_host->OnSwappedOut(false); 817 test_host->OnSwappedOut(false);
820 818
821 // Commit. 819 // Commit.
822 manager->DidNavigateMainFrame(host3->render_view_host()); 820 manager->DidNavigateMainFrame(host3->render_view_host());
823 EXPECT_TRUE(host3 == manager->current_frame_host()); 821 EXPECT_TRUE(host3 == manager->current_frame_host());
824 ASSERT_TRUE(host3); 822 ASSERT_TRUE(host3);
825 EXPECT_TRUE(static_cast<SiteInstanceImpl*>(host3->GetSiteInstance())-> 823 EXPECT_TRUE(static_cast<SiteInstanceImpl*>(host3->GetSiteInstance())->
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
1018 increment_active_view_count(); 1016 increment_active_view_count();
1019 1017
1020 // Now go back, but suppose the SwapOut_ACK isn't received. This shouldn't 1018 // 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 1019 // happen, but we have seen it when going back quickly across many entries
1022 // (http://crbug.com/93427). 1020 // (http://crbug.com/93427).
1023 contents()->GetController().GoBack(); 1021 contents()->GetController().GoBack();
1024 EXPECT_TRUE(rvh2->is_waiting_for_beforeunload_ack()); 1022 EXPECT_TRUE(rvh2->is_waiting_for_beforeunload_ack());
1025 contents()->ProceedWithCrossSiteNavigation(); 1023 contents()->ProceedWithCrossSiteNavigation();
1026 EXPECT_FALSE(rvh2->is_waiting_for_beforeunload_ack()); 1024 EXPECT_FALSE(rvh2->is_waiting_for_beforeunload_ack());
1027 rvh2->SwapOut(); 1025 rvh2->SwapOut();
1028 EXPECT_TRUE(rvh2->is_waiting_for_unload_ack()); 1026 EXPECT_TRUE(rvh2->IsWaitingForUnloadACK());
1029 1027
1030 // The back navigation commits. We should proactively clear the 1028 // The back navigation commits.
1031 // is_waiting_for_unload_ack state to be safe.
1032 const NavigationEntry* entry1 = contents()->GetController().GetPendingEntry(); 1029 const NavigationEntry* entry1 = contents()->GetController().GetPendingEntry();
1033 rvh1->SendNavigate(entry1->GetPageID(), entry1->GetURL()); 1030 rvh1->SendNavigate(entry1->GetPageID(), entry1->GetURL());
1034 EXPECT_TRUE(rvh2->is_swapped_out()); 1031 EXPECT_EQ(RenderViewHostImpl::STATE_PENDING_SWAP_OUT, rvh2->rvh_state());
1035 EXPECT_FALSE(rvh2->is_waiting_for_unload_ack());
1036 1032
1037 // We should be able to navigate forward. 1033 // We should be able to navigate forward.
1038 contents()->GetController().GoForward(); 1034 contents()->GetController().GoForward();
1039 contents()->ProceedWithCrossSiteNavigation(); 1035 contents()->ProceedWithCrossSiteNavigation();
1040 const NavigationEntry* entry2 = contents()->GetController().GetPendingEntry(); 1036 const NavigationEntry* entry2 = contents()->GetController().GetPendingEntry();
1041 rvh2->SendNavigate(entry2->GetPageID(), entry2->GetURL()); 1037 rvh2->SendNavigate(entry2->GetPageID(), entry2->GetURL());
1042 EXPECT_EQ(rvh2, rvh()); 1038 EXPECT_EQ(rvh2, rvh());
1043 EXPECT_FALSE(rvh2->is_swapped_out()); 1039 EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh2->rvh_state());
1044 EXPECT_TRUE(rvh1->is_swapped_out()); 1040 EXPECT_EQ(RenderViewHostImpl::STATE_PENDING_SWAP_OUT, rvh1->rvh_state());
1041 rvh1->OnSwappedOut(false);
1042 EXPECT_TRUE(rvh1->IsSwappedOut());
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_TRUE(opener1_rvh->IsSwappedOut());
1092 1090
1093 // Ensure a swapped out RVH is created in the second opener tab. 1091 // Ensure a swapped out RVH is created in the second opener tab.
1094 TestRenderViewHost* opener2_rvh = static_cast<TestRenderViewHost*>( 1092 TestRenderViewHost* opener2_rvh = static_cast<TestRenderViewHost*>(
1095 opener2_manager->GetSwappedOutRenderViewHost(rvh2->GetSiteInstance())); 1093 opener2_manager->GetSwappedOutRenderViewHost(rvh2->GetSiteInstance()));
1096 EXPECT_TRUE(opener2_manager->IsRVHOnSwappedOutList(opener2_rvh)); 1094 EXPECT_TRUE(opener2_manager->IsRVHOnSwappedOutList(opener2_rvh));
1097 EXPECT_TRUE(opener2_rvh->is_swapped_out()); 1095 EXPECT_TRUE(opener2_rvh->IsSwappedOut());
1098 1096
1099 // Navigate to a cross-BrowsingInstance URL. 1097 // Navigate to a cross-BrowsingInstance URL.
1100 contents()->NavigateAndCommit(kChromeUrl); 1098 contents()->NavigateAndCommit(kChromeUrl);
1101 TestRenderViewHost* rvh3 = test_rvh(); 1099 TestRenderViewHost* rvh3 = test_rvh();
1102 EXPECT_NE(rvh1->GetSiteInstance(), rvh3->GetSiteInstance()); 1100 EXPECT_NE(rvh1->GetSiteInstance(), rvh3->GetSiteInstance());
1103 EXPECT_FALSE(rvh1->GetSiteInstance()->IsRelatedSiteInstance( 1101 EXPECT_FALSE(rvh1->GetSiteInstance()->IsRelatedSiteInstance(
1104 rvh3->GetSiteInstance())); 1102 rvh3->GetSiteInstance()));
1105 1103
1106 // No scripting is allowed across BrowsingInstances, so we should not create 1104 // No scripting is allowed across BrowsingInstances, so we should not create
1107 // swapped out RVHs for the opener chain in this case. 1105 // 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); 1187 contents()->NavigateAndCommit(kPluginUrl);
1190 TestRenderViewHost* rvh2 = test_rvh(); 1188 TestRenderViewHost* rvh2 = test_rvh();
1191 EXPECT_NE(rvh1->GetSiteInstance(), rvh2->GetSiteInstance()); 1189 EXPECT_NE(rvh1->GetSiteInstance(), rvh2->GetSiteInstance());
1192 EXPECT_TRUE(rvh1->GetSiteInstance()->IsRelatedSiteInstance( 1190 EXPECT_TRUE(rvh1->GetSiteInstance()->IsRelatedSiteInstance(
1193 rvh2->GetSiteInstance())); 1191 rvh2->GetSiteInstance()));
1194 1192
1195 // Ensure a swapped out RVH is created in the first opener tab. 1193 // Ensure a swapped out RVH is created in the first opener tab.
1196 TestRenderViewHost* opener1_rvh = static_cast<TestRenderViewHost*>( 1194 TestRenderViewHost* opener1_rvh = static_cast<TestRenderViewHost*>(
1197 opener1_manager->GetSwappedOutRenderViewHost(rvh2->GetSiteInstance())); 1195 opener1_manager->GetSwappedOutRenderViewHost(rvh2->GetSiteInstance()));
1198 EXPECT_TRUE(opener1_manager->IsRVHOnSwappedOutList(opener1_rvh)); 1196 EXPECT_TRUE(opener1_manager->IsRVHOnSwappedOutList(opener1_rvh));
1199 EXPECT_TRUE(opener1_rvh->is_swapped_out()); 1197 EXPECT_TRUE(opener1_rvh->IsSwappedOut());
1200 1198
1201 // Ensure the new RVH has WebUI bindings. 1199 // Ensure the new RVH has WebUI bindings.
1202 EXPECT_TRUE(rvh2->GetEnabledBindings() & BINDINGS_POLICY_WEB_UI); 1200 EXPECT_TRUE(rvh2->GetEnabledBindings() & BINDINGS_POLICY_WEB_UI);
1203 } 1201 }
1204 1202
1205 // Test that we reuse the same guest SiteInstance if we navigate across sites. 1203 // Test that we reuse the same guest SiteInstance if we navigate across sites.
1206 TEST_F(RenderFrameHostManagerTest, NoSwapOnGuestNavigations) { 1204 TEST_F(RenderFrameHostManagerTest, NoSwapOnGuestNavigations) {
1207 TestNotificationTracker notifications; 1205 TestNotificationTracker notifications;
1208 1206
1209 GURL guest_url(std::string(kGuestScheme).append("://abc123")); 1207 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, 1333 notifications.ListenFor(NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
1336 Source<RenderWidgetHost>(host2->render_view_host())); 1334 Source<RenderWidgetHost>(host2->render_view_host()));
1337 manager->ShouldClosePage(false, true, base::TimeTicks()); 1335 manager->ShouldClosePage(false, true, base::TimeTicks());
1338 1336
1339 EXPECT_TRUE( 1337 EXPECT_TRUE(
1340 notifications.Check1AndReset(NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED)); 1338 notifications.Check1AndReset(NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED));
1341 EXPECT_FALSE(manager->pending_frame_host()); 1339 EXPECT_FALSE(manager->pending_frame_host());
1342 EXPECT_EQ(host, manager->current_frame_host()); 1340 EXPECT_EQ(host, manager->current_frame_host());
1343 } 1341 }
1344 1342
1343 // This checks that the given RVH has been properly deleted.
1344 class RenderViewHostDestructionObserver : public WebContentsObserver {
1345 public:
1346 RenderViewHostDestructionObserver(RenderViewHost* render_view_host)
1347 : WebContentsObserver(WebContents::FromRenderViewHost(render_view_host)),
1348 render_view_host_(render_view_host),
1349 rvh_deleted_(false) {}
1350
1351 bool rvh_deleted() { return rvh_deleted_; }
1352
1353 virtual void RenderViewDeleted(RenderViewHost* render_view_host) OVERRIDE {
1354 if (render_view_host == render_view_host_)
1355 rvh_deleted_ = true;
1356 }
1357
1358 private:
1359 RenderViewHost* render_view_host_;
1360 bool rvh_deleted_;
1361
1362 DISALLOW_COPY_AND_ASSIGN(RenderViewHostDestructionObserver);
1363 };
1364
1365 // Tests that the RenderViewHost is properly deleted when the SwapOutACK is
1366 // received before the new page commits.
1367 TEST_F(RenderFrameHostManagerTest,
1368 SwapOutACKBeforeNewPageCommitsLeadsToDeletion) {
1369 const GURL kUrl1("http://www.google.com/");
1370 const GURL kUrl2("http://www.chromium.org/");
1371
1372 // Navigate to the first page.
1373 contents()->NavigateAndCommit(kUrl1);
1374 TestRenderViewHost* rvh1 = test_rvh();
1375 RenderViewHostDestructionObserver destruction_observer(rvh1);
1376 EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh1->rvh_state());
1377
1378 // Navigate to new site, simulating onbeforeunload approval.
1379 controller().LoadURL(kUrl2, Referrer(), PAGE_TRANSITION_LINK, std::string());
1380 base::TimeTicks now = base::TimeTicks::Now();
1381 rvh1->OnMessageReceived(ViewHostMsg_ShouldClose_ACK(0, true, now, now));
1382 EXPECT_TRUE(contents()->cross_navigation_pending());
1383 TestRenderViewHost* rvh2 =
1384 static_cast<TestRenderViewHost*>(contents()->GetPendingRenderViewHost());
1385
1386 // Simulate rvh2's response, which leads to an unload request being sent to
1387 // rvh1.
1388 std::vector<GURL> url_chain;
1389 url_chain.push_back(GURL());
1390 contents()->GetRenderManagerForTesting()->OnCrossSiteResponse(
1391 rvh2, GlobalRequestID(0, 0), false, url_chain, Referrer(),
1392 PAGE_TRANSITION_TYPED, 1, false);
1393 EXPECT_TRUE(contents()->cross_navigation_pending());
1394 EXPECT_EQ(RenderViewHostImpl::STATE_WAITING_FOR_UNLOAD_ACK,
1395 rvh1->rvh_state());
1396
1397 // Simulate the swap out ack.
1398 rvh1->OnSwappedOut(false);
1399 EXPECT_EQ(RenderViewHostImpl::STATE_WAITING_FOR_COMMIT, rvh1->rvh_state());
1400
1401 // The new page commits.
1402 contents()->TestDidNavigate(rvh2, 1, kUrl2, PAGE_TRANSITION_TYPED);
1403 EXPECT_FALSE(contents()->cross_navigation_pending());
1404 EXPECT_EQ(rvh2, rvh());
1405 EXPECT_TRUE(contents()->GetPendingRenderViewHost() == NULL);
1406 EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh2->rvh_state());
1407
1408 // rvh1 should have been deleted.
1409 EXPECT_TRUE(destruction_observer.rvh_deleted());
1410 rvh1 = NULL;
1411 }
1412
1413 // Tests that the RenderViewHost is properly swapped out when the SwapOutACK is
1414 // received before the new page commits.
1415 TEST_F(RenderFrameHostManagerTest,
1416 SwapOutACKBeforeNewPageCommitsLeadsToSwapOut) {
1417 const GURL kUrl1("http://www.google.com/");
1418 const GURL kUrl2("http://www.chromium.org/");
1419
1420 // Navigate to the first page.
1421 contents()->NavigateAndCommit(kUrl1);
1422 TestRenderViewHost* rvh1 = test_rvh();
1423 RenderViewHostDestructionObserver destruction_observer(rvh1);
1424 EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh1->rvh_state());
1425
1426 // Increment the number of active views in SiteInstanceImpl so that rvh2 is
1427 // not deleted on swap out.
1428 static_cast<SiteInstanceImpl*>(
1429 rvh1->GetSiteInstance())->increment_active_view_count();
1430
1431 // Navigate to new site, simulating onbeforeunload approval.
1432 controller().LoadURL(kUrl2, Referrer(), PAGE_TRANSITION_LINK, std::string());
1433 base::TimeTicks now = base::TimeTicks::Now();
1434 rvh1->OnMessageReceived(ViewHostMsg_ShouldClose_ACK(0, true, now, now));
1435 EXPECT_TRUE(contents()->cross_navigation_pending());
1436 TestRenderViewHost* rvh2 =
1437 static_cast<TestRenderViewHost*>(contents()->GetPendingRenderViewHost());
1438
1439 // Simulate rvh2's response, which leads to an unload request being sent to
1440 // rvh1.
1441 std::vector<GURL> url_chain;
1442 url_chain.push_back(GURL());
1443 contents()->GetRenderManagerForTesting()->OnCrossSiteResponse(
1444 rvh2, GlobalRequestID(0, 0), false, url_chain, Referrer(),
1445 PAGE_TRANSITION_TYPED, 1, false);
1446 EXPECT_TRUE(contents()->cross_navigation_pending());
1447 EXPECT_EQ(RenderViewHostImpl::STATE_WAITING_FOR_UNLOAD_ACK,
1448 rvh1->rvh_state());
1449
1450 // Simulate the swap out ack.
1451 rvh1->OnSwappedOut(false);
1452 EXPECT_EQ(RenderViewHostImpl::STATE_WAITING_FOR_COMMIT, rvh1->rvh_state());
1453
1454 // The new page commits.
1455 contents()->TestDidNavigate(rvh2, 1, kUrl2, PAGE_TRANSITION_TYPED);
1456 EXPECT_FALSE(contents()->cross_navigation_pending());
1457 EXPECT_EQ(rvh2, rvh());
1458 EXPECT_TRUE(contents()->GetPendingRenderViewHost() == NULL);
1459 EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh2->rvh_state());
1460
1461 // rvh1 should be swapped out.
1462 EXPECT_FALSE(destruction_observer.rvh_deleted());
1463 EXPECT_TRUE(rvh1->IsSwappedOut());
1464 }
1465
1466 // Tests that the RenderViewHost is properly deleted when the new
1467 // page commits before the swap out ack is received.
1468 TEST_F(RenderFrameHostManagerTest,
1469 NewPageCommitsBeforeSwapOutACKLeadsToDeletion) {
1470 const GURL kUrl1("http://www.google.com/");
1471 const GURL kUrl2("http://www.chromium.org/");
1472
1473 // Navigate to the first page.
1474 contents()->NavigateAndCommit(kUrl1);
1475 TestRenderViewHost* rvh1 = test_rvh();
1476 RenderViewHostDestructionObserver destruction_observer(rvh1);
1477 EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh1->rvh_state());
1478
1479 // Navigate to new site, simulating onbeforeunload approval.
1480 controller().LoadURL(kUrl2, Referrer(), PAGE_TRANSITION_LINK, std::string());
1481 base::TimeTicks now = base::TimeTicks::Now();
1482 rvh1->OnMessageReceived(ViewHostMsg_ShouldClose_ACK(0, true, now, now));
1483 EXPECT_TRUE(contents()->cross_navigation_pending());
1484 TestRenderViewHost* rvh2 =
1485 static_cast<TestRenderViewHost*>(contents()->GetPendingRenderViewHost());
1486
1487 // Simulate rvh2's response, which leads to an unload request being sent to
1488 // rvh1.
1489 std::vector<GURL> url_chain;
1490 url_chain.push_back(GURL());
1491 contents()->GetRenderManagerForTesting()->OnCrossSiteResponse(
1492 rvh2, GlobalRequestID(0, 0), false, url_chain, Referrer(),
1493 PAGE_TRANSITION_TYPED, 1, false);
1494 EXPECT_TRUE(contents()->cross_navigation_pending());
1495 EXPECT_EQ(RenderViewHostImpl::STATE_WAITING_FOR_UNLOAD_ACK,
1496 rvh1->rvh_state());
1497
1498 // The new page commits.
1499 contents()->TestDidNavigate(rvh2, 1, kUrl2, PAGE_TRANSITION_TYPED);
1500 EXPECT_FALSE(contents()->cross_navigation_pending());
1501 EXPECT_EQ(rvh2, rvh());
1502 EXPECT_TRUE(contents()->GetPendingRenderViewHost() == NULL);
1503 EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh2->rvh_state());
1504 EXPECT_EQ(RenderViewHostImpl::STATE_PENDING_SHUTDOWN, rvh1->rvh_state());
1505
1506 // Simulate the swap out ack.
1507 rvh1->OnSwappedOut(false);
1508
1509 // rvh1 should have been deleted.
1510 EXPECT_TRUE(destruction_observer.rvh_deleted());
1511 rvh1 = NULL;
1512 }
1513
1514 // Tests that the RenderViewHost is properly swapped out when the new page
1515 // commits before the swap out ack is received.
1516 TEST_F(RenderFrameHostManagerTest,
1517 NewPageCommitsBeforeSwapOutACKLeadsToSwapOut) {
1518 const GURL kUrl1("http://www.google.com/");
1519 const GURL kUrl2("http://www.chromium.org/");
1520
1521 // Navigate to the first page.
1522 contents()->NavigateAndCommit(kUrl1);
1523 TestRenderViewHost* rvh1 = test_rvh();
1524 RenderViewHostDestructionObserver destruction_observer(rvh1);
1525 EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh1->rvh_state());
1526
1527 // Increment the number of active views in SiteInstanceImpl so that rvh1 is
1528 // not deleted on swap out.
1529 static_cast<SiteInstanceImpl*>(
1530 rvh1->GetSiteInstance())->increment_active_view_count();
1531
1532 // Navigate to new site, simulating onbeforeunload approval.
1533 controller().LoadURL(kUrl2, Referrer(), PAGE_TRANSITION_LINK, std::string());
1534 base::TimeTicks now = base::TimeTicks::Now();
1535 rvh1->OnMessageReceived(ViewHostMsg_ShouldClose_ACK(0, true, now, now));
1536 EXPECT_TRUE(contents()->cross_navigation_pending());
1537 TestRenderViewHost* rvh2 =
1538 static_cast<TestRenderViewHost*>(contents()->GetPendingRenderViewHost());
1539
1540 // Simulate rvh2's response, which leads to an unload request being sent to
1541 // rvh1.
1542 std::vector<GURL> url_chain;
1543 url_chain.push_back(GURL());
1544 contents()->GetRenderManagerForTesting()->OnCrossSiteResponse(
1545 rvh2, GlobalRequestID(0, 0), false, url_chain, Referrer(),
1546 PAGE_TRANSITION_TYPED, 1, false);
1547 EXPECT_TRUE(contents()->cross_navigation_pending());
1548 EXPECT_EQ(RenderViewHostImpl::STATE_WAITING_FOR_UNLOAD_ACK,
1549 rvh1->rvh_state());
1550
1551 // The new page commits.
1552 contents()->TestDidNavigate(rvh2, 1, kUrl2, PAGE_TRANSITION_TYPED);
1553 EXPECT_FALSE(contents()->cross_navigation_pending());
1554 EXPECT_EQ(rvh2, rvh());
1555 EXPECT_TRUE(contents()->GetPendingRenderViewHost() == NULL);
1556 EXPECT_EQ(RenderViewHostImpl::STATE_DEFAULT, rvh2->rvh_state());
1557 EXPECT_EQ(RenderViewHostImpl::STATE_PENDING_SWAP_OUT, rvh1->rvh_state());
1558
1559 // Simulate the swap out ack.
1560 rvh1->OnSwappedOut(false);
1561
1562 // rvh1 should be swapped out.
1563 EXPECT_FALSE(destruction_observer.rvh_deleted());
1564 EXPECT_TRUE(rvh1->IsSwappedOut());
1565 }
1566
1345 } // namespace content 1567 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/frame_host/render_frame_host_manager.cc ('k') | content/browser/renderer_host/render_process_host_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698