| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Models; |
| | 4 | | using NUnit.Framework; |
| | 5 | | using System.Collections; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.TestTools; |
| | 9 | |
|
| | 10 | | namespace Tests |
| | 11 | | { |
| | 12 | | public class TextShapeTests : IntegrationTestSuite_Legacy |
| | 13 | | { |
| | 14 | | [UnityTest] |
| | 15 | | public IEnumerator TestCreate() |
| | 16 | | { |
| 1 | 17 | | string entityId = "e1"; |
| | 18 | |
|
| 1 | 19 | | TestHelpers.CreateSceneEntity(scene, entityId); |
| | 20 | |
|
| 1 | 21 | | yield return null; |
| | 22 | |
|
| 1 | 23 | | var textShapeModel = new TextShape.Model() |
| | 24 | | { |
| | 25 | | value = "Hello world!", |
| | 26 | |
|
| | 27 | | color = Color.white, |
| | 28 | | opacity = 0.5f, |
| | 29 | | fontSize = 10, |
| | 30 | | fontWeight = "", |
| | 31 | |
|
| | 32 | | width = 20, |
| | 33 | | height = 20, |
| | 34 | | adaptHeight = false, |
| | 35 | | adaptWidth = false, |
| | 36 | | paddingTop = 10, |
| | 37 | | paddingRight = 0, |
| | 38 | | paddingBottom = 10, |
| | 39 | | paddingLeft = 0, |
| | 40 | |
|
| | 41 | | shadowBlur = 0, |
| | 42 | | shadowOffsetX = 0, |
| | 43 | | shadowOffsetY = 0, |
| | 44 | | shadowColor = Color.white |
| | 45 | | }; |
| | 46 | |
|
| 1 | 47 | | TextShape textShape = |
| | 48 | | TestHelpers.EntityComponentCreate<TextShape, TextShape.Model>(scene, scene.entities[entityId], |
| | 49 | | textShapeModel); |
| | 50 | |
|
| 1 | 51 | | yield return textShape.routine; |
| | 52 | |
|
| 1 | 53 | | TextMeshPro tmpro = textShape.GetComponentInChildren<TextMeshPro>(); |
| | 54 | |
|
| 1 | 55 | | Assert.IsTrue(textShape != null, "Component creation fail!"); |
| 1 | 56 | | Assert.IsTrue(tmpro != null, "TextMeshPro doesn't exists for TextShape!"); |
| 1 | 57 | | Assert.IsTrue(textShape.text != null, "Unity Text component doesn't exists for TextShape!"); |
| | 58 | |
|
| 1 | 59 | | yield return null; |
| | 60 | |
|
| 1 | 61 | | TMProConsistencyAsserts(tmpro, textShapeModel); |
| | 62 | |
|
| 1 | 63 | | textShapeModel.paddingLeft = 5; |
| 1 | 64 | | textShapeModel.paddingRight = 15; |
| 1 | 65 | | textShapeModel.value = "Hello world again!"; |
| | 66 | |
|
| 1 | 67 | | TextShape textShape2 = |
| | 68 | | TestHelpers.EntityComponentCreate<TextShape, TextShape.Model>(scene, scene.entities[entityId], |
| | 69 | | textShapeModel); |
| | 70 | |
|
| 1 | 71 | | TMProConsistencyAsserts(tmpro, textShapeModel); |
| | 72 | |
|
| 1 | 73 | | yield return null; |
| 1 | 74 | | } |
| | 75 | |
|
| | 76 | | void TMProConsistencyAsserts(TextMeshPro tmpro, TextShape.Model model) |
| | 77 | | { |
| 2 | 78 | | Assert.AreEqual(model.paddingLeft, tmpro.margin[0], 0.01, |
| | 79 | | string.Format("Left margin must be {0}", model.paddingLeft)); |
| 2 | 80 | | Assert.AreEqual(model.paddingTop, tmpro.margin[1], 0.01, |
| | 81 | | string.Format("Top margin must be {0}", model.paddingTop)); |
| 2 | 82 | | Assert.AreEqual(model.paddingRight, tmpro.margin[2], 0.01, |
| | 83 | | string.Format("Right margin must be {0}", model.paddingRight)); |
| 2 | 84 | | Assert.AreEqual(model.paddingBottom, tmpro.margin[3], 0.01, |
| | 85 | | string.Format("Bottom margin must be {0}", model.paddingBottom)); |
| | 86 | |
|
| 2 | 87 | | Assert.IsTrue(tmpro.text == model.value, "Text wasn't set correctly!"); |
| 2 | 88 | | } |
| | 89 | |
|
| | 90 | | [UnityTest] |
| | 91 | | public IEnumerator TestMissingValuesGetDefaultedOnUpdate() |
| | 92 | | { |
| 1 | 93 | | string entityId = "1"; |
| 1 | 94 | | TestHelpers.CreateSceneEntity(scene, entityId); |
| | 95 | |
|
| | 96 | | // 1. Create component with non-default configs |
| 1 | 97 | | TextShape.Model textShapeModel = new TextShape.Model |
| | 98 | | { |
| | 99 | | color = Color.green, |
| | 100 | | width = 0.25f, |
| | 101 | | lineCount = 3, |
| | 102 | | fontAutoSize = true, |
| | 103 | | shadowColor = Color.red |
| | 104 | | }; |
| | 105 | |
|
| 1 | 106 | | TextShape textShapeComponent = |
| | 107 | | TestHelpers.EntityComponentCreate<TextShape, TextShape.Model>(scene, scene.entities[entityId], |
| | 108 | | textShapeModel); |
| | 109 | |
|
| 1 | 110 | | yield return textShapeComponent.routine; |
| | 111 | |
|
| | 112 | | // 2. Check configured values |
| 1 | 113 | | Assert.AreEqual(Color.green, textShapeComponent.GetModel().color); |
| 1 | 114 | | Assert.AreEqual(0.25f, textShapeComponent.GetModel().width); |
| 1 | 115 | | Assert.AreEqual(3, textShapeComponent.GetModel().lineCount); |
| 1 | 116 | | Assert.IsTrue(textShapeComponent.GetModel().fontAutoSize); |
| 1 | 117 | | Assert.AreEqual(Color.red, textShapeComponent.GetModel().shadowColor); |
| | 118 | |
|
| | 119 | | // 3. Update component with missing values |
| 1 | 120 | | scene.EntityComponentUpdate(scene.entities[entityId], CLASS_ID_COMPONENT.TEXT_SHAPE, |
| | 121 | | JsonUtility.ToJson(new TextShape.Model { })); |
| | 122 | |
|
| 1 | 123 | | yield return textShapeComponent.routine; |
| | 124 | |
|
| | 125 | | // 4. Check defaulted values |
| 1 | 126 | | Assert.AreEqual(Color.white, textShapeComponent.GetModel().color); |
| 1 | 127 | | Assert.AreEqual(1f, textShapeComponent.GetModel().width); |
| 1 | 128 | | Assert.AreEqual(0, textShapeComponent.GetModel().lineCount); |
| 1 | 129 | | Assert.IsFalse(textShapeComponent.GetModel().fontAutoSize); |
| 1 | 130 | | Assert.AreEqual(new Color(1, 1, 1), textShapeComponent.GetModel().shadowColor); |
| 1 | 131 | | } |
| | 132 | |
|
| | 133 | | [UnityTest] |
| | 134 | | [TestCase(0, ExpectedResult = null)] |
| | 135 | | [TestCase(0.3f, ExpectedResult = null)] |
| | 136 | | [TestCase(1, ExpectedResult = null)] |
| | 137 | | public IEnumerator OpacityIsProcessedCorrectly(float opacity) |
| | 138 | | { |
| 3 | 139 | | IDCLEntity entity = TestHelpers.CreateSceneEntity(scene); |
| 3 | 140 | | TextShape textShapeComponent = TestHelpers.EntityComponentCreate<TextShape, TextShape.Model>(scene, entity, |
| | 141 | |
|
| 3 | 142 | | yield return textShapeComponent.routine; |
| | 143 | |
|
| 3 | 144 | | TextMeshPro tmpro = textShapeComponent.gameObject.GetComponentInChildren<TextMeshPro>(); |
| | 145 | |
|
| 3 | 146 | | Assert.NotNull(tmpro); |
| 3 | 147 | | Assert.AreEqual(opacity, textShapeComponent.GetModel().opacity); |
| 3 | 148 | | Assert.AreEqual(tmpro.color.a, opacity); |
| 3 | 149 | | } |
| | 150 | |
|
| | 151 | | [UnityTest] |
| | 152 | | public IEnumerator VisibleTrueIsProcessedCorrectly() |
| | 153 | | { |
| 1 | 154 | | IDCLEntity entity = TestHelpers.CreateSceneEntity(scene); |
| 1 | 155 | | TextShape textShapeComponent = TestHelpers.EntityComponentCreate<TextShape, TextShape.Model>(scene, entity, |
| | 156 | |
|
| 1 | 157 | | yield return textShapeComponent.routine; |
| | 158 | |
|
| 1 | 159 | | TextMeshPro tmpro = textShapeComponent.gameObject.GetComponentInChildren<TextMeshPro>(); |
| | 160 | |
|
| 1 | 161 | | Assert.NotNull(tmpro); |
| 1 | 162 | | Assert.AreEqual(0.3f, textShapeComponent.GetModel().opacity); |
| 1 | 163 | | Assert.IsTrue(textShapeComponent.GetModel().visible); |
| 1 | 164 | | Assert.AreEqual(tmpro.color.a, 0.3f); |
| 1 | 165 | | } |
| | 166 | |
|
| | 167 | | [UnityTest] |
| | 168 | | public IEnumerator VisibleFalseIsProcessedCorrectly() |
| | 169 | | { |
| 1 | 170 | | IDCLEntity entity = TestHelpers.CreateSceneEntity(scene); |
| 1 | 171 | | TextShape textShapeComponent = TestHelpers.EntityComponentCreate<TextShape, TextShape.Model>(scene, entity, |
| | 172 | |
|
| 1 | 173 | | yield return textShapeComponent.routine; |
| | 174 | |
|
| 1 | 175 | | TextMeshPro tmpro = textShapeComponent.gameObject.GetComponentInChildren<TextMeshPro>(); |
| | 176 | |
|
| 1 | 177 | | Assert.NotNull(tmpro); |
| 1 | 178 | | Assert.AreEqual(0.3f, textShapeComponent.GetModel().opacity); |
| 1 | 179 | | Assert.IsFalse(textShapeComponent.GetModel().visible); |
| 1 | 180 | | Assert.AreEqual(tmpro.color.a, 0f); |
| 1 | 181 | | } |
| | 182 | | } |
| | 183 | | } |