File size: 5,406 Bytes
18a519f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using System.Collections;
using System.IO;
using NUnit.Framework;
using UnityEditor;
using UnityEngine.EventSystems;
using UnityEngine.TestTools;
using UnityEngine.UI;
using UnityEngine;

public class ButtonTests : IPrebuildSetup
{
    GameObject m_PrefabRoot;
    const string kPrefabPath = "Assets/Resources/ButtonPrefab.prefab";

    public void Setup()
    {
#if UNITY_EDITOR
        var rootGO = new GameObject("rootGo");
        var canvasGO = new GameObject("Canvas", typeof(Canvas));
        canvasGO.transform.SetParent(rootGO.transform);
        var canvas = canvasGO.GetComponent<Canvas>();
        canvas.referencePixelsPerUnit = 100;
        GameObject eventSystemGO = new GameObject("EventSystem", typeof(EventSystem));
        eventSystemGO.transform.SetParent(rootGO.transform);
        GameObject TestButtonGO = new GameObject("TestButton", typeof(RectTransform), typeof(TestButton));
        TestButtonGO.transform.SetParent(canvasGO.transform);

        if (!Directory.Exists("Assets/Resources/"))
            Directory.CreateDirectory("Assets/Resources/");

        PrefabUtility.SaveAsPrefabAsset(rootGO, kPrefabPath);
        GameObject.DestroyImmediate(rootGO);
#endif
    }

    [SetUp]
    public void TestSetup()
    {
        m_PrefabRoot = Object.Instantiate(Resources.Load("ButtonPrefab")) as GameObject;
    }

    [TearDown]
    public void TearDown()
    {
        GameObject.DestroyImmediate(m_PrefabRoot);
    }

    [OneTimeTearDown]
    public void OneTimeTearDown()
    {
#if UNITY_EDITOR
        AssetDatabase.DeleteAsset(kPrefabPath);
#endif
    }

    [Test]
    public void PressShouldCallClickHandler()
    {
        Button button = m_PrefabRoot.GetComponentInChildren<Button>();
        bool called = false;
        button.onClick.AddListener(() => { called = true; });
        button.OnPointerClick(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = PointerEventData.InputButton.Left });
        Assert.True(called);
    }

    [Test]
    public void PressInactiveShouldNotCallClickHandler()
    {
        Button button = m_PrefabRoot.GetComponentInChildren<Button>();
        bool called = false;
        button.enabled = false;
        button.onClick.AddListener(() => { called = true; });
        button.OnPointerClick(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = PointerEventData.InputButton.Left });
        Assert.False(called);
    }

    [Test]
    public void PressNotInteractableShouldNotCallClickHandler()
    {
        Button button = m_PrefabRoot.GetComponentInChildren<Button>();
        bool called = false;
        button.interactable = false;
        button.onClick.AddListener(() => { called = true; });
        button.OnPointerClick(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = PointerEventData.InputButton.Left });
        Assert.False(called);
    }

    [Test]
    public void SelectShouldHoldThePreviousStateAfterDisablingAndEnabling()
    {
        TestButton button = m_PrefabRoot.GetComponentInChildren<TestButton>();
        button.onClick.AddListener(() => {
            button.Select();
            button.enabled = false;
        });
        button.OnPointerClick(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = PointerEventData.InputButton.Left });
        Assert.False(button.enabled, "Expected button to not be enabled");
        button.enabled = true;
        Assert.True(button.isStateSelected, "Expected selected state to be true");
    }

    [Test]
    public void SubmitShouldCallClickHandler()
    {
        Button button = m_PrefabRoot.GetComponentInChildren<Button>();
        bool called = false;
        button.onClick.AddListener(() => { called = true; });
        button.OnSubmit(null);
        Assert.True(called);
    }

    [Test]
    public void SubmitInactiveShouldNotCallClickHandler()
    {
        Button button = m_PrefabRoot.GetComponentInChildren<Button>();
        bool called = false;
        button.enabled = false;
        button.onClick.AddListener(() => { called = true; });
        button.OnSubmit(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = PointerEventData.InputButton.Left });
        Assert.False(called);
    }

    [Test]
    public void SubmitNotInteractableShouldNotCallClickHandler()
    {
        Button button = m_PrefabRoot.GetComponentInChildren<Button>();
        bool called = false;
        button.interactable = false;
        button.onClick.AddListener(() => { called = true; });
        button.OnSubmit(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = PointerEventData.InputButton.Left });
        Assert.False(called);
    }

    [UnityTest]
    public IEnumerator SubmitShouldTransitionToPressedStateAndBackToNormal()
    {
        TestButton button = m_PrefabRoot.GetComponentInChildren<TestButton>();
        Assert.True(button.IsTransitionToNormal(0));

        button.OnSubmit(null);
        Assert.True(button.isStateNormal);
        Assert.True(button.IsTransitionToPressed(1));
        yield return new WaitWhile(() => button.StateTransitionCount == 2);

        // 3rd transition back to normal should have started
        Assert.True(button.IsTransitionToNormal(2));
        yield return null;

        Assert.True(button.isStateNormal);
    }
}