Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / frame_host / frame_tree_unittest.cc
blob7cf3428a547a1bb2c2efeb879a8cbf97044701a3
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("RenderFrameHostChanged(old)", old_host);
74 LogWhatHappened("RenderFrameHostChanged(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(
213 "RenderFrameHostChanged(new)(14) -> 1: []\n"
214 "RenderFrameCreated(14) -> 1: [14: []]",
215 activity.GetLog());
216 main_test_rfh()->OnCreateChildFrame(18, std::string(), SandboxFlags::NONE);
217 EXPECT_EQ(
218 "RenderFrameHostChanged(new)(18) -> 1: [14: []]\n"
219 "RenderFrameCreated(18) -> 1: [14: [], 18: []]",
220 activity.GetLog());
221 frame_tree->RemoveFrame(root->child_at(0));
222 EXPECT_EQ("RenderFrameDeleted(14) -> 1: [18: []]", activity.GetLog());
223 frame_tree->RemoveFrame(root->child_at(0));
224 EXPECT_EQ("RenderFrameDeleted(18) -> 1: []", activity.GetLog());
227 // Make sure that WebContentsObservers see a consistent view of the tree after
228 // recovery from a render process crash.
229 TEST_F(FrameTreeTest, ObserverWalksTreeAfterCrash) {
230 TreeWalkingWebContentsLogger activity(contents());
232 main_test_rfh()->OnCreateChildFrame(22, std::string(), SandboxFlags::NONE);
233 EXPECT_EQ(
234 "RenderFrameHostChanged(new)(22) -> 1: []\n"
235 "RenderFrameCreated(22) -> 1: [22: []]",
236 activity.GetLog());
237 main_test_rfh()->OnCreateChildFrame(23, std::string(), SandboxFlags::NONE);
238 EXPECT_EQ(
239 "RenderFrameHostChanged(new)(23) -> 1: [22: []]\n"
240 "RenderFrameCreated(23) -> 1: [22: [], 23: []]",
241 activity.GetLog());
243 // Crash the renderer
244 main_test_rfh()->OnMessageReceived(FrameHostMsg_RenderProcessGone(
245 main_test_rfh()->GetRoutingID(), base::TERMINATION_STATUS_PROCESS_CRASHED,
246 -1));
247 EXPECT_EQ(
248 "RenderFrameDeleted(22) -> 1: []\n"
249 "RenderFrameDeleted(23) -> 1: []\n"
250 "RenderFrameDeleted(1) -> 1: []\n"
251 "RenderProcessGone -> 1: []",
252 activity.GetLog());
255 // Ensure that frames are not added to the tree, if the process passed in
256 // is different than the process of the parent node.
257 TEST_F(FrameTreeTest, FailAddFrameWithWrongProcessId) {
258 FrameTree* frame_tree = contents()->GetFrameTree();
259 FrameTreeNode* root = frame_tree->root();
260 int process_id = root->current_frame_host()->GetProcess()->GetID();
262 ASSERT_EQ("1: []", GetTreeState(frame_tree));
264 // Simulate attaching a frame from mismatched process id.
265 ASSERT_FALSE(frame_tree->AddFrame(root, process_id + 1, 1, std::string()));
266 ASSERT_EQ("1: []", GetTreeState(frame_tree));
269 // Ensure that frames removed while a process has crashed are not preserved in
270 // the global map of id->frame.
271 TEST_F(FrameTreeTest, ProcessCrashClearsGlobalMap) {
272 // Add a couple child frames to the main frame.
273 FrameTreeNode* root = contents()->GetFrameTree()->root();
275 main_test_rfh()->OnCreateChildFrame(22, std::string(), SandboxFlags::NONE);
276 main_test_rfh()->OnCreateChildFrame(23, std::string(), SandboxFlags::NONE);
278 // Add one grandchild frame.
279 RenderFrameHostImpl* child1_rfh = root->child_at(0)->current_frame_host();
280 child1_rfh->OnCreateChildFrame(33, std::string(), SandboxFlags::NONE);
282 // Ensure they can be found by id.
283 int64 id1 = root->child_at(0)->frame_tree_node_id();
284 int64 id2 = root->child_at(1)->frame_tree_node_id();
285 int64 id3 = root->child_at(0)->child_at(0)->frame_tree_node_id();
286 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id1));
287 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id2));
288 EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id3));
290 // Crash the renderer.
291 main_test_rfh()->OnMessageReceived(FrameHostMsg_RenderProcessGone(
292 0, base::TERMINATION_STATUS_PROCESS_CRASHED, -1));
294 // Ensure they cannot be found by id after the process has crashed.
295 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id1));
296 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id2));
297 EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id3));
300 } // namespace content