[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 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 | |
[email protected] | d4a8ca48 | 2013-10-30 21:06:40 | [diff] [blame] | 5 | #include "content/browser/frame_host/frame_tree.h" |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 6 | |
| 7 | #include "base/run_loop.h" |
[email protected] | 7cc7ebd | 2013-10-08 00:59:00 | [diff] [blame] | 8 | #include "base/strings/string_number_conversions.h" |
[email protected] | 6ea6bdf | 2013-12-06 13:35:01 | [diff] [blame] | 9 | #include "content/browser/frame_host/navigator_impl.h" |
[email protected] | 2a18ee22 | 2013-11-21 07:52:44 | [diff] [blame] | 10 | #include "content/browser/frame_host/render_frame_host_factory.h" |
[email protected] | d4a8ca48 | 2013-10-30 21:06:40 | [diff] [blame] | 11 | #include "content/browser/frame_host/render_frame_host_impl.h" |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 12 | #include "content/browser/renderer_host/render_view_host_impl.h" |
[email protected] | 94d0cc1 | 2013-12-18 00:07:41 | [diff] [blame] | 13 | #include "content/browser/web_contents/web_contents_impl.h" |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 14 | #include "content/common/view_messages.h" |
| 15 | #include "content/public/browser/web_contents_observer.h" |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 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" |
[email protected] | 6b50e36 | 2014-08-15 05:15:59 | [diff] [blame] | 19 | #include "content/test/test_render_frame_host.h" |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 20 | #include "content/test/test_render_view_host.h" |
| 21 | #include "content/test/test_web_contents.h" |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 22 | #include "testing/gtest/include/gtest/gtest.h" |
| 23 | |
| 24 | namespace content { |
dgrogan | fb22f9a | 2014-10-20 21:32:32 | [diff] [blame] | 25 | |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 26 | namespace { |
| 27 | |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 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("'"); |
| 36 | } |
| 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 = ", "; |
| 43 | } |
| 44 | result->append("]"); |
| 45 | } |
| 46 | |
| 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) {} |
| 53 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 54 | ~TreeWalkingWebContentsLogger() override { |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 55 | EXPECT_EQ("", log_) << "Activity logged that was not expected"; |
| 56 | } |
| 57 | |
| 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; |
| 63 | } |
| 64 | |
| 65 | // content::WebContentsObserver implementation. |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 66 | void RenderFrameCreated(RenderFrameHost* render_frame_host) override { |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 67 | LogWhatHappened("RenderFrameCreated", render_frame_host); |
| 68 | } |
| 69 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 70 | void RenderFrameHostChanged(RenderFrameHost* old_host, |
| 71 | RenderFrameHost* new_host) override { |
[email protected] | 02d7b6e | 2014-06-24 21:01:50 | [diff] [blame] | 72 | if (old_host) |
| 73 | LogWhatHappened("RenderFrameChanged(old)", old_host); |
| 74 | LogWhatHappened("RenderFrameChanged(new)", new_host); |
| 75 | } |
| 76 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 77 | void RenderFrameDeleted(RenderFrameHost* render_frame_host) override { |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 78 | LogWhatHappened("RenderFrameDeleted", render_frame_host); |
| 79 | } |
| 80 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 81 | void RenderProcessGone(base::TerminationStatus status) override { |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 82 | LogWhatHappened("RenderProcessGone"); |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | void LogWhatHappened(const std::string& event_name) { |
| 87 | if (!log_.empty()) { |
| 88 | log_.append("\n"); |
| 89 | } |
| 90 | log_.append(event_name + " -> "); |
| 91 | AppendTreeNodeState( |
| 92 | static_cast<WebContentsImpl*>(web_contents())->GetFrameTree()->root(), |
| 93 | &log_); |
| 94 | } |
| 95 | |
| 96 | void LogWhatHappened(const std::string& event_name, RenderFrameHost* rfh) { |
| 97 | LogWhatHappened( |
| 98 | base::StringPrintf("%s(%d)", event_name.c_str(), rfh->GetRoutingID())); |
| 99 | } |
| 100 | |
| 101 | std::string log_; |
| 102 | |
| 103 | DISALLOW_COPY_AND_ASSIGN(TreeWalkingWebContentsLogger); |
| 104 | }; |
| 105 | |
dgrogan | fb22f9a | 2014-10-20 21:32:32 | [diff] [blame] | 106 | } // namespace |
| 107 | |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 108 | class FrameTreeTest : public RenderViewHostImplTestHarness { |
[email protected] | 7cc7ebd | 2013-10-08 00:59:00 | [diff] [blame] | 109 | protected: |
| 110 | // Prints a FrameTree, for easy assertions of the tree hierarchy. |
| 111 | std::string GetTreeState(FrameTree* frame_tree) { |
| 112 | std::string result; |
[email protected] | fa944cb8 | 2013-11-15 17:51:21 | [diff] [blame] | 113 | AppendTreeNodeState(frame_tree->root(), &result); |
[email protected] | 7cc7ebd | 2013-10-08 00:59:00 | [diff] [blame] | 114 | return result; |
| 115 | } |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 116 | }; |
| 117 | |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 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. |
nick | f9acfbe | 2014-12-23 19:12:37 | [diff] [blame^] | 121 | // |
| 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) { |
[email protected] | 94d0cc1 | 2013-12-18 00:07:41 | [diff] [blame] | 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. |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 130 | FrameTree* frame_tree = contents()->GetFrameTree(); |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 131 | FrameTreeNode* root = frame_tree->root(); |
[email protected] | 190b8c5 | 2013-11-09 01:35:44 | [diff] [blame] | 132 | |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 133 | std::string no_children_node("no children node"); |
| 134 | std::string deep_subtree("node with deep subtree"); |
dgrogan | fb22f9a | 2014-10-20 21:32:32 | [diff] [blame] | 135 | int process_id = root->current_frame_host()->GetProcess()->GetID(); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 136 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 137 | ASSERT_EQ("1: []", GetTreeState(frame_tree)); |
[email protected] | 7cc7ebd | 2013-10-08 00:59:00 | [diff] [blame] | 138 | |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 139 | // Simulate attaching a series of frames to build the frame tree. |
dgrogan | fb22f9a | 2014-10-20 21:32:32 | [diff] [blame] | 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()); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 143 | |
dgrogan | fb22f9a | 2014-10-20 21:32:32 | [diff] [blame] | 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()); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 147 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 148 | ASSERT_EQ("1: [14: [244: [], 245: []], " |
[email protected] | 7cc7ebd | 2013-10-08 00:59:00 | [diff] [blame] | 149 | "15: [255 'no children node': []], " |
| 150 | "16: []]", |
[email protected] | 94d0cc1 | 2013-12-18 00:07:41 | [diff] [blame] | 151 | GetTreeState(frame_tree)); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 152 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 153 | FrameTreeNode* child_16 = root->child_at(2); |
dgrogan | fb22f9a | 2014-10-20 21:32:32 | [diff] [blame] | 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()); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 159 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 160 | FrameTreeNode* child_267 = child_16->child_at(3); |
dgrogan | fb22f9a | 2014-10-20 21:32:32 | [diff] [blame] | 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, |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 164 | std::string()); |
dgrogan | fb22f9a | 2014-10-20 21:32:32 | [diff] [blame] | 165 | frame_tree->AddFrame(child_267->child_at(0)->child_at(0)->child_at(0), |
| 166 | process_id, 655, std::string()); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 167 | |
[email protected] | 7cc7ebd | 2013-10-08 00:59:00 | [diff] [blame] | 168 | // Now that's it's fully built, verify the tree structure is as expected. |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 169 | ASSERT_EQ("1: [14: [244: [], 245: []], " |
[email protected] | 7cc7ebd | 2013-10-08 00:59:00 | [diff] [blame] | 170 | "15: [255 'no children node': []], " |
| 171 | "16: [264: [], 265: [], 266: [], " |
| 172 | "267 'node with deep subtree': " |
| 173 | "[365: [455: [555: [655: []]]]], 268: []]]", |
[email protected] | 94d0cc1 | 2013-12-18 00:07:41 | [diff] [blame] | 174 | GetTreeState(frame_tree)); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 175 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 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: []], " |
[email protected] | 7cc7ebd | 2013-10-08 00:59:00 | [diff] [blame] | 179 | "15: [255 'no children node': []], " |
| 180 | "16: [264: [], 265: [], 266: [], " |
| 181 | "267 'node with deep subtree': " |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 182 | "[365: [455: []]], 268: []]]", |
[email protected] | 94d0cc1 | 2013-12-18 00:07:41 | [diff] [blame] | 183 | GetTreeState(frame_tree)); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 184 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 185 | frame_tree->RemoveFrame(child_16->child_at(1)); |
| 186 | ASSERT_EQ("1: [14: [244: [], 245: []], " |
[email protected] | 7cc7ebd | 2013-10-08 00:59:00 | [diff] [blame] | 187 | "15: [255 'no children node': []], " |
| 188 | "16: [264: [], 266: [], " |
| 189 | "267 'node with deep subtree': " |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 190 | "[365: [455: []]], 268: []]]", |
[email protected] | 94d0cc1 | 2013-12-18 00:07:41 | [diff] [blame] | 191 | GetTreeState(frame_tree)); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 192 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 193 | frame_tree->RemoveFrame(root->child_at(1)); |
| 194 | ASSERT_EQ("1: [14: [244: [], 245: []], " |
[email protected] | 7cc7ebd | 2013-10-08 00:59:00 | [diff] [blame] | 195 | "16: [264: [], 266: [], " |
| 196 | "267 'node with deep subtree': " |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 197 | "[365: [455: []]], 268: []]]", |
[email protected] | 94d0cc1 | 2013-12-18 00:07:41 | [diff] [blame] | 198 | GetTreeState(frame_tree)); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 199 | } |
| 200 | |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 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(); |
| 207 | |
[email protected] | 02d7b6e | 2014-06-24 21:01:50 | [diff] [blame] | 208 | EXPECT_EQ("", activity.GetLog()); |
| 209 | |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 210 | // Simulate attaching a series of frames to build the frame tree. |
| 211 | main_test_rfh()->OnCreateChildFrame(14, std::string()); |
| 212 | EXPECT_EQ("RenderFrameCreated(14) -> 1: [14: []]", activity.GetLog()); |
| 213 | main_test_rfh()->OnCreateChildFrame(18, std::string()); |
| 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()); |
| 219 | } |
| 220 | |
| 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()); |
| 225 | |
| 226 | main_test_rfh()->OnCreateChildFrame(22, std::string()); |
| 227 | EXPECT_EQ("RenderFrameCreated(22) -> 1: [22: []]", activity.GetLog()); |
| 228 | main_test_rfh()->OnCreateChildFrame(23, std::string()); |
| 229 | EXPECT_EQ("RenderFrameCreated(23) -> 1: [22: [], 23: []]", activity.GetLog()); |
| 230 | |
| 231 | // Crash the renderer |
| 232 | test_rvh()->OnMessageReceived(ViewHostMsg_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()); |
| 239 | } |
| 240 | |
dgrogan | fb22f9a | 2014-10-20 21:32:32 | [diff] [blame] | 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(); |
| 247 | |
| 248 | ASSERT_EQ("1: []", GetTreeState(frame_tree)); |
| 249 | |
| 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)); |
| 253 | } |
| 254 | |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 255 | } // namespace content |