[Ozone-Gbm] Explicitly crash if trying software rendering on GBM
[chromium-blink-merge.git] / content / browser / frame_host / frame_tree_unittest.cc
blobad4fd7567fbec6dcab38dbb2c7f30607ebf26ce5
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 // TODO(nasko): Re-enable this logging once RenderFrameHostChanged observer
73 // methods are fixed. See https://crbug.com/450799.
75 if (old_host)
76 LogWhatHappened("RenderFrameChanged(old)", old_host);
77 LogWhatHappened("RenderFrameChanged(new)", new_host);
81 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override {
82 LogWhatHappened("RenderFrameDeleted", render_frame_host);
85 void RenderProcessGone(base::TerminationStatus status) override {
86 LogWhatHappened("RenderProcessGone");
89 private:
90 void LogWhatHappened(const std::string& event_name) {
91 if (!log_.empty()) {
92 log_.append("\n");
94 log_.append(event_name + " -> ");
95 AppendTreeNodeState(
96 static_cast<WebContentsImpl*>(web_contents())->GetFrameTree()->root(),
97 &log_);
100 void LogWhatHappened(const std::string& event_name, RenderFrameHost* rfh) {
101 LogWhatHappened(
102 base::StringPrintf("%s(%d)", event_name.c_str(), rfh->GetRoutingID()));
105 std::string log_;
107 DISALLOW_COPY_AND_ASSIGN(TreeWalkingWebContentsLogger);
110 } // namespace
112 class FrameTreeTest : public RenderViewHostImplTestHarness {
113 protected:
114 // Prints a FrameTree, for easy assertions of the tree hierarchy.
115 std::string GetTreeState(FrameTree* frame_tree) {
116 std::string result;
117 AppendTreeNodeState(frame_tree->root(), &result);
118 return result;
122 // Exercise tree manipulation routines.
123 // - Add a series of nodes and verify tree structure.
124 // - Remove a series of nodes and verify tree structure.
126 // TODO(nick): http://crbug.com/444722 Disabled temporarily because of a bad
127 // interaction with the WebContentsObserverConsistencyChecker -- calling
128 // AddFrame directly causes the RFH to not be announced. We either need to
129 // rewrite this test, or be consistent in the layer at which we announce render
130 // frame creation.
131 TEST_F(FrameTreeTest, DISABLED_Shape) {
132 // Use the FrameTree of the WebContents so that it has all the delegates it
133 // needs. We may want to consider a test version of this.
134 FrameTree* frame_tree = contents()->GetFrameTree();
135 FrameTreeNode* root = frame_tree->root();
137 std::string no_children_node("no children node");
138 std::string deep_subtree("node with deep subtree");
139 int process_id = root->current_frame_host()->GetProcess()->GetID();
141 ASSERT_EQ("1: []", GetTreeState(frame_tree));
143 // Simulate attaching a series of frames to build the frame tree.
144 frame_tree->AddFrame(root, process_id, 14, std::string());
145 frame_tree->AddFrame(root, process_id, 15, std::string());
146 frame_tree->AddFrame(root, process_id, 16, std::string());
148 frame_tree->AddFrame(root->child_at(0), process_id, 244, std::string());
149 frame_tree->AddFrame(root->child_at(1), process_id, 255, no_children_node);
150 frame_tree->AddFrame(root->child_at(0), process_id, 245, std::string());
152 ASSERT_EQ("1: [14: [244: [], 245: []], "
153 "15: [255 'no children node': []], "
154 "16: []]",
155 GetTreeState(frame_tree));
157 FrameTreeNode* child_16 = root->child_at(2);
158 frame_tree->AddFrame(child_16, process_id, 264, std::string());
159 frame_tree->AddFrame(child_16, process_id, 265, std::string());
160 frame_tree->AddFrame(child_16, process_id, 266, std::string());
161 frame_tree->AddFrame(child_16, process_id, 267, deep_subtree);
162 frame_tree->AddFrame(child_16, process_id, 268, std::string());
164 FrameTreeNode* child_267 = child_16->child_at(3);
165 frame_tree->AddFrame(child_267, process_id, 365, std::string());
166 frame_tree->AddFrame(child_267->child_at(0), process_id, 455, std::string());
167 frame_tree->AddFrame(child_267->child_at(0)->child_at(0), process_id, 555,
168 std::string());
169 frame_tree->AddFrame(child_267->child_at(0)->child_at(0)->child_at(0),
170 process_id, 655, std::string());
172 // Now that's it's fully built, verify the tree structure is as expected.
173 ASSERT_EQ("1: [14: [244: [], 245: []], "
174 "15: [255 'no children node': []], "
175 "16: [264: [], 265: [], 266: [], "
176 "267 'node with deep subtree': "
177 "[365: [455: [555: [655: []]]]], 268: []]]",
178 GetTreeState(frame_tree));
180 FrameTreeNode* child_555 = child_267->child_at(0)->child_at(0)->child_at(0);
181 frame_tree->RemoveFrame(child_555);
182 ASSERT_EQ("1: [14: [244: [], 245: []], "
183 "15: [255 'no children node': []], "
184 "16: [264: [], 265: [], 266: [], "
185 "267 'node with deep subtree': "
186 "[365: [455: []]], 268: []]]",
187 GetTreeState(frame_tree));
189 frame_tree->RemoveFrame(child_16->child_at(1));
190 ASSERT_EQ("1: [14: [244: [], 245: []], "
191 "15: [255 'no children node': []], "
192 "16: [264: [], 266: [], "
193 "267 'node with deep subtree': "
194 "[365: [455: []]], 268: []]]",
195 GetTreeState(frame_tree));
197 frame_tree->RemoveFrame(root->child_at(1));
198 ASSERT_EQ("1: [14: [244: [], 245: []], "
199 "16: [264: [], 266: [], "
200 "267 'node with deep subtree': "
201 "[365: [455: []]], 268: []]]",
202 GetTreeState(frame_tree));
205 // Do some simple manipulations of the frame tree, making sure that
206 // WebContentsObservers see a consistent view of the tree as we go.
207 TEST_F(FrameTreeTest, ObserverWalksTreeDuringFrameCreation) {
208 TreeWalkingWebContentsLogger activity(contents());
209 FrameTree* frame_tree = contents()->GetFrameTree();
210 FrameTreeNode* root = frame_tree->root();
212 EXPECT_EQ("", activity.GetLog());
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("RenderFrameCreated(14) -> 1: [14: []]", activity.GetLog());
217 main_test_rfh()->OnCreateChildFrame(18, std::string(), SandboxFlags::NONE);
218 EXPECT_EQ("RenderFrameCreated(18) -> 1: [14: [], 18: []]", activity.GetLog());
219 frame_tree->RemoveFrame(root->child_at(0));
220 EXPECT_EQ("RenderFrameDeleted(14) -> 1: [18: []]", activity.GetLog());
221 frame_tree->RemoveFrame(root->child_at(0));
222 EXPECT_EQ("RenderFrameDeleted(18) -> 1: []", activity.GetLog());
225 // Make sure that WebContentsObservers see a consistent view of the tree after
226 // recovery from a render process crash.
227 TEST_F(FrameTreeTest, ObserverWalksTreeAfterCrash) {
228 TreeWalkingWebContentsLogger activity(contents());
230 main_test_rfh()->OnCreateChildFrame(22, std::string(), SandboxFlags::NONE);
231 EXPECT_EQ("RenderFrameCreated(22) -> 1: [22: []]", activity.GetLog());
232 main_test_rfh()->OnCreateChildFrame(23, std::string(), SandboxFlags::NONE);
233 EXPECT_EQ("RenderFrameCreated(23) -> 1: [22: [], 23: []]", activity.GetLog());
235 // Crash the renderer
236 main_test_rfh()->OnMessageReceived(FrameHostMsg_RenderProcessGone(
237 main_test_rfh()->GetRoutingID(), base::TERMINATION_STATUS_PROCESS_CRASHED,
238 -1));
239 EXPECT_EQ(
240 "RenderFrameDeleted(22) -> 1: []\n"
241 "RenderFrameDeleted(23) -> 1: []\n"
242 "RenderFrameDeleted(1) -> 1: []\n"
243 "RenderProcessGone -> 1: []",
244 activity.GetLog());
247 // Ensure that frames are not added to the tree, if the process passed in
248 // is different than the process of the parent node.
249 TEST_F(FrameTreeTest, FailAddFrameWithWrongProcessId) {
250 FrameTree* frame_tree = contents()->GetFrameTree();
251 FrameTreeNode* root = frame_tree->root();
252 int process_id = root->current_frame_host()->GetProcess()->GetID();
254 ASSERT_EQ("1: []", GetTreeState(frame_tree));
256 // Simulate attaching a frame from mismatched process id.
257 ASSERT_FALSE(frame_tree->AddFrame(root, process_id + 1, 1, std::string()));
258 ASSERT_EQ("1: []", GetTreeState(frame_tree));
261 // Ensure that frames removed while a process has crashed are not preserved in
262 // the global map of id->frame.
263 TEST_F(FrameTreeTest, ProcessCrashClearsGlobalMap) {
264 // Add a couple child frames to the main frame.
265 FrameTreeNode* root = contents()->GetFrameTree()->root();
267 main_test_rfh()->OnCreateChildFrame(22, std::string(), SandboxFlags::NONE);
268 main_test_rfh()->OnCreateChildFrame(23, std::string(), SandboxFlags::NONE);
270 // Ensure they can be found by id.
271 int64 id1 = root->child_at(0)->frame_tree_node_id();
272 int64 id2 = root->child_at(1)->frame_tree_node_id();
273 EXPECT_TRUE(FrameTree::GloballyFindByID(id1));
274 EXPECT_TRUE(FrameTree::GloballyFindByID(id2));
276 // Crash the renderer.
277 main_test_rfh()->OnMessageReceived(FrameHostMsg_RenderProcessGone(
278 0, base::TERMINATION_STATUS_PROCESS_CRASHED, -1));
280 // Ensure they cannot be found by id after the process has crashed.
281 EXPECT_FALSE(FrameTree::GloballyFindByID(id1));
282 EXPECT_FALSE(FrameTree::GloballyFindByID(id2));
285 } // namespace content