File: performance_manager_impl_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (180 lines) | stat: -rw-r--r-- 8,116 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/performance_manager/performance_manager_impl.h"

#include <utility>

#include "base/run_loop.h"
#include "base/test/gtest_util.h"
#include "components/performance_manager/graph/frame_node_impl.h"
#include "components/performance_manager/graph/page_node_impl.h"
#include "components/performance_manager/graph/process_node_impl.h"
#include "components/performance_manager/public/render_process_host_id.h"
#include "components/performance_manager/public/render_process_host_proxy.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/process_type.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/tokens/tokens.h"

namespace performance_manager {

class PerformanceManagerImplTest : public testing::Test {
 public:
  PerformanceManagerImplTest() = default;

  PerformanceManagerImplTest(const PerformanceManagerImplTest&) = delete;
  PerformanceManagerImplTest& operator=(const PerformanceManagerImplTest&) =
      delete;

  ~PerformanceManagerImplTest() override = default;

  void SetUp() override {
    EXPECT_FALSE(PerformanceManagerImpl::IsAvailable());
    performance_manager_ = PerformanceManagerImpl::Create();
    // Make sure creation registers the created instance.
    EXPECT_TRUE(PerformanceManagerImpl::IsAvailable());
  }

  void TearDown() override {
    PerformanceManagerImpl::Destroy(std::move(performance_manager_));
    // Make sure destruction unregisters the instance.
    EXPECT_FALSE(PerformanceManagerImpl::IsAvailable());

    task_environment_.RunUntilIdle();
  }

