Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / content / browser / frame_host / frame_tree_unittest.cc
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
5 #include "content/browser/frame_host/frame_tree.h"
6
7 #include "base/run_loop.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "content/browser/frame_host/navigator_impl.h"
10 #include "content/browser/frame_host/render_frame_host_factory.h"
11 #include "content/browser/frame_host/render_frame_host_impl.h"
12 #include "content/browser/renderer_host/render_view_host_impl.h"
13 #include "content/browser/web_contents/web_contents_impl.h"
14 #include "content/public/test/mock_render_process_host.h"
15 #include "content/public/test/test_browser_context.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "content/public/test/test_renderer_host.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace content {
21 namespace {
22
23 class FrameTreeTest : public RenderViewHostTestHarness {
24  protected:
25   // Prints a FrameTree, for easy assertions of the tree hierarchy.
26   std::string GetTreeState(FrameTree* frame_tree) {
27     std::string result;
28     AppendTreeNodeState(frame_tree->root(), &result);
29     return result;
30   }
31
32  private:
33   void AppendTreeNodeState(FrameTreeNode* node, std::string* result) {
34     result->append(base::Int64ToString(
35         node->current_frame_host()->GetRoutingID()));
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
52 // Exercise tree manipulation routines.
53 //  - Add a series of nodes and verify tree structure.
54 //  - Remove a series of nodes and verify tree structure.
55 TEST_F(FrameTreeTest, Shape) {
56   // Use the FrameTree of the WebContents so that it has all the delegates it
57   // needs.  We may want to consider a test version of this.
58   FrameTree* frame_tree =
59       static_cast<WebContentsImpl*>(web_contents())->GetFrameTree();
60   FrameTreeNode* root = frame_tree->root();
61
62   std::string no_children_node("no children node");
63   std::string deep_subtree("node with deep subtree");
64
65   ASSERT_EQ("1: []", GetTreeState(frame_tree));
66
67   // Simulate attaching a series of frames to build the frame tree.
68   frame_tree->AddFrame(root, 14, std::string());
69   frame_tree->AddFrame(root, 15, std::string());
70   frame_tree->AddFrame(root, 16, std::string());
71
72   frame_tree->AddFrame(root->child_at(0), 244, std::string());
73   frame_tree->AddFrame(root->child_at(1), 255, no_children_node);
74   frame_tree->AddFrame(root->child_at(0), 245, std::string());
75
76   ASSERT_EQ("1: [14: [244: [], 245: []], "
77                 "15: [255 'no children node': []], "
78                 "16: []]",
79             GetTreeState(frame_tree));
80
81   FrameTreeNode* child_16 = root->child_at(2);
82   frame_tree->AddFrame(child_16, 264, std::string());
83   frame_tree->AddFrame(child_16, 265, std::string());
84   frame_tree->AddFrame(child_16, 266, std::string());
85   frame_tree->AddFrame(child_16, 267, deep_subtree);
86   frame_tree->AddFrame(child_16, 268, std::string());
87
88   FrameTreeNode* child_267 = child_16->child_at(3);
89   frame_tree->AddFrame(child_267, 365, std::string());
90   frame_tree->AddFrame(child_267->child_at(0), 455, 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());
94
95   // Now that's it's fully built, verify the tree structure is as expected.
96   ASSERT_EQ("1: [14: [244: [], 245: []], "
97                 "15: [255 'no children node': []], "
98                 "16: [264: [], 265: [], 266: [], "
99                      "267 'node with deep subtree': "
100                          "[365: [455: [555: [655: []]]]], 268: []]]",
101             GetTreeState(frame_tree));
102
103   FrameTreeNode* child_555 = child_267->child_at(0)->child_at(0)->child_at(0);
104   frame_tree->RemoveFrame(child_555);
105   ASSERT_EQ("1: [14: [244: [], 245: []], "
106                 "15: [255 'no children node': []], "
107                 "16: [264: [], 265: [], 266: [], "
108                      "267 'node with deep subtree': "
109                          "[365: [455: []]], 268: []]]",
110             GetTreeState(frame_tree));
111
112   frame_tree->RemoveFrame(child_16->child_at(1));
113   ASSERT_EQ("1: [14: [244: [], 245: []], "
114                 "15: [255 'no children node': []], "
115                 "16: [264: [], 266: [], "
116                      "267 'node with deep subtree': "
117                          "[365: [455: []]], 268: []]]",
118             GetTreeState(frame_tree));
119
120   frame_tree->RemoveFrame(root->child_at(1));
121   ASSERT_EQ("1: [14: [244: [], 245: []], "
122                 "16: [264: [], 266: [], "
123                      "267 'node with deep subtree': "
124                          "[365: [455: []]], 268: []]]",
125             GetTreeState(frame_tree));
126 }
127
128 }  // namespace
129 }  // namespace content