UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ObjectRefTrackingTestBase.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#if WITH_LOW_LEVEL_TESTS
6
7#include "TestHarness.h"
8
9#include "CoreMinimal.h"
11#include "Misc/ScopeLock.h"
12
13#if UE_WITH_OBJECT_HANDLE_LATE_RESOLVE
15{
16 UE::CoreUObject::Private::FPackedObjectRef PackedObjectRef = UE::CoreUObject::Private::MakePackedObjectRef(Obj);
17 return FObjectHandle{ PackedObjectRef.EncodedRef };
18}
19
20#endif
21
23{
24public:
25 uint32 GetNumResolves() const { return NumResolves; }
27 uint32 GetNumReads() const { return NumReads; }
28 uint32 GetNumReads(const UObject* const Obj)
29 {
30 const auto* N = NumReadsPerObject.Find(Obj);
31 return N ? *N : 0;
32 }
34 {
35 return NumReadsPerObject;
36 }
37
39 {
40 public:
42 : Test(InTest)
47 {
48 Test.InstallCallbacks();
49 }
50
52 {
53 Test.RemoveCallbacks();
54 Test.NumReadsPerObject.Empty();
55 }
56
57 void TestNumResolves(const TCHAR* What, uint32 ExpectedDelta)
58 {
59#if UE_WITH_OBJECT_HANDLE_TRACKING
60 TEST_EQUAL(What, OriginalNumResolves + ExpectedDelta, Test.GetNumResolves());
61#endif
62 }
63
65 {
66#if UE_WITH_OBJECT_HANDLE_TRACKING
67 TEST_EQUAL(What, OriginalNumFailedResolves + ExpectedDelta, Test.GetNumFailedResolves());
68#endif
69 }
70
71 void TestNumReads(const TCHAR* What, uint32 ExpectedDelta, bool bAllowAdditionalReads = false)
72 {
73#if UE_WITH_OBJECT_HANDLE_TRACKING
74 bool bValue = false;
76 {
77 INFO(What);
78 CHECK(Test.GetNumReads() >= OriginalNumReads + ExpectedDelta);
79 }
80 else
81 {
82 bValue = OriginalNumReads + ExpectedDelta == Test.GetNumReads();
83 TEST_TRUE(What, bValue);
84 }
85#endif
86 }
87
88 void TestNumReads(const TCHAR* What, const UObject* const Obj, uint32 ExpectedDelta, bool bAllowAdditionalReads = false)
89 {
90#if UE_WITH_OBJECT_HANDLE_TRACKING
91 bool bValue = false;
93 {
94 INFO(What);
95 CHECK(Test.GetNumReads(Obj) >= OriginalNumReadsPerObject.FindOrAdd(Obj, 0) + ExpectedDelta);
96 }
97 else
98 {
99 bValue = OriginalNumReadsPerObject.FindOrAdd(Obj, 0) + ExpectedDelta == Test.GetNumReads(Obj);
100 TEST_TRUE(What, bValue);
101 }
102#endif
103 }
104
105 private:
111 };
112
113private:
114#if UE_WITH_OBJECT_HANDLE_TRACKING
115 static void OnRefResolved(const FObjectRef& ObjectRef, UPackage* Pkg, UObject* Obj)
116 {
117 NumResolves++;
118 if (!ObjectRef.IsNull() && !Obj)
119 {
121 }
122 }
123 static void OnRefRead(const TArrayView<const UObject* const>& Objects)
124 {
125 NumReads++;
126 for (auto Obj : Objects)
127 {
128 ++NumReadsPerObject.FindOrAdd(Obj, 0);
129 }
130 }
131#endif
132
133 static void InstallCallbacks()
134 {
135#if UE_WITH_OBJECT_HANDLE_TRACKING
136 ResolvedCallbackHandle = UE::CoreUObject::AddObjectHandleReferenceResolvedCallback(OnRefResolved);
137 HandleReadCallbackHandle = UE::CoreUObject::AddObjectHandleReadCallback(OnRefRead);
138#endif
139 }
140
141 static void RemoveCallbacks()
142 {
143#if UE_WITH_OBJECT_HANDLE_TRACKING
144 UE::CoreUObject::RemoveObjectHandleReferenceResolvedCallback(ResolvedCallbackHandle);
145 UE::CoreUObject::RemoveObjectHandleReadCallback(HandleReadCallbackHandle);
146#endif
147 }
148
149#if UE_WITH_OBJECT_HANDLE_TRACKING
150 static UE::CoreUObject::FObjectHandleTrackingCallbackId ResolvedCallbackHandle;
151 static UE::CoreUObject::FObjectHandleTrackingCallbackId HandleReadCallbackHandle;
152#endif
153 static thread_local uint32 NumResolves;
154 static thread_local uint32 NumFailedResolves;
155 static thread_local uint32 NumReads;
157};
158
159#endif
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define CHECK(...)
Definition LowLevelTestAdapter.h:126
#define INFO(Message)
Definition LowLevelTestAdapter.h:152
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition ArrayView.h:139
Definition UnrealString.h.inl:34
Definition Object.h:95
Definition Package.h:216
Definition TestUtils.cpp:8