OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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/renderer_host/frame_tree.h" |
| 6 |
| 7 #include "base/run_loop.h" |
| 8 #include "content/browser/renderer_host/render_frame_host_impl.h" |
| 9 #include "content/public/test/mock_render_process_host.h" |
| 10 #include "content/public/test/test_browser_context.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "content/public/test/test_renderer_host.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace content { |
| 16 namespace { |
| 17 |
| 18 class FrameTreeTest : public RenderViewHostTestHarness { |
| 19 }; |
| 20 |
| 21 // This test asserts the shape of the frame tree is correct, based on incoming |
| 22 // frame attached/detached messages. |
| 23 TEST_F(FrameTreeTest, FrameTreeShape) { |
| 24 FrameTree frame_tree; |
| 25 std::string no_children_node("no children node"); |
| 26 std::string deep_subtree("node with deep subtree"); |
| 27 |
| 28 // Ensure the top-level node of the FrameTree is initalized by simulating a |
| 29 // main frame swap here. |
| 30 RenderFrameHostImpl render_frame_host(process(), &frame_tree, 5, false); |
| 31 frame_tree.SwapMainFrame(&render_frame_host); |
| 32 frame_tree.OnFirstNavigationAfterSwap(5); |
| 33 |
| 34 // Let's send a series of messages for frame attached and build the |
| 35 // frame tree. |
| 36 frame_tree.AddFrame(14, 5, 14, std::string()); |
| 37 frame_tree.AddFrame(15, 5, 15, std::string()); |
| 38 frame_tree.AddFrame(16, 5, 16, std::string()); |
| 39 |
| 40 frame_tree.AddFrame(244, 14, 244, std::string()); |
| 41 frame_tree.AddFrame(245, 14, 245, std::string()); |
| 42 |
| 43 frame_tree.AddFrame(255, 15, 255, no_children_node); |
| 44 |
| 45 frame_tree.AddFrame(264, 16, 264, std::string()); |
| 46 frame_tree.AddFrame(265, 16, 265, std::string()); |
| 47 frame_tree.AddFrame(266, 16, 266, std::string()); |
| 48 frame_tree.AddFrame(267, 16, 267, deep_subtree); |
| 49 frame_tree.AddFrame(268, 16, 268, std::string()); |
| 50 |
| 51 frame_tree.AddFrame(365, 267, 365, std::string()); |
| 52 frame_tree.AddFrame(455, 365, 455, std::string()); |
| 53 frame_tree.AddFrame(555, 455, 555, std::string()); |
| 54 frame_tree.AddFrame(655, 555, 655, std::string()); |
| 55 |
| 56 // Now, verify the tree structure is as expected. |
| 57 FrameTreeNode* root = frame_tree.GetRootForTesting(); |
| 58 EXPECT_EQ(5, root->frame_id()); |
| 59 EXPECT_EQ(3UL, root->child_count()); |
| 60 |
| 61 EXPECT_EQ(2UL, root->child_at(0)->child_count()); |
| 62 EXPECT_EQ(0UL, root->child_at(0)->child_at(0)->child_count()); |
| 63 EXPECT_EQ(0UL, root->child_at(0)->child_at(1)->child_count()); |
| 64 |
| 65 EXPECT_EQ(1UL, root->child_at(1)->child_count()); |
| 66 EXPECT_EQ(0UL, root->child_at(1)->child_at(0)->child_count()); |
| 67 EXPECT_STREQ(no_children_node.c_str(), |
| 68 root->child_at(1)->child_at(0)->frame_name().c_str()); |
| 69 |
| 70 EXPECT_EQ(5UL, root->child_at(2)->child_count()); |
| 71 EXPECT_EQ(0UL, root->child_at(2)->child_at(0)->child_count()); |
| 72 EXPECT_EQ(0UL, root->child_at(2)->child_at(1)->child_count()); |
| 73 EXPECT_EQ(0UL, root->child_at(2)->child_at(2)->child_count()); |
| 74 EXPECT_EQ(1UL, root->child_at(2)->child_at(3)->child_count()); |
| 75 EXPECT_STREQ(deep_subtree.c_str(), |
| 76 root->child_at(2)->child_at(3)->frame_name().c_str()); |
| 77 EXPECT_EQ(0UL, root->child_at(2)->child_at(4)->child_count()); |
| 78 |
| 79 FrameTreeNode* deep_tree = root->child_at(2)->child_at(3)->child_at(0); |
| 80 EXPECT_EQ(365, deep_tree->frame_id()); |
| 81 EXPECT_EQ(1UL, deep_tree->child_count()); |
| 82 EXPECT_EQ(455, deep_tree->child_at(0)->frame_id()); |
| 83 EXPECT_EQ(1UL, deep_tree->child_at(0)->child_count()); |
| 84 EXPECT_EQ(555, deep_tree->child_at(0)->child_at(0)->frame_id()); |
| 85 EXPECT_EQ(1UL, deep_tree->child_at(0)->child_at(0)->child_count()); |
| 86 EXPECT_EQ(655, deep_tree->child_at(0)->child_at(0)->child_at(0)->frame_id()); |
| 87 EXPECT_EQ(0UL, |
| 88 deep_tree->child_at(0)->child_at(0)->child_at(0)->child_count()); |
| 89 |
| 90 // Test removing of nodes. |
| 91 frame_tree.RemoveFrame(555, 655); |
| 92 EXPECT_EQ(0UL, deep_tree->child_at(0)->child_at(0)->child_count()); |
| 93 |
| 94 frame_tree.RemoveFrame(16, 265); |
| 95 EXPECT_EQ(4UL, root->child_at(2)->child_count()); |
| 96 |
| 97 frame_tree.RemoveFrame(5, 15); |
| 98 EXPECT_EQ(2UL, root->child_count()); |
| 99 |
| 100 // Drop the main frame to avoid a UaF. |
| 101 frame_tree.SwapMainFrame(NULL); |
| 102 } |
| 103 |
| 104 } // namespace |
| 105 } // namespace content |
OLD | NEW |