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.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 } #region Press [Test] public void PressShouldCallClickHandler() { Button button = m_PrefabRoot.GetComponentInChildren