first commit
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using NUnit.Framework;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.TestTools;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.UI.Tests;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace ToggleTest
|
||||
{
|
||||
class TestableToggleGroup : ToggleGroup
|
||||
{
|
||||
public bool ToggleListContains(Toggle toggle)
|
||||
{
|
||||
return m_Toggles.Contains(toggle);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e0e4f43a3816add4c86c9671d7961f2f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,160 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using NUnit.Framework;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.TestTools;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.UI.Tests;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace ToggleTest
|
||||
{
|
||||
class ToggleGroupTests : IPrebuildSetup
|
||||
{
|
||||
const string kPrefabToggleGroupPath = "Assets/Resources/TestToggleGroup.prefab";
|
||||
|
||||
protected GameObject m_PrefabRoot;
|
||||
protected List<Toggle> m_toggle = new List<Toggle>();
|
||||
protected static int nbToggleInGroup = 2;
|
||||
private TestableToggleGroup m_toggleGroup;
|
||||
|
||||
public void Setup()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
var rootGO = new GameObject("rootGo");
|
||||
GameObject canvasGO = new GameObject("Canvas", typeof(RectTransform), typeof(Canvas));
|
||||
canvasGO.transform.SetParent(rootGO.transform);
|
||||
|
||||
var toggleGroupGO = new GameObject("ToggleGroup", typeof(RectTransform), typeof(TestableToggleGroup));
|
||||
toggleGroupGO.transform.SetParent(canvasGO.transform);
|
||||
toggleGroupGO.AddComponent(typeof(TestableToggleGroup));
|
||||
|
||||
var toggle0GO = new GameObject("TestToggle0", typeof(RectTransform), typeof(Toggle), typeof(Image));
|
||||
toggle0GO.transform.SetParent(toggleGroupGO.transform);
|
||||
|
||||
var toggle1GO = new GameObject("TestToggle1", typeof(RectTransform), typeof(Toggle), typeof(Image));
|
||||
toggle1GO.transform.SetParent(toggleGroupGO.transform);
|
||||
|
||||
var toggle = toggle0GO.GetComponent<Toggle>();
|
||||
toggle.graphic = toggle0GO.GetComponent<Image>();
|
||||
toggle.graphic.canvasRenderer.SetColor(Color.white);
|
||||
|
||||
var toggle1 = toggle1GO.GetComponent<Toggle>();
|
||||
toggle1.graphic = toggle1GO.GetComponent<Image>();
|
||||
toggle1.graphic.canvasRenderer.SetColor(Color.white);
|
||||
|
||||
|
||||
if (!Directory.Exists("Assets/Resources/"))
|
||||
Directory.CreateDirectory("Assets/Resources/");
|
||||
|
||||
PrefabUtility.SaveAsPrefabAsset(rootGO, kPrefabToggleGroupPath);
|
||||
|
||||
GameObject.DestroyImmediate(rootGO);
|
||||
#endif
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void TestSetup()
|
||||
{
|
||||
m_PrefabRoot = Object.Instantiate(Resources.Load("TestToggleGroup")) as GameObject;
|
||||
|
||||
m_toggleGroup = m_PrefabRoot.GetComponentInChildren<TestableToggleGroup>();
|
||||
m_toggle.AddRange(m_PrefabRoot.GetComponentsInChildren<Toggle>());
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
m_toggle.Clear();
|
||||
m_toggleGroup = null;
|
||||
Object.DestroyImmediate(m_PrefabRoot);
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void OneTimeTearDown()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
AssetDatabase.DeleteAsset(kPrefabToggleGroupPath);
|
||||
#endif
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TogglingOneShouldDisableOthersInGroup()
|
||||
{
|
||||
m_toggle[0].group = m_toggleGroup;
|
||||
m_toggle[1].group = m_toggleGroup;
|
||||
m_toggle[0].isOn = true;
|
||||
m_toggle[1].isOn = true;
|
||||
Assert.IsFalse(m_toggle[0].isOn);
|
||||
Assert.IsTrue(m_toggle[1].isOn);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DisallowSwitchOffShouldKeepToggleOnWhenClicking()
|
||||
{
|
||||
m_toggle[0].group = m_toggleGroup;
|
||||
m_toggle[1].group = m_toggleGroup;
|
||||
m_toggle[0].isOn = true;
|
||||
Assert.IsTrue(m_toggle[0].isOn);
|
||||
m_toggle[0].OnPointerClick(new PointerEventData(EventSystem.current) { button = PointerEventData.InputButton.Left });
|
||||
Assert.IsTrue(m_toggle[0].isOn);
|
||||
Assert.IsFalse(m_toggle[1].isOn);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DisallowSwitchOffShouldDisableToggleWhenClicking()
|
||||
{
|
||||
m_toggleGroup.allowSwitchOff = true;
|
||||
m_toggle[0].group = m_toggleGroup;
|
||||
m_toggle[1].group = m_toggleGroup;
|
||||
m_toggle[0].isOn = true;
|
||||
Assert.IsTrue(m_toggle[0].isOn);
|
||||
m_toggle[0].OnPointerClick(new PointerEventData(EventSystem.current) { button = PointerEventData.InputButton.Left });
|
||||
Assert.IsFalse(m_toggle[0].isOn);
|
||||
Assert.IsFalse(m_toggle[1].isOn);
|
||||
}
|
||||
|
||||
[TestCase(0)]
|
||||
[TestCase(1)]
|
||||
public void ReEnablingGameObjectWithToggleGroupRetainsPreviouslySelectedToggle(int toggleIndex)
|
||||
{
|
||||
m_toggle[0].group = m_toggleGroup;
|
||||
m_toggle[1].group = m_toggleGroup;
|
||||
|
||||
m_toggle[toggleIndex].isOn = true;
|
||||
m_PrefabRoot.SetActive(false);
|
||||
m_PrefabRoot.SetActive(true);
|
||||
Assert.IsTrue(m_toggle[toggleIndex].isOn);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ChangingToggleGroupUnregistersFromOriginalGroup()
|
||||
{
|
||||
m_toggle[0].group = m_toggleGroup;
|
||||
Assert.IsTrue(m_toggleGroup.ToggleListContains(m_toggle[0]));
|
||||
m_toggle[0].group = null;
|
||||
Assert.IsFalse(m_toggleGroup.ToggleListContains(m_toggle[0]));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DisabledToggleGroupDoesntControlChildren()
|
||||
{
|
||||
m_toggleGroup.enabled = false;
|
||||
m_toggle[0].group = m_toggleGroup;
|
||||
m_toggle[1].group = m_toggleGroup;
|
||||
m_toggle[0].isOn = true;
|
||||
m_toggle[1].isOn = true;
|
||||
Assert.IsTrue(m_toggle[0].isOn);
|
||||
Assert.IsTrue(m_toggle[1].isOn);
|
||||
|
||||
m_toggleGroup.enabled = true;
|
||||
IEnumerable<Toggle> activeToggles = m_toggleGroup.ActiveToggles();
|
||||
Assert.IsTrue(activeToggles.Count() == 1);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 50081206855385544a5ddd0e3b6ba9be
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using NUnit.Framework;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.TestTools;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.UI.Tests;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace ToggleTest
|
||||
{
|
||||
class ToggleTests : IPrebuildSetup
|
||||
{
|
||||
const string kPrefabTogglePath = "Assets/Resources/TestToggle.prefab";
|
||||
|
||||
protected GameObject m_PrefabRoot;
|
||||
protected List<Toggle> m_toggle = new List<Toggle>();
|
||||
protected static int nbToggleInGroup = 2;
|
||||
|
||||
public void Setup()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
var rootGO = new GameObject("rootGo");
|
||||
|
||||
GameObject canvasGO = new GameObject("Canvas", typeof(RectTransform), typeof(Canvas));
|
||||
canvasGO.transform.SetParent(rootGO.transform);
|
||||
|
||||
var canvas = canvasGO.GetComponent<Canvas>();
|
||||
canvas.referencePixelsPerUnit = 100;
|
||||
|
||||
var toggleGO = new GameObject("TestToggle", typeof(RectTransform), typeof(Toggle), typeof(Image));
|
||||
toggleGO.transform.SetParent(canvasGO.transform);
|
||||
|
||||
var toggle = toggleGO.GetComponent<Toggle>();
|
||||
toggle.enabled = true;
|
||||
toggle.graphic = toggleGO.GetComponent<Image>();
|
||||
toggle.graphic.canvasRenderer.SetColor(Color.white);
|
||||
|
||||
if (!Directory.Exists("Assets/Resources/"))
|
||||
Directory.CreateDirectory("Assets/Resources/");
|
||||
|
||||
PrefabUtility.SaveAsPrefabAsset(rootGO, kPrefabTogglePath);
|
||||
|
||||
GameObject.DestroyImmediate(rootGO);
|
||||
#endif
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public virtual void TestSetup()
|
||||
{
|
||||
m_PrefabRoot = Object.Instantiate(Resources.Load("TestToggle")) as GameObject;
|
||||
m_toggle.Add(m_PrefabRoot.GetComponentInChildren<Toggle>());
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public virtual void TearDown()
|
||||
{
|
||||
m_toggle.Clear();
|
||||
Object.DestroyImmediate(m_PrefabRoot);
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void OneTimeTearDown()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
AssetDatabase.DeleteAsset(kPrefabTogglePath);
|
||||
#endif
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SetIsOnWithoutNotifyWillNotNotify()
|
||||
{
|
||||
m_toggle[0].isOn = false;
|
||||
bool calledOnValueChanged = false;
|
||||
m_toggle[0].onValueChanged.AddListener(b => { calledOnValueChanged = true; });
|
||||
m_toggle[0].SetIsOnWithoutNotify(true);
|
||||
Assert.IsTrue(m_toggle[0].isOn);
|
||||
Assert.IsFalse(calledOnValueChanged);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NonInteractableCantBeToggled()
|
||||
{
|
||||
m_toggle[0].isOn = true;
|
||||
Assert.IsTrue(m_toggle[0].isOn);
|
||||
m_toggle[0].interactable = false;
|
||||
m_toggle[0].OnSubmit(null);
|
||||
Assert.IsTrue(m_toggle[0].isOn);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InactiveCantBeToggled()
|
||||
{
|
||||
m_toggle[0].isOn = true;
|
||||
Assert.IsTrue(m_toggle[0].isOn);
|
||||
m_toggle[0].enabled = false;
|
||||
m_toggle[0].OnSubmit(null);
|
||||
Assert.IsTrue(m_toggle[0].isOn);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 14427eecd0bccea468addc3492aaef57
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user