| 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/renderer_host/frame_tree.h" |
| 6 |
| 7 #include "base/run_loop.h" |
| 8 #include "content/browser/renderer_host/render_frame_host_impl.h" |
| 9 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 10 #include "content/public/test/mock_render_process_host.h" |
| 11 #include "content/public/test/test_browser_context.h" |
| 12 #include "content/public/test/test_browser_thread_bundle.h" |
| 13 #include "content/public/test/test_renderer_host.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace content { |
| 17 namespace { |
| 18 |
| 19 class FrameTreeTest : public RenderViewHostTestHarness { |
| 20 }; |
| 21 |
| 22 // The root node never changes during navigation even though its |
| 23 // RenderFrameHost does. |
| 24 // - Swapping main frame doesn't change root node. |
| 25 // - Swapping back to NULL doesn't crash (easier tear-down for interstitials). |
| 26 // - Main frame does not own RenderFrameHost. |
| 27 TEST_F(FrameTreeTest, RootNode) { |
| 28 FrameTree frame_tree; |
| 29 |
| 30 // Initial state has empty node. |
| 31 FrameTreeNode* root = frame_tree.GetRootForTesting(); |
| 32 ASSERT_TRUE(root); |
| 33 EXPECT_FALSE(frame_tree.GetMainFrame()); |
| 34 |
| 35 // Swap in main frame. |
| 36 RenderFrameHostImpl* dummy = reinterpret_cast<RenderFrameHostImpl*>(0x1); |
| 37 frame_tree.SwapMainFrame(dummy); |
| 38 EXPECT_EQ(root, frame_tree.GetRootForTesting()); |
| 39 EXPECT_EQ(dummy, frame_tree.GetMainFrame()); |
| 40 |
| 41 // Move back to NULL. |
| 42 frame_tree.SwapMainFrame(NULL); |
| 43 EXPECT_EQ(root, frame_tree.GetRootForTesting()); |
| 44 EXPECT_FALSE(frame_tree.GetMainFrame()); |
| 45 |
| 46 // Move back to an invalid pointer, let the FrameTree go out of scope. Test |
| 47 // should not crash because the main frame isn't owned. |
| 48 frame_tree.SwapMainFrame(dummy); |
| 49 } |
| 50 |
| 51 // Test that swapping the main frame resets the renderer-assigned frame id. |
| 52 // - On creation, frame id is unassigned. |
| 53 // - After a swap, frame id is unassigned. |
| 54 TEST_F(FrameTreeTest, FirstNavigationAfterSwap) { |
| 55 FrameTree frame_tree; |
| 56 |
| 57 EXPECT_TRUE(frame_tree.IsFirstNavigationAfterSwap()); |
| 58 EXPECT_EQ(FrameTreeNode::kInvalidFrameId, |
| 59 frame_tree.GetRootForTesting()->frame_id()); |
| 60 frame_tree.OnFirstNavigationAfterSwap(1); |
| 61 EXPECT_FALSE(frame_tree.IsFirstNavigationAfterSwap()); |
| 62 EXPECT_EQ(1, frame_tree.GetRootForTesting()->frame_id()); |
| 63 |
| 64 frame_tree.SwapMainFrame(NULL); |
| 65 EXPECT_TRUE(frame_tree.IsFirstNavigationAfterSwap()); |
| 66 EXPECT_EQ(FrameTreeNode::kInvalidFrameId, |
| 67 frame_tree.GetRootForTesting()->frame_id()); |
| 68 } |
| 69 |
| 70 // Exercise tree manipulation routines. |
| 71 // - Add a series of nodes and verify tree structure. |
| 72 // - Remove a series of nodes and verify tree structure. |
| 73 TEST_F(FrameTreeTest, Shape) { |
| 74 FrameTree frame_tree; |
| 75 std::string no_children_node("no children node"); |
| 76 std::string deep_subtree("node with deep subtree"); |
| 77 |
| 78 // Ensure the top-level node of the FrameTree is initialized by simulating a |
| 79 // main frame swap here. |
| 80 RenderFrameHostImpl render_frame_host(static_cast<RenderViewHostImpl*>(rvh()), |
| 81 &frame_tree, |
| 82 process()->GetNextRoutingID(), false); |
| 83 frame_tree.SwapMainFrame(&render_frame_host); |
| 84 frame_tree.OnFirstNavigationAfterSwap(5); |
| 85 |
| 86 // Simulate attaching a series of frames to build the frame tree. |
| 87 frame_tree.AddFrame(process()->GetNextRoutingID(), 5, 14, std::string()); |
| 88 frame_tree.AddFrame(process()->GetNextRoutingID(), 5, 15, std::string()); |
| 89 frame_tree.AddFrame(process()->GetNextRoutingID(), 5, 16, std::string()); |
| 90 |
| 91 frame_tree.AddFrame(process()->GetNextRoutingID(), 14, 244, std::string()); |
| 92 frame_tree.AddFrame(process()->GetNextRoutingID(), 14, 245, std::string()); |
| 93 |
| 94 frame_tree.AddFrame(process()->GetNextRoutingID(), 15, 255, no_children_node); |
| 95 |
| 96 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 264, std::string()); |
| 97 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 265, std::string()); |
| 98 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 266, std::string()); |
| 99 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 267, deep_subtree); |
| 100 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 268, std::string()); |
| 101 |
| 102 frame_tree.AddFrame(process()->GetNextRoutingID(), 267, 365, std::string()); |
| 103 frame_tree.AddFrame(process()->GetNextRoutingID(), 365, 455, std::string()); |
| 104 frame_tree.AddFrame(process()->GetNextRoutingID(), 455, 555, std::string()); |
| 105 frame_tree.AddFrame(process()->GetNextRoutingID(), 555, 655, std::string()); |
| 106 |
| 107 // Now, verify the tree structure is as expected. |
| 108 FrameTreeNode* root = frame_tree.GetRootForTesting(); |
| 109 EXPECT_EQ(5, root->frame_id()); |
| 110 EXPECT_EQ(3UL, root->child_count()); |
| 111 |
| 112 EXPECT_EQ(2UL, root->child_at(0)->child_count()); |
| 113 EXPECT_EQ(0UL, root->child_at(0)->child_at(0)->child_count()); |
| 114 EXPECT_EQ(0UL, root->child_at(0)->child_at(1)->child_count()); |
| 115 |
| 116 EXPECT_EQ(1UL, root->child_at(1)->child_count()); |
| 117 EXPECT_EQ(0UL, root->child_at(1)->child_at(0)->child_count()); |
| 118 EXPECT_STREQ(no_children_node.c_str(), |
| 119 root->child_at(1)->child_at(0)->frame_name().c_str()); |
| 120 |
| 121 EXPECT_EQ(5UL, root->child_at(2)->child_count()); |
| 122 EXPECT_EQ(0UL, root->child_at(2)->child_at(0)->child_count()); |
| 123 EXPECT_EQ(0UL, root->child_at(2)->child_at(1)->child_count()); |
| 124 EXPECT_EQ(0UL, root->child_at(2)->child_at(2)->child_count()); |
| 125 EXPECT_EQ(1UL, root->child_at(2)->child_at(3)->child_count()); |
| 126 EXPECT_STREQ(deep_subtree.c_str(), |
| 127 root->child_at(2)->child_at(3)->frame_name().c_str()); |
| 128 EXPECT_EQ(0UL, root->child_at(2)->child_at(4)->child_count()); |
| 129 |
| 130 FrameTreeNode* deep_tree = root->child_at(2)->child_at(3)->child_at(0); |
| 131 EXPECT_EQ(365, deep_tree->frame_id()); |
| 132 EXPECT_EQ(1UL, deep_tree->child_count()); |
| 133 EXPECT_EQ(455, deep_tree->child_at(0)->frame_id()); |
| 134 EXPECT_EQ(1UL, deep_tree->child_at(0)->child_count()); |
| 135 EXPECT_EQ(555, deep_tree->child_at(0)->child_at(0)->frame_id()); |
| 136 EXPECT_EQ(1UL, deep_tree->child_at(0)->child_at(0)->child_count()); |
| 137 EXPECT_EQ(655, deep_tree->child_at(0)->child_at(0)->child_at(0)->frame_id()); |
| 138 EXPECT_EQ(0UL, |
| 139 deep_tree->child_at(0)->child_at(0)->child_at(0)->child_count()); |
| 140 |
| 141 // Test removing of nodes. |
| 142 frame_tree.RemoveFrame(555, 655); |
| 143 EXPECT_EQ(0UL, deep_tree->child_at(0)->child_at(0)->child_count()); |
| 144 |
| 145 frame_tree.RemoveFrame(16, 265); |
| 146 EXPECT_EQ(4UL, root->child_at(2)->child_count()); |
| 147 |
| 148 frame_tree.RemoveFrame(5, 15); |
| 149 EXPECT_EQ(2UL, root->child_count()); |
| 150 } |
| 151 |
| 152 } // namespace |
| 153 } // namespace content |
| OLD | NEW |