blob: a22c8a9797c399ce0fffaed1f4d0384c5f034ddc [file] [log] [blame]
[email protected]9b159a52013-10-03 17:24:551// Copyright 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
[email protected]d4a8ca482013-10-30 21:06:405#include "content/browser/frame_host/frame_tree.h"
[email protected]9b159a52013-10-03 17:24:556
7#include "base/run_loop.h"
[email protected]7cc7ebd2013-10-08 00:59:008#include "base/strings/string_number_conversions.h"
[email protected]6ea6bdf2013-12-06 13:35:019#include "content/browser/frame_host/navigator_impl.h"
[email protected]2a18ee222013-11-21 07:52:4410#include "content/browser/frame_host/render_frame_host_factory.h"
[email protected]d4a8ca482013-10-30 21:06:4011#include "content/browser/frame_host/render_frame_host_impl.h"
[email protected]9b159a52013-10-03 17:24:5512#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]94d0cc12013-12-18 00:07:4113#include "content/browser/web_contents/web_contents_impl.h"
[email protected]14266072014-04-19 00:35:2014#include "content/common/view_messages.h"
15#include "content/public/browser/web_contents_observer.h"
[email protected]9b159a52013-10-03 17:24:5516#include "content/public/test/mock_render_process_host.h"
17#include "content/public/test/test_browser_context.h"
18#include "content/public/test/test_browser_thread_bundle.h"
[email protected]6b50e362014-08-15 05:15:5919#include "content/test/test_render_frame_host.h"
[email protected]14266072014-04-19 00:35:2020#include "content/test/test_render_view_host.h"
21#include "content/test/test_web_contents.h"
[email protected]9b159a52013-10-03 17:24:5522#include "testing/gtest/include/gtest/gtest.h"
23
24namespace content {
dgroganfb22f9a2014-10-20 21:32:3225
[email protected]9b159a52013-10-03 17:24:5526namespace {
27
[email protected]14266072014-04-19 00:35:2028// Appends a description of the structure of the frame tree to |result|.
29void AppendTreeNodeState(FrameTreeNode* node, std::string* result) {
30 result->append(
31 base::Int64ToString(node->current_frame_host()->GetRoutingID()));
32 if (!node->frame_name().empty()) {
33 result->append(" '");
34 result->append(node->frame_name());
35 result->append("'");
36 }
37 result->append(": [");
38 const char* separator = "";
39 for (size_t i = 0; i < node->child_count(); i++) {
40 result->append(separator);
41 AppendTreeNodeState(node->child_at(i), result);
42 separator = ", ";
43 }
44 result->append("]");
45}
46
47// Logs calls to WebContentsObserver along with the state of the frame tree,
48// for later use in EXPECT_EQ().
49class TreeWalkingWebContentsLogger : public WebContentsObserver {
50 public:
51 explicit TreeWalkingWebContentsLogger(WebContents* web_contents)
52 : WebContentsObserver(web_contents) {}
53
dchengc2282aa2014-10-21 12:07:5854 ~TreeWalkingWebContentsLogger() override {
[email protected]14266072014-04-19 00:35:2055 EXPECT_EQ("", log_) << "Activity logged that was not expected";
56 }
57
58 // Gets and resets the log, which is a string of what happened.
59 std::string GetLog() {
60 std::string result = log_;
61 log_.clear();
62 return result;
63 }
64
65 // content::WebContentsObserver implementation.
dchengc2282aa2014-10-21 12:07:5866 void RenderFrameCreated(RenderFrameHost* render_frame_host) override {
[email protected]14266072014-04-19 00:35:2067 LogWhatHappened("RenderFrameCreated", render_frame_host);
68 }
69
dchengc2282aa2014-10-21 12:07:5870 void RenderFrameHostChanged(RenderFrameHost* old_host,
71 RenderFrameHost* new_host) override {
[email protected]02d7b6e2014-06-24 21:01:5072 if (old_host)
73 LogWhatHappened("RenderFrameChanged(old)", old_host);
74 LogWhatHappened("RenderFrameChanged(new)", new_host);
75 }
76
dchengc2282aa2014-10-21 12:07:5877 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override {
[email protected]14266072014-04-19 00:35:2078 LogWhatHappened("RenderFrameDeleted", render_frame_host);
79 }
80
dchengc2282aa2014-10-21 12:07:5881 void RenderProcessGone(base::TerminationStatus status) override {
[email protected]14266072014-04-19 00:35:2082 LogWhatHappened("RenderProcessGone");
83 }
84
85 private:
86 void LogWhatHappened(const std::string& event_name) {
87 if (!log_.empty()) {
88 log_.append("\n");
89 }
90 log_.append(event_name + " -> ");
91 AppendTreeNodeState(
92 static_cast<WebContentsImpl*>(web_contents())->GetFrameTree()->root(),
93 &log_);
94 }
95
96 void LogWhatHappened(const std::string& event_name, RenderFrameHost* rfh) {
97 LogWhatHappened(
98 base::StringPrintf("%s(%d)", event_name.c_str(), rfh->GetRoutingID()));
99 }
100
101 std::string log_;
102
103 DISALLOW_COPY_AND_ASSIGN(TreeWalkingWebContentsLogger);
104};
105
dgroganfb22f9a2014-10-20 21:32:32106} // namespace
107
[email protected]14266072014-04-19 00:35:20108class FrameTreeTest : public RenderViewHostImplTestHarness {
[email protected]7cc7ebd2013-10-08 00:59:00109 protected:
110 // Prints a FrameTree, for easy assertions of the tree hierarchy.
111 std::string GetTreeState(FrameTree* frame_tree) {
112 std::string result;
[email protected]fa944cb82013-11-15 17:51:21113 AppendTreeNodeState(frame_tree->root(), &result);
[email protected]7cc7ebd2013-10-08 00:59:00114 return result;
115 }
[email protected]9b159a52013-10-03 17:24:55116};
117
[email protected]9b159a52013-10-03 17:24:55118// Exercise tree manipulation routines.
119// - Add a series of nodes and verify tree structure.
120// - Remove a series of nodes and verify tree structure.
nickf9acfbe2014-12-23 19:12:37121//
122// TODO(nick): http://crbug.com/444722 Disabled temporarily because of a bad
123// interaction with the WebContentsObserverConsistencyChecker -- calling
124// AddFrame directly causes the RFH to not be announced. We either need to
125// rewrite this test, or be consistent in the layer at which we announce render
126// frame creation.
127TEST_F(FrameTreeTest, DISABLED_Shape) {
[email protected]94d0cc12013-12-18 00:07:41128 // Use the FrameTree of the WebContents so that it has all the delegates it
129 // needs. We may want to consider a test version of this.
[email protected]14266072014-04-19 00:35:20130 FrameTree* frame_tree = contents()->GetFrameTree();
[email protected]58faf942014-02-20 21:03:58131 FrameTreeNode* root = frame_tree->root();
[email protected]190b8c52013-11-09 01:35:44132
[email protected]9b159a52013-10-03 17:24:55133 std::string no_children_node("no children node");
134 std::string deep_subtree("node with deep subtree");
dgroganfb22f9a2014-10-20 21:32:32135 int process_id = root->current_frame_host()->GetProcess()->GetID();
[email protected]9b159a52013-10-03 17:24:55136
[email protected]58faf942014-02-20 21:03:58137 ASSERT_EQ("1: []", GetTreeState(frame_tree));
[email protected]7cc7ebd2013-10-08 00:59:00138
[email protected]9b159a52013-10-03 17:24:55139 // Simulate attaching a series of frames to build the frame tree.
dgroganfb22f9a2014-10-20 21:32:32140 frame_tree->AddFrame(root, process_id, 14, std::string());
141 frame_tree->AddFrame(root, process_id, 15, std::string());
142 frame_tree->AddFrame(root, process_id, 16, std::string());
[email protected]9b159a52013-10-03 17:24:55143
dgroganfb22f9a2014-10-20 21:32:32144 frame_tree->AddFrame(root->child_at(0), process_id, 244, std::string());
145 frame_tree->AddFrame(root->child_at(1), process_id, 255, no_children_node);
146 frame_tree->AddFrame(root->child_at(0), process_id, 245, std::string());
[email protected]9b159a52013-10-03 17:24:55147
[email protected]58faf942014-02-20 21:03:58148 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00149 "15: [255 'no children node': []], "
150 "16: []]",
[email protected]94d0cc12013-12-18 00:07:41151 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55152
[email protected]58faf942014-02-20 21:03:58153 FrameTreeNode* child_16 = root->child_at(2);
dgroganfb22f9a2014-10-20 21:32:32154 frame_tree->AddFrame(child_16, process_id, 264, std::string());
155 frame_tree->AddFrame(child_16, process_id, 265, std::string());
156 frame_tree->AddFrame(child_16, process_id, 266, std::string());
157 frame_tree->AddFrame(child_16, process_id, 267, deep_subtree);
158 frame_tree->AddFrame(child_16, process_id, 268, std::string());
[email protected]9b159a52013-10-03 17:24:55159
[email protected]58faf942014-02-20 21:03:58160 FrameTreeNode* child_267 = child_16->child_at(3);
dgroganfb22f9a2014-10-20 21:32:32161 frame_tree->AddFrame(child_267, process_id, 365, std::string());
162 frame_tree->AddFrame(child_267->child_at(0), process_id, 455, std::string());
163 frame_tree->AddFrame(child_267->child_at(0)->child_at(0), process_id, 555,
[email protected]58faf942014-02-20 21:03:58164 std::string());
dgroganfb22f9a2014-10-20 21:32:32165 frame_tree->AddFrame(child_267->child_at(0)->child_at(0)->child_at(0),
166 process_id, 655, std::string());
[email protected]9b159a52013-10-03 17:24:55167
[email protected]7cc7ebd2013-10-08 00:59:00168 // Now that's it's fully built, verify the tree structure is as expected.
[email protected]58faf942014-02-20 21:03:58169 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00170 "15: [255 'no children node': []], "
171 "16: [264: [], 265: [], 266: [], "
172 "267 'node with deep subtree': "
173 "[365: [455: [555: [655: []]]]], 268: []]]",
[email protected]94d0cc12013-12-18 00:07:41174 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55175
[email protected]58faf942014-02-20 21:03:58176 FrameTreeNode* child_555 = child_267->child_at(0)->child_at(0)->child_at(0);
177 frame_tree->RemoveFrame(child_555);
178 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00179 "15: [255 'no children node': []], "
180 "16: [264: [], 265: [], 266: [], "
181 "267 'node with deep subtree': "
[email protected]58faf942014-02-20 21:03:58182 "[365: [455: []]], 268: []]]",
[email protected]94d0cc12013-12-18 00:07:41183 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55184
[email protected]58faf942014-02-20 21:03:58185 frame_tree->RemoveFrame(child_16->child_at(1));
186 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00187 "15: [255 'no children node': []], "
188 "16: [264: [], 266: [], "
189 "267 'node with deep subtree': "
[email protected]58faf942014-02-20 21:03:58190 "[365: [455: []]], 268: []]]",
[email protected]94d0cc12013-12-18 00:07:41191 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55192
[email protected]58faf942014-02-20 21:03:58193 frame_tree->RemoveFrame(root->child_at(1));
194 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00195 "16: [264: [], 266: [], "
196 "267 'node with deep subtree': "
[email protected]58faf942014-02-20 21:03:58197 "[365: [455: []]], 268: []]]",
[email protected]94d0cc12013-12-18 00:07:41198 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55199}
200
[email protected]14266072014-04-19 00:35:20201// Do some simple manipulations of the frame tree, making sure that
202// WebContentsObservers see a consistent view of the tree as we go.
203TEST_F(FrameTreeTest, ObserverWalksTreeDuringFrameCreation) {
204 TreeWalkingWebContentsLogger activity(contents());
205 FrameTree* frame_tree = contents()->GetFrameTree();
206 FrameTreeNode* root = frame_tree->root();
207
[email protected]02d7b6e2014-06-24 21:01:50208 EXPECT_EQ("", activity.GetLog());
209
[email protected]14266072014-04-19 00:35:20210 // Simulate attaching a series of frames to build the frame tree.
211 main_test_rfh()->OnCreateChildFrame(14, std::string());
212 EXPECT_EQ("RenderFrameCreated(14) -> 1: [14: []]", activity.GetLog());
213 main_test_rfh()->OnCreateChildFrame(18, std::string());
214 EXPECT_EQ("RenderFrameCreated(18) -> 1: [14: [], 18: []]", activity.GetLog());
215 frame_tree->RemoveFrame(root->child_at(0));
216 EXPECT_EQ("RenderFrameDeleted(14) -> 1: [18: []]", activity.GetLog());
217 frame_tree->RemoveFrame(root->child_at(0));
218 EXPECT_EQ("RenderFrameDeleted(18) -> 1: []", activity.GetLog());
219}
220
221// Make sure that WebContentsObservers see a consistent view of the tree after
222// recovery from a render process crash.
223TEST_F(FrameTreeTest, ObserverWalksTreeAfterCrash) {
224 TreeWalkingWebContentsLogger activity(contents());
225
226 main_test_rfh()->OnCreateChildFrame(22, std::string());
227 EXPECT_EQ("RenderFrameCreated(22) -> 1: [22: []]", activity.GetLog());
228 main_test_rfh()->OnCreateChildFrame(23, std::string());
229 EXPECT_EQ("RenderFrameCreated(23) -> 1: [22: [], 23: []]", activity.GetLog());
230
231 // Crash the renderer
232 test_rvh()->OnMessageReceived(ViewHostMsg_RenderProcessGone(
233 0, base::TERMINATION_STATUS_PROCESS_CRASHED, -1));
234 EXPECT_EQ(
235 "RenderFrameDeleted(22) -> 1: []\n"
236 "RenderFrameDeleted(23) -> 1: []\n"
237 "RenderProcessGone -> 1: []",
238 activity.GetLog());
239}
240
dgroganfb22f9a2014-10-20 21:32:32241// Ensure that frames are not added to the tree, if the process passed in
242// is different than the process of the parent node.
243TEST_F(FrameTreeTest, FailAddFrameWithWrongProcessId) {
244 FrameTree* frame_tree = contents()->GetFrameTree();
245 FrameTreeNode* root = frame_tree->root();
246 int process_id = root->current_frame_host()->GetProcess()->GetID();
247
248 ASSERT_EQ("1: []", GetTreeState(frame_tree));
249
250 // Simulate attaching a frame from mismatched process id.
251 ASSERT_FALSE(frame_tree->AddFrame(root, process_id + 1, 1, std::string()));
252 ASSERT_EQ("1: []", GetTreeState(frame_tree));
253}
254
[email protected]9b159a52013-10-03 17:24:55255} // namespace content