blob: 015150461cdbe6f45cb50e5358791d6a09c89cba [file] [log] [blame]
Albert J. Wongcb004632018-07-10 22:58:251// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]9b159a52013-10-03 17:24:552// 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"
Blink Reformata30d4232018-04-07 15:31:0627#include "third_party/blink/public/common/frame/frame_policy.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
Balazs Engedyba034e72017-10-27 22:26:2855service_manager::mojom::InterfaceProviderRequest
56CreateStubInterfaceProviderRequest() {
57 return TestRenderFrameHost::CreateStubInterfaceProviderRequest();
58}
59
[email protected]14266072014-04-19 00:35:2060// Logs calls to WebContentsObserver along with the state of the frame tree,
61// for later use in EXPECT_EQ().
62class TreeWalkingWebContentsLogger : public WebContentsObserver {
63 public:
64 explicit TreeWalkingWebContentsLogger(WebContents* web_contents)
65 : WebContentsObserver(web_contents) {}
66
dchengc2282aa2014-10-21 12:07:5867 ~TreeWalkingWebContentsLogger() override {
[email protected]14266072014-04-19 00:35:2068 EXPECT_EQ("", log_) << "Activity logged that was not expected";
69 }
70
71 // Gets and resets the log, which is a string of what happened.
72 std::string GetLog() {
73 std::string result = log_;
74 log_.clear();
75 return result;
76 }
77
78 // content::WebContentsObserver implementation.
dchengc2282aa2014-10-21 12:07:5879 void RenderFrameCreated(RenderFrameHost* render_frame_host) override {
[email protected]14266072014-04-19 00:35:2080 LogWhatHappened("RenderFrameCreated", render_frame_host);
81 }
82
dchengc2282aa2014-10-21 12:07:5883 void RenderFrameHostChanged(RenderFrameHost* old_host,
84 RenderFrameHost* new_host) override {
[email protected]02d7b6e2014-06-24 21:01:5085 if (old_host)
naskof5940b9f2015-03-02 23:04:0586 LogWhatHappened("RenderFrameHostChanged(old)", old_host);
87 LogWhatHappened("RenderFrameHostChanged(new)", new_host);
[email protected]02d7b6e2014-06-24 21:01:5088 }
89
dchengc2282aa2014-10-21 12:07:5890 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override {
[email protected]14266072014-04-19 00:35:2091 LogWhatHappened("RenderFrameDeleted", render_frame_host);
92 }
93
dchengc2282aa2014-10-21 12:07:5894 void RenderProcessGone(base::TerminationStatus status) override {
[email protected]14266072014-04-19 00:35:2095 LogWhatHappened("RenderProcessGone");
96 }
97
98 private:
99 void LogWhatHappened(const std::string& event_name) {
100 if (!log_.empty()) {
101 log_.append("\n");
102 }
103 log_.append(event_name + " -> ");
104 AppendTreeNodeState(
105 static_cast<WebContentsImpl*>(web_contents())->GetFrameTree()->root(),
106 &log_);
107 }
108
109 void LogWhatHappened(const std::string& event_name, RenderFrameHost* rfh) {
110 LogWhatHappened(
111 base::StringPrintf("%s(%d)", event_name.c_str(), rfh->GetRoutingID()));
112 }
113
114 std::string log_;
115
116 DISALLOW_COPY_AND_ASSIGN(TreeWalkingWebContentsLogger);
117};
118
dgroganfb22f9a2014-10-20 21:32:32119} // namespace
120
[email protected]14266072014-04-19 00:35:20121class FrameTreeTest : public RenderViewHostImplTestHarness {
[email protected]7cc7ebd2013-10-08 00:59:00122 protected:
123 // Prints a FrameTree, for easy assertions of the tree hierarchy.
124 std::string GetTreeState(FrameTree* frame_tree) {
125 std::string result;
[email protected]fa944cb82013-11-15 17:51:21126 AppendTreeNodeState(frame_tree->root(), &result);
[email protected]7cc7ebd2013-10-08 00:59:00127 return result;
128 }
nick4ed970292016-01-20 21:46:45129
130 std::string GetTraversalOrder(FrameTree* frame_tree,
Alex Moshchuk27caae82017-09-11 23:11:18131 FrameTreeNode* subtree_to_skip) {
nick4ed970292016-01-20 21:46:45132 std::string result;
Alex Moshchuk27caae82017-09-11 23:11:18133 for (FrameTreeNode* node :
134 frame_tree->NodesExceptSubtree(subtree_to_skip)) {
dcheng57e39e22016-01-21 00:25:38135 if (!result.empty())
136 result += " ";
137 result += base::Int64ToString(node->current_frame_host()->GetRoutingID());
138 }
nick4ed970292016-01-20 21:46:45139 return result;
140 }
[email protected]9b159a52013-10-03 17:24:55141};
142
[email protected]9b159a52013-10-03 17:24:55143// Exercise tree manipulation routines.
144// - Add a series of nodes and verify tree structure.
145// - Remove a series of nodes and verify tree structure.
nasko9f2261b2015-07-02 11:40:26146TEST_F(FrameTreeTest, Shape) {
nick8814e652015-12-18 01:44:12147 main_test_rfh()->InitializeRenderFrameIfNeeded();
148
[email protected]94d0cc12013-12-18 00:07:41149 // Use the FrameTree of the WebContents so that it has all the delegates it
150 // needs. We may want to consider a test version of this.
[email protected]14266072014-04-19 00:35:20151 FrameTree* frame_tree = contents()->GetFrameTree();
[email protected]58faf942014-02-20 21:03:58152 FrameTreeNode* root = frame_tree->root();
[email protected]190b8c52013-11-09 01:35:44153
[email protected]9b159a52013-10-03 17:24:55154 std::string no_children_node("no children node");
155 std::string deep_subtree("node with deep subtree");
dgroganfb22f9a2014-10-20 21:32:32156 int process_id = root->current_frame_host()->GetProcess()->GetID();
[email protected]9b159a52013-10-03 17:24:55157
nasko9f2261b2015-07-02 11:40:26158 // Do not navigate each frame separately, since that will clutter the test
159 // itself. Instead, leave them in "not live" state, which is indicated by the
160 // * after the frame id, since this test cares about the shape, not the
nick8814e652015-12-18 01:44:12161 // frame liveness.
Albert J. Wongcb004632018-07-10 22:58:25162 EXPECT_EQ("3: []", GetTreeState(frame_tree));
[email protected]7cc7ebd2013-10-08 00:59:00163
[email protected]9b159a52013-10-03 17:24:55164 // Simulate attaching a series of frames to build the frame tree.
Luna Luc3fdacdf2017-11-08 04:48:53165 frame_tree->AddFrame(root, process_id, 14,
166 CreateStubInterfaceProviderRequest(),
167 blink::WebTreeScopeType::kDocument, std::string(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45168 "uniqueName0", false, base::UnguessableToken::Create(),
Shubhie Panickerddf2a4e2018-03-06 00:09:06169 blink::FramePolicy(), FrameOwnerProperties(), false);
Luna Luc3fdacdf2017-11-08 04:48:53170 frame_tree->AddFrame(root, process_id, 15,
171 CreateStubInterfaceProviderRequest(),
172 blink::WebTreeScopeType::kDocument, std::string(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45173 "uniqueName1", false, base::UnguessableToken::Create(),
Shubhie Panickerddf2a4e2018-03-06 00:09:06174 blink::FramePolicy(), FrameOwnerProperties(), false);
Luna Luc3fdacdf2017-11-08 04:48:53175 frame_tree->AddFrame(root, process_id, 16,
176 CreateStubInterfaceProviderRequest(),
177 blink::WebTreeScopeType::kDocument, std::string(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45178 "uniqueName2", false, base::UnguessableToken::Create(),
Shubhie Panickerddf2a4e2018-03-06 00:09:06179 blink::FramePolicy(), FrameOwnerProperties(), false);
Luna Luc3fdacdf2017-11-08 04:48:53180 frame_tree->AddFrame(root->child_at(0), process_id, 244,
181 CreateStubInterfaceProviderRequest(),
182 blink::WebTreeScopeType::kDocument, std::string(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45183 "uniqueName3", false, base::UnguessableToken::Create(),
Shubhie Panickerddf2a4e2018-03-06 00:09:06184 blink::FramePolicy(), FrameOwnerProperties(), false);
Luna Luc3fdacdf2017-11-08 04:48:53185 frame_tree->AddFrame(root->child_at(1), process_id, 255,
186 CreateStubInterfaceProviderRequest(),
187 blink::WebTreeScopeType::kDocument, no_children_node,
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45188 "uniqueName4", false, base::UnguessableToken::Create(),
Shubhie Panickerddf2a4e2018-03-06 00:09:06189 blink::FramePolicy(), FrameOwnerProperties(), false);
Luna Luc3fdacdf2017-11-08 04:48:53190 frame_tree->AddFrame(root->child_at(0), process_id, 245,
191 CreateStubInterfaceProviderRequest(),
192 blink::WebTreeScopeType::kDocument, std::string(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45193 "uniqueName5", false, base::UnguessableToken::Create(),
Shubhie Panickerddf2a4e2018-03-06 00:09:06194 blink::FramePolicy(), FrameOwnerProperties(), false);
[email protected]9b159a52013-10-03 17:24:55195
dcheng3ce04b62015-10-26 23:30:55196 EXPECT_EQ(
Albert J. Wongcb004632018-07-10 22:58:25197 "3: [14: [244: [], 245: []], "
nick8814e652015-12-18 01:44:12198 "15: [255 'no children node': []], "
199 "16: []]",
dcheng3ce04b62015-10-26 23:30:55200 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55201
[email protected]58faf942014-02-20 21:03:58202 FrameTreeNode* child_16 = root->child_at(2);
Luna Luc3fdacdf2017-11-08 04:48:53203 frame_tree->AddFrame(child_16, process_id, 264,
204 CreateStubInterfaceProviderRequest(),
205 blink::WebTreeScopeType::kDocument, std::string(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45206 "uniqueName6", false, base::UnguessableToken::Create(),
Shubhie Panickerddf2a4e2018-03-06 00:09:06207 blink::FramePolicy(), FrameOwnerProperties(), false);
Luna Luc3fdacdf2017-11-08 04:48:53208 frame_tree->AddFrame(child_16, process_id, 265,
209 CreateStubInterfaceProviderRequest(),
210 blink::WebTreeScopeType::kDocument, std::string(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45211 "uniqueName7", false, base::UnguessableToken::Create(),
Shubhie Panickerddf2a4e2018-03-06 00:09:06212 blink::FramePolicy(), FrameOwnerProperties(), false);
Luna Luc3fdacdf2017-11-08 04:48:53213 frame_tree->AddFrame(child_16, process_id, 266,
214 CreateStubInterfaceProviderRequest(),
215 blink::WebTreeScopeType::kDocument, std::string(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45216 "uniqueName8", false, base::UnguessableToken::Create(),
Shubhie Panickerddf2a4e2018-03-06 00:09:06217 blink::FramePolicy(), FrameOwnerProperties(), false);
Luna Luc3fdacdf2017-11-08 04:48:53218 frame_tree->AddFrame(child_16, process_id, 267,
219 CreateStubInterfaceProviderRequest(),
220 blink::WebTreeScopeType::kDocument, deep_subtree,
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45221 "uniqueName9", false, base::UnguessableToken::Create(),
Shubhie Panickerddf2a4e2018-03-06 00:09:06222 blink::FramePolicy(), FrameOwnerProperties(), false);
Luna Luc3fdacdf2017-11-08 04:48:53223 frame_tree->AddFrame(child_16, process_id, 268,
224 CreateStubInterfaceProviderRequest(),
225 blink::WebTreeScopeType::kDocument, std::string(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45226 "uniqueName10", false, base::UnguessableToken::Create(),
Shubhie Panickerddf2a4e2018-03-06 00:09:06227 blink::FramePolicy(), FrameOwnerProperties(), false);
[email protected]9b159a52013-10-03 17:24:55228
[email protected]58faf942014-02-20 21:03:58229 FrameTreeNode* child_267 = child_16->child_at(3);
Luna Luc3fdacdf2017-11-08 04:48:53230 frame_tree->AddFrame(child_267, process_id, 365,
231 CreateStubInterfaceProviderRequest(),
232 blink::WebTreeScopeType::kDocument, std::string(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45233 "uniqueName11", false, base::UnguessableToken::Create(),
Shubhie Panickerddf2a4e2018-03-06 00:09:06234 blink::FramePolicy(), FrameOwnerProperties(), false);
dcheng860817a2015-05-22 03:16:56235 frame_tree->AddFrame(child_267->child_at(0), process_id, 455,
Balazs Engedyba034e72017-10-27 22:26:28236 CreateStubInterfaceProviderRequest(),
Blink Reformat1c4d759e2017-04-09 16:34:54237 blink::WebTreeScopeType::kDocument, std::string(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45238 "uniqueName12", false, base::UnguessableToken::Create(),
Shubhie Panickerddf2a4e2018-03-06 00:09:06239 blink::FramePolicy(), FrameOwnerProperties(), false);
dgroganfb22f9a2014-10-20 21:32:32240 frame_tree->AddFrame(child_267->child_at(0)->child_at(0), process_id, 555,
Balazs Engedyba034e72017-10-27 22:26:28241 CreateStubInterfaceProviderRequest(),
Blink Reformat1c4d759e2017-04-09 16:34:54242 blink::WebTreeScopeType::kDocument, std::string(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45243 "uniqueName13", false, base::UnguessableToken::Create(),
Shubhie Panickerddf2a4e2018-03-06 00:09:06244 blink::FramePolicy(), FrameOwnerProperties(), false);
Balazs Engedyba034e72017-10-27 22:26:28245 frame_tree->AddFrame(child_267->child_at(0)->child_at(0)->child_at(0),
246 process_id, 655, CreateStubInterfaceProviderRequest(),
247 blink::WebTreeScopeType::kDocument, std::string(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45248 "uniqueName14", false, base::UnguessableToken::Create(),
Shubhie Panickerddf2a4e2018-03-06 00:09:06249 blink::FramePolicy(), FrameOwnerProperties(), false);
[email protected]9b159a52013-10-03 17:24:55250
[email protected]7cc7ebd2013-10-08 00:59:00251 // Now that's it's fully built, verify the tree structure is as expected.
dcheng3ce04b62015-10-26 23:30:55252 EXPECT_EQ(
Albert J. Wongcb004632018-07-10 22:58:25253 "3: [14: [244: [], 245: []], "
nick8814e652015-12-18 01:44:12254 "15: [255 'no children node': []], "
255 "16: [264: [], 265: [], 266: [], "
256 "267 'node with deep subtree': "
257 "[365: [455: [555: [655: []]]]], 268: []]]",
dcheng3ce04b62015-10-26 23:30:55258 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55259
nick4ed970292016-01-20 21:46:45260 // Verify that traversal order is breadth first, even if we skip a subtree.
261 FrameTreeNode* child_14 = root->child_at(0);
262 FrameTreeNode* child_15 = root->child_at(1);
263 FrameTreeNode* child_244 = child_14->child_at(0);
264 FrameTreeNode* child_245 = child_14->child_at(1);
[email protected]58faf942014-02-20 21:03:58265 FrameTreeNode* child_555 = child_267->child_at(0)->child_at(0)->child_at(0);
nick4ed970292016-01-20 21:46:45266 FrameTreeNode* child_655 = child_555->child_at(0);
Albert J. Wongcb004632018-07-10 22:58:25267 EXPECT_EQ("3 14 15 16 244 245 255 264 265 266 267 268 365 455 555 655",
nick4ed970292016-01-20 21:46:45268 GetTraversalOrder(frame_tree, nullptr));
Albert J. Wongcb004632018-07-10 22:58:25269 EXPECT_EQ("3", GetTraversalOrder(frame_tree, root));
270 EXPECT_EQ("3 14 15 16 255 264 265 266 267 268 365 455 555 655",
nick4ed970292016-01-20 21:46:45271 GetTraversalOrder(frame_tree, child_14));
Albert J. Wongcb004632018-07-10 22:58:25272 EXPECT_EQ("3 14 15 16 244 245 255 264 265 266 267 268 365 455 555 655",
nick4ed970292016-01-20 21:46:45273 GetTraversalOrder(frame_tree, child_244));
Albert J. Wongcb004632018-07-10 22:58:25274 EXPECT_EQ("3 14 15 16 244 245 255 264 265 266 267 268 365 455 555 655",
nick4ed970292016-01-20 21:46:45275 GetTraversalOrder(frame_tree, child_245));
Albert J. Wongcb004632018-07-10 22:58:25276 EXPECT_EQ("3 14 15 16 244 245 264 265 266 267 268 365 455 555 655",
nick4ed970292016-01-20 21:46:45277 GetTraversalOrder(frame_tree, child_15));
Albert J. Wongcb004632018-07-10 22:58:25278 EXPECT_EQ("3 14 15 16 244 245 255 264 265 266 267 268",
nick4ed970292016-01-20 21:46:45279 GetTraversalOrder(frame_tree, child_267));
Albert J. Wongcb004632018-07-10 22:58:25280 EXPECT_EQ("3 14 15 16 244 245 255 264 265 266 267 268 365 455 555",
Alex Moshchuk27caae82017-09-11 23:11:18281 GetTraversalOrder(frame_tree, child_555));
Albert J. Wongcb004632018-07-10 22:58:25282 EXPECT_EQ("3 14 15 16 244 245 255 264 265 266 267 268 365 455 555 655",
nick4ed970292016-01-20 21:46:45283 GetTraversalOrder(frame_tree, child_655));
284
[email protected]58faf942014-02-20 21:03:58285 frame_tree->RemoveFrame(child_555);
dcheng3ce04b62015-10-26 23:30:55286 EXPECT_EQ(
Albert J. Wongcb004632018-07-10 22:58:25287 "3: [14: [244: [], 245: []], "
nick8814e652015-12-18 01:44:12288 "15: [255 'no children node': []], "
289 "16: [264: [], 265: [], 266: [], "
290 "267 'node with deep subtree': "
291 "[365: [455: []]], 268: []]]",
dcheng3ce04b62015-10-26 23:30:55292 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55293
[email protected]58faf942014-02-20 21:03:58294 frame_tree->RemoveFrame(child_16->child_at(1));
dcheng3ce04b62015-10-26 23:30:55295 EXPECT_EQ(
Albert J. Wongcb004632018-07-10 22:58:25296 "3: [14: [244: [], 245: []], "
nick8814e652015-12-18 01:44:12297 "15: [255 'no children node': []], "
298 "16: [264: [], 266: [], "
299 "267 'node with deep subtree': "
300 "[365: [455: []]], 268: []]]",
dcheng3ce04b62015-10-26 23:30:55301 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55302
[email protected]58faf942014-02-20 21:03:58303 frame_tree->RemoveFrame(root->child_at(1));
dcheng3ce04b62015-10-26 23:30:55304 EXPECT_EQ(
Albert J. Wongcb004632018-07-10 22:58:25305 "3: [14: [244: [], 245: []], "
nick8814e652015-12-18 01:44:12306 "16: [264: [], 266: [], "
307 "267 'node with deep subtree': "
308 "[365: [455: []]], 268: []]]",
dcheng3ce04b62015-10-26 23:30:55309 GetTreeState(frame_tree));
[email protected]9b159a52013-10-03 17:24:55310}
311
creis6a93a812015-04-24 23:13:17312// Ensure frames can be found by frame_tree_node_id, routing ID, or name.
Charlie Reisb1405622018-04-02 22:52:39313TEST_F(FrameTreeTest, FindFrames) {
lfg269b702f2015-06-08 19:28:19314 main_test_rfh()->InitializeRenderFrameIfNeeded();
315
creis6a93a812015-04-24 23:13:17316 // Add a few child frames to the main frame.
317 FrameTree* frame_tree = contents()->GetFrameTree();
318 FrameTreeNode* root = frame_tree->root();
lfg269b702f2015-06-08 19:28:19319
lukasza464d8692016-02-22 19:26:32320 main_test_rfh()->OnCreateChildFrame(
Balazs Engedyba034e72017-10-27 22:26:28321 22, CreateStubInterfaceProviderRequest(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45322 blink::WebTreeScopeType::kDocument, "child0", "uniqueName0", false,
Luna Luc3fdacdf2017-11-08 04:48:53323 base::UnguessableToken::Create(), blink::FramePolicy(),
324 FrameOwnerProperties());
lukasza464d8692016-02-22 19:26:32325 main_test_rfh()->OnCreateChildFrame(
Balazs Engedyba034e72017-10-27 22:26:28326 23, CreateStubInterfaceProviderRequest(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45327 blink::WebTreeScopeType::kDocument, "child1", "uniqueName1", false,
Luna Luc3fdacdf2017-11-08 04:48:53328 base::UnguessableToken::Create(), blink::FramePolicy(),
329 FrameOwnerProperties());
lukasza464d8692016-02-22 19:26:32330 main_test_rfh()->OnCreateChildFrame(
Balazs Engedyba034e72017-10-27 22:26:28331 24, CreateStubInterfaceProviderRequest(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45332 blink::WebTreeScopeType::kDocument, std::string(), "uniqueName2", false,
Luna Luc3fdacdf2017-11-08 04:48:53333 base::UnguessableToken::Create(), blink::FramePolicy(),
334 FrameOwnerProperties());
creis6a93a812015-04-24 23:13:17335 FrameTreeNode* child0 = root->child_at(0);
336 FrameTreeNode* child1 = root->child_at(1);
creis6a93a812015-04-24 23:13:17337 FrameTreeNode* child2 = root->child_at(2);
338
339 // Add one grandchild frame.
dcheng860817a2015-05-22 03:16:56340 child1->current_frame_host()->OnCreateChildFrame(
Balazs Engedyba034e72017-10-27 22:26:28341 33, CreateStubInterfaceProviderRequest(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45342 blink::WebTreeScopeType::kDocument, "grandchild", "uniqueName3", false,
Luna Luc3fdacdf2017-11-08 04:48:53343 base::UnguessableToken::Create(), blink::FramePolicy(),
344 FrameOwnerProperties());
creis6a93a812015-04-24 23:13:17345 FrameTreeNode* grandchild = child1->child_at(0);
346
347 // Ensure they can be found by FTN id.
348 EXPECT_EQ(root, frame_tree->FindByID(root->frame_tree_node_id()));
349 EXPECT_EQ(child0, frame_tree->FindByID(child0->frame_tree_node_id()));
350 EXPECT_EQ(child1, frame_tree->FindByID(child1->frame_tree_node_id()));
351 EXPECT_EQ(child2, frame_tree->FindByID(child2->frame_tree_node_id()));
352 EXPECT_EQ(grandchild, frame_tree->FindByID(grandchild->frame_tree_node_id()));
353 EXPECT_EQ(nullptr, frame_tree->FindByID(-1));
354
355 // Ensure they can be found by routing id.
356 int process_id = main_test_rfh()->GetProcess()->GetID();
357 EXPECT_EQ(root, frame_tree->FindByRoutingID(process_id,
358 main_test_rfh()->GetRoutingID()));
359 EXPECT_EQ(child0, frame_tree->FindByRoutingID(process_id, 22));
360 EXPECT_EQ(child1, frame_tree->FindByRoutingID(process_id, 23));
361 EXPECT_EQ(child2, frame_tree->FindByRoutingID(process_id, 24));
362 EXPECT_EQ(grandchild, frame_tree->FindByRoutingID(process_id, 33));
363 EXPECT_EQ(nullptr, frame_tree->FindByRoutingID(process_id, 37));
364
365 // Ensure they can be found by name, if they have one.
366 EXPECT_EQ(root, frame_tree->FindByName(std::string()));
367 EXPECT_EQ(child0, frame_tree->FindByName("child0"));
368 EXPECT_EQ(child1, frame_tree->FindByName("child1"));
369 EXPECT_EQ(grandchild, frame_tree->FindByName("grandchild"));
370 EXPECT_EQ(nullptr, frame_tree->FindByName("no such frame"));
371}
372
paulmeyer322777fb2016-05-16 23:15:39373// Check that PreviousSibling() and NextSibling() are retrieved correctly.
374TEST_F(FrameTreeTest, GetSibling) {
lfg269b702f2015-06-08 19:28:19375 main_test_rfh()->InitializeRenderFrameIfNeeded();
376
alexmos9f8705a2015-05-06 19:58:59377 // Add a few child frames to the main frame.
378 FrameTree* frame_tree = contents()->GetFrameTree();
379 FrameTreeNode* root = frame_tree->root();
lukasza464d8692016-02-22 19:26:32380 main_test_rfh()->OnCreateChildFrame(
Balazs Engedyba034e72017-10-27 22:26:28381 22, CreateStubInterfaceProviderRequest(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45382 blink::WebTreeScopeType::kDocument, "child0", "uniqueName0", false,
Luna Luc3fdacdf2017-11-08 04:48:53383 base::UnguessableToken::Create(), blink::FramePolicy(),
384 FrameOwnerProperties());
lukasza464d8692016-02-22 19:26:32385 main_test_rfh()->OnCreateChildFrame(
Balazs Engedyba034e72017-10-27 22:26:28386 23, CreateStubInterfaceProviderRequest(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45387 blink::WebTreeScopeType::kDocument, "child1", "uniqueName1", false,
Luna Luc3fdacdf2017-11-08 04:48:53388 base::UnguessableToken::Create(), blink::FramePolicy(),
389 FrameOwnerProperties());
lukasza464d8692016-02-22 19:26:32390 main_test_rfh()->OnCreateChildFrame(
Balazs Engedyba034e72017-10-27 22:26:28391 24, CreateStubInterfaceProviderRequest(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45392 blink::WebTreeScopeType::kDocument, "child2", "uniqueName2", false,
Luna Luc3fdacdf2017-11-08 04:48:53393 base::UnguessableToken::Create(), blink::FramePolicy(),
394 FrameOwnerProperties());
alexmos9f8705a2015-05-06 19:58:59395 FrameTreeNode* child0 = root->child_at(0);
396 FrameTreeNode* child1 = root->child_at(1);
397 FrameTreeNode* child2 = root->child_at(2);
398
399 // Add one grandchild frame.
dcheng860817a2015-05-22 03:16:56400 child1->current_frame_host()->OnCreateChildFrame(
Balazs Engedyba034e72017-10-27 22:26:28401 33, CreateStubInterfaceProviderRequest(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45402 blink::WebTreeScopeType::kDocument, "grandchild", "uniqueName3", false,
Luna Luc3fdacdf2017-11-08 04:48:53403 base::UnguessableToken::Create(), blink::FramePolicy(),
404 FrameOwnerProperties());
alexmos9f8705a2015-05-06 19:58:59405 FrameTreeNode* grandchild = child1->child_at(0);
406
paulmeyer322777fb2016-05-16 23:15:39407 // Test PreviousSibling().
alexmos9f8705a2015-05-06 19:58:59408 EXPECT_EQ(nullptr, root->PreviousSibling());
409 EXPECT_EQ(nullptr, child0->PreviousSibling());
410 EXPECT_EQ(child0, child1->PreviousSibling());
411 EXPECT_EQ(child1, child2->PreviousSibling());
412 EXPECT_EQ(nullptr, grandchild->PreviousSibling());
paulmeyer322777fb2016-05-16 23:15:39413
414 // Test NextSibling().
415 EXPECT_EQ(nullptr, root->NextSibling());
416 EXPECT_EQ(child1, child0->NextSibling());
417 EXPECT_EQ(child2, child1->NextSibling());
418 EXPECT_EQ(nullptr, child2->NextSibling());
419 EXPECT_EQ(nullptr, grandchild->NextSibling());
alexmos9f8705a2015-05-06 19:58:59420}
421
[email protected]14266072014-04-19 00:35:20422// Do some simple manipulations of the frame tree, making sure that
423// WebContentsObservers see a consistent view of the tree as we go.
424TEST_F(FrameTreeTest, ObserverWalksTreeDuringFrameCreation) {
425 TreeWalkingWebContentsLogger activity(contents());
schenney6408fed22015-04-17 17:44:57426 contents()->NavigateAndCommit(GURL("http://www.google.com"));
Albert J. Wongcb004632018-07-10 22:58:25427 EXPECT_EQ("RenderFrameCreated(3) -> 3: []", activity.GetLog());
schenney6408fed22015-04-17 17:44:57428
[email protected]14266072014-04-19 00:35:20429 FrameTree* frame_tree = contents()->GetFrameTree();
430 FrameTreeNode* root = frame_tree->root();
431
432 // Simulate attaching a series of frames to build the frame tree.
lukasza464d8692016-02-22 19:26:32433 main_test_rfh()->OnCreateChildFrame(
Balazs Engedyba034e72017-10-27 22:26:28434 14, CreateStubInterfaceProviderRequest(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45435 blink::WebTreeScopeType::kDocument, std::string(), "uniqueName0", false,
Luna Luc3fdacdf2017-11-08 04:48:53436 base::UnguessableToken::Create(), blink::FramePolicy(),
437 FrameOwnerProperties());
naskof5940b9f2015-03-02 23:04:05438 EXPECT_EQ(
Albert J. Wongcb004632018-07-10 22:58:25439 "RenderFrameHostChanged(new)(14) -> 3: []\n"
440 "RenderFrameCreated(14) -> 3: [14: []]",
naskof5940b9f2015-03-02 23:04:05441 activity.GetLog());
lukasza464d8692016-02-22 19:26:32442 main_test_rfh()->OnCreateChildFrame(
Balazs Engedyba034e72017-10-27 22:26:28443 18, CreateStubInterfaceProviderRequest(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45444 blink::WebTreeScopeType::kDocument, std::string(), "uniqueName1", false,
Luna Luc3fdacdf2017-11-08 04:48:53445 base::UnguessableToken::Create(), blink::FramePolicy(),
446 FrameOwnerProperties());
naskof5940b9f2015-03-02 23:04:05447 EXPECT_EQ(
Albert J. Wongcb004632018-07-10 22:58:25448 "RenderFrameHostChanged(new)(18) -> 3: [14: []]\n"
449 "RenderFrameCreated(18) -> 3: [14: [], 18: []]",
naskof5940b9f2015-03-02 23:04:05450 activity.GetLog());
[email protected]14266072014-04-19 00:35:20451 frame_tree->RemoveFrame(root->child_at(0));
Albert J. Wongcb004632018-07-10 22:58:25452 EXPECT_EQ("RenderFrameDeleted(14) -> 3: [18: []]", activity.GetLog());
[email protected]14266072014-04-19 00:35:20453 frame_tree->RemoveFrame(root->child_at(0));
Albert J. Wongcb004632018-07-10 22:58:25454 EXPECT_EQ("RenderFrameDeleted(18) -> 3: []", activity.GetLog());
[email protected]14266072014-04-19 00:35:20455}
456
457// Make sure that WebContentsObservers see a consistent view of the tree after
458// recovery from a render process crash.
459TEST_F(FrameTreeTest, ObserverWalksTreeAfterCrash) {
460 TreeWalkingWebContentsLogger activity(contents());
schenney6408fed22015-04-17 17:44:57461 contents()->NavigateAndCommit(GURL("http://www.google.com"));
Albert J. Wongcb004632018-07-10 22:58:25462 EXPECT_EQ("RenderFrameCreated(3) -> 3: []", activity.GetLog());
[email protected]14266072014-04-19 00:35:20463
lukasza464d8692016-02-22 19:26:32464 main_test_rfh()->OnCreateChildFrame(
Balazs Engedyba034e72017-10-27 22:26:28465 22, CreateStubInterfaceProviderRequest(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45466 blink::WebTreeScopeType::kDocument, std::string(), "uniqueName0", false,
Luna Luc3fdacdf2017-11-08 04:48:53467 base::UnguessableToken::Create(), blink::FramePolicy(),
468 FrameOwnerProperties());
naskof5940b9f2015-03-02 23:04:05469 EXPECT_EQ(
Albert J. Wongcb004632018-07-10 22:58:25470 "RenderFrameHostChanged(new)(22) -> 3: []\n"
471 "RenderFrameCreated(22) -> 3: [22: []]",
naskof5940b9f2015-03-02 23:04:05472 activity.GetLog());
lukasza464d8692016-02-22 19:26:32473 main_test_rfh()->OnCreateChildFrame(
Balazs Engedyba034e72017-10-27 22:26:28474 23, CreateStubInterfaceProviderRequest(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45475 blink::WebTreeScopeType::kDocument, std::string(), "uniqueName1", false,
Luna Luc3fdacdf2017-11-08 04:48:53476 base::UnguessableToken::Create(), blink::FramePolicy(),
477 FrameOwnerProperties());
naskof5940b9f2015-03-02 23:04:05478 EXPECT_EQ(
Albert J. Wongcb004632018-07-10 22:58:25479 "RenderFrameHostChanged(new)(23) -> 3: [22: []]\n"
480 "RenderFrameCreated(23) -> 3: [22: [], 23: []]",
naskof5940b9f2015-03-02 23:04:05481 activity.GetLog());
[email protected]14266072014-04-19 00:35:20482
483 // Crash the renderer
nick16b07652015-04-18 02:35:31484 main_test_rfh()->GetProcess()->SimulateCrash();
[email protected]14266072014-04-19 00:35:20485 EXPECT_EQ(
Albert J. Wongcb004632018-07-10 22:58:25486 "RenderProcessGone -> 3*: [22*: [], 23*: []]\n"
487 "RenderFrameDeleted(23) -> 3*: [22*: [], 23*: []]\n"
488 "RenderFrameDeleted(22) -> 3*: [22*: [], 23*: []]\n"
489 "RenderFrameDeleted(3) -> 3*: []",
[email protected]14266072014-04-19 00:35:20490 activity.GetLog());
491}
492
dgroganfb22f9a2014-10-20 21:32:32493// Ensure that frames are not added to the tree, if the process passed in
494// is different than the process of the parent node.
495TEST_F(FrameTreeTest, FailAddFrameWithWrongProcessId) {
schenney6408fed22015-04-17 17:44:57496 contents()->NavigateAndCommit(GURL("http://www.google.com"));
dgroganfb22f9a2014-10-20 21:32:32497 FrameTree* frame_tree = contents()->GetFrameTree();
498 FrameTreeNode* root = frame_tree->root();
499 int process_id = root->current_frame_host()->GetProcess()->GetID();
500
Albert J. Wongcb004632018-07-10 22:58:25501 ASSERT_EQ("3: []", GetTreeState(frame_tree));
dgroganfb22f9a2014-10-20 21:32:32502
503 // Simulate attaching a frame from mismatched process id.
dcheng5f60abb2015-05-28 01:39:36504 ASSERT_FALSE(frame_tree->AddFrame(
Balazs Engedyba034e72017-10-27 22:26:28505 root, process_id + 1, 1, CreateStubInterfaceProviderRequest(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45506 blink::WebTreeScopeType::kDocument, std::string(), "uniqueName0", false,
Luna Luc3fdacdf2017-11-08 04:48:53507 base::UnguessableToken::Create(), blink::FramePolicy(),
Shubhie Panickerddf2a4e2018-03-06 00:09:06508 FrameOwnerProperties(), false));
Albert J. Wongcb004632018-07-10 22:58:25509 ASSERT_EQ("3: []", GetTreeState(frame_tree));
dgroganfb22f9a2014-10-20 21:32:32510}
511
naskoaeca57b2015-02-13 00:50:46512// Ensure that frames removed while a process has crashed are not preserved in
513// the global map of id->frame.
514TEST_F(FrameTreeTest, ProcessCrashClearsGlobalMap) {
lfg269b702f2015-06-08 19:28:19515 main_test_rfh()->InitializeRenderFrameIfNeeded();
516
naskoaeca57b2015-02-13 00:50:46517 // Add a couple child frames to the main frame.
518 FrameTreeNode* root = contents()->GetFrameTree()->root();
519
lukasza464d8692016-02-22 19:26:32520 main_test_rfh()->OnCreateChildFrame(
Balazs Engedyba034e72017-10-27 22:26:28521 22, CreateStubInterfaceProviderRequest(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45522 blink::WebTreeScopeType::kDocument, std::string(), "uniqueName0", false,
Luna Luc3fdacdf2017-11-08 04:48:53523 base::UnguessableToken::Create(), blink::FramePolicy(),
524 FrameOwnerProperties());
lukasza464d8692016-02-22 19:26:32525 main_test_rfh()->OnCreateChildFrame(
Balazs Engedyba034e72017-10-27 22:26:28526 23, CreateStubInterfaceProviderRequest(),
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45527 blink::WebTreeScopeType::kDocument, std::string(), "uniqueName1", false,
Luna Luc3fdacdf2017-11-08 04:48:53528 base::UnguessableToken::Create(), blink::FramePolicy(),
529 FrameOwnerProperties());
naskoaeca57b2015-02-13 00:50:46530
dmazzonie950ea232015-03-13 21:39:45531 // Add one grandchild frame.
532 RenderFrameHostImpl* child1_rfh = root->child_at(0)->current_frame_host();
Luna Luc3fdacdf2017-11-08 04:48:53533 child1_rfh->OnCreateChildFrame(33, CreateStubInterfaceProviderRequest(),
534 blink::WebTreeScopeType::kDocument,
Lukasz Anforowicz7bfb2e92017-11-22 17:19:45535 std::string(), "uniqueName2", false,
Luna Luc3fdacdf2017-11-08 04:48:53536 base::UnguessableToken::Create(),
537 blink::FramePolicy(), FrameOwnerProperties());
dmazzonie950ea232015-03-13 21:39:45538
naskoaeca57b2015-02-13 00:50:46539 // Ensure they can be found by id.
vishal.b782eb5d2015-04-29 12:22:57540 int id1 = root->child_at(0)->frame_tree_node_id();
541 int id2 = root->child_at(1)->frame_tree_node_id();
542 int id3 = root->child_at(0)->child_at(0)->frame_tree_node_id();
dmazzonie950ea232015-03-13 21:39:45543 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id1));
544 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id2));
545 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id3));
naskoaeca57b2015-02-13 00:50:46546
547 // Crash the renderer.
nick16b07652015-04-18 02:35:31548 main_test_rfh()->GetProcess()->SimulateCrash();
naskoaeca57b2015-02-13 00:50:46549
550 // Ensure they cannot be found by id after the process has crashed.
dmazzonie950ea232015-03-13 21:39:45551 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id1));
552 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id2));
553 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id3));
naskoaeca57b2015-02-13 00:50:46554}
555
[email protected]9b159a52013-10-03 17:24:55556} // namespace content