File: frame_node_impl_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; 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 (369 lines) | stat: -rw-r--r-- 13,517 bytes parent folder | download | duplicates (4)
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
// 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/graph/frame_node_impl.h"

#include <vector>

#include "base/functional/callback.h"
#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/performance_manager/graph/page_node_impl.h"
#include "components/performance_manager/performance_manager_impl.h"
#include "components/performance_manager/public/features.h"
#include "components/performance_manager/public/graph/frame_node.h"
#include "components/performance_manager/public/graph/graph.h"
#include "components/performance_manager/public/viewport_intersection.h"
#include "components/performance_manager/test_support/graph/mock_frame_node_observer.h"
#include "content/public/test/back_forward_cache_util.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/prerender_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/rect_f.h"

namespace performance_manager {

using testing::_;
using testing::AllOf;
using testing::Not;
using testing::UnorderedElementsAre;

MATCHER(IsMainFrame, "") {
  return arg->IsMainFrame();
}

MATCHER_P(HasViewportIntersection, viewport_intersection, "") {
  return arg->GetViewportIntersection() == viewport_intersection;
}

namespace {

// Sends a 'p' key press to the page, simulating a user edit if a text field is
// focused.
void SimulateKeyPress(content::WebContents* web_contents) {
  content::SimulateKeyPress(web_contents, ui::DomKey::FromCharacter('p'),
                            ui::DomCode::US_P, ui::VKEY_P, /*control=*/false,
                            /*shift=*/false, /*alt=*/false, /*command=*/false);
}

using FrameNodeImplBrowserTest = InProcessBrowserTest;

class ParameterizedFrameNodeImplBrowserTest
    : public FrameNodeImplBrowserTest,
      public testing::WithParamInterface<bool> {
 public:
  ParameterizedFrameNodeImplBrowserTest() {
    base::FieldTrialParams params = {
        {features::kRenderedOutOfViewIsNotVisible.name,
         GetParam() ? "true" : "false"}};
    scoped_feature_list_.InitAndEnableFeatureWithParameters(
        features::kPMProcessPriorityPolicy, params);
  }
  ~ParameterizedFrameNodeImplBrowserTest() override = default;

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

}  // namespace

IN_PROC_BROWSER_TEST_P(ParameterizedFrameNodeImplBrowserTest,
                       ViewportIntersection_OutOfView) {
  ASSERT_TRUE(embedded_test_server()->Start());
  EXPECT_EQ(1, browser()->tab_strip_model()->count());

  const bool expects_intersects_viewport =
      !performance_manager::features::kRenderedOutOfViewIsNotVisible.Get();
  testing::Matcher<const FrameNode*> viewport_intersection_matcher =
      expects_intersects_viewport
          ? HasViewportIntersection(ViewportIntersection::kIntersecting)
          : HasViewportIntersection(ViewportIntersection::kNotIntersecting);

  const GURL main_frame_url(
      embedded_test_server()->GetURL("/iframe_out_of_view.html"));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), main_frame_url));

  Graph* graph = PerformanceManager::GetGraph();
  auto all_frame_nodes = graph->GetAllFrameNodes().AsVector();

  EXPECT_THAT(
      all_frame_nodes,
      UnorderedElementsAre(
          // One main frame, intersects with the viewport.
          AllOf(IsMainFrame(),
                HasViewportIntersection(ViewportIntersection::kIntersecting)),
          // One child frame, intersects with the viewport depending on the
          // value of the kRenderedOutOfViewIsNotVisible feature.
          AllOf(Not(IsMainFrame()), viewport_intersection_matcher)));
}

INSTANTIATE_TEST_SUITE_P(,
                         ParameterizedFrameNodeImplBrowserTest,
                         testing::Bool());

IN_PROC_BROWSER_TEST_F(FrameNodeImplBrowserTest, ViewportIntersection_Hidden) {
  ASSERT_TRUE(embedded_test_server()->Start());
  EXPECT_EQ(1, browser()->tab_strip_model()->count());

  const GURL main_frame_url(
      embedded_test_server()->GetURL("/iframe_hidden.html"));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), main_frame_url));

  Graph* graph = PerformanceManager::GetGraph();
  auto all_frame_nodes = graph->GetAllFrameNodes().AsVector();

  EXPECT_THAT(
      all_frame_nodes,
      UnorderedElementsAre(
          // One main frame, intersects with the viewport.
          AllOf(IsMainFrame(),
                HasViewportIntersection(ViewportIntersection::kIntersecting)),
          // One child frame, does not intersect with the viewport.
          AllOf(Not(IsMainFrame()),
                HasViewportIntersection(
                    ViewportIntersection::kNotIntersecting))));
}

