File size: 10,905 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | using System;
using System.Linq;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.TestTools;
using System.Collections;
using System.IO;
using UnityEditor;
using UnityEngine.UI;
using System.Reflection;
namespace InputfieldTests
{
public class TouchInputFieldTests : BaseInputFieldTests, IPrebuildSetup
{
protected const string kPrefabPath = "Assets/Resources/TouchInputFieldPrefab.prefab";
public void Setup()
{
#if UNITY_EDITOR
CreateInputFieldAsset(kPrefabPath);
#endif
}
[SetUp]
public void TestSetup()
{
m_PrefabRoot = UnityEngine.Object.Instantiate(Resources.Load("TouchInputFieldPrefab")) as GameObject;
FieldInfo inputModule = typeof(EventSystem).GetField("m_CurrentInputModule", BindingFlags.NonPublic | BindingFlags.Instance);
inputModule.SetValue(m_PrefabRoot.GetComponentInChildren<EventSystem>(), m_PrefabRoot.GetComponentInChildren<FakeInputModule>());
}
[TearDown]
public void TearDown()
{
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
TouchScreenKeyboard.hideInput = false;
FontUpdateTracker.UntrackText(m_PrefabRoot.GetComponentInChildren<Text>());
GameObject.DestroyImmediate(m_PrefabRoot);
}
[OneTimeTearDown]
public void OnetimeTearDown()
{
#if UNITY_EDITOR
AssetDatabase.DeleteAsset(kPrefabPath);
#endif
}
protected const string kDefaultInputStr = "foobar";
const string kEmailSpecialCharacters = "!#$%&'*+-/=?^_`{|}~";
public struct CharValidationTestData
{
public string input, output;
public InputField.CharacterValidation validation;
public CharValidationTestData(string input, string output, InputField.CharacterValidation validation)
{
this.input = input;
this.output = output;
this.validation = validation;
}
public override string ToString()
{
// these won't properly show up if test runners UI if we don't replace it
string input = this.input.Replace(kEmailSpecialCharacters, "specialchars");
string output = this.output.Replace(kEmailSpecialCharacters, "specialchars");
return string.Format("input={0}, output={1}, validation={2}", input, output, validation);
}
}
[Test]
[TestCase("*Azé09", "*Azé09", InputField.CharacterValidation.None)]
[TestCase("*Azé09?.", "Az09", InputField.CharacterValidation.Alphanumeric)]
[TestCase("Abc10x", "10", InputField.CharacterValidation.Integer)]
[TestCase("-10", "-10", InputField.CharacterValidation.Integer)]
[TestCase("10.0", "100", InputField.CharacterValidation.Integer)]
[TestCase("10.0", "10.0", InputField.CharacterValidation.Decimal)]
[TestCase(" -10.0x", "-10.0", InputField.CharacterValidation.Decimal)]
[TestCase("10,0", "10,0", InputField.CharacterValidation.Decimal)]
[TestCase(" -10,0x", "-10,0", InputField.CharacterValidation.Decimal)]
[TestCase("A10,0 ", "10,0", InputField.CharacterValidation.Decimal)]
[TestCase("A'a aaa aaa", "A'a Aaa Aaa", InputField.CharacterValidation.Name)]
[TestCase("Unity-Editor", "Unity-Editor", InputField.CharacterValidation.Name)]
[TestCase("Unity--Editor", "Unity-Editor", InputField.CharacterValidation.Name)]
[TestCase("-UnityEditor", "Unityeditor", InputField.CharacterValidation.Name)]
[TestCase(" _JOHN* (Doe)", "John Doe", InputField.CharacterValidation.Name)]
[TestCase("johndoe@unity3d.com", "johndoe@unity3d.com", InputField.CharacterValidation.EmailAddress)]
[TestCase(">john doe\\@unity3d.com", "johndoe@unity3d.com", InputField.CharacterValidation.EmailAddress)]
[TestCase(kEmailSpecialCharacters + "@unity3d.com", kEmailSpecialCharacters + "@unity3d.com", InputField.CharacterValidation.EmailAddress)]
public void HonorsCharacterValidationSettingsAssignment(string input, string output, InputField.CharacterValidation validation)
{
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
inputField.characterValidation = validation;
inputField.text = input;
Assert.AreEqual(output, inputField.text, string.Format("Failed character validation: input ={0}, output ={1}, validation ={2}",
input.Replace(kEmailSpecialCharacters, "specialchars"),
output.Replace(kEmailSpecialCharacters, "specialchars"),
validation));
}
[UnityTest]
[TestCase("*Azé09", "*Azé09", InputField.CharacterValidation.None, ExpectedResult = null)]
[TestCase("*Azé09?.", "Az09", InputField.CharacterValidation.Alphanumeric, ExpectedResult = null)]
[TestCase("Abc10x", "10", InputField.CharacterValidation.Integer, ExpectedResult = null)]
[TestCase("-10", "-10", InputField.CharacterValidation.Integer, ExpectedResult = null)]
[TestCase("10.0", "100", InputField.CharacterValidation.Integer, ExpectedResult = null)]
[TestCase("10.0", "10.0", InputField.CharacterValidation.Decimal, ExpectedResult = null)]
[TestCase(" -10.0x", "-10.0", InputField.CharacterValidation.Decimal, ExpectedResult = null)]
[TestCase("10,0", "10,0", InputField.CharacterValidation.Decimal, ExpectedResult = null)]
[TestCase(" -10,0x", "-10,0", InputField.CharacterValidation.Decimal, ExpectedResult = null)]
[TestCase("A10,0 ", "10,0", InputField.CharacterValidation.Decimal, ExpectedResult = null)]
[TestCase("A'a aaa aaa", "A'a Aaa Aaa", InputField.CharacterValidation.Name, ExpectedResult = null)]
[TestCase(" _JOHN* (Doe)", "John Doe", InputField.CharacterValidation.Name, ExpectedResult = null)]
[TestCase("johndoe@unity3d.com", "johndoe@unity3d.com", InputField.CharacterValidation.EmailAddress, ExpectedResult = null)]
[TestCase(">john doe\\@unity3d.com", "johndoe@unity3d.com", InputField.CharacterValidation.EmailAddress, ExpectedResult = null)]
[TestCase(kEmailSpecialCharacters + "@unity3d.com", kEmailSpecialCharacters + "@unity3d.com", InputField.CharacterValidation.EmailAddress, ExpectedResult = null)]
public IEnumerator HonorsCharacterValidationSettingsTypingWithSelection(string input, string output, InputField.CharacterValidation validation)
{
if (!TouchScreenKeyboard.isSupported)
yield break;
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
inputField.characterValidation = validation;
inputField.text = input;
inputField.OnSelect(eventData);
yield return null;
Assert.AreEqual(output, inputField.text, string.Format("Failed character validation: input ={0}, output ={1}, validation ={2}",
input.Replace(kEmailSpecialCharacters, "specialchars"),
output.Replace(kEmailSpecialCharacters, "specialchars"),
validation));
}
[Test]
public void AssignmentAgainstCharacterLimit([Values("ABC", "abcdefghijkl")] string text)
{
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
// test assignment
inputField.characterLimit = 5;
inputField.text = text;
Assert.AreEqual(text.Substring(0, Math.Min(text.Length, inputField.characterLimit)), inputField.text);
}
[Test] // regression test 793119
public void AssignmentAgainstCharacterLimitWithContentType([Values("Abc", "Abcdefghijkl")] string text)
{
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
// test assignment
inputField.characterLimit = 5;
inputField.contentType = InputField.ContentType.Name;
inputField.text = text;
Assert.AreEqual(text.Substring(0, Math.Min(text.Length, inputField.characterLimit)), inputField.text);
}
[UnityTest]
public IEnumerator SendsEndEditEventOnDeselect()
{
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
inputField.OnSelect(eventData);
yield return null;
var called = false;
inputField.onEndEdit.AddListener((s) => { called = true; });
inputField.OnDeselect(eventData);
Assert.IsTrue(called, "Expected invocation of onEndEdit");
}
[Test]
public void StripsNullCharacters2()
{
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
inputField.text = "a\0b";
Assert.AreEqual("ab", inputField.text, "\\0 characters should be stripped");
}
[UnityTest]
public IEnumerator FocusOpensTouchScreenKeyboard()
{
var isInPlaceEditingDisabled = typeof(TouchScreenKeyboard).GetProperty("disableInPlaceEditing",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
isInPlaceEditingDisabled.SetValue(null, true);
if (!TouchScreenKeyboard.isSupported)
yield break;
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
inputField.OnSelect(eventData);
yield return null;
Assert.NotNull(inputField.touchScreenKeyboard, "Expect a keyboard to be opened");
}
[UnityTest]
public IEnumerator AssignsShouldHideInput()
{
if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
{
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
inputField.shouldHideMobileInput = false;
inputField.OnSelect(eventData);
yield return null;
Assert.IsFalse(inputField.shouldHideMobileInput);
Assert.IsFalse(TouchScreenKeyboard.hideInput, "Expect TouchScreenKeyboard.hideInput to be set");
}
}
}
}
|