frame_tree_unittest.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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.
4
5#include "content/browser/frame_host/frame_tree.h"
6
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/view_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_view_host.h"
20#include "content/test/test_web_contents.h"
21#include "testing/gtest/include/gtest/gtest.h"
22
23namespace content {
24namespace {
25
26// Appends a description of the structure of the frame tree to |result|.
27void AppendTreeNodeState(FrameTreeNode* node, std::string* result) {
28  result->append(
29      base::Int64ToString(node->current_frame_host()->GetRoutingID()));
30  if (!node->frame_name().empty()) {
31    result->append(" '");
32    result->append(node->frame_name());
33    result->append("'");
34  }
35  result->append(": [");
36  const char* separator = "";
37  for (size_t i = 0; i < node->child_count(); i++) {
38    result->append(separator);
39    AppendTreeNodeState(node->child_at(i), result);
40    separator = ", ";
41  }
42  result->append("]");
43}
44
45// Logs calls to WebContentsObserver along with the state of the frame tree,
46// for later use in EXPECT_EQ().
47class TreeWalkingWebContentsLogger : public WebContentsObserver {
48 public:
49  explicit TreeWalkingWebContentsLogger(WebContents* web_contents)
50      : WebContentsObserver(web_contents) {}
51
52  virtual ~TreeWalkingWebContentsLogger() {
53    EXPECT_EQ("", log_) << "Activity logged that was not expected";
54  }
55
56  // Gets and resets the log, which is a string of what happened.
57  std::string GetLog() {
58    std::string result = log_;
59    log_.clear();
60    return result;
61  }
62
63  // content::WebContentsObserver implementation.
64  virtual void RenderFrameCreated(RenderFrameHost* render_frame_host) OVERRIDE {
65    LogWhatHappened("RenderFrameCreated", render_frame_host);
66  }
67
68  virtual void RenderFrameHostChanged(RenderFrameHost* old_host,
69                                      RenderFrameHost* new_host) OVERRIDE {
70    if (old_host)
71      LogWhatHappened("RenderFrameChanged(old)", old_host);
72    LogWhatHappened("RenderFrameChanged(new)", new_host);
73  }
74
75  virtual void RenderFrameDeleted(RenderFrameHost* render_frame_host) OVERRIDE {
76    LogWhatHappened("RenderFrameDeleted", render_frame_host);
77  }
78
79  virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE {
80    LogWhatHappened("RenderProcessGone");
81  }
82
83 private:
84  void LogWhatHappened(const std::string& event_name) {
85    if (!log_.empty()) {
86      log_.append("\n");
87    }
88    log_.append(event_name + " -> ");
89    AppendTreeNodeState(
90        static_cast<WebContentsImpl*>(web_contents())->GetFrameTree()->root(),
91        &log_);
92  }
93
94  void LogWhatHappened(const std::string& event_name, RenderFrameHost* rfh) {
95    LogWhatHappened(
96        base::StringPrintf("%s(%d)", event_name.c_str(), rfh->GetRoutingID()));
97  }
98
99  std::string log_;
100
101  DISALLOW_COPY_AND_ASSIGN(TreeWalkingWebContentsLogger);
102};
103
104class FrameTreeTest : public RenderViewHostImplTestHarness {
105 protected:
106  // Prints a FrameTree, for easy assertions of the tree hierarchy.
107  std::string GetTreeState(FrameTree* frame_tree) {
108    std::string result;
109    AppendTreeNodeState(frame_tree->root(), &result);
110    return result;
111  }
112};
113
114// Exercise tree manipulation routines.
115//  - Add a series of nodes and verify tree structure.
116//  - Remove a series of nodes and verify tree structure.
117TEST_F(FrameTreeTest, Shape) {
118  // Use the FrameTree of the WebContents so that it has all the delegates it
119  // needs.  We may want to consider a test version of this.
120  FrameTree* frame_tree = contents()->GetFrameTree();
121  FrameTreeNode* root = frame_tree->root();
122
123  std::string no_children_node("no children node");
124  std::string deep_subtree("node with deep subtree");
125
126  ASSERT_EQ("1: []", GetTreeState(frame_tree));
127
128  // Simulate attaching a series of frames to build the frame tree.
129  frame_tree->AddFrame(root, 14, std::string());
130  frame_tree->AddFrame(root, 15, std::string());
131  frame_tree->AddFrame(root, 16, std::string());
132
133  frame_tree->AddFrame(root->child_at(0), 244, std::string());
134  frame_tree->AddFrame(root->child_at(1), 255, no_children_node);
135  frame_tree->AddFrame(root->child_at(0), 245, std::string());
136
137  ASSERT_EQ("1: [14: [244: [], 245: []], "
138                "15: [255 'no children node': []], "
139                "16: []]",
140            GetTreeState(frame_tree));
141
142  FrameTreeNode* child_16 = root->child_at(2);
143  frame_tree->AddFrame(child_16, 264, std::string());
144  frame_tree->AddFrame(child_16, 265, std::string());
145  frame_tree->AddFrame(child_16, 266, std::string());
146  frame_tree->AddFrame(child_16, 267, deep_subtree);
147  frame_tree->AddFrame(child_16, 268, std::string());
148
149  FrameTreeNode* child_267 = child_16->child_at(3);
150  frame_tree->AddFrame(child_267, 365, std::string());
151  frame_tree->AddFrame(child_267->child_at(0), 455, std::string());
152  frame_tree->AddFrame(child_267->child_at(0)->child_at(0), 555, std::string());
153  frame_tree->AddFrame(child_267->child_at(0)->child_at(0)->child_at(0), 655,
154                       std::string());
155
156  // Now that's it's fully built, verify the tree structure is as expected.
157  ASSERT_EQ("1: [14: [244: [], 245: []], "
158                "15: [255 'no children node': []], "
159                "16: [264: [], 265: [], 266: [], "
160                     "267 'node with deep subtree': "
161                         "[365: [455: [555: [655: []]]]], 268: []]]",
162            GetTreeState(frame_tree));
163
164  FrameTreeNode* child_555 = child_267->child_at(0)->child_at(0)->child_at(0);
165  frame_tree->RemoveFrame(child_555);
166  ASSERT_EQ("1: [14: [244: [], 245: []], "
167                "15: [255 'no children node': []], "
168                "16: [264: [], 265: [], 266: [], "
169                     "267 'node with deep subtree': "
170                         "[365: [455: []]], 268: []]]",
171            GetTreeState(frame_tree));
172
173  frame_tree->RemoveFrame(child_16->child_at(1));
174  ASSERT_EQ("1: [14: [244: [], 245: []], "
175                "15: [255 'no children node': []], "
176                "16: [264: [], 266: [], "
177                     "267 'node with deep subtree': "
178                         "[365: [455: []]], 268: []]]",
179            GetTreeState(frame_tree));
180
181  frame_tree->RemoveFrame(root->child_at(1));
182  ASSERT_EQ("1: [14: [244: [], 245: []], "
183                "16: [264: [], 266: [], "
184                     "267 'node with deep subtree': "
185                         "[365: [455: []]], 268: []]]",
186            GetTreeState(frame_tree));
187}
188
189// Do some simple manipulations of the frame tree, making sure that
190// WebContentsObservers see a consistent view of the tree as we go.
191TEST_F(FrameTreeTest, ObserverWalksTreeDuringFrameCreation) {
192  TreeWalkingWebContentsLogger activity(contents());
193  FrameTree* frame_tree = contents()->GetFrameTree();
194  FrameTreeNode* root = frame_tree->root();
195
196  EXPECT_EQ("", activity.GetLog());
197
198  // Simulate attaching a series of frames to build the frame tree.
199  main_test_rfh()->OnCreateChildFrame(14, std::string());
200  EXPECT_EQ("RenderFrameCreated(14) -> 1: [14: []]", activity.GetLog());
201  main_test_rfh()->OnCreateChildFrame(18, std::string());
202  EXPECT_EQ("RenderFrameCreated(18) -> 1: [14: [], 18: []]", activity.GetLog());
203  frame_tree->RemoveFrame(root->child_at(0));
204  EXPECT_EQ("RenderFrameDeleted(14) -> 1: [18: []]", activity.GetLog());
205  frame_tree->RemoveFrame(root->child_at(0));
206  EXPECT_EQ("RenderFrameDeleted(18) -> 1: []", activity.GetLog());
207}
208
209// Make sure that WebContentsObservers see a consistent view of the tree after
210// recovery from a render process crash.
211TEST_F(FrameTreeTest, ObserverWalksTreeAfterCrash) {
212  TreeWalkingWebContentsLogger activity(contents());
213
214  main_test_rfh()->OnCreateChildFrame(22, std::string());
215  EXPECT_EQ("RenderFrameCreated(22) -> 1: [22: []]", activity.GetLog());
216  main_test_rfh()->OnCreateChildFrame(23, std::string());
217  EXPECT_EQ("RenderFrameCreated(23) -> 1: [22: [], 23: []]", activity.GetLog());
218
219  // Crash the renderer
220  test_rvh()->OnMessageReceived(ViewHostMsg_RenderProcessGone(
221      0, base::TERMINATION_STATUS_PROCESS_CRASHED, -1));
222  EXPECT_EQ(
223      "RenderFrameDeleted(22) -> 1: []\n"
224      "RenderFrameDeleted(23) -> 1: []\n"
225      "RenderProcessGone -> 1: []",
226      activity.GetLog());
227}
228
229}  // namespace
230}  // namespace content
231