Maze / Library /PackageCache /com.unity.ugui@1.0.0 /Tests /Runtime /InputField /TouchInputFieldTests.cs
| 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() | |
| { | |
| CreateInputFieldAsset(kPrefabPath); | |
| } | |
| [] | |
| 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>()); | |
| } | |
| [] | |
| public void TearDown() | |
| { | |
| InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>(); | |
| TouchScreenKeyboard.hideInput = false; | |
| FontUpdateTracker.UntrackText(m_PrefabRoot.GetComponentInChildren<Text>()); | |
| GameObject.DestroyImmediate(m_PrefabRoot); | |
| } | |
| [] | |
| public void OnetimeTearDown() | |
| { | |
| AssetDatabase.DeleteAsset(kPrefabPath); | |
| } | |
| 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); | |
| } | |
| } | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| 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)); | |
| } | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| 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)); | |
| } | |
| [] | |
| 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); | |
| } | |
| [] // 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); | |
| } | |
| [] | |
| 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"); | |
| } | |
| [] | |
| public void StripsNullCharacters2() | |
| { | |
| InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>(); | |
| inputField.text = "a\0b"; | |
| Assert.AreEqual("ab", inputField.text, "\\0 characters should be stripped"); | |
| } | |
| [] | |
| 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"); | |
| } | |
| [] | |
| 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"); | |
| } | |
| } | |
| } | |
| } | |