Pass FrameTreeNode (not RenderFrameHost) to NavigateToEntry.
[chromium-blink-merge.git] / content / browser / frame_host / frame_tree_unittest.cc
blob8fdcf71e3fc9dcfade31a24deca0ffe5cf78a03a
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->frame_name().empty()) {
33 result->append(" '");
34 result->append(node->frame_name());
35 result->append("'");
37 result->append(": [");
38 const char* separator = "";
39 for (size_t i = 0; i < node->child_count(); i++) {
40 result->append(separator);
41 AppendTreeNodeState(node->child_at(i), result);
42 separator = ", ";
44 result->append("]");
47 // Logs calls to WebContentsObserver along with the state of the frame tree,
48 // for later use in EXPECT_EQ().
49 class TreeWalkingWebContentsLogger : public WebContentsObserver {
50 public:
51 explicit TreeWalkingWebContentsLogger(WebContents* web_contents)
52 : WebContentsObserver(web_contents) {}
54 ~TreeWalkingWebContentsLogger() override {
55 EXPECT_EQ("", log_) << "Activity logged that was not expected";
58 // Gets and resets the log, which is a string of what happened.
59 std::string GetLog() {
60 std::string result = log_;
61 log_.clear();
62 return result;
65 // content::WebContentsObserver implementation.
66 void RenderFrameCreated(RenderFrameHost* render_frame_host) override {
67 LogWhatHappened("RenderFrameCreated", render_frame_host);
70 void RenderFrameHostChanged(RenderFrameHost* old_host,
71 RenderFrameHost* new_host) override {
72 if (old_host)
73 LogWhatHappened("RenderFrameChanged(old)", old_host);
74 LogWhatHappened("RenderFrameChanged(new)", new_host);
77 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override {
78 LogWhatHappened("RenderFrameDeleted", render_frame_host);
81 void RenderProcessGone(base::TerminationStatus status) override {
82 LogWhatHappened("RenderProcessGone");
85 private:
86 void LogWhatHappened(const std::string& event_name) {
87 if (!log_.empty()) {
88 log_.append("\n");
90 log_.append(event_name + " -> ");
91 AppendTreeNodeState(
92 static_cast<WebContentsImpl*>(web_contents())->GetFrameTree()->root(),
93 &log_);
96 void LogWhatHappened(const std::string& event_name, RenderFrameHost* rfh) {
97 LogWhatHappened(
98 base::StringPrintf("%s(%d)", event_name.c_str(), rfh->GetRoutingID()));
101 std::string log_;
103 DISALLOW_COPY_AND_ASSIGN(TreeWalkingWebContentsLogger);
106 } // namespace
108 class FrameTreeTest : public RenderViewHostImplTestHarness {
109 protected:
110 // Prints a FrameTree, for easy assertions of the tree hierarchy.
111 std::string GetTreeState(FrameTree* frame_tree) {
112 std::string result;
113 AppendTreeNodeState(frame_tree->root(), &result);
114 return result;
118 // Exercise tree manipulation routines.
119 // - Add a series of nodes and verify tree structure.
120 // - Remove a series of nodes and verify tree structure.
122 // TODO(nick): http://crbug.com/444722 Disabled temporarily because of a bad
123 // interaction with the WebContentsObserverConsistencyChecker -- calling
124 // AddFrame directly causes the RFH to not be announced. We either need to
125 // rewrite this test, or be consistent in the layer at which we announce render
126 // frame creation.
127 TEST_F(FrameTreeTest, DISABLED_Shape) {
128 // Use the FrameTree of the WebContents so that it has all the delegates it
129 // needs. We may want to consider a test version of this.
130 FrameTree* frame_tree = contents()->GetFrameTree();
131 FrameTreeNode* root = frame_tree->root();
133 std::string no_children_node("no children node");
134 std::string deep_subtree("node with deep subtree");
135 int process_id = root->current_frame_host()->GetProcess()->GetID();
137 ASSERT_EQ("1: []", GetTreeState(frame_tree));
139 // Simulate attaching a series of frames to build the frame tree.
140 frame_tree->AddFrame(root, process_id, 14, std::string());
141 frame_tree->AddFrame(root, process_id, 15, std::string());
142 frame_tree->AddFrame(root, process_id, 16, std::string());
144 frame_tree->AddFrame(root->child_at(0), process_id, 244, std::string());
145 frame_tree->AddFrame(root->child_at(1), process_id, 255, no_children_node);
146 frame_tree->AddFrame(root->child_at(0), process_id, 245, std::string());
148 ASSERT_EQ("1: [14: [244: [], 245: []], "
149 "15: [255 'no children node': []], "
150 "16: []]",
151 GetTreeState(frame_tree));
153 FrameTreeNode* child_16 = root->child_at(2);
154 frame_tree->AddFrame(child_16, process_id, 264, std::string());
155 frame_tree->AddFrame(child_16, process_id, 265, std::string());
156 frame_tree->AddFrame(child_16, process_id, 266, std::string());
157 frame_tree->AddFrame(child_16, process_id, 267, deep_subtree);
158 frame_tree->AddFrame(child_16, process_id, 268, std::string());
160 FrameTreeNode* child_267 = child_16->child_at(3);
161 frame_tree->AddFrame(child_267, process_id, 365, std::string());
162 frame_tree->AddFrame(child_267->child_at(0), process_id, 455, std::string());
163 frame_tree->AddFrame(child_267->child_at(0)->child_at(0), process_id, 555,
164 std::string());
165 frame_tree->AddFrame(child_267->child_at(0)->child_at(0)->child_at(0),
166 process_id, 655, std::string());
168 // Now that's it's fully built, verify the tree structure is as expected.
169 ASSERT_EQ("1: [14: [244: [], 245: []], "
170 "15: [255 'no children node': []], "
171 "16: [264: [], 265: [], 266: [], "
172 "267 'node with deep subtree': "
173 "[365: [455: [555: [655: []]]]], 268: []]]",
174 GetTreeState(frame_tree));
176 FrameTreeNode* child_555 = child_267->child_at(0)->child_at(0)->child_at(0);
177 frame_tree->RemoveFrame(child_555);
178 ASSERT_EQ("1: [14: [244: [], 245: []], "
179 "15: [255 'no children node': []], "
180 "16: [264: [], 265: [], 266: [], "
181 "267 'node with deep subtree': "
182 "[365: [455: []]], 268: []]]",
183 GetTreeState(frame_tree));
185 frame_tree->RemoveFrame(child_16->child_at(1));
186 ASSERT_EQ("1: [14: [244: [], 245: []], "
187 "15: [255 'no children node': []], "
188 "16: [264: [], 266: [], "
189 "267 'node with deep subtree': "
190 "[365: [455: []]], 268: []]]",
191 GetTreeState(frame_tree));
193 frame_tree->RemoveFrame(root->child_at(1));
194 ASSERT_EQ("1: [14: [244: [], 245: []], "
195 "16: [264: [], 266: [], "
196 "267 'node with deep subtree': "
197 "[365: [455: []]], 268: []]]",
198 GetTreeState(frame_tree));
201 // Do some simple manipulations of the frame tree, making sure that
202 // WebContentsObservers see a consistent view of the tree as we go.
203 TEST_F(FrameTreeTest, ObserverWalksTreeDuringFrameCreation) {
204 TreeWalkingWebContentsLogger activity(contents());
205 FrameTree* frame_tree = contents()->GetFrameTree();
206 FrameTreeNode* root = frame_tree->root();
208 EXPECT_EQ("", activity.GetLog());
210 // Simulate attaching a series of frames to build the frame tree.
211 main_test_rfh()->OnCreateChildFrame(14, std::string(), SandboxFlags::NONE);
212 EXPECT_EQ("RenderFrameCreated(14) -> 1: [14: []]", activity.GetLog());
213 main_test_rfh()->OnCreateChildFrame(18, std::string(), SandboxFlags::NONE);
214 EXPECT_EQ("RenderFrameCreated(18) -> 1: [14: [], 18: []]", activity.GetLog());
215 frame_tree->RemoveFrame(root->child_at(0));
216 EXPECT_EQ("RenderFrameDeleted(14) -> 1: [18: []]", activity.GetLog());
217 frame_tree->RemoveFrame(root->child_at(0));
218 EXPECT_EQ("RenderFrameDeleted(18) -> 1: []", activity.GetLog());
221 // Make sure that WebContentsObservers see a consistent view of the tree after
222 // recovery from a render process crash.
223 TEST_F(FrameTreeTest, ObserverWalksTreeAfterCrash) {
224 TreeWalkingWebContentsLogger activity(contents());
226 main_test_rfh()->OnCreateChildFrame(22, std::string(), SandboxFlags::NONE);
227 EXPECT_EQ("RenderFrameCreated(22) -> 1: [22: []]", activity.GetLog());
228 main_test_rfh()->OnCreateChildFrame(23, std::string(), SandboxFlags::NONE);
229 EXPECT_EQ("RenderFrameCreated(23) -> 1: [22: [], 23: []]", activity.GetLog());
231 // Crash the renderer
232 main_rfh()->OnMessageReceived(FrameHostMsg_RenderProcessGone(
233 0, base::TERMINATION_STATUS_PROCESS_CRASHED, -1));
234 EXPECT_EQ(
235 "RenderFrameDeleted(22) -> 1: []\n"
236 "RenderFrameDeleted(23) -> 1: []\n"
237 "RenderProcessGone -> 1: []",
238 activity.GetLog());
241 // Ensure that frames are not added to the tree, if the process passed in
242 // is different than the process of the parent node.
243 TEST_F(FrameTreeTest, FailAddFrameWithWrongProcessId) {
244 FrameTree* frame_tree = contents()->GetFrameTree();
245 FrameTreeNode* root = frame_tree->root();
246 int process_id = root->current_frame_host()->GetProcess()->GetID();
248 ASSERT_EQ("1: []", GetTreeState(frame_tree));
250 // Simulate attaching a frame from mismatched process id.
251 ASSERT_FALSE(frame_tree->AddFrame(root, process_id + 1, 1, std::string()));
252 ASSERT_EQ("1: []", GetTreeState(frame_tree));
255 // Ensure that frames removed while a process has crashed are not preserved in
256 // the global map of id->frame.
257 TEST_F(FrameTreeTest, ProcessCrashClearsGlobalMap) {
258 // Add a couple child frames to the main frame.
259 FrameTreeNode* root = contents()->GetFrameTree()->root();
261 main_test_rfh()->OnCreateChildFrame(22, std::string(), SandboxFlags::NONE);
262 main_test_rfh()->OnCreateChildFrame(23, std::string(), SandboxFlags::NONE);
264 // Ensure they can be found by id.
265 int64 id1 = root->child_at(0)->frame_tree_node_id();
266 int64 id2 = root->child_at(1)->frame_tree_node_id();
267 EXPECT_TRUE(FrameTree::GloballyFindByID(id1));
268 EXPECT_TRUE(FrameTree::GloballyFindByID(id2));
270 // Crash the renderer.
271 main_test_rfh()->OnMessageReceived(FrameHostMsg_RenderProcessGone(
272 0, base::TERMINATION_STATUS_PROCESS_CRASHED, -1));
274 // Ensure they cannot be found by id after the process has crashed.
275 EXPECT_FALSE(FrameTree::GloballyFindByID(id1));
276 EXPECT_FALSE(FrameTree::GloballyFindByID(id2));
279 } // namespace content