A game about forced loneliness, made by TACStudios
at master 83 lines 2.5 kB view raw
1using NUnit.Framework; 2using UnityEditor.Events; 3using UnityEngine; 4using UnityEngine.Events; 5 6public class UnityEventInvoke 7{ 8 class SimpleCounter : MonoBehaviour 9 { 10 public int m_Count = 0; 11 12 public void Add() 13 { 14 ++m_Count; 15 } 16 17 public void NoOp(int i) 18 { 19 } 20 } 21 22 GameObject m_CounterObject; 23 SimpleCounter Counter { get; set; } 24 25 [SetUp] 26 public void TestSetup() 27 { 28 m_CounterObject = new GameObject("Counter"); 29 Counter = m_CounterObject.AddComponent<SimpleCounter>(); 30 } 31 32 [TearDown] 33 public void TearDown() 34 { 35 GameObject.DestroyImmediate(m_CounterObject); 36 } 37 38 [Test] 39 [Description("Using a CachedInvokableCall in a UnityEvent should not go re-trigger all the calls stored in the UnityEvent. Case-950588")] 40 public void UnityEvent_InvokeCallsListenerOnce() 41 { 42 var _event = new UnityEvent(); 43 UnityEventTools.AddPersistentListener(_event, new UnityAction(Counter.Add)); 44 _event.SetPersistentListenerState(0, UnityEventCallState.EditorAndRuntime); 45 46 _event.Invoke(); 47 48 Assert.AreEqual(1, Counter.m_Count); 49 50 for (int i = 1; i < 5; ++i) 51 { 52 UnityEventTools.AddIntPersistentListener(_event, new UnityAction<int>(Counter.NoOp), i); 53 _event.SetPersistentListenerState(i, UnityEventCallState.EditorAndRuntime); 54 } 55 56 _event.Invoke(); 57 58 Assert.AreEqual(2, Counter.m_Count); 59 } 60 61 [Test] 62 [Description("Using a CachedInvokableCall in a UnityEvent should not go re-trigger all the calls stored in the UnityEvent. Case-950588")] 63 public void UnityEvent_EditMode_InvokeDoesNotCallRuntimeListener() 64 { 65 var _event = new UnityEvent(); 66 UnityEventTools.AddPersistentListener(_event, new UnityAction(Counter.Add)); 67 Assert.AreEqual(UnityEventCallState.RuntimeOnly, _event.GetPersistentListenerState(0)); 68 Assert.False(Application.isPlaying); 69 70 _event.Invoke(); 71 72 Assert.AreEqual(0, Counter.m_Count, "Expected Event to not be called when not in play mode and event is marked as Runtime only"); 73 74 for (int i = 1; i < 5; ++i) 75 { 76 UnityEventTools.AddIntPersistentListener(_event, new UnityAction<int>(Counter.NoOp), i); 77 } 78 79 _event.Invoke(); 80 81 Assert.AreEqual(0, Counter.m_Count, "Expected Event to not be called when not in play mode and event is marked as Runtime only"); 82 } 83}