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/public/test/mock_render_process_host.h"
15 #include "content/public/test/test_browser_context.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "content/public/test/test_renderer_host.h"
18 #include "testing/gtest/include/gtest/gtest.h"
23 class FrameTreeTest
: public RenderViewHostTestHarness
{
25 // Prints a FrameTree, for easy assertions of the tree hierarchy.
26 std::string
GetTreeState(FrameTree
* frame_tree
) {
28 AppendTreeNodeState(frame_tree
->root(), &result
);
33 void AppendTreeNodeState(FrameTreeNode
* node
, std::string
* result
) {
34 result
->append(base::Int64ToString(node
->frame_id()));
35 if (!node
->frame_name().empty()) {
37 result
->append(node
->frame_name());
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
);
51 // Test that swapping the main frame resets the renderer-assigned frame id.
52 // - On creation, frame id is unassigned.
53 // - After a swap, frame id is unassigned.
54 TEST_F(FrameTreeTest
, FirstNavigationAfterSwap
) {
55 FrameTree
frame_tree(new NavigatorImpl(NULL
, NULL
), NULL
, NULL
, NULL
, NULL
);
57 EXPECT_TRUE(frame_tree
.IsFirstNavigationAfterSwap());
58 EXPECT_EQ(FrameTreeNode::kInvalidFrameId
,
59 frame_tree
.root()->frame_id());
60 frame_tree
.OnFirstNavigationAfterSwap(1);
61 EXPECT_FALSE(frame_tree
.IsFirstNavigationAfterSwap());
62 EXPECT_EQ(1, frame_tree
.root()->frame_id());
64 frame_tree
.ResetForMainFrameSwap();
65 EXPECT_TRUE(frame_tree
.IsFirstNavigationAfterSwap());
66 EXPECT_EQ(FrameTreeNode::kInvalidFrameId
,
67 frame_tree
.root()->frame_id());
70 // Exercise tree manipulation routines.
71 // - Add a series of nodes and verify tree structure.
72 // - Remove a series of nodes and verify tree structure.
73 TEST_F(FrameTreeTest
, Shape
) {
74 // Use the FrameTree of the WebContents so that it has all the delegates it
75 // needs. We may want to consider a test version of this.
76 FrameTree
* frame_tree
=
77 static_cast<WebContentsImpl
*>(web_contents())->GetFrameTree();
79 std::string
no_children_node("no children node");
80 std::string
deep_subtree("node with deep subtree");
82 frame_tree
->OnFirstNavigationAfterSwap(5);
84 ASSERT_EQ("5: []", GetTreeState(frame_tree
));
86 // Simulate attaching a series of frames to build the frame tree.
87 frame_tree
->AddFrame(process()->GetNextRoutingID(), 5, 14, std::string());
88 frame_tree
->AddFrame(process()->GetNextRoutingID(), 5, 15, std::string());
89 frame_tree
->AddFrame(process()->GetNextRoutingID(), 5, 16, std::string());
91 frame_tree
->AddFrame(process()->GetNextRoutingID(), 14, 244, std::string());
92 frame_tree
->AddFrame(process()->GetNextRoutingID(), 15, 255,
94 frame_tree
->AddFrame(process()->GetNextRoutingID(), 14, 245, std::string());
96 ASSERT_EQ("5: [14: [244: [], 245: []], "
97 "15: [255 'no children node': []], "
99 GetTreeState(frame_tree
));
101 frame_tree
->AddFrame(process()->GetNextRoutingID(), 16, 264, std::string());
102 frame_tree
->AddFrame(process()->GetNextRoutingID(), 16, 265, std::string());
103 frame_tree
->AddFrame(process()->GetNextRoutingID(), 16, 266, std::string());
104 frame_tree
->AddFrame(process()->GetNextRoutingID(), 16, 267, deep_subtree
);
105 frame_tree
->AddFrame(process()->GetNextRoutingID(), 16, 268, std::string());
107 frame_tree
->AddFrame(process()->GetNextRoutingID(), 267, 365, std::string());
108 frame_tree
->AddFrame(process()->GetNextRoutingID(), 365, 455, std::string());
109 frame_tree
->AddFrame(process()->GetNextRoutingID(), 455, 555, std::string());
110 frame_tree
->AddFrame(process()->GetNextRoutingID(), 555, 655, std::string());
112 // Now that's it's fully built, verify the tree structure is as expected.
113 ASSERT_EQ("5: [14: [244: [], 245: []], "
114 "15: [255 'no children node': []], "
115 "16: [264: [], 265: [], 266: [], "
116 "267 'node with deep subtree': "
117 "[365: [455: [555: [655: []]]]], 268: []]]",
118 GetTreeState(frame_tree
));
120 // Test removing of nodes. Clear the frame removal listener so we can pass a
122 frame_tree
->ClearFrameRemoveListenerForTesting();
123 frame_tree
->RemoveFrame(NULL
, 555, 655);
124 ASSERT_EQ("5: [14: [244: [], 245: []], "
125 "15: [255 'no children node': []], "
126 "16: [264: [], 265: [], 266: [], "
127 "267 'node with deep subtree': "
128 "[365: [455: [555: []]]], 268: []]]",
129 GetTreeState(frame_tree
));
131 frame_tree
->RemoveFrame(NULL
, 16, 265);
132 ASSERT_EQ("5: [14: [244: [], 245: []], "
133 "15: [255 'no children node': []], "
134 "16: [264: [], 266: [], "
135 "267 'node with deep subtree': "
136 "[365: [455: [555: []]]], 268: []]]",
137 GetTreeState(frame_tree
));
139 frame_tree
->RemoveFrame(NULL
, 5, 15);
140 ASSERT_EQ("5: [14: [244: [], 245: []], "
141 "16: [264: [], 266: [], "
142 "267 'node with deep subtree': "
143 "[365: [455: [555: []]]], 268: []]]",
144 GetTreeState(frame_tree
));
148 } // namespace content