No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ContentSizeFitterTests.cs 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using NUnit.Framework;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using UnityEngine.TestTools;
  7. using UnityEngine.UI;
  8. using UnityEngine.UI.Tests;
  9. namespace LayoutTests
  10. {
  11. class ContentSizeFitterTests : IPrebuildSetup
  12. {
  13. const string kPrefabPath = "Assets/Resources/ContentSizeFitterTests.prefab";
  14. private GameObject m_PrefabRoot;
  15. private ContentSizeFitter m_ContentSizeFitter;
  16. private RectTransform m_RectTransform;
  17. public void Setup()
  18. {
  19. #if UNITY_EDITOR
  20. var rootGO = new GameObject("rootGo");
  21. GameObject canvasGO = new GameObject("Canvas", typeof(RectTransform), typeof(Canvas));
  22. canvasGO.transform.SetParent(rootGO.transform);
  23. var canvas = canvasGO.GetComponent<Canvas>();
  24. canvas.referencePixelsPerUnit = 100;
  25. var testGO = new GameObject("TestObject", typeof(RectTransform), typeof(ContentSizeFitter));
  26. testGO.transform.SetParent(canvasGO.transform);
  27. if (!Directory.Exists("Assets/Resources/"))
  28. Directory.CreateDirectory("Assets/Resources/");
  29. PrefabUtility.SaveAsPrefabAsset(rootGO, kPrefabPath);
  30. GameObject.DestroyImmediate(rootGO);
  31. #endif
  32. }
  33. [SetUp]
  34. public void TestSetup()
  35. {
  36. m_PrefabRoot = Object.Instantiate(Resources.Load("ContentSizeFitterTests")) as GameObject;
  37. m_ContentSizeFitter = m_PrefabRoot.GetComponentInChildren<ContentSizeFitter>();
  38. m_ContentSizeFitter.enabled = true;
  39. m_RectTransform = m_ContentSizeFitter.GetComponent<RectTransform>();
  40. m_RectTransform.sizeDelta = new Vector2(50, 50);
  41. GameObject testObject = m_ContentSizeFitter.gameObject;
  42. // set up components
  43. var componentA = testObject.AddComponent<LayoutElement>();
  44. componentA.minWidth = 5;
  45. componentA.minHeight = 10;
  46. componentA.preferredWidth = 100;
  47. componentA.preferredHeight = 105;
  48. componentA.flexibleWidth = 0;
  49. componentA.flexibleHeight = 0;
  50. componentA.enabled = true;
  51. var componentB = testObject.AddComponent<LayoutElement>();
  52. componentB.minWidth = 15;
  53. componentB.minHeight = 20;
  54. componentB.preferredWidth = 110;
  55. componentB.preferredHeight = 115;
  56. componentB.flexibleWidth = 0;
  57. componentB.flexibleHeight = 0;
  58. componentB.enabled = true;
  59. var componentC = testObject.AddComponent<LayoutElement>();
  60. componentC.minWidth = 25;
  61. componentC.minHeight = 30;
  62. componentC.preferredWidth = 120;
  63. componentC.preferredHeight = 125;
  64. componentC.flexibleWidth = 0;
  65. componentC.flexibleHeight = 0;
  66. componentC.enabled = true;
  67. }
  68. [TearDown]
  69. public void TearDown()
  70. {
  71. GameObject.DestroyImmediate(m_PrefabRoot);
  72. m_PrefabRoot = null;
  73. m_ContentSizeFitter = null;
  74. m_RectTransform = null;
  75. }
  76. [OneTimeTearDown]
  77. public void OneTimeTearDown()
  78. {
  79. #if UNITY_EDITOR
  80. AssetDatabase.DeleteAsset(kPrefabPath);
  81. #endif
  82. }
  83. [Test]
  84. public void TestFitModeUnconstrained()
  85. {
  86. m_ContentSizeFitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
  87. m_ContentSizeFitter.verticalFit = ContentSizeFitter.FitMode.Unconstrained;
  88. m_ContentSizeFitter.SetLayoutHorizontal();
  89. m_ContentSizeFitter.SetLayoutVertical();
  90. Assert.AreEqual(50, m_RectTransform.rect.width);
  91. Assert.AreEqual(50, m_RectTransform.rect.height);
  92. }
  93. [Test]
  94. public void TestFitModeMinSize()
  95. {
  96. m_ContentSizeFitter.horizontalFit = ContentSizeFitter.FitMode.MinSize;
  97. m_ContentSizeFitter.verticalFit = ContentSizeFitter.FitMode.MinSize;
  98. m_ContentSizeFitter.SetLayoutHorizontal();
  99. m_ContentSizeFitter.SetLayoutVertical();
  100. Assert.AreEqual(25, m_RectTransform.rect.width);
  101. Assert.AreEqual(30, m_RectTransform.rect.height);
  102. }
  103. [Test]
  104. public void TestFitModePreferredSize()
  105. {
  106. m_ContentSizeFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
  107. m_ContentSizeFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
  108. m_ContentSizeFitter.SetLayoutHorizontal();
  109. m_ContentSizeFitter.SetLayoutVertical();
  110. Assert.AreEqual(120, m_RectTransform.rect.width);
  111. Assert.AreEqual(125, m_RectTransform.rect.height);
  112. }
  113. }
  114. }