OLD | NEW |
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" |
(...skipping 13 matching lines...) Expand all Loading... |
24 protected: | 24 protected: |
25 // Prints a FrameTree, for easy assertions of the tree hierarchy. | 25 // Prints a FrameTree, for easy assertions of the tree hierarchy. |
26 std::string GetTreeState(FrameTree* frame_tree) { | 26 std::string GetTreeState(FrameTree* frame_tree) { |
27 std::string result; | 27 std::string result; |
28 AppendTreeNodeState(frame_tree->root(), &result); | 28 AppendTreeNodeState(frame_tree->root(), &result); |
29 return result; | 29 return result; |
30 } | 30 } |
31 | 31 |
32 private: | 32 private: |
33 void AppendTreeNodeState(FrameTreeNode* node, std::string* result) { | 33 void AppendTreeNodeState(FrameTreeNode* node, std::string* result) { |
34 result->append(base::Int64ToString(node->frame_id())); | 34 result->append(base::Int64ToString( |
| 35 node->current_frame_host()->GetRoutingID())); |
35 if (!node->frame_name().empty()) { | 36 if (!node->frame_name().empty()) { |
36 result->append(" '"); | 37 result->append(" '"); |
37 result->append(node->frame_name()); | 38 result->append(node->frame_name()); |
38 result->append("'"); | 39 result->append("'"); |
39 } | 40 } |
40 result->append(": ["); | 41 result->append(": ["); |
41 const char* separator = ""; | 42 const char* separator = ""; |
42 for (size_t i = 0; i < node->child_count(); i++) { | 43 for (size_t i = 0; i < node->child_count(); i++) { |
43 result->append(separator); | 44 result->append(separator); |
44 AppendTreeNodeState(node->child_at(i), result); | 45 AppendTreeNodeState(node->child_at(i), result); |
45 separator = ", "; | 46 separator = ", "; |
46 } | 47 } |
47 result->append("]"); | 48 result->append("]"); |
48 } | 49 } |
49 }; | 50 }; |
50 | 51 |
51 // Test that swapping the main frame resets the renderer-assigned frame id. | |
52 // - On creation, frame id is unassigned. | |
53 // - After a swap, frame id is unassigned. | |
54 TEST_F(FrameTreeTest, FirstNavigationAfterSwap) { | |
55 FrameTree frame_tree(new NavigatorImpl(NULL, NULL), NULL, NULL, NULL, NULL); | |
56 | |
57 EXPECT_TRUE(frame_tree.IsFirstNavigationAfterSwap()); | |
58 EXPECT_EQ(FrameTreeNode::kInvalidFrameId, | |
59 frame_tree.root()->frame_id()); | |
60 frame_tree.OnFirstNavigationAfterSwap(1); | |
61 EXPECT_FALSE(frame_tree.IsFirstNavigationAfterSwap()); | |
62 EXPECT_EQ(1, frame_tree.root()->frame_id()); | |
63 | |
64 frame_tree.ResetForMainFrameSwap(); | |
65 EXPECT_TRUE(frame_tree.IsFirstNavigationAfterSwap()); | |
66 EXPECT_EQ(FrameTreeNode::kInvalidFrameId, | |
67 frame_tree.root()->frame_id()); | |
68 } | |
69 | |
70 // Exercise tree manipulation routines. | 52 // Exercise tree manipulation routines. |
71 // - Add a series of nodes and verify tree structure. | 53 // - Add a series of nodes and verify tree structure. |
72 // - Remove a series of nodes and verify tree structure. | 54 // - Remove a series of nodes and verify tree structure. |
73 TEST_F(FrameTreeTest, Shape) { | 55 TEST_F(FrameTreeTest, Shape) { |
74 // Use the FrameTree of the WebContents so that it has all the delegates it | 56 // 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. | 57 // needs. We may want to consider a test version of this. |
76 FrameTree* frame_tree = | 58 FrameTree* frame_tree = |
77 static_cast<WebContentsImpl*>(web_contents())->GetFrameTree(); | 59 static_cast<WebContentsImpl*>(web_contents())->GetFrameTree(); |
| 60 FrameTreeNode* root = frame_tree->root(); |
78 | 61 |
79 std::string no_children_node("no children node"); | 62 std::string no_children_node("no children node"); |
80 std::string deep_subtree("node with deep subtree"); | 63 std::string deep_subtree("node with deep subtree"); |
81 | 64 |
82 frame_tree->OnFirstNavigationAfterSwap(5); | 65 ASSERT_EQ("1: []", GetTreeState(frame_tree)); |
83 | |
84 ASSERT_EQ("5: []", GetTreeState(frame_tree)); | |
85 | 66 |
86 // Simulate attaching a series of frames to build the frame tree. | 67 // Simulate attaching a series of frames to build the frame tree. |
87 frame_tree->AddFrame(process()->GetNextRoutingID(), 5, 14, std::string()); | 68 frame_tree->AddFrame(root, 14, std::string()); |
88 frame_tree->AddFrame(process()->GetNextRoutingID(), 5, 15, std::string()); | 69 frame_tree->AddFrame(root, 15, std::string()); |
89 frame_tree->AddFrame(process()->GetNextRoutingID(), 5, 16, std::string()); | 70 frame_tree->AddFrame(root, 16, std::string()); |
90 | 71 |
91 frame_tree->AddFrame(process()->GetNextRoutingID(), 14, 244, std::string()); | 72 frame_tree->AddFrame(root->child_at(0), 244, std::string()); |
92 frame_tree->AddFrame(process()->GetNextRoutingID(), 15, 255, | 73 frame_tree->AddFrame(root->child_at(1), 255, no_children_node); |
93 no_children_node); | 74 frame_tree->AddFrame(root->child_at(0), 245, std::string()); |
94 frame_tree->AddFrame(process()->GetNextRoutingID(), 14, 245, std::string()); | |
95 | 75 |
96 ASSERT_EQ("5: [14: [244: [], 245: []], " | 76 ASSERT_EQ("1: [14: [244: [], 245: []], " |
97 "15: [255 'no children node': []], " | 77 "15: [255 'no children node': []], " |
98 "16: []]", | 78 "16: []]", |
99 GetTreeState(frame_tree)); | 79 GetTreeState(frame_tree)); |
100 | 80 |
101 frame_tree->AddFrame(process()->GetNextRoutingID(), 16, 264, std::string()); | 81 FrameTreeNode* child_16 = root->child_at(2); |
102 frame_tree->AddFrame(process()->GetNextRoutingID(), 16, 265, std::string()); | 82 frame_tree->AddFrame(child_16, 264, std::string()); |
103 frame_tree->AddFrame(process()->GetNextRoutingID(), 16, 266, std::string()); | 83 frame_tree->AddFrame(child_16, 265, std::string()); |
104 frame_tree->AddFrame(process()->GetNextRoutingID(), 16, 267, deep_subtree); | 84 frame_tree->AddFrame(child_16, 266, std::string()); |
105 frame_tree->AddFrame(process()->GetNextRoutingID(), 16, 268, std::string()); | 85 frame_tree->AddFrame(child_16, 267, deep_subtree); |
| 86 frame_tree->AddFrame(child_16, 268, std::string()); |
106 | 87 |
107 frame_tree->AddFrame(process()->GetNextRoutingID(), 267, 365, std::string()); | 88 FrameTreeNode* child_267 = child_16->child_at(3); |
108 frame_tree->AddFrame(process()->GetNextRoutingID(), 365, 455, std::string()); | 89 frame_tree->AddFrame(child_267, 365, std::string()); |
109 frame_tree->AddFrame(process()->GetNextRoutingID(), 455, 555, std::string()); | 90 frame_tree->AddFrame(child_267->child_at(0), 455, std::string()); |
110 frame_tree->AddFrame(process()->GetNextRoutingID(), 555, 655, std::string()); | 91 frame_tree->AddFrame(child_267->child_at(0)->child_at(0), 555, std::string()); |
| 92 frame_tree->AddFrame(child_267->child_at(0)->child_at(0)->child_at(0), 655, |
| 93 std::string()); |
111 | 94 |
112 // Now that's it's fully built, verify the tree structure is as expected. | 95 // Now that's it's fully built, verify the tree structure is as expected. |
113 ASSERT_EQ("5: [14: [244: [], 245: []], " | 96 ASSERT_EQ("1: [14: [244: [], 245: []], " |
114 "15: [255 'no children node': []], " | 97 "15: [255 'no children node': []], " |
115 "16: [264: [], 265: [], 266: [], " | 98 "16: [264: [], 265: [], 266: [], " |
116 "267 'node with deep subtree': " | 99 "267 'node with deep subtree': " |
117 "[365: [455: [555: [655: []]]]], 268: []]]", | 100 "[365: [455: [555: [655: []]]]], 268: []]]", |
118 GetTreeState(frame_tree)); | 101 GetTreeState(frame_tree)); |
119 | 102 |
120 // Test removing of nodes. Clear the frame removal listener so we can pass a | 103 FrameTreeNode* child_555 = child_267->child_at(0)->child_at(0)->child_at(0); |
121 // NULL RFH here. | 104 frame_tree->RemoveFrame(child_555); |
122 frame_tree->ClearFrameRemoveListenerForTesting(); | 105 ASSERT_EQ("1: [14: [244: [], 245: []], " |
123 frame_tree->RemoveFrame(NULL, 555, 655); | |
124 ASSERT_EQ("5: [14: [244: [], 245: []], " | |
125 "15: [255 'no children node': []], " | 106 "15: [255 'no children node': []], " |
126 "16: [264: [], 265: [], 266: [], " | 107 "16: [264: [], 265: [], 266: [], " |
127 "267 'node with deep subtree': " | 108 "267 'node with deep subtree': " |
128 "[365: [455: [555: []]]], 268: []]]", | 109 "[365: [455: []]], 268: []]]", |
129 GetTreeState(frame_tree)); | 110 GetTreeState(frame_tree)); |
130 | 111 |
131 frame_tree->RemoveFrame(NULL, 16, 265); | 112 frame_tree->RemoveFrame(child_16->child_at(1)); |
132 ASSERT_EQ("5: [14: [244: [], 245: []], " | 113 ASSERT_EQ("1: [14: [244: [], 245: []], " |
133 "15: [255 'no children node': []], " | 114 "15: [255 'no children node': []], " |
134 "16: [264: [], 266: [], " | 115 "16: [264: [], 266: [], " |
135 "267 'node with deep subtree': " | 116 "267 'node with deep subtree': " |
136 "[365: [455: [555: []]]], 268: []]]", | 117 "[365: [455: []]], 268: []]]", |
137 GetTreeState(frame_tree)); | 118 GetTreeState(frame_tree)); |
138 | 119 |
139 frame_tree->RemoveFrame(NULL, 5, 15); | 120 frame_tree->RemoveFrame(root->child_at(1)); |
140 ASSERT_EQ("5: [14: [244: [], 245: []], " | 121 ASSERT_EQ("1: [14: [244: [], 245: []], " |
141 "16: [264: [], 266: [], " | 122 "16: [264: [], 266: [], " |
142 "267 'node with deep subtree': " | 123 "267 'node with deep subtree': " |
143 "[365: [455: [555: []]]], 268: []]]", | 124 "[365: [455: []]], 268: []]]", |
144 GetTreeState(frame_tree)); | 125 GetTreeState(frame_tree)); |
145 } | 126 } |
146 | 127 |
147 } // namespace | 128 } // namespace |
148 } // namespace content | 129 } // namespace content |
OLD | NEW |