File: performance_manager_browsertest.cc

package info (click to toggle)
chromium 135.0.7049.95-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,959,392 kB
  • sloc: cpp: 34,198,526; ansic: 7,100,035; javascript: 3,985,800; python: 1,395,489; asm: 896,754; xml: 722,891; pascal: 180,504; sh: 94,909; perl: 88,388; objc: 79,739; sql: 53,020; cs: 41,358; fortran: 24,137; makefile: 22,501; php: 13,699; tcl: 10,142; yacc: 8,822; ruby: 7,350; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; awk: 197; sed: 36
file content (216 lines) | stat: -rw-r--r-- 8,184 bytes parent folder | download
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
// Copyright 2020 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/public/performance_manager.h"

#include <utility>

#include "base/run_loop.h"
#include "base/test/bind.h"
#include "components/performance_manager/graph/frame_node_impl.h"
#include "components/performance_manager/performance_manager_tab_helper.h"
#include "components/performance_manager/public/graph/frame_node.h"
#include "components/performance_manager/public/graph/graph.h"
#include "components/performance_manager/public/graph/page_node.h"
#include "components/performance_manager/public/render_frame_host_proxy.h"
#include "components/performance_manager/test_support/performance_manager_browsertest_harness.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "content/shell/browser/shell.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "url/origin.h"

namespace performance_manager {

using PerformanceManagerBrowserTest = PerformanceManagerBrowserTestHarness;

// A full browser test is required for this because we need RenderFrameHosts
// to be created.
IN_PROC_BROWSER_TEST_F(PerformanceManagerBrowserTest,
                       GetFrameNodeForRenderFrameHost) {
  // NavigateToURL blocks until the load has completed. We assert that the
  // contents has been reused as we don't have general WebContents creation
  // hooks in our BrowserTest fixture, and if a new contents was created it
  // would be missing the PM tab helper.
  auto* old_contents = shell()->web_contents();
  static const char kUrl[] = "about:blank";
  ASSERT_TRUE(NavigateToURL(shell(), GURL(kUrl)));
  content::WebContents* contents = shell()->web_contents();
  ASSERT_EQ(contents, old_contents);
  ASSERT_EQ(contents->GetLastCommittedURL().possibly_invalid_spec(), kUrl);
  content::RenderFrameHost* rfh = contents->GetPrimaryMainFrame();
  ASSERT_TRUE(rfh->IsRenderFrameLive());

  base::WeakPtr<FrameNode> frame_node =
      PerformanceManager::GetFrameNodeForRenderFrameHost(rfh);

  const RenderFrameHostProxy& rfh_proxy = frame_node->GetRenderFrameHostProxy();
  EXPECT_EQ(rfh, rfh_proxy.Get());

  // This closes the window, and destroys the underlying WebContents.
  shell()->Close();
  contents = nullptr;

  // After deleting |contents| the corresponding FrameNode WeakPtr should be
  // invalid.
  EXPECT_FALSE(frame_node);
}

IN_PROC_BROWSER_TEST_F(PerformanceManagerBrowserTest, OpenerTrackingWorks) {
  // Load a page that will load a popup.
  GURL url(embedded_test_server()->GetURL("a.com", "/a_popup_a.html"));
  content::ShellAddedObserver shell_added_observer;
  ASSERT_TRUE(NavigateToURL(shell(), url));

  // Wait for the popup window to appear, and then wait for it to load.
  auto* popup = shell_added_observer.GetShell();
  ASSERT_TRUE(popup);
  WaitForLoad(popup->web_contents());

  auto* contents = shell()->web_contents();

  // Make sure everything is connected as expected in the graph.
  auto page = PerformanceManager::GetPrimaryPageNodeForWebContents(contents);
  EXPECT_TRUE(page);
  auto* frame = page->GetMainFrameNode();
  EXPECT_EQ(1u, frame->GetOpenedPageNodes().size());
  auto* embedded_page = *(frame->GetOpenedPageNodes().begin());
  EXPECT_EQ(frame, embedded_page->GetOpenerFrameNode());
}

namespace {

class WebRTCUsageChangeWaiter : public PageNodeObserver {
 public:
  WebRTCUsageChangeWaiter() = default;

  void WaitForWebRTCUsageChange() { run_loop_.Run(); }

  // PageNodeObserver:
  void OnPageUsesWebRTCChanged(const PageNode* page_node) override {
    run_loop_.Quit();
  }

