Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / browser / frame_host / frame_tree_unittest.cc
blobf24814143326186cf83cf798ab70875c318a4bfd
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 frame_tree->AddFrame(root, process_id, 15, std::string());
145 frame_tree->AddFrame(root, process_id, 16, std::string());
147 frame_tree->AddFrame(root->child_at(0), process_id, 244, std::string());
148 frame_tree->AddFrame(root->child_at(1), process_id, 255, no_children_node);
149 frame_tree->AddFrame(root->child_at(0), process_id, 245, std::string());
151 ASSERT_EQ("1: [14: [244: [], 245: []], "
152 "15: [255 'no children node': []], "
153 "16: []]",
154 GetTreeState(frame_tree));
156 FrameTreeNode* child_16 = root->child_at(2);
157 frame_tree->AddFrame(child_16, process_id, 264, std::string());
158 frame_tree->AddFrame(child_16, process_id, 265, std::string());
159 frame_tree->AddFrame(child_16, process_id, 266, std::string());
160 frame_tree->AddFrame(child_16, process_id, 267, deep_subtree);
161 frame_tree->AddFrame(child_16, process_id, 268, std::string());
163 FrameTreeNode* child_267 = child_16->child_at(3);
164 frame_tree->AddFrame(child_267, process_id, 365, std::string());
165 frame_tree->AddFrame(child_267->child_at(0), process_id, 455, std::string());
166 frame_tree->AddFrame(child_267->child_at(0)->child_at(0), process_id, 555,
167 std::string());
168 frame_tree->AddFrame(child_267->child_at(0)->child_at(0)->child_at(0),
169 process_id, 655, std::string());
171 // Now that's it's fully built, verify the tree structure is as expected.
172 ASSERT_EQ("1: [14: [244: [], 245: []], "
173 "15: [255 'no children node': []], "
174 "16: [264: [], 265: [], 266: [], "
175 "267 'node with deep subtree': "
176 "[365: [455: [555: [655: []]]]], 268: []]]",
177 GetTreeState(frame_tree));
179 FrameTreeNode* child_555 = child_267->child_at(0)->child_at(0)->child_at(0);
180 frame_tree->RemoveFrame(child_555);
181 ASSERT_EQ("1: [14: [244: [], 245: []], "
182 "15: [255 'no children node': []], "
183 "16: [264: [], 265: [], 266: [], "
184 "267 'node with deep subtree': "
185 "[365: [455: []]], 268: []]]",
186 GetTreeState(frame_tree));
188 frame_tree->RemoveFrame(child_16->child_at(1));
189 ASSERT_EQ("1: [14: [244: [], 245: []], "
190 "15: [255 'no children node': []], "
191 "16: [264: [], 266: [], "
192 "267 'node with deep subtree': "
193 "[365: [455: []]], 268: []]]",
194 GetTreeState(frame_tree));
196 frame_tree->RemoveFrame(root->child_at(1));
197 ASSERT_EQ("1: [14: [244: [], 245: []], "
198 "16: [264: [], 266: [], "
199 "267 'node with deep subtree': "
200 "[365: [455: []]], 268: []]]",
201 GetTreeState(frame_tree));
204 // Do some simple manipulations of the frame tree, making sure that
205 // WebContentsObservers see a consistent view of the tree as we go.
206 TEST_F(FrameTreeTest, ObserverWalksTreeDuringFrameCreation) {
207 TreeWalkingWebContentsLogger activity(contents());
208 contents()->NavigateAndCommit(GURL("http://www.google.com"));
209 EXPECT_EQ("", activity.GetLog());
211 FrameTree* frame_tree = contents()->GetFrameTree();
212 FrameTreeNode* root = frame_tree->root();
214 // Simulate attaching a series of frames to build the frame tree.
215 main_test_rfh()->OnCreateChildFrame(14, std::string(), SandboxFlags::NONE);
216 EXPECT_EQ(
217 "RenderFrameHostChanged(new)(14) -> 1: []\n"
218 "RenderFrameCreated(14) -> 1: [14: []]",
219 activity.GetLog());
220 main_test_rfh()->OnCreateChildFrame(18, std::string(), SandboxFlags::NONE);
221 EXPECT_EQ(
222 "RenderFrameHostChanged(new)(18) -> 1: [14: []]\n"
223 "RenderFrameCreated(18) -> 1: [14: [], 18: []]",
224 activity.GetLog());
225 frame_tree->RemoveFrame(root->child_at(0));
226 EXPECT_EQ("RenderFrameDeleted(14) -> 1: [18: []]", activity.GetLog());
227 frame_tree->RemoveFrame(root->child_at(0));
228 EXPECT_EQ("RenderFrameDeleted(18) -> 1: []", activity.GetLog());
231 // Make sure that WebContentsObservers see a consistent view of the tree after
232 // recovery from a render process crash.
233 TEST_F(FrameTreeTest, ObserverWalksTreeAfterCrash) {
234 TreeWalkingWebContentsLogger activity(contents());
235 contents()->NavigateAndCommit(GURL("http://www.google.com"));
236 EXPECT_EQ("", activity.GetLog());
238 main_test_rfh()->OnCreateChildFrame(22, std::string(), SandboxFlags::NONE);
239 EXPECT_EQ(
240 "RenderFrameHostChanged(new)(22) -> 1: []\n"
241 "RenderFrameCreated(22) -> 1: [22: []]",
242 activity.GetLog());
243 main_test_rfh()->OnCreateChildFrame(23, std::string(), SandboxFlags::NONE);
244 EXPECT_EQ(
245 "RenderFrameHostChanged(new)(23) -> 1: [22: []]\n"
246 "RenderFrameCreated(23) -> 1: [22: [], 23: []]",
247 activity.GetLog());
249 // Crash the renderer
250 main_test_rfh()->GetProcess()->SimulateCrash();
251 EXPECT_EQ(
252 "RenderFrameDeleted(23) -> 1: [22: [], 23*: []]\n"
253 "RenderFrameDeleted(22) -> 1: [22*: [], 23*: []]\n"
254 "RenderFrameDeleted(1) -> 1: []\n" // TODO(nick): Should be "1*:"
255 "RenderProcessGone -> 1*: []",
256 activity.GetLog());
259 // Ensure that frames are not added to the tree, if the process passed in
260 // is different than the process of the parent node.
261 TEST_F(FrameTreeTest, FailAddFrameWithWrongProcessId) {
262 contents()->NavigateAndCommit(GURL("http://www.google.com"));
263 FrameTree* frame_tree = contents()->GetFrameTree();
264 FrameTreeNode* root = frame_tree->root();
265 int process_id = root->current_frame_host()->GetProcess()->GetID();
267 ASSERT_EQ("1: []", GetTreeState(frame_tree));
269 // Simulate attaching a frame from mismatched process id.
270 ASSERT_FALSE(frame_tree->AddFrame(root, process_id + 1, 1, std::string()));
271 ASSERT_EQ("1: []", GetTreeState(frame_tree));
274 // Ensure that frames removed while a process has crashed are not preserved in
275 // the global map of id->frame.
276 TEST_F(FrameTreeTest, ProcessCrashClearsGlobalMap) {
277 // Add a couple child frames to the main frame.
278 FrameTreeNode* root = contents()->GetFrameTree()->root();
280 main_test_rfh()->OnCreateChildFrame(22, std::string(), SandboxFlags::NONE);
281 main_test_rfh()->OnCreateChildFrame(23, std::string(), SandboxFlags::NONE);
283 // Add one grandchild frame.
284 RenderFrameHostImpl* child1_rfh = root->child_at(0)->current_frame_host();
285 child1_rfh->OnCreateChildFrame(33, std::string(), SandboxFlags::NONE);
287 // Ensure they can be found by id.
288 int64 id1 = root->child_at(0)->frame_tree_node_id();
289 int64 id2 = root->child_at(1)->frame_tree_node_id();
290 int64 id3 = root->child_at(0)->child_at(0)->frame_tree_node_id();
291 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id1));
292 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id2));
293 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id3));
295 // Crash the renderer.
296 main_test_rfh()->GetProcess()->SimulateCrash();
298 // Ensure they cannot be found by id after the process has crashed.
299 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id1));
300 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id2));
301 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id3));
304 } // namespace content