 private:
  std::unique_ptr<PerformanceManagerImpl> performance_manager_;
  content::BrowserTaskEnvironment task_environment_;
};

using PerformanceManagerImplDeathTest = PerformanceManagerImplTest;

TEST_F(PerformanceManagerImplTest, InstantiateNodes) {
  const auto render_process_host_id = RenderProcessHostId(1);
  int next_render_frame_id = 0;

  std::unique_ptr<ProcessNodeImpl> process_node =
      PerformanceManagerImpl::CreateProcessNode(
          RenderProcessHostProxy::CreateForTesting(render_process_host_id),
          base::TaskPriority::HIGHEST);
  EXPECT_NE(nullptr, process_node.get());
  std::unique_ptr<PageNodeImpl> page_node =
      PerformanceManagerImpl::CreatePageNode(nullptr, std::string(), GURL(),
                                             PagePropertyFlags{},
                                             base::TimeTicks::Now());
  EXPECT_NE(nullptr, page_node.get());

  // Create a node of each type.
  std::unique_ptr<FrameNodeImpl> frame_node =
      PerformanceManagerImpl::CreateFrameNode(
          process_node.get(), page_node.get(), /*parent_frame_node=*/nullptr,
          /*outer_document_for_fenced_frame*/ nullptr, ++next_render_frame_id,
          blink::LocalFrameToken(), content::BrowsingInstanceId(0),
          content::SiteInstanceGroupId(0), /*is_current=*/true);
  EXPECT_NE(nullptr, frame_node.get());

  PerformanceManagerImpl::DeleteNode(std::move(frame_node));
  PerformanceManagerImpl::DeleteNode(std::move(page_node));
  PerformanceManagerImpl::DeleteNode(std::move(process_node));
}

TEST_F(PerformanceManagerImplDeathTest, InvalidProcessHostProxies) {
  const auto browser_child_process_host_id = BrowserChildProcessHostId(1);
  EXPECT_CHECK_DEATH(PerformanceManagerImpl::CreateProcessNode(
      RenderProcessHostProxy(), base::TaskPriority::HIGHEST));
  EXPECT_CHECK_DEATH(PerformanceManagerImpl::CreateProcessNode(
      content::PROCESS_TYPE_UTILITY, BrowserChildProcessHostProxy()));

  // Valid proxy, wrong process type.
  EXPECT_CHECK_DEATH(PerformanceManagerImpl::CreateProcessNode(
      content::PROCESS_TYPE_BROWSER,
      BrowserChildProcessHostProxy::CreateForTesting(
          browser_child_process_host_id)));
  EXPECT_CHECK_DEATH(PerformanceManagerImpl::CreateProcessNode(
      content::PROCESS_TYPE_RENDERER,
      BrowserChildProcessHostProxy::CreateForTesting(
          browser_child_process_host_id)));
}

TEST_F(PerformanceManagerImplTest, BatchDeleteNodes) {
  const auto render_process_host_id = RenderProcessHostId(1);
  int next_render_frame_id = 0;
  // Create a page node and a small hierarchy of frames.
  std::unique_ptr<ProcessNodeImpl> process_node =
      PerformanceManagerImpl::CreateProcessNode(
          RenderProcessHostProxy::CreateForTesting(render_process_host_id),
          base::TaskPriority::HIGHEST);
  std::unique_ptr<PageNodeImpl> page_node =
      PerformanceManagerImpl::CreatePageNode(nullptr, std::string(), GURL(),
                                             PagePropertyFlags{},
                                             base::TimeTicks::Now());

  std::unique_ptr<FrameNodeImpl> parent1_frame =
      PerformanceManagerImpl::CreateFrameNode(
          process_node.get(), page_node.get(), /*parent_frame_node=*/nullptr,
          /*outer_document_for_fenced_frame*/ nullptr, ++next_render_frame_id,
          blink::LocalFrameToken(), content::BrowsingInstanceId(0),
          content::SiteInstanceGroupId(0), /*is_current*/ true);
  std::unique_ptr<FrameNodeImpl> parent2_frame =
      PerformanceManagerImpl::CreateFrameNode(
          process_node.get(), page_node.get(), /*parent_frame_node=*/nullptr,
          /*outer_document_for_fenced_frame*/ nullptr, ++next_render_frame_id,
          blink::LocalFrameToken(), content::BrowsingInstanceId(0),
          content::SiteInstanceGroupId(0), /*is_current*/ true);

  std::unique_ptr<FrameNodeImpl> child1_frame =
      PerformanceManagerImpl::CreateFrameNode(
          process_node.get(), page_node.get(), parent1_frame.get(),
          /*outer_document_for_fenced_frame*/ nullptr, ++next_render_frame_id,
          blink::LocalFrameToken(), content::BrowsingInstanceId(0),
          content::SiteInstanceGroupId(0), /*is_current*/ true);
  std::unique_ptr<FrameNodeImpl> child2_frame =
      PerformanceManagerImpl::CreateFrameNode(
          process_node.get(), page_node.get(), parent2_frame.get(),
          /*outer_document_for_fenced_frame*/ nullptr, ++next_render_frame_id,
          blink::LocalFrameToken(), content::BrowsingInstanceId(0),
          content::SiteInstanceGroupId(0), /*is_current*/ true);

  std::vector<std::unique_ptr<NodeBase>> nodes;
  for (size_t i = 0; i < 10; ++i) {
    nodes.push_back(PerformanceManagerImpl::CreateFrameNode(
        process_node.get(), page_node.get(), child1_frame.get(),
        /*outer_document_for_fenced_frame*/ nullptr, ++next_render_frame_id,
        blink::LocalFrameToken(), content::BrowsingInstanceId(0),
        content::SiteInstanceGroupId(0), /*is_current*/ true));
    nodes.push_back(PerformanceManagerImpl::CreateFrameNode(
        process_node.get(), page_node.get(), child1_frame.get(),
        /*outer_document_for_fenced_frame*/ nullptr, ++next_render_frame_id,
        blink::LocalFrameToken(), content::BrowsingInstanceId(0),
        content::SiteInstanceGroupId(0), /*is_current*/ true));
  }

  nodes.push_back(std::move(process_node));
  nodes.push_back(std::move(page_node));
  nodes.push_back(std::move(parent1_frame));
  nodes.push_back(std::move(parent2_frame));
  nodes.push_back(std::move(child1_frame));
  nodes.push_back(std::move(child2_frame));

  PerformanceManagerImpl::BatchDeleteNodes(std::move(nodes));
}

TEST_F(PerformanceManagerImplTest, GetGraphImpl) {
  // Create a page node for something to target.
  std::unique_ptr<PageNodeImpl> page_node =
      PerformanceManagerImpl::CreatePageNode(nullptr, std::string(), GURL(),
                                             PagePropertyFlags{},
                                             base::TimeTicks::Now());

  ASSERT_TRUE(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
  GraphImpl* graph = PerformanceManagerImpl::GetGraphImpl();
  EXPECT_EQ(page_node.get()->graph(), graph);

  PerformanceManagerImpl::DeleteNode(std::move(page_node));
}

}  // namespace performance_manager