 private:
  base::RunLoop run_loop_;
};

}  // namespace

// Integration test for WebRTC usage tracking on PageNode and FrameNode.
IN_PROC_BROWSER_TEST_F(PerformanceManagerBrowserTest, UsesWebRTC) {
  Graph* graph = PerformanceManager::GetGraph();

  WebRTCUsageChangeWaiter waiter;
  graph->AddPageNodeObserver(&waiter);

  GURL url(embedded_test_server()->GetURL("a.com", "/webrtc_basic.html"));
  content::ShellAddedObserver shell_added_observer;
  ASSERT_TRUE(NavigateToURL(shell(), url));

  auto* contents = shell()->web_contents();
  auto page = PerformanceManager::GetPrimaryPageNodeForWebContents(contents);

  waiter.WaitForWebRTCUsageChange();

  EXPECT_TRUE(page->UsesWebRTC());
  EXPECT_TRUE(page->GetMainFrameNode()->UsesWebRTC());

  graph->RemovePageNodeObserver(&waiter);
}

namespace {

MATCHER_P(IsOpaqueDerivedFrom,
          base_origin,
          "is opaque derived from " + base_origin.GetDebugString()) {
  return arg.has_value() && arg->opaque() &&
         arg->GetTupleOrPrecursorTupleIfOpaque() ==
             base_origin.GetTupleOrPrecursorTupleIfOpaque();
}

}  // namespace

IN_PROC_BROWSER_TEST_F(PerformanceManagerBrowserTest, OriginAboutBlankFrame) {
  // Load a page with:
  // - A regular frame navigated to about:blank.
  // - A sandboxed frame navigated to about:blank.
  GURL main_frame_url(
      embedded_test_server()->GetURL("a.com", "/about_blank_iframes.html"));
  const url::Origin main_frame_origin = url::Origin::Create(main_frame_url);
  ASSERT_TRUE(NavigateToURL(shell(), main_frame_url));

  auto* contents = shell()->web_contents();
  auto page = PerformanceManager::GetPrimaryPageNodeForWebContents(contents);

  // Make sure everything is connected as expected in the graph.
  EXPECT_TRUE(page);

  // Verify that the regular frame has the same origin as its parent whereas
  // the sandboxed frame has an opaque origin derived from it, as assumed by
  // Resource Attribution.
  std::vector<std::optional<url::Origin>> child_frame_origins;
  for (const FrameNode* node : page->GetMainFrameNode()->GetChildFrameNodes()) {
    child_frame_origins.push_back(node->GetOrigin());
  }
  EXPECT_THAT(child_frame_origins,
              testing::UnorderedElementsAre(
                  main_frame_origin, IsOpaqueDerivedFrom(main_frame_origin)));
}

class PerformanceManagerFencedFrameBrowserTest
    : public PerformanceManagerBrowserTest {
 public:
  PerformanceManagerFencedFrameBrowserTest() = default;
  ~PerformanceManagerFencedFrameBrowserTest() override = default;

  content::test::FencedFrameTestHelper& fenced_frame_test_helper() {
    return fenced_frame_helper_;
  }

  content::WebContents* GetWebContents() { return shell()->web_contents(); }

 private:
  content::test::FencedFrameTestHelper fenced_frame_helper_;
};

IN_PROC_BROWSER_TEST_F(PerformanceManagerFencedFrameBrowserTest,
                       NoParentFrameNode) {
  auto initial_url = embedded_test_server()->GetURL("/empty.html");
  ASSERT_TRUE(NavigateToURL(shell(), initial_url));

  PerformanceManagerTabHelper* tab_helper =
      PerformanceManagerTabHelper::FromWebContents(GetWebContents());
  ASSERT_TRUE(tab_helper);

  // Main frame, which is going to be the fenced frame's outer document.
  content::RenderFrameHost* main_frame_host =
      GetWebContents()->GetPrimaryMainFrame();
  FrameNodeImpl* main_frame_node = tab_helper->GetFrameNode(main_frame_host);

  // Load a fenced frame.
  GURL fenced_frame_url =
      embedded_test_server()->GetURL("/fenced_frames/title1.html");
  content::RenderFrameHost* fenced_frame_host =
      fenced_frame_test_helper().CreateFencedFrame(main_frame_host,
                                                   fenced_frame_url);
  FrameNodeImpl* fenced_frame_node =
      tab_helper->GetFrameNode(fenced_frame_host);

  // Make sure |fenced_frame_node| does not have a parent frame node.
  EXPECT_EQ(fenced_frame_node->parent_frame_node(), nullptr);

  // The outer document of the fenced frame is available.
  EXPECT_EQ(fenced_frame_node->parent_or_outer_document_or_embedder(),
            main_frame_node);
}

}  // namespace performance_manager