blob: bb705c7f881adc195a656a8539e0e1a1977e5135 [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.
121TEST_F(FrameTreeTest, Shape) {
[email protected]94d0cc12013-12-18 00:07:41122 // 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.
[email protected]14266072014-04-19 00:35:20124 FrameTree* frame_tree = contents()->GetFrameTree();
[email protected]58faf942014-02-20 21:03:58125 FrameTreeNode* root = frame_tree->root();
[email protected]190b8c52013-11-09 01:35:44126
[email protected]9b159a52013-10-03 17:24:55127 std::string no_children_node("no children node");
128 std::string deep_subtree("node with deep subtree");
dgroganfb22f9a2014-10-20 21:32:32129 int process_id = root->current_frame_host()->GetProcess()->GetID();
[email protected]9b159a52013-10-03 17:24:55130
[email protected]58faf942014-02-20 21:03:58131 ASSERT_EQ("1: []", GetTreeState(frame_tree));
[email protected]7cc7ebd2013-10-08 00:59:00132
[email protected]9b159a52013-10-03 17:24:55133 // Simulate attaching a series of frames to build the frame tree.
dgroganfb22f9a2014-10-20 21:32:32134 frame_tree->AddFrame(root, process_id, 14, std::string());
135 frame_tree->AddFrame(root, process_id, 15, std::string());
136 frame_tree->AddFrame(root, process_id, 16, std::string());
[email protected]9b159a52013-10-03 17:24:55137
dgroganfb22f9a2014-10-20 21:32:32138 frame_tree->AddFrame(root->child_at(0), process_id, 244, std::string());
139 frame_tree->AddFrame(root->child_at(1), process_id, 255, no_children_node);
140 frame_tree->AddFrame(root->child_at(0), process_id, 245, std::string());
[email protected]9b159a52013-10-03 17:24:55141
[email protected]58faf942014-02-20 21:03:58142 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00143 "15: [255 'no children node': []], "
144 "16: []]",
[email protected]94d0cc12013-12-18 00:07:41145 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55146
[email protected]58faf942014-02-20 21:03:58147 FrameTreeNode* child_16 = root->child_at(2);
dgroganfb22f9a2014-10-20 21:32:32148 frame_tree->AddFrame(child_16, process_id, 264, std::string());
149 frame_tree->AddFrame(child_16, process_id, 265, std::string());
150 frame_tree->AddFrame(child_16, process_id, 266, std::string());
151 frame_tree->AddFrame(child_16, process_id, 267, deep_subtree);
152 frame_tree->AddFrame(child_16, process_id, 268, std::string());
[email protected]9b159a52013-10-03 17:24:55153
[email protected]58faf942014-02-20 21:03:58154 FrameTreeNode* child_267 = child_16->child_at(3);
dgroganfb22f9a2014-10-20 21:32:32155 frame_tree->AddFrame(child_267, process_id, 365, std::string());
156 frame_tree->AddFrame(child_267->child_at(0), process_id, 455, std::string());
157 frame_tree->AddFrame(child_267->child_at(0)->child_at(0), process_id, 555,
[email protected]58faf942014-02-20 21:03:58158 std::string());
dgroganfb22f9a2014-10-20 21:32:32159 frame_tree->AddFrame(child_267->child_at(0)->child_at(0)->child_at(0),
160 process_id, 655, std::string());
[email protected]9b159a52013-10-03 17:24:55161
[email protected]7cc7ebd2013-10-08 00:59:00162 // Now that's it's fully built, verify the tree structure is as expected.
[email protected]58faf942014-02-20 21:03:58163 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00164 "15: [255 'no children node': []], "
165 "16: [264: [], 265: [], 266: [], "
166 "267 'node with deep subtree': "
167 "[365: [455: [555: [655: []]]]], 268: []]]",
[email protected]94d0cc12013-12-18 00:07:41168 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55169
[email protected]58faf942014-02-20 21:03:58170 FrameTreeNode* child_555 = child_267->child_at(0)->child_at(0)->child_at(0);
171 frame_tree->RemoveFrame(child_555);
172 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00173 "15: [255 'no children node': []], "
174 "16: [264: [], 265: [], 266: [], "
175 "267 'node with deep subtree': "
[email protected]58faf942014-02-20 21:03:58176 "[365: [455: []]], 268: []]]",
[email protected]94d0cc12013-12-18 00:07:41177 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55178
[email protected]58faf942014-02-20 21:03:58179 frame_tree->RemoveFrame(child_16->child_at(1));
180 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00181 "15: [255 'no children node': []], "
182 "16: [264: [], 266: [], "
183 "267 'node with deep subtree': "
[email protected]58faf942014-02-20 21:03:58184 "[365: [455: []]], 268: []]]",
[email protected]94d0cc12013-12-18 00:07:41185 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55186
[email protected]58faf942014-02-20 21:03:58187 frame_tree->RemoveFrame(root->child_at(1));
188 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00189 "16: [264: [], 266: [], "
190 "267 'node with deep subtree': "
[email protected]58faf942014-02-20 21:03:58191 "[365: [455: []]], 268: []]]",
[email protected]94d0cc12013-12-18 00:07:41192 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55193}
194
[email protected]14266072014-04-19 00:35:20195// Do some simple manipulations of the frame tree, making sure that
196// WebContentsObservers see a consistent view of the tree as we go.
197TEST_F(FrameTreeTest, ObserverWalksTreeDuringFrameCreation) {
198 TreeWalkingWebContentsLogger activity(contents());
199 FrameTree* frame_tree = contents()->GetFrameTree();
200 FrameTreeNode* root = frame_tree->root();
201
[email protected]02d7b6e2014-06-24 21:01:50202 EXPECT_EQ("", activity.GetLog());
203
[email protected]14266072014-04-19 00:35:20204 // Simulate attaching a series of frames to build the frame tree.
205 main_test_rfh()->OnCreateChildFrame(14, std::string());
206 EXPECT_EQ("RenderFrameCreated(14) -> 1: [14: []]", activity.GetLog());
207 main_test_rfh()->OnCreateChildFrame(18, std::string());
208 EXPECT_EQ("RenderFrameCreated(18) -> 1: [14: [], 18: []]", activity.GetLog());
209 frame_tree->RemoveFrame(root->child_at(0));
210 EXPECT_EQ("RenderFrameDeleted(14) -> 1: [18: []]", activity.GetLog());
211 frame_tree->RemoveFrame(root->child_at(0));
212 EXPECT_EQ("RenderFrameDeleted(18) -> 1: []", activity.GetLog());
213}
214
215// Make sure that WebContentsObservers see a consistent view of the tree after
216// recovery from a render process crash.
217TEST_F(FrameTreeTest, ObserverWalksTreeAfterCrash) {
218 TreeWalkingWebContentsLogger activity(contents());
219
220 main_test_rfh()->OnCreateChildFrame(22, std::string());
221 EXPECT_EQ("RenderFrameCreated(22) -> 1: [22: []]", activity.GetLog());
222 main_test_rfh()->OnCreateChildFrame(23, std::string());
223 EXPECT_EQ("RenderFrameCreated(23) -> 1: [22: [], 23: []]", activity.GetLog());
224
225 // Crash the renderer
226 test_rvh()->OnMessageReceived(ViewHostMsg_RenderProcessGone(
227 0, base::TERMINATION_STATUS_PROCESS_CRASHED, -1));
228 EXPECT_EQ(
229 "RenderFrameDeleted(22) -> 1: []\n"
230 "RenderFrameDeleted(23) -> 1: []\n"
231 "RenderProcessGone -> 1: []",
232 activity.GetLog());
233}
234
dgroganfb22f9a2014-10-20 21:32:32235// Ensure that frames are not added to the tree, if the process passed in
236// is different than the process of the parent node.
237TEST_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
[email protected]9b159a52013-10-03 17:24:55249} // namespace content