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

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

Issue 117693002: Make RenderFrameHostManager swap RenderFrameHosts, not RenderViewHosts. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years 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_impl.h" 9 #include "content/browser/frame_host/navigator_impl.h"
10 #include "content/browser/frame_host/render_frame_host_factory.h" 10 #include "content/browser/frame_host/render_frame_host_factory.h"
11 #include "content/browser/frame_host/render_frame_host_impl.h" 11 #include "content/browser/frame_host/render_frame_host_impl.h"
12 #include "content/browser/renderer_host/render_view_host_impl.h" 12 #include "content/browser/renderer_host/render_view_host_impl.h"
13 #include "content/browser/web_contents/web_contents_impl.h"
13 #include "content/public/test/mock_render_process_host.h" 14 #include "content/public/test/mock_render_process_host.h"
14 #include "content/public/test/test_browser_context.h" 15 #include "content/public/test/test_browser_context.h"
15 #include "content/public/test/test_browser_thread_bundle.h" 16 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "content/public/test/test_renderer_host.h" 17 #include "content/public/test/test_renderer_host.h"
17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
18 19
19 namespace content { 20 namespace content {
20 namespace { 21 namespace {
21 22
22 class FrameTreeTest : public RenderViewHostTestHarness { 23 class FrameTreeTest : public RenderViewHostTestHarness {
(...skipping 17 matching lines...) Expand all
40 const char* separator = ""; 41 const char* separator = "";
41 for (size_t i = 0; i < node->child_count(); i++) { 42 for (size_t i = 0; i < node->child_count(); i++) {
42 result->append(separator); 43 result->append(separator);
43 AppendTreeNodeState(node->child_at(i), result); 44 AppendTreeNodeState(node->child_at(i), result);
44 separator = ", "; 45 separator = ", ";
45 } 46 }
46 result->append("]"); 47 result->append("]");
47 } 48 }
48 }; 49 };
49 50
50 // The root node never changes during navigation even though its
51 // RenderFrameHost does.
52 // - Swapping main frame doesn't change root node.
53 // - Swapping back to NULL doesn't crash (easier tear-down for interstitials).
54 // - Main frame does not own RenderFrameHost.
55 TEST_F(FrameTreeTest, RootNode) {
56 FrameTree frame_tree(new NavigatorImpl(NULL, NULL), NULL, NULL, NULL, NULL);
57
58 // Initial state has empty node.
59 FrameTreeNode* root = frame_tree.root();
60 ASSERT_TRUE(root);
61 EXPECT_FALSE(frame_tree.GetMainFrame());
62
63 // Swap in main frame.
64 RenderFrameHostImpl* dummy = reinterpret_cast<RenderFrameHostImpl*>(0x1);
65 frame_tree.SwapMainFrame(dummy);
66 EXPECT_EQ(root, frame_tree.root());
67 EXPECT_EQ(dummy, frame_tree.GetMainFrame());
68
69 // Move back to NULL.
70 frame_tree.SwapMainFrame(NULL);
71 EXPECT_EQ(root, frame_tree.root());
72 EXPECT_FALSE(frame_tree.GetMainFrame());
73
74 // Move back to an invalid pointer, let the FrameTree go out of scope. Test
75 // should not crash because the main frame isn't owned.
76 frame_tree.SwapMainFrame(dummy);
77 }
78
79 // Test that swapping the main frame resets the renderer-assigned frame id. 51 // Test that swapping the main frame resets the renderer-assigned frame id.
80 // - On creation, frame id is unassigned. 52 // - On creation, frame id is unassigned.
81 // - After a swap, frame id is unassigned. 53 // - After a swap, frame id is unassigned.
82 TEST_F(FrameTreeTest, FirstNavigationAfterSwap) { 54 TEST_F(FrameTreeTest, FirstNavigationAfterSwap) {
83 FrameTree frame_tree(new NavigatorImpl(NULL, NULL), NULL, NULL, NULL, NULL); 55 FrameTree frame_tree(new NavigatorImpl(NULL, NULL), NULL, NULL, NULL, NULL);
84 56
85 EXPECT_TRUE(frame_tree.IsFirstNavigationAfterSwap()); 57 EXPECT_TRUE(frame_tree.IsFirstNavigationAfterSwap());
86 EXPECT_EQ(FrameTreeNode::kInvalidFrameId, 58 EXPECT_EQ(FrameTreeNode::kInvalidFrameId,
87 frame_tree.root()->frame_id()); 59 frame_tree.root()->frame_id());
88 frame_tree.OnFirstNavigationAfterSwap(1); 60 frame_tree.OnFirstNavigationAfterSwap(1);
89 EXPECT_FALSE(frame_tree.IsFirstNavigationAfterSwap()); 61 EXPECT_FALSE(frame_tree.IsFirstNavigationAfterSwap());
90 EXPECT_EQ(1, frame_tree.root()->frame_id()); 62 EXPECT_EQ(1, frame_tree.root()->frame_id());
91 63
92 frame_tree.SwapMainFrame(NULL); 64 frame_tree.ResetForMainFrameSwap();
93 EXPECT_TRUE(frame_tree.IsFirstNavigationAfterSwap()); 65 EXPECT_TRUE(frame_tree.IsFirstNavigationAfterSwap());
94 EXPECT_EQ(FrameTreeNode::kInvalidFrameId, 66 EXPECT_EQ(FrameTreeNode::kInvalidFrameId,
95 frame_tree.root()->frame_id()); 67 frame_tree.root()->frame_id());
96 } 68 }
97 69
98 // Exercise tree manipulation routines. 70 // Exercise tree manipulation routines.
99 // - Add a series of nodes and verify tree structure. 71 // - Add a series of nodes and verify tree structure.
100 // - Remove a series of nodes and verify tree structure. 72 // - Remove a series of nodes and verify tree structure.
101 TEST_F(FrameTreeTest, Shape) { 73 TEST_F(FrameTreeTest, Shape) {
102 FrameTree frame_tree(new NavigatorImpl(NULL, NULL), NULL, NULL, NULL, NULL); 74 // Use the FrameTree of the WebContents so that it has all the delegates it
75 // needs. We may want to consider a test version of this.
76 FrameTree* frame_tree =
77 static_cast<WebContentsImpl*>(web_contents())->GetFrameTree();
103 78
104 std::string no_children_node("no children node"); 79 std::string no_children_node("no children node");
105 std::string deep_subtree("node with deep subtree"); 80 std::string deep_subtree("node with deep subtree");
106 81
107 // Ensure the top-level node of the FrameTree is initialized by simulating a 82 frame_tree->OnFirstNavigationAfterSwap(5);
108 // main frame swap here.
109 scoped_ptr<RenderFrameHostImpl> render_frame_host =
110 RenderFrameHostFactory::Create(static_cast<RenderViewHostImpl*>(rvh()),
111 NULL,
112 &frame_tree,
113 frame_tree.root(),
114 process()->GetNextRoutingID(),
115 false);
116 frame_tree.SwapMainFrame(render_frame_host.get());
117 frame_tree.OnFirstNavigationAfterSwap(5);
118 83
119 ASSERT_EQ("5: []", GetTreeState(&frame_tree)); 84 ASSERT_EQ("5: []", GetTreeState(frame_tree));
120 85
121 // Simulate attaching a series of frames to build the frame tree. 86 // Simulate attaching a series of frames to build the frame tree.
122 frame_tree.AddFrame(process()->GetNextRoutingID(), 5, 14, std::string()); 87 frame_tree->AddFrame(process()->GetNextRoutingID(), 5, 14, std::string());
123 frame_tree.AddFrame(process()->GetNextRoutingID(), 5, 15, std::string()); 88 frame_tree->AddFrame(process()->GetNextRoutingID(), 5, 15, std::string());
124 frame_tree.AddFrame(process()->GetNextRoutingID(), 5, 16, std::string()); 89 frame_tree->AddFrame(process()->GetNextRoutingID(), 5, 16, std::string());
125 90
126 frame_tree.AddFrame(process()->GetNextRoutingID(), 14, 244, std::string()); 91 frame_tree->AddFrame(process()->GetNextRoutingID(), 14, 244, std::string());
127 frame_tree.AddFrame(process()->GetNextRoutingID(), 15, 255, no_children_node); 92 frame_tree->AddFrame(process()->GetNextRoutingID(), 15, 255,
128 frame_tree.AddFrame(process()->GetNextRoutingID(), 14, 245, std::string()); 93 no_children_node);
94 frame_tree->AddFrame(process()->GetNextRoutingID(), 14, 245, std::string());
129 95
130 ASSERT_EQ("5: [14: [244: [], 245: []], " 96 ASSERT_EQ("5: [14: [244: [], 245: []], "
131 "15: [255 'no children node': []], " 97 "15: [255 'no children node': []], "
132 "16: []]", 98 "16: []]",
133 GetTreeState(&frame_tree)); 99 GetTreeState(frame_tree));
134 100
135 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 264, std::string()); 101 frame_tree->AddFrame(process()->GetNextRoutingID(), 16, 264, std::string());
136 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 265, std::string()); 102 frame_tree->AddFrame(process()->GetNextRoutingID(), 16, 265, std::string());
137 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 266, std::string()); 103 frame_tree->AddFrame(process()->GetNextRoutingID(), 16, 266, std::string());
138 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 267, deep_subtree); 104 frame_tree->AddFrame(process()->GetNextRoutingID(), 16, 267, deep_subtree);
139 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 268, std::string()); 105 frame_tree->AddFrame(process()->GetNextRoutingID(), 16, 268, std::string());
140 106
141 frame_tree.AddFrame(process()->GetNextRoutingID(), 267, 365, std::string()); 107 frame_tree->AddFrame(process()->GetNextRoutingID(), 267, 365, std::string());
142 frame_tree.AddFrame(process()->GetNextRoutingID(), 365, 455, std::string()); 108 frame_tree->AddFrame(process()->GetNextRoutingID(), 365, 455, std::string());
143 frame_tree.AddFrame(process()->GetNextRoutingID(), 455, 555, std::string()); 109 frame_tree->AddFrame(process()->GetNextRoutingID(), 455, 555, std::string());
144 frame_tree.AddFrame(process()->GetNextRoutingID(), 555, 655, std::string()); 110 frame_tree->AddFrame(process()->GetNextRoutingID(), 555, 655, std::string());
145 111
146 // Now that's it's fully built, verify the tree structure is as expected. 112 // Now that's it's fully built, verify the tree structure is as expected.
147 ASSERT_EQ("5: [14: [244: [], 245: []], " 113 ASSERT_EQ("5: [14: [244: [], 245: []], "
148 "15: [255 'no children node': []], " 114 "15: [255 'no children node': []], "
149 "16: [264: [], 265: [], 266: [], " 115 "16: [264: [], 265: [], 266: [], "
150 "267 'node with deep subtree': " 116 "267 'node with deep subtree': "
151 "[365: [455: [555: [655: []]]]], 268: []]]", 117 "[365: [455: [555: [655: []]]]], 268: []]]",
152 GetTreeState(&frame_tree)); 118 GetTreeState(frame_tree));
153 119
154 // Test removing of nodes. 120 // Test removing of nodes. Clear the frame removal listener so we can pass a
155 frame_tree.RemoveFrame(NULL, 555, 655); 121 // NULL RFH here.
122 frame_tree->ClearFrameRemoveListenerForTesting();
123 frame_tree->RemoveFrame(NULL, 555, 655);
156 ASSERT_EQ("5: [14: [244: [], 245: []], " 124 ASSERT_EQ("5: [14: [244: [], 245: []], "
157 "15: [255 'no children node': []], " 125 "15: [255 'no children node': []], "
158 "16: [264: [], 265: [], 266: [], " 126 "16: [264: [], 265: [], 266: [], "
159 "267 'node with deep subtree': " 127 "267 'node with deep subtree': "
160 "[365: [455: [555: []]]], 268: []]]", 128 "[365: [455: [555: []]]], 268: []]]",
161 GetTreeState(&frame_tree)); 129 GetTreeState(frame_tree));
162 130
163 frame_tree.RemoveFrame(NULL, 16, 265); 131 frame_tree->RemoveFrame(NULL, 16, 265);
164 ASSERT_EQ("5: [14: [244: [], 245: []], " 132 ASSERT_EQ("5: [14: [244: [], 245: []], "
165 "15: [255 'no children node': []], " 133 "15: [255 'no children node': []], "
166 "16: [264: [], 266: [], " 134 "16: [264: [], 266: [], "
167 "267 'node with deep subtree': " 135 "267 'node with deep subtree': "
168 "[365: [455: [555: []]]], 268: []]]", 136 "[365: [455: [555: []]]], 268: []]]",
169 GetTreeState(&frame_tree)); 137 GetTreeState(frame_tree));
170 138
171 frame_tree.RemoveFrame(NULL, 5, 15); 139 frame_tree->RemoveFrame(NULL, 5, 15);
172 ASSERT_EQ("5: [14: [244: [], 245: []], " 140 ASSERT_EQ("5: [14: [244: [], 245: []], "
173 "16: [264: [], 266: [], " 141 "16: [264: [], 266: [], "
174 "267 'node with deep subtree': " 142 "267 'node with deep subtree': "
175 "[365: [455: [555: []]]], 268: []]]", 143 "[365: [455: [555: []]]], 268: []]]",
176 GetTreeState(&frame_tree)); 144 GetTreeState(frame_tree));
177 } 145 }
178 146
179 } // namespace 147 } // namespace
180 } // namespace content 148 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698