Chromium Code Reviews| 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 // The root node never changes during navigation even though its | |
| 22 // RenderFrameHost does. | |
| 23 // - Swapping main frame doesn't change root node. | |
| 24 // - Swapping back to NULL doesn't crash (easier tear-down). | |
| 25 // - Main frame does not own RenderFrameHost. | |
| 26 TEST_F(FrameTreeTest, RootNode) { | |
| 27 FrameTree frame_tree; | |
| 28 | |
| 29 // Initial state has empty node. | |
| 30 FrameTreeNode* root = frame_tree.GetRootForTesting(); | |
| 31 ASSERT_TRUE(root); | |
| 32 EXPECT_FALSE(frame_tree.GetMainFrame()); | |
| 33 | |
| 34 // Swap in main frame. | |
| 35 RenderFrameHostImpl* dummy = reinterpret_cast<RenderFrameHostImpl*>(0x1); | |
| 36 frame_tree.SwapMainFrame(dummy); | |
| 37 EXPECT_EQ(root, frame_tree.GetRootForTesting()); | |
| 38 EXPECT_EQ(dummy, frame_tree.GetMainFrame()); | |
| 39 | |
| 40 // Move back to NULL. | |
|
Charlie Reis
2013/09/27 19:19:21
It's not immediately clear why we need to support
awong
2013/09/27 20:50:09
It's in the top level comment.
| |
| 41 frame_tree.SwapMainFrame(NULL); | |
| 42 EXPECT_EQ(root, frame_tree.GetRootForTesting()); | |
| 43 EXPECT_FALSE(frame_tree.GetMainFrame()); | |
| 44 | |
| 45 // Move back to an invalid pointer, let the FrameTree go out of scope. Test | |
| 46 // should not crash because the main frame isn't owned. | |
| 47 frame_tree.SwapMainFrame(dummy); | |
| 48 } | |
| 49 | |
| 50 // Test swapping of main frame resets the render-assgined frame id. | |
|
Charlie Reis
2013/09/27 19:19:21
nit: Test that swapping the main frame
(hard to re
awong
2013/09/27 20:50:09
Done.
| |
| 51 // - On creation, frame id is unassigned. | |
| 52 // - After a swap, frame id is unassigned. | |
| 53 TEST_F(FrameTreeTest, FirstNavigationAfterSwap) { | |
| 54 FrameTree frame_tree; | |
| 55 | |
| 56 ASSERT_TRUE(frame_tree.IsFirstNavigationAfterSwap()); | |
|
Charlie Reis
2013/09/27 19:19:21
These should use EXPECT, right? (ASSERT is for th
awong
2013/09/27 20:50:09
Good catch. done.
| |
| 57 frame_tree.OnFirstNavigationAfterSwap(1); | |
| 58 ASSERT_FALSE(frame_tree.IsFirstNavigationAfterSwap()); | |
| 59 | |
| 60 frame_tree.SwapMainFrame(NULL); | |
| 61 ASSERT_TRUE(frame_tree.IsFirstNavigationAfterSwap()); | |
|
Charlie Reis
2013/09/27 19:19:21
We haven't actually tested anything about the fram
awong
2013/09/27 20:50:09
Done.
| |
| 62 } | |
| 63 | |
| 64 // Exercise tree manipulation routines. | |
| 65 // - Add a series of nodes and verify tree structure. | |
|
Charlie Reis
2013/09/27 19:19:21
nit: One less indent space, like the comments abov
awong
2013/09/27 20:50:09
Done.
| |
| 66 // - Remove a series of nodes and verify tree structure. | |
| 67 TEST_F(FrameTreeTest, Shape) { | |
| 68 FrameTree frame_tree; | |
| 69 std::string no_children_node("no children node"); | |
| 70 std::string deep_subtree("node with deep subtree"); | |
| 71 | |
| 72 // Ensure the top-level node of the FrameTree is initalized by simulating a | |
|
Charlie Reis
2013/09/27 19:19:21
initialized
awong
2013/09/27 20:50:09
Done.
| |
| 73 // main frame swap here. | |
| 74 RenderFrameHostImpl render_frame_host(process(), &frame_tree, | |
| 75 process()->GetNextRoutingID(), false); | |
| 76 frame_tree.SwapMainFrame(&render_frame_host); | |
| 77 frame_tree.OnFirstNavigationAfterSwap(5); | |
| 78 | |
| 79 // Let's send a series of messages for frame attached and build the | |
|
Charlie Reis
2013/09/27 19:19:21
for frame attached -> to simulate attaching a fram
awong
2013/09/27 20:50:09
rewrote the whole comment.
| |
| 80 // frame tree. | |
| 81 frame_tree.AddFrame(process()->GetNextRoutingID(), 5, 14, std::string()); | |
| 82 frame_tree.AddFrame(process()->GetNextRoutingID(), 5, 15, std::string()); | |
| 83 frame_tree.AddFrame(process()->GetNextRoutingID(), 5, 16, std::string()); | |
| 84 | |
| 85 frame_tree.AddFrame(process()->GetNextRoutingID(), 14, 244, std::string()); | |
| 86 frame_tree.AddFrame(process()->GetNextRoutingID(), 14, 245, std::string()); | |
| 87 | |
| 88 frame_tree.AddFrame(process()->GetNextRoutingID(), 15, 255, no_children_node); | |
| 89 | |
| 90 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 264, std::string()); | |
| 91 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 265, std::string()); | |
| 92 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 266, std::string()); | |
| 93 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 267, deep_subtree); | |
| 94 frame_tree.AddFrame(process()->GetNextRoutingID(), 16, 268, std::string()); | |
| 95 | |
| 96 frame_tree.AddFrame(process()->GetNextRoutingID(), 267, 365, std::string()); | |
| 97 frame_tree.AddFrame(process()->GetNextRoutingID(), 365, 455, std::string()); | |
| 98 frame_tree.AddFrame(process()->GetNextRoutingID(), 455, 555, std::string()); | |
| 99 frame_tree.AddFrame(process()->GetNextRoutingID(), 555, 655, std::string()); | |
| 100 | |
| 101 // Now, verify the tree structure is as expected. | |
| 102 FrameTreeNode* root = frame_tree.GetRootForTesting(); | |
| 103 EXPECT_EQ(5, root->frame_id()); | |
| 104 EXPECT_EQ(3UL, root->child_count()); | |
| 105 | |
| 106 EXPECT_EQ(2UL, root->child_at(0)->child_count()); | |
| 107 EXPECT_EQ(0UL, root->child_at(0)->child_at(0)->child_count()); | |
| 108 EXPECT_EQ(0UL, root->child_at(0)->child_at(1)->child_count()); | |
| 109 | |
| 110 EXPECT_EQ(1UL, root->child_at(1)->child_count()); | |
| 111 EXPECT_EQ(0UL, root->child_at(1)->child_at(0)->child_count()); | |
| 112 EXPECT_STREQ(no_children_node.c_str(), | |
| 113 root->child_at(1)->child_at(0)->frame_name().c_str()); | |
| 114 | |
| 115 EXPECT_EQ(5UL, root->child_at(2)->child_count()); | |
| 116 EXPECT_EQ(0UL, root->child_at(2)->child_at(0)->child_count()); | |
| 117 EXPECT_EQ(0UL, root->child_at(2)->child_at(1)->child_count()); | |
| 118 EXPECT_EQ(0UL, root->child_at(2)->child_at(2)->child_count()); | |
| 119 EXPECT_EQ(1UL, root->child_at(2)->child_at(3)->child_count()); | |
| 120 EXPECT_STREQ(deep_subtree.c_str(), | |
| 121 root->child_at(2)->child_at(3)->frame_name().c_str()); | |
| 122 EXPECT_EQ(0UL, root->child_at(2)->child_at(4)->child_count()); | |
| 123 | |
| 124 FrameTreeNode* deep_tree = root->child_at(2)->child_at(3)->child_at(0); | |
| 125 EXPECT_EQ(365, deep_tree->frame_id()); | |
| 126 EXPECT_EQ(1UL, deep_tree->child_count()); | |
| 127 EXPECT_EQ(455, deep_tree->child_at(0)->frame_id()); | |
| 128 EXPECT_EQ(1UL, deep_tree->child_at(0)->child_count()); | |
| 129 EXPECT_EQ(555, deep_tree->child_at(0)->child_at(0)->frame_id()); | |
| 130 EXPECT_EQ(1UL, deep_tree->child_at(0)->child_at(0)->child_count()); | |
| 131 EXPECT_EQ(655, deep_tree->child_at(0)->child_at(0)->child_at(0)->frame_id()); | |
| 132 EXPECT_EQ(0UL, | |
| 133 deep_tree->child_at(0)->child_at(0)->child_at(0)->child_count()); | |
| 134 | |
| 135 // Test removing of nodes. | |
| 136 frame_tree.RemoveFrame(555, 655); | |
| 137 EXPECT_EQ(0UL, deep_tree->child_at(0)->child_at(0)->child_count()); | |
| 138 | |
| 139 frame_tree.RemoveFrame(16, 265); | |
| 140 EXPECT_EQ(4UL, root->child_at(2)->child_count()); | |
| 141 | |
| 142 frame_tree.RemoveFrame(5, 15); | |
| 143 EXPECT_EQ(2UL, root->child_count()); | |
| 144 } | |
| 145 | |
| 146 } // namespace | |
| 147 } // namespace content | |
| OLD | NEW |