File: performance_manager_tab_helper_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 (410 lines) | stat: -rw-r--r-- 16,729 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright 2019 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_tab_helper.h"

#include <optional>
#include <set>
#include <utility>

#include "base/containers/contains.h"
#include "base/test/scoped_feature_list.h"
#include "components/performance_manager/graph/frame_node_impl.h"
#include "components/performance_manager/graph/graph_impl.h"
#include "components/performance_manager/graph/page_node_impl.h"
#include "components/performance_manager/graph/process_node_impl.h"
#include "components/performance_manager/performance_manager_impl.h"
#include "components/performance_manager/public/features.h"
#include "components/performance_manager/public/graph/graph.h"
#include "components/performance_manager/public/graph/graph_operations.h"
#include "components/performance_manager/public/graph/page_node.h"
#include "components/performance_manager/render_process_user_data.h"
#include "components/performance_manager/test_support/graph/mock_page_node_observer.h"
#include "components/performance_manager/test_support/performance_manager_test_harness.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/permission_descriptor_util.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/common/content_features.h"
#include "content/public/common/process_type.h"
#include "content/public/test/mock_permission_controller.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/render_frame_host_test_support.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/permissions/permission_utils.h"
#include "third_party/blink/public/mojom/favicon/favicon_url.mojom.h"