IN_PROC_BROWSER_TEST_F(FrameNodeImplBrowserTest,
                       ViewportIntersection_PartiallyVisible) {
  ASSERT_TRUE(embedded_test_server()->Start());
  EXPECT_EQ(1, browser()->tab_strip_model()->count());

  const GURL main_frame_url(
      embedded_test_server()->GetURL("/iframe_partially_visible.html"));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), main_frame_url));

  Graph* graph = PerformanceManager::GetGraph();
  auto all_frame_nodes = graph->GetAllFrameNodes().AsVector();

  EXPECT_THAT(
      all_frame_nodes,
      UnorderedElementsAre(
          // One main frame, intersects with the viewport.
          AllOf(IsMainFrame(),
                HasViewportIntersection(ViewportIntersection::kIntersecting)),
          // One child frame, also intersects with the viewport.
          AllOf(Not(IsMainFrame()),
                HasViewportIntersection(ViewportIntersection::kIntersecting))));
}

IN_PROC_BROWSER_TEST_F(FrameNodeImplBrowserTest, ViewportIntersection_Scaled) {
  ASSERT_TRUE(embedded_test_server()->Start());
  EXPECT_EQ(1, browser()->tab_strip_model()->count());

  const GURL main_frame_url(
      embedded_test_server()->GetURL("/iframe_scaled.html"));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), main_frame_url));

  Graph* graph = PerformanceManager::GetGraph();
  auto all_frame_nodes = graph->GetAllFrameNodes().AsVector();

  EXPECT_THAT(
      all_frame_nodes,
      UnorderedElementsAre(
          // One main frame, intersects with the viewport.
          AllOf(IsMainFrame(),
                HasViewportIntersection(ViewportIntersection::kIntersecting)),
          // One child frame, also intersects with the viewport.
          AllOf(Not(IsMainFrame()),
                HasViewportIntersection(ViewportIntersection::kIntersecting))));
}

IN_PROC_BROWSER_TEST_F(FrameNodeImplBrowserTest, ViewportIntersection_Rotated) {
  ASSERT_TRUE(embedded_test_server()->Start());
  EXPECT_EQ(1, browser()->tab_strip_model()->count());

  const GURL main_frame_url(
      embedded_test_server()->GetURL("/iframe_rotated.html"));
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), main_frame_url));

  Graph* graph = PerformanceManager::GetGraph();
  auto all_frame_nodes = graph->GetAllFrameNodes().AsVector();

  EXPECT_THAT(
      all_frame_nodes,
      UnorderedElementsAre(
          // One main frame, intersects with the viewport.
          AllOf(IsMainFrame(),
                HasViewportIntersection(ViewportIntersection::kIntersecting)),
          // One child frame, also intersects with the viewport.
          AllOf(Not(IsMainFrame()),
                HasViewportIntersection(ViewportIntersection::kIntersecting))));
}

// TODO(https://crbug.com/376315752): Deflake and re-enable.
IN_PROC_BROWSER_TEST_F(FrameNodeImplBrowserTest,
                       DISABLED_Bind_SimpleNavigation) {
  ASSERT_TRUE(embedded_test_server()->Start());
  EXPECT_EQ(1, browser()->tab_strip_model()->count());

  const GURL kTestUrl =
      embedded_test_server()->GetURL("/form_interaction.html");

  content::RenderFrameHost* rfh =
      ui_test_utils::NavigateToURL(browser(), kTestUrl);
  ASSERT_TRUE(rfh);
  EXPECT_EQ(rfh->GetLifecycleState(),
            content::RenderFrameHost::LifecycleState::kActive);

  Graph* graph = PerformanceManager::GetGraph();

  // Get the frame's node.

  // Check that a form interaction notification is received through the bound
  // receiver.
  MockFrameNodeObserver obs;
  graph->AddFrameNodeObserver(&obs);

  base::RunLoop run_loop;
  EXPECT_CALL(obs, OnHadFormInteractionChanged(_)).WillOnce([&]() {
    run_loop.Quit();
  });

  SimulateKeyPress(browser()->tab_strip_model()->GetActiveWebContents());
  run_loop.Run();

  // Clean up.
  graph->RemoveFrameNodeObserver(&obs);
}

class FrameNodeImplBackForwardCacheBrowserTest
    : public FrameNodeImplBrowserTest {
 public:
  FrameNodeImplBackForwardCacheBrowserTest() {
    content::InitBackForwardCacheFeature(&scoped_feature_list_,
                                         /*enable_back_forward_cache=*/true);
  }

 public:
  base::test::ScopedFeatureList scoped_feature_list_;
};

