Fix broken path in extensions/common/PRESUBMIT.py
[chromium-blink-merge.git] / content / browser / frame_host / frame_tree_unittest.cc
blob96f6b4d9466f14b806183d8b548c774d5a350782
1 // 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.
5 #include "content/browser/frame_host/frame_tree.h"
7 #include "base/run_loop.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "content/browser/frame_host/navigator_impl.h"
10 #include "content/browser/frame_host/render_frame_host_factory.h"
11 #include "content/browser/frame_host/render_frame_host_impl.h"
12 #include "content/browser/renderer_host/render_view_host_impl.h"
13 #include "content/browser/web_contents/web_contents_impl.h"
14 #include "content/common/frame_messages.h"
15 #include "content/public/browser/web_contents_observer.h"
16 #include "content/public/test/mock_render_process_host.h"
17 #include "content/public/test/test_browser_context.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "content/test/test_render_frame_host.h"
20 #include "content/test/test_render_view_host.h"
21 #include "content/test/test_web_contents.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 namespace content {
26 namespace {
28 // Appends a description of the structure of the frame tree to |result|.
29 void AppendTreeNodeState(FrameTreeNode* node, std::string* result) {
30 result->append(
31 base::Int64ToString(node->current_frame_host()->GetRoutingID()));
32 if (!node->current_frame_host()->IsRenderFrameLive())
33 result->append("*"); // Asterisk next to dead frames.
35 if (!node->frame_name().empty()) {
36 result->append(" '");
37 result->append(node->frame_name());
38 result->append("'");
40 result->append(": [");
41 const char* separator = "";
42 for (size_t i = 0; i < node->child_count(); i++) {
43 result->append(separator);
44 AppendTreeNodeState(node->child_at(i), result);
45 separator = ", ";
47 result->append("]");
50 // Logs calls to WebContentsObserver along with the state of the frame tree,
51 // for later use in EXPECT_EQ().
52 class TreeWalkingWebContentsLogger : public WebContentsObserver {
53 public:
54 explicit TreeWalkingWebContentsLogger(WebContents* web_contents)
55 : WebContentsObserver(web_contents) {}
57 ~TreeWalkingWebContentsLogger() override {
58 EXPECT_EQ("", log_) << "Activity logged that was not expected";
61 // Gets and resets the log, which is a string of what happened.
62 std::string GetLog() {
63 std::string result = log_;
64 log_.clear();
65 return result;
68 // content::WebContentsObserver implementation.
69 void RenderFrameCreated(RenderFrameHost* render_frame_host) override {
70 LogWhatHappened("RenderFrameCreated", render_frame_host);
73 void RenderFrameHostChanged(RenderFrameHost* old_host,
74 RenderFrameHost* new_host) override {
75 if (old_host)
76 LogWhatHappened("RenderFrameHostChanged(old)", old_host);
77 LogWhatHappened("RenderFrameHostChanged(new)", new_host);
80 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override {
81 LogWhatHappened("RenderFrameDeleted", render_frame_host);
84 void RenderProcessGone(base::TerminationStatus status) override {
85 LogWhatHappened("RenderProcessGone");
88 private:
89 void LogWhatHappened(const std::string& event_name) {
90 if (!log_.empty()) {
91 log_.append("\n");
93 log_.append(event_name + " -> ");
94 AppendTreeNodeState(
95 static_cast<WebContentsImpl*>(web_contents())->GetFrameTree()->root(),
96 &log_);
99 void LogWhatHappened(const std::string& event_name, RenderFrameHost* rfh) {
100 LogWhatHappened(
101 base::StringPrintf("%s(%d)", event_name.c_str(), rfh->GetRoutingID()));
104 std::string log_;
106 DISALLOW_COPY_AND_ASSIGN(TreeWalkingWebContentsLogger);
109 } // namespace
111 class FrameTreeTest : public RenderViewHostImplTestHarness {
112 protected:
113 // Prints a FrameTree, for easy assertions of the tree hierarchy.
114 std::string GetTreeState(FrameTree* frame_tree) {
115 std::string result;
116 AppendTreeNodeState(frame_tree->root(), &result);
117 return result;
121 // Exercise tree manipulation routines.
122 // - Add a series of nodes and verify tree structure.
123 // - Remove a series of nodes and verify tree structure.
125 // TODO(nick): http://crbug.com/444722 Disabled temporarily because of a bad
126 // interaction with the WebContentsObserverConsistencyChecker -- calling
127 // AddFrame directly causes the RFH to not be announced. We either need to
128 // rewrite this test, or be consistent in the layer at which we announce render
129 // frame creation.
130 TEST_F(FrameTreeTest, DISABLED_Shape) {
131 // Use the FrameTree of the WebContents so that it has all the delegates it
132 // needs. We may want to consider a test version of this.
133 FrameTree* frame_tree = contents()->GetFrameTree();
134 FrameTreeNode* root = frame_tree->root();
136 std::string no_children_node("no children node");
137 std::string deep_subtree("node with deep subtree");
138 int process_id = root->current_frame_host()->GetProcess()->GetID();
140 ASSERT_EQ("1: []", GetTreeState(frame_tree));
142 // Simulate attaching a series of frames to build the frame tree.
143 frame_tree->AddFrame(root, process_id, 14, std::string(),
144 SandboxFlags::NONE);
145 frame_tree->AddFrame(root, process_id, 15, std::string(),
146 SandboxFlags::NONE);
147 frame_tree->AddFrame(root, process_id, 16, std::string(),
148 SandboxFlags::NONE);
150 frame_tree->AddFrame(root->child_at(0), process_id, 244, std::string(),
151 SandboxFlags::NONE);
152 frame_tree->AddFrame(root->child_at(1), process_id, 255, no_children_node,
153 SandboxFlags::NONE);
154 frame_tree->AddFrame(root->child_at(0), process_id, 245, std::string(),
155 SandboxFlags::NONE);
157 ASSERT_EQ("1: [14: [244: [], 245: []], "
158 "15: [255 'no children node': []], "
159 "16: []]",
160 GetTreeState(frame_tree));
162 FrameTreeNode* child_16 = root->child_at(2);
163 frame_tree->AddFrame(child_16, process_id, 264, std::string(),
164 SandboxFlags::NONE);
165 frame_tree->AddFrame(child_16, process_id, 265, std::string(),
166 SandboxFlags::NONE);
167 frame_tree->AddFrame(child_16, process_id, 266, std::string(),
168 SandboxFlags::NONE);
169 frame_tree->AddFrame(child_16, process_id, 267, deep_subtree,
170 SandboxFlags::NONE);
171 frame_tree->AddFrame(child_16, process_id, 268, std::string(),
172 SandboxFlags::NONE);
174 FrameTreeNode* child_267 = child_16->child_at(3);
175 frame_tree->AddFrame(child_267, process_id, 365, std::string(),
176 SandboxFlags::NONE);
177 frame_tree->AddFrame(child_267->child_at(0), process_id, 455, std::string(),
178 SandboxFlags::NONE);
179 frame_tree->AddFrame(child_267->child_at(0)->child_at(0), process_id, 555,
180 std::string(), SandboxFlags::NONE);
181 frame_tree->AddFrame(child_267->child_at(0)->child_at(0)->child_at(0),
182 process_id, 655, std::string(), SandboxFlags::NONE);
184 // Now that's it's fully built, verify the tree structure is as expected.
185 ASSERT_EQ("1: [14: [244: [], 245: []], "
186 "15: [255 'no children node': []], "
187 "16: [264: [], 265: [], 266: [], "
188 "267 'node with deep subtree': "
189 "[365: [455: [555: [655: []]]]], 268: []]]",
190 GetTreeState(frame_tree));
192 FrameTreeNode* child_555 = child_267->child_at(0)->child_at(0)->child_at(0);
193 frame_tree->RemoveFrame(child_555);
194 ASSERT_EQ("1: [14: [244: [], 245: []], "
195 "15: [255 'no children node': []], "
196 "16: [264: [], 265: [], 266: [], "
197 "267 'node with deep subtree': "
198 "[365: [455: []]], 268: []]]",
199 GetTreeState(frame_tree));
201 frame_tree->RemoveFrame(child_16->child_at(1));
202 ASSERT_EQ("1: [14: [244: [], 245: []], "
203 "15: [255 'no children node': []], "
204 "16: [264: [], 266: [], "
205 "267 'node with deep subtree': "
206 "[365: [455: []]], 268: []]]",
207 GetTreeState(frame_tree));
209 frame_tree->RemoveFrame(root->child_at(1));
210 ASSERT_EQ("1: [14: [244: [], 245: []], "
211 "16: [264: [], 266: [], "
212 "267 'node with deep subtree': "
213 "[365: [455: []]], 268: []]]",
214 GetTreeState(frame_tree));
217 // Ensure frames can be found by frame_tree_node_id, routing ID, or name.
218 TEST_F(FrameTreeTest, FindFrames) {
219 // Add a few child frames to the main frame.
220 FrameTree* frame_tree = contents()->GetFrameTree();
221 FrameTreeNode* root = frame_tree->root();
222 main_test_rfh()->OnCreateChildFrame(22, "child0", SandboxFlags::NONE);
223 main_test_rfh()->OnCreateChildFrame(23, "child1", SandboxFlags::NONE);
224 main_test_rfh()->OnCreateChildFrame(24, std::string(), SandboxFlags::NONE);
225 FrameTreeNode* child0 = root->child_at(0);
226 FrameTreeNode* child1 = root->child_at(1);
227 FrameTreeNode* child2 = root->child_at(2);
229 // Add one grandchild frame.
230 child1->current_frame_host()->OnCreateChildFrame(33, "grandchild",
231 SandboxFlags::NONE);
232 FrameTreeNode* grandchild = child1->child_at(0);
234 // Ensure they can be found by FTN id.
235 EXPECT_EQ(root, frame_tree->FindByID(root->frame_tree_node_id()));
236 EXPECT_EQ(child0, frame_tree->FindByID(child0->frame_tree_node_id()));
237 EXPECT_EQ(child1, frame_tree->FindByID(child1->frame_tree_node_id()));
238 EXPECT_EQ(child2, frame_tree->FindByID(child2->frame_tree_node_id()));
239 EXPECT_EQ(grandchild, frame_tree->FindByID(grandchild->frame_tree_node_id()));
240 EXPECT_EQ(nullptr, frame_tree->FindByID(-1));
242 // Ensure they can be found by routing id.
243 int process_id = main_test_rfh()->GetProcess()->GetID();
244 EXPECT_EQ(root, frame_tree->FindByRoutingID(process_id,
245 main_test_rfh()->GetRoutingID()));
246 EXPECT_EQ(child0, frame_tree->FindByRoutingID(process_id, 22));
247 EXPECT_EQ(child1, frame_tree->FindByRoutingID(process_id, 23));
248 EXPECT_EQ(child2, frame_tree->FindByRoutingID(process_id, 24));
249 EXPECT_EQ(grandchild, frame_tree->FindByRoutingID(process_id, 33));
250 EXPECT_EQ(nullptr, frame_tree->FindByRoutingID(process_id, 37));
252 // Ensure they can be found by name, if they have one.
253 EXPECT_EQ(root, frame_tree->FindByName(std::string()));
254 EXPECT_EQ(child0, frame_tree->FindByName("child0"));
255 EXPECT_EQ(child1, frame_tree->FindByName("child1"));
256 EXPECT_EQ(grandchild, frame_tree->FindByName("grandchild"));
257 EXPECT_EQ(nullptr, frame_tree->FindByName("no such frame"));
260 // Check that PreviousSibling() is retrieved correctly.
261 TEST_F(FrameTreeTest, PreviousSibling) {
262 // Add a few child frames to the main frame.
263 FrameTree* frame_tree = contents()->GetFrameTree();
264 FrameTreeNode* root = frame_tree->root();
265 main_test_rfh()->OnCreateChildFrame(22, "child0", SandboxFlags::NONE);
266 main_test_rfh()->OnCreateChildFrame(23, "child1", SandboxFlags::NONE);
267 main_test_rfh()->OnCreateChildFrame(24, "child2", SandboxFlags::NONE);
268 FrameTreeNode* child0 = root->child_at(0);
269 FrameTreeNode* child1 = root->child_at(1);
270 FrameTreeNode* child2 = root->child_at(2);
272 // Add one grandchild frame.
273 child1->current_frame_host()->OnCreateChildFrame(33, "grandchild",
274 SandboxFlags::NONE);
275 FrameTreeNode* grandchild = child1->child_at(0);
277 EXPECT_EQ(nullptr, root->PreviousSibling());
278 EXPECT_EQ(nullptr, child0->PreviousSibling());
279 EXPECT_EQ(child0, child1->PreviousSibling());
280 EXPECT_EQ(child1, child2->PreviousSibling());
281 EXPECT_EQ(nullptr, grandchild->PreviousSibling());
284 // Do some simple manipulations of the frame tree, making sure that
285 // WebContentsObservers see a consistent view of the tree as we go.
286 TEST_F(FrameTreeTest, ObserverWalksTreeDuringFrameCreation) {
287 TreeWalkingWebContentsLogger activity(contents());
288 contents()->NavigateAndCommit(GURL("http://www.google.com"));
289 EXPECT_EQ("", activity.GetLog());
291 FrameTree* frame_tree = contents()->GetFrameTree();
292 FrameTreeNode* root = frame_tree->root();
294 // Simulate attaching a series of frames to build the frame tree.
295 main_test_rfh()->OnCreateChildFrame(14, std::string(), SandboxFlags::NONE);
296 EXPECT_EQ(
297 "RenderFrameHostChanged(new)(14) -> 1: []\n"
298 "RenderFrameCreated(14) -> 1: [14: []]",
299 activity.GetLog());
300 main_test_rfh()->OnCreateChildFrame(18, std::string(), SandboxFlags::NONE);
301 EXPECT_EQ(
302 "RenderFrameHostChanged(new)(18) -> 1: [14: []]\n"
303 "RenderFrameCreated(18) -> 1: [14: [], 18: []]",
304 activity.GetLog());
305 frame_tree->RemoveFrame(root->child_at(0));
306 EXPECT_EQ("RenderFrameDeleted(14) -> 1: [18: []]", activity.GetLog());
307 frame_tree->RemoveFrame(root->child_at(0));
308 EXPECT_EQ("RenderFrameDeleted(18) -> 1: []", activity.GetLog());
311 // Make sure that WebContentsObservers see a consistent view of the tree after
312 // recovery from a render process crash.
313 TEST_F(FrameTreeTest, ObserverWalksTreeAfterCrash) {
314 TreeWalkingWebContentsLogger activity(contents());
315 contents()->NavigateAndCommit(GURL("http://www.google.com"));
316 EXPECT_EQ("", activity.GetLog());
318 main_test_rfh()->OnCreateChildFrame(22, std::string(), SandboxFlags::NONE);
319 EXPECT_EQ(
320 "RenderFrameHostChanged(new)(22) -> 1: []\n"
321 "RenderFrameCreated(22) -> 1: [22: []]",
322 activity.GetLog());
323 main_test_rfh()->OnCreateChildFrame(23, std::string(), SandboxFlags::NONE);
324 EXPECT_EQ(
325 "RenderFrameHostChanged(new)(23) -> 1: [22: []]\n"
326 "RenderFrameCreated(23) -> 1: [22: [], 23: []]",
327 activity.GetLog());
329 // Crash the renderer
330 main_test_rfh()->GetProcess()->SimulateCrash();
331 EXPECT_EQ(
332 "RenderFrameDeleted(23) -> 1: [22: [], 23*: []]\n"
333 "RenderFrameDeleted(22) -> 1: [22*: [], 23*: []]\n"
334 "RenderFrameDeleted(1) -> 1: []\n" // TODO(nick): Should be "1*:"
335 "RenderProcessGone -> 1*: []",
336 activity.GetLog());
339 // Ensure that frames are not added to the tree, if the process passed in
340 // is different than the process of the parent node.
341 TEST_F(FrameTreeTest, FailAddFrameWithWrongProcessId) {
342 contents()->NavigateAndCommit(GURL("http://www.google.com"));
343 FrameTree* frame_tree = contents()->GetFrameTree();
344 FrameTreeNode* root = frame_tree->root();
345 int process_id = root->current_frame_host()->GetProcess()->GetID();
347 ASSERT_EQ("1: []", GetTreeState(frame_tree));
349 // Simulate attaching a frame from mismatched process id.
350 ASSERT_FALSE(frame_tree->AddFrame(root, process_id + 1, 1, std::string(),
351 SandboxFlags::NONE));
352 ASSERT_EQ("1: []", GetTreeState(frame_tree));
355 // Ensure that frames removed while a process has crashed are not preserved in
356 // the global map of id->frame.
357 TEST_F(FrameTreeTest, ProcessCrashClearsGlobalMap) {
358 // Add a couple child frames to the main frame.
359 FrameTreeNode* root = contents()->GetFrameTree()->root();
361 main_test_rfh()->OnCreateChildFrame(22, std::string(), SandboxFlags::NONE);
362 main_test_rfh()->OnCreateChildFrame(23, std::string(), SandboxFlags::NONE);
364 // Add one grandchild frame.
365 RenderFrameHostImpl* child1_rfh = root->child_at(0)->current_frame_host();
366 child1_rfh->OnCreateChildFrame(33, std::string(), SandboxFlags::NONE);
368 // Ensure they can be found by id.
369 int id1 = root->child_at(0)->frame_tree_node_id();
370 int id2 = root->child_at(1)->frame_tree_node_id();
371 int id3 = root->child_at(0)->child_at(0)->frame_tree_node_id();
372 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id1));
373 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id2));
374 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id3));
376 // Crash the renderer.
377 main_test_rfh()->GetProcess()->SimulateCrash();
379 // Ensure they cannot be found by id after the process has crashed.
380 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id1));
381 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id2));
382 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id3));
385 } // namespace content