namespace performance_manager {

namespace {

const char kParentUrl[] = "https://parent.com/";
const char kChild1Url[] = "https://child1.com/";
const char kChild2Url[] = "https://child2.com/";
const char kGrandchildUrl[] = "https://grandchild.com/";
const char kNewGrandchildUrl[] = "https://newgrandchild.com/";
const char kCousinFreddyUrl[] = "https://cousinfreddy.com/";

class PerformanceManagerTabHelperTest
    : public PerformanceManagerTestHarness,
      public testing::WithParamInterface<bool> {
 public:
  PerformanceManagerTabHelperTest() {
    scoped_feature_list_.InitWithFeatureState(
        features::kSeamlessRenderFrameSwap, GetParam());
  }

  void TearDown() override {
    // Clean up the web contents, which should dispose of the page and frame
    // nodes involved.
    DeleteContents();

    PerformanceManagerTestHarness::TearDown();
  }

  // A helper function for checking that the graph matches the topology of
  // stuff in content. The graph should reflect the set of processes provided
  // by |hosts|, even though content may actually have other processes lying
  // around.
  void CheckGraphTopology(const std::set<content::RenderProcessHost*>& hosts,
                          const char* grandchild_url);

 protected:
  static size_t CountAllRenderProcessHosts() {
    size_t num_hosts = 0;
    for (auto it = content::RenderProcessHost::AllHostsIterator();
         !it.IsAtEnd(); it.Advance()) {
      ++num_hosts;
    }
    return num_hosts;
  }

  static size_t CountAllRenderProcessNodes(Graph* graph) {
    size_t num_hosts = 0;
    for (const ProcessNode* process_node : graph->GetAllProcessNodes()) {
      if (process_node->GetProcessType() == content::PROCESS_TYPE_RENDERER) {
        ++num_hosts;
      }
    }
    return num_hosts;
  }

  base::test::ScopedFeatureList scoped_feature_list_;
};

void PerformanceManagerTabHelperTest::CheckGraphTopology(
    const std::set<content::RenderProcessHost*>& hosts,
    const char* grandchild_url) {
  // There may be more RenderProcessHosts in existence than those used from
  // the RFHs above. The graph may not reflect all of them, as only those
  // observed through the TabHelper will have been reflected in the graph.
  size_t num_hosts = CountAllRenderProcessHosts();
  EXPECT_LE(hosts.size(), num_hosts);
  EXPECT_NE(0u, hosts.size());

  // Convert the RPHs to ProcessNodeImpls so we can check they match.
  std::set<const ProcessNode*> process_nodes;
  for (auto* host : hosts) {
    auto* data = RenderProcessUserData::GetForRenderProcessHost(host);
    EXPECT_TRUE(data);
    process_nodes.insert(data->process_node());
  }
  EXPECT_EQ(process_nodes.size(), hosts.size());

  // Check out the graph itself.
  auto* graph = PerformanceManager::GetGraph();

  EXPECT_GE(num_hosts, CountAllRenderProcessNodes(graph));
  EXPECT_EQ(4u, graph->GetAllFrameNodes().size());

  // Expect all frame nodes to be current. This fails if our
  // implementation of RenderFrameHostChanged is borked.
  for (auto* frame : graph->GetAllFrameNodes()) {
    EXPECT_TRUE(frame->IsCurrent());
  }

  ASSERT_EQ(1u, graph->GetAllPageNodes().size());
  auto* page = graph->GetAllPageNodes().AsVector()[0];

  // Extra RPHs can and most definitely do exist.
  auto associated_process_nodes =
      GraphOperations::GetAssociatedProcessNodes(page);
  EXPECT_GE(CountAllRenderProcessNodes(graph), associated_process_nodes.size());
  EXPECT_GE(num_hosts, associated_process_nodes.size());

  for (const ProcessNode* process_node : associated_process_nodes) {
    EXPECT_TRUE(base::Contains(process_nodes, process_node));
  }

  EXPECT_EQ(4u, GraphOperations::GetFrameNodes(page).size());
  ASSERT_EQ(1u, page->GetMainFrameNodes().size());

  auto* main_frame = page->GetMainFrameNode();
  EXPECT_EQ(kParentUrl, main_frame->GetURL().spec());
  EXPECT_EQ(2u, main_frame->GetChildFrameNodes().size());

  for (const FrameNode* child_frame : main_frame->GetChildFrameNodes()) {
    if (child_frame->GetURL().spec() == kChild1Url) {
      ASSERT_EQ(1u, child_frame->GetChildFrameNodes().size());
      auto* grandchild_frame = *child_frame->GetChildFrameNodes().begin();
      EXPECT_EQ(grandchild_url, grandchild_frame->GetURL().spec());
    } else if (child_frame->GetURL().spec() == kChild2Url) {
      EXPECT_TRUE(child_frame->GetChildFrameNodes().empty());
    } else {
      FAIL() << "Unexpected child frame: " << child_frame->GetURL().spec();
    }
  }
}

}  // namespace

INSTANTIATE_TEST_SUITE_P(All, PerformanceManagerTabHelperTest, testing::Bool());

TEST_P(PerformanceManagerTabHelperTest, FrameHierarchyReflectsToGraph) {
  SetContents(CreateTestWebContents());

  auto* parent = content::NavigationSimulator::NavigateAndCommitFromBrowser(
      web_contents(), GURL(kParentUrl));
  DCHECK(parent);

  auto* parent_tester = content::RenderFrameHostTester::For(parent);
  auto* child1 = content::NavigationSimulator::NavigateAndCommitFromDocument(
      GURL(kChild1Url), parent_tester->AppendChild("child1"));
  auto* grandchild =
      content::NavigationSimulator::NavigateAndCommitFromDocument(
          GURL(kGrandchildUrl),
          content::RenderFrameHostTester::For(child1)->AppendChild(
              "grandchild"));
  auto* child2 = content::NavigationSimulator::NavigateAndCommitFromDocument(
      GURL(kChild2Url), parent_tester->AppendChild("child2"));

  // Count the RFHs referenced.
  std::set<content::RenderProcessHost*> hosts;
  auto* grandchild_process = grandchild->GetProcess();
  hosts.insert(main_rfh()->GetProcess());
  hosts.insert(child1->GetProcess());
  hosts.insert(grandchild->GetProcess());
  hosts.insert(child2->GetProcess());

  CheckGraphTopology(hosts, kGrandchildUrl);

  // Navigate the grand-child frame. This tests that we accurately observe the
  // new RFH being created and marked current, with the old one being marked not
  // current and torn down. Note that the old RPH doesn't get torn down.
  auto* new_grandchild =
      content::NavigationSimulator::NavigateAndCommitFromDocument(
          GURL(kNewGrandchildUrl), grandchild);
  auto* new_grandchild_process = new_grandchild->GetProcess();

  // Update the set of processes we expect to be associated with the page.
  hosts.erase(grandchild_process);
  hosts.insert(new_grandchild_process);

  CheckGraphTopology(hosts, kNewGrandchildUrl);

  // Clean up the web contents, which should dispose of the page and frame nodes
  // involved.
  DeleteContents();

  // Allow content/ to settle.
  task_environment()->RunUntilIdle();

  size_t num_hosts = CountAllRenderProcessHosts();

  auto* graph = PerformanceManager::GetGraph();
  EXPECT_GE(num_hosts, CountAllRenderProcessNodes(graph));
  EXPECT_EQ(0u, graph->GetAllFrameNodes().size());
  ASSERT_EQ(0u, graph->GetAllPageNodes().size());
}

namespace {

void ExpectPageIsAudible(bool is_audible) {
  auto* graph = PerformanceManager::GetGraph();
  ASSERT_EQ(1u, graph->GetAllPageNodes().size());
  auto* page = graph->GetAllPageNodes().AsVector()[0];
  EXPECT_EQ(is_audible, page->IsAudible());
}

#if !BUILDFLAG(IS_ANDROID)
void ExpectNotificationPermissionStatus(
    std::optional<blink::mojom::PermissionStatus> status) {
  auto* graph = PerformanceManager::GetGraph();
  ASSERT_EQ(1u, graph->GetAllPageNodes().size());
  auto* page = graph->GetAllPageNodes().AsVector()[0];
  EXPECT_EQ(status, page->GetNotificationPermissionStatus());
}
#endif  // !BUILDFLAG(IS_ANDROID)

}  // namespace

TEST_P(PerformanceManagerTabHelperTest, PageIsAudible) {
  SetContents(CreateTestWebContents());

  ExpectPageIsAudible(false);
  content::WebContentsTester::For(web_contents())->SetIsCurrentlyAudible(true);
  ExpectPageIsAudible(true);
  content::WebContentsTester::For(web_contents())->SetIsCurrentlyAudible(false);
  ExpectPageIsAudible(false);
}

#if !BUILDFLAG(IS_ANDROID)
TEST_P(PerformanceManagerTabHelperTest, NotificationPermission) {
  auto owned_permission_controller =
      std::make_unique<testing::NiceMock<content::MockPermissionController>>();
  auto* permission_controller = owned_permission_controller.get();
  GetBrowserContext()->SetPermissionControllerForTesting(
      std::move(owned_permission_controller));
  content::PermissionController::SubscriptionId::Generator
      subscription_id_generator;
  const auto kFirstSubscriptionId = subscription_id_generator.GenerateNextId();
  const auto kSecondSubscriptionId = subscription_id_generator.GenerateNextId();

  SetContents(CreateTestWebContents());
  ExpectNotificationPermissionStatus(std::nullopt);

  // Navigate to an origin with `PermissionStatus::ASK`.
  {
    content::RenderFrameHost* rfh_arg = nullptr;
    content::RenderFrameHost* rfh_arg_2 = nullptr;
    blink::mojom::PermissionDescriptorPtr descriptor;

    EXPECT_CALL(*permission_controller, GetPermissionStatusForCurrentDocument)
        .WillOnce([&](const blink::mojom::PermissionDescriptorPtr&
                          permission_descriptor,
                      content::RenderFrameHost* render_frame_host) {
          descriptor = permission_descriptor->Clone();
          rfh_arg = render_frame_host;
          return blink::mojom::PermissionStatus::ASK;
        });
    EXPECT_CALL(*permission_controller,
                SubscribeToPermissionStatusChange(
                    blink::PermissionType::NOTIFICATIONS, testing::_,
                    testing::_, testing::_, testing::_, testing::_))
        .WillOnce(testing::DoAll(testing::SaveArg<2>(&rfh_arg_2),
                                 testing::Return(kFirstSubscriptionId)));
    content::NavigationSimulator::NavigateAndCommitFromBrowser(
        web_contents(), GURL(kParentUrl));
    testing::Mock::VerifyAndClear(permission_controller);
    EXPECT_EQ(blink::PermissionDescriptorToPermissionType(descriptor),
              blink::PermissionType::NOTIFICATIONS);
    EXPECT_EQ(rfh_arg, web_contents()->GetPrimaryMainFrame());
    EXPECT_EQ(rfh_arg_2, web_contents()->GetPrimaryMainFrame());
    ExpectNotificationPermissionStatus(blink::mojom::PermissionStatus::ASK);
  }

  base::RepeatingCallback<void(content::PermissionStatus)> callback_arg;

  // Navigate to an origin with `PermissionStatus::GRANTED`.
  {
    content::RenderFrameHost* rfh_arg = nullptr;
    content::RenderProcessHost* rph_arg = nullptr;
    blink::mojom::PermissionDescriptorPtr descriptor;

    EXPECT_CALL(*permission_controller, GetPermissionStatusForCurrentDocument)
        .WillOnce([&](const blink::mojom::PermissionDescriptorPtr&
                          permission_descriptor,
                      content::RenderFrameHost* render_frame_host) {
          descriptor = permission_descriptor->Clone();
          rfh_arg = render_frame_host;
          return blink::mojom::PermissionStatus::GRANTED;
        });
    EXPECT_CALL(*permission_controller,
                UnsubscribeFromPermissionStatusChange(kFirstSubscriptionId));
    EXPECT_CALL(*permission_controller,
                SubscribeToPermissionStatusChange(
                    blink::PermissionType::NOTIFICATIONS, testing::_,
                    testing::_, testing::_, testing::_, testing::_))
        .WillOnce(testing::DoAll(testing::SaveArg<1>(&rph_arg),
                                 testing::SaveArg<5>(&callback_arg),
                                 testing::Return(kSecondSubscriptionId)));
    content::NavigationSimulator::NavigateAndCommitFromBrowser(
        web_contents(), GURL(kCousinFreddyUrl));
    testing::Mock::VerifyAndClear(permission_controller);
    EXPECT_EQ(blink::PermissionDescriptorToPermissionType(descriptor),
              blink::PermissionType::NOTIFICATIONS);
    EXPECT_EQ(rfh_arg, web_contents()->GetPrimaryMainFrame());
    ExpectNotificationPermissionStatus(blink::mojom::PermissionStatus::GRANTED);
  }

  // Simulate a change of permission status independent from navigation.
  callback_arg.Run(blink::mojom::PermissionStatus::DENIED);
  ExpectNotificationPermissionStatus(blink::mojom::PermissionStatus::DENIED);

  // The last subscription is removed when the tab helper is deleted.
  EXPECT_CALL(*permission_controller,
              UnsubscribeFromPermissionStatusChange(kSecondSubscriptionId));
}
#endif  // BUILDFLAG(IS_ANDROID)

TEST_P(PerformanceManagerTabHelperTest, GetFrameNode) {
  SetContents(CreateTestWebContents());

  auto* tab_helper =
      PerformanceManagerTabHelper::FromWebContents(web_contents());
  ASSERT_TRUE(tab_helper);

  // GetFrameNode() can return nullptr. In this test, it is achieved by using an
  // empty RenderFrameHost.
  auto* empty_frame = web_contents()->GetPrimaryMainFrame();
  DCHECK(empty_frame);

  auto* empty_frame_node = tab_helper->GetFrameNode(empty_frame);
  EXPECT_FALSE(empty_frame_node);

  // This navigation will create a frame node.
  auto* new_frame = content::NavigationSimulator::NavigateAndCommitFromBrowser(
      web_contents(), GURL(kParentUrl));
  DCHECK(new_frame);

  auto* new_frame_node = tab_helper->GetFrameNode(new_frame);
  EXPECT_TRUE(new_frame_node);
}

TEST_P(PerformanceManagerTabHelperTest,
       NotificationsFromInactiveFrameTreeAreIgnored) {
  SetContents(CreateTestWebContents());

  content::NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(),
                                                             GURL(kParentUrl));
  auto* first_nav_main_rfh = web_contents()->GetPrimaryMainFrame();

