File: bfcache_policy_unittest.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 (100 lines) | stat: -rw-r--r-- 3,936 bytes parent folder | download | duplicates (9)
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
// Copyright 2021 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/policies/bfcache_policy.h"

#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "components/performance_manager/graph/frame_node_impl.h"
#include "components/performance_manager/graph/graph_impl.h"
#include "components/performance_manager/graph/system_node_impl.h"
#include "components/performance_manager/test_support/graph_test_harness.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace performance_manager::policies {

namespace {

// Mock version of a performance_manager::BFCachePolicy.
class LenientMockBFCachePolicy : public BFCachePolicy {
 public:
  LenientMockBFCachePolicy() = default;
  ~LenientMockBFCachePolicy() override = default;
  LenientMockBFCachePolicy(const LenientMockBFCachePolicy& other) = delete;
  LenientMockBFCachePolicy& operator=(const LenientMockBFCachePolicy&) = delete;
  MOCK_METHOD(void,
              MaybeFlushBFCache,
              (const PageNode* page_node,
               MemoryPressureLevel memory_pressure_level),
              (override));
};
using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel;
using MockBFCachePolicy = ::testing::StrictMock<LenientMockBFCachePolicy>;

}  // namespace

class BFCachePolicyTest : public GraphTestHarness {
 public:
  BFCachePolicyTest() = default;
  ~BFCachePolicyTest() override = default;
  BFCachePolicyTest(const BFCachePolicyTest& other) = delete;
  BFCachePolicyTest& operator=(const BFCachePolicyTest&) = delete;

  void OnGraphCreated(GraphImpl* graph) override {
    // Create the policy and pass it to the graph.
    auto policy = std::make_unique<MockBFCachePolicy>();
    policy_ = policy.get();
    graph->PassToGraph(std::move(policy));

    page_node_ = CreateNode<performance_manager::PageNodeImpl>();

    // Add 2 main frame nodes to the page and mark one of them as not current to
    // pretend that one of these main frames is in the bfcache.
    process_node_ = CreateNode<ProcessNodeImpl>();
    frame_node_1_ =
        CreateFrameNodeAutoId(process_node_.get(), page_node_.get());
    frame_node_2_ =
        CreateFrameNodeAutoId(process_node_.get(), page_node_.get());
    // TODO: There is 2 current frame nodes and then we change 1 to not be
    // current anymore but that breaks the "only 1 current frame" invariant.
    FrameNodeImpl::UpdateCurrentFrame(frame_node_2_.get(), nullptr, graph);
  }

 protected:
  performance_manager::TestNodeWrapper<performance_manager::PageNodeImpl>
      page_node_;
  performance_manager::TestNodeWrapper<performance_manager::ProcessNodeImpl>
      process_node_;
  performance_manager::TestNodeWrapper<performance_manager::FrameNodeImpl>
      frame_node_1_;
  performance_manager::TestNodeWrapper<performance_manager::FrameNodeImpl>
      frame_node_2_;

  raw_ptr<MockBFCachePolicy> policy_;
};

TEST_F(BFCachePolicyTest, BFCacheFlushedOnMemoryPressure) {
  page_node_->SetIsVisible(true);
  page_node_->SetLoadingState(PageNode::LoadingState::kLoadedBusy);
  ::testing::Mock::VerifyAndClearExpectations(policy_);

  EXPECT_CALL(
      *policy_,
      MaybeFlushBFCache(page_node_.get(),
                        MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE));
  GetSystemNode()->OnMemoryPressureForTesting(
      MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE);
  ::testing::Mock::VerifyAndClearExpectations(policy_);

  EXPECT_CALL(
      *policy_,
      MaybeFlushBFCache(page_node_.get(),
                        MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL));
  GetSystemNode()->OnMemoryPressureForTesting(
      MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL);
  ::testing::Mock::VerifyAndClearExpectations(policy_);
}

}  // namespace performance_manager::policies