performance_manager_unittest.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2019 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. #include "components/performance_manager/public/performance_manager.h"
  5. #include <utility>
  6. #include "base/run_loop.h"
  7. #include "base/test/bind.h"
  8. #include "components/performance_manager/public/graph/frame_node.h"
  9. #include "components/performance_manager/public/graph/page_node.h"
  10. #include "components/performance_manager/public/graph/process_node.h"
  11. #include "components/performance_manager/public/render_frame_host_proxy.h"
  12. #include "components/performance_manager/public/render_process_host_proxy.h"
  13. #include "components/performance_manager/public/web_contents_proxy.h"
  14. #include "components/performance_manager/test_support/performance_manager_test_harness.h"
  15. #include "content/public/browser/browser_task_traits.h"
  16. #include "content/public/browser/browser_thread.h"
  17. #include "content/public/browser/web_contents.h"
  18. #include "content/public/test/navigation_simulator.h"
  19. #include "testing/gtest/include/gtest/gtest.h"
  20. #include "url/gurl.h"
  21. namespace performance_manager {
  22. class PerformanceManagerTest : public PerformanceManagerTestHarness {
  23. public:
  24. using Super = PerformanceManagerTestHarness;
  25. PerformanceManagerTest() {}
  26. void SetUp() override {
  27. EXPECT_FALSE(PerformanceManager::IsAvailable());
  28. Super::SetUp();
  29. EXPECT_TRUE(PerformanceManager::IsAvailable());
  30. }
  31. void TearDown() override {
  32. EXPECT_TRUE(PerformanceManager::IsAvailable());
  33. Super::TearDown();
  34. EXPECT_FALSE(PerformanceManager::IsAvailable());
  35. }
  36. PerformanceManagerTest(const PerformanceManagerTest&) = delete;
  37. PerformanceManagerTest& operator=(const PerformanceManagerTest&) = delete;
  38. ~PerformanceManagerTest() override {}
  39. };
  40. TEST_F(PerformanceManagerTest, NodeAccessors) {
  41. auto contents = CreateTestWebContents();
  42. content::RenderFrameHost* rfh = contents->GetPrimaryMainFrame();
  43. ASSERT_TRUE(rfh);
  44. content::RenderProcessHost* rph = rfh->GetProcess();
  45. ASSERT_TRUE(rph);
  46. base::WeakPtr<PageNode> page_node =
  47. PerformanceManager::GetPrimaryPageNodeForWebContents(contents.get());
  48. // FrameNode's and ProcessNode's don't exist until an observer fires on
  49. // navigation. Verify that looking them up before that returns null instead
  50. // of crashing.
  51. EXPECT_FALSE(PerformanceManager::GetFrameNodeForRenderFrameHost(rfh));
  52. EXPECT_FALSE(PerformanceManager::GetProcessNodeForRenderProcessHost(rph));
  53. // Simulate a committed navigation to create the nodes.
  54. content::NavigationSimulator::NavigateAndCommitFromBrowser(
  55. contents.get(), GURL("https://www.example.com/"));
  56. base::WeakPtr<FrameNode> frame_node =
  57. PerformanceManager::GetFrameNodeForRenderFrameHost(rfh);
  58. base::WeakPtr<ProcessNode> process_node =
  59. PerformanceManager::GetProcessNodeForRenderProcessHost(rph);
  60. // Post a task to the Graph and make it call a function on the UI thread that
  61. // will ensure that the nodes are really associated with the content objects.
  62. base::RunLoop run_loop;
  63. auto check_proxies_on_main_thread =
  64. base::BindLambdaForTesting([&](const WebContentsProxy& wc_proxy,
  65. const RenderFrameHostProxy& rfh_proxy,
  66. const RenderProcessHostProxy& rph_proxy) {
  67. EXPECT_EQ(contents.get(), wc_proxy.Get());
  68. EXPECT_EQ(rfh, rfh_proxy.Get());
  69. EXPECT_EQ(rph, rph_proxy.Get());
  70. run_loop.Quit();
  71. });
  72. auto call_on_graph_cb = base::BindLambdaForTesting([&]() {
  73. EXPECT_TRUE(page_node.get());
  74. EXPECT_TRUE(frame_node.get());
  75. EXPECT_TRUE(process_node.get());
  76. content::GetUIThreadTaskRunner({})->PostTask(
  77. FROM_HERE, base::BindOnce(std::move(check_proxies_on_main_thread),
  78. page_node->GetContentsProxy(),
  79. frame_node->GetRenderFrameHostProxy(),
  80. process_node->GetRenderProcessHostProxy()));
  81. });
  82. PerformanceManager::CallOnGraph(FROM_HERE, call_on_graph_cb);
  83. // Wait for |check_proxies_on_main_thread| to be called.
  84. run_loop.Run();
  85. contents.reset();
  86. // After deleting |contents| the corresponding WeakPtr's should be
  87. // invalid.
  88. base::RunLoop run_loop_after_contents_reset;
  89. auto quit_closure = run_loop_after_contents_reset.QuitClosure();
  90. auto call_on_graph_cb_2 = base::BindLambdaForTesting([&]() {
  91. EXPECT_FALSE(page_node.get());
  92. EXPECT_FALSE(frame_node.get());
  93. EXPECT_FALSE(process_node.get());
  94. std::move(quit_closure).Run();
  95. });
  96. PerformanceManager::CallOnGraph(FROM_HERE, call_on_graph_cb_2);
  97. run_loop_after_contents_reset.Run();
  98. }
  99. } // namespace performance_manager