OLD | NEW |
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 "content/browser/frame_host/frame_tree.h" | 5 #include "content/browser/frame_host/frame_tree.h" |
6 | 6 |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "content/browser/frame_host/navigator_impl.h" | 9 #include "content/browser/frame_host/navigator_impl.h" |
10 #include "content/browser/frame_host/render_frame_host_factory.h" | 10 #include "content/browser/frame_host/render_frame_host_factory.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 protected: | 24 protected: |
25 // Prints a FrameTree, for easy assertions of the tree hierarchy. | 25 // Prints a FrameTree, for easy assertions of the tree hierarchy. |
26 std::string GetTreeState(FrameTree* frame_tree) { | 26 std::string GetTreeState(FrameTree* frame_tree) { |
27 std::string result; | 27 std::string result; |
28 AppendTreeNodeState(frame_tree->root(), &result); | 28 AppendTreeNodeState(frame_tree->root(), &result); |
29 return result; | 29 return result; |
30 } | 30 } |
31 | 31 |
32 private: | 32 private: |
33 void AppendTreeNodeState(FrameTreeNode* node, std::string* result) { | 33 void AppendTreeNodeState(FrameTreeNode* node, std::string* result) { |
34 result->append(base::Int64ToString(node->frame_id())); | 34 int frame_id = node->current_frame_host()->render_view_host()-> |
| 35 GetFrameIDForTesting(node->frame_tree_node_id()); |
| 36 result->append(base::Int64ToString(frame_id)); |
35 if (!node->frame_name().empty()) { | 37 if (!node->frame_name().empty()) { |
36 result->append(" '"); | 38 result->append(" '"); |
37 result->append(node->frame_name()); | 39 result->append(node->frame_name()); |
38 result->append("'"); | 40 result->append("'"); |
39 } | 41 } |
40 result->append(": ["); | 42 result->append(": ["); |
41 const char* separator = ""; | 43 const char* separator = ""; |
42 for (size_t i = 0; i < node->child_count(); i++) { | 44 for (size_t i = 0; i < node->child_count(); i++) { |
43 result->append(separator); | 45 result->append(separator); |
44 AppendTreeNodeState(node->child_at(i), result); | 46 AppendTreeNodeState(node->child_at(i), result); |
45 separator = ", "; | 47 separator = ", "; |
46 } | 48 } |
47 result->append("]"); | 49 result->append("]"); |
48 } | 50 } |
49 }; | 51 }; |
50 | 52 |
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(new NavigatorImpl(NULL, NULL), NULL, NULL, NULL, NULL); | |
56 | |
57 EXPECT_TRUE(frame_tree.IsFirstNavigationAfterSwap()); | |
58 EXPECT_EQ(FrameTreeNode::kInvalidFrameId, | |
59 frame_tree.root()->frame_id()); | |
60 frame_tree.OnFirstNavigationAfterSwap(1); | |
61 EXPECT_FALSE(frame_tree.IsFirstNavigationAfterSwap()); | |
62 EXPECT_EQ(1, frame_tree.root()->frame_id()); | |
63 | |
64 frame_tree.ResetForMainFrameSwap(); | |
65 EXPECT_TRUE(frame_tree.IsFirstNavigationAfterSwap()); | |
66 EXPECT_EQ(FrameTreeNode::kInvalidFrameId, | |
67 frame_tree.root()->frame_id()); | |
68 } | |
69 | |
70 // Exercise tree manipulation routines. | 53 // Exercise tree manipulation routines. |
71 // - Add a series of nodes and verify tree structure. | 54 // - Add a series of nodes and verify tree structure. |
72 // - Remove a series of nodes and verify tree structure. | 55 // - Remove a series of nodes and verify tree structure. |
73 TEST_F(FrameTreeTest, Shape) { | 56 TEST_F(FrameTreeTest, Shape) { |
74 // Use the FrameTree of the WebContents so that it has all the delegates it | 57 // Use the FrameTree of the WebContents so that it has all the delegates it |
75 // needs. We may want to consider a test version of this. | 58 // needs. We may want to consider a test version of this. |
76 FrameTree* frame_tree = | 59 FrameTree* frame_tree = |
77 static_cast<WebContentsImpl*>(web_contents())->GetFrameTree(); | 60 static_cast<WebContentsImpl*>(web_contents())->GetFrameTree(); |
78 | 61 |
| 62 // Navigate to a URL to register the frame ID. |
| 63 NavigateAndCommit(GURL("about:blank")); |
| 64 |
79 std::string no_children_node("no children node"); | 65 std::string no_children_node("no children node"); |
80 std::string deep_subtree("node with deep subtree"); | 66 std::string deep_subtree("node with deep subtree"); |
81 | 67 |
82 frame_tree->OnFirstNavigationAfterSwap(5); | 68 // Ensure the top-level node of the FrameTree is initialized by simulating a |
| 69 // main frame swap here. |
| 70 RenderViewHostImpl* rvhi = static_cast<RenderViewHostImpl*>(rvh()); |
83 | 71 |
84 ASSERT_EQ("5: []", GetTreeState(frame_tree)); | 72 // kFrameId in TestRenderViewHost is 13. |
| 73 ASSERT_EQ("13: []", GetTreeState(frame_tree)); |
85 | 74 |
86 // Simulate attaching a series of frames to build the frame tree. | 75 // Simulate attaching a series of frames to build the frame tree. |
87 frame_tree->AddFrame(process()->GetNextRoutingID(), 5, 14, std::string()); | 76 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 13, 14, |
88 frame_tree->AddFrame(process()->GetNextRoutingID(), 5, 15, std::string()); | 77 std::string()); |
89 frame_tree->AddFrame(process()->GetNextRoutingID(), 5, 16, std::string()); | 78 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 13, 15, |
| 79 std::string()); |
| 80 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 13, 16, |
| 81 std::string()); |
90 | 82 |
91 frame_tree->AddFrame(process()->GetNextRoutingID(), 14, 244, std::string()); | 83 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 14, 244, |
92 frame_tree->AddFrame(process()->GetNextRoutingID(), 15, 255, | 84 std::string()); |
| 85 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 15, 255, |
93 no_children_node); | 86 no_children_node); |
94 frame_tree->AddFrame(process()->GetNextRoutingID(), 14, 245, std::string()); | 87 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 14, 245, |
| 88 std::string()); |
95 | 89 |
96 ASSERT_EQ("5: [14: [244: [], 245: []], " | 90 ASSERT_EQ("13: [14: [244: [], 245: []], " |
97 "15: [255 'no children node': []], " | 91 "15: [255 'no children node': []], " |
98 "16: []]", | 92 "16: []]", |
99 GetTreeState(frame_tree)); | 93 GetTreeState(frame_tree)); |
100 | 94 |
101 frame_tree->AddFrame(process()->GetNextRoutingID(), 16, 264, std::string()); | 95 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 16, 264, |
102 frame_tree->AddFrame(process()->GetNextRoutingID(), 16, 265, std::string()); | 96 std::string()); |
103 frame_tree->AddFrame(process()->GetNextRoutingID(), 16, 266, std::string()); | 97 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 16, 265, |
104 frame_tree->AddFrame(process()->GetNextRoutingID(), 16, 267, deep_subtree); | 98 std::string()); |
105 frame_tree->AddFrame(process()->GetNextRoutingID(), 16, 268, std::string()); | 99 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 16, 266, |
| 100 std::string()); |
| 101 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 16, 267, |
| 102 deep_subtree); |
| 103 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 16, 268, |
| 104 std::string()); |
106 | 105 |
107 frame_tree->AddFrame(process()->GetNextRoutingID(), 267, 365, std::string()); | 106 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 267, 365, |
108 frame_tree->AddFrame(process()->GetNextRoutingID(), 365, 455, std::string()); | 107 std::string()); |
109 frame_tree->AddFrame(process()->GetNextRoutingID(), 455, 555, std::string()); | 108 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 365, 455, |
110 frame_tree->AddFrame(process()->GetNextRoutingID(), 555, 655, std::string()); | 109 std::string()); |
| 110 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 455, 555, |
| 111 std::string()); |
| 112 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 555, 655, |
| 113 std::string()); |
111 | 114 |
112 // Now that's it's fully built, verify the tree structure is as expected. | 115 // Now that's it's fully built, verify the tree structure is as expected. |
113 ASSERT_EQ("5: [14: [244: [], 245: []], " | 116 ASSERT_EQ("13: [14: [244: [], 245: []], " |
114 "15: [255 'no children node': []], " | 117 "15: [255 'no children node': []], " |
115 "16: [264: [], 265: [], 266: [], " | 118 "16: [264: [], 265: [], 266: [], " |
116 "267 'node with deep subtree': " | 119 "267 'node with deep subtree': " |
117 "[365: [455: [555: [655: []]]]], 268: []]]", | 120 "[365: [455: [555: [655: []]]]], 268: []]]", |
118 GetTreeState(frame_tree)); | 121 GetTreeState(frame_tree)); |
119 | 122 |
120 // Test removing of nodes. Clear the frame removal listener so we can pass a | 123 // Test removing of nodes. Clear the frame removal listener so we can pass a |
121 // NULL RFH here. | 124 // NULL RFH here. |
122 frame_tree->ClearFrameRemoveListenerForTesting(); | 125 frame_tree->ClearFrameRemoveListenerForTesting(); |
123 frame_tree->RemoveFrame(NULL, 555, 655); | 126 frame_tree->RemoveFrame(NULL, rvhi->GetFrameTreeNodeID(555), |
124 ASSERT_EQ("5: [14: [244: [], 245: []], " | 127 rvhi->GetFrameTreeNodeID(655), 655); |
125 "15: [255 'no children node': []], " | 128 ASSERT_EQ("13: [14: [244: [], 245: []], " |
126 "16: [264: [], 265: [], 266: [], " | 129 "15: [255 'no children node': []], " |
127 "267 'node with deep subtree': " | 130 "16: [264: [], 265: [], 266: [], " |
128 "[365: [455: [555: []]]], 268: []]]", | 131 "267 'node with deep subtree': " |
| 132 "[365: [455: [555: []]]], 268: []]]", |
129 GetTreeState(frame_tree)); | 133 GetTreeState(frame_tree)); |
130 | 134 |
131 frame_tree->RemoveFrame(NULL, 16, 265); | 135 frame_tree->RemoveFrame(NULL, rvhi->GetFrameTreeNodeID(16), |
132 ASSERT_EQ("5: [14: [244: [], 245: []], " | 136 rvhi->GetFrameTreeNodeID(265), 265); |
133 "15: [255 'no children node': []], " | 137 ASSERT_EQ("13: [14: [244: [], 245: []], " |
134 "16: [264: [], 266: [], " | 138 "15: [255 'no children node': []], " |
135 "267 'node with deep subtree': " | 139 "16: [264: [], 266: [], " |
136 "[365: [455: [555: []]]], 268: []]]", | 140 "267 'node with deep subtree': " |
| 141 "[365: [455: [555: []]]], 268: []]]", |
137 GetTreeState(frame_tree)); | 142 GetTreeState(frame_tree)); |
138 | 143 |
139 frame_tree->RemoveFrame(NULL, 5, 15); | 144 frame_tree->RemoveFrame(NULL, rvhi->GetFrameTreeNodeID(13), |
140 ASSERT_EQ("5: [14: [244: [], 245: []], " | 145 rvhi->GetFrameTreeNodeID(15), 15); |
141 "16: [264: [], 266: [], " | 146 ASSERT_EQ("13: [14: [244: [], 245: []], " |
142 "267 'node with deep subtree': " | 147 "16: [264: [], 266: [], " |
143 "[365: [455: [555: []]]], 268: []]]", | 148 "267 'node with deep subtree': " |
| 149 "[365: [455: [555: []]]], 268: []]]", |
144 GetTreeState(frame_tree)); | 150 GetTreeState(frame_tree)); |
145 } | 151 } |
146 | 152 |
147 } // namespace | 153 } // namespace |
148 } // namespace content | 154 } // namespace content |
OLD | NEW |