Chromium Code Reviews| Index: content/browser/frame_host/render_frame_host_manager_unittest.cc |
| diff --git a/content/browser/frame_host/render_frame_host_manager_unittest.cc b/content/browser/frame_host/render_frame_host_manager_unittest.cc |
| index bac4f6df8215295e4fe88f03dc88fe333798b34d..a9b0e1ef59dfa77d8f45fa0bfb38c05437b95578 100644 |
| --- a/content/browser/frame_host/render_frame_host_manager_unittest.cc |
| +++ b/content/browser/frame_host/render_frame_host_manager_unittest.cc |
| @@ -37,6 +37,7 @@ |
| #include "content/public/test/mock_render_process_host.h" |
| #include "content/public/test/test_notification_tracker.h" |
| #include "content/public/test/test_utils.h" |
| +#include "content/test/browser_side_navigation_test_utils.h" |
| #include "content/test/test_content_browser_client.h" |
| #include "content/test/test_content_client.h" |
| #include "content/test/test_render_frame_host.h" |
| @@ -54,7 +55,8 @@ class RenderFrameHostManagerTestWebUIControllerFactory |
| : public WebUIControllerFactory { |
| public: |
| RenderFrameHostManagerTestWebUIControllerFactory() |
| - : should_create_webui_(false) { |
| + : should_create_webui_(false), type_(1) { |
| + CHECK_NE(reinterpret_cast<void*>(type_), WebUI::kNoWebUI); |
| } |
| ~RenderFrameHostManagerTestWebUIControllerFactory() override {} |
| @@ -62,6 +64,11 @@ class RenderFrameHostManagerTestWebUIControllerFactory |
| should_create_webui_ = should_create_webui; |
| } |
| + void set_webui_type(int type) { |
| + CHECK_NE(reinterpret_cast<void*>(type), WebUI::kNoWebUI); |
| + type_ = type; |
| + } |
| + |
| // WebUIFactory implementation. |
| WebUIController* CreateWebUIControllerForURL(WebUI* web_ui, |
| const GURL& url) const override { |
| @@ -77,8 +84,10 @@ class RenderFrameHostManagerTestWebUIControllerFactory |
| // If WebUI creation is enabled for the test and this is a WebUI URL, |
| // returns a mock WebUI type. |
| if (should_create_webui_ && HasWebUIScheme(url)) { |
| - return const_cast<RenderFrameHostManagerTestWebUIControllerFactory*>( |
| - this); |
| + // WebUI::TypeID values are void pointers to structures each factory knows |
| + // are unique to their respective cases. So these return values should be |
| + // safe if very low values are used (just above zero). |
| + return reinterpret_cast<void*>(type_); |
|
clamy
2015/10/21 16:54:22
If you want to cast back and from a pointer, you s
carlosk
2015/10/22 13:36:19
Done for using uintptr_t.
But, as I tried to expl
|
| } |
| return WebUI::kNoWebUI; |
| } |
| @@ -95,6 +104,7 @@ class RenderFrameHostManagerTestWebUIControllerFactory |
| private: |
| bool should_create_webui_; |
| + int type_; |
| DISALLOW_COPY_AND_ASSIGN(RenderFrameHostManagerTestWebUIControllerFactory); |
| }; |
| @@ -272,6 +282,8 @@ class RenderFrameHostManagerTest : public RenderViewHostImplTestHarness { |
| factory_.set_should_create_webui(should_create_webui); |
| } |
| + void set_webui_type(int type) { factory_.set_webui_type(type); } |
| + |
| void NavigateActiveAndCommit(const GURL& url) { |
| // Note: we navigate the active RenderFrameHost because previous navigations |
| // won't have committed yet, so NavigateAndCommit does the wrong thing |
| @@ -409,6 +421,11 @@ class RenderFrameHostManagerTest : public RenderViewHostImplTestHarness { |
| frame_entry->referrer(), *frame_entry, entry, |
| FrameMsg_Navigate_Type::NORMAL, false, base::TimeTicks::Now(), |
| static_cast<NavigationControllerImpl*>(&controller())); |
| + // Simulates request creation that triggers the 1st internal call to |
|
clamy
2015/10/21 16:54:22
nit: add a line before the comment.
carlosk
2015/10/22 13:36:19
Done.
|
| + // GetFrameHostForNavigation. |
| + manager->DidCreateNavigationRequest(*navigation_request); |
| + // And also simulates the 2nd and final call to GetFrameHostForNavigation |
|
clamy
2015/10/21 16:54:22
nit: same here.
carlosk
2015/10/22 13:36:19
Done.
|
| + // that determines the final frame that will commit the navigation. |
| TestRenderFrameHost* frame_host = static_cast<TestRenderFrameHost*>( |
| manager->GetFrameHostForNavigation(*navigation_request)); |
| CHECK(frame_host); |
| @@ -1108,13 +1125,17 @@ TEST_F(RenderFrameHostManagerTest, WebUI) { |
| // The Web UI is committed immediately because the RenderViewHost has not been |
| // used yet. UpdateStateForNavigate() took the short cut path. |
| + EXPECT_TRUE(manager->web_ui()); |
| if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| switches::kEnableBrowserSideNavigation)) { |
| - EXPECT_FALSE(manager->speculative_web_ui()); |
| + // For PlzNavigate as GetFrameHostForNavigation is called twice in a row, a |
| + // speculative WebUI will exist because of the 2nd call but it should just |
| + // be the re-using of the current one that has been committed. |
| + EXPECT_TRUE(manager->speculative_web_ui()); |
| + EXPECT_EQ(manager->web_ui(), manager->speculative_web_ui()); |
| } else { |
| EXPECT_FALSE(manager->pending_web_ui()); |
| } |
| - EXPECT_TRUE(manager->web_ui()); |
| // Commit. |
| manager->DidNavigateFrame(host, true); |
| @@ -2488,4 +2509,172 @@ TEST_F(RenderFrameHostManagerTest, TraverseComplexOpenerChain) { |
| nodes_with_back_links.end()); |
| } |
| +class RenderFrameHostManagerTestWithBrowserSideNavigation |
| + : public RenderFrameHostManagerTest { |
| + public: |
| + void SetUp() override { |
| + EnableBrowserSideNavigation(); |
| + RenderFrameHostManagerTest::SetUp(); |
| + } |
| +}; |
| + |
| +TEST_F(RenderFrameHostManagerTestWithBrowserSideNavigation, |
| + NavigateBetweenWebUIs) { |
| + set_should_create_webui(true); |
| + RenderViewHostChangedObserver change_observer(contents()); |
| + RenderFrameHostManager* manager = contents()->GetRenderManagerForTesting(); |
| + |
| + // 1) The first navigation to a WebUI. -------------------------- |
| + { |
| + RenderFrameHostImpl* initial_host = manager->current_frame_host(); |
| + ASSERT_TRUE(initial_host); |
| + EXPECT_FALSE(initial_host->IsRenderFrameLive()); |
| + |
| + const GURL kUrl("chrome://foo"); |
| + NavigationEntryImpl entry(NULL /* instance */, -1 /* page_id */, kUrl, |
| + Referrer(), base::string16() /* title */, |
| + ui::PAGE_TRANSITION_TYPED, |
| + false /* is_renderer_init */); |
| + FrameNavigationEntry* frame_entry = entry.root_node()->frame_entry.get(); |
| + scoped_ptr<NavigationRequest> navigation_request = |
| + NavigationRequest::CreateBrowserInitiated( |
| + contents()->GetFrameTree()->root(), frame_entry->url(), |
| + frame_entry->referrer(), *frame_entry, entry, |
| + FrameMsg_Navigate_Type::NORMAL, false, base::TimeTicks::Now(), |
| + static_cast<NavigationControllerImpl*>(&controller())); |
| + // Navigation request |
| + manager->DidCreateNavigationRequest(*navigation_request); |
| + EXPECT_FALSE(GetPendingFrameHost(manager)); |
| + // As the initial renderer was not be live, the new RenderFrameHost should |
| + // be made immediately active at request time. |
| + EXPECT_TRUE(change_observer.DidHostChange()); |
| + TestRenderFrameHost* host = |
| + static_cast<TestRenderFrameHost*>(manager->current_frame_host()); |
| + ASSERT_TRUE(host); |
| + EXPECT_NE(host, initial_host); |
| + EXPECT_TRUE(host->IsRenderFrameLive()); |
| + WebUIImpl* web_ui = host->web_ui(); |
| + EXPECT_TRUE(web_ui); |
| + EXPECT_FALSE(manager->speculative_web_ui()); |
| + |
| + // Preparing to commit, updates the navigating RenderFrameHost |
| + EXPECT_EQ(host, manager->GetFrameHostForNavigation(*navigation_request)); |
| + host->set_pending_commit(true); |
| + |
| + // No pending RenderFrameHost as the current should be reused |
| + EXPECT_FALSE(GetPendingFrameHost(manager)); |
| + // But now there will be a pending WebUI set to re-using the current one. |
| + EXPECT_EQ(web_ui, host->web_ui()); |
| + EXPECT_EQ(web_ui, manager->speculative_web_ui()); |
| + EXPECT_EQ(web_ui, host->pending_web_ui()); |
| + |
| + // Commit. |
| + manager->DidNavigateFrame(host, true); |
| + EXPECT_FALSE(change_observer.DidHostChange()); |
| + EXPECT_EQ(host, manager->current_frame_host()); |
| + EXPECT_FALSE(GetPendingFrameHost(manager)); |
| + EXPECT_EQ(web_ui, host->web_ui()); |
| + EXPECT_FALSE(manager->speculative_web_ui()); |
| + EXPECT_FALSE(host->pending_web_ui()); |
| + } |
| + |
| + // 2) Navigate to next WebUI. ------------------------- |
| + { |
| + // Same-site navigation and to a different WebUI. |
| + set_webui_type(2); |
| + const GURL kUrl("chrome://foo/bar"); |
| + // Navigation request |
| + NavigationEntryImpl entry(NULL /* instance */, -1 /* page_id */, kUrl, |
| + Referrer(), base::string16() /* title */, |
| + ui::PAGE_TRANSITION_TYPED, |
| + false /* is_renderer_init */); |
| + FrameNavigationEntry* frame_entry = entry.root_node()->frame_entry.get(); |
| + scoped_ptr<NavigationRequest> navigation_request = |
| + NavigationRequest::CreateBrowserInitiated( |
| + contents()->GetFrameTree()->root(), frame_entry->url(), |
| + frame_entry->referrer(), *frame_entry, entry, |
| + FrameMsg_Navigate_Type::NORMAL, false, base::TimeTicks::Now(), |
| + static_cast<NavigationControllerImpl*>(&controller())); |
| + manager->DidCreateNavigationRequest(*navigation_request); |
| + |
| + EXPECT_FALSE(GetPendingFrameHost(manager)); |
| + TestRenderFrameHost* host = |
| + static_cast<TestRenderFrameHost*>(manager->current_frame_host()); |
| + // Current WebUI should still be in place and a new one should be pending |
| + // from the current RenderFrameHost. |
| + EXPECT_TRUE(host->web_ui()); |
| + WebUIImpl* next_web_ui = manager->speculative_web_ui(); |
| + EXPECT_TRUE(next_web_ui); |
| + EXPECT_NE(next_web_ui, host->web_ui()); |
| + EXPECT_EQ(next_web_ui, host->pending_web_ui()); |
| + |
| + // Preparing to commit, updates the navigating RenderFrameHost |
| + EXPECT_EQ(host, manager->GetFrameHostForNavigation(*navigation_request)); |
| + host->set_pending_commit(true); |
| + |
| + EXPECT_FALSE(GetPendingFrameHost(manager)); |
| + EXPECT_TRUE(host->web_ui()); |
| + EXPECT_NE(next_web_ui, host->web_ui()); |
| + EXPECT_EQ(next_web_ui, manager->speculative_web_ui()); |
| + EXPECT_EQ(next_web_ui, host->pending_web_ui()); |
| + |
| + // Commit. |
| + manager->DidNavigateFrame(host, true); |
| + EXPECT_FALSE(change_observer.DidHostChange()); |
| + EXPECT_EQ(next_web_ui, host->web_ui()); |
| + EXPECT_FALSE(manager->speculative_web_ui()); |
| + EXPECT_FALSE(host->pending_web_ui()); |
| + } |
| + |
| + // 3) Navigate to yet another WebUI. ------------------------- |
| + { |
| + // Cross-site navigation and to (forcefully) a different WebUI. |
| + set_webui_type(3); // Not really necessary; just for consistency. |
| + const GURL kUrl("chrome://boo"); |
| + // Navigation request |
| + NavigationEntryImpl entry(NULL /* instance */, -1 /* page_id */, kUrl, |
| + Referrer(), base::string16() /* title */, |
| + ui::PAGE_TRANSITION_TYPED, |
| + false /* is_renderer_init */); |
| + FrameNavigationEntry* frame_entry = entry.root_node()->frame_entry.get(); |
| + scoped_ptr<NavigationRequest> navigation_request = |
| + NavigationRequest::CreateBrowserInitiated( |
| + contents()->GetFrameTree()->root(), frame_entry->url(), |
| + frame_entry->referrer(), *frame_entry, entry, |
| + FrameMsg_Navigate_Type::NORMAL, false, base::TimeTicks::Now(), |
| + static_cast<NavigationControllerImpl*>(&controller())); |
| + manager->DidCreateNavigationRequest(*navigation_request); |
| + |
| + TestRenderFrameHost* speculative_host = |
| + static_cast<TestRenderFrameHost*>(GetPendingFrameHost(manager)); |
| + EXPECT_TRUE(speculative_host); |
| + // Current WebUI should still be in place and a new one should be pending |
| + // from the speculative RenderFrameHost. |
| + EXPECT_TRUE(manager->current_frame_host()->web_ui()); |
| + WebUIImpl* next_web_ui = manager->speculative_web_ui(); |
| + EXPECT_TRUE(next_web_ui); |
| + EXPECT_NE(next_web_ui, manager->current_frame_host()->web_ui()); |
| + EXPECT_EQ(next_web_ui, speculative_host->web_ui()); |
| + |
| + // Preparing to commit, updates the navigating RenderFrameHost |
| + EXPECT_EQ(speculative_host, |
| + manager->GetFrameHostForNavigation(*navigation_request)); |
| + speculative_host->set_pending_commit(true); |
| + |
| + EXPECT_EQ(speculative_host, GetPendingFrameHost(manager)); |
| + EXPECT_TRUE(manager->current_frame_host()->web_ui()); |
| + EXPECT_NE(next_web_ui, manager->current_frame_host()->web_ui()); |
| + EXPECT_EQ(next_web_ui, manager->speculative_web_ui()); |
| + EXPECT_EQ(next_web_ui, speculative_host->web_ui()); |
| + |
| + // Commit. |
| + manager->DidNavigateFrame(speculative_host, true); |
| + EXPECT_EQ(speculative_host, manager->current_frame_host()); |
| + EXPECT_TRUE(change_observer.DidHostChange()); |
| + EXPECT_EQ(next_web_ui, manager->current_frame_host()->web_ui()); |
| + EXPECT_FALSE(manager->speculative_web_ui()); |
| + EXPECT_FALSE(speculative_host->pending_web_ui()); |
| + } |
| +} |
| + |
| } // namespace content |