File size: 12,466 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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | 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 GenericInputFieldTests : BaseInputFieldTests, IPrebuildSetup
{
protected const string kPrefabPath = "Assets/Resources/GenericInputFieldPrefab.prefab";
public void Setup()
{
#if UNITY_EDITOR
CreateInputFieldAsset(kPrefabPath);
#endif
}
[SetUp]
public virtual void TestSetup()
{
m_PrefabRoot = UnityEngine.Object.Instantiate(Resources.Load("GenericInputFieldPrefab")) as GameObject;
}
[TearDown]
public virtual void TearDown()
{
FontUpdateTracker.UntrackText(m_PrefabRoot.GetComponentInChildren<Text>());
GameObject.DestroyImmediate(m_PrefabRoot);
}
[OneTimeTearDown]
public void OnetimeTearDown()
{
#if UNITY_EDITOR
AssetDatabase.DeleteAsset(kPrefabPath);
#endif
}
[UnityTest]
public IEnumerator CannotFocusIfNotTextComponent()
{
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
inputField.textComponent = null;
inputField.OnSelect(eventData);
yield return null;
Assert.False(inputField.isFocused);
}
[UnityTest]
public IEnumerator CannotFocusIfNullFont()
{
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
inputField.textComponent.font = null;
inputField.OnSelect(eventData);
yield return null;
Assert.False(inputField.isFocused);
}
[UnityTest]
public IEnumerator CannotFocusIfNotActive()
{
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
inputField.enabled = false;
inputField.OnSelect(eventData);
yield return null;
Assert.False(inputField.isFocused);
}
[UnityTest]
public IEnumerator CannotFocusWithoutEventSystem()
{
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
UnityEngine.Object.DestroyImmediate(m_PrefabRoot.GetComponentInChildren<FakeInputModule>());
yield return null;
UnityEngine.Object.DestroyImmediate(m_PrefabRoot.GetComponentInChildren<EventSystem>());
BaseEventData eventData = new BaseEventData(null);
yield return null;
inputField.OnSelect(eventData);
yield return null;
Assert.False(inputField.isFocused);
}
[Test]
public void FocusesOnSelect()
{
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
inputField.OnSelect(eventData);
MethodInfo lateUpdate = typeof(InputField).GetMethod("LateUpdate", BindingFlags.NonPublic | BindingFlags.Instance);
lateUpdate.Invoke(inputField, null);
Assert.True(inputField.isFocused);
}
[Test]
public void DoesNotFocusesOnSelectWhenShouldActivateOnSelect_IsFalse()
{
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
inputField.shouldActivateOnSelect = false;
BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
inputField.OnSelect(eventData);
MethodInfo lateUpdate = typeof(InputField).GetMethod("LateUpdate", BindingFlags.NonPublic | BindingFlags.Instance);
lateUpdate.Invoke(inputField, null);
Assert.False(inputField.isFocused);
}
[Test]
public void InputFieldSetTextWithoutNotifyWillNotNotify()
{
InputField i = m_PrefabRoot.GetComponentInChildren<InputField>();
i.text = "Hello";
bool calledOnValueChanged = false;
i.onValueChanged.AddListener(s => { calledOnValueChanged = true; });
i.SetTextWithoutNotify("Goodbye");
Assert.IsTrue(i.text == "Goodbye");
Assert.IsFalse(calledOnValueChanged);
}
[Test]
public void ContentTypeSetsValues()
{
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
inputField.contentType = InputField.ContentType.Standard;
Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
Assert.AreEqual(TouchScreenKeyboardType.Default, inputField.keyboardType);
Assert.AreEqual(InputField.CharacterValidation.None, inputField.characterValidation);
inputField.contentType = InputField.ContentType.Autocorrected;
Assert.AreEqual(InputField.InputType.AutoCorrect, inputField.inputType);
Assert.AreEqual(TouchScreenKeyboardType.Default, inputField.keyboardType);
Assert.AreEqual(InputField.CharacterValidation.None, inputField.characterValidation);
inputField.contentType = InputField.ContentType.IntegerNumber;
Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
Assert.AreEqual(TouchScreenKeyboardType.NumberPad, inputField.keyboardType);
Assert.AreEqual(InputField.CharacterValidation.Integer, inputField.characterValidation);
inputField.contentType = InputField.ContentType.DecimalNumber;
Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
Assert.AreEqual(TouchScreenKeyboardType.NumbersAndPunctuation, inputField.keyboardType);
Assert.AreEqual(InputField.CharacterValidation.Decimal, inputField.characterValidation);
inputField.contentType = InputField.ContentType.Alphanumeric;
Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
Assert.AreEqual(TouchScreenKeyboardType.ASCIICapable, inputField.keyboardType);
Assert.AreEqual(InputField.CharacterValidation.Alphanumeric, inputField.characterValidation);
inputField.contentType = InputField.ContentType.Name;
Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
Assert.AreEqual(TouchScreenKeyboardType.NamePhonePad, inputField.keyboardType);
Assert.AreEqual(InputField.CharacterValidation.Name, inputField.characterValidation);
inputField.contentType = InputField.ContentType.EmailAddress;
Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
Assert.AreEqual(TouchScreenKeyboardType.EmailAddress, inputField.keyboardType);
Assert.AreEqual(InputField.CharacterValidation.EmailAddress, inputField.characterValidation);
inputField.contentType = InputField.ContentType.Password;
Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
Assert.AreEqual(InputField.InputType.Password, inputField.inputType);
Assert.AreEqual(TouchScreenKeyboardType.Default, inputField.keyboardType);
Assert.AreEqual(InputField.CharacterValidation.None, inputField.characterValidation);
inputField.contentType = InputField.ContentType.Pin;
Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
Assert.AreEqual(InputField.InputType.Password, inputField.inputType);
Assert.AreEqual(TouchScreenKeyboardType.NumberPad, inputField.keyboardType);
Assert.AreEqual(InputField.CharacterValidation.Integer, inputField.characterValidation);
}
[Test]
public void SettingLineTypeDoesNotChangesContentTypeToCustom([Values(InputField.ContentType.Standard, InputField.ContentType.Autocorrected)] InputField.ContentType type)
{
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
inputField.contentType = type;
inputField.lineType = InputField.LineType.MultiLineNewline;
Assert.AreEqual(type, inputField.contentType);
}
[Test]
public void SettingLineTypeChangesContentTypeToCustom()
{
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
inputField.contentType = InputField.ContentType.Name;
inputField.lineType = InputField.LineType.MultiLineNewline;
Assert.AreEqual(InputField.ContentType.Custom, inputField.contentType);
}
[Test]
public void SettingInputChangesContentTypeToCustom()
{
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
inputField.contentType = InputField.ContentType.Name;
inputField.inputType = InputField.InputType.Password;
Assert.AreEqual(InputField.ContentType.Custom, inputField.contentType);
}
[Test]
public void SettingCharacterValidationChangesContentTypeToCustom()
{
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
inputField.contentType = InputField.ContentType.Name;
inputField.characterValidation = InputField.CharacterValidation.None;
Assert.AreEqual(InputField.ContentType.Custom, inputField.contentType);
}
[Test]
public void SettingKeyboardTypeChangesContentTypeToCustom()
{
InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
inputField.contentType = InputField.ContentType.Name;
inputField.keyboardType = TouchScreenKeyboardType.ASCIICapable;
Assert.AreEqual(InputField.ContentType.Custom, inputField.contentType);
}
[UnityTest]
public IEnumerator CaretRectSameSizeAsTextRect()
{
InputField inputfield = m_PrefabRoot.GetComponentInChildren<InputField>();
HorizontalLayoutGroup lg = inputfield.gameObject.AddComponent<HorizontalLayoutGroup>();
lg.childControlWidth = true;
lg.childControlHeight = false;
lg.childForceExpandWidth = true;
lg.childForceExpandHeight = true;
ContentSizeFitter csf = inputfield.gameObject.AddComponent<ContentSizeFitter>();
csf.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
csf.verticalFit = ContentSizeFitter.FitMode.Unconstrained;
inputfield.text = "Hello World!";
yield return new WaitForSeconds(1.0f);
Rect prevTextRect = inputfield.textComponent.rectTransform.rect;
Rect prevCaretRect = (inputfield.textComponent.transform.parent.GetChild(0) as RectTransform).rect;
inputfield.text = "Hello World!Hello World!Hello World!";
LayoutRebuilder.MarkLayoutForRebuild(inputfield.transform as RectTransform);
yield return new WaitForSeconds(1.0f);
Rect newTextRect = inputfield.textComponent.rectTransform.rect;
Rect newCaretRect = (inputfield.textComponent.transform.parent.GetChild(0) as RectTransform).rect;
Assert.IsFalse(prevTextRect == newTextRect);
Assert.IsTrue(prevTextRect == prevCaretRect);
Assert.IsFalse(prevCaretRect == newCaretRect);
Assert.IsTrue(newTextRect == newCaretRect);
}
}
}
|