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