123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- 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);
- }
- }
- }
|