| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Interface; |
| | 4 | | using DCL.Models; |
| | 5 | | using NUnit.Framework; |
| | 6 | | using System.Collections; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.TestTools; |
| | 9 | | using Color = UnityEngine.Color; |
| | 10 | |
|
| | 11 | | namespace Tests |
| | 12 | | { |
| | 13 | | public class UIInputTextTests : IntegrationTestSuite_Legacy |
| | 14 | | { |
| | 15 | | UIScreenSpace ssshape; |
| | 16 | | UIInputText textInput; |
| | 17 | | Camera mockCamera; |
| | 18 | |
|
| | 19 | | public IEnumerator InputTextCreate() |
| | 20 | | { |
| 4 | 21 | | ssshape = TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>( |
| | 22 | | scene, |
| | 23 | | DCL.Models.CLASS_ID.UI_SCREEN_SPACE_SHAPE); |
| | 24 | |
|
| 4 | 25 | | yield return ssshape.routine; |
| | 26 | |
|
| 4 | 27 | | if (mockCamera == null) |
| | 28 | | { |
| 4 | 29 | | GameObject go = new GameObject("Mock camera"); |
| 4 | 30 | | mockCamera = go.AddComponent<Camera>(); |
| 4 | 31 | | mockCamera.clearFlags = CameraClearFlags.Color; |
| 4 | 32 | | mockCamera.backgroundColor = Color.black; |
| | 33 | | } |
| | 34 | |
|
| 4 | 35 | | textInput = TestHelpers.SharedComponentCreate<UIInputText, UIInputText.Model>( |
| | 36 | | scene, |
| | 37 | | DCL.Models.CLASS_ID.UI_INPUT_TEXT_SHAPE, |
| | 38 | | new UIInputText.Model() |
| | 39 | | { |
| | 40 | | textModel = new DCL.Components.TextShape.Model() |
| | 41 | | { |
| | 42 | | color = Color.white, |
| | 43 | | opacity = 1, |
| | 44 | | }, |
| | 45 | |
|
| | 46 | | isPointerBlocker = true, |
| | 47 | | placeholder = "Chat here!", |
| | 48 | | placeholderColor = Color.grey, |
| | 49 | | focusedBackground = Color.black, |
| | 50 | | parentComponent = ssshape.id, |
| | 51 | | positionX = new UIValue(0.5f, UIValue.Unit.PERCENT), |
| | 52 | | positionY = new UIValue(0.5f, UIValue.Unit.PERCENT), |
| | 53 | | height = new UIValue(100), |
| | 54 | | width = new UIValue(100), |
| | 55 | | onClick = "UUIDFakeEventId" |
| | 56 | | }); |
| | 57 | |
|
| 4 | 58 | | yield return textInput.routine; |
| 4 | 59 | | yield return null; |
| | 60 | |
|
| 4 | 61 | | if (mockCamera != null) |
| 4 | 62 | | Object.Destroy(mockCamera.gameObject); |
| 4 | 63 | | } |
| | 64 | |
|
| | 65 | | [UnityTest] |
| | 66 | | public IEnumerator TestOnClickEvent() |
| | 67 | | { |
| 1 | 68 | | yield return InputTextCreate(); |
| | 69 | |
|
| | 70 | | //------------------------------------------------------------------------ |
| | 71 | | // Test click events |
| 1 | 72 | | TMPro.TMP_InputField inputField = textInput.referencesContainer.inputField; |
| | 73 | |
|
| 1 | 74 | | string targetEventType = "SceneEvent"; |
| | 75 | |
|
| 1 | 76 | | var onClickEvent = new WebInterface.OnClickEvent(); |
| 1 | 77 | | onClickEvent.uuid = textInput.model.onClick; |
| | 78 | |
|
| 1 | 79 | | var sceneEvent = new WebInterface.SceneEvent<WebInterface.OnClickEvent>(); |
| 1 | 80 | | sceneEvent.sceneId = scene.sceneData.id; |
| 1 | 81 | | sceneEvent.payload = onClickEvent; |
| 1 | 82 | | sceneEvent.eventType = "uuidEvent"; |
| 1 | 83 | | string eventJSON = JsonUtility.ToJson(sceneEvent); |
| | 84 | |
|
| 1 | 85 | | bool eventTriggered = false; |
| | 86 | |
|
| | 87 | |
|
| 1 | 88 | | yield return TestHelpers.WaitForMessageFromEngine(targetEventType, eventJSON, |
| | 89 | | () => |
| | 90 | | { |
| 1 | 91 | | UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject(inputField.gameObject, null); |
| 1 | 92 | | inputField.ActivateInputField(); |
| 1 | 93 | | inputField.Select(); |
| 1 | 94 | | }, |
| 2 | 95 | | () => { eventTriggered = true; }); |
| | 96 | |
|
| 1 | 97 | | yield return null; |
| | 98 | |
|
| 1 | 99 | | Assert.IsTrue(eventTriggered); |
| 1 | 100 | | } |
| | 101 | |
|
| | 102 | | [UnityTest] |
| | 103 | | public IEnumerator TestPropertiesAreAppliedCorrectly() |
| | 104 | | { |
| 1 | 105 | | yield return InputTextCreate(); |
| | 106 | |
|
| 1 | 107 | | Assert.AreEqual(Color.white, textInput.model.textModel.color); |
| 1 | 108 | | Assert.AreEqual(1, textInput.model.textModel.opacity); |
| 1 | 109 | | Assert.AreEqual("Chat here!", textInput.inputField.text); |
| 1 | 110 | | Assert.IsTrue(textInput.referencesContainer != null, "Ref container is null?!"); |
| 1 | 111 | | Assert.AreEqual(textInput.referencesContainer.transform.parent, ssshape.childHookRectTransform); |
| 1 | 112 | | Assert.AreEqual(textInput.model.focusedBackground.r, textInput.referencesContainer.bgImage.color.r); |
| 1 | 113 | | Assert.AreEqual(textInput.model.focusedBackground.g, textInput.referencesContainer.bgImage.color.g); |
| 1 | 114 | | Assert.AreEqual(textInput.model.focusedBackground.b, textInput.referencesContainer.bgImage.color.b); |
| 1 | 115 | | Assert.AreEqual(textInput.model.textModel.opacity, textInput.referencesContainer.bgImage.color.a); |
| | 116 | |
|
| 1 | 117 | | ssshape.Dispose(); |
| 1 | 118 | | textInput.Dispose(); |
| 1 | 119 | | Object.DestroyImmediate(mockCamera.gameObject); |
| 1 | 120 | | } |
| | 121 | |
|
| | 122 | | [UnityTest] |
| 2 | 123 | | public IEnumerator AddedCorrectlyOnInvisibleParent() { yield return TestHelpers.TestUIElementAddedCorrectlyOnInv |
| | 124 | |
|
| | 125 | | [UnityTest] |
| | 126 | | public IEnumerator TestOnFocus() |
| | 127 | | { |
| 1 | 128 | | yield return InputTextCreate(); |
| | 129 | |
|
| 1 | 130 | | textInput.OnFocus(""); |
| | 131 | |
|
| 1 | 132 | | Assert.IsTrue(textInput.inputField.caretColor == Color.white); |
| 1 | 133 | | Assert.IsTrue(textInput.inputField.text == ""); |
| | 134 | |
|
| 1 | 135 | | textInput.OnBlur(""); |
| | 136 | |
|
| 1 | 137 | | Assert.IsTrue(textInput.inputField.text == textInput.model.placeholder); |
| 1 | 138 | | Assert.IsTrue(textInput.inputField.caretColor == Color.clear); |
| | 139 | |
|
| 1 | 140 | | ssshape.Dispose(); |
| 1 | 141 | | textInput.Dispose(); |
| 1 | 142 | | Object.DestroyImmediate(mockCamera.gameObject); |
| 1 | 143 | | } |
| | 144 | |
|
| | 145 | | [UnityTest] |
| | 146 | | public IEnumerator TestOnSubmit() |
| | 147 | | { |
| 1 | 148 | | yield return InputTextCreate(); |
| | 149 | |
|
| 1 | 150 | | textInput.inputField.text = "hello world"; |
| 1 | 151 | | textInput.OnSubmit(""); |
| | 152 | |
|
| 1 | 153 | | Assert.IsTrue(textInput.inputField.text == ""); |
| 1 | 154 | | Assert.IsTrue(textInput.inputField.caretColor == Color.white); |
| | 155 | |
|
| 1 | 156 | | ssshape.Dispose(); |
| 1 | 157 | | textInput.Dispose(); |
| 1 | 158 | | Object.DestroyImmediate(mockCamera.gameObject); |
| 1 | 159 | | } |
| | 160 | | } |
| | 161 | | } |