File: page_discarding_helper_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (696 lines) | stat: -rw-r--r-- 27,081 bytes parent folder | download | duplicates (5)
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/performance_manager/policies/page_discarding_helper.h"

#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "base/feature_list.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/test_future.h"
#include "chrome/browser/performance_manager/policies/discard_eligibility_policy.h"
#include "chrome/browser/performance_manager/policies/freezing_opt_out_checker.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/resource_coordinator/lifecycle_unit.h"
#include "chrome/browser/resource_coordinator/lifecycle_unit_observer.h"
#include "chrome/browser/resource_coordinator/tab_lifecycle_unit_external.h"
#include "chrome/browser/resource_coordinator/tab_lifecycle_unit_source.h"
#include "chrome/browser/resource_coordinator/utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/performance_manager/public/freezing/freezing.h"
#include "components/performance_manager/public/graph/graph.h"
#include "components/performance_manager/public/graph/page_node.h"
#include "components/performance_manager/public/mojom/coordination_unit.mojom-forward.h"
#include "components/performance_manager/public/performance_manager.h"
#include "content/public/browser/page_navigator.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/content_features.h"
#include "content/public/common/referrer.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "ui/base/page_transition_types.h"
#include "ui/base/window_open_disposition.h"
#include "url/gurl.h"

namespace performance_manager::policies {

namespace {

using DiscardReason = DiscardEligibilityPolicy::DiscardReason;

class FaviconWatcher final : public content::WebContentsObserver {
 public:
  explicit FaviconWatcher(content::WebContents* web_contents)
      : content::WebContentsObserver(web_contents) {}
  ~FaviconWatcher() final = default;

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

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

 private:
  // WebContentsObserver
  void DidUpdateFaviconURL(
      content::RenderFrameHost* render_frame_host,
      const std::vector<blink::mojom::FaviconURLPtr>& candidates) final {
    run_loop_.Quit();
  }

