Skip to content

Commit e20740e

Browse files
PaulDemeulenaereGitHub Enterprise
authored andcommitted
Add test to cover behavior of https://fogbugz.unity3d.com/f/cases/1300115/ (#192)
1 parent 58b89ee commit e20740e

File tree

1 file changed

+94
-19
lines changed

1 file changed

+94
-19
lines changed

TestProjects/VisualEffectGraph_HDRP/Assets/AllTests/Editor/Tests/VFXSpawnerTest.cs

Lines changed: 94 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,86 @@ public IEnumerator Create_Asset_And_Component_Spawner_Check_Initial_Event()
231231
yield return new ExitPlayMode();
232232
}
233233

234-
static List<int> s_receivedEvent;
235-
static void OnEventReceived(VFXOutputEventArgs evt)
234+
235+
static List<Vector3> s_RecordedPositions = new List<Vector3>();
236+
static void OnEventReceived_SavePosition(VFXOutputEventArgs evt)
237+
{
238+
s_RecordedPositions.Add(evt.eventAttribute.GetVector3("position"));
239+
}
240+
241+
static bool[] s_Verify_Reseed_OnPlay_Behavior_options = new bool[] { false, true };
242+
243+
[UnityTest]
244+
public IEnumerator Verify_Reseed_OnPlay_Behavior([ValueSource("s_Verify_Reseed_OnPlay_Behavior_options")] bool reseed, [ValueSource("s_Verify_Reseed_OnPlay_Behavior_options")] bool useSendEvent)
245+
{
246+
yield return new EnterPlayMode();
247+
248+
var spawnCountValue = 1.0f;
249+
VisualEffect vfxComponent;
250+
GameObject cameraObj, gameObj;
251+
VFXGraph graph;
252+
CreateAssetAndComponent(spawnCountValue, "OnPlay", out graph, out vfxComponent, out gameObj, out cameraObj);
253+
254+
var outputEvent = ScriptableObject.CreateInstance<VFXOutputEvent>();
255+
var eventName = "qsdf";
256+
outputEvent.SetSettingValue("eventName", eventName);
257+
var basicSpawner = graph.children.OfType<VFXBasicSpawner>().FirstOrDefault();
258+
graph.AddChild(outputEvent);
259+
outputEvent.LinkFrom(basicSpawner);
260+
261+
//Add constant random to inspect the current seed
262+
var setAttributePosition = ScriptableObject.CreateInstance<VFXSpawnerSetAttribute>();
263+
setAttributePosition.SetSettingValue("attribute", "position");
264+
basicSpawner.AddChild(setAttributePosition);
265+
266+
for (int i = 0; i < 3; ++i)
267+
{
268+
var random = ScriptableObject.CreateInstance<Operator.Random>();
269+
random.SetSettingValue("seed", VFXSeedMode.PerComponent);
270+
random.SetSettingValue("constant", true);
271+
graph.AddChild(outputEvent);
272+
random.outputSlots.First().Link(setAttributePosition.inputSlots.First()[i]);
273+
}
274+
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(graph));
275+
276+
s_RecordedPositions = new List<Vector3>();
277+
vfxComponent.outputEventReceived += OnEventReceived_SavePosition;
278+
vfxComponent.resetSeedOnPlay = reseed;
279+
280+
int maxFrame = 256;
281+
while (s_RecordedPositions.Count < 3 && --maxFrame > 0)
282+
yield return null;
283+
284+
Assert.IsTrue(maxFrame > 0);
285+
Assert.AreEqual(1, s_RecordedPositions.Distinct().Count());
286+
287+
for (int i = 0; i < 3; ++i)
288+
{
289+
//The seed should change depending on resetSeedOnPlay settings
290+
if (useSendEvent)
291+
vfxComponent.SendEvent(VisualEffectAsset.PlayEventID);
292+
else
293+
vfxComponent.Play();
294+
295+
maxFrame = 256;
296+
while (s_RecordedPositions.Count < 3 + i * 3 && --maxFrame > 0)
297+
yield return null;
298+
Assert.IsTrue(maxFrame > 0);
299+
}
300+
301+
var distinctCount = s_RecordedPositions.Distinct().Count();
302+
if (reseed)
303+
Assert.AreNotEqual(1, distinctCount);
304+
else
305+
Assert.AreEqual(1, distinctCount);
306+
307+
yield return new ExitPlayMode();
308+
}
309+
310+
static List<int> s_ReceivedEventNamedId;
311+
static void OnEventReceived_RegisterNameID(VFXOutputEventArgs evt)
236312
{
237-
s_receivedEvent.Add(evt.nameId);
313+
s_ReceivedEventNamedId.Add(evt.nameId);
238314
}
239315

