File: node_attached_data_unittest.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 (105 lines) | stat: -rw-r--r-- 3,379 bytes parent folder | download | duplicates (6)
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
// Copyright 2019 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/graph/node_attached_data.h"

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "components/performance_manager/graph/graph_impl.h"
#include "components/performance_manager/graph/page_node_impl.h"
#include "components/performance_manager/test_support/graph_test_harness.h"
#include "components/performance_manager/test_support/mock_graphs.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace performance_manager {

namespace {

// An implementation of map-stored user-data using the public interface.
class BarData : public ExternalNodeAttachedDataImpl<BarData> {
 public:
  explicit BarData(const PageNode* page_node) : page_node_(page_node) {}

  ~BarData() override = default;

  raw_ptr<const PageNode> page_node_ = nullptr;
};

// A second implementation, which should not be confused with BarData.
class FooData : public ExternalNodeAttachedDataImpl<FooData> {
 public:
  explicit FooData(const PageNode* page_node) : page_node_(page_node) {}

  ~FooData() override = default;

  raw_ptr<const PageNode> page_node_ = nullptr;

  // Exposes weak pointers to track when the data is deleted.
  base::WeakPtrFactory<FooData> weak_factory_{this};
};

}  // namespace

using NodeAttachedDataTest = GraphTestHarness;

TEST_F(NodeAttachedDataTest, PublicNodeAttachedData) {
  base::WeakPtr<FooData> weak_foo_data;
  base::WeakPtr<FooData> weak_foo_data2;
  {
    MockMultiplePagesInSingleProcessGraph mock_graph(graph());
    const PageNode* page_node = mock_graph.page.get();
    const PageNode* other_page_node = mock_graph.other_page.get();

    BarData* bar_data = BarData::Get(page_node);
    EXPECT_FALSE(bar_data);

    bar_data = BarData::GetOrCreate(page_node);
    EXPECT_TRUE(bar_data);
    EXPECT_EQ(page_node, bar_data->page_node_);

    EXPECT_EQ(bar_data, BarData::Get(page_node));

    // Make sure FooData and BarData are not aliased.
    FooData* foo_data = FooData::Get(page_node);
    EXPECT_FALSE(foo_data);

    foo_data = FooData::GetOrCreate(page_node);
    EXPECT_TRUE(foo_data);
    EXPECT_EQ(page_node, foo_data->page_node_);

    EXPECT_EQ(foo_data, FooData::Get(page_node));

    EXPECT_NE(foo_data->GetKey(), bar_data->GetKey());

    // Make sure data can be stored in multiple nodes.
    FooData* foo_data2 = FooData::Get(other_page_node);
    EXPECT_FALSE(foo_data2);

    foo_data2 = FooData::GetOrCreate(other_page_node);
    EXPECT_TRUE(foo_data2);
    EXPECT_EQ(other_page_node, foo_data2->page_node_);

    EXPECT_EQ(foo_data2, FooData::Get(other_page_node));

    EXPECT_NE(foo_data, foo_data2);
    EXPECT_EQ(foo_data->GetKey(), foo_data2->GetKey());

    // Make sure data can be destroyed.
    EXPECT_TRUE(BarData::Destroy(page_node));
    EXPECT_FALSE(BarData::Destroy(page_node));
    EXPECT_FALSE(BarData::Get(page_node));

    // Data should also be destroyed when the node is deleted.
    weak_foo_data = foo_data->weak_factory_.GetWeakPtr();
    weak_foo_data2 = foo_data->weak_factory_.GetWeakPtr();
    EXPECT_TRUE(weak_foo_data);
    EXPECT_TRUE(weak_foo_data2);
  }

  // Mock graph went out of scope.
  EXPECT_FALSE(weak_foo_data);
  EXPECT_FALSE(weak_foo_data2);
}

}  // namespace performance_manager