  base::RunLoop run_loop_;
};

// Waits for `page_node` to transition to the LoadingState::kLoadedIdle state.
class PageNodeIdleWaiter : public PageNodeObserver,
                           public GraphOwnedDefaultImpl {
 public:
  explicit PageNodeIdleWaiter(base::WeakPtr<PageNode> page_node)
      : page_node_(page_node) {
    PerformanceManager::GetGraph()->AddPageNodeObserver(this);
  }
  ~PageNodeIdleWaiter() override = default;

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

 private:
  // PageNodeObserver:
  void OnLoadingStateChanged(const PageNode* page_node,
                             PageNode::LoadingState previous_state) override {
    if ((page_node == page_node_.get()) &&
        page_node->GetLoadingState() == PageNode::LoadingState::kLoadedIdle) {
      PerformanceManager::GetGraph()->RemovePageNodeObserver(this);
      run_loop_.Quit();
      return;
    }
  }

  base::RunLoop run_loop_;
  const base::WeakPtr<PageNode> page_node_;
};

// Waits for resource coordinator to register a LifecycleUnitState::FROZEN state
// change.
class TabLifecycleUnitFreezeWaiter
    : public resource_coordinator::LifecycleUnitObserver {
 public:
  TabLifecycleUnitFreezeWaiter() {
    resource_coordinator::GetTabLifecycleUnitSource()->AddLifecycleObserver(
        this);
  }
  ~TabLifecycleUnitFreezeWaiter() override {
    resource_coordinator::GetTabLifecycleUnitSource()->RemoveLifecycleObserver(
        this);
  }

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

 private:
  // resource_coordinator::LifecycleUnitObserver:
  void OnLifecycleUnitStateChanged(
      resource_coordinator::LifecycleUnit* lifecycle_unit,
      ::mojom::LifecycleUnitState last_state,
      ::mojom::LifecycleUnitStateChangeReason reason) override {
    if (lifecycle_unit->GetState() == ::mojom::LifecycleUnitState::FROZEN) {
      run_loop_.Quit();
    }
  }

  base::RunLoop run_loop_;
};

// Ensures that `browser` has `num_tabs` tabs.
void EnsureTabsInBrowser(Browser* browser, int num_tabs) {
  EXPECT_EQ(1, browser->tab_strip_model()->count());

  for (int i = 0; i < num_tabs; ++i) {
    ui_test_utils::NavigateToURLWithDisposition(
        browser, GURL("data:text/html;charset=utf-8,hello"),
        i == 0 ? WindowOpenDisposition::CURRENT_TAB
               : WindowOpenDisposition::NEW_BACKGROUND_TAB,
        ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
  }

  EXPECT_EQ(num_tabs, browser->tab_strip_model()->count());
}

// Creates a browser with `num_tabs` tabs.
Browser* CreateBrowserWithTabs(int num_tabs) {
  Browser* current_browser = BrowserList::GetInstance()->GetLastActive();
  ui_test_utils::BrowserChangeObserver new_browser_observer(
      nullptr, ui_test_utils::BrowserChangeObserver::ChangeType::kAdded);
  chrome::NewWindow(current_browser);
  ui_test_utils::WaitForBrowserSetLastActive(new_browser_observer.Wait());
  Browser* new_browser = BrowserList::GetInstance()->GetLastActive();
  EXPECT_NE(new_browser, current_browser);

  EnsureTabsInBrowser(new_browser, num_tabs);
  return new_browser;
}

bool IsTabDiscarded(content::WebContents* web_contents) {
  return resource_coordinator::TabLifecycleUnitExternal::FromWebContents(
             web_contents)
             ->GetTabState() == ::mojom::LifecycleUnitState::DISCARDED;
}

class PageDiscardingHelperBrowserTest
    : public InProcessBrowserTest,
      public ::testing::WithParamInterface<bool> {
 protected:
  PageDiscardingHelperBrowserTest() {
    scoped_feature_list_.InitWithFeatureState(::features::kWebContentsDiscard,
                                              GetParam());
  }
  ~PageDiscardingHelperBrowserTest() override = default;

  void SetUpOnMainThread() override {
    InProcessBrowserTest::SetUpOnMainThread();
    ASSERT_TRUE(embedded_test_server()->Start());
  }

  // Opens a new page in the background, and returns its index in the tab strip.
  int OpenNewBackgroundPage() {
    // Load a page with title and favicon so that some tests can manipulate
    // them.
    content::OpenURLParams page(
        embedded_test_server()->GetURL("/favicon/title2_with_favicon.html"),
        content::Referrer(), WindowOpenDisposition::NEW_BACKGROUND_TAB,
        ui::PAGE_TRANSITION_TYPED, false);
    content::WebContents* contents =
        browser()->OpenURL(page, /*navigation_handle_callback=*/{});
    content::TestNavigationObserver observer(contents);
    observer.set_expected_initial_url(page.url);

    // Wait for the page and the initial favicon to finish loading.
    FaviconWatcher favicon_watcher(contents);
    observer.Wait();
    favicon_watcher.Wait();

    return browser()->tab_strip_model()->GetIndexOfWebContents(contents);
  }

  void UpdatePageTitle(int index) {
    constexpr char16_t kNewTitle[] = u"New title";
    content::WebContents* contents =
        browser()->tab_strip_model()->GetWebContentsAt(index);
    content::TitleWatcher title_watcher(contents, kNewTitle);
    ASSERT_TRUE(content::ExecJs(
        contents, base::StrCat({"document.title = '",
                                base::UTF16ToASCII(kNewTitle), "'"})));
    EXPECT_EQ(title_watcher.WaitAndGetTitle(), kNewTitle);
  }

  void UpdateFavicon(int index) {
    content::WebContents* contents =
        browser()->tab_strip_model()->GetWebContentsAt(index);
    // Change the favicon link from "icon.png" to "icon.svg".
    FaviconWatcher favicon_watcher(contents);
    ASSERT_TRUE(content::ExecJs(
        contents,
        "document.getElementsByTagName('link')[0].href = 'icon.svg'"));
    favicon_watcher.Wait();
  }

  base::WeakPtr<PageNode> GetPageNodeAtIndex(int index) {
    return PerformanceManager::GetPrimaryPageNodeForWebContents(
        browser()->tab_strip_model()->GetWebContentsAt(index));
  }

  void ExpectImmediateDiscard(
      int index,
      DiscardReason discard_reason,
      bool expected_result,
      const base::Location& location = base::Location::Current()) {
    const char* discard_string;
    switch (discard_reason) {
      case DiscardReason::URGENT:
        discard_string = "Urgent";
        break;
      case DiscardReason::PROACTIVE:
        discard_string = "Proactive";
        break;
      case DiscardReason::EXTERNAL:
        discard_string = "External";
        break;
      case DiscardReason::SUGGESTED:
        discard_string = "Suggested";
        break;
      case DiscardReason::FROZEN_WITH_GROWING_MEMORY:
        discard_string = "Frozen with growing memory";
        break;
    }
    SCOPED_TRACE(::testing::Message()
                 << discard_string << " discard from " << location.ToString());

    base::WeakPtr<PageNode> page_node = GetPageNodeAtIndex(index);
    ASSERT_TRUE(page_node);

    Graph* graph = PerformanceManager::GetGraph();
    auto* helper = PageDiscardingHelper::GetFromGraph(graph);
    ASSERT_TRUE(helper);

    const bool discard_success = helper->ImmediatelyDiscardMultiplePages(
        {page_node.get()}, discard_reason);
    EXPECT_EQ(discard_success, expected_result);
    EXPECT_EQ(
        browser()->tab_strip_model()->GetWebContentsAt(index)->WasDiscarded(),
        expected_result);
  }

  void ExpectOptedOutOfDiscardingAndFreezing(
      int index,
      FreezingOptOutChecker& freezing_opt_out_checker,
      bool expect_opted_out) {
    base::WeakPtr<PageNode> page_node = GetPageNodeAtIndex(index);

    ASSERT_TRUE(page_node);
    auto* eligibility_policy =
        DiscardEligibilityPolicy::GetFromGraph(page_node->GetGraph());
    ASSERT_TRUE(eligibility_policy);
    EXPECT_EQ(
        eligibility_policy->IsPageOptedOutOfDiscarding(
            page_node->GetBrowserContextID(), page_node->GetMainFrameUrl()),
        expect_opted_out);
    EXPECT_EQ(
        freezing_opt_out_checker.IsPageOptedOutOfFreezing(
            page_node->GetBrowserContextID(), page_node->GetMainFrameUrl()),
        expect_opted_out);
  }

  content::WebContents* GetWebContentsAt(int index) {
    return browser()->tab_strip_model()->GetWebContentsAt(index);
  }

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

IN_PROC_BROWSER_TEST_P(PageDiscardingHelperBrowserTest, DiscardSpecificPage) {
  // Test urgent and proactive discards in a loop to avoid the overhead of
  // starting a new browser every time.
  // TODO(crbug.com/40899366): Add tests for all the other heuristics in
  // DiscardEligibilityPolicy::CanDiscard().
  for (auto discard_reason :
       {DiscardReason::EXTERNAL, DiscardReason::URGENT,
        DiscardReason::PROACTIVE, DiscardReason::SUGGESTED,
        DiscardReason::FROZEN_WITH_GROWING_MEMORY}) {
    {
      // A background page can be discarded.
      const int index1 = OpenNewBackgroundPage();
      ExpectImmediateDiscard(index1, discard_reason, true);

      // A foreground page blocks URGENT, PROACTIVE and SUGGESTED discards.
      const int index2 = OpenNewBackgroundPage();
      browser()->tab_strip_model()->ActivateTabAt(index2);
      switch (discard_reason) {
        case DiscardReason::EXTERNAL:
        case DiscardReason::FROZEN_WITH_GROWING_MEMORY:
          ExpectImmediateDiscard(index2, discard_reason, true);
          break;
        case DiscardReason::URGENT:
        case DiscardReason::PROACTIVE:
        case DiscardReason::SUGGESTED:
          ExpectImmediateDiscard(index2, discard_reason, false);
          break;
      }
    }

    {
      // Updating title in background blocks PROACTIVE and SUGGESTED discards.
      const int index1 = OpenNewBackgroundPage();
      UpdatePageTitle(index1);
      switch (discard_reason) {
        case DiscardReason::EXTERNAL:
        case DiscardReason::URGENT:
        case DiscardReason::FROZEN_WITH_GROWING_MEMORY:
          ExpectImmediateDiscard(index1, discard_reason, true);
          break;
        case DiscardReason::PROACTIVE:
        case DiscardReason::SUGGESTED:
          ExpectImmediateDiscard(index1, discard_reason, false);
          break;
      }

      // Updating favicon in the foreground does not block discards.
      const int index2 = OpenNewBackgroundPage();
      browser()->tab_strip_model()->ActivateTabAt(index2);
      UpdatePageTitle(index2);
      browser()->tab_strip_model()->ActivateTabAt(index1);
      ExpectImmediateDiscard(index2, discard_reason, true);
    }

    {
      // Updating favicon in background blocks PROACTIVE and SUGGESTED discards.
      const int index1 = OpenNewBackgroundPage();
      UpdateFavicon(index1);
      switch (discard_reason) {
        case DiscardReason::EXTERNAL:
        case DiscardReason::URGENT:
        case DiscardReason::FROZEN_WITH_GROWING_MEMORY:
          ExpectImmediateDiscard(index1, discard_reason, true);
          break;
        case DiscardReason::PROACTIVE:
        case DiscardReason::SUGGESTED:
          ExpectImmediateDiscard(index1, discard_reason, false);
          break;
      }

      // Updating favicon in the foreground does not block discards.
      const int index2 = OpenNewBackgroundPage();
      browser()->tab_strip_model()->ActivateTabAt(index2);
      UpdateFavicon(index2);
      browser()->tab_strip_model()->ActivateTabAt(index1);
      ExpectImmediateDiscard(index2, discard_reason, true);
    }
  }
}

IN_PROC_BROWSER_TEST_P(PageDiscardingHelperBrowserTest, NoDiscardPatterns) {
  const std::string default_browser_context_id =
      browser()->profile()->UniqueId();
  const std::string base_url_pattern =
      embedded_test_server()->base_url().spec();

  // Test urgent and proactive discards in a loop to avoid the overhead of
  // starting a new browser every time.
  for (auto discard_reason :
       {DiscardReason::EXTERNAL, DiscardReason::URGENT,
        DiscardReason::PROACTIVE, DiscardReason::SUGGESTED,
        DiscardReason::FROZEN_WITH_GROWING_MEMORY}) {
    // Also test that FreezingOptOutChecker is hooked up to
    // PageDiscardingHelper correctly.
    base::test::TestFuture<std::string_view> policy_changed_future;
    auto policy_changed_callback = policy_changed_future.GetRepeatingCallback();

    auto* eligibility_policy =
        DiscardEligibilityPolicy::GetFromGraph(PerformanceManager::GetGraph());
    ASSERT_TRUE(eligibility_policy);
    std::unique_ptr<FreezingOptOutChecker> freezing_opt_out_checker =
        std::make_unique<FreezingOptOutChecker>(
            eligibility_policy->GetWeakPtr());

    eligibility_policy->SetNoDiscardPatternsForProfile(
        default_browser_context_id, {base_url_pattern});

    // The callback wasn't set during SetNoDiscardPatternsForProfile(),
    // which should safely do nothing. Future calls should notify the
    // TestFuture.
    freezing_opt_out_checker->SetOptOutPolicyChangedCallback(
        std::move(policy_changed_callback));

    EXPECT_FALSE(policy_changed_future.IsReady());

    // Background page should be blocked from discarding because its url is
    // in NoDiscardPatterns (URGENT, PROACTIVE and SUGGESTED discards only).
    const int index1 = OpenNewBackgroundPage();
    ExpectOptedOutOfDiscardingAndFreezing(index1, *freezing_opt_out_checker,
                                          true);
    switch (discard_reason) {
      case DiscardReason::EXTERNAL:
      case DiscardReason::FROZEN_WITH_GROWING_MEMORY:
        ExpectImmediateDiscard(index1, discard_reason, true);
        break;
      case DiscardReason::URGENT:
      case DiscardReason::PROACTIVE:
      case DiscardReason::SUGGESTED:
        ExpectImmediateDiscard(index1, discard_reason, false);
        break;
    }

    // Empty pattern list.
    eligibility_policy->SetNoDiscardPatternsForProfile(
        default_browser_context_id, {});

    EXPECT_EQ(policy_changed_future.Take(), default_browser_context_id);

    // No longer blocked from discarding. (Need a new page because the first
    // may already be discarded.)
    const int index2 = OpenNewBackgroundPage();
    ExpectOptedOutOfDiscardingAndFreezing(index2, *freezing_opt_out_checker,
                                          false);
    ExpectImmediateDiscard(index2, discard_reason, true);

    // Delete pattern list.
    eligibility_policy->ClearNoDiscardPatternsForProfile(
        default_browser_context_id);

    EXPECT_EQ(policy_changed_future.Take(), default_browser_context_id);

    // With no list available, page should be treated as if it's opted out.
    const int index3 = OpenNewBackgroundPage();
    ExpectOptedOutOfDiscardingAndFreezing(index3, *freezing_opt_out_checker,
                                          true);
    switch (discard_reason) {
      case DiscardReason::EXTERNAL:
      case DiscardReason::FROZEN_WITH_GROWING_MEMORY:
        ExpectImmediateDiscard(index3, discard_reason, true);
        break;
      case DiscardReason::URGENT:
      case DiscardReason::PROACTIVE:
      case DiscardReason::SUGGESTED:
        ExpectImmediateDiscard(index3, discard_reason, false);
        break;
    }
  }
}

// Integration test verifying that discarding is disallowed for a tab which was
// just discarded but still has a main frame.
IN_PROC_BROWSER_TEST_P(PageDiscardingHelperBrowserTest,
                       DiscardedTabCannotBeDiscarded) {
  Graph* graph = PerformanceManager::GetGraph();
  auto* helper = PageDiscardingHelper::GetFromGraph(graph);
  auto* eligibility_policy = DiscardEligibilityPolicy::GetFromGraph(graph);
  ASSERT_TRUE(helper);

  OpenNewBackgroundPage();
  EXPECT_EQ(browser()->tab_strip_model()->count(), 2);
  base::WeakPtr<PageNode> page_to_discard = GetPageNodeAtIndex(1);

  // Keep-alive the process hosting the background page's main frame, to prevent
  // fast shutdown. This ensures that when the WebContentsDiscard feature is
  // enabled, we test the code path in which a tab is discarded but still has a
  // main frame (that situation cannot occur with the feature disabled).
  page_to_discard->GetWebContents()
      ->GetPrimaryMainFrame()
      ->GetProcess()
      ->IncrementPendingReuseRefCount();

  // Discard a background page.
  ASSERT_TRUE(page_to_discard);
  EXPECT_EQ(CanDiscardResult::kEligible,
            eligibility_policy->CanDiscard(
                page_to_discard.get(), DiscardReason::URGENT,
                /*minimum_time_in_background=*/base::TimeDelta()));
  ASSERT_TRUE(helper->ImmediatelyDiscardMultiplePages({page_to_discard.get()},
                                                      DiscardReason::URGENT));
  ASSERT_EQ(GetParam(),
            base::FeatureList::IsEnabled(::features::kWebContentsDiscard));
  if (GetParam()) {
    EXPECT_TRUE(page_to_discard->GetWebContents()->GetPrimaryMainFrame());
  }

  // The discarded page should no longer be eligible for discarding.
  base::WeakPtr<PageNode> discarded_page = GetPageNodeAtIndex(1);
  ASSERT_TRUE(discarded_page);
  EXPECT_EQ(CanDiscardResult::kDisallowed,
            eligibility_policy->CanDiscard(
                discarded_page.get(), DiscardReason::URGENT,
                /*minimum_time_in_background=*/base::TimeDelta()));
}

// Regression test for crbug.com/386801193. Ensure discarded tabs remain
// eligible for successive discard operations following a reactivation / reload.
IN_PROC_BROWSER_TEST_P(PageDiscardingHelperBrowserTest,
                       DiscardedTabEligibleForSuccessiveDiscards) {
  // Add a new background tab.
  OpenNewBackgroundPage();
  EXPECT_EQ(browser()->tab_strip_model()->count(), 2);

  tabs::TabInterface* tab1 = browser()->tab_strip_model()->GetTabAtIndex(0);
  tabs::TabInterface* tab2 = browser()->tab_strip_model()->GetTabAtIndex(1);
  EXPECT_EQ(browser()->tab_strip_model()->GetActiveTab(), tab1);

  // Attempt to discard the background tab.
  const auto attempt_discard = [this]() {
    base::WeakPtr<PageNode> discard_target_page_node = GetPageNodeAtIndex(1);
    ASSERT_TRUE(discard_target_page_node);
    Graph* graph = PerformanceManager::GetGraph();
    auto* eligibility_policy = DiscardEligibilityPolicy::GetFromGraph(graph);
    ASSERT_TRUE(eligibility_policy);
    EXPECT_EQ(CanDiscardResult::kEligible,
              eligibility_policy->CanDiscard(discard_target_page_node.get(),
                                             DiscardReason::URGENT,
                                             base::TimeDelta()));
    auto* helper = PageDiscardingHelper::GetFromGraph(graph);
    ASSERT_TRUE(helper);
    std::optional<base::TimeTicks> first_discarded_at =
        helper->DiscardAPage(DiscardReason::URGENT, base::TimeDelta());

    EXPECT_TRUE(first_discarded_at.has_value());
  };
  attempt_discard();

  // Ensure the background tab has been discarded.
  EXPECT_FALSE(tab1->GetContents()->WasDiscarded());
  EXPECT_TRUE(tab2->GetContents()->WasDiscarded());

  // Activate and reload the discarded background page.
  content::TestNavigationObserver reload_waiter(tab2->GetContents(), 1);
  browser()->tab_strip_model()->ActivateTabAt(1);
  EXPECT_EQ(browser()->tab_strip_model()->GetActiveTab(), tab2);
  reload_waiter.Wait();

  EXPECT_FALSE(tab1->GetContents()->WasDiscarded());
  EXPECT_FALSE(tab2->GetContents()->WasDiscarded());

  // Background the discarded tab again and attempt another discard.
  browser()->tab_strip_model()->ActivateTabAt(0);
  EXPECT_EQ(browser()->tab_strip_model()->GetActiveTab(), tab1);
  attempt_discard();

  // Ensure the background tab has been discarded again.
  EXPECT_FALSE(tab1->GetContents()->WasDiscarded());
  EXPECT_TRUE(tab2->GetContents()->WasDiscarded());
}

// Regression test for crbug.com/394242157. Ensure that discarding a frozen tab
// does not result in invalid lifecycle state transitions.
IN_PROC_BROWSER_TEST_P(PageDiscardingHelperBrowserTest,
                       DiscardingFrozenTabCorrectlyTransitionsLifecycleState) {
  // Add a new background tab.
  OpenNewBackgroundPage();
  EXPECT_EQ(browser()->tab_strip_model()->count(), 2);

  tabs::TabInterface* tab1 = browser()->tab_strip_model()->GetTabAtIndex(0);
  tabs::TabInterface* tab2 = browser()->tab_strip_model()->GetTabAtIndex(1);

  EXPECT_EQ(browser()->tab_strip_model()->GetActiveTab(), tab1);

  // Ensure the off-thread page node has registered the background tab as idle.
  PageNodeIdleWaiter page_node_idle_waiter(GetPageNodeAtIndex(1));
  page_node_idle_waiter.Wait();

  // Freeze the background tab and ensure the state transition has been
  // registered by the tab lifecycle unit.
  auto freezing_vote =
      std::make_unique<performance_manager::freezing::FreezingVote>(
          tab2->GetContents());

  TabLifecycleUnitFreezeWaiter freeze_waiter;
  freeze_waiter.Wait();

  // Discard the background tab.
  const auto attempt_discard = [this]() {
    base::WeakPtr<PageNode> discard_target_page_node = GetPageNodeAtIndex(1);
    ASSERT_TRUE(discard_target_page_node);
    Graph* graph = PerformanceManager::GetGraph();
    auto* eligibility_policy = DiscardEligibilityPolicy::GetFromGraph(graph);
    ASSERT_TRUE(eligibility_policy);
    EXPECT_EQ(CanDiscardResult::kEligible,
              eligibility_policy->CanDiscard(discard_target_page_node.get(),
                                             DiscardReason::URGENT,
                                             base::TimeDelta()));
    auto* helper = PageDiscardingHelper::GetFromGraph(graph);
    ASSERT_TRUE(helper);
    std::optional<base::TimeTicks> first_discarded_at =
        helper->DiscardAPage(DiscardReason::URGENT, base::TimeDelta());

    EXPECT_TRUE(first_discarded_at.has_value());
  };
  attempt_discard();

  // Assert the background tab has been successfully discarded.
  EXPECT_FALSE(tab1->GetContents()->WasDiscarded());
  EXPECT_TRUE(tab2->GetContents()->WasDiscarded());

  // Assert the lifecycle state transition is correctly registered in the
  // lifecycle unit.
  auto* lifecycle_unit =
      resource_coordinator::TabLifecycleUnitExternal::FromWebContents(
          tab2->GetContents());
  EXPECT_EQ(::mojom::LifecycleUnitState::DISCARDED,
            lifecycle_unit->GetTabState());
}

IN_PROC_BROWSER_TEST_P(PageDiscardingHelperBrowserTest,
                       DiscardTabsWithMinimizedWindow) {
  // Minimize browser.
  EnsureTabsInBrowser(browser(), 2);
  browser()->window()->Minimize();

  // Request to discard pages a few times.
  auto* helper =
      PageDiscardingHelper::GetFromGraph(PerformanceManager::GetGraph());
  ASSERT_TRUE(helper);
  for (int i = 0; i < 3; ++i) {
    helper->DiscardAPage(DiscardReason::URGENT,
                         /*minimum_time_in_background=*/base::TimeDelta());
  }

  // The active tab is the minimized window isn't discarded.
  EXPECT_FALSE(IsTabDiscarded(GetWebContentsAt(0)));

  // This non-active tab is discarded.
  EXPECT_TRUE(IsTabDiscarded(GetWebContentsAt(1)));
}

IN_PROC_BROWSER_TEST_P(PageDiscardingHelperBrowserTest,
                       DiscardTabsWithOccludedWindow) {
  // This browser will be occluded.
  EnsureTabsInBrowser(browser(), 2);
  browser()->window()->SetBounds(gfx::Rect(10, 10, 10, 10));
  // Create another browser which occludes the previous browser.
  Browser* other_browser = CreateBrowserWithTabs(1);
  EXPECT_NE(other_browser, browser());
  other_browser->window()->SetBounds(gfx::Rect(0, 0, 100, 100));

  // Request to discard pages a few times.
  auto* helper =
      PageDiscardingHelper::GetFromGraph(PerformanceManager::GetGraph());
  ASSERT_TRUE(helper);
  for (int i = 0; i < 3; ++i) {
    helper->DiscardAPage(DiscardReason::URGENT,
                         /*minimum_time_in_background=*/base::TimeDelta());
  }

  // The active tab is the occluded window isn't discarded.
  EXPECT_FALSE(IsTabDiscarded(GetWebContentsAt(0)));

  // This non-active tab is discarded.
  EXPECT_TRUE(IsTabDiscarded(GetWebContentsAt(1)));
}

INSTANTIATE_TEST_SUITE_P(
    ,
    PageDiscardingHelperBrowserTest,
    ::testing::Values(false, true),
    [](const ::testing::TestParamInfo<
        PageDiscardingHelperBrowserTest::ParamType>& info) {
      return info.param ? "RetainedWebContents" : "UnretainedWebContents";
    });

}  // namespace

}  // namespace performance_manager::policies