240316
[UnityTest]
@@ -257,8 +333,8 @@ public IEnumerator Create_Asset_And_Component_Spawner_And_Output_Event()
257333
outputEvent.LinkFrom(basicSpawner);
258334
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(graph));
259335

260-
s_receivedEvent = new List<int>();
261-
vfxComponent.outputEventReceived += OnEventReceived;
336+
s_ReceivedEventNamedId = new List<int>();
337+
vfxComponent.outputEventReceived += OnEventReceived_RegisterNameID;
262338

263339
int maxFrame = 64;
264340
while (vfxComponent.culled && --maxFrame > 0)
@@ -274,19 +350,19 @@ public IEnumerator Create_Asset_And_Component_Spawner_And_Output_Event()
274350
Assert.AreEqual(outputEventName, eventName);
275351

276352
//Checking invalid event (waiting for the first event)
277-
Assert.AreEqual(0u, s_receivedEvent.Count);
353+
Assert.AreEqual(0u, s_ReceivedEventNamedId.Count);
278354

279355
//Checking on valid event while there is an event
280-
maxFrame = 64; s_receivedEvent.Clear();
281-
while (s_receivedEvent.Count == 0u && --maxFrame > 0)
356+
maxFrame = 64; s_ReceivedEventNamedId.Clear();
357+
while (s_ReceivedEventNamedId.Count == 0u && --maxFrame > 0)
282358
{
283359
yield return null;
284360
}
285361
Assert.IsTrue(maxFrame > 0);
286-
Assert.IsTrue(s_receivedEvent.Count > 0);
287-
Assert.AreEqual(Shader.PropertyToID(eventName), s_receivedEvent.FirstOrDefault());
362+
Assert.IsTrue(s_ReceivedEventNamedId.Count > 0);
363+
Assert.AreEqual(Shader.PropertyToID(eventName), s_ReceivedEventNamedId.FirstOrDefault());
288364

289-
s_receivedEvent.Clear();
365+
s_ReceivedEventNamedId.Clear();
290366

291367
yield return new ExitPlayMode();
292368
}
@@ -309,8 +385,8 @@ public IEnumerator Create_Asset_And_Component_Spawner_And_Output_Event_Expected_
309385
outputEvent.LinkFrom(basicSpawner);
310386
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(graph));
311387

312-
s_receivedEvent = new List<int>();
313-
vfxComponent.outputEventReceived += OnEventReceived;
388+
s_ReceivedEventNamedId = new List<int>();
389+
vfxComponent.outputEventReceived += OnEventReceived_RegisterNameID;
314390

315391
int maxFrame = 512;
316392
while (vfxComponent.culled && --maxFrame > 0)
@@ -323,24 +399,23 @@ public IEnumerator Create_Asset_And_Component_Spawner_And_Output_Event_Expected_
323399
float deltaTime = 0.1f;
324400
uint count = 32;
325401
vfxComponent.Simulate(deltaTime, count);
326-
Assert.AreEqual(0u, s_receivedEvent.Count); //The simulate is asynchronous
402+
Assert.AreEqual(0u, s_ReceivedEventNamedId.Count); //The simulate is asynchronous
327403

328404
float simulateTime = deltaTime * count;
329405
uint expectedEventCount = (uint)Mathf.Floor(simulateTime / spawnCountValue);
330406

331-
maxFrame = 64; s_receivedEvent.Clear();
407+
maxFrame = 64; s_ReceivedEventNamedId.Clear();
332408
cameraObj.SetActive(false);
333-
while (s_receivedEvent.Count == 0u && --maxFrame > 0)
409+
while (s_ReceivedEventNamedId.Count == 0u && --maxFrame > 0)
334410
{
335411
yield return null;
336412
}
337-
Assert.AreEqual(expectedEventCount, (uint)s_receivedEvent.Count);
413+
Assert.AreEqual(expectedEventCount, (uint)s_ReceivedEventNamedId.Count);
338414
yield return null;
339415

340416
yield return new ExitPlayMode();
341417
}
342-
343-
418+
344419
[UnityTest]
345420
public IEnumerator Create_Asset_And_Component_Spawner()
346421
{

0 commit comments

Comments
 (0)