A game about forced loneliness, made by TACStudios
1using System.Collections;
2using System.IO;
3using NUnit.Framework;
4using UnityEditor;
5using UnityEngine.EventSystems;
6using UnityEngine.TestTools;
7using UnityEngine.UI;
8using UnityEngine;
9
10public class ButtonTests : IPrebuildSetup
11{
12 GameObject m_PrefabRoot;
13 const string kPrefabPath = "Assets/Resources/ButtonPrefab.prefab";
14
15 public void Setup()
16 {
17#if UNITY_EDITOR
18 var rootGO = new GameObject("rootGo");
19 var canvasGO = new GameObject("Canvas", typeof(Canvas));
20 canvasGO.transform.SetParent(rootGO.transform);
21 var canvas = canvasGO.GetComponent<Canvas>();
22 canvas.referencePixelsPerUnit = 100;
23 GameObject eventSystemGO = new GameObject("EventSystem", typeof(EventSystem));
24 eventSystemGO.transform.SetParent(rootGO.transform);
25 GameObject TestButtonGO = new GameObject("TestButton", typeof(RectTransform), typeof(TestButton));
26 TestButtonGO.transform.SetParent(canvasGO.transform);
27
28 if (!Directory.Exists("Assets/Resources/"))
29 Directory.CreateDirectory("Assets/Resources/");
30
31 PrefabUtility.SaveAsPrefabAsset(rootGO, kPrefabPath);
32 GameObject.DestroyImmediate(rootGO);
33#endif
34 }
35
36 [SetUp]
37 public void TestSetup()
38 {
39 m_PrefabRoot = Object.Instantiate(Resources.Load("ButtonPrefab")) as GameObject;
40 }
41
42 [TearDown]
43 public void TearDown()
44 {
45 GameObject.DestroyImmediate(m_PrefabRoot);
46 }
47
48 [OneTimeTearDown]
49 public void OneTimeTearDown()
50 {
51#if UNITY_EDITOR
52 AssetDatabase.DeleteAsset(kPrefabPath);
53#endif
54 }
55
56 [Test]
57 public void PressShouldCallClickHandler()
58 {
59 Button button = m_PrefabRoot.GetComponentInChildren<Button>();
60 bool called = false;
61 button.onClick.AddListener(() => { called = true; });
62 button.OnPointerClick(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = PointerEventData.InputButton.Left });
63 Assert.True(called);
64 }
65
66 [Test]
67 public void PressInactiveShouldNotCallClickHandler()
68 {
69 Button button = m_PrefabRoot.GetComponentInChildren<Button>();
70 bool called = false;
71 button.enabled = false;
72 button.onClick.AddListener(() => { called = true; });
73 button.OnPointerClick(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = PointerEventData.InputButton.Left });
74 Assert.False(called);
75 }
76
77 [Test]
78 public void PressNotInteractableShouldNotCallClickHandler()
79 {
80 Button button = m_PrefabRoot.GetComponentInChildren<Button>();
81 bool called = false;
82 button.interactable = false;
83 button.onClick.AddListener(() => { called = true; });
84 button.OnPointerClick(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = PointerEventData.InputButton.Left });
85 Assert.False(called);
86 }
87
88 [Test]
89 public void SelectShouldHoldThePreviousStateAfterDisablingAndEnabling()
90 {
91 TestButton button = m_PrefabRoot.GetComponentInChildren<TestButton>();
92 button.onClick.AddListener(() => {
93 button.Select();
94 button.enabled = false;
95 });
96 button.OnPointerClick(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = PointerEventData.InputButton.Left });
97 Assert.False(button.enabled, "Expected button to not be enabled");
98 button.enabled = true;
99 Assert.True(button.isStateSelected, "Expected selected state to be true");
100 }
101
102 [Test]
103 public void SubmitShouldCallClickHandler()
104 {
105 Button button = m_PrefabRoot.GetComponentInChildren<Button>();
106 bool called = false;
107 button.onClick.AddListener(() => { called = true; });
108 button.OnSubmit(null);
109 Assert.True(called);
110 }
111
112 [Test]
113 public void SubmitInactiveShouldNotCallClickHandler()
114 {
115 Button button = m_PrefabRoot.GetComponentInChildren<Button>();
116 bool called = false;
117 button.enabled = false;
118 button.onClick.AddListener(() => { called = true; });
119 button.OnSubmit(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = PointerEventData.InputButton.Left });
120 Assert.False(called);
121 }
122
123 [Test]
124 public void SubmitNotInteractableShouldNotCallClickHandler()
125 {
126 Button button = m_PrefabRoot.GetComponentInChildren<Button>();
127 bool called = false;
128 button.interactable = false;
129 button.onClick.AddListener(() => { called = true; });
130 button.OnSubmit(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = PointerEventData.InputButton.Left });
131 Assert.False(called);
132 }
133
134 [UnityTest]
135 public IEnumerator SubmitShouldTransitionToPressedStateAndBackToNormal()
136 {
137 TestButton button = m_PrefabRoot.GetComponentInChildren<TestButton>();
138 Assert.True(button.IsTransitionToNormal(0));
139
140 button.OnSubmit(null);
141 Assert.True(button.isStateNormal);
142 Assert.True(button.IsTransitionToPressed(1));
143 yield return new WaitWhile(() => button.StateTransitionCount == 2);
144
145 // 3rd transition back to normal should have started
146 Assert.True(button.IsTransitionToNormal(2));
147 yield return null;
148
149 Assert.True(button.isStateNormal);
150 }
151}