[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" |
nasko | b985af1 | 2015-02-06 04:15:33 | [diff] [blame] | 14 | #include "content/common/frame_messages.h" |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 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" |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 23 | #include "third_party/WebKit/public/web/WebSandboxFlags.h" |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 24 | |
| 25 | namespace content { |
dgrogan | fb22f9a | 2014-10-20 21:32:32 | [diff] [blame] | 26 | |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 27 | namespace { |
| 28 | |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 29 | // Appends a description of the structure of the frame tree to |result|. |
| 30 | void AppendTreeNodeState(FrameTreeNode* node, std::string* result) { |
| 31 | result->append( |
| 32 | base::Int64ToString(node->current_frame_host()->GetRoutingID())); |
schenney | 6408fed2 | 2015-04-17 17:44:57 | [diff] [blame] | 33 | if (!node->current_frame_host()->IsRenderFrameLive()) |
| 34 | result->append("*"); // Asterisk next to dead frames. |
| 35 | |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 36 | if (!node->frame_name().empty()) { |
| 37 | result->append(" '"); |
| 38 | result->append(node->frame_name()); |
| 39 | result->append("'"); |
| 40 | } |
| 41 | result->append(": ["); |
| 42 | const char* separator = ""; |
| 43 | for (size_t i = 0; i < node->child_count(); i++) { |
| 44 | result->append(separator); |
| 45 | AppendTreeNodeState(node->child_at(i), result); |
| 46 | separator = ", "; |
| 47 | } |
| 48 | result->append("]"); |
| 49 | } |
| 50 | |
| 51 | // Logs calls to WebContentsObserver along with the state of the frame tree, |
| 52 | // for later use in EXPECT_EQ(). |
| 53 | class TreeWalkingWebContentsLogger : public WebContentsObserver { |
| 54 | public: |
| 55 | explicit TreeWalkingWebContentsLogger(WebContents* web_contents) |
| 56 | : WebContentsObserver(web_contents) {} |
| 57 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 58 | ~TreeWalkingWebContentsLogger() override { |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 59 | EXPECT_EQ("", log_) << "Activity logged that was not expected"; |
| 60 | } |
| 61 | |
| 62 | // Gets and resets the log, which is a string of what happened. |
| 63 | std::string GetLog() { |
| 64 | std::string result = log_; |
| 65 | log_.clear(); |
| 66 | return result; |
| 67 | } |
| 68 | |
| 69 | // content::WebContentsObserver implementation. |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 70 | void RenderFrameCreated(RenderFrameHost* render_frame_host) override { |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 71 | LogWhatHappened("RenderFrameCreated", render_frame_host); |
| 72 | } |
| 73 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 74 | void RenderFrameHostChanged(RenderFrameHost* old_host, |
| 75 | RenderFrameHost* new_host) override { |
[email protected] | 02d7b6e | 2014-06-24 21:01:50 | [diff] [blame] | 76 | if (old_host) |
nasko | f5940b9f | 2015-03-02 23:04:05 | [diff] [blame] | 77 | LogWhatHappened("RenderFrameHostChanged(old)", old_host); |
| 78 | LogWhatHappened("RenderFrameHostChanged(new)", new_host); |
[email protected] | 02d7b6e | 2014-06-24 21:01:50 | [diff] [blame] | 79 | } |
| 80 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 81 | void RenderFrameDeleted(RenderFrameHost* render_frame_host) override { |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 82 | LogWhatHappened("RenderFrameDeleted", render_frame_host); |
| 83 | } |
| 84 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 85 | void RenderProcessGone(base::TerminationStatus status) override { |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 86 | LogWhatHappened("RenderProcessGone"); |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | void LogWhatHappened(const std::string& event_name) { |
| 91 | if (!log_.empty()) { |
| 92 | log_.append("\n"); |
| 93 | } |
| 94 | log_.append(event_name + " -> "); |
| 95 | AppendTreeNodeState( |
| 96 | static_cast<WebContentsImpl*>(web_contents())->GetFrameTree()->root(), |
| 97 | &log_); |
| 98 | } |
| 99 | |
| 100 | void LogWhatHappened(const std::string& event_name, RenderFrameHost* rfh) { |
| 101 | LogWhatHappened( |
| 102 | base::StringPrintf("%s(%d)", event_name.c_str(), rfh->GetRoutingID())); |
| 103 | } |
| 104 | |
| 105 | std::string log_; |
| 106 | |
| 107 | DISALLOW_COPY_AND_ASSIGN(TreeWalkingWebContentsLogger); |
| 108 | }; |
| 109 | |
dgrogan | fb22f9a | 2014-10-20 21:32:32 | [diff] [blame] | 110 | } // namespace |
| 111 | |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 112 | class FrameTreeTest : public RenderViewHostImplTestHarness { |
[email protected] | 7cc7ebd | 2013-10-08 00:59:00 | [diff] [blame] | 113 | protected: |
| 114 | // Prints a FrameTree, for easy assertions of the tree hierarchy. |
| 115 | std::string GetTreeState(FrameTree* frame_tree) { |
| 116 | std::string result; |
[email protected] | fa944cb8 | 2013-11-15 17:51:21 | [diff] [blame] | 117 | AppendTreeNodeState(frame_tree->root(), &result); |
[email protected] | 7cc7ebd | 2013-10-08 00:59:00 | [diff] [blame] | 118 | return result; |
| 119 | } |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 120 | }; |
| 121 | |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 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. |
nick | f9acfbe | 2014-12-23 19:12:37 | [diff] [blame] | 125 | // |
| 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) { |
[email protected] | 94d0cc1 | 2013-12-18 00:07:41 | [diff] [blame] | 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. |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 134 | FrameTree* frame_tree = contents()->GetFrameTree(); |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 135 | FrameTreeNode* root = frame_tree->root(); |
[email protected] | 190b8c5 | 2013-11-09 01:35:44 | [diff] [blame] | 136 | |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 137 | std::string no_children_node("no children node"); |
| 138 | std::string deep_subtree("node with deep subtree"); |
dgrogan | fb22f9a | 2014-10-20 21:32:32 | [diff] [blame] | 139 | int process_id = root->current_frame_host()->GetProcess()->GetID(); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 140 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 141 | ASSERT_EQ("1: []", GetTreeState(frame_tree)); |
[email protected] | 7cc7ebd | 2013-10-08 00:59:00 | [diff] [blame] | 142 | |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 143 | // Simulate attaching a series of frames to build the frame tree. |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 144 | frame_tree->AddFrame(root, process_id, 14, blink::WebTreeScopeType::Document, |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 145 | std::string(), blink::WebSandboxFlags::None); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 146 | frame_tree->AddFrame(root, process_id, 15, blink::WebTreeScopeType::Document, |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 147 | std::string(), blink::WebSandboxFlags::None); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 148 | frame_tree->AddFrame(root, process_id, 16, blink::WebTreeScopeType::Document, |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 149 | std::string(), blink::WebSandboxFlags::None); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 150 | |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 151 | frame_tree->AddFrame(root->child_at(0), process_id, 244, |
| 152 | blink::WebTreeScopeType::Document, std::string(), |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 153 | blink::WebSandboxFlags::None); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 154 | frame_tree->AddFrame(root->child_at(1), process_id, 255, |
| 155 | blink::WebTreeScopeType::Document, no_children_node, |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 156 | blink::WebSandboxFlags::None); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 157 | frame_tree->AddFrame(root->child_at(0), process_id, 245, |
| 158 | blink::WebTreeScopeType::Document, std::string(), |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 159 | blink::WebSandboxFlags::None); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 160 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 161 | ASSERT_EQ("1: [14: [244: [], 245: []], " |
[email protected] | 7cc7ebd | 2013-10-08 00:59:00 | [diff] [blame] | 162 | "15: [255 'no children node': []], " |
| 163 | "16: []]", |
[email protected] | 94d0cc1 | 2013-12-18 00:07:41 | [diff] [blame] | 164 | GetTreeState(frame_tree)); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 165 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 166 | FrameTreeNode* child_16 = root->child_at(2); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 167 | frame_tree->AddFrame(child_16, process_id, 264, |
| 168 | blink::WebTreeScopeType::Document, std::string(), |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 169 | blink::WebSandboxFlags::None); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 170 | frame_tree->AddFrame(child_16, process_id, 265, |
| 171 | blink::WebTreeScopeType::Document, std::string(), |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 172 | blink::WebSandboxFlags::None); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 173 | frame_tree->AddFrame(child_16, process_id, 266, |
| 174 | blink::WebTreeScopeType::Document, std::string(), |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 175 | blink::WebSandboxFlags::None); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 176 | frame_tree->AddFrame(child_16, process_id, 267, |
| 177 | blink::WebTreeScopeType::Document, deep_subtree, |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 178 | blink::WebSandboxFlags::None); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 179 | frame_tree->AddFrame(child_16, process_id, 268, |
| 180 | blink::WebTreeScopeType::Document, std::string(), |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 181 | blink::WebSandboxFlags::None); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 182 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 183 | FrameTreeNode* child_267 = child_16->child_at(3); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 184 | frame_tree->AddFrame(child_267, process_id, 365, |
| 185 | blink::WebTreeScopeType::Document, std::string(), |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 186 | blink::WebSandboxFlags::None); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 187 | frame_tree->AddFrame(child_267->child_at(0), process_id, 455, |
| 188 | blink::WebTreeScopeType::Document, std::string(), |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 189 | blink::WebSandboxFlags::None); |
dgrogan | fb22f9a | 2014-10-20 21:32:32 | [diff] [blame] | 190 | frame_tree->AddFrame(child_267->child_at(0)->child_at(0), process_id, 555, |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 191 | blink::WebTreeScopeType::Document, std::string(), |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 192 | blink::WebSandboxFlags::None); |
dgrogan | fb22f9a | 2014-10-20 21:32:32 | [diff] [blame] | 193 | frame_tree->AddFrame(child_267->child_at(0)->child_at(0)->child_at(0), |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 194 | process_id, 655, blink::WebTreeScopeType::Document, |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 195 | std::string(), blink::WebSandboxFlags::None); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 196 | |
[email protected] | 7cc7ebd | 2013-10-08 00:59:00 | [diff] [blame] | 197 | // 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] | 198 | ASSERT_EQ("1: [14: [244: [], 245: []], " |
[email protected] | 7cc7ebd | 2013-10-08 00:59:00 | [diff] [blame] | 199 | "15: [255 'no children node': []], " |
| 200 | "16: [264: [], 265: [], 266: [], " |
| 201 | "267 'node with deep subtree': " |
| 202 | "[365: [455: [555: [655: []]]]], 268: []]]", |
[email protected] | 94d0cc1 | 2013-12-18 00:07:41 | [diff] [blame] | 203 | GetTreeState(frame_tree)); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 204 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 205 | FrameTreeNode* child_555 = child_267->child_at(0)->child_at(0)->child_at(0); |
| 206 | frame_tree->RemoveFrame(child_555); |
| 207 | ASSERT_EQ("1: [14: [244: [], 245: []], " |
[email protected] | 7cc7ebd | 2013-10-08 00:59:00 | [diff] [blame] | 208 | "15: [255 'no children node': []], " |
| 209 | "16: [264: [], 265: [], 266: [], " |
| 210 | "267 'node with deep subtree': " |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 211 | "[365: [455: []]], 268: []]]", |
[email protected] | 94d0cc1 | 2013-12-18 00:07:41 | [diff] [blame] | 212 | GetTreeState(frame_tree)); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 213 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 214 | frame_tree->RemoveFrame(child_16->child_at(1)); |
| 215 | ASSERT_EQ("1: [14: [244: [], 245: []], " |
[email protected] | 7cc7ebd | 2013-10-08 00:59:00 | [diff] [blame] | 216 | "15: [255 'no children node': []], " |
| 217 | "16: [264: [], 266: [], " |
| 218 | "267 'node with deep subtree': " |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 219 | "[365: [455: []]], 268: []]]", |
[email protected] | 94d0cc1 | 2013-12-18 00:07:41 | [diff] [blame] | 220 | GetTreeState(frame_tree)); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 221 | |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 222 | frame_tree->RemoveFrame(root->child_at(1)); |
| 223 | ASSERT_EQ("1: [14: [244: [], 245: []], " |
[email protected] | 7cc7ebd | 2013-10-08 00:59:00 | [diff] [blame] | 224 | "16: [264: [], 266: [], " |
| 225 | "267 'node with deep subtree': " |
[email protected] | 58faf94 | 2014-02-20 21:03:58 | [diff] [blame] | 226 | "[365: [455: []]], 268: []]]", |
[email protected] | 94d0cc1 | 2013-12-18 00:07:41 | [diff] [blame] | 227 | GetTreeState(frame_tree)); |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 228 | } |
| 229 | |
creis | 6a93a81 | 2015-04-24 23:13:17 | [diff] [blame] | 230 | // Ensure frames can be found by frame_tree_node_id, routing ID, or name. |
| 231 | TEST_F(FrameTreeTest, FindFrames) { |
| 232 | // Add a few child frames to the main frame. |
| 233 | FrameTree* frame_tree = contents()->GetFrameTree(); |
| 234 | FrameTreeNode* root = frame_tree->root(); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 235 | main_test_rfh()->OnCreateChildFrame(22, blink::WebTreeScopeType::Document, |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 236 | "child0", blink::WebSandboxFlags::None); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 237 | main_test_rfh()->OnCreateChildFrame(23, blink::WebTreeScopeType::Document, |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 238 | "child1", blink::WebSandboxFlags::None); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 239 | main_test_rfh()->OnCreateChildFrame(24, blink::WebTreeScopeType::Document, |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 240 | std::string(), |
| 241 | blink::WebSandboxFlags::None); |
creis | 6a93a81 | 2015-04-24 23:13:17 | [diff] [blame] | 242 | FrameTreeNode* child0 = root->child_at(0); |
| 243 | FrameTreeNode* child1 = root->child_at(1); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 244 | |
creis | 6a93a81 | 2015-04-24 23:13:17 | [diff] [blame] | 245 | FrameTreeNode* child2 = root->child_at(2); |
| 246 | |
| 247 | // Add one grandchild frame. |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 248 | child1->current_frame_host()->OnCreateChildFrame( |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 249 | 33, blink::WebTreeScopeType::Document, "grandchild", |
| 250 | blink::WebSandboxFlags::None); |
creis | 6a93a81 | 2015-04-24 23:13:17 | [diff] [blame] | 251 | FrameTreeNode* grandchild = child1->child_at(0); |
| 252 | |
| 253 | // Ensure they can be found by FTN id. |
| 254 | EXPECT_EQ(root, frame_tree->FindByID(root->frame_tree_node_id())); |
| 255 | EXPECT_EQ(child0, frame_tree->FindByID(child0->frame_tree_node_id())); |
| 256 | EXPECT_EQ(child1, frame_tree->FindByID(child1->frame_tree_node_id())); |
| 257 | EXPECT_EQ(child2, frame_tree->FindByID(child2->frame_tree_node_id())); |
| 258 | EXPECT_EQ(grandchild, frame_tree->FindByID(grandchild->frame_tree_node_id())); |
| 259 | EXPECT_EQ(nullptr, frame_tree->FindByID(-1)); |
| 260 | |
| 261 | // Ensure they can be found by routing id. |
| 262 | int process_id = main_test_rfh()->GetProcess()->GetID(); |
| 263 | EXPECT_EQ(root, frame_tree->FindByRoutingID(process_id, |
| 264 | main_test_rfh()->GetRoutingID())); |
| 265 | EXPECT_EQ(child0, frame_tree->FindByRoutingID(process_id, 22)); |
| 266 | EXPECT_EQ(child1, frame_tree->FindByRoutingID(process_id, 23)); |
| 267 | EXPECT_EQ(child2, frame_tree->FindByRoutingID(process_id, 24)); |
| 268 | EXPECT_EQ(grandchild, frame_tree->FindByRoutingID(process_id, 33)); |
| 269 | EXPECT_EQ(nullptr, frame_tree->FindByRoutingID(process_id, 37)); |
| 270 | |
| 271 | // Ensure they can be found by name, if they have one. |
| 272 | EXPECT_EQ(root, frame_tree->FindByName(std::string())); |
| 273 | EXPECT_EQ(child0, frame_tree->FindByName("child0")); |
| 274 | EXPECT_EQ(child1, frame_tree->FindByName("child1")); |
| 275 | EXPECT_EQ(grandchild, frame_tree->FindByName("grandchild")); |
| 276 | EXPECT_EQ(nullptr, frame_tree->FindByName("no such frame")); |
| 277 | } |
| 278 | |
alexmos | 9f8705a | 2015-05-06 19:58:59 | [diff] [blame] | 279 | // Check that PreviousSibling() is retrieved correctly. |
| 280 | TEST_F(FrameTreeTest, PreviousSibling) { |
| 281 | // Add a few child frames to the main frame. |
| 282 | FrameTree* frame_tree = contents()->GetFrameTree(); |
| 283 | FrameTreeNode* root = frame_tree->root(); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 284 | main_test_rfh()->OnCreateChildFrame(22, blink::WebTreeScopeType::Document, |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 285 | "child0", blink::WebSandboxFlags::None); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 286 | main_test_rfh()->OnCreateChildFrame(23, blink::WebTreeScopeType::Document, |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 287 | "child1", blink::WebSandboxFlags::None); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 288 | main_test_rfh()->OnCreateChildFrame(24, blink::WebTreeScopeType::Document, |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 289 | "child2", blink::WebSandboxFlags::None); |
alexmos | 9f8705a | 2015-05-06 19:58:59 | [diff] [blame] | 290 | FrameTreeNode* child0 = root->child_at(0); |
| 291 | FrameTreeNode* child1 = root->child_at(1); |
| 292 | FrameTreeNode* child2 = root->child_at(2); |
| 293 | |
| 294 | // Add one grandchild frame. |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 295 | child1->current_frame_host()->OnCreateChildFrame( |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 296 | 33, blink::WebTreeScopeType::Document, "grandchild", |
| 297 | blink::WebSandboxFlags::None); |
alexmos | 9f8705a | 2015-05-06 19:58:59 | [diff] [blame] | 298 | FrameTreeNode* grandchild = child1->child_at(0); |
| 299 | |
| 300 | EXPECT_EQ(nullptr, root->PreviousSibling()); |
| 301 | EXPECT_EQ(nullptr, child0->PreviousSibling()); |
| 302 | EXPECT_EQ(child0, child1->PreviousSibling()); |
| 303 | EXPECT_EQ(child1, child2->PreviousSibling()); |
| 304 | EXPECT_EQ(nullptr, grandchild->PreviousSibling()); |
| 305 | } |
| 306 | |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 307 | // Do some simple manipulations of the frame tree, making sure that |
| 308 | // WebContentsObservers see a consistent view of the tree as we go. |
| 309 | TEST_F(FrameTreeTest, ObserverWalksTreeDuringFrameCreation) { |
| 310 | TreeWalkingWebContentsLogger activity(contents()); |
schenney | 6408fed2 | 2015-04-17 17:44:57 | [diff] [blame] | 311 | contents()->NavigateAndCommit(GURL("http://www.google.com")); |
| 312 | EXPECT_EQ("", activity.GetLog()); |
| 313 | |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 314 | FrameTree* frame_tree = contents()->GetFrameTree(); |
| 315 | FrameTreeNode* root = frame_tree->root(); |
| 316 | |
| 317 | // Simulate attaching a series of frames to build the frame tree. |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 318 | main_test_rfh()->OnCreateChildFrame(14, blink::WebTreeScopeType::Document, |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 319 | std::string(), |
| 320 | blink::WebSandboxFlags::None); |
nasko | f5940b9f | 2015-03-02 23:04:05 | [diff] [blame] | 321 | EXPECT_EQ( |
| 322 | "RenderFrameHostChanged(new)(14) -> 1: []\n" |
| 323 | "RenderFrameCreated(14) -> 1: [14: []]", |
| 324 | activity.GetLog()); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 325 | main_test_rfh()->OnCreateChildFrame(18, blink::WebTreeScopeType::Document, |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 326 | std::string(), |
| 327 | blink::WebSandboxFlags::None); |
nasko | f5940b9f | 2015-03-02 23:04:05 | [diff] [blame] | 328 | EXPECT_EQ( |
| 329 | "RenderFrameHostChanged(new)(18) -> 1: [14: []]\n" |
| 330 | "RenderFrameCreated(18) -> 1: [14: [], 18: []]", |
| 331 | activity.GetLog()); |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 332 | frame_tree->RemoveFrame(root->child_at(0)); |
| 333 | EXPECT_EQ("RenderFrameDeleted(14) -> 1: [18: []]", activity.GetLog()); |
| 334 | frame_tree->RemoveFrame(root->child_at(0)); |
| 335 | EXPECT_EQ("RenderFrameDeleted(18) -> 1: []", activity.GetLog()); |
| 336 | } |
| 337 | |
| 338 | // Make sure that WebContentsObservers see a consistent view of the tree after |
| 339 | // recovery from a render process crash. |
| 340 | TEST_F(FrameTreeTest, ObserverWalksTreeAfterCrash) { |
| 341 | TreeWalkingWebContentsLogger activity(contents()); |
schenney | 6408fed2 | 2015-04-17 17:44:57 | [diff] [blame] | 342 | contents()->NavigateAndCommit(GURL("http://www.google.com")); |
| 343 | EXPECT_EQ("", activity.GetLog()); |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 344 | |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 345 | main_test_rfh()->OnCreateChildFrame(22, blink::WebTreeScopeType::Document, |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 346 | std::string(), |
| 347 | blink::WebSandboxFlags::None); |
nasko | f5940b9f | 2015-03-02 23:04:05 | [diff] [blame] | 348 | EXPECT_EQ( |
| 349 | "RenderFrameHostChanged(new)(22) -> 1: []\n" |
| 350 | "RenderFrameCreated(22) -> 1: [22: []]", |
| 351 | activity.GetLog()); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 352 | main_test_rfh()->OnCreateChildFrame(23, blink::WebTreeScopeType::Document, |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 353 | std::string(), |
| 354 | blink::WebSandboxFlags::None); |
nasko | f5940b9f | 2015-03-02 23:04:05 | [diff] [blame] | 355 | EXPECT_EQ( |
| 356 | "RenderFrameHostChanged(new)(23) -> 1: [22: []]\n" |
| 357 | "RenderFrameCreated(23) -> 1: [22: [], 23: []]", |
| 358 | activity.GetLog()); |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 359 | |
| 360 | // Crash the renderer |
nick | 16b0765 | 2015-04-18 02:35:31 | [diff] [blame] | 361 | main_test_rfh()->GetProcess()->SimulateCrash(); |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 362 | EXPECT_EQ( |
nick | 16b0765 | 2015-04-18 02:35:31 | [diff] [blame] | 363 | "RenderFrameDeleted(23) -> 1: [22: [], 23*: []]\n" |
| 364 | "RenderFrameDeleted(22) -> 1: [22*: [], 23*: []]\n" |
| 365 | "RenderFrameDeleted(1) -> 1: []\n" // TODO(nick): Should be "1*:" |
| 366 | "RenderProcessGone -> 1*: []", |
[email protected] | 1426607 | 2014-04-19 00:35:20 | [diff] [blame] | 367 | activity.GetLog()); |
| 368 | } |
| 369 | |
dgrogan | fb22f9a | 2014-10-20 21:32:32 | [diff] [blame] | 370 | // Ensure that frames are not added to the tree, if the process passed in |
| 371 | // is different than the process of the parent node. |
| 372 | TEST_F(FrameTreeTest, FailAddFrameWithWrongProcessId) { |
schenney | 6408fed2 | 2015-04-17 17:44:57 | [diff] [blame] | 373 | contents()->NavigateAndCommit(GURL("http://www.google.com")); |
dgrogan | fb22f9a | 2014-10-20 21:32:32 | [diff] [blame] | 374 | FrameTree* frame_tree = contents()->GetFrameTree(); |
| 375 | FrameTreeNode* root = frame_tree->root(); |
| 376 | int process_id = root->current_frame_host()->GetProcess()->GetID(); |
| 377 | |
| 378 | ASSERT_EQ("1: []", GetTreeState(frame_tree)); |
| 379 | |
| 380 | // Simulate attaching a frame from mismatched process id. |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 381 | ASSERT_FALSE(frame_tree->AddFrame( |
| 382 | root, process_id + 1, 1, blink::WebTreeScopeType::Document, std::string(), |
| 383 | blink::WebSandboxFlags::None)); |
dgrogan | fb22f9a | 2014-10-20 21:32:32 | [diff] [blame] | 384 | ASSERT_EQ("1: []", GetTreeState(frame_tree)); |
| 385 | } |
| 386 | |
nasko | aeca57b | 2015-02-13 00:50:46 | [diff] [blame] | 387 | // Ensure that frames removed while a process has crashed are not preserved in |
| 388 | // the global map of id->frame. |
| 389 | TEST_F(FrameTreeTest, ProcessCrashClearsGlobalMap) { |
| 390 | // Add a couple child frames to the main frame. |
| 391 | FrameTreeNode* root = contents()->GetFrameTree()->root(); |
| 392 | |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 393 | main_test_rfh()->OnCreateChildFrame(22, blink::WebTreeScopeType::Document, |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 394 | std::string(), |
| 395 | blink::WebSandboxFlags::None); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 396 | main_test_rfh()->OnCreateChildFrame(23, blink::WebTreeScopeType::Document, |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 397 | std::string(), |
| 398 | blink::WebSandboxFlags::None); |
nasko | aeca57b | 2015-02-13 00:50:46 | [diff] [blame] | 399 | |
dmazzoni | e950ea23 | 2015-03-13 21:39:45 | [diff] [blame] | 400 | // Add one grandchild frame. |
| 401 | RenderFrameHostImpl* child1_rfh = root->child_at(0)->current_frame_host(); |
dcheng | 860817a | 2015-05-22 03:16:56 | [diff] [blame] | 402 | child1_rfh->OnCreateChildFrame(33, blink::WebTreeScopeType::Document, |
dcheng | 5f60abb | 2015-05-28 01:39:36 | [diff] [blame^] | 403 | std::string(), blink::WebSandboxFlags::None); |
dmazzoni | e950ea23 | 2015-03-13 21:39:45 | [diff] [blame] | 404 | |
nasko | aeca57b | 2015-02-13 00:50:46 | [diff] [blame] | 405 | // Ensure they can be found by id. |
vishal.b | 782eb5d | 2015-04-29 12:22:57 | [diff] [blame] | 406 | int id1 = root->child_at(0)->frame_tree_node_id(); |
| 407 | int id2 = root->child_at(1)->frame_tree_node_id(); |
| 408 | int id3 = root->child_at(0)->child_at(0)->frame_tree_node_id(); |
dmazzoni | e950ea23 | 2015-03-13 21:39:45 | [diff] [blame] | 409 | EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id1)); |
| 410 | EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id2)); |
| 411 | EXPECT_TRUE(FrameTreeNode::GloballyFindByID(id3)); |
nasko | aeca57b | 2015-02-13 00:50:46 | [diff] [blame] | 412 | |
| 413 | // Crash the renderer. |
nick | 16b0765 | 2015-04-18 02:35:31 | [diff] [blame] | 414 | main_test_rfh()->GetProcess()->SimulateCrash(); |
nasko | aeca57b | 2015-02-13 00:50:46 | [diff] [blame] | 415 | |
| 416 | // Ensure they cannot be found by id after the process has crashed. |
dmazzoni | e950ea23 | 2015-03-13 21:39:45 | [diff] [blame] | 417 | EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id1)); |
| 418 | EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id2)); |
| 419 | EXPECT_FALSE(FrameTreeNode::GloballyFindByID(id3)); |
nasko | aeca57b | 2015-02-13 00:50:46 | [diff] [blame] | 420 | } |
| 421 | |
[email protected] | 9b159a5 | 2013-10-03 17:24:55 | [diff] [blame] | 422 | } // namespace content |