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

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

Issue 1417953002: PlzNavigate: Add tests to check WebUI navigations. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase to a different patch (http://crrev.com/1418853003) and comment update. Created 5 years, 1 month 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/command_line.h" 5 #include "base/command_line.h"
6 #include "base/files/file_path.h" 6 #include "base/files/file_path.h"
7 #include "base/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
8 #include "base/test/histogram_tester.h" 8 #include "base/test/histogram_tester.h"
9 #include "base/time/time.h" 9 #include "base/time/time.h"
10 #include "content/browser/compositor/test/no_transport_image_transport_factory.h " 10 #include "content/browser/compositor/test/no_transport_image_transport_factory.h "
(...skipping 20 matching lines...) Expand all
31 #include "content/public/browser/web_contents_observer.h" 31 #include "content/public/browser/web_contents_observer.h"
32 #include "content/public/browser/web_ui_controller.h" 32 #include "content/public/browser/web_ui_controller.h"
33 #include "content/public/common/bindings_policy.h" 33 #include "content/public/common/bindings_policy.h"
34 #include "content/public/common/content_switches.h" 34 #include "content/public/common/content_switches.h"
35 #include "content/public/common/javascript_message_type.h" 35 #include "content/public/common/javascript_message_type.h"
36 #include "content/public/common/url_constants.h" 36 #include "content/public/common/url_constants.h"
37 #include "content/public/common/url_utils.h" 37 #include "content/public/common/url_utils.h"
38 #include "content/public/test/mock_render_process_host.h" 38 #include "content/public/test/mock_render_process_host.h"
39 #include "content/public/test/test_notification_tracker.h" 39 #include "content/public/test/test_notification_tracker.h"
40 #include "content/public/test/test_utils.h" 40 #include "content/public/test/test_utils.h"
41 #include "content/test/browser_side_navigation_test_utils.h"
41 #include "content/test/test_content_browser_client.h" 42 #include "content/test/test_content_browser_client.h"
42 #include "content/test/test_content_client.h" 43 #include "content/test/test_content_client.h"
43 #include "content/test/test_render_frame_host.h" 44 #include "content/test/test_render_frame_host.h"
44 #include "content/test/test_render_view_host.h" 45 #include "content/test/test_render_view_host.h"
45 #include "content/test/test_web_contents.h" 46 #include "content/test/test_web_contents.h"
46 #include "net/base/load_flags.h" 47 #include "net/base/load_flags.h"
47 #include "testing/gtest/include/gtest/gtest.h" 48 #include "testing/gtest/include/gtest/gtest.h"
48 #include "third_party/WebKit/public/web/WebFrameOwnerProperties.h" 49 #include "third_party/WebKit/public/web/WebFrameOwnerProperties.h"
49 #include "third_party/WebKit/public/web/WebSandboxFlags.h" 50 #include "third_party/WebKit/public/web/WebSandboxFlags.h"
50 #include "ui/base/page_transition_types.h" 51 #include "ui/base/page_transition_types.h"
(...skipping 12 matching lines...) Expand all
63 EXPECT_EQ(expected_routing_id, message->routing_id()); 64 EXPECT_EQ(expected_routing_id, message->routing_id());
64 InputMsg_SetFocus::Param params; 65 InputMsg_SetFocus::Param params;
65 EXPECT_TRUE(InputMsg_SetFocus::Read(message, &params)); 66 EXPECT_TRUE(InputMsg_SetFocus::Read(message, &params));
66 EXPECT_EQ(expected_focus, base::get<0>(params)); 67 EXPECT_EQ(expected_focus, base::get<0>(params));
67 } 68 }
68 69
69 class RenderFrameHostManagerTestWebUIControllerFactory 70 class RenderFrameHostManagerTestWebUIControllerFactory
70 : public WebUIControllerFactory { 71 : public WebUIControllerFactory {
71 public: 72 public:
72 RenderFrameHostManagerTestWebUIControllerFactory() 73 RenderFrameHostManagerTestWebUIControllerFactory()
73 : should_create_webui_(false) { 74 : should_create_webui_(false), type_(1) {
75 CHECK_NE(reinterpret_cast<WebUI::TypeID>(type_), WebUI::kNoWebUI);
74 } 76 }
75 ~RenderFrameHostManagerTestWebUIControllerFactory() override {} 77 ~RenderFrameHostManagerTestWebUIControllerFactory() override {}
76 78
77 void set_should_create_webui(bool should_create_webui) { 79 void set_should_create_webui(bool should_create_webui) {
78 should_create_webui_ = should_create_webui; 80 should_create_webui_ = should_create_webui;
79 } 81 }
80 82
83 // This method simulates the expectation that different WebUI instance types
84 // would be created. The |type| value will be returned by GetWebUIType casted
85 // to WebUI::TypeID.
86 // As WebUI::TypeID is a typedef to void pointer, factory implementations
87 // return values that they know to be unique to their respective cases. So
88 // values set here should be safe if kept very low (just above zero).
89 void set_webui_type(uintptr_t type) {
90 CHECK_NE(reinterpret_cast<WebUI::TypeID>(type), WebUI::kNoWebUI);
91 type_ = type;
92 }
93
81 // WebUIFactory implementation. 94 // WebUIFactory implementation.
82 WebUIController* CreateWebUIControllerForURL(WebUI* web_ui, 95 WebUIController* CreateWebUIControllerForURL(WebUI* web_ui,
83 const GURL& url) const override { 96 const GURL& url) const override {
84 // If WebUI creation is enabled for the test and this is a WebUI URL, 97 // If WebUI creation is enabled for the test and this is a WebUI URL,
85 // returns a new instance. 98 // returns a new instance.
86 if (should_create_webui_ && HasWebUIScheme(url)) 99 if (should_create_webui_ && HasWebUIScheme(url))
87 return new WebUIController(web_ui); 100 return new WebUIController(web_ui);
88 return nullptr; 101 return nullptr;
89 } 102 }
90 103
91 WebUI::TypeID GetWebUIType(BrowserContext* browser_context, 104 WebUI::TypeID GetWebUIType(BrowserContext* browser_context,
92 const GURL& url) const override { 105 const GURL& url) const override {
93 // If WebUI creation is enabled for the test and this is a WebUI URL, 106 // If WebUI creation is enabled for the test and this is a WebUI URL,
94 // returns a mock WebUI type. 107 // returns a mock WebUI type.
95 if (should_create_webui_ && HasWebUIScheme(url)) { 108 if (should_create_webui_ && HasWebUIScheme(url)) {
96 return const_cast<RenderFrameHostManagerTestWebUIControllerFactory*>( 109 return reinterpret_cast<WebUI::TypeID>(type_);
97 this);
98 } 110 }
99 return WebUI::kNoWebUI; 111 return WebUI::kNoWebUI;
100 } 112 }
101 113
102 bool UseWebUIForURL(BrowserContext* browser_context, 114 bool UseWebUIForURL(BrowserContext* browser_context,
103 const GURL& url) const override { 115 const GURL& url) const override {
104 return HasWebUIScheme(url); 116 return HasWebUIScheme(url);
105 } 117 }
106 118
107 bool UseWebUIBindingsForURL(BrowserContext* browser_context, 119 bool UseWebUIBindingsForURL(BrowserContext* browser_context,
108 const GURL& url) const override { 120 const GURL& url) const override {
109 return HasWebUIScheme(url); 121 return HasWebUIScheme(url);
110 } 122 }
111 123
112 private: 124 private:
113 bool should_create_webui_; 125 bool should_create_webui_;
126 uintptr_t type_;
114 127
115 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostManagerTestWebUIControllerFactory); 128 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostManagerTestWebUIControllerFactory);
116 }; 129 };
117 130
118 class BeforeUnloadFiredWebContentsDelegate : public WebContentsDelegate { 131 class BeforeUnloadFiredWebContentsDelegate : public WebContentsDelegate {
119 public: 132 public:
120 BeforeUnloadFiredWebContentsDelegate() {} 133 BeforeUnloadFiredWebContentsDelegate() {}
121 ~BeforeUnloadFiredWebContentsDelegate() override {} 134 ~BeforeUnloadFiredWebContentsDelegate() override {}
122 135
123 void BeforeUnloadFired(WebContents* web_contents, 136 void BeforeUnloadFired(WebContents* web_contents,
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 // RenderWidgetHostView holds on to a reference to SurfaceManager, so it 294 // RenderWidgetHostView holds on to a reference to SurfaceManager, so it
282 // must be shut down before the ImageTransportFactory. 295 // must be shut down before the ImageTransportFactory.
283 ImageTransportFactory::Terminate(); 296 ImageTransportFactory::Terminate();
284 #endif 297 #endif
285 } 298 }
286 299
287 void set_should_create_webui(bool should_create_webui) { 300 void set_should_create_webui(bool should_create_webui) {
288 factory_.set_should_create_webui(should_create_webui); 301 factory_.set_should_create_webui(should_create_webui);
289 } 302 }
290 303
304 void set_webui_type(int type) { factory_.set_webui_type(type); }
305
291 void NavigateActiveAndCommit(const GURL& url) { 306 void NavigateActiveAndCommit(const GURL& url) {
292 // Note: we navigate the active RenderFrameHost because previous navigations 307 // Note: we navigate the active RenderFrameHost because previous navigations
293 // won't have committed yet, so NavigateAndCommit does the wrong thing 308 // won't have committed yet, so NavigateAndCommit does the wrong thing
294 // for us. 309 // for us.
295 controller().LoadURL( 310 controller().LoadURL(
296 url, Referrer(), ui::PAGE_TRANSITION_LINK, std::string()); 311 url, Referrer(), ui::PAGE_TRANSITION_LINK, std::string());
297 int entry_id = controller().GetPendingEntry()->GetUniqueID(); 312 int entry_id = controller().GetPendingEntry()->GetUniqueID();
298 313
299 // Simulate the BeforeUnload_ACK that is received from the current renderer 314 // Simulate the BeforeUnload_ACK that is received from the current renderer
300 // for a cross-site navigation. 315 // for a cross-site navigation.
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 // Tests currently only navigate using main frame FrameNavigationEntries. 433 // Tests currently only navigate using main frame FrameNavigationEntries.
419 FrameNavigationEntry* frame_entry = entry.root_node()->frame_entry.get(); 434 FrameNavigationEntry* frame_entry = entry.root_node()->frame_entry.get();
420 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 435 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
421 switches::kEnableBrowserSideNavigation)) { 436 switches::kEnableBrowserSideNavigation)) {
422 scoped_ptr<NavigationRequest> navigation_request = 437 scoped_ptr<NavigationRequest> navigation_request =
423 NavigationRequest::CreateBrowserInitiated( 438 NavigationRequest::CreateBrowserInitiated(
424 manager->frame_tree_node_, frame_entry->url(), 439 manager->frame_tree_node_, frame_entry->url(),
425 frame_entry->referrer(), *frame_entry, entry, 440 frame_entry->referrer(), *frame_entry, entry,
426 FrameMsg_Navigate_Type::NORMAL, false, base::TimeTicks::Now(), 441 FrameMsg_Navigate_Type::NORMAL, false, base::TimeTicks::Now(),
427 static_cast<NavigationControllerImpl*>(&controller())); 442 static_cast<NavigationControllerImpl*>(&controller()));
443
444 // Simulates request creation that triggers the 1st internal call to
445 // GetFrameHostForNavigation.
446 manager->DidCreateNavigationRequest(*navigation_request);
447
448 // And also simulates the 2nd and final call to GetFrameHostForNavigation
449 // that determines the final frame that will commit the navigation.
428 TestRenderFrameHost* frame_host = static_cast<TestRenderFrameHost*>( 450 TestRenderFrameHost* frame_host = static_cast<TestRenderFrameHost*>(
429 manager->GetFrameHostForNavigation(*navigation_request)); 451 manager->GetFrameHostForNavigation(*navigation_request));
430 CHECK(frame_host); 452 CHECK(frame_host);
431 frame_host->set_pending_commit(true); 453 frame_host->set_pending_commit(true);
432 return frame_host; 454 return frame_host;
433 } 455 }
434 456
435 return manager->Navigate(frame_entry->url(), *frame_entry, entry); 457 return manager->Navigate(frame_entry->url(), *frame_entry, entry);
436 } 458 }
437 459
(...skipping 2212 matching lines...) Expand 10 before | Expand all | Expand 10 after
2650 RenderFrameProxyHost* proxy = 2672 RenderFrameProxyHost* proxy =
2651 root->render_manager()->GetRenderFrameProxyHost(hostC->GetSiteInstance()); 2673 root->render_manager()->GetRenderFrameProxyHost(hostC->GetSiteInstance());
2652 EXPECT_TRUE(proxy); 2674 EXPECT_TRUE(proxy);
2653 2675
2654 // Since the B->C navigation happened while the current page was focused, 2676 // Since the B->C navigation happened while the current page was focused,
2655 // page focus should propagate to the new subframe process. Check that 2677 // page focus should propagate to the new subframe process. Check that
2656 // process C received the proper focus message. 2678 // process C received the proper focus message.
2657 VerifyPageFocusMessage(hostC->GetProcess(), true, proxy->GetRoutingID()); 2679 VerifyPageFocusMessage(hostC->GetProcess(), true, proxy->GetRoutingID());
2658 } 2680 }
2659 2681
2682 // Creates a test class for PlzNavigate tests.
2683 class RenderFrameHostManagerTestWithBrowserSideNavigation
2684 : public RenderFrameHostManagerTest {
2685 public:
2686 void SetUp() override {
2687 EnableBrowserSideNavigation();
2688 RenderFrameHostManagerTest::SetUp();
2689 }
2690 };
2691
2692 // PlzNavigate: Tests that the correct intermediary and final navigation states
2693 // are reached when navigating from a renderer that is not live to a WebUI URL.
2694 TEST_F(RenderFrameHostManagerTestWithBrowserSideNavigation,
2695 NavigateFromDeadRendererToWebUI) {
2696 set_should_create_webui(true);
2697 RenderViewHostChangedObserver change_observer(contents());
2698 RenderFrameHostManager* manager = contents()->GetRenderManagerForTesting();
2699
2700 RenderFrameHostImpl* initial_host = manager->current_frame_host();
2701 ASSERT_TRUE(initial_host);
2702 EXPECT_FALSE(initial_host->IsRenderFrameLive());
2703
2704 // Navigation request.
2705 const GURL kUrl("chrome://foo");
2706 NavigationEntryImpl entry(NULL /* instance */, -1 /* page_id */, kUrl,
2707 Referrer(), base::string16() /* title */,
2708 ui::PAGE_TRANSITION_TYPED,
2709 false /* is_renderer_init */);
2710 FrameNavigationEntry* frame_entry = entry.root_node()->frame_entry.get();
2711 scoped_ptr<NavigationRequest> navigation_request =
2712 NavigationRequest::CreateBrowserInitiated(
2713 contents()->GetFrameTree()->root(), frame_entry->url(),
2714 frame_entry->referrer(), *frame_entry, entry,
2715 FrameMsg_Navigate_Type::NORMAL, false, base::TimeTicks::Now(),
2716 static_cast<NavigationControllerImpl*>(&controller()));
2717 manager->DidCreateNavigationRequest(*navigation_request);
2718
2719 // As the initial renderer was not live, the new RenderFrameHost should be
2720 // made immediately active at request time.
2721 EXPECT_TRUE(change_observer.DidHostChange());
2722 EXPECT_FALSE(GetPendingFrameHost(manager));
2723 TestRenderFrameHost* host =
2724 static_cast<TestRenderFrameHost*>(manager->current_frame_host());
2725 ASSERT_TRUE(host);
2726 EXPECT_NE(host, initial_host);
2727 EXPECT_TRUE(host->IsRenderFrameLive());
2728 WebUIImpl* web_ui = host->web_ui();
2729 EXPECT_TRUE(web_ui);
2730
2731 // Prepare to commit, update the navigating RenderFrameHost.
2732 EXPECT_EQ(host, manager->GetFrameHostForNavigation(*navigation_request));
2733
2734 // No pending RenderFrameHost as the current one should be reused.
2735 EXPECT_FALSE(GetPendingFrameHost(manager));
2736
2737 // But there should be a pending WebUI set to re-use the current one.
2738 EXPECT_EQ(web_ui, host->web_ui());
2739
2740 // The RenderFrameHost committed.
2741 manager->DidNavigateFrame(host, true);
2742 EXPECT_FALSE(change_observer.DidHostChange());
2743 EXPECT_EQ(host, manager->current_frame_host());
2744 EXPECT_FALSE(GetPendingFrameHost(manager));
2745 EXPECT_EQ(web_ui, host->web_ui());
2746 }
2747
2748 // PlzNavigate: Tests that the correct intermediary and final navigation states
2749 // are reached when navigating same-site between two WebUIs of the same type.
2750 TEST_F(RenderFrameHostManagerTestWithBrowserSideNavigation,
2751 NavigateSameSiteBetweenWebUIs) {
2752 set_should_create_webui(true);
2753 NavigateActiveAndCommit(GURL("chrome://foo"));
2754
2755 RenderFrameHostManager* manager = contents()->GetRenderManagerForTesting();
2756 TestRenderFrameHost* host =
2757 static_cast<TestRenderFrameHost*>(manager->current_frame_host());
2758 EXPECT_TRUE(host->IsRenderFrameLive());
2759 WebUIImpl* web_ui = host->web_ui();
2760 EXPECT_TRUE(web_ui);
2761
2762 RenderViewHostChangedObserver change_observer(contents());
2763
2764 // Navigation request. No change in the returned WebUI type.
2765 const GURL kUrl("chrome://foo/bar");
2766 NavigationEntryImpl entry(NULL /* instance */, -1 /* page_id */, kUrl,
2767 Referrer(), base::string16() /* title */,
2768 ui::PAGE_TRANSITION_TYPED,
2769 false /* is_renderer_init */);
2770 FrameNavigationEntry* frame_entry = entry.root_node()->frame_entry.get();
2771 scoped_ptr<NavigationRequest> navigation_request =
2772 NavigationRequest::CreateBrowserInitiated(
2773 contents()->GetFrameTree()->root(), frame_entry->url(),
2774 frame_entry->referrer(), *frame_entry, entry,
2775 FrameMsg_Navigate_Type::NORMAL, false, base::TimeTicks::Now(),
2776 static_cast<NavigationControllerImpl*>(&controller()));
2777 manager->DidCreateNavigationRequest(*navigation_request);
2778
2779 // The current WebUI should still be in place and there should be a new
2780 // pending WebUI instance in the current RenderFrameHost.
2781 EXPECT_FALSE(change_observer.DidHostChange());
2782 EXPECT_FALSE(GetPendingFrameHost(manager));
2783 EXPECT_EQ(web_ui, host->web_ui());
2784
2785 // Prepare to commit, update the navigating RenderFrameHost.
2786 EXPECT_EQ(host, manager->GetFrameHostForNavigation(*navigation_request));
2787
2788 EXPECT_FALSE(GetPendingFrameHost(manager));
2789 EXPECT_EQ(web_ui, host->web_ui());
2790
2791 // The RenderFrameHost committed.
2792 manager->DidNavigateFrame(host, true);
2793 EXPECT_FALSE(change_observer.DidHostChange());
2794 EXPECT_EQ(web_ui, host->web_ui());
2795 }
2796
2797 // PlzNavigate: Tests that the correct intermediary and final navigation states
2798 // are reached when navigating cross-site between two different WebUI types.
2799 TEST_F(RenderFrameHostManagerTestWithBrowserSideNavigation,
2800 NavigateCrossSiteBetweenWebUIs) {
2801 // Cross-site navigations will always cause the change of the WebUI instance
2802 // but for consistency sake different types will be set for each navigation.
2803 set_should_create_webui(true);
2804 set_webui_type(1);
2805 NavigateActiveAndCommit(GURL("chrome://foo"));
2806
2807 RenderFrameHostManager* manager = contents()->GetRenderManagerForTesting();
2808 TestRenderFrameHost* host =
2809 static_cast<TestRenderFrameHost*>(manager->current_frame_host());
2810 EXPECT_TRUE(host->IsRenderFrameLive());
2811 EXPECT_TRUE(host->web_ui());
2812
2813 RenderViewHostChangedObserver change_observer(contents());
2814 set_webui_type(2);
2815
2816 // Navigation request.
2817 const GURL kUrl("chrome://bar");
2818 NavigationEntryImpl entry(NULL /* instance */, -1 /* page_id */, kUrl,
2819 Referrer(), base::string16() /* title */,
2820 ui::PAGE_TRANSITION_TYPED,
2821 false /* is_renderer_init */);
2822 FrameNavigationEntry* frame_entry = entry.root_node()->frame_entry.get();
2823 scoped_ptr<NavigationRequest> navigation_request =
2824 NavigationRequest::CreateBrowserInitiated(
2825 contents()->GetFrameTree()->root(), frame_entry->url(),
2826 frame_entry->referrer(), *frame_entry, entry,
2827 FrameMsg_Navigate_Type::NORMAL, false, base::TimeTicks::Now(),
2828 static_cast<NavigationControllerImpl*>(&controller()));
2829 manager->DidCreateNavigationRequest(*navigation_request);
2830
2831 // The current WebUI should still be in place and there should be a new
2832 // active WebUI instance in the speculative RenderFrameHost.
2833 EXPECT_FALSE(change_observer.DidHostChange());
2834 TestRenderFrameHost* speculative_host =
2835 static_cast<TestRenderFrameHost*>(GetPendingFrameHost(manager));
2836 EXPECT_TRUE(speculative_host);
2837 EXPECT_TRUE(manager->current_frame_host()->web_ui());
2838 WebUIImpl* next_web_ui = speculative_host->web_ui();
2839 EXPECT_TRUE(next_web_ui);
2840 EXPECT_NE(next_web_ui, manager->current_frame_host()->web_ui());
2841
2842 // Prepare to commit, update the navigating RenderFrameHost.
2843 EXPECT_EQ(speculative_host,
2844 manager->GetFrameHostForNavigation(*navigation_request));
2845
2846 EXPECT_EQ(speculative_host, GetPendingFrameHost(manager));
2847 EXPECT_TRUE(manager->current_frame_host()->web_ui());
2848 EXPECT_NE(next_web_ui, manager->current_frame_host()->web_ui());
2849 EXPECT_EQ(next_web_ui, speculative_host->web_ui());
2850
2851 // The RenderFrameHost committed.
2852 manager->DidNavigateFrame(speculative_host, true);
2853 EXPECT_EQ(speculative_host, manager->current_frame_host());
2854 EXPECT_TRUE(change_observer.DidHostChange());
2855 EXPECT_EQ(next_web_ui, manager->current_frame_host()->web_ui());
2856 EXPECT_FALSE(GetPendingFrameHost(manager));
2857 }
2858
2660 } // namespace content 2859 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698