blob: e22ca30f9b015a2e6bdde7d0ac88fe0702b2bae6 [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"
naskob985af12015-02-06 04:15:3314#include "content/common/frame_messages.h"
[email protected]14266072014-04-19 00:35:2015#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"
dcheng5f60abb2015-05-28 01:39:3623#include "third_party/WebKit/public/web/WebSandboxFlags.h"
[email protected]9b159a52013-10-03 17:24:5524
25namespace content {
dgroganfb22f9a2014-10-20 21:32:3226
[email protected]9b159a52013-10-03 17:24:5527namespace {
28
[email protected]14266072014-04-19 00:35:2029// Appends a description of the structure of the frame tree to |result|.
30void AppendTreeNodeState(FrameTreeNode* node, std::string* result) {
31 result->append(
32 base::Int64ToString(node->current_frame_host()->GetRoutingID()));
schenney6408fed22015-04-17 17:44:5733 if (!node->current_frame_host()->IsRenderFrameLive())
34 result->append("*"); // Asterisk next to dead frames.
35
[email protected]14266072014-04-19 00:35:2036 if (!node->frame_name().empty()) {
37 result->append(" '");
38 result->append(node->frame_name());
39 result->append("'");
40 }
41 result->append(": [");
42 const char* separator = "";
43 for (size_t i = 0; i < node->child_count(); i++) {
44 result->append(separator);
45 AppendTreeNodeState(node->child_at(i), result);
46 separator = ", ";
47 }
48 result->append("]");
49}
50
51// Logs calls to WebContentsObserver along with the state of the frame tree,
52// for later use in EXPECT_EQ().
53class TreeWalkingWebContentsLogger : public WebContentsObserver {
54 public:
55 explicit TreeWalkingWebContentsLogger(WebContents* web_contents)
56 : WebContentsObserver(web_contents) {}
57
dchengc2282aa2014-10-21 12:07:5858 ~TreeWalkingWebContentsLogger() override {
[email protected]14266072014-04-19 00:35:2059 EXPECT_EQ("", log_) << "Activity logged that was not expected";
60 }
61
62 // Gets and resets the log, which is a string of what happened.
63 std::string GetLog() {
64 std::string result = log_;
65 log_.clear();
66 return result;
67 }
68
69 // content::WebContentsObserver implementation.
dchengc2282aa2014-10-21 12:07:5870 void RenderFrameCreated(RenderFrameHost* render_frame_host) override {
[email protected]14266072014-04-19 00:35:2071 LogWhatHappened("RenderFrameCreated", render_frame_host);
72 }
73
dchengc2282aa2014-10-21 12:07:5874 void RenderFrameHostChanged(RenderFrameHost* old_host,
75 RenderFrameHost* new_host) override {
[email protected]02d7b6e2014-06-24 21:01:5076 if (old_host)
naskof5940b9f2015-03-02 23:04:0577 LogWhatHappened("RenderFrameHostChanged(old)", old_host);
78 LogWhatHappened("RenderFrameHostChanged(new)", new_host);
[email protected]02d7b6e2014-06-24 21:01:5079 }
80
dchengc2282aa2014-10-21 12:07:5881 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override {
[email protected]14266072014-04-19 00:35:2082 LogWhatHappened("RenderFrameDeleted", render_frame_host);
83 }
84
dchengc2282aa2014-10-21 12:07:5885 void RenderProcessGone(base::TerminationStatus status) override {
[email protected]14266072014-04-19 00:35:2086 LogWhatHappened("RenderProcessGone");
87 }
88
89 private:
90 void LogWhatHappened(const std::string& event_name) {
91 if (!log_.empty()) {
92 log_.append("\n");
93 }
94 log_.append(event_name + " -> ");
95 AppendTreeNodeState(
96 static_cast<WebContentsImpl*>(web_contents())->GetFrameTree()->root(),
97 &log_);
98 }
99
100 void LogWhatHappened(const std::string& event_name, RenderFrameHost* rfh) {
101 LogWhatHappened(
102 base::StringPrintf("%s(%d)", event_name.c_str(), rfh->GetRoutingID()));
103 }
104
105 std::string log_;
106
107 DISALLOW_COPY_AND_ASSIGN(TreeWalkingWebContentsLogger);
108};
109
dgroganfb22f9a2014-10-20 21:32:32110} // namespace
111
[email protected]14266072014-04-19 00:35:20112class FrameTreeTest : public RenderViewHostImplTestHarness {
[email protected]7cc7ebd2013-10-08 00:59:00113 protected:
114 // Prints a FrameTree, for easy assertions of the tree hierarchy.
115 std::string GetTreeState(FrameTree* frame_tree) {
116 std::string result;
[email protected]fa944cb82013-11-15 17:51:21117 AppendTreeNodeState(frame_tree->root(), &result);
[email protected]7cc7ebd2013-10-08 00:59:00118 return result;
119 }
[email protected]9b159a52013-10-03 17:24:55120};
121
[email protected]9b159a52013-10-03 17:24:55122// Exercise tree manipulation routines.
123// - Add a series of nodes and verify tree structure.
124// - Remove a series of nodes and verify tree structure.
nickf9acfbe2014-12-23 19:12:37125//
126// TODO(nick): http://crbug.com/444722 Disabled temporarily because of a bad
127// interaction with the WebContentsObserverConsistencyChecker -- calling
128// AddFrame directly causes the RFH to not be announced. We either need to
129// rewrite this test, or be consistent in the layer at which we announce render
130// frame creation.
131TEST_F(FrameTreeTest, DISABLED_Shape) {
[email protected]94d0cc12013-12-18 00:07:41132 // Use the FrameTree of the WebContents so that it has all the delegates it
133 // needs. We may want to consider a test version of this.
[email protected]14266072014-04-19 00:35:20134 FrameTree* frame_tree = contents()->GetFrameTree();
[email protected]58faf942014-02-20 21:03:58135 FrameTreeNode* root = frame_tree->root();
[email protected]190b8c52013-11-09 01:35:44136
[email protected]9b159a52013-10-03 17:24:55137 std::string no_children_node("no children node");
138 std::string deep_subtree("node with deep subtree");
dgroganfb22f9a2014-10-20 21:32:32139 int process_id = root->current_frame_host()->GetProcess()->GetID();
[email protected]9b159a52013-10-03 17:24:55140
[email protected]58faf942014-02-20 21:03:58141 ASSERT_EQ("1: []", GetTreeState(frame_tree));
[email protected]7cc7ebd2013-10-08 00:59:00142
[email protected]9b159a52013-10-03 17:24:55143 // Simulate attaching a series of frames to build the frame tree.
dcheng860817a2015-05-22 03:16:56144 frame_tree->AddFrame(root, process_id, 14, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36145 std::string(), blink::WebSandboxFlags::None);
dcheng860817a2015-05-22 03:16:56146 frame_tree->AddFrame(root, process_id, 15, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36147 std::string(), blink::WebSandboxFlags::None);
dcheng860817a2015-05-22 03:16:56148 frame_tree->AddFrame(root, process_id, 16, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36149 std::string(), blink::WebSandboxFlags::None);
[email protected]9b159a52013-10-03 17:24:55150
dcheng860817a2015-05-22 03:16:56151 frame_tree->AddFrame(root->child_at(0), process_id, 244,
152 blink::WebTreeScopeType::Document, std::string(),
dcheng5f60abb2015-05-28 01:39:36153 blink::WebSandboxFlags::None);
dcheng860817a2015-05-22 03:16:56154 frame_tree->AddFrame(root->child_at(1), process_id, 255,
155 blink::WebTreeScopeType::Document, no_children_node,
dcheng5f60abb2015-05-28 01:39:36156 blink::WebSandboxFlags::None);
dcheng860817a2015-05-22 03:16:56157 frame_tree->AddFrame(root->child_at(0), process_id, 245,
158 blink::WebTreeScopeType::Document, std::string(),
dcheng5f60abb2015-05-28 01:39:36159 blink::WebSandboxFlags::None);
[email protected]9b159a52013-10-03 17:24:55160
[email protected]58faf942014-02-20 21:03:58161 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00162 "15: [255 'no children node': []], "
163 "16: []]",
[email protected]94d0cc12013-12-18 00:07:41164 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55165
[email protected]58faf942014-02-20 21:03:58166 FrameTreeNode* child_16 = root->child_at(2);
dcheng860817a2015-05-22 03:16:56167 frame_tree->AddFrame(child_16, process_id, 264,
168 blink::WebTreeScopeType::Document, std::string(),
dcheng5f60abb2015-05-28 01:39:36169 blink::WebSandboxFlags::None);
dcheng860817a2015-05-22 03:16:56170 frame_tree->AddFrame(child_16, process_id, 265,
171 blink::WebTreeScopeType::Document, std::string(),
dcheng5f60abb2015-05-28 01:39:36172 blink::WebSandboxFlags::None);
dcheng860817a2015-05-22 03:16:56173 frame_tree->AddFrame(child_16, process_id, 266,
174 blink::WebTreeScopeType::Document, std::string(),
dcheng5f60abb2015-05-28 01:39:36175 blink::WebSandboxFlags::None);
dcheng860817a2015-05-22 03:16:56176 frame_tree->AddFrame(child_16, process_id, 267,
177 blink::WebTreeScopeType::Document, deep_subtree,
dcheng5f60abb2015-05-28 01:39:36178 blink::WebSandboxFlags::None);
dcheng860817a2015-05-22 03:16:56179 frame_tree->AddFrame(child_16, process_id, 268,
180 blink::WebTreeScopeType::Document, std::string(),
dcheng5f60abb2015-05-28 01:39:36181 blink::WebSandboxFlags::None);
[email protected]9b159a52013-10-03 17:24:55182
[email protected]58faf942014-02-20 21:03:58183 FrameTreeNode* child_267 = child_16->child_at(3);
dcheng860817a2015-05-22 03:16:56184 frame_tree->AddFrame(child_267, process_id, 365,
185 blink::WebTreeScopeType::Document, std::string(),
dcheng5f60abb2015-05-28 01:39:36186 blink::WebSandboxFlags::None);
dcheng860817a2015-05-22 03:16:56187 frame_tree->AddFrame(child_267->child_at(0), process_id, 455,
188 blink::WebTreeScopeType::Document, std::string(),
dcheng5f60abb2015-05-28 01:39:36189 blink::WebSandboxFlags::None);
dgroganfb22f9a2014-10-20 21:32:32190 frame_tree->AddFrame(child_267->child_at(0)->child_at(0), process_id, 555,
dcheng860817a2015-05-22 03:16:56191 blink::WebTreeScopeType::Document, std::string(),
dcheng5f60abb2015-05-28 01:39:36192 blink::WebSandboxFlags::None);
dgroganfb22f9a2014-10-20 21:32:32193 frame_tree->AddFrame(child_267->child_at(0)->child_at(0)->child_at(0),
dcheng860817a2015-05-22 03:16:56194 process_id, 655, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36195 std::string(), blink::WebSandboxFlags::None);
[email protected]9b159a52013-10-03 17:24:55196
[email protected]7cc7ebd2013-10-08 00:59:00197 // Now that's it's fully built, verify the tree structure is as expected.
[email protected]58faf942014-02-20 21:03:58198 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00199 "15: [255 'no children node': []], "
200 "16: [264: [], 265: [], 266: [], "
201 "267 'node with deep subtree': "
202 "[365: [455: [555: [655: []]]]], 268: []]]",
[email protected]94d0cc12013-12-18 00:07:41203 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55204
[email protected]58faf942014-02-20 21:03:58205 FrameTreeNode* child_555 = child_267->child_at(0)->child_at(0)->child_at(0);
206 frame_tree->RemoveFrame(child_555);
207 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00208 "15: [255 'no children node': []], "
209 "16: [264: [], 265: [], 266: [], "
210 "267 'node with deep subtree': "
[email protected]58faf942014-02-20 21:03:58211 "[365: [455: []]], 268: []]]",
[email protected]94d0cc12013-12-18 00:07:41212 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55213
[email protected]58faf942014-02-20 21:03:58214 frame_tree->RemoveFrame(child_16->child_at(1));
215 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00216 "15: [255 'no children node': []], "
217 "16: [264: [], 266: [], "
218 "267 'node with deep subtree': "
[email protected]58faf942014-02-20 21:03:58219 "[365: [455: []]], 268: []]]",
[email protected]94d0cc12013-12-18 00:07:41220 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55221
[email protected]58faf942014-02-20 21:03:58222 frame_tree->RemoveFrame(root->child_at(1));
223 ASSERT_EQ("1: [14: [244: [], 245: []], "
[email protected]7cc7ebd2013-10-08 00:59:00224 "16: [264: [], 266: [], "
225 "267 'node with deep subtree': "
[email protected]58faf942014-02-20 21:03:58226 "[365: [455: []]], 268: []]]",
[email protected]94d0cc12013-12-18 00:07:41227 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55228}
229
creis6a93a812015-04-24 23:13:17230// Ensure frames can be found by frame_tree_node_id, routing ID, or name.
231TEST_F(FrameTreeTest, FindFrames) {
232 // Add a few child frames to the main frame.
233 FrameTree* frame_tree = contents()->GetFrameTree();
234 FrameTreeNode* root = frame_tree->root();
dcheng860817a2015-05-22 03:16:56235 main_test_rfh()->OnCreateChildFrame(22, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36236 "child0", blink::WebSandboxFlags::None);
dcheng860817a2015-05-22 03:16:56237 main_test_rfh()->OnCreateChildFrame(23, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36238 "child1", blink::WebSandboxFlags::None);
dcheng860817a2015-05-22 03:16:56239 main_test_rfh()->OnCreateChildFrame(24, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36240 std::string(),
241 blink::WebSandboxFlags::None);
creis6a93a812015-04-24 23:13:17242 FrameTreeNode* child0 = root->child_at(0);
243 FrameTreeNode* child1 = root->child_at(1);
dcheng860817a2015-05-22 03:16:56244
creis6a93a812015-04-24 23:13:17245 FrameTreeNode* child2 = root->child_at(2);
246
247 // Add one grandchild frame.
dcheng860817a2015-05-22 03:16:56248 child1->current_frame_host()->OnCreateChildFrame(
dcheng5f60abb2015-05-28 01:39:36249 33, blink::WebTreeScopeType::Document, "grandchild",
250 blink::WebSandboxFlags::None);
creis6a93a812015-04-24 23:13:17251 FrameTreeNode* grandchild = child1->child_at(0);
252
253 // Ensure they can be found by FTN id.
254 EXPECT_EQ(root, frame_tree->FindByID(root->frame_tree_node_id()));
255 EXPECT_EQ(child0, frame_tree->FindByID(child0->frame_tree_node_id()));
256 EXPECT_EQ(child1, frame_tree->FindByID(child1->frame_tree_node_id()));
257 EXPECT_EQ(child2, frame_tree->FindByID(child2->frame_tree_node_id()));
258 EXPECT_EQ(grandchild, frame_tree->FindByID(grandchild->frame_tree_node_id()));
259 EXPECT_EQ(nullptr, frame_tree->FindByID(-1));
260
261 // Ensure they can be found by routing id.
262 int process_id = main_test_rfh()->GetProcess()->GetID();
263 EXPECT_EQ(root, frame_tree->FindByRoutingID(process_id,
264 main_test_rfh()->GetRoutingID()));
265 EXPECT_EQ(child0, frame_tree->FindByRoutingID(process_id, 22));
266 EXPECT_EQ(child1, frame_tree->FindByRoutingID(process_id, 23));
267 EXPECT_EQ(child2, frame_tree->FindByRoutingID(process_id, 24));
268 EXPECT_EQ(grandchild, frame_tree->FindByRoutingID(process_id, 33));
269 EXPECT_EQ(nullptr, frame_tree->FindByRoutingID(process_id, 37));
270
271 // Ensure they can be found by name, if they have one.
272 EXPECT_EQ(root, frame_tree->FindByName(std::string()));
273 EXPECT_EQ(child0, frame_tree->FindByName("child0"));
274 EXPECT_EQ(child1, frame_tree->FindByName("child1"));
275 EXPECT_EQ(grandchild, frame_tree->FindByName("grandchild"));
276 EXPECT_EQ(nullptr, frame_tree->FindByName("no such frame"));
277}
278
alexmos9f8705a2015-05-06 19:58:59279// Check that PreviousSibling() is retrieved correctly.
280TEST_F(FrameTreeTest, PreviousSibling) {
281 // Add a few child frames to the main frame.
282 FrameTree* frame_tree = contents()->GetFrameTree();
283 FrameTreeNode* root = frame_tree->root();
dcheng860817a2015-05-22 03:16:56284 main_test_rfh()->OnCreateChildFrame(22, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36285 "child0", blink::WebSandboxFlags::None);
dcheng860817a2015-05-22 03:16:56286 main_test_rfh()->OnCreateChildFrame(23, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36287 "child1", blink::WebSandboxFlags::None);
dcheng860817a2015-05-22 03:16:56288 main_test_rfh()->OnCreateChildFrame(24, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36289 "child2", blink::WebSandboxFlags::None);
alexmos9f8705a2015-05-06 19:58:59290 FrameTreeNode* child0 = root->child_at(0);
291 FrameTreeNode* child1 = root->child_at(1);
292 FrameTreeNode* child2 = root->child_at(2);
293
294 // Add one grandchild frame.
dcheng860817a2015-05-22 03:16:56295 child1->current_frame_host()->OnCreateChildFrame(
dcheng5f60abb2015-05-28 01:39:36296 33, blink::WebTreeScopeType::Document, "grandchild",
297 blink::WebSandboxFlags::None);
alexmos9f8705a2015-05-06 19:58:59298 FrameTreeNode* grandchild = child1->child_at(0);
299
300 EXPECT_EQ(nullptr, root->PreviousSibling());
301 EXPECT_EQ(nullptr, child0->PreviousSibling());
302 EXPECT_EQ(child0, child1->PreviousSibling());
303 EXPECT_EQ(child1, child2->PreviousSibling());
304 EXPECT_EQ(nullptr, grandchild->PreviousSibling());
305}
306
[email protected]14266072014-04-19 00:35:20307// Do some simple manipulations of the frame tree, making sure that
308// WebContentsObservers see a consistent view of the tree as we go.
309TEST_F(FrameTreeTest, ObserverWalksTreeDuringFrameCreation) {
310 TreeWalkingWebContentsLogger activity(contents());
schenney6408fed22015-04-17 17:44:57311 contents()->NavigateAndCommit(GURL("http://www.google.com"));
312 EXPECT_EQ("", activity.GetLog());
313
[email protected]14266072014-04-19 00:35:20314 FrameTree* frame_tree = contents()->GetFrameTree();
315 FrameTreeNode* root = frame_tree->root();
316
317 // Simulate attaching a series of frames to build the frame tree.
dcheng860817a2015-05-22 03:16:56318 main_test_rfh()->OnCreateChildFrame(14, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36319 std::string(),
320 blink::WebSandboxFlags::None);
naskof5940b9f2015-03-02 23:04:05321 EXPECT_EQ(
322 "RenderFrameHostChanged(new)(14) -> 1: []\n"
323 "RenderFrameCreated(14) -> 1: [14: []]",
324 activity.GetLog());
dcheng860817a2015-05-22 03:16:56325 main_test_rfh()->OnCreateChildFrame(18, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36326 std::string(),
327 blink::WebSandboxFlags::None);
naskof5940b9f2015-03-02 23:04:05328 EXPECT_EQ(
329 "RenderFrameHostChanged(new)(18) -> 1: [14: []]\n"
330 "RenderFrameCreated(18) -> 1: [14: [], 18: []]",
331 activity.GetLog());
[email protected]14266072014-04-19 00:35:20332 frame_tree->RemoveFrame(root->child_at(0));
333 EXPECT_EQ("RenderFrameDeleted(14) -> 1: [18: []]", activity.GetLog());
334 frame_tree->RemoveFrame(root->child_at(0));
335 EXPECT_EQ("RenderFrameDeleted(18) -> 1: []", activity.GetLog());
336}
337
338// Make sure that WebContentsObservers see a consistent view of the tree after
339// recovery from a render process crash.
340TEST_F(FrameTreeTest, ObserverWalksTreeAfterCrash) {
341 TreeWalkingWebContentsLogger activity(contents());
schenney6408fed22015-04-17 17:44:57342 contents()->NavigateAndCommit(GURL("http://www.google.com"));
343 EXPECT_EQ("", activity.GetLog());
[email protected]14266072014-04-19 00:35:20344
dcheng860817a2015-05-22 03:16:56345 main_test_rfh()->OnCreateChildFrame(22, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36346 std::string(),
347 blink::WebSandboxFlags::None);
naskof5940b9f2015-03-02 23:04:05348 EXPECT_EQ(
349 "RenderFrameHostChanged(new)(22) -> 1: []\n"
350 "RenderFrameCreated(22) -> 1: [22: []]",
351 activity.GetLog());
dcheng860817a2015-05-22 03:16:56352 main_test_rfh()->OnCreateChildFrame(23, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36353 std::string(),
354 blink::WebSandboxFlags::None);
naskof5940b9f2015-03-02 23:04:05355 EXPECT_EQ(
356 "RenderFrameHostChanged(new)(23) -> 1: [22: []]\n"
357 "RenderFrameCreated(23) -> 1: [22: [], 23: []]",
358 activity.GetLog());
[email protected]14266072014-04-19 00:35:20359
360 // Crash the renderer
nick16b07652015-04-18 02:35:31361 main_test_rfh()->GetProcess()->SimulateCrash();
[email protected]14266072014-04-19 00:35:20362 EXPECT_EQ(
nick16b07652015-04-18 02:35:31363 "RenderFrameDeleted(23) -> 1: [22: [], 23*: []]\n"
364 "RenderFrameDeleted(22) -> 1: [22*: [], 23*: []]\n"
365 "RenderFrameDeleted(1) -> 1: []\n" // TODO(nick): Should be "1*:"
366 "RenderProcessGone -> 1*: []",
[email protected]14266072014-04-19 00:35:20367 activity.GetLog());
368}
369
dgroganfb22f9a2014-10-20 21:32:32370// Ensure that frames are not added to the tree, if the process passed in
371// is different than the process of the parent node.
372TEST_F(FrameTreeTest, FailAddFrameWithWrongProcessId) {
schenney6408fed22015-04-17 17:44:57373 contents()->NavigateAndCommit(GURL("http://www.google.com"));
dgroganfb22f9a2014-10-20 21:32:32374 FrameTree* frame_tree = contents()->GetFrameTree();
375 FrameTreeNode* root = frame_tree->root();
376 int process_id = root->current_frame_host()->GetProcess()->GetID();
377
378 ASSERT_EQ("1: []", GetTreeState(frame_tree));
379
380 // Simulate attaching a frame from mismatched process id.
dcheng5f60abb2015-05-28 01:39:36381 ASSERT_FALSE(frame_tree->AddFrame(
382 root, process_id + 1, 1, blink::WebTreeScopeType::Document, std::string(),
383 blink::WebSandboxFlags::None));
dgroganfb22f9a2014-10-20 21:32:32384 ASSERT_EQ("1: []", GetTreeState(frame_tree));
385}
386
naskoaeca57b2015-02-13 00:50:46387// Ensure that frames removed while a process has crashed are not preserved in
388// the global map of id->frame.
389TEST_F(FrameTreeTest, ProcessCrashClearsGlobalMap) {
390 // Add a couple child frames to the main frame.
391 FrameTreeNode* root = contents()->GetFrameTree()->root();
392
dcheng860817a2015-05-22 03:16:56393 main_test_rfh()->OnCreateChildFrame(22, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36394 std::string(),
395 blink::WebSandboxFlags::None);
dcheng860817a2015-05-22 03:16:56396 main_test_rfh()->OnCreateChildFrame(23, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36397 std::string(),
398 blink::WebSandboxFlags::None);
naskoaeca57b2015-02-13 00:50:46399
dmazzonie950ea232015-03-13 21:39:45400 // Add one grandchild frame.
401 RenderFrameHostImpl* child1_rfh = root->child_at(0)->current_frame_host();
dcheng860817a2015-05-22 03:16:56402 child1_rfh->OnCreateChildFrame(33, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36403 std::string(), blink::WebSandboxFlags::None);
dmazzonie950ea232015-03-13 21:39:45404
naskoaeca57b2015-02-13 00:50:46405 // Ensure they can be found by id.
vishal.b782eb5d2015-04-29 12:22:57406 int id1 = root->child_at(0)->frame_tree_node_id();
407 int id2 = root->child_at(1)->frame_tree_node_id();
408 int id3 = root->child_at(0)->child_at(0)->frame_tree_node_id();
dmazzonie950ea232015-03-13 21:39:45409 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id1));
410 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id2));
411 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id3));
naskoaeca57b2015-02-13 00:50:46412
413 // Crash the renderer.
nick16b07652015-04-18 02:35:31414 main_test_rfh()->GetProcess()->SimulateCrash();
naskoaeca57b2015-02-13 00:50:46415
416 // Ensure they cannot be found by id after the process has crashed.
dmazzonie950ea232015-03-13 21:39:45417 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id1));
418 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id2));
419 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id3));
naskoaeca57b2015-02-13 00:50:46420}
421
[email protected]9b159a52013-10-03 17:24:55422} // namespace content