File: freezing_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 (85 lines) | stat: -rw-r--r-- 2,904 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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <optional>

#include "base/run_loop.h"
#include "components/performance_manager/freezing/freezer.h"
#include "components/performance_manager/public/graph/graph.h"
#include "components/performance_manager/public/graph/page_node.h"
#include "components/performance_manager/public/performance_manager.h"
#include "components/performance_manager/test_support/performance_manager_browsertest_harness.h"
#include "content/public/browser/visibility.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"

namespace performance_manager {

namespace {

using PerformanceManagerFreezingBrowserTest =
    PerformanceManagerBrowserTestHarness;

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

  void SetExpectedLifecycleState(PageNode::LifecycleState lifecycle_state) {
    run_loop_ = std::make_unique<base::RunLoop>();
    expected_lifecycle_state_ = lifecycle_state;
  }
  void WaitForExpectedLifecycleState() {
    CHECK(run_loop_);
    run_loop_->Run();
    run_loop_.reset();
  }

  // PageNodeObserver:
  void OnPageLifecycleStateChanged(const PageNode* page_node) override {
    CHECK(run_loop_);
    EXPECT_EQ(page_node->GetLifecycleState(),
              expected_lifecycle_state_.value());
    run_loop_->Quit();
  }

 private:
  std::unique_ptr<base::RunLoop> run_loop_;
  std::optional<PageNode::LifecycleState> expected_lifecycle_state_;
};

}  // namespace

// Verifies that a page can be frozen a second time after being unfrozen by a
// visibility change (regression test for issue fixed by crrev.com/c/5827022).
IN_PROC_BROWSER_TEST_F(PerformanceManagerFreezingBrowserTest, FreezeTwice) {
  Freezer freezer;
  ASSERT_TRUE(NavigateToURL(shell(), GURL("about:blank")));
  content::WebContents* contents = shell()->web_contents();
  contents->WasHidden();
  base::WeakPtr<PageNode> page_node =
      PerformanceManager::GetPrimaryPageNodeForWebContents(contents);

  PageLifecycleWaiter waiter;
  Graph* graph = PerformanceManager::GetGraph();
  graph->AddPageNodeObserver(&waiter);

  waiter.SetExpectedLifecycleState(PageNode::LifecycleState::kFrozen);
  freezer.MaybeFreezePageNode(page_node.get());
  waiter.WaitForExpectedLifecycleState();

  waiter.SetExpectedLifecycleState(PageNode::LifecycleState::kRunning);
  contents->WasShown();
  waiter.WaitForExpectedLifecycleState();

  contents->WasHidden();
  waiter.SetExpectedLifecycleState(PageNode::LifecycleState::kFrozen);
  freezer.MaybeFreezePageNode(page_node.get());
  waiter.WaitForExpectedLifecycleState();

  graph->RemovePageNodeObserver(&waiter);
}

}  // namespace performance_manager