5#include "fxjs/gc/gced_tree_node_mixin.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 ObservableGCedTreeNodeMixinForTest
20 :
public cppgc::GarbageCollected<ObservableGCedTreeNodeMixinForTest>,
21 public GCedTreeNodeMixin<ObservableGCedTreeNodeMixinForTest>,
27 void Trace(cppgc::Visitor* visitor)
const override {
28 GCedTreeNodeMixin<ObservableGCedTreeNodeMixinForTest>::Trace(visitor);
32 ObservableGCedTreeNodeMixinForTest() =
default;
48 ObservableGCedTreeNodeMixinForTest*
root()
const {
return root_; }
52 return cppgc::MakeGarbageCollected<ObservableGCedTreeNodeMixinForTest>(
53 heap()->GetAllocationHandle());
57 for (
int i = 0; i < 4; ++i) {
58 parent->AppendFirstChild(
59 cppgc::MakeGarbageCollected<ObservableGCedTreeNodeMixinForTest>(
60 heap()->GetAllocationHandle()));
65 for (
int i = 0; i < 4; ++i) {
66 parent->AppendLastChild(
67 cppgc::MakeGarbageCollected<ObservableGCedTreeNodeMixinForTest>(
68 heap()->GetAllocationHandle()));
73 cppgc::Persistent<ObservableGCedTreeNodeMixinForTest> root_;
78 ObservedPtr<ObservableGCedTreeNodeMixinForTest> watcher(root());
84 ObservedPtr<ObservableGCedTreeNodeMixinForTest> watcher(CreateNode());
86 EXPECT_FALSE(watcher);
91 ObservedPtr<ObservableGCedTreeNodeMixinForTest> watcher(CreateNode());
92 root()->AppendFirstChild(watcher.Get());
96 root()->RemoveChild(watcher.Get());
99 EXPECT_FALSE(watcher);
102 watcher.Reset(CreateNode());
103 root()->AppendFirstChild(watcher.Get());
104 AddClutterToFront(root());
105 AddClutterToBack(root());
108 EXPECT_TRUE(watcher);
109 root()->RemoveChild(watcher.Get());
112 EXPECT_FALSE(watcher);
117 ObservedPtr<ObservableGCedTreeNodeMixinForTest> watcher(CreateNode());
118 root()->AppendFirstChild(watcher.Get());
121 ASSERT_TRUE(watcher);
122 watcher->RemoveSelfIfParented();
125 EXPECT_FALSE(watcher);
130 AddClutterToFront(root());
131 ObservedPtr<ObservableGCedTreeNodeMixinForTest> watcher(CreateNode());
132 root()->AppendFirstChild(watcher.Get());
133 root()->InsertBefore(root()->GetFirstChild(), root()->GetLastChild());
134 root()->InsertAfter(root()->GetLastChild(), root()->GetFirstChild());
137 EXPECT_TRUE(watcher);
138 root()->RemoveChild(watcher.Get());
141 EXPECT_FALSE(watcher);
145 std::map<cppgc::Persistent<ObservableGCedTreeNodeMixinForTest>,
int> score;
146 ObservableGCedTreeNodeMixinForTest* node = CreateNode();
148 EXPECT_EQ(100, score[node]);
GCedTreeNodeMixinUnitTest()=default
void AddClutterToBack(ObservableGCedTreeNodeMixinForTest *parent)
~GCedTreeNodeMixinUnitTest() override=default
ObservableGCedTreeNodeMixinForTest * root() const
ObservableGCedTreeNodeMixinForTest * CreateNode()
void AddClutterToFront(ObservableGCedTreeNodeMixinForTest *parent)
TEST_F(GCedTreeNodeMixinUnitTest, OneRefence)
#define CONSTRUCT_VIA_MAKE_GARBAGE_COLLECTED