Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

GraphicTests.cs 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using NUnit.Framework;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.TestTools;
  8. using UnityEngine.UI;
  9. using UnityEngine.UI.Tests;
  10. namespace Graphics
  11. {
  12. class GraphicTests : IPrebuildSetup
  13. {
  14. private GameObject m_PrefabRoot;
  15. private ConcreteGraphic m_graphic;
  16. private Canvas m_canvas;
  17. const string kPrefabPath = "Assets/Resources/GraphicTestsPrefab.prefab";
  18. bool m_dirtyVert;
  19. bool m_dirtyLayout;
  20. bool m_dirtyMaterial;
  21. public void Setup()
  22. {
  23. #if UNITY_EDITOR
  24. var rootGO = new GameObject("rootGo");
  25. var canvasGO = new GameObject("CanvasRoot", typeof(Canvas), typeof(GraphicRaycaster));
  26. canvasGO.transform.SetParent(rootGO.transform);
  27. var graphicGO = new GameObject("Graphic", typeof(RectTransform), typeof(ConcreteGraphic));
  28. graphicGO.transform.SetParent(canvasGO.transform);
  29. var gameObject = new GameObject("EventSystem", typeof(EventSystem));
  30. gameObject.transform.SetParent(rootGO.transform);
  31. if (!Directory.Exists("Assets/Resources/"))
  32. Directory.CreateDirectory("Assets/Resources/");
  33. PrefabUtility.SaveAsPrefabAsset(rootGO, kPrefabPath);
  34. GameObject.DestroyImmediate(rootGO);
  35. #endif
  36. }
  37. [SetUp]
  38. public void TestSetup()
  39. {
  40. m_PrefabRoot = Object.Instantiate(Resources.Load("GraphicTestsPrefab")) as GameObject;
  41. m_canvas = m_PrefabRoot.GetComponentInChildren<Canvas>();
  42. m_graphic = m_PrefabRoot.GetComponentInChildren<ConcreteGraphic>();
  43. m_graphic.RegisterDirtyVerticesCallback(() => m_dirtyVert = true);
  44. m_graphic.RegisterDirtyLayoutCallback(() => m_dirtyLayout = true);
  45. m_graphic.RegisterDirtyMaterialCallback(() => m_dirtyMaterial = true);
  46. ResetDirtyFlags();
  47. }
  48. [TearDown]
  49. public void TearDown()
  50. {
  51. m_graphic = null;
  52. m_canvas = null;
  53. GameObject.DestroyImmediate(m_PrefabRoot);
  54. }
  55. [OneTimeTearDown]
  56. public void OneTimeTearDown()
  57. {
  58. #if UNITY_EDITOR
  59. AssetDatabase.DeleteAsset(kPrefabPath);
  60. #endif
  61. }
  62. private void ResetDirtyFlags()
  63. {
  64. m_dirtyVert = m_dirtyLayout = m_dirtyMaterial = false;
  65. }
  66. [Test]
  67. public void SettingDirtyOnActiveGraphicCallsCallbacks()
  68. {
  69. m_graphic.enabled = false;
  70. m_graphic.enabled = true;
  71. m_graphic.SetAllDirty();
  72. Assert.True(m_dirtyVert, "Vertices have not been dirtied");
  73. Assert.True(m_dirtyLayout, "Layout has not been dirtied");
  74. Assert.True(m_dirtyMaterial, "Material has not been dirtied");
  75. ResetDirtyFlags();
  76. m_graphic.SetLayoutDirty();
  77. Assert.True(m_dirtyLayout, "Layout has not been dirtied");
  78. m_graphic.SetVerticesDirty();
  79. Assert.True(m_dirtyVert, "Vertices have not been dirtied");
  80. m_graphic.SetMaterialDirty();
  81. Assert.True(m_dirtyMaterial, "Material has not been dirtied");
  82. }
  83. private bool ContainsGraphic(IList<Graphic> array, int size, Graphic element)
  84. {
  85. for (int i = 0; i < size; ++i)
  86. {
  87. if (array[i] == element)
  88. return true;
  89. }
  90. return false;
  91. }
  92. private void RefreshGraphicByRaycast()
  93. {
  94. // force refresh by raycast
  95. List<RaycastResult> raycastResultCache = new List<RaycastResult>();
  96. PointerEventData data = new PointerEventData(EventSystem.current);
  97. var raycaster = m_canvas.GetComponent<GraphicRaycaster>();
  98. raycaster.Raycast(data, raycastResultCache);
  99. }
  100. private bool CheckGraphicAddedToGraphicRegistry()
  101. {
  102. var graphicList = GraphicRegistry.GetGraphicsForCanvas(m_canvas);
  103. var graphicListSize = graphicList.Count;
  104. return ContainsGraphic(graphicList, graphicListSize, m_graphic);
  105. }
  106. private bool CheckGraphicAddedToRaycastGraphicRegistry()
  107. {
  108. var graphicList = GraphicRegistry.GetRaycastableGraphicsForCanvas(m_canvas);
  109. var graphicListSize = graphicList.Count;
  110. return ContainsGraphic(graphicList, graphicListSize, m_graphic);
  111. }
  112. private void CheckGraphicRaycastDisableValidity()
  113. {
  114. RefreshGraphicByRaycast();
  115. Assert.False(CheckGraphicAddedToGraphicRegistry(), "Graphic should no longer be registered in m_CanvasGraphics");
  116. Assert.False(CheckGraphicAddedToRaycastGraphicRegistry(), "Graphic should no longer be registered in m_RaycastableGraphics");
  117. }
  118. [Test]
  119. public void OnEnableLeavesGraphicInExpectedState()
  120. {
  121. m_graphic.enabled = false;
  122. m_graphic.enabled = true; // on enable is called directly
  123. Assert.True(CheckGraphicAddedToGraphicRegistry(), "Graphic should be registered in m_CanvasGraphics");
  124. Assert.True(CheckGraphicAddedToRaycastGraphicRegistry(), "Graphic should be registered in m_RaycastableGraphics");
  125. Assert.AreEqual(Texture2D.whiteTexture, m_graphic.mainTexture, "mainTexture should be Texture2D.whiteTexture");
  126. Assert.NotNull(m_graphic.canvas);
  127. Assert.True(m_dirtyVert, "Vertices have not been dirtied");
  128. Assert.True(m_dirtyLayout, "Layout has not been dirtied");
  129. Assert.True(m_dirtyMaterial, "Material has not been dirtied");
  130. }
  131. [Test]
  132. public void OnEnableTwiceLeavesGraphicInExpectedState()
  133. {
  134. m_graphic.enabled = true;
  135. // force onEnable by reflection to call it second time
  136. m_graphic.InvokeOnEnable();
  137. m_graphic.enabled = false;
  138. CheckGraphicRaycastDisableValidity();
  139. }
  140. [Test]
  141. public void OnDisableLeavesGraphicInExpectedState()
  142. {
  143. m_graphic.enabled = true;
  144. m_graphic.enabled = false;
  145. CheckGraphicRaycastDisableValidity();
  146. }
  147. [Test]
  148. public void OnPopulateMeshWorksAsExpected()
  149. {
  150. m_graphic.rectTransform.anchoredPosition = new Vector2(100, 100);
  151. m_graphic.rectTransform.sizeDelta = new Vector2(150, 742);
  152. m_graphic.color = new Color(50, 100, 150, 200);
  153. VertexHelper vh = new VertexHelper();
  154. m_graphic.InvokeOnPopulateMesh(vh);
  155. GraphicTestHelper.TestOnPopulateMeshDefaultBehavior(m_graphic, vh);
  156. }
  157. [Test]
  158. public void OnDidApplyAnimationPropertiesSetsAllDirty()
  159. {
  160. m_graphic.enabled = true; // Usually set through SetEnabled, from Behavior
  161. m_graphic.InvokeOnDidApplyAnimationProperties();
  162. Assert.True(m_dirtyVert, "Vertices have not been dirtied");
  163. Assert.True(m_dirtyLayout, "Layout has not been dirtied");
  164. Assert.True(m_dirtyMaterial, "Material has not been dirtied");
  165. }
  166. [Test]
  167. public void MakingGraphicNonRaycastableRemovesGraphicFromProperLists()
  168. {
  169. Assert.True(CheckGraphicAddedToGraphicRegistry(), "Graphic should be registered in m_CanvasGraphics");
  170. Assert.True(CheckGraphicAddedToRaycastGraphicRegistry(), "Graphic should be registered in m_RaycastableGraphics");
  171. m_graphic.raycastTarget = false;
  172. Assert.True(CheckGraphicAddedToGraphicRegistry(), "Graphic should be registered in m_CanvasGraphics");
  173. Assert.False(CheckGraphicAddedToRaycastGraphicRegistry(), "Graphic should no longer be registered in m_RaycastableGraphics");
  174. }
  175. [Test]
  176. public void OnEnableLeavesNonRaycastGraphicInExpectedState()
  177. {
  178. m_graphic.enabled = false;
  179. m_graphic.raycastTarget = false;
  180. m_graphic.enabled = true; // on enable is called directly
  181. Assert.True(CheckGraphicAddedToGraphicRegistry(), "Graphic should be registered in m_CanvasGraphics");
  182. Assert.False(CheckGraphicAddedToRaycastGraphicRegistry(), "Graphic should no longer be registered in m_RaycastableGraphics");
  183. Assert.AreEqual(Texture2D.whiteTexture, m_graphic.mainTexture, "mainTexture should be Texture2D.whiteTexture");
  184. Assert.NotNull(m_graphic.canvas);
  185. Assert.True(m_dirtyVert, "Vertices have not been dirtied");
  186. Assert.True(m_dirtyLayout, "Layout has not been dirtied");
  187. Assert.True(m_dirtyMaterial, "Material has not been dirtied");
  188. }
  189. [Test]
  190. public void SettingRaycastTargetOnDisabledGraphicDoesntAddItRaycastList()
  191. {
  192. Assert.True(CheckGraphicAddedToGraphicRegistry(), "Graphic should be registered in m_CanvasGraphics");
  193. Assert.True(CheckGraphicAddedToRaycastGraphicRegistry(), "Graphic should be registered in m_RaycastableGraphics");
  194. m_graphic.raycastTarget = false;
  195. Assert.True(CheckGraphicAddedToGraphicRegistry(), "Graphic should be registered in m_CanvasGraphics");
  196. Assert.False(CheckGraphicAddedToRaycastGraphicRegistry(), "Graphic should no longer be registered in m_RaycastableGraphics");
  197. m_graphic.enabled = false;
  198. Assert.False(CheckGraphicAddedToGraphicRegistry(), "Graphic should NOT be registered in m_CanvasGraphics");
  199. Assert.False(CheckGraphicAddedToRaycastGraphicRegistry(), "Graphic should no longer be registered in m_RaycastableGraphics");
  200. m_graphic.raycastTarget = true;
  201. Assert.False(CheckGraphicAddedToGraphicRegistry(), "Graphic should NOT be registered in m_CanvasGraphics");
  202. Assert.False(CheckGraphicAddedToRaycastGraphicRegistry(), "Graphic should no longer be registered in m_RaycastableGraphics");
  203. }
  204. }
  205. }