IN_PROC_BROWSER_TEST_F(FrameNodeImplBackForwardCacheBrowserTest,
                       Bind_BackForwardCache) {
  ASSERT_TRUE(embedded_test_server()->Start());
  EXPECT_EQ(1, browser()->tab_strip_model()->count());

  const GURL kTestUrl =
      embedded_test_server()->GetURL("/form_interaction.html");
  const GURL kOtherUrl = embedded_test_server()->GetURL("/title1.html");

  // Navigation to the test URL.
  content::RenderFrameHost* rfh =
      ui_test_utils::NavigateToURL(browser(), kTestUrl);
  ASSERT_TRUE(rfh);
  EXPECT_EQ(rfh->GetLifecycleState(),
            content::RenderFrameHost::LifecycleState::kActive);

  // Navigate to some other URL.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), kOtherUrl));
  EXPECT_EQ(rfh->GetLifecycleState(),
            content::RenderFrameHost::LifecycleState::kInBackForwardCache);

  // Back to the test URL.
  EXPECT_TRUE(content::HistoryGoBack(
      browser()->tab_strip_model()->GetActiveWebContents()));
  EXPECT_EQ(rfh->GetLifecycleState(),
            content::RenderFrameHost::LifecycleState::kActive);

  Graph* graph = PerformanceManager::GetGraph();

  // Check that a form interaction notification is received through the bound
  // receiver.
  LenientMockFrameNodeObserver obs;
  graph->AddFrameNodeObserver(&obs);

  base::RunLoop run_loop;
  EXPECT_CALL(obs, OnHadFormInteractionChanged(_)).WillOnce([&]() {
    run_loop.Quit();
  });

  // After HistoryGoBack(), the text field is no longer focused so we explicly
  // re-focus it.
  EXPECT_TRUE(ExecJs(rfh, "FocusTextField();"));
  SimulateKeyPress(browser()->tab_strip_model()->GetActiveWebContents());
  run_loop.Run();

  // Clean up.
  graph->RemoveFrameNodeObserver(&obs);
}

class FrameNodeImplPrerenderBrowserTest : public FrameNodeImplBrowserTest {
 public:
  FrameNodeImplPrerenderBrowserTest()
      : prerender_test_helper_(base::BindRepeating(
            &FrameNodeImplPrerenderBrowserTest::GetWebContents,
            base::Unretained(this))) {}

  content::WebContents* GetWebContents() const {
    return browser()->tab_strip_model()->GetActiveWebContents();
  }

 public:
  content::test::ScopedPrerenderFeatureList scoped_prerender_feature_list_;
  content::test::PrerenderTestHelper prerender_test_helper_;
};

// TODO(362360274): Fix this flaky test.
IN_PROC_BROWSER_TEST_F(FrameNodeImplPrerenderBrowserTest,
                       DISABLED_Bind_PrerenderNavigation) {
  ASSERT_TRUE(embedded_test_server()->Start());
  EXPECT_EQ(1, browser()->tab_strip_model()->count());

  const GURL kInitialUrl = embedded_test_server()->GetURL("/empty.html");
  const GURL kPrerenderUrl =
      embedded_test_server()->GetURL("/form_interaction.html");

  // Initial navigation. Needed so we can add prerendered frames.
  content::RenderFrameHost* rfh =
      ui_test_utils::NavigateToURL(browser(), kInitialUrl);
  ASSERT_TRUE(rfh);
  EXPECT_EQ(rfh->GetLifecycleState(),
            content::RenderFrameHost::LifecycleState::kActive);

  // Create the prerendered frame.
  content::FrameTreeNodeId host_id =
      prerender_test_helper_.AddPrerender(kPrerenderUrl);
  content::RenderFrameHost* prerender_rfh =
      prerender_test_helper_.GetPrerenderedMainFrameHost(host_id);
  ASSERT_TRUE(prerender_rfh);
  EXPECT_EQ(prerender_rfh->GetLifecycleState(),
            content::RenderFrameHost::LifecycleState::kPrerendering);

  // Navigate to the prerendered frame.
  prerender_test_helper_.NavigatePrimaryPage(kPrerenderUrl);
  EXPECT_EQ(prerender_rfh->GetLifecycleState(),
            content::RenderFrameHost::LifecycleState::kActive);

  Graph* graph = PerformanceManager::GetGraph();

  // Check that a form interaction notification is received through the bound
  // receiver.
  MockFrameNodeObserver obs;
  graph->AddFrameNodeObserver(&obs);

  base::RunLoop run_loop;
  EXPECT_CALL(obs, OnHadFormInteractionChanged(_)).WillOnce([&]() {
    run_loop.Quit();
  });

  // After activating the prerender, the text field is no longer focused so we
  // explicly re-focus it.
  EXPECT_TRUE(ExecJs(prerender_rfh, "FocusTextField();"));
  SimulateKeyPress(browser()->tab_strip_model()->GetActiveWebContents());
  run_loop.Run();

  // Clean up.
  graph->RemoveFrameNodeObserver(&obs);
}

}  // namespace performance_manager