Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(343)

Side by Side Diff: content/browser/frame_host/frame_tree_unittest.cc

Issue 30323002: [DRAFT] Create RenderFrameHostManager. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/frame_host/frame_tree.h" 5 #include "content/browser/frame_host/frame_tree.h"
6 6
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "base/strings/string_number_conversions.h" 8 #include "base/strings/string_number_conversions.h"
9 #include "content/browser/frame_host/navigator.h" 9 #include "content/browser/frame_host/navigator.h"
10 #include "content/browser/frame_host/render_frame_host_impl.h" 10 #include "content/browser/frame_host/render_frame_host_impl.h"
11 #include "content/browser/renderer_host/render_view_host_impl.h" 11 #include "content/browser/renderer_host/render_view_host_impl.h"
12 #include "content/browser/web_contents/web_contents_impl.h"
12 #include "content/public/test/mock_render_process_host.h" 13 #include "content/public/test/mock_render_process_host.h"
13 #include "content/public/test/test_browser_context.h" 14 #include "content/public/test/test_browser_context.h"
14 #include "content/public/test/test_browser_thread_bundle.h" 15 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "content/public/test/test_renderer_host.h" 16 #include "content/public/test/test_renderer_host.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 namespace content { 19 namespace content {
19 namespace { 20 namespace {
20 21
21 class FrameTreeTest : public RenderViewHostTestHarness { 22 class FrameTreeTest : public RenderViewHostTestHarness {
22 protected: 23 protected:
23 // Prints a FrameTree, for easy assertions of the tree hierarchy. 24 // Prints a FrameTree, for easy assertions of the tree hierarchy.
24 std::string GetTreeState(FrameTree* frame_tree) { 25 std::string GetTreeState(FrameTree* frame_tree) {
25 std::string result; 26 std::string result;
26 AppendTreeNodeState(frame_tree->root(), &result); 27 AppendTreeNodeState(frame_tree->root(), &result);
27 return result; 28 return result;
28 } 29 }
29 30
30 private: 31 private:
31 void AppendTreeNodeState(FrameTreeNode* node, std::string* result) { 32 void AppendTreeNodeState(FrameTreeNode* node, std::string* result) {
32 result->append(base::Int64ToString(node->frame_id())); 33 int frame_id = node->render_frame_host()->render_view_host()->
34 GetFrameIDForTesting(node->frame_tree_node_id());
35 result->append(base::Int64ToString(frame_id));
33 if (!node->frame_name().empty()) { 36 if (!node->frame_name().empty()) {
34 result->append(" '"); 37 result->append(" '");
35 result->append(node->frame_name()); 38 result->append(node->frame_name());
36 result->append("'"); 39 result->append("'");
37 } 40 }
38 result->append(": ["); 41 result->append(": [");
39 const char* separator = ""; 42 const char* separator = "";
40 for (size_t i = 0; i < node->child_count(); i++) { 43 for (size_t i = 0; i < node->child_count(); i++) {
41 result->append(separator); 44 result->append(separator);
42 AppendTreeNodeState(node->child_at(i), result); 45 AppendTreeNodeState(node->child_at(i), result);
43 separator = ", "; 46 separator = ", ";
44 } 47 }
45 result->append("]"); 48 result->append("]");
46 } 49 }
47 }; 50 };
48 51
49 // The root node never changes during navigation even though its 52 // The root node never changes during navigation even though its
50 // RenderFrameHost does. 53 // RenderFrameHost does.
51 // - Swapping main frame doesn't change root node. 54 // - Swapping main frame doesn't change root node.
52 // - Swapping back to NULL doesn't crash (easier tear-down for interstitials). 55 // - Swapping back to NULL doesn't crash (easier tear-down for interstitials).
53 // - Main frame does not own RenderFrameHost. 56 // - Main frame does not own RenderFrameHost.
54 TEST_F(FrameTreeTest, RootNode) { 57 // TODO(creis): Do we still want these tests? Depends on SwapMainFrame.
58 /*TEST_F(FrameTreeTest, RootNode) {
55 FrameTree frame_tree(new Navigator(NULL, NULL), NULL, NULL, NULL); 59 FrameTree frame_tree(new Navigator(NULL, NULL), NULL, NULL, NULL);
56 60
57 // Initial state has empty node. 61 // Initial state has empty node.
58 FrameTreeNode* root = frame_tree.root(); 62 FrameTreeNode* root = frame_tree.root();
59 ASSERT_TRUE(root); 63 ASSERT_TRUE(root);
60 EXPECT_FALSE(frame_tree.GetMainFrame()); 64 EXPECT_FALSE(frame_tree.GetMainFrame());
61 65
62 // Swap in main frame. 66 // Swap in main frame.
63 RenderFrameHostImpl* dummy = reinterpret_cast<RenderFrameHostImpl*>(0x1); 67 RenderFrameHostImpl* dummy = reinterpret_cast<RenderFrameHostImpl*>(0x1);
64 frame_tree.SwapMainFrame(dummy); 68 frame_tree.SwapMainFrame(dummy);
65 EXPECT_EQ(root, frame_tree.root()); 69 EXPECT_EQ(root, frame_tree.root());
66 EXPECT_EQ(dummy, frame_tree.GetMainFrame()); 70 EXPECT_EQ(dummy, frame_tree.GetMainFrame());
67 71
68 // Move back to NULL. 72 // Move back to NULL.
69 frame_tree.SwapMainFrame(NULL); 73 frame_tree.SwapMainFrame(NULL);
70 EXPECT_EQ(root, frame_tree.root()); 74 EXPECT_EQ(root, frame_tree.root());
71 EXPECT_FALSE(frame_tree.GetMainFrame()); 75 EXPECT_FALSE(frame_tree.GetMainFrame());
72 76
73 // Move back to an invalid pointer, let the FrameTree go out of scope. Test 77 // Move back to an invalid pointer, let the FrameTree go out of scope. Test
74 // should not crash because the main frame isn't owned. 78 // should not crash because the main frame isn't owned.
75 frame_tree.SwapMainFrame(dummy); 79 frame_tree.SwapMainFrame(dummy);
76 } 80 }*/
77 81
78 // Test that swapping the main frame resets the renderer-assigned frame id. 82 // Test that swapping the main frame resets the renderer-assigned frame id.
79 // - On creation, frame id is unassigned. 83 // - On creation, frame id is unassigned.
80 // - After a swap, frame id is unassigned. 84 // - After a swap, frame id is unassigned.
81 TEST_F(FrameTreeTest, FirstNavigationAfterSwap) { 85 /*TEST_F(FrameTreeTest, FirstNavigationAfterSwap) {
82 FrameTree frame_tree(new Navigator(NULL, NULL), NULL, NULL, NULL); 86 FrameTree frame_tree(new Navigator(NULL, NULL), NULL, NULL, NULL);
83 87
84 EXPECT_TRUE(frame_tree.IsFirstNavigationAfterSwap()); 88 EXPECT_TRUE(frame_tree.IsFirstNavigationAfterSwap());
85 EXPECT_EQ(FrameTreeNode::kInvalidFrameId, 89 EXPECT_EQ(FrameTreeNode::kInvalidFrameId,
86 frame_tree.root()->frame_id()); 90 frame_tree.root()->frame_id());
87 frame_tree.OnFirstNavigationAfterSwap(1); 91 frame_tree.OnFirstNavigationAfterSwap(1);
88 EXPECT_FALSE(frame_tree.IsFirstNavigationAfterSwap()); 92 EXPECT_FALSE(frame_tree.IsFirstNavigationAfterSwap());
89 EXPECT_EQ(1, frame_tree.root()->frame_id()); 93 EXPECT_EQ(1, frame_tree.root()->frame_id());
90 94
91 frame_tree.SwapMainFrame(NULL); 95 frame_tree.SwapMainFrame(NULL);
92 EXPECT_TRUE(frame_tree.IsFirstNavigationAfterSwap()); 96 EXPECT_TRUE(frame_tree.IsFirstNavigationAfterSwap());
93 EXPECT_EQ(FrameTreeNode::kInvalidFrameId, 97 EXPECT_EQ(FrameTreeNode::kInvalidFrameId,
94 frame_tree.root()->frame_id()); 98 frame_tree.root()->frame_id());
95 } 99 }*/
96 100
97 // Exercise tree manipulation routines. 101 // Exercise tree manipulation routines.
98 // - Add a series of nodes and verify tree structure. 102 // - Add a series of nodes and verify tree structure.
99 // - Remove a series of nodes and verify tree structure. 103 // - Remove a series of nodes and verify tree structure.
100 TEST_F(FrameTreeTest, Shape) { 104 TEST_F(FrameTreeTest, Shape) {
101 FrameTree frame_tree(new Navigator(NULL, NULL), NULL, NULL, NULL); 105 // Use the FrameTree of the WebContents so that it has all the delegates it
106 // needs. We may want to consider a test version of this.
107 FrameTree* frame_tree =
108 static_cast<WebContentsImpl*>(web_contents())->GetFrameTree();
109
110 // Navigate to a URL to register the frame ID.
111 NavigateAndCommit(GURL("about:blank"));
102 112
103 std::string no_children_node("no children node"); 113 std::string no_children_node("no children node");
104 std::string deep_subtree("node with deep subtree"); 114 std::string deep_subtree("node with deep subtree");
105 115
106 // Ensure the top-level node of the FrameTree is initialized by simulating a 116 // Ensure the top-level node of the FrameTree is initialized by simulating a
107 // main frame swap here. 117 // main frame swap here.
108 RenderFrameHostImpl render_frame_host(static_cast<RenderViewHostImpl*>(rvh()), 118 RenderViewHostImpl* rvhi = static_cast<RenderViewHostImpl*>(rvh());
109 &frame_tree,
110 process()->GetNextRoutingID(), false);
111 frame_tree.SwapMainFrame(&render_frame_host);
112 frame_tree.OnFirstNavigationAfterSwap(5);
113 119
114 ASSERT_EQ("5: []", GetTreeState(&frame_tree)); 120 // kFrameId in TestRenderViewHost is 13.
121 ASSERT_EQ("13: []", GetTreeState(frame_tree));
115 122
116 // Simulate attaching a series of frames to build the frame tree. 123 // Simulate attaching a series of frames to build the frame tree.
117 frame_tree.AddFrame(process()->GetNextRoutingID(), 5, 14, std::string()); 124 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 13, 14,
118 frame_tree.AddFrame(process()->GetNextRoutingID(), 5, 15, std::string()); 125 std::string());
119 frame_tree.AddFrame(process()->GetNextRoutingID(), 5, 16, std::string()); 126 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 13, 15,
127 std::string());
128 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 13, 16,
129 std::string());
120 130
121 frame_tree.AddFrame(process()->GetNextRoutingID(), 14, 244, std::string()); 131 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 14, 244,
122 frame_tree.AddFrame(process()->GetNextRoutingID(), 15, 255, no_children_node); 132 std::string());
123 frame_tree.AddFrame(process()->GetNextRoutingID(), 14, 245, std::string()); 133 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 15, 255,
134 no_children_node);
135 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 14, 245,
136 std::string());
124 137
125 ASSERT_EQ("5: [14: [244: [], 245: []], " 138 ASSERT_EQ("13: [14: [244: [], 245: []], "
126 "15: [255 'no children node': []], " 139 "15: [255 'no children node': []], "
127 "16: []]", 140 "16: []]",
128 GetTreeState(&frame_tree)); 141 GetTreeState(frame_tree));
129 142
130 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 264, std::string()); 143 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 16, 264,
131 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 265, std::string()); 144 std::string());
132 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 266, std::string()); 145 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 16, 265,
133 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 267, deep_subtree); 146 std::string());
134 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 268, std::string()); 147 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 16, 266,
148 std::string());
149 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 16, 267,
150 deep_subtree);
151 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 16, 268,
152 std::string());
135 153
136 frame_tree.AddFrame(process()->GetNextRoutingID(), 267, 365, std::string()); 154 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 267, 365,
137 frame_tree.AddFrame(process()->GetNextRoutingID(), 365, 455, std::string()); 155 std::string());
138 frame_tree.AddFrame(process()->GetNextRoutingID(), 455, 555, std::string()); 156 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 365, 455,
139 frame_tree.AddFrame(process()->GetNextRoutingID(), 555, 655, std::string()); 157 std::string());
158 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 455, 555,
159 std::string());
160 frame_tree->AddFrame(rvhi, process()->GetNextRoutingID(), 555, 655,
161 std::string());
140 162
141 // Now that's it's fully built, verify the tree structure is as expected. 163 // Now that's it's fully built, verify the tree structure is as expected.
142 ASSERT_EQ("5: [14: [244: [], 245: []], " 164 ASSERT_EQ("13: [14: [244: [], 245: []], "
143 "15: [255 'no children node': []], " 165 "15: [255 'no children node': []], "
144 "16: [264: [], 265: [], 266: [], " 166 "16: [264: [], 265: [], 266: [], "
145 "267 'node with deep subtree': " 167 "267 'node with deep subtree': "
146 "[365: [455: [555: [655: []]]]], 268: []]]", 168 "[365: [455: [555: [655: []]]]], 268: []]]",
147 GetTreeState(&frame_tree)); 169 GetTreeState(frame_tree));
148 170
149 // Test removing of nodes. 171 // Test removing of nodes.
150 frame_tree.RemoveFrame(555, 655); 172 frame_tree->RemoveFrame(rvhi->GetFrameTreeNodeID(555),
151 ASSERT_EQ("5: [14: [244: [], 245: []], " 173 rvhi->GetFrameTreeNodeID(655));
152 "15: [255 'no children node': []], " 174 ASSERT_EQ("13: [14: [244: [], 245: []], "
153 "16: [264: [], 265: [], 266: [], " 175 "15: [255 'no children node': []], "
154 "267 'node with deep subtree': " 176 "16: [264: [], 265: [], 266: [], "
155 "[365: [455: [555: []]]], 268: []]]", 177 "267 'node with deep subtree': "
156 GetTreeState(&frame_tree)); 178 "[365: [455: [555: []]]], 268: []]]",
179 GetTreeState(frame_tree));
157 180
158 frame_tree.RemoveFrame(16, 265); 181 frame_tree->RemoveFrame(rvhi->GetFrameTreeNodeID(16),
159 ASSERT_EQ("5: [14: [244: [], 245: []], " 182 rvhi->GetFrameTreeNodeID(265));
160 "15: [255 'no children node': []], " 183 ASSERT_EQ("13: [14: [244: [], 245: []], "
161 "16: [264: [], 266: [], " 184 "15: [255 'no children node': []], "
162 "267 'node with deep subtree': " 185 "16: [264: [], 266: [], "
163 "[365: [455: [555: []]]], 268: []]]", 186 "267 'node with deep subtree': "
164 GetTreeState(&frame_tree)); 187 "[365: [455: [555: []]]], 268: []]]",
188 GetTreeState(frame_tree));
165 189
166 frame_tree.RemoveFrame(5, 15); 190 frame_tree->RemoveFrame(rvhi->GetFrameTreeNodeID(13),
167 ASSERT_EQ("5: [14: [244: [], 245: []], " 191 rvhi->GetFrameTreeNodeID(15));
168 "16: [264: [], 266: [], " 192 ASSERT_EQ("13: [14: [244: [], 245: []], "
169 "267 'node with deep subtree': " 193 "16: [264: [], 266: [], "
170 "[365: [455: [555: []]]], 268: []]]", 194 "267 'node with deep subtree': "
171 GetTreeState(&frame_tree)); 195 "[365: [455: [555: []]]], 268: []]]",
196 GetTreeState(frame_tree));
172 } 197 }
173 198
174 } // namespace 199 } // namespace
175 } // namespace content 200 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698