forked from stride3d/stride
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestEntity.cs
More file actions
314 lines (252 loc) · 12.7 KB
/
TestEntity.cs
File metadata and controls
314 lines (252 loc) · 12.7 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
using Stride.Core;
using Stride.Core.Annotations;
using Stride.Engine.Design;
using Stride.Rendering;
namespace Stride.Engine.Tests
{
/// <summary>
/// Tests for <see cref="Entity"/> and <see cref="EntityComponentCollection"/>.
/// </summary>
public class TestEntity
{
/// <summary>
/// Test various manipulation on Entity.Components with TransformComponent and CustomComponent
/// </summary>
[Fact]
public void TestComponents()
{
var entity = new Entity();
// Plug an event handler to track events
var events = new List<EntityComponentEvent>();
entity.EntityManager = new DelegateEntityComponentNotify(evt => events.Add(evt));
// Make sure that an entity has a transform component
Assert.NotNull(entity.Transform);
Assert.Single(entity.Components);
Assert.Equal(entity.Transform, entity.Components[0]);
// Remove Transform
var oldTransform = entity.Transform;
entity.Components.RemoveAt(0);
Assert.Null(entity.Transform);
// Check that events is correctly propagated
Assert.Equal(new List<EntityComponentEvent>() { new EntityComponentEvent(entity, 0, oldTransform, null) }, events);
events.Clear();
// Re-add transform
var transform = new TransformComponent();
entity.Components.Add(transform);
Assert.NotNull(entity.Transform);
// Check that events is correctly propagated
Assert.Equal(new List<EntityComponentEvent>() { new EntityComponentEvent(entity, 0, null, transform) }, events);
events.Clear();
// We cannot add a single component
var invalidOpException = Assert.Throws<InvalidOperationException>(() => entity.Components.Add(new TransformComponent()));
Assert.Equal($"Cannot add a component of type [{typeof(TransformComponent)}] multiple times", invalidOpException.Message);
invalidOpException = Assert.Throws<InvalidOperationException>(() => entity.Components.Add(transform));
Assert.Equal("Cannot add a same component multiple times. Already set at index [0]", invalidOpException.Message);
// We cannot add a null component
Assert.Throws<ArgumentNullException>(() => entity.Components.Add(null));
// Replace Transform
var custom = new CustomEntityComponent();
entity.Components[0] = custom;
Assert.Null(entity.Transform);
// Check that events is correctly propagated
Assert.Equal(new List<EntityComponentEvent>() { new EntityComponentEvent(entity, 0, transform, custom) }, events);
events.Clear();
// Add again transform component
transform = new TransformComponent();
entity.Components.Add(transform);
Assert.NotNull(entity.Transform);
Assert.Equal(transform, entity.Components[1]);
// Check that TransformComponent is on index 1 now
Assert.Equal(new List<EntityComponentEvent>() { new EntityComponentEvent(entity, 1, null, transform) }, events);
events.Clear();
// Clear components and check that Transform is also removed
entity.Components.Clear();
Assert.Empty(entity.Components);
Assert.Null(entity.Transform);
// Check that events is correctly propagated
Assert.Equal(new List<EntityComponentEvent>()
{
new EntityComponentEvent(entity, 1, transform, null),
new EntityComponentEvent(entity, 0, custom, null),
}, events);
events.Clear();
}
/// <summary>
/// Tests multiple components.
/// </summary>
[Fact]
public void TestMultipleComponents()
{
// Check that TransformComponent cannot be added multiple times
Assert.False(EntityComponentAttributes.Get<TransformComponent>().AllowMultipleComponents);
// Check that CustomEntityComponent can be added multiple times
Assert.True(EntityComponentAttributes.Get<CustomEntityComponent>().AllowMultipleComponents);
// Check that DerivedEntityComponentBase can be added multiple times
Assert.True(EntityComponentAttributes.Get<DerivedEntityComponent>().AllowMultipleComponents);
var entity = new Entity();
var transform = entity.Get<TransformComponent>();
Assert.NotNull(transform);
Assert.Equal(entity.Transform, transform);
var custom = entity.GetOrCreate<CustomEntityComponent>();
Assert.NotNull(custom);
var custom2 = new CustomEntityComponent();
entity.Components.Add(custom2);
Assert.Equal(custom, entity.Get<CustomEntityComponent>());
var allComponents = entity.GetAll<CustomEntityComponent>().ToList();
Assert.Equal(new List<EntityComponent>() { custom, custom2 }, allComponents);
}
[Fact]
public void TestEntityAndPrefabClone()
{
var entity = new Entity("Parent");
var childEntity = new Entity("Child");
entity.AddChild(childEntity);
var custom = entity.GetOrCreate<CustomEntityComponent>();
custom.Link = childEntity;
custom.CustomObject = new Model();
var newEntity = entity.Clone();
CheckEntity(newEntity);
// Check prefab cloning
var prefab = new Prefab();
prefab.Entities.Add(entity);
var newEntities = prefab.Instantiate();
Assert.Single(newEntities);
CheckEntity(newEntities[0]);
return;
void CheckEntity([NotNull] Entity e)
{
Assert.Single(e.Transform.Children);
var newChildEntity = e.Transform.Children[0].Entity;
Assert.Equal("Child", newChildEntity.Name);
Assert.NotNull(e.Get<CustomEntityComponent>());
var newCustom = e.Get<CustomEntityComponent>();
// Make sure that the old component and the new component are different
Assert.NotEqual(custom, newCustom);
// Make sure that the property is referencing the new cloned entity
Assert.Equal(newChildEntity, newCustom.Link);
// Verify that objects references outside the Entity/Component hierarchy are not cloned (shared)
Assert.Equal(custom.CustomObject, newCustom.CustomObject);
}
}
[Fact]
public void TestCloningBehavior()
{
var externalEntity = new Entity();
var sourceEntity = new Entity();
var sourceComponent = new EntityComponentWithPrefab
{
Prefab = new Prefab(),
ExternalEntityRef = externalEntity,
// Not yet supported, see commented out ExternalComponentRef declaration further and PR #2914
//ExternalComponentRef = externalEntity.Transform
};
sourceComponent.Prefab.Entities.Add(sourceEntity);
sourceEntity.Add(sourceComponent);
var clonedComponent = sourceComponent.Prefab.Instantiate()[0].Get<EntityComponentWithPrefab>();
// Validate that cloning did clone the entity
Assert.NotEqual(clonedComponent.Entity, sourceComponent.Entity);
// References to prefabs should not be deep cloned as they are content types
Assert.Equal(clonedComponent.Prefab, sourceComponent.Prefab);
// References to entities outside this one's hierarchy should not clone the entity referenced, it should point to the same reference
Assert.Equal(clonedComponent.ExternalEntityRef, sourceComponent.ExternalEntityRef);
// References to entity component outside this one's hierarchy should not clone the component referenced, it should point to the same reference
// Not yet supported, see commented out ExternalComponentRef declaration further and PR #2914
/*Assert.Equal(clonedComponent.ExternalComponentRef, sourceComponent.ExternalComponentRef);*/
}
private class DelegateEntityComponentNotify : EntityManager
{
private readonly Action<EntityComponentEvent> action;
public DelegateEntityComponentNotify(Action<EntityComponentEvent> action) : base(new ServiceRegistry())
{
if (action == null) throw new ArgumentNullException(nameof(action));
this.action = action;
}
protected override void OnComponentChanged(Entity entity, int index, EntityComponent oldComponent, EntityComponent newComponent)
{
action(new EntityComponentEvent(entity, index, oldComponent, newComponent));
}
}
struct EntityComponentEvent : IEquatable<EntityComponentEvent>
{
public EntityComponentEvent(Entity entity, int index, EntityComponent oldComponent, EntityComponent newComponent)
{
Entity = entity;
this.Index = index;
OldComponent = oldComponent;
NewComponent = newComponent;
}
public readonly Entity Entity;
public readonly int Index;
public readonly EntityComponent OldComponent;
public readonly EntityComponent NewComponent;
public bool Equals(EntityComponentEvent other)
{
return Equals(Entity, other.Entity) && Index == other.Index && Equals(OldComponent, other.OldComponent) && Equals(NewComponent, other.NewComponent);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is EntityComponentEvent && Equals((EntityComponentEvent)obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = (Entity != null ? Entity.GetHashCode() : 0);
hashCode = (hashCode*397) ^ Index;
hashCode = (hashCode*397) ^ (OldComponent != null ? OldComponent.GetHashCode() : 0);
hashCode = (hashCode*397) ^ (NewComponent != null ? NewComponent.GetHashCode() : 0);
return hashCode;
}
}
public static bool operator ==(EntityComponentEvent left, EntityComponentEvent right)
{
return left.Equals(right);
}
public static bool operator !=(EntityComponentEvent left, EntityComponentEvent right)
{
return !left.Equals(right);
}
}
}
[DataContract()]
[DefaultEntityComponentProcessor(typeof(CustomEntityComponentProcessor))]
[AllowMultipleComponents]
public sealed class CustomEntityComponent : CustomEntityComponentBase
{
public Entity Link { get; set; }
public object CustomObject { get; set; }
}
[AllowMultipleComponents]
public class MultipleEntityComponentBase : CustomEntityComponentBase
{
}
public sealed class DerivedEntityComponent : MultipleEntityComponentBase
{
}
[DataContract()]
public abstract class CustomEntityComponentBase : EntityComponent
{
public Action<CustomEntityComponentEventArgs> Changed;
}
[DataContract]
public class EntityComponentWithPrefab : EntityComponent
{
public required Prefab Prefab { get; set; }
public required Entity ExternalEntityRef { get; set; }
/* TODO:
* References to entity component outside of a prefab's hierarchy should not clone the component referenced, it should point to the same reference.
* More work is required on that front, particularly with EntityComponent which does not have a specific serializer,
* we would need one that derive from DataContentSerializerWithReuse to properly filter external references.
* The serializer for TransformComponent derives from a simple DataSerializer for example.
* See also PR #2914
*/
/*public required TransformComponent ExternalComponentRef { get; set; }*/
}
}