OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/frame_host/frame_tree.h" |
| 6 #include "content/browser/frame_host/frame_tree_node.h" |
| 7 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 8 #include "content/browser/web_contents/web_contents_impl.h" |
| 9 #include "content/public/browser/notification_service.h" |
| 10 #include "content/public/browser/notification_types.h" |
| 11 #include "content/public/common/url_constants.h" |
| 12 #include "content/public/test/browser_test_utils.h" |
| 13 #include "content/public/test/test_navigation_observer.h" |
| 14 #include "content/public/test/test_utils.h" |
| 15 #include "content/shell/browser/shell.h" |
| 16 #include "content/test/content_browser_test.h" |
| 17 #include "content/test/content_browser_test_utils.h" |
| 18 #include "net/dns/mock_host_resolver.h" |
| 19 |
| 20 namespace content { |
| 21 |
| 22 class FrameTreeBrowserTest : public ContentBrowserTest { |
| 23 public: |
| 24 FrameTreeBrowserTest() {} |
| 25 |
| 26 private: |
| 27 DISALLOW_COPY_AND_ASSIGN(FrameTreeBrowserTest); |
| 28 }; |
| 29 |
| 30 // Ensures FrameTree correctly reflects page structure during navigations. |
| 31 IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, FrameTreeShape) { |
| 32 host_resolver()->AddRule("*", "127.0.0.1"); |
| 33 ASSERT_TRUE(test_server()->Start()); |
| 34 |
| 35 GURL base_url = test_server()->GetURL("files/site_isolation/"); |
| 36 GURL::Replacements replace_host; |
| 37 std::string host_str("A.com"); // Must stay in scope with replace_host. |
| 38 replace_host.SetHostStr(host_str); |
| 39 base_url = base_url.ReplaceComponents(replace_host); |
| 40 |
| 41 // Load doc without iframes. Verify FrameTree just has root. |
| 42 // Frame tree: |
| 43 // Site-A Root |
| 44 NavigateToURL(shell(), base_url.Resolve("blank.html")); |
| 45 FrameTreeNode* root = |
| 46 static_cast<WebContentsImpl*>(shell()->web_contents())-> |
| 47 GetFrameTree()->root(); |
| 48 EXPECT_EQ(0U, root->child_count()); |
| 49 |
| 50 // Add 2 same-site frames. Verify 3 nodes in tree with proper names. |
| 51 // Frame tree: |
| 52 // Site-A Root -- Site-A frame1 |
| 53 // \-- Site-A frame2 |
| 54 WindowedNotificationObserver observer1( |
| 55 content::NOTIFICATION_LOAD_STOP, |
| 56 content::Source<NavigationController>( |
| 57 &shell()->web_contents()->GetController())); |
| 58 NavigateToURL(shell(), base_url.Resolve("frames-X-X.html")); |
| 59 observer1.Wait(); |
| 60 ASSERT_EQ(2U, root->child_count()); |
| 61 EXPECT_EQ(0U, root->child_at(0)->child_count()); |
| 62 EXPECT_EQ(0U, root->child_at(1)->child_count()); |
| 63 } |
| 64 |
| 65 // TODO(ajwong): Talk with nasko and merge this functionality with |
| 66 // FrameTreeShape. |
| 67 IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, FrameTreeShape2) { |
| 68 ASSERT_TRUE(test_server()->Start()); |
| 69 NavigateToURL(shell(), |
| 70 test_server()->GetURL("files/frame_tree/top.html")); |
| 71 |
| 72 WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 73 RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>( |
| 74 wc->GetRenderViewHost()); |
| 75 FrameTreeNode* root = wc->GetFrameTree()->root(); |
| 76 |
| 77 // Check that the root node is properly created with the frame id of the |
| 78 // initial navigation. |
| 79 ASSERT_EQ(3UL, root->child_count()); |
| 80 EXPECT_EQ(std::string(), root->frame_name()); |
| 81 EXPECT_EQ(rvh->main_frame_id(), root->frame_id()); |
| 82 |
| 83 ASSERT_EQ(2UL, root->child_at(0)->child_count()); |
| 84 EXPECT_STREQ("1-1-name", root->child_at(0)->frame_name().c_str()); |
| 85 |
| 86 // Verify the deepest node exists and has the right name. |
| 87 ASSERT_EQ(2UL, root->child_at(2)->child_count()); |
| 88 EXPECT_EQ(1UL, root->child_at(2)->child_at(1)->child_count()); |
| 89 EXPECT_EQ(0UL, root->child_at(2)->child_at(1)->child_at(0)->child_count()); |
| 90 EXPECT_STREQ("3-1-id", |
| 91 root->child_at(2)->child_at(1)->child_at(0)->frame_name().c_str()); |
| 92 |
| 93 // Navigate to about:blank, which should leave only the root node of the frame |
| 94 // tree in the browser process. |
| 95 NavigateToURL(shell(), test_server()->GetURL("files/title1.html")); |
| 96 |
| 97 root = wc->GetFrameTree()->root(); |
| 98 EXPECT_EQ(0UL, root->child_count()); |
| 99 EXPECT_EQ(std::string(), root->frame_name()); |
| 100 EXPECT_EQ(rvh->main_frame_id(), root->frame_id()); |
| 101 } |
| 102 |
| 103 // Test that we can navigate away if the previous renderer doesn't clean up its |
| 104 // child frames. |
| 105 IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, FrameTreeAfterCrash) { |
| 106 ASSERT_TRUE(test_server()->Start()); |
| 107 NavigateToURL(shell(), |
| 108 test_server()->GetURL("files/frame_tree/top.html")); |
| 109 |
| 110 // Crash the renderer so that it doesn't send any FrameDetached messages. |
| 111 RenderProcessHostWatcher crash_observer( |
| 112 shell()->web_contents(), |
| 113 RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT); |
| 114 NavigateToURL(shell(), GURL(kChromeUICrashURL)); |
| 115 crash_observer.Wait(); |
| 116 |
| 117 // The frame tree should be cleared, and the frame ID should be reset. |
| 118 WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 119 RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>( |
| 120 wc->GetRenderViewHost()); |
| 121 FrameTreeNode* root = wc->GetFrameTree()->root(); |
| 122 EXPECT_EQ(0UL, root->child_count()); |
| 123 EXPECT_EQ(FrameTreeNode::kInvalidFrameId, root->frame_id()); |
| 124 EXPECT_EQ(rvh->main_frame_id(), root->frame_id()); |
| 125 |
| 126 // Navigate to a new URL. |
| 127 NavigateToURL(shell(), test_server()->GetURL("files/title1.html")); |
| 128 |
| 129 // The frame ID should now be set. |
| 130 EXPECT_EQ(0UL, root->child_count()); |
| 131 EXPECT_NE(FrameTreeNode::kInvalidFrameId, root->frame_id()); |
| 132 EXPECT_EQ(rvh->main_frame_id(), root->frame_id()); |
| 133 } |
| 134 |
| 135 // Test that we can navigate away if the previous renderer doesn't clean up its |
| 136 // child frames. |
| 137 IN_PROC_BROWSER_TEST_F(FrameTreeBrowserTest, NavigateWithLeftoverFrames) { |
| 138 host_resolver()->AddRule("*", "127.0.0.1"); |
| 139 ASSERT_TRUE(test_server()->Start()); |
| 140 |
| 141 GURL base_url = test_server()->GetURL("files/site_isolation/"); |
| 142 GURL::Replacements replace_host; |
| 143 std::string host_str("A.com"); // Must stay in scope with replace_host. |
| 144 replace_host.SetHostStr(host_str); |
| 145 base_url = base_url.ReplaceComponents(replace_host); |
| 146 |
| 147 NavigateToURL(shell(), |
| 148 test_server()->GetURL("files/frame_tree/top.html")); |
| 149 |
| 150 // Hang the renderer so that it doesn't send any FrameDetached messages. |
| 151 // (This navigation will never complete, so don't wait for it.) |
| 152 shell()->LoadURL(GURL(kChromeUIHangURL)); |
| 153 |
| 154 // Check that the frame tree still has children. |
| 155 WebContentsImpl* wc = static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 156 FrameTreeNode* root = wc->GetFrameTree()->root(); |
| 157 ASSERT_EQ(3UL, root->child_count()); |
| 158 |
| 159 // Navigate to a new URL. We use LoadURL because NavigateToURL will try to |
| 160 // wait for the previous navigation to stop. |
| 161 TestNavigationObserver tab_observer(wc, 1); |
| 162 shell()->LoadURL(base_url.Resolve("blank.html")); |
| 163 tab_observer.Wait(); |
| 164 |
| 165 // The frame tree should now be cleared, and the frame ID should be valid. |
| 166 EXPECT_EQ(0UL, root->child_count()); |
| 167 EXPECT_NE(FrameTreeNode::kInvalidFrameId, root->frame_id()); |
| 168 } |
| 169 |
| 170 } // namespace content |
OLD | NEW |