  content::LeaveInPendingDeletionState(first_nav_main_rfh);

  content::NavigationSimulator::NavigateAndCommitFromBrowser(
      web_contents(), GURL(kCousinFreddyUrl));
  EXPECT_NE(web_contents()->GetPrimaryMainFrame(), first_nav_main_rfh);

  // Mock observer.
  Graph* graph = PerformanceManager::GetGraph();

  MockPageNodeObserver observer;
  graph->AddPageNodeObserver(&observer);

  auto* tab_helper =
      PerformanceManagerTabHelper::FromWebContents(web_contents());
  ASSERT_TRUE(tab_helper);

  // The first favicon change is always ignored, call DidUpdateFaviconURL twice
  // to ensure that the test doesn't pass simply because of that.
  tab_helper->DidUpdateFaviconURL(first_nav_main_rfh, {});
  tab_helper->DidUpdateFaviconURL(first_nav_main_rfh, {});

  // The observer shouldn't have been called at this point.
  testing::Mock::VerifyAndClear(&observer);
  // Set the expectation for the next check.
  EXPECT_CALL(observer, OnFaviconUpdated(::testing::_));

  // Sanity check to ensure that notification sent to the active main frame are
  // forwarded. DidUpdateFaviconURL needs to be called twice as the first
  // favicon change is always ignored.
  tab_helper->DidUpdateFaviconURL(web_contents()->GetPrimaryMainFrame(), {});
  tab_helper->DidUpdateFaviconURL(web_contents()->GetPrimaryMainFrame(), {});

  testing::Mock::VerifyAndClear(&observer);
  graph->RemovePageNodeObserver(&observer);
}

}  // namespace performance_manager