blob: 1b6005e8b1d91123881415ad5ae438a6c7024910 [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
avib7348942015-12-25 20:57:107#include <stddef.h>
8
9#include "base/macros.h"
[email protected]9b159a52013-10-03 17:24:5510#include "base/run_loop.h"
[email protected]7cc7ebd2013-10-08 00:59:0011#include "base/strings/string_number_conversions.h"
[email protected]6ea6bdf2013-12-06 13:35:0112#include "content/browser/frame_host/navigator_impl.h"
[email protected]2a18ee222013-11-21 07:52:4413#include "content/browser/frame_host/render_frame_host_factory.h"
[email protected]d4a8ca482013-10-30 21:06:4014#include "content/browser/frame_host/render_frame_host_impl.h"
[email protected]9b159a52013-10-03 17:24:5515#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]94d0cc12013-12-18 00:07:4116#include "content/browser/web_contents/web_contents_impl.h"
naskob985af12015-02-06 04:15:3317#include "content/common/frame_messages.h"
raymes31457802016-07-20 06:08:0918#include "content/common/frame_owner_properties.h"
[email protected]14266072014-04-19 00:35:2019#include "content/public/browser/web_contents_observer.h"
[email protected]9b159a52013-10-03 17:24:5520#include "content/public/test/mock_render_process_host.h"
21#include "content/public/test/test_browser_context.h"
22#include "content/public/test/test_browser_thread_bundle.h"
[email protected]6b50e362014-08-15 05:15:5923#include "content/test/test_render_frame_host.h"
[email protected]14266072014-04-19 00:35:2024#include "content/test/test_render_view_host.h"
25#include "content/test/test_web_contents.h"
[email protected]9b159a52013-10-03 17:24:5526#include "testing/gtest/include/gtest/gtest.h"
dcheng5f60abb2015-05-28 01:39:3627#include "third_party/WebKit/public/web/WebSandboxFlags.h"
[email protected]9b159a52013-10-03 17:24:5528
29namespace content {
dgroganfb22f9a2014-10-20 21:32:3230
[email protected]9b159a52013-10-03 17:24:5531namespace {
32
[email protected]14266072014-04-19 00:35:2033// Appends a description of the structure of the frame tree to |result|.
34void AppendTreeNodeState(FrameTreeNode* node, std::string* result) {
35 result->append(
36 base::Int64ToString(node->current_frame_host()->GetRoutingID()));
schenney6408fed22015-04-17 17:44:5737 if (!node->current_frame_host()->IsRenderFrameLive())
38 result->append("*"); // Asterisk next to dead frames.
39
[email protected]14266072014-04-19 00:35:2040 if (!node->frame_name().empty()) {
41 result->append(" '");
42 result->append(node->frame_name());
43 result->append("'");
44 }
45 result->append(": [");
46 const char* separator = "";
47 for (size_t i = 0; i < node->child_count(); i++) {
48 result->append(separator);
49 AppendTreeNodeState(node->child_at(i), result);
50 separator = ", ";
51 }
52 result->append("]");
53}
54
55// Logs calls to WebContentsObserver along with the state of the frame tree,
56// for later use in EXPECT_EQ().
57class TreeWalkingWebContentsLogger : public WebContentsObserver {
58 public:
59 explicit TreeWalkingWebContentsLogger(WebContents* web_contents)
60 : WebContentsObserver(web_contents) {}
61
dchengc2282aa2014-10-21 12:07:5862 ~TreeWalkingWebContentsLogger() override {
[email protected]14266072014-04-19 00:35:2063 EXPECT_EQ("", log_) << "Activity logged that was not expected";
64 }
65
66 // Gets and resets the log, which is a string of what happened.
67 std::string GetLog() {
68 std::string result = log_;
69 log_.clear();
70 return result;
71 }
72
73 // content::WebContentsObserver implementation.
dchengc2282aa2014-10-21 12:07:5874 void RenderFrameCreated(RenderFrameHost* render_frame_host) override {
[email protected]14266072014-04-19 00:35:2075 LogWhatHappened("RenderFrameCreated", render_frame_host);
76 }
77
dchengc2282aa2014-10-21 12:07:5878 void RenderFrameHostChanged(RenderFrameHost* old_host,
79 RenderFrameHost* new_host) override {
[email protected]02d7b6e2014-06-24 21:01:5080 if (old_host)
naskof5940b9f2015-03-02 23:04:0581 LogWhatHappened("RenderFrameHostChanged(old)", old_host);
82 LogWhatHappened("RenderFrameHostChanged(new)", new_host);
[email protected]02d7b6e2014-06-24 21:01:5083 }
84
dchengc2282aa2014-10-21 12:07:5885 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override {
[email protected]14266072014-04-19 00:35:2086 LogWhatHappened("RenderFrameDeleted", render_frame_host);
87 }
88
dchengc2282aa2014-10-21 12:07:5889 void RenderProcessGone(base::TerminationStatus status) override {
[email protected]14266072014-04-19 00:35:2090 LogWhatHappened("RenderProcessGone");
91 }
92
93 private:
94 void LogWhatHappened(const std::string& event_name) {
95 if (!log_.empty()) {
96 log_.append("\n");
97 }
98 log_.append(event_name + " -> ");
99 AppendTreeNodeState(
100 static_cast<WebContentsImpl*>(web_contents())->GetFrameTree()->root(),
101 &log_);
102 }
103
104 void LogWhatHappened(const std::string& event_name, RenderFrameHost* rfh) {
105 LogWhatHappened(
106 base::StringPrintf("%s(%d)", event_name.c_str(), rfh->GetRoutingID()));
107 }
108
109 std::string log_;
110
111 DISALLOW_COPY_AND_ASSIGN(TreeWalkingWebContentsLogger);
112};
113
dgroganfb22f9a2014-10-20 21:32:32114} // namespace
115
[email protected]14266072014-04-19 00:35:20116class FrameTreeTest : public RenderViewHostImplTestHarness {
[email protected]7cc7ebd2013-10-08 00:59:00117 protected:
118 // Prints a FrameTree, for easy assertions of the tree hierarchy.
119 std::string GetTreeState(FrameTree* frame_tree) {
120 std::string result;
[email protected]fa944cb82013-11-15 17:51:21121 AppendTreeNodeState(frame_tree->root(), &result);
[email protected]7cc7ebd2013-10-08 00:59:00122 return result;
123 }
nick4ed970292016-01-20 21:46:45124
125 std::string GetTraversalOrder(FrameTree* frame_tree,
126 FrameTreeNode* node_to_skip) {
127 std::string result;
dcheng57e39e22016-01-21 00:25:38128 for (FrameTreeNode* node : frame_tree->NodesExcept(node_to_skip)) {
129 if (!result.empty())
130 result += " ";
131 result += base::Int64ToString(node->current_frame_host()->GetRoutingID());
132 }
nick4ed970292016-01-20 21:46:45133 return result;
134 }
[email protected]9b159a52013-10-03 17:24:55135};
136
[email protected]9b159a52013-10-03 17:24:55137// Exercise tree manipulation routines.
138// - Add a series of nodes and verify tree structure.
139// - Remove a series of nodes and verify tree structure.
nasko9f2261b2015-07-02 11:40:26140TEST_F(FrameTreeTest, Shape) {
nick8814e652015-12-18 01:44:12141 main_test_rfh()->InitializeRenderFrameIfNeeded();
142
[email protected]94d0cc12013-12-18 00:07:41143 // Use the FrameTree of the WebContents so that it has all the delegates it
144 // needs. We may want to consider a test version of this.
[email protected]14266072014-04-19 00:35:20145 FrameTree* frame_tree = contents()->GetFrameTree();
[email protected]58faf942014-02-20 21:03:58146 FrameTreeNode* root = frame_tree->root();
[email protected]190b8c52013-11-09 01:35:44147
[email protected]9b159a52013-10-03 17:24:55148 std::string no_children_node("no children node");
149 std::string deep_subtree("node with deep subtree");
dgroganfb22f9a2014-10-20 21:32:32150 int process_id = root->current_frame_host()->GetProcess()->GetID();
[email protected]9b159a52013-10-03 17:24:55151
nasko9f2261b2015-07-02 11:40:26152 // Do not navigate each frame separately, since that will clutter the test
153 // itself. Instead, leave them in "not live" state, which is indicated by the
154 // * after the frame id, since this test cares about the shape, not the
nick8814e652015-12-18 01:44:12155 // frame liveness.
156 EXPECT_EQ("2: []", GetTreeState(frame_tree));
[email protected]7cc7ebd2013-10-08 00:59:00157
[email protected]9b159a52013-10-03 17:24:55158 // Simulate attaching a series of frames to build the frame tree.
Blink Reformat1c4d759e2017-04-09 16:34:54159 frame_tree->AddFrame(root, process_id, 14, blink::WebTreeScopeType::kDocument,
lukasza464d8692016-02-22 19:26:32160 std::string(), "uniqueName0",
Blink Reformat1c4d759e2017-04-09 16:34:54161 blink::WebSandboxFlags::kNone, FrameOwnerProperties());
162 frame_tree->AddFrame(root, process_id, 15, blink::WebTreeScopeType::kDocument,
lukasza464d8692016-02-22 19:26:32163 std::string(), "uniqueName1",
Blink Reformat1c4d759e2017-04-09 16:34:54164 blink::WebSandboxFlags::kNone, FrameOwnerProperties());
165 frame_tree->AddFrame(root, process_id, 16, blink::WebTreeScopeType::kDocument,
lukasza464d8692016-02-22 19:26:32166 std::string(), "uniqueName2",
Blink Reformat1c4d759e2017-04-09 16:34:54167 blink::WebSandboxFlags::kNone, FrameOwnerProperties());
[email protected]9b159a52013-10-03 17:24:55168
dcheng860817a2015-05-22 03:16:56169 frame_tree->AddFrame(root->child_at(0), process_id, 244,
Blink Reformat1c4d759e2017-04-09 16:34:54170 blink::WebTreeScopeType::kDocument, std::string(),
171 "uniqueName3", blink::WebSandboxFlags::kNone,
raymes31457802016-07-20 06:08:09172 FrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56173 frame_tree->AddFrame(root->child_at(1), process_id, 255,
Blink Reformat1c4d759e2017-04-09 16:34:54174 blink::WebTreeScopeType::kDocument, no_children_node,
175 "uniqueName4", blink::WebSandboxFlags::kNone,
raymes31457802016-07-20 06:08:09176 FrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56177 frame_tree->AddFrame(root->child_at(0), process_id, 245,
Blink Reformat1c4d759e2017-04-09 16:34:54178 blink::WebTreeScopeType::kDocument, std::string(),
179 "uniqueName5", blink::WebSandboxFlags::kNone,
raymes31457802016-07-20 06:08:09180 FrameOwnerProperties());
[email protected]9b159a52013-10-03 17:24:55181
dcheng3ce04b62015-10-26 23:30:55182 EXPECT_EQ(
nick8814e652015-12-18 01:44:12183 "2: [14: [244: [], 245: []], "
184 "15: [255 'no children node': []], "
185 "16: []]",
dcheng3ce04b62015-10-26 23:30:55186 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55187
[email protected]58faf942014-02-20 21:03:58188 FrameTreeNode* child_16 = root->child_at(2);
dcheng860817a2015-05-22 03:16:56189 frame_tree->AddFrame(child_16, process_id, 264,
Blink Reformat1c4d759e2017-04-09 16:34:54190 blink::WebTreeScopeType::kDocument, std::string(),
191 "uniqueName6", blink::WebSandboxFlags::kNone,
raymes31457802016-07-20 06:08:09192 FrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56193 frame_tree->AddFrame(child_16, process_id, 265,
Blink Reformat1c4d759e2017-04-09 16:34:54194 blink::WebTreeScopeType::kDocument, std::string(),
195 "uniqueName7", blink::WebSandboxFlags::kNone,
raymes31457802016-07-20 06:08:09196 FrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56197 frame_tree->AddFrame(child_16, process_id, 266,
Blink Reformat1c4d759e2017-04-09 16:34:54198 blink::WebTreeScopeType::kDocument, std::string(),
199 "uniqueName8", blink::WebSandboxFlags::kNone,
raymes31457802016-07-20 06:08:09200 FrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56201 frame_tree->AddFrame(child_16, process_id, 267,
Blink Reformat1c4d759e2017-04-09 16:34:54202 blink::WebTreeScopeType::kDocument, deep_subtree,
203 "uniqueName9", blink::WebSandboxFlags::kNone,
raymes31457802016-07-20 06:08:09204 FrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56205 frame_tree->AddFrame(child_16, process_id, 268,
Blink Reformat1c4d759e2017-04-09 16:34:54206 blink::WebTreeScopeType::kDocument, std::string(),
207 "uniqueName10", blink::WebSandboxFlags::kNone,
raymes31457802016-07-20 06:08:09208 FrameOwnerProperties());
[email protected]9b159a52013-10-03 17:24:55209
[email protected]58faf942014-02-20 21:03:58210 FrameTreeNode* child_267 = child_16->child_at(3);
dcheng860817a2015-05-22 03:16:56211 frame_tree->AddFrame(child_267, process_id, 365,
Blink Reformat1c4d759e2017-04-09 16:34:54212 blink::WebTreeScopeType::kDocument, std::string(),
213 "uniqueName11", blink::WebSandboxFlags::kNone,
raymes31457802016-07-20 06:08:09214 FrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56215 frame_tree->AddFrame(child_267->child_at(0), process_id, 455,
Blink Reformat1c4d759e2017-04-09 16:34:54216 blink::WebTreeScopeType::kDocument, std::string(),
217 "uniqueName12", blink::WebSandboxFlags::kNone,
raymes31457802016-07-20 06:08:09218 FrameOwnerProperties());
dgroganfb22f9a2014-10-20 21:32:32219 frame_tree->AddFrame(child_267->child_at(0)->child_at(0), process_id, 555,
Blink Reformat1c4d759e2017-04-09 16:34:54220 blink::WebTreeScopeType::kDocument, std::string(),
221 "uniqueName13", blink::WebSandboxFlags::kNone,
raymes31457802016-07-20 06:08:09222 FrameOwnerProperties());
223 frame_tree->AddFrame(child_267->child_at(0)->child_at(0)->child_at(0),
Blink Reformat1c4d759e2017-04-09 16:34:54224 process_id, 655, blink::WebTreeScopeType::kDocument,
raymes31457802016-07-20 06:08:09225 std::string(), "uniqueName14",
Blink Reformat1c4d759e2017-04-09 16:34:54226 blink::WebSandboxFlags::kNone, FrameOwnerProperties());
[email protected]9b159a52013-10-03 17:24:55227
[email protected]7cc7ebd2013-10-08 00:59:00228 // Now that's it's fully built, verify the tree structure is as expected.
dcheng3ce04b62015-10-26 23:30:55229 EXPECT_EQ(
nick8814e652015-12-18 01:44:12230 "2: [14: [244: [], 245: []], "
231 "15: [255 'no children node': []], "
232 "16: [264: [], 265: [], 266: [], "
233 "267 'node with deep subtree': "
234 "[365: [455: [555: [655: []]]]], 268: []]]",
dcheng3ce04b62015-10-26 23:30:55235 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55236
nick4ed970292016-01-20 21:46:45237 // Verify that traversal order is breadth first, even if we skip a subtree.
238 FrameTreeNode* child_14 = root->child_at(0);
239 FrameTreeNode* child_15 = root->child_at(1);
240 FrameTreeNode* child_244 = child_14->child_at(0);
241 FrameTreeNode* child_245 = child_14->child_at(1);
[email protected]58faf942014-02-20 21:03:58242 FrameTreeNode* child_555 = child_267->child_at(0)->child_at(0)->child_at(0);
nick4ed970292016-01-20 21:46:45243 FrameTreeNode* child_655 = child_555->child_at(0);
244 EXPECT_EQ("2 14 15 16 244 245 255 264 265 266 267 268 365 455 555 655",
245 GetTraversalOrder(frame_tree, nullptr));
246 EXPECT_EQ("", GetTraversalOrder(frame_tree, root));
247 EXPECT_EQ("2 15 16 255 264 265 266 267 268 365 455 555 655",
248 GetTraversalOrder(frame_tree, child_14));
249 EXPECT_EQ("2 14 15 16 245 255 264 265 266 267 268 365 455 555 655",
250 GetTraversalOrder(frame_tree, child_244));
251 EXPECT_EQ("2 14 15 16 244 255 264 265 266 267 268 365 455 555 655",
252 GetTraversalOrder(frame_tree, child_245));
253 EXPECT_EQ("2 14 16 244 245 264 265 266 267 268 365 455 555 655",
254 GetTraversalOrder(frame_tree, child_15));
255 EXPECT_EQ("2 14 15 16 244 245 255 264 265 266 268",
256 GetTraversalOrder(frame_tree, child_267));
257 EXPECT_EQ("2 14 15 16 244 245 255 264 265 266 267 268 365 455",
258 GetTraversalOrder(frame_tree, child_555));
259 EXPECT_EQ("2 14 15 16 244 245 255 264 265 266 267 268 365 455 555",
260 GetTraversalOrder(frame_tree, child_655));
261
[email protected]58faf942014-02-20 21:03:58262 frame_tree->RemoveFrame(child_555);
dcheng3ce04b62015-10-26 23:30:55263 EXPECT_EQ(
nick8814e652015-12-18 01:44:12264 "2: [14: [244: [], 245: []], "
265 "15: [255 'no children node': []], "
266 "16: [264: [], 265: [], 266: [], "
267 "267 'node with deep subtree': "
268 "[365: [455: []]], 268: []]]",
dcheng3ce04b62015-10-26 23:30:55269 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55270
[email protected]58faf942014-02-20 21:03:58271 frame_tree->RemoveFrame(child_16->child_at(1));
dcheng3ce04b62015-10-26 23:30:55272 EXPECT_EQ(
nick8814e652015-12-18 01:44:12273 "2: [14: [244: [], 245: []], "
274 "15: [255 'no children node': []], "
275 "16: [264: [], 266: [], "
276 "267 'node with deep subtree': "
277 "[365: [455: []]], 268: []]]",
dcheng3ce04b62015-10-26 23:30:55278 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55279
[email protected]58faf942014-02-20 21:03:58280 frame_tree->RemoveFrame(root->child_at(1));
dcheng3ce04b62015-10-26 23:30:55281 EXPECT_EQ(
nick8814e652015-12-18 01:44:12282 "2: [14: [244: [], 245: []], "
283 "16: [264: [], 266: [], "
284 "267 'node with deep subtree': "
285 "[365: [455: []]], 268: []]]",
dcheng3ce04b62015-10-26 23:30:55286 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55287}
288
creis6a93a812015-04-24 23:13:17289// Ensure frames can be found by frame_tree_node_id, routing ID, or name.
290TEST_F(FrameTreeTest, FindFrames) {
lfg269b702f2015-06-08 19:28:19291 main_test_rfh()->InitializeRenderFrameIfNeeded();
292
creis6a93a812015-04-24 23:13:17293 // Add a few child frames to the main frame.
294 FrameTree* frame_tree = contents()->GetFrameTree();
295 FrameTreeNode* root = frame_tree->root();
lfg269b702f2015-06-08 19:28:19296
lukasza464d8692016-02-22 19:26:32297 main_test_rfh()->OnCreateChildFrame(
Blink Reformat1c4d759e2017-04-09 16:34:54298 22, blink::WebTreeScopeType::kDocument, "child0", "uniqueName0",
299 blink::WebSandboxFlags::kNone, FrameOwnerProperties());
lukasza464d8692016-02-22 19:26:32300 main_test_rfh()->OnCreateChildFrame(
Blink Reformat1c4d759e2017-04-09 16:34:54301 23, blink::WebTreeScopeType::kDocument, "child1", "uniqueName1",
302 blink::WebSandboxFlags::kNone, FrameOwnerProperties());
lukasza464d8692016-02-22 19:26:32303 main_test_rfh()->OnCreateChildFrame(
Blink Reformat1c4d759e2017-04-09 16:34:54304 24, blink::WebTreeScopeType::kDocument, std::string(), "uniqueName2",
305 blink::WebSandboxFlags::kNone, FrameOwnerProperties());
creis6a93a812015-04-24 23:13:17306 FrameTreeNode* child0 = root->child_at(0);
307 FrameTreeNode* child1 = root->child_at(1);
dcheng860817a2015-05-22 03:16:56308
creis6a93a812015-04-24 23:13:17309 FrameTreeNode* child2 = root->child_at(2);
310
311 // Add one grandchild frame.
dcheng860817a2015-05-22 03:16:56312 child1->current_frame_host()->OnCreateChildFrame(
Blink Reformat1c4d759e2017-04-09 16:34:54313 33, blink::WebTreeScopeType::kDocument, "grandchild", "uniqueName3",
314 blink::WebSandboxFlags::kNone, FrameOwnerProperties());
creis6a93a812015-04-24 23:13:17315 FrameTreeNode* grandchild = child1->child_at(0);
316
317 // Ensure they can be found by FTN id.
318 EXPECT_EQ(root, frame_tree->FindByID(root->frame_tree_node_id()));
319 EXPECT_EQ(child0, frame_tree->FindByID(child0->frame_tree_node_id()));
320 EXPECT_EQ(child1, frame_tree->FindByID(child1->frame_tree_node_id()));
321 EXPECT_EQ(child2, frame_tree->FindByID(child2->frame_tree_node_id()));
322 EXPECT_EQ(grandchild, frame_tree->FindByID(grandchild->frame_tree_node_id()));
323 EXPECT_EQ(nullptr, frame_tree->FindByID(-1));
324
325 // Ensure they can be found by routing id.
326 int process_id = main_test_rfh()->GetProcess()->GetID();
327 EXPECT_EQ(root, frame_tree->FindByRoutingID(process_id,
328 main_test_rfh()->GetRoutingID()));
329 EXPECT_EQ(child0, frame_tree->FindByRoutingID(process_id, 22));
330 EXPECT_EQ(child1, frame_tree->FindByRoutingID(process_id, 23));
331 EXPECT_EQ(child2, frame_tree->FindByRoutingID(process_id, 24));
332 EXPECT_EQ(grandchild, frame_tree->FindByRoutingID(process_id, 33));
333 EXPECT_EQ(nullptr, frame_tree->FindByRoutingID(process_id, 37));
334
335 // Ensure they can be found by name, if they have one.
336 EXPECT_EQ(root, frame_tree->FindByName(std::string()));
337 EXPECT_EQ(child0, frame_tree->FindByName("child0"));
338 EXPECT_EQ(child1, frame_tree->FindByName("child1"));
339 EXPECT_EQ(grandchild, frame_tree->FindByName("grandchild"));
340 EXPECT_EQ(nullptr, frame_tree->FindByName("no such frame"));
341}
342
paulmeyer322777fb2016-05-16 23:15:39343// Check that PreviousSibling() and NextSibling() are retrieved correctly.
344TEST_F(FrameTreeTest, GetSibling) {
lfg269b702f2015-06-08 19:28:19345 main_test_rfh()->InitializeRenderFrameIfNeeded();
346
alexmos9f8705a2015-05-06 19:58:59347 // Add a few child frames to the main frame.
348 FrameTree* frame_tree = contents()->GetFrameTree();
349 FrameTreeNode* root = frame_tree->root();
lukasza464d8692016-02-22 19:26:32350 main_test_rfh()->OnCreateChildFrame(
Blink Reformat1c4d759e2017-04-09 16:34:54351 22, blink::WebTreeScopeType::kDocument, "child0", "uniqueName0",
352 blink::WebSandboxFlags::kNone, FrameOwnerProperties());
lukasza464d8692016-02-22 19:26:32353 main_test_rfh()->OnCreateChildFrame(
Blink Reformat1c4d759e2017-04-09 16:34:54354 23, blink::WebTreeScopeType::kDocument, "child1", "uniqueName1",
355 blink::WebSandboxFlags::kNone, FrameOwnerProperties());
lukasza464d8692016-02-22 19:26:32356 main_test_rfh()->OnCreateChildFrame(
Blink Reformat1c4d759e2017-04-09 16:34:54357 24, blink::WebTreeScopeType::kDocument, "child2", "uniqueName2",
358 blink::WebSandboxFlags::kNone, FrameOwnerProperties());
alexmos9f8705a2015-05-06 19:58:59359 FrameTreeNode* child0 = root->child_at(0);
360 FrameTreeNode* child1 = root->child_at(1);
361 FrameTreeNode* child2 = root->child_at(2);
362
363 // Add one grandchild frame.
dcheng860817a2015-05-22 03:16:56364 child1->current_frame_host()->OnCreateChildFrame(
Blink Reformat1c4d759e2017-04-09 16:34:54365 33, blink::WebTreeScopeType::kDocument, "grandchild", "uniqueName3",
366 blink::WebSandboxFlags::kNone, FrameOwnerProperties());
alexmos9f8705a2015-05-06 19:58:59367 FrameTreeNode* grandchild = child1->child_at(0);
368
paulmeyer322777fb2016-05-16 23:15:39369 // Test PreviousSibling().
alexmos9f8705a2015-05-06 19:58:59370 EXPECT_EQ(nullptr, root->PreviousSibling());
371 EXPECT_EQ(nullptr, child0->PreviousSibling());
372 EXPECT_EQ(child0, child1->PreviousSibling());
373 EXPECT_EQ(child1, child2->PreviousSibling());
374 EXPECT_EQ(nullptr, grandchild->PreviousSibling());
paulmeyer322777fb2016-05-16 23:15:39375
376 // Test NextSibling().
377 EXPECT_EQ(nullptr, root->NextSibling());
378 EXPECT_EQ(child1, child0->NextSibling());
379 EXPECT_EQ(child2, child1->NextSibling());
380 EXPECT_EQ(nullptr, child2->NextSibling());
381 EXPECT_EQ(nullptr, grandchild->NextSibling());
alexmos9f8705a2015-05-06 19:58:59382}
383
[email protected]14266072014-04-19 00:35:20384// Do some simple manipulations of the frame tree, making sure that
385// WebContentsObservers see a consistent view of the tree as we go.
386TEST_F(FrameTreeTest, ObserverWalksTreeDuringFrameCreation) {
387 TreeWalkingWebContentsLogger activity(contents());
schenney6408fed22015-04-17 17:44:57388 contents()->NavigateAndCommit(GURL("http://www.google.com"));
dcheng3ce04b62015-10-26 23:30:55389 EXPECT_EQ("RenderFrameCreated(2) -> 2: []", activity.GetLog());
schenney6408fed22015-04-17 17:44:57390
[email protected]14266072014-04-19 00:35:20391 FrameTree* frame_tree = contents()->GetFrameTree();
392 FrameTreeNode* root = frame_tree->root();
393
394 // Simulate attaching a series of frames to build the frame tree.
lukasza464d8692016-02-22 19:26:32395 main_test_rfh()->OnCreateChildFrame(
Blink Reformat1c4d759e2017-04-09 16:34:54396 14, blink::WebTreeScopeType::kDocument, std::string(), "uniqueName0",
397 blink::WebSandboxFlags::kNone, FrameOwnerProperties());
naskof5940b9f2015-03-02 23:04:05398 EXPECT_EQ(
dcheng3ce04b62015-10-26 23:30:55399 "RenderFrameHostChanged(new)(14) -> 2: []\n"
400 "RenderFrameCreated(14) -> 2: [14: []]",
naskof5940b9f2015-03-02 23:04:05401 activity.GetLog());
lukasza464d8692016-02-22 19:26:32402 main_test_rfh()->OnCreateChildFrame(
Blink Reformat1c4d759e2017-04-09 16:34:54403 18, blink::WebTreeScopeType::kDocument, std::string(), "uniqueName1",
404 blink::WebSandboxFlags::kNone, FrameOwnerProperties());
naskof5940b9f2015-03-02 23:04:05405 EXPECT_EQ(
dcheng3ce04b62015-10-26 23:30:55406 "RenderFrameHostChanged(new)(18) -> 2: [14: []]\n"
407 "RenderFrameCreated(18) -> 2: [14: [], 18: []]",
naskof5940b9f2015-03-02 23:04:05408 activity.GetLog());
[email protected]14266072014-04-19 00:35:20409 frame_tree->RemoveFrame(root->child_at(0));
dcheng3ce04b62015-10-26 23:30:55410 EXPECT_EQ("RenderFrameDeleted(14) -> 2: [18: []]", activity.GetLog());
[email protected]14266072014-04-19 00:35:20411 frame_tree->RemoveFrame(root->child_at(0));
dcheng3ce04b62015-10-26 23:30:55412 EXPECT_EQ("RenderFrameDeleted(18) -> 2: []", activity.GetLog());
[email protected]14266072014-04-19 00:35:20413}
414
415// Make sure that WebContentsObservers see a consistent view of the tree after
416// recovery from a render process crash.
417TEST_F(FrameTreeTest, ObserverWalksTreeAfterCrash) {
418 TreeWalkingWebContentsLogger activity(contents());
schenney6408fed22015-04-17 17:44:57419 contents()->NavigateAndCommit(GURL("http://www.google.com"));
dcheng3ce04b62015-10-26 23:30:55420 EXPECT_EQ("RenderFrameCreated(2) -> 2: []", activity.GetLog());
[email protected]14266072014-04-19 00:35:20421
lukasza464d8692016-02-22 19:26:32422 main_test_rfh()->OnCreateChildFrame(
Blink Reformat1c4d759e2017-04-09 16:34:54423 22, blink::WebTreeScopeType::kDocument, std::string(), "uniqueName0",
424 blink::WebSandboxFlags::kNone, FrameOwnerProperties());
naskof5940b9f2015-03-02 23:04:05425 EXPECT_EQ(
dcheng3ce04b62015-10-26 23:30:55426 "RenderFrameHostChanged(new)(22) -> 2: []\n"
427 "RenderFrameCreated(22) -> 2: [22: []]",
naskof5940b9f2015-03-02 23:04:05428 activity.GetLog());
lukasza464d8692016-02-22 19:26:32429 main_test_rfh()->OnCreateChildFrame(
Blink Reformat1c4d759e2017-04-09 16:34:54430 23, blink::WebTreeScopeType::kDocument, std::string(), "uniqueName1",
431 blink::WebSandboxFlags::kNone, FrameOwnerProperties());
naskof5940b9f2015-03-02 23:04:05432 EXPECT_EQ(
dcheng3ce04b62015-10-26 23:30:55433 "RenderFrameHostChanged(new)(23) -> 2: [22: []]\n"
434 "RenderFrameCreated(23) -> 2: [22: [], 23: []]",
naskof5940b9f2015-03-02 23:04:05435 activity.GetLog());
[email protected]14266072014-04-19 00:35:20436
437 // Crash the renderer
nick16b07652015-04-18 02:35:31438 main_test_rfh()->GetProcess()->SimulateCrash();
[email protected]14266072014-04-19 00:35:20439 EXPECT_EQ(
dcheng3ce04b62015-10-26 23:30:55440 "RenderProcessGone -> 2*: [22*: [], 23*: []]\n"
441 "RenderFrameDeleted(23) -> 2*: [22*: [], 23*: []]\n"
442 "RenderFrameDeleted(22) -> 2*: [22*: [], 23*: []]\n"
443 "RenderFrameDeleted(2) -> 2*: []",
[email protected]14266072014-04-19 00:35:20444 activity.GetLog());
445}
446
dgroganfb22f9a2014-10-20 21:32:32447// Ensure that frames are not added to the tree, if the process passed in
448// is different than the process of the parent node.
449TEST_F(FrameTreeTest, FailAddFrameWithWrongProcessId) {
schenney6408fed22015-04-17 17:44:57450 contents()->NavigateAndCommit(GURL("http://www.google.com"));
dgroganfb22f9a2014-10-20 21:32:32451 FrameTree* frame_tree = contents()->GetFrameTree();
452 FrameTreeNode* root = frame_tree->root();
453 int process_id = root->current_frame_host()->GetProcess()->GetID();
454
dcheng3ce04b62015-10-26 23:30:55455 ASSERT_EQ("2: []", GetTreeState(frame_tree));
dgroganfb22f9a2014-10-20 21:32:32456
457 // Simulate attaching a frame from mismatched process id.
dcheng5f60abb2015-05-28 01:39:36458 ASSERT_FALSE(frame_tree->AddFrame(
Blink Reformat1c4d759e2017-04-09 16:34:54459 root, process_id + 1, 1, blink::WebTreeScopeType::kDocument,
460 std::string(), "uniqueName0", blink::WebSandboxFlags::kNone,
461 FrameOwnerProperties()));
dcheng3ce04b62015-10-26 23:30:55462 ASSERT_EQ("2: []", GetTreeState(frame_tree));
dgroganfb22f9a2014-10-20 21:32:32463}
464
naskoaeca57b2015-02-13 00:50:46465// Ensure that frames removed while a process has crashed are not preserved in
466// the global map of id->frame.
467TEST_F(FrameTreeTest, ProcessCrashClearsGlobalMap) {
lfg269b702f2015-06-08 19:28:19468 main_test_rfh()->InitializeRenderFrameIfNeeded();
469
naskoaeca57b2015-02-13 00:50:46470 // Add a couple child frames to the main frame.
471 FrameTreeNode* root = contents()->GetFrameTree()->root();
472
lukasza464d8692016-02-22 19:26:32473 main_test_rfh()->OnCreateChildFrame(
Blink Reformat1c4d759e2017-04-09 16:34:54474 22, blink::WebTreeScopeType::kDocument, std::string(), "uniqueName0",
475 blink::WebSandboxFlags::kNone, FrameOwnerProperties());
lukasza464d8692016-02-22 19:26:32476 main_test_rfh()->OnCreateChildFrame(
Blink Reformat1c4d759e2017-04-09 16:34:54477 23, blink::WebTreeScopeType::kDocument, std::string(), "uniqueName1",
478 blink::WebSandboxFlags::kNone, FrameOwnerProperties());
naskoaeca57b2015-02-13 00:50:46479
dmazzonie950ea232015-03-13 21:39:45480 // Add one grandchild frame.
481 RenderFrameHostImpl* child1_rfh = root->child_at(0)->current_frame_host();
lukasza464d8692016-02-22 19:26:32482 child1_rfh->OnCreateChildFrame(
Blink Reformat1c4d759e2017-04-09 16:34:54483 33, blink::WebTreeScopeType::kDocument, std::string(), "uniqueName2",
484 blink::WebSandboxFlags::kNone, FrameOwnerProperties());
dmazzonie950ea232015-03-13 21:39:45485
naskoaeca57b2015-02-13 00:50:46486 // Ensure they can be found by id.
vishal.b782eb5d2015-04-29 12:22:57487 int id1 = root->child_at(0)->frame_tree_node_id();
488 int id2 = root->child_at(1)->frame_tree_node_id();
489 int id3 = root->child_at(0)->child_at(0)->frame_tree_node_id();
dmazzonie950ea232015-03-13 21:39:45490 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id1));
491 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id2));
492 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id3));
naskoaeca57b2015-02-13 00:50:46493
494 // Crash the renderer.
nick16b07652015-04-18 02:35:31495 main_test_rfh()->GetProcess()->SimulateCrash();
naskoaeca57b2015-02-13 00:50:46496
497 // Ensure they cannot be found by id after the process has crashed.
dmazzonie950ea232015-03-13 21:39:45498 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id1));
499 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id2));
500 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id3));
naskoaeca57b2015-02-13 00:50:46501}
502
[email protected]9b159a52013-10-03 17:24:55503} // namespace content