blob: 2ec37786a28b5eb9c6d2df206ace928b32bc28d0 [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"
[email protected]14266072014-04-19 00:35:2018#include "content/public/browser/web_contents_observer.h"
[email protected]9b159a52013-10-03 17:24:5519#include "content/public/test/mock_render_process_host.h"
20#include "content/public/test/test_browser_context.h"
21#include "content/public/test/test_browser_thread_bundle.h"
[email protected]6b50e362014-08-15 05:15:5922#include "content/test/test_render_frame_host.h"
[email protected]14266072014-04-19 00:35:2023#include "content/test/test_render_view_host.h"
24#include "content/test/test_web_contents.h"
[email protected]9b159a52013-10-03 17:24:5525#include "testing/gtest/include/gtest/gtest.h"
dcheng5f60abb2015-05-28 01:39:3626#include "third_party/WebKit/public/web/WebSandboxFlags.h"
[email protected]9b159a52013-10-03 17:24:5527
28namespace content {
dgroganfb22f9a2014-10-20 21:32:3229
[email protected]9b159a52013-10-03 17:24:5530namespace {
31
[email protected]14266072014-04-19 00:35:2032// Appends a description of the structure of the frame tree to |result|.
33void AppendTreeNodeState(FrameTreeNode* node, std::string* result) {
34 result->append(
35 base::Int64ToString(node->current_frame_host()->GetRoutingID()));
schenney6408fed22015-04-17 17:44:5736 if (!node->current_frame_host()->IsRenderFrameLive())
37 result->append("*"); // Asterisk next to dead frames.
38
[email protected]14266072014-04-19 00:35:2039 if (!node->frame_name().empty()) {
40 result->append(" '");
41 result->append(node->frame_name());
42 result->append("'");
43 }
44 result->append(": [");
45 const char* separator = "";
46 for (size_t i = 0; i < node->child_count(); i++) {
47 result->append(separator);
48 AppendTreeNodeState(node->child_at(i), result);
49 separator = ", ";
50 }
51 result->append("]");
52}
53
nick4ed970292016-01-20 21:46:4554bool AppendRoutingId(std::string* result, FrameTreeNode* node) {
55 if (!result->empty())
56 result->append(" ");
57 result->append(
58 base::Int64ToString(node->current_frame_host()->GetRoutingID()));
59 return true;
60}
61
[email protected]14266072014-04-19 00:35:2062// Logs calls to WebContentsObserver along with the state of the frame tree,
63// for later use in EXPECT_EQ().
64class TreeWalkingWebContentsLogger : public WebContentsObserver {
65 public:
66 explicit TreeWalkingWebContentsLogger(WebContents* web_contents)
67 : WebContentsObserver(web_contents) {}
68
dchengc2282aa2014-10-21 12:07:5869 ~TreeWalkingWebContentsLogger() override {
[email protected]14266072014-04-19 00:35:2070 EXPECT_EQ("", log_) << "Activity logged that was not expected";
71 }
72
73 // Gets and resets the log, which is a string of what happened.
74 std::string GetLog() {
75 std::string result = log_;
76 log_.clear();
77 return result;
78 }
79
80 // content::WebContentsObserver implementation.
dchengc2282aa2014-10-21 12:07:5881 void RenderFrameCreated(RenderFrameHost* render_frame_host) override {
[email protected]14266072014-04-19 00:35:2082 LogWhatHappened("RenderFrameCreated", render_frame_host);
83 }
84
dchengc2282aa2014-10-21 12:07:5885 void RenderFrameHostChanged(RenderFrameHost* old_host,
86 RenderFrameHost* new_host) override {
[email protected]02d7b6e2014-06-24 21:01:5087 if (old_host)
naskof5940b9f2015-03-02 23:04:0588 LogWhatHappened("RenderFrameHostChanged(old)", old_host);
89 LogWhatHappened("RenderFrameHostChanged(new)", new_host);
[email protected]02d7b6e2014-06-24 21:01:5090 }
91
dchengc2282aa2014-10-21 12:07:5892 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override {
[email protected]14266072014-04-19 00:35:2093 LogWhatHappened("RenderFrameDeleted", render_frame_host);
94 }
95
dchengc2282aa2014-10-21 12:07:5896 void RenderProcessGone(base::TerminationStatus status) override {
[email protected]14266072014-04-19 00:35:2097 LogWhatHappened("RenderProcessGone");
98 }
99
100 private:
101 void LogWhatHappened(const std::string& event_name) {
102 if (!log_.empty()) {
103 log_.append("\n");
104 }
105 log_.append(event_name + " -> ");
106 AppendTreeNodeState(
107 static_cast<WebContentsImpl*>(web_contents())->GetFrameTree()->root(),
108 &log_);
109 }
110
111 void LogWhatHappened(const std::string& event_name, RenderFrameHost* rfh) {
112 LogWhatHappened(
113 base::StringPrintf("%s(%d)", event_name.c_str(), rfh->GetRoutingID()));
114 }
115
116 std::string log_;
117
118 DISALLOW_COPY_AND_ASSIGN(TreeWalkingWebContentsLogger);
119};
120
dgroganfb22f9a2014-10-20 21:32:32121} // namespace
122
[email protected]14266072014-04-19 00:35:20123class FrameTreeTest : public RenderViewHostImplTestHarness {
[email protected]7cc7ebd2013-10-08 00:59:00124 protected:
125 // Prints a FrameTree, for easy assertions of the tree hierarchy.
126 std::string GetTreeState(FrameTree* frame_tree) {
127 std::string result;
[email protected]fa944cb82013-11-15 17:51:21128 AppendTreeNodeState(frame_tree->root(), &result);
[email protected]7cc7ebd2013-10-08 00:59:00129 return result;
130 }
nick4ed970292016-01-20 21:46:45131
132 std::string GetTraversalOrder(FrameTree* frame_tree,
133 FrameTreeNode* node_to_skip) {
134 std::string result;
135 frame_tree->ForEach(base::Bind(&AppendRoutingId, &result), node_to_skip);
136 return result;
137 }
[email protected]9b159a52013-10-03 17:24:55138};
139
[email protected]9b159a52013-10-03 17:24:55140// Exercise tree manipulation routines.
141// - Add a series of nodes and verify tree structure.
142// - Remove a series of nodes and verify tree structure.
nasko9f2261b2015-07-02 11:40:26143TEST_F(FrameTreeTest, Shape) {
nick8814e652015-12-18 01:44:12144 main_test_rfh()->InitializeRenderFrameIfNeeded();
145
[email protected]94d0cc12013-12-18 00:07:41146 // Use the FrameTree of the WebContents so that it has all the delegates it
147 // needs. We may want to consider a test version of this.
[email protected]14266072014-04-19 00:35:20148 FrameTree* frame_tree = contents()->GetFrameTree();
[email protected]58faf942014-02-20 21:03:58149 FrameTreeNode* root = frame_tree->root();
[email protected]190b8c52013-11-09 01:35:44150
[email protected]9b159a52013-10-03 17:24:55151 std::string no_children_node("no children node");
152 std::string deep_subtree("node with deep subtree");
dgroganfb22f9a2014-10-20 21:32:32153 int process_id = root->current_frame_host()->GetProcess()->GetID();
[email protected]9b159a52013-10-03 17:24:55154
nasko9f2261b2015-07-02 11:40:26155 // Do not navigate each frame separately, since that will clutter the test
156 // itself. Instead, leave them in "not live" state, which is indicated by the
157 // * after the frame id, since this test cares about the shape, not the
nick8814e652015-12-18 01:44:12158 // frame liveness.
159 EXPECT_EQ("2: []", GetTreeState(frame_tree));
[email protected]7cc7ebd2013-10-08 00:59:00160
[email protected]9b159a52013-10-03 17:24:55161 // Simulate attaching a series of frames to build the frame tree.
dcheng860817a2015-05-22 03:16:56162 frame_tree->AddFrame(root, process_id, 14, blink::WebTreeScopeType::Document,
lazyboy70605c32015-11-03 01:27:31163 std::string(), blink::WebSandboxFlags::None,
164 blink::WebFrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56165 frame_tree->AddFrame(root, process_id, 15, blink::WebTreeScopeType::Document,
lazyboy70605c32015-11-03 01:27:31166 std::string(), blink::WebSandboxFlags::None,
167 blink::WebFrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56168 frame_tree->AddFrame(root, process_id, 16, blink::WebTreeScopeType::Document,
lazyboy70605c32015-11-03 01:27:31169 std::string(), blink::WebSandboxFlags::None,
170 blink::WebFrameOwnerProperties());
[email protected]9b159a52013-10-03 17:24:55171
dcheng860817a2015-05-22 03:16:56172 frame_tree->AddFrame(root->child_at(0), process_id, 244,
173 blink::WebTreeScopeType::Document, std::string(),
lazyboy70605c32015-11-03 01:27:31174 blink::WebSandboxFlags::None,
175 blink::WebFrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56176 frame_tree->AddFrame(root->child_at(1), process_id, 255,
177 blink::WebTreeScopeType::Document, no_children_node,
lazyboy70605c32015-11-03 01:27:31178 blink::WebSandboxFlags::None,
179 blink::WebFrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56180 frame_tree->AddFrame(root->child_at(0), process_id, 245,
181 blink::WebTreeScopeType::Document, std::string(),
lazyboy70605c32015-11-03 01:27:31182 blink::WebSandboxFlags::None,
183 blink::WebFrameOwnerProperties());
[email protected]9b159a52013-10-03 17:24:55184
dcheng3ce04b62015-10-26 23:30:55185 EXPECT_EQ(
nick8814e652015-12-18 01:44:12186 "2: [14: [244: [], 245: []], "
187 "15: [255 'no children node': []], "
188 "16: []]",
dcheng3ce04b62015-10-26 23:30:55189 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55190
[email protected]58faf942014-02-20 21:03:58191 FrameTreeNode* child_16 = root->child_at(2);
dcheng860817a2015-05-22 03:16:56192 frame_tree->AddFrame(child_16, process_id, 264,
193 blink::WebTreeScopeType::Document, std::string(),
lazyboy70605c32015-11-03 01:27:31194 blink::WebSandboxFlags::None,
195 blink::WebFrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56196 frame_tree->AddFrame(child_16, process_id, 265,
197 blink::WebTreeScopeType::Document, std::string(),
lazyboy70605c32015-11-03 01:27:31198 blink::WebSandboxFlags::None,
199 blink::WebFrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56200 frame_tree->AddFrame(child_16, process_id, 266,
201 blink::WebTreeScopeType::Document, std::string(),
lazyboy70605c32015-11-03 01:27:31202 blink::WebSandboxFlags::None,
203 blink::WebFrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56204 frame_tree->AddFrame(child_16, process_id, 267,
205 blink::WebTreeScopeType::Document, deep_subtree,
lazyboy70605c32015-11-03 01:27:31206 blink::WebSandboxFlags::None,
207 blink::WebFrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56208 frame_tree->AddFrame(child_16, process_id, 268,
209 blink::WebTreeScopeType::Document, std::string(),
lazyboy70605c32015-11-03 01:27:31210 blink::WebSandboxFlags::None,
211 blink::WebFrameOwnerProperties());
[email protected]9b159a52013-10-03 17:24:55212
[email protected]58faf942014-02-20 21:03:58213 FrameTreeNode* child_267 = child_16->child_at(3);
dcheng860817a2015-05-22 03:16:56214 frame_tree->AddFrame(child_267, process_id, 365,
215 blink::WebTreeScopeType::Document, std::string(),
lazyboy70605c32015-11-03 01:27:31216 blink::WebSandboxFlags::None,
217 blink::WebFrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56218 frame_tree->AddFrame(child_267->child_at(0), process_id, 455,
219 blink::WebTreeScopeType::Document, std::string(),
lazyboy70605c32015-11-03 01:27:31220 blink::WebSandboxFlags::None,
221 blink::WebFrameOwnerProperties());
dgroganfb22f9a2014-10-20 21:32:32222 frame_tree->AddFrame(child_267->child_at(0)->child_at(0), process_id, 555,
dcheng860817a2015-05-22 03:16:56223 blink::WebTreeScopeType::Document, std::string(),
lazyboy70605c32015-11-03 01:27:31224 blink::WebSandboxFlags::None,
225 blink::WebFrameOwnerProperties());
dgroganfb22f9a2014-10-20 21:32:32226 frame_tree->AddFrame(child_267->child_at(0)->child_at(0)->child_at(0),
dcheng860817a2015-05-22 03:16:56227 process_id, 655, blink::WebTreeScopeType::Document,
lazyboy70605c32015-11-03 01:27:31228 std::string(), blink::WebSandboxFlags::None,
229 blink::WebFrameOwnerProperties());
[email protected]9b159a52013-10-03 17:24:55230
[email protected]7cc7ebd2013-10-08 00:59:00231 // Now that's it's fully built, verify the tree structure is as expected.
dcheng3ce04b62015-10-26 23:30:55232 EXPECT_EQ(
nick8814e652015-12-18 01:44:12233 "2: [14: [244: [], 245: []], "
234 "15: [255 'no children node': []], "
235 "16: [264: [], 265: [], 266: [], "
236 "267 'node with deep subtree': "
237 "[365: [455: [555: [655: []]]]], 268: []]]",
dcheng3ce04b62015-10-26 23:30:55238 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55239
nick4ed970292016-01-20 21:46:45240 // Verify that traversal order is breadth first, even if we skip a subtree.
241 FrameTreeNode* child_14 = root->child_at(0);
242 FrameTreeNode* child_15 = root->child_at(1);
243 FrameTreeNode* child_244 = child_14->child_at(0);
244 FrameTreeNode* child_245 = child_14->child_at(1);
[email protected]58faf942014-02-20 21:03:58245 FrameTreeNode* child_555 = child_267->child_at(0)->child_at(0)->child_at(0);
nick4ed970292016-01-20 21:46:45246 FrameTreeNode* child_655 = child_555->child_at(0);
247 EXPECT_EQ("2 14 15 16 244 245 255 264 265 266 267 268 365 455 555 655",
248 GetTraversalOrder(frame_tree, nullptr));
249 EXPECT_EQ("", GetTraversalOrder(frame_tree, root));
250 EXPECT_EQ("2 15 16 255 264 265 266 267 268 365 455 555 655",
251 GetTraversalOrder(frame_tree, child_14));
252 EXPECT_EQ("2 14 15 16 245 255 264 265 266 267 268 365 455 555 655",
253 GetTraversalOrder(frame_tree, child_244));
254 EXPECT_EQ("2 14 15 16 244 255 264 265 266 267 268 365 455 555 655",
255 GetTraversalOrder(frame_tree, child_245));
256 EXPECT_EQ("2 14 16 244 245 264 265 266 267 268 365 455 555 655",
257 GetTraversalOrder(frame_tree, child_15));
258 EXPECT_EQ("2 14 15 16 244 245 255 264 265 266 268",
259 GetTraversalOrder(frame_tree, child_267));
260 EXPECT_EQ("2 14 15 16 244 245 255 264 265 266 267 268 365 455",
261 GetTraversalOrder(frame_tree, child_555));
262 EXPECT_EQ("2 14 15 16 244 245 255 264 265 266 267 268 365 455 555",
263 GetTraversalOrder(frame_tree, child_655));
264
[email protected]58faf942014-02-20 21:03:58265 frame_tree->RemoveFrame(child_555);
dcheng3ce04b62015-10-26 23:30:55266 EXPECT_EQ(
nick8814e652015-12-18 01:44:12267 "2: [14: [244: [], 245: []], "
268 "15: [255 'no children node': []], "
269 "16: [264: [], 265: [], 266: [], "
270 "267 'node with deep subtree': "
271 "[365: [455: []]], 268: []]]",
dcheng3ce04b62015-10-26 23:30:55272 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55273
[email protected]58faf942014-02-20 21:03:58274 frame_tree->RemoveFrame(child_16->child_at(1));
dcheng3ce04b62015-10-26 23:30:55275 EXPECT_EQ(
nick8814e652015-12-18 01:44:12276 "2: [14: [244: [], 245: []], "
277 "15: [255 'no children node': []], "
278 "16: [264: [], 266: [], "
279 "267 'node with deep subtree': "
280 "[365: [455: []]], 268: []]]",
dcheng3ce04b62015-10-26 23:30:55281 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55282
[email protected]58faf942014-02-20 21:03:58283 frame_tree->RemoveFrame(root->child_at(1));
dcheng3ce04b62015-10-26 23:30:55284 EXPECT_EQ(
nick8814e652015-12-18 01:44:12285 "2: [14: [244: [], 245: []], "
286 "16: [264: [], 266: [], "
287 "267 'node with deep subtree': "
288 "[365: [455: []]], 268: []]]",
dcheng3ce04b62015-10-26 23:30:55289 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55290}
291
creis6a93a812015-04-24 23:13:17292// Ensure frames can be found by frame_tree_node_id, routing ID, or name.
293TEST_F(FrameTreeTest, FindFrames) {
lfg269b702f2015-06-08 19:28:19294 main_test_rfh()->InitializeRenderFrameIfNeeded();
295
creis6a93a812015-04-24 23:13:17296 // Add a few child frames to the main frame.
297 FrameTree* frame_tree = contents()->GetFrameTree();
298 FrameTreeNode* root = frame_tree->root();
lfg269b702f2015-06-08 19:28:19299
dcheng860817a2015-05-22 03:16:56300 main_test_rfh()->OnCreateChildFrame(22, blink::WebTreeScopeType::Document,
lazyboy70605c32015-11-03 01:27:31301 "child0", blink::WebSandboxFlags::None,
302 blink::WebFrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56303 main_test_rfh()->OnCreateChildFrame(23, blink::WebTreeScopeType::Document,
lazyboy70605c32015-11-03 01:27:31304 "child1", blink::WebSandboxFlags::None,
305 blink::WebFrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56306 main_test_rfh()->OnCreateChildFrame(24, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36307 std::string(),
lazyboy70605c32015-11-03 01:27:31308 blink::WebSandboxFlags::None,
309 blink::WebFrameOwnerProperties());
creis6a93a812015-04-24 23:13:17310 FrameTreeNode* child0 = root->child_at(0);
311 FrameTreeNode* child1 = root->child_at(1);
dcheng860817a2015-05-22 03:16:56312
creis6a93a812015-04-24 23:13:17313 FrameTreeNode* child2 = root->child_at(2);
314
315 // Add one grandchild frame.
dcheng860817a2015-05-22 03:16:56316 child1->current_frame_host()->OnCreateChildFrame(
dcheng5f60abb2015-05-28 01:39:36317 33, blink::WebTreeScopeType::Document, "grandchild",
lazyboy70605c32015-11-03 01:27:31318 blink::WebSandboxFlags::None, blink::WebFrameOwnerProperties());
creis6a93a812015-04-24 23:13:17319 FrameTreeNode* grandchild = child1->child_at(0);
320
321 // Ensure they can be found by FTN id.
322 EXPECT_EQ(root, frame_tree->FindByID(root->frame_tree_node_id()));
323 EXPECT_EQ(child0, frame_tree->FindByID(child0->frame_tree_node_id()));
324 EXPECT_EQ(child1, frame_tree->FindByID(child1->frame_tree_node_id()));
325 EXPECT_EQ(child2, frame_tree->FindByID(child2->frame_tree_node_id()));
326 EXPECT_EQ(grandchild, frame_tree->FindByID(grandchild->frame_tree_node_id()));
327 EXPECT_EQ(nullptr, frame_tree->FindByID(-1));
328
329 // Ensure they can be found by routing id.
330 int process_id = main_test_rfh()->GetProcess()->GetID();
331 EXPECT_EQ(root, frame_tree->FindByRoutingID(process_id,
332 main_test_rfh()->GetRoutingID()));
333 EXPECT_EQ(child0, frame_tree->FindByRoutingID(process_id, 22));
334 EXPECT_EQ(child1, frame_tree->FindByRoutingID(process_id, 23));
335 EXPECT_EQ(child2, frame_tree->FindByRoutingID(process_id, 24));
336 EXPECT_EQ(grandchild, frame_tree->FindByRoutingID(process_id, 33));
337 EXPECT_EQ(nullptr, frame_tree->FindByRoutingID(process_id, 37));
338
339 // Ensure they can be found by name, if they have one.
340 EXPECT_EQ(root, frame_tree->FindByName(std::string()));
341 EXPECT_EQ(child0, frame_tree->FindByName("child0"));
342 EXPECT_EQ(child1, frame_tree->FindByName("child1"));
343 EXPECT_EQ(grandchild, frame_tree->FindByName("grandchild"));
344 EXPECT_EQ(nullptr, frame_tree->FindByName("no such frame"));
345}
346
alexmos9f8705a2015-05-06 19:58:59347// Check that PreviousSibling() is retrieved correctly.
348TEST_F(FrameTreeTest, PreviousSibling) {
lfg269b702f2015-06-08 19:28:19349 main_test_rfh()->InitializeRenderFrameIfNeeded();
350
alexmos9f8705a2015-05-06 19:58:59351 // Add a few child frames to the main frame.
352 FrameTree* frame_tree = contents()->GetFrameTree();
353 FrameTreeNode* root = frame_tree->root();
dcheng860817a2015-05-22 03:16:56354 main_test_rfh()->OnCreateChildFrame(22, blink::WebTreeScopeType::Document,
lazyboy70605c32015-11-03 01:27:31355 "child0", blink::WebSandboxFlags::None,
356 blink::WebFrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56357 main_test_rfh()->OnCreateChildFrame(23, blink::WebTreeScopeType::Document,
lazyboy70605c32015-11-03 01:27:31358 "child1", blink::WebSandboxFlags::None,
359 blink::WebFrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56360 main_test_rfh()->OnCreateChildFrame(24, blink::WebTreeScopeType::Document,
lazyboy70605c32015-11-03 01:27:31361 "child2", blink::WebSandboxFlags::None,
362 blink::WebFrameOwnerProperties());
alexmos9f8705a2015-05-06 19:58:59363 FrameTreeNode* child0 = root->child_at(0);
364 FrameTreeNode* child1 = root->child_at(1);
365 FrameTreeNode* child2 = root->child_at(2);
366
367 // Add one grandchild frame.
dcheng860817a2015-05-22 03:16:56368 child1->current_frame_host()->OnCreateChildFrame(
dcheng5f60abb2015-05-28 01:39:36369 33, blink::WebTreeScopeType::Document, "grandchild",
lazyboy70605c32015-11-03 01:27:31370 blink::WebSandboxFlags::None, blink::WebFrameOwnerProperties());
alexmos9f8705a2015-05-06 19:58:59371 FrameTreeNode* grandchild = child1->child_at(0);
372
373 EXPECT_EQ(nullptr, root->PreviousSibling());
374 EXPECT_EQ(nullptr, child0->PreviousSibling());
375 EXPECT_EQ(child0, child1->PreviousSibling());
376 EXPECT_EQ(child1, child2->PreviousSibling());
377 EXPECT_EQ(nullptr, grandchild->PreviousSibling());
378}
379
[email protected]14266072014-04-19 00:35:20380// Do some simple manipulations of the frame tree, making sure that
381// WebContentsObservers see a consistent view of the tree as we go.
382TEST_F(FrameTreeTest, ObserverWalksTreeDuringFrameCreation) {
383 TreeWalkingWebContentsLogger activity(contents());
schenney6408fed22015-04-17 17:44:57384 contents()->NavigateAndCommit(GURL("http://www.google.com"));
dcheng3ce04b62015-10-26 23:30:55385 EXPECT_EQ("RenderFrameCreated(2) -> 2: []", activity.GetLog());
schenney6408fed22015-04-17 17:44:57386
[email protected]14266072014-04-19 00:35:20387 FrameTree* frame_tree = contents()->GetFrameTree();
388 FrameTreeNode* root = frame_tree->root();
389
390 // Simulate attaching a series of frames to build the frame tree.
dcheng860817a2015-05-22 03:16:56391 main_test_rfh()->OnCreateChildFrame(14, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36392 std::string(),
lazyboy70605c32015-11-03 01:27:31393 blink::WebSandboxFlags::None,
394 blink::WebFrameOwnerProperties());
naskof5940b9f2015-03-02 23:04:05395 EXPECT_EQ(
dcheng3ce04b62015-10-26 23:30:55396 "RenderFrameHostChanged(new)(14) -> 2: []\n"
397 "RenderFrameCreated(14) -> 2: [14: []]",
naskof5940b9f2015-03-02 23:04:05398 activity.GetLog());
dcheng860817a2015-05-22 03:16:56399 main_test_rfh()->OnCreateChildFrame(18, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36400 std::string(),
lazyboy70605c32015-11-03 01:27:31401 blink::WebSandboxFlags::None,
402 blink::WebFrameOwnerProperties());
naskof5940b9f2015-03-02 23:04:05403 EXPECT_EQ(
dcheng3ce04b62015-10-26 23:30:55404 "RenderFrameHostChanged(new)(18) -> 2: [14: []]\n"
405 "RenderFrameCreated(18) -> 2: [14: [], 18: []]",
naskof5940b9f2015-03-02 23:04:05406 activity.GetLog());
[email protected]14266072014-04-19 00:35:20407 frame_tree->RemoveFrame(root->child_at(0));
dcheng3ce04b62015-10-26 23:30:55408 EXPECT_EQ("RenderFrameDeleted(14) -> 2: [18: []]", 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(18) -> 2: []", activity.GetLog());
[email protected]14266072014-04-19 00:35:20411}
412
413// Make sure that WebContentsObservers see a consistent view of the tree after
414// recovery from a render process crash.
415TEST_F(FrameTreeTest, ObserverWalksTreeAfterCrash) {
416 TreeWalkingWebContentsLogger activity(contents());
schenney6408fed22015-04-17 17:44:57417 contents()->NavigateAndCommit(GURL("http://www.google.com"));
dcheng3ce04b62015-10-26 23:30:55418 EXPECT_EQ("RenderFrameCreated(2) -> 2: []", activity.GetLog());
[email protected]14266072014-04-19 00:35:20419
dcheng860817a2015-05-22 03:16:56420 main_test_rfh()->OnCreateChildFrame(22, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36421 std::string(),
lazyboy70605c32015-11-03 01:27:31422 blink::WebSandboxFlags::None,
423 blink::WebFrameOwnerProperties());
naskof5940b9f2015-03-02 23:04:05424 EXPECT_EQ(
dcheng3ce04b62015-10-26 23:30:55425 "RenderFrameHostChanged(new)(22) -> 2: []\n"
426 "RenderFrameCreated(22) -> 2: [22: []]",
naskof5940b9f2015-03-02 23:04:05427 activity.GetLog());
dcheng860817a2015-05-22 03:16:56428 main_test_rfh()->OnCreateChildFrame(23, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36429 std::string(),
lazyboy70605c32015-11-03 01:27:31430 blink::WebSandboxFlags::None,
431 blink::WebFrameOwnerProperties());
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(
459 root, process_id + 1, 1, blink::WebTreeScopeType::Document, std::string(),
lazyboy70605c32015-11-03 01:27:31460 blink::WebSandboxFlags::None, blink::WebFrameOwnerProperties()));
dcheng3ce04b62015-10-26 23:30:55461 ASSERT_EQ("2: []", GetTreeState(frame_tree));
dgroganfb22f9a2014-10-20 21:32:32462}
463
naskoaeca57b2015-02-13 00:50:46464// Ensure that frames removed while a process has crashed are not preserved in
465// the global map of id->frame.
466TEST_F(FrameTreeTest, ProcessCrashClearsGlobalMap) {
lfg269b702f2015-06-08 19:28:19467 main_test_rfh()->InitializeRenderFrameIfNeeded();
468
naskoaeca57b2015-02-13 00:50:46469 // Add a couple child frames to the main frame.
470 FrameTreeNode* root = contents()->GetFrameTree()->root();
471
dcheng860817a2015-05-22 03:16:56472 main_test_rfh()->OnCreateChildFrame(22, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36473 std::string(),
lazyboy70605c32015-11-03 01:27:31474 blink::WebSandboxFlags::None,
475 blink::WebFrameOwnerProperties());
dcheng860817a2015-05-22 03:16:56476 main_test_rfh()->OnCreateChildFrame(23, blink::WebTreeScopeType::Document,
dcheng5f60abb2015-05-28 01:39:36477 std::string(),
lazyboy70605c32015-11-03 01:27:31478 blink::WebSandboxFlags::None,
479 blink::WebFrameOwnerProperties());
naskoaeca57b2015-02-13 00:50:46480
dmazzonie950ea232015-03-13 21:39:45481 // Add one grandchild frame.
482 RenderFrameHostImpl* child1_rfh = root->child_at(0)->current_frame_host();
dcheng860817a2015-05-22 03:16:56483 child1_rfh->OnCreateChildFrame(33, blink::WebTreeScopeType::Document,
lazyboy70605c32015-11-03 01:27:31484 std::string(), blink::WebSandboxFlags::None,
485 blink::WebFrameOwnerProperties());
dmazzonie950ea232015-03-13 21:39:45486
naskoaeca57b2015-02-13 00:50:46487 // Ensure they can be found by id.
vishal.b782eb5d2015-04-29 12:22:57488 int id1 = root->child_at(0)->frame_tree_node_id();
489 int id2 = root->child_at(1)->frame_tree_node_id();
490 int id3 = root->child_at(0)->child_at(0)->frame_tree_node_id();
dmazzonie950ea232015-03-13 21:39:45491 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id1));
492 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id2));
493 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id3));
naskoaeca57b2015-02-13 00:50:46494
495 // Crash the renderer.
nick16b07652015-04-18 02:35:31496 main_test_rfh()->GetProcess()->SimulateCrash();
naskoaeca57b2015-02-13 00:50:46497
498 // Ensure they cannot be found by id after the process has crashed.
dmazzonie950ea232015-03-13 21:39:45499 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id1));
500 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id2));
501 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id3));
naskoaeca57b2015-02-13 00:50:46502}
503
[email protected]9b159a52013-10-03 17:24:55504} // namespace content