| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Models; |
| | 4 | | using NUnit.Framework; |
| | 5 | | using System.Collections; |
| | 6 | | using Newtonsoft.Json; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.TestTools; |
| | 9 | |
|
| | 10 | | namespace Tests |
| | 11 | | { |
| | 12 | | public class UITextTests : UITestsBase |
| | 13 | | { |
| | 14 | | [UnityTest] |
| | 15 | | public IEnumerator TestPropertiesAreAppliedCorrectly() |
| | 16 | | { |
| | 17 | | // Create UIScreenSpaceShape |
| 1 | 18 | | UIScreenSpace screenSpaceShape = |
| | 19 | | TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene, |
| | 20 | | CLASS_ID.UI_SCREEN_SPACE_SHAPE); |
| | 21 | |
|
| 1 | 22 | | yield return screenSpaceShape.routine; |
| | 23 | |
|
| | 24 | | // Create UITextShape |
| 1 | 25 | | UIText uiTextShape = TestHelpers.SharedComponentCreate<UIText, UIText.Model>(scene, CLASS_ID.UI_TEXT_SHAPE, |
| | 26 | | new UIText.Model { }); |
| | 27 | |
|
| 1 | 28 | | yield return uiTextShape.routine; |
| | 29 | |
|
| | 30 | | // Check default properties are applied correctly |
| 1 | 31 | | Assert.IsTrue(uiTextShape.referencesContainer.transform.parent == screenSpaceShape.childHookRectTransform); |
| 1 | 32 | | Assert.IsTrue(uiTextShape.referencesContainer.canvasGroup.blocksRaycasts); |
| 1 | 33 | | Assert.AreEqual(100f, uiTextShape.childHookRectTransform.rect.width); |
| 1 | 34 | | Assert.AreEqual(50f, uiTextShape.childHookRectTransform.rect.height); |
| 1 | 35 | | Assert.IsTrue(uiTextShape.referencesContainer.text.enabled); |
| 1 | 36 | | Assert.AreEqual(Color.white, uiTextShape.referencesContainer.text.color); |
| 1 | 37 | | Assert.AreEqual(100f, uiTextShape.referencesContainer.text.fontSize); |
| 1 | 38 | | Assert.AreEqual("", uiTextShape.referencesContainer.text.text); |
| 1 | 39 | | Assert.AreEqual(int.MaxValue, uiTextShape.referencesContainer.text.maxVisibleLines); |
| 1 | 40 | | Assert.AreEqual(0, uiTextShape.referencesContainer.text.lineSpacing); |
| 1 | 41 | | Assert.IsFalse(uiTextShape.referencesContainer.text.enableAutoSizing); |
| 1 | 42 | | Assert.IsFalse(uiTextShape.referencesContainer.text.enableWordWrapping); |
| 1 | 43 | | Assert.IsFalse(uiTextShape.referencesContainer.text.fontMaterial.IsKeywordEnabled("UNDERLAY_ON")); |
| | 44 | |
|
| 1 | 45 | | Vector2 alignedPosition = CalculateAlignedAnchoredPosition(screenSpaceShape.childHookRectTransform.rect, |
| | 46 | | uiTextShape.childHookRectTransform.rect); |
| 1 | 47 | | Assert.AreEqual(alignedPosition.ToString(), uiTextShape.childHookRectTransform.anchoredPosition.ToString()); |
| | 48 | |
|
| 1 | 49 | | Assert.AreEqual(0, uiTextShape.referencesContainer.text.margin.x); |
| 1 | 50 | | Assert.AreEqual(0, uiTextShape.referencesContainer.text.margin.y); |
| 1 | 51 | | Assert.AreEqual(0, uiTextShape.referencesContainer.text.margin.z); |
| 1 | 52 | | Assert.AreEqual(0, uiTextShape.referencesContainer.text.margin.w); |
| | 53 | |
|
| | 54 | | // Update UITextShape |
| 1 | 55 | | yield return TestHelpers.SharedComponentUpdate<UIText>(uiTextShape, |
| | 56 | | new UIText.Model |
| | 57 | | { |
| | 58 | | isPointerBlocker = true, |
| | 59 | | hAlign = "left", |
| | 60 | | vAlign = "bottom", |
| | 61 | | textModel = new TextShape.Model |
| | 62 | | { |
| | 63 | | value = "hello world", |
| | 64 | | color = Color.green, |
| | 65 | | opacity = 0.5f, |
| | 66 | | fontSize = 35f, |
| | 67 | | paddingTop = 10f, |
| | 68 | | paddingRight = 30f, |
| | 69 | | paddingBottom = 20f, |
| | 70 | | paddingLeft = 15, |
| | 71 | | lineSpacing = 0.1f, |
| | 72 | | lineCount = 3, |
| | 73 | | shadowOffsetX = 0.1f, |
| | 74 | | shadowOffsetY = 0.1f, |
| | 75 | | shadowColor = Color.yellow, |
| | 76 | | textWrapping = true |
| | 77 | | } |
| | 78 | | }); |
| | 79 | |
|
| | 80 | | // Check default properties are applied correctly |
| 1 | 81 | | Assert.IsTrue(uiTextShape.referencesContainer.transform.parent == screenSpaceShape.childHookRectTransform); |
| 1 | 82 | | Assert.IsTrue(uiTextShape.referencesContainer.text.raycastTarget); |
| 1 | 83 | | Assert.AreEqual(100f, uiTextShape.childHookRectTransform.rect.width); |
| 1 | 84 | | Assert.AreEqual(50f, uiTextShape.childHookRectTransform.rect.height); |
| 1 | 85 | | Assert.AreEqual("hello world", uiTextShape.referencesContainer.text.text); |
| 1 | 86 | | Assert.IsTrue(uiTextShape.referencesContainer.text.enabled); |
| 1 | 87 | | Assert.AreEqual(new Color(0f, 1f, 0f, 0.5f), uiTextShape.referencesContainer.text.color); |
| 1 | 88 | | Assert.AreEqual(35f, uiTextShape.referencesContainer.text.fontSize); |
| 1 | 89 | | Assert.AreEqual(3, uiTextShape.referencesContainer.text.maxVisibleLines); |
| 1 | 90 | | Assert.AreEqual(0.1f, uiTextShape.referencesContainer.text.lineSpacing); |
| 1 | 91 | | Assert.IsTrue(uiTextShape.referencesContainer.text.enableWordWrapping); |
| 1 | 92 | | Assert.IsTrue(uiTextShape.referencesContainer.text.fontMaterial.IsKeywordEnabled("UNDERLAY_ON")); |
| 1 | 93 | | Assert.AreEqual(Color.yellow, uiTextShape.referencesContainer.text.fontMaterial.GetColor("_UnderlayColor")); |
| | 94 | |
|
| 1 | 95 | | alignedPosition = CalculateAlignedAnchoredPosition(screenSpaceShape.childHookRectTransform.rect, |
| | 96 | | uiTextShape.childHookRectTransform.rect, "bottom", "left"); |
| 1 | 97 | | Assert.AreEqual(alignedPosition.ToString(), uiTextShape.childHookRectTransform.anchoredPosition.ToString()); |
| | 98 | |
|
| 1 | 99 | | Assert.AreEqual(15f, uiTextShape.referencesContainer.text.margin.x); |
| 1 | 100 | | Assert.AreEqual(10f, uiTextShape.referencesContainer.text.margin.y); |
| 1 | 101 | | Assert.AreEqual(30f, uiTextShape.referencesContainer.text.margin.z); |
| 1 | 102 | | Assert.AreEqual(20f, uiTextShape.referencesContainer.text.margin.w); |
| | 103 | |
|
| 1 | 104 | | yield return TestHelpers.SharedComponentUpdate<UIText>(uiTextShape, |
| | 105 | | new UIText.Model |
| | 106 | | { |
| | 107 | |
|
| | 108 | | positionX = new UIValue(80), |
| | 109 | | positionY = new UIValue(80), |
| | 110 | | }); |
| | 111 | |
|
| 1 | 112 | | Assert.AreEqual(80f, uiTextShape.model.positionX.value); |
| 1 | 113 | | Assert.AreEqual(80f, uiTextShape.model.positionY.value); |
| 1 | 114 | | } |
| | 115 | |
|
| | 116 | | [UnityTest] |
| | 117 | | public IEnumerator TestOnClickEvent() |
| | 118 | | { |
| 1 | 119 | | UIScreenSpace screenSpaceShape = |
| | 120 | | TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene, |
| | 121 | | CLASS_ID.UI_SCREEN_SPACE_SHAPE); |
| | 122 | |
|
| 1 | 123 | | yield return screenSpaceShape.routine; |
| | 124 | |
|
| 1 | 125 | | Assert.IsFalse(screenSpaceShape == null); |
| | 126 | |
|
| | 127 | | // Create UITextShape |
| 1 | 128 | | UIText uiTextShape = TestHelpers.SharedComponentCreate<UIText, UIText.Model>(scene, CLASS_ID.UI_TEXT_SHAPE, |
| | 129 | | new UIText.Model { }); |
| | 130 | |
|
| 1 | 131 | | yield return uiTextShape.routine; |
| | 132 | |
|
| | 133 | | // Update UITextShape |
| 1 | 134 | | yield return TestHelpers.SharedComponentUpdate( |
| | 135 | | uiTextShape, |
| | 136 | | new UIText.Model |
| | 137 | | { |
| | 138 | | isPointerBlocker = true, |
| | 139 | | hAlign = "left", |
| | 140 | | vAlign = "bottom", |
| | 141 | | onClick = "UUIDFakeEventId", |
| | 142 | | textModel = new TextShape.Model |
| | 143 | | { |
| | 144 | | value = "hello world", |
| | 145 | | color = Color.green, |
| | 146 | | opacity = 0.5f, |
| | 147 | | fontSize = 35f, |
| | 148 | | paddingTop = 10f, |
| | 149 | | paddingRight = 30f, |
| | 150 | | paddingBottom = 20f, |
| | 151 | | paddingLeft = 15, |
| | 152 | | lineSpacing = 0.1f, |
| | 153 | | lineCount = 3, |
| | 154 | | shadowOffsetX = 0.1f, |
| | 155 | | shadowOffsetY = 0.1f, |
| | 156 | | shadowColor = Color.yellow, |
| | 157 | | textWrapping = true, |
| | 158 | | } |
| | 159 | | }); |
| | 160 | |
|
| | 161 | | //------------------------------------------------------------------------ |
| | 162 | | // Test click events |
| 1 | 163 | | bool eventResult = false; |
| | 164 | |
|
| 1 | 165 | | yield return TestHelpers.TestUIClickEventPropagation( |
| | 166 | | scene.sceneData.id, |
| | 167 | | uiTextShape.model, |
| | 168 | | uiTextShape.referencesContainer.childHookRectTransform, |
| | 169 | | (bool res) => |
| | 170 | | { |
| | 171 | | // Check image object clicking triggers the correct event |
| 1 | 172 | | eventResult = res; |
| 1 | 173 | | }); |
| | 174 | |
|
| 1 | 175 | | Assert.IsTrue(eventResult); |
| 1 | 176 | | } |
| | 177 | |
|
| | 178 | | [UnityTest] |
| 2 | 179 | | public IEnumerator AddedCorrectlyOnInvisibleParent() { yield return TestHelpers.TestUIElementAddedCorrectlyOnInv |
| | 180 | | } |
| | 181 | | } |