5#include "fxjs/gc/gced_tree_node.h"
9#include "core/fxcrt/observed_ptr.h"
10#include "fxjs/gc/heap.h"
11#include "testing/fxgc_unittest.h"
12#include "testing/gtest/include/gtest/gtest.h"
13#include "testing/v8_test_environment.h"
14#include "v8/include/cppgc/allocation.h"
15#include "v8/include/cppgc/persistent.h"
19class ObservableGCedTreeNodeForTest
20 :
public GCedTreeNode<ObservableGCedTreeNodeForTest>,
26 ObservableGCedTreeNodeForTest() =
default;
42 ObservableGCedTreeNodeForTest*
root()
const {
return root_; }
46 return cppgc::MakeGarbageCollected<ObservableGCedTreeNodeForTest>(
47 heap()->GetAllocationHandle());
51 for (
int i = 0; i < 4; ++i) {
52 parent->AppendFirstChild(
53 cppgc::MakeGarbageCollected<ObservableGCedTreeNodeForTest>(
54 heap()->GetAllocationHandle()));
59 for (
int i = 0; i < 4; ++i) {
60 parent->AppendLastChild(
61 cppgc::MakeGarbageCollected<ObservableGCedTreeNodeForTest>(
62 heap()->GetAllocationHandle()));
67 cppgc::Persistent<ObservableGCedTreeNodeForTest> root_;
72 ObservedPtr<ObservableGCedTreeNodeForTest> watcher(root());
78 ObservedPtr<ObservableGCedTreeNodeForTest> watcher(CreateNode());
80 EXPECT_FALSE(watcher);
85 ObservedPtr<ObservableGCedTreeNodeForTest> watcher(CreateNode());
86 root()->AppendFirstChild(watcher.Get());
90 root()->RemoveChild(watcher.Get());
93 EXPECT_FALSE(watcher);
96 watcher.Reset(CreateNode());
97 root()->AppendFirstChild(watcher.Get());
98 AddClutterToFront(root());
99 AddClutterToBack(root());
102 EXPECT_TRUE(watcher);
103 root()->RemoveChild(watcher.Get());
106 EXPECT_FALSE(watcher);
111 ObservedPtr<ObservableGCedTreeNodeForTest> watcher(CreateNode());
112 root()->AppendFirstChild(watcher.Get());
115 ASSERT_TRUE(watcher);
116 watcher->RemoveSelfIfParented();
119 EXPECT_FALSE(watcher);
124 AddClutterToFront(root());
125 ObservedPtr<ObservableGCedTreeNodeForTest> watcher(CreateNode());
126 root()->AppendFirstChild(watcher.Get());
127 root()->InsertBefore(root()->GetFirstChild(), root()->GetLastChild());
128 root()->InsertAfter(root()->GetLastChild(), root()->GetFirstChild());
131 EXPECT_TRUE(watcher);
132 root()->RemoveChild(watcher.Get());
135 EXPECT_FALSE(watcher);
139 std::map<cppgc::Persistent<ObservableGCedTreeNodeForTest>,
int> score;
140 ObservableGCedTreeNodeForTest* node = CreateNode();
142 EXPECT_EQ(100, score[node]);
void AddClutterToFront(ObservableGCedTreeNodeForTest *parent)
ObservableGCedTreeNodeForTest * CreateNode()
void AddClutterToBack(ObservableGCedTreeNodeForTest *parent)
~GCedTreeNodeUnitTest() override=default
GCedTreeNodeUnitTest()=default
ObservableGCedTreeNodeForTest * root() const
TEST_F(GCedTreeNodeUnitTest, OneRefence)
#define CONSTRUCT_VIA_MAKE_GARBAGE_COLLECTED