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" |
11 #include "content/browser/frame_host/render_frame_host_impl.h" | 11 #include "content/browser/frame_host/render_frame_host_impl.h" |
12 #include "content/browser/renderer_host/render_view_host_impl.h" | 12 #include "content/browser/renderer_host/render_view_host_impl.h" |
13 #include "content/browser/web_contents/web_contents_impl.h" | 13 #include "content/browser/web_contents/web_contents_impl.h" |
14 #include "content/common/view_messages.h" | 14 #include "content/common/view_messages.h" |
15 #include "content/public/browser/web_contents_observer.h" | 15 #include "content/public/browser/web_contents_observer.h" |
16 #include "content/public/test/mock_render_process_host.h" | 16 #include "content/public/test/mock_render_process_host.h" |
17 #include "content/public/test/test_browser_context.h" | 17 #include "content/public/test/test_browser_context.h" |
18 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
19 #include "content/test/test_render_frame_host.h" | 19 #include "content/test/test_render_frame_host.h" |
20 #include "content/test/test_render_view_host.h" | 20 #include "content/test/test_render_view_host.h" |
21 #include "content/test/test_web_contents.h" | 21 #include "content/test/test_web_contents.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
24 namespace content { | 24 namespace content { |
25 | |
26 namespace { | 25 namespace { |
27 | 26 |
28 // Appends a description of the structure of the frame tree to |result|. | 27 // Appends a description of the structure of the frame tree to |result|. |
29 void AppendTreeNodeState(FrameTreeNode* node, std::string* result) { | 28 void AppendTreeNodeState(FrameTreeNode* node, std::string* result) { |
30 result->append( | 29 result->append( |
31 base::Int64ToString(node->current_frame_host()->GetRoutingID())); | 30 base::Int64ToString(node->current_frame_host()->GetRoutingID())); |
32 if (!node->frame_name().empty()) { | 31 if (!node->frame_name().empty()) { |
33 result->append(" '"); | 32 result->append(" '"); |
34 result->append(node->frame_name()); | 33 result->append(node->frame_name()); |
35 result->append("'"); | 34 result->append("'"); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 void LogWhatHappened(const std::string& event_name, RenderFrameHost* rfh) { | 95 void LogWhatHappened(const std::string& event_name, RenderFrameHost* rfh) { |
97 LogWhatHappened( | 96 LogWhatHappened( |
98 base::StringPrintf("%s(%d)", event_name.c_str(), rfh->GetRoutingID())); | 97 base::StringPrintf("%s(%d)", event_name.c_str(), rfh->GetRoutingID())); |
99 } | 98 } |
100 | 99 |
101 std::string log_; | 100 std::string log_; |
102 | 101 |
103 DISALLOW_COPY_AND_ASSIGN(TreeWalkingWebContentsLogger); | 102 DISALLOW_COPY_AND_ASSIGN(TreeWalkingWebContentsLogger); |
104 }; | 103 }; |
105 | 104 |
106 } // namespace | |
107 | |
108 class FrameTreeTest : public RenderViewHostImplTestHarness { | 105 class FrameTreeTest : public RenderViewHostImplTestHarness { |
109 protected: | 106 protected: |
110 // Prints a FrameTree, for easy assertions of the tree hierarchy. | 107 // Prints a FrameTree, for easy assertions of the tree hierarchy. |
111 std::string GetTreeState(FrameTree* frame_tree) { | 108 std::string GetTreeState(FrameTree* frame_tree) { |
112 std::string result; | 109 std::string result; |
113 AppendTreeNodeState(frame_tree->root(), &result); | 110 AppendTreeNodeState(frame_tree->root(), &result); |
114 return result; | 111 return result; |
115 } | 112 } |
116 }; | 113 }; |
117 | 114 |
118 // Exercise tree manipulation routines. | 115 // Exercise tree manipulation routines. |
119 // - Add a series of nodes and verify tree structure. | 116 // - Add a series of nodes and verify tree structure. |
120 // - Remove a series of nodes and verify tree structure. | 117 // - Remove a series of nodes and verify tree structure. |
121 TEST_F(FrameTreeTest, Shape) { | 118 TEST_F(FrameTreeTest, Shape) { |
122 // Use the FrameTree of the WebContents so that it has all the delegates it | 119 // Use the FrameTree of the WebContents so that it has all the delegates it |
123 // needs. We may want to consider a test version of this. | 120 // needs. We may want to consider a test version of this. |
124 FrameTree* frame_tree = contents()->GetFrameTree(); | 121 FrameTree* frame_tree = contents()->GetFrameTree(); |
125 FrameTreeNode* root = frame_tree->root(); | 122 FrameTreeNode* root = frame_tree->root(); |
126 | 123 |
127 std::string no_children_node("no children node"); | 124 std::string no_children_node("no children node"); |
128 std::string deep_subtree("node with deep subtree"); | 125 std::string deep_subtree("node with deep subtree"); |
129 int process_id = root->current_frame_host()->GetProcess()->GetID(); | |
130 | 126 |
131 ASSERT_EQ("1: []", GetTreeState(frame_tree)); | 127 ASSERT_EQ("1: []", GetTreeState(frame_tree)); |
132 | 128 |
133 // Simulate attaching a series of frames to build the frame tree. | 129 // Simulate attaching a series of frames to build the frame tree. |
134 frame_tree->AddFrame(root, process_id, 14, std::string()); | 130 frame_tree->AddFrame(root, 14, std::string()); |
135 frame_tree->AddFrame(root, process_id, 15, std::string()); | 131 frame_tree->AddFrame(root, 15, std::string()); |
136 frame_tree->AddFrame(root, process_id, 16, std::string()); | 132 frame_tree->AddFrame(root, 16, std::string()); |
137 | 133 |
138 frame_tree->AddFrame(root->child_at(0), process_id, 244, std::string()); | 134 frame_tree->AddFrame(root->child_at(0), 244, std::string()); |
139 frame_tree->AddFrame(root->child_at(1), process_id, 255, no_children_node); | 135 frame_tree->AddFrame(root->child_at(1), 255, no_children_node); |
140 frame_tree->AddFrame(root->child_at(0), process_id, 245, std::string()); | 136 frame_tree->AddFrame(root->child_at(0), 245, std::string()); |
141 | 137 |
142 ASSERT_EQ("1: [14: [244: [], 245: []], " | 138 ASSERT_EQ("1: [14: [244: [], 245: []], " |
143 "15: [255 'no children node': []], " | 139 "15: [255 'no children node': []], " |
144 "16: []]", | 140 "16: []]", |
145 GetTreeState(frame_tree)); | 141 GetTreeState(frame_tree)); |
146 | 142 |
147 FrameTreeNode* child_16 = root->child_at(2); | 143 FrameTreeNode* child_16 = root->child_at(2); |
148 frame_tree->AddFrame(child_16, process_id, 264, std::string()); | 144 frame_tree->AddFrame(child_16, 264, std::string()); |
149 frame_tree->AddFrame(child_16, process_id, 265, std::string()); | 145 frame_tree->AddFrame(child_16, 265, std::string()); |
150 frame_tree->AddFrame(child_16, process_id, 266, std::string()); | 146 frame_tree->AddFrame(child_16, 266, std::string()); |
151 frame_tree->AddFrame(child_16, process_id, 267, deep_subtree); | 147 frame_tree->AddFrame(child_16, 267, deep_subtree); |
152 frame_tree->AddFrame(child_16, process_id, 268, std::string()); | 148 frame_tree->AddFrame(child_16, 268, std::string()); |
153 | 149 |
154 FrameTreeNode* child_267 = child_16->child_at(3); | 150 FrameTreeNode* child_267 = child_16->child_at(3); |
155 frame_tree->AddFrame(child_267, process_id, 365, std::string()); | 151 frame_tree->AddFrame(child_267, 365, std::string()); |
156 frame_tree->AddFrame(child_267->child_at(0), process_id, 455, std::string()); | 152 frame_tree->AddFrame(child_267->child_at(0), 455, std::string()); |
157 frame_tree->AddFrame(child_267->child_at(0)->child_at(0), process_id, 555, | 153 frame_tree->AddFrame(child_267->child_at(0)->child_at(0), 555, std::string()); |
| 154 frame_tree->AddFrame(child_267->child_at(0)->child_at(0)->child_at(0), 655, |
158 std::string()); | 155 std::string()); |
159 frame_tree->AddFrame(child_267->child_at(0)->child_at(0)->child_at(0), | |
160 process_id, 655, std::string()); | |
161 | 156 |
162 // Now that's it's fully built, verify the tree structure is as expected. | 157 // Now that's it's fully built, verify the tree structure is as expected. |
163 ASSERT_EQ("1: [14: [244: [], 245: []], " | 158 ASSERT_EQ("1: [14: [244: [], 245: []], " |
164 "15: [255 'no children node': []], " | 159 "15: [255 'no children node': []], " |
165 "16: [264: [], 265: [], 266: [], " | 160 "16: [264: [], 265: [], 266: [], " |
166 "267 'node with deep subtree': " | 161 "267 'node with deep subtree': " |
167 "[365: [455: [555: [655: []]]]], 268: []]]", | 162 "[365: [455: [555: [655: []]]]], 268: []]]", |
168 GetTreeState(frame_tree)); | 163 GetTreeState(frame_tree)); |
169 | 164 |
170 FrameTreeNode* child_555 = child_267->child_at(0)->child_at(0)->child_at(0); | 165 FrameTreeNode* child_555 = child_267->child_at(0)->child_at(0)->child_at(0); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 // Crash the renderer | 220 // Crash the renderer |
226 test_rvh()->OnMessageReceived(ViewHostMsg_RenderProcessGone( | 221 test_rvh()->OnMessageReceived(ViewHostMsg_RenderProcessGone( |
227 0, base::TERMINATION_STATUS_PROCESS_CRASHED, -1)); | 222 0, base::TERMINATION_STATUS_PROCESS_CRASHED, -1)); |
228 EXPECT_EQ( | 223 EXPECT_EQ( |
229 "RenderFrameDeleted(22) -> 1: []\n" | 224 "RenderFrameDeleted(22) -> 1: []\n" |
230 "RenderFrameDeleted(23) -> 1: []\n" | 225 "RenderFrameDeleted(23) -> 1: []\n" |
231 "RenderProcessGone -> 1: []", | 226 "RenderProcessGone -> 1: []", |
232 activity.GetLog()); | 227 activity.GetLog()); |
233 } | 228 } |
234 | 229 |
235 // Ensure that frames are not added to the tree, if the process passed in | 230 } // namespace |
236 // is different than the process of the parent node. | |
237 TEST_F(FrameTreeTest, FailAddFrameWithWrongProcessId) { | |
238 FrameTree* frame_tree = contents()->GetFrameTree(); | |
239 FrameTreeNode* root = frame_tree->root(); | |
240 int process_id = root->current_frame_host()->GetProcess()->GetID(); | |
241 | |
242 ASSERT_EQ("1: []", GetTreeState(frame_tree)); | |
243 | |
244 // Simulate attaching a frame from mismatched process id. | |
245 ASSERT_FALSE(frame_tree->AddFrame(root, process_id + 1, 1, std::string())); | |
246 ASSERT_EQ("1: []", GetTreeState(frame_tree)); | |
247 } | |
248 | |
249 } // namespace content | 231 } // namespace content |
OLD | NEW |