Ingen beskrivning
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.

GridLayoutGroupTests.cs 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. using NUnit.Framework;
  2. using UnityEngine.EventSystems;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.TestTools;
  6. using System.IO;
  7. using UnityEditor;
  8. class GridLayoutGroupTests : IPrebuildSetup
  9. {
  10. const string kPrefabPath = "Assets/Resources/GridLayoutGroupTests.prefab";
  11. private GameObject m_PrefabRoot;
  12. private GridLayoutGroup m_LayoutGroup;
  13. public void Setup()
  14. {
  15. #if UNITY_EDITOR
  16. var rootGO = new GameObject("rootGo");
  17. var canvasGO = new GameObject("Canvas");
  18. canvasGO.transform.SetParent(rootGO.transform);
  19. Canvas canvas = canvasGO.AddComponent<Canvas>();
  20. canvas.referencePixelsPerUnit = 100;
  21. var groupGO = new GameObject("Group", typeof(RectTransform), typeof(GridLayoutGroup));
  22. groupGO.transform.SetParent(canvas.transform);
  23. var rectTransform = groupGO.GetComponent<RectTransform>();
  24. rectTransform.sizeDelta = new Vector2(400, 500);
  25. rectTransform.anchoredPosition = new Vector2(0, 0);
  26. rectTransform.pivot = new Vector2(0, 0);
  27. var layoutGroup = groupGO.GetComponent<GridLayoutGroup>();
  28. layoutGroup.spacing = new Vector2(10, 0);
  29. layoutGroup.startCorner = GridLayoutGroup.Corner.UpperLeft;
  30. layoutGroup.cellSize = new Vector2(90, 50);
  31. layoutGroup.constraint = GridLayoutGroup.Constraint.Flexible;
  32. layoutGroup.startAxis = GridLayoutGroup.Axis.Horizontal;
  33. layoutGroup.childAlignment = TextAnchor.UpperLeft;
  34. layoutGroup.enabled = false;
  35. layoutGroup.enabled = true;
  36. var el1 = new GameObject("Element1");
  37. el1.transform.SetParent(rectTransform);
  38. var element1 = el1.AddComponent<LayoutElement>();
  39. (el1.transform as RectTransform).pivot = Vector2.zero;
  40. element1.minWidth = 5;
  41. element1.minHeight = 10;
  42. element1.preferredWidth = 100;
  43. element1.preferredHeight = 50;
  44. element1.flexibleWidth = 0;
  45. element1.flexibleHeight = 0;
  46. element1.enabled = true;
  47. var el2 = new GameObject("Element2");
  48. el2.transform.SetParent(rectTransform);
  49. var element2 = el2.AddComponent<LayoutElement>();
  50. (el2.transform as RectTransform).pivot = Vector2.zero;
  51. element2.minWidth = 10;
  52. element2.minHeight = 5;
  53. element2.preferredWidth = -1;
  54. element2.preferredHeight = -1;
  55. element2.flexibleWidth = 0;
  56. element2.flexibleHeight = 0;
  57. element2.enabled = true;
  58. var el3 = new GameObject("Element3");
  59. el3.transform.SetParent(rectTransform);
  60. var element3 = el3.AddComponent<LayoutElement>();
  61. (el3.transform as RectTransform).pivot = Vector2.zero;
  62. element3.minWidth = 60;
  63. element3.minHeight = 25;
  64. element3.preferredWidth = 120;
  65. element3.preferredHeight = 40;
  66. element3.flexibleWidth = 1;
  67. element3.flexibleHeight = 1;
  68. element3.enabled = true;
  69. var el4 = new GameObject("Element4");
  70. el4.transform.SetParent(rectTransform);
  71. var element4 = el4.AddComponent<LayoutElement>();
  72. (el4.transform as RectTransform).pivot = Vector2.zero;
  73. element4.minWidth = 60;
  74. element4.minHeight = 25;
  75. element4.preferredWidth = 120;
  76. element4.preferredHeight = 40;
  77. element4.flexibleWidth = 1;
  78. element4.flexibleHeight = 1;
  79. element4.enabled = true;
  80. var el5 = new GameObject("Element5");
  81. el5.transform.SetParent(rectTransform);
  82. var element5 = el5.AddComponent<LayoutElement>();
  83. (el5.transform as RectTransform).pivot = Vector2.zero;
  84. element5.minWidth = 60;
  85. element5.minHeight = 25;
  86. element5.preferredWidth = 120;
  87. element5.preferredHeight = 40;
  88. element5.flexibleWidth = 1;
  89. element5.flexibleHeight = 1;
  90. element5.enabled = true;
  91. var el6 = new GameObject("Element6");
  92. el6.transform.SetParent(rectTransform);
  93. var element6 = el6.AddComponent<LayoutElement>();
  94. (el6.transform as RectTransform).pivot = Vector2.zero;
  95. element6.minWidth = 60;
  96. element6.minHeight = 25;
  97. element6.preferredWidth = 120;
  98. element6.preferredHeight = 40;
  99. element6.flexibleWidth = 1;
  100. element6.flexibleHeight = 1;
  101. element6.enabled = true;
  102. var el7 = new GameObject("Element7");
  103. el7.transform.SetParent(rectTransform);
  104. var element7 = el7.AddComponent<LayoutElement>();
  105. (el7.transform as RectTransform).pivot = Vector2.zero;
  106. element7.minWidth = 60;
  107. element7.minHeight = 25;
  108. element7.preferredWidth = 120;
  109. element7.preferredHeight = 40;
  110. element7.flexibleWidth = 1;
  111. element7.flexibleHeight = 1;
  112. element7.enabled = true;
  113. var el8 = new GameObject("Element8");
  114. el8.transform.SetParent(rectTransform);
  115. var element8 = el8.AddComponent<LayoutElement>();
  116. (el8.transform as RectTransform).pivot = Vector2.zero;
  117. element8.minWidth = 60;
  118. element8.minHeight = 25;
  119. element8.preferredWidth = 120;
  120. element8.preferredHeight = 40;
  121. element8.flexibleWidth = 1;
  122. element8.flexibleHeight = 1;
  123. element8.enabled = true;
  124. var el9 = new GameObject("Element9");
  125. el9.transform.SetParent(rectTransform);
  126. var element9 = el9.AddComponent<LayoutElement>();
  127. (el9.transform as RectTransform).pivot = Vector2.zero;
  128. element9.minWidth = 500;
  129. element9.minHeight = 300;
  130. element9.preferredWidth = 1000;
  131. element9.preferredHeight = 600;
  132. element9.flexibleWidth = 1;
  133. element9.flexibleHeight = 1;
  134. element9.enabled = true;
  135. element9.ignoreLayout = true;
  136. if (!Directory.Exists("Assets/Resources/"))
  137. Directory.CreateDirectory("Assets/Resources/");
  138. PrefabUtility.SaveAsPrefabAsset(rootGO, kPrefabPath);
  139. GameObject.DestroyImmediate(rootGO);
  140. #endif
  141. }
  142. [SetUp]
  143. public void TestSetup()
  144. {
  145. m_PrefabRoot = GameObject.Instantiate(Resources.Load("GridLayoutGroupTests")) as GameObject;
  146. m_LayoutGroup = m_PrefabRoot.GetComponentInChildren<GridLayoutGroup>();
  147. }
  148. [TearDown]
  149. public void TearDown()
  150. {
  151. GameObject.DestroyImmediate(m_PrefabRoot);
  152. m_LayoutGroup = null;
  153. }
  154. [OneTimeTearDown]
  155. public void OneTimeTearDown()
  156. {
  157. #if UNITY_EDITOR
  158. AssetDatabase.DeleteAsset(kPrefabPath);
  159. #endif
  160. }
  161. [Test]
  162. public void TestFlexibleCalculateLayout()
  163. {
  164. m_LayoutGroup.constraint = GridLayoutGroup.Constraint.Flexible;
  165. Assert.AreEqual(GridLayoutGroup.Constraint.Flexible, m_LayoutGroup.constraint);
  166. LayoutRebuilder.ForceRebuildLayoutImmediate(m_LayoutGroup.GetComponent<RectTransform>());
  167. Assert.AreEqual(90, m_LayoutGroup.minWidth, "Expected layout group min width to match but it did not");
  168. Assert.AreEqual(100, m_LayoutGroup.minHeight, "Expected layout group min height to match but it did not");
  169. Assert.AreEqual(290, m_LayoutGroup.preferredWidth, "Expected layout group preferred width to match but it did not");
  170. Assert.AreEqual(100, m_LayoutGroup.preferredHeight, "Expected layout group preferred height to match but it did not");
  171. Assert.AreEqual(-1, m_LayoutGroup.flexibleWidth, "Expected layout group flexiblle width to match but it did not");
  172. Assert.AreEqual(-1, m_LayoutGroup.flexibleHeight, "Expected layout group flexiblle height to match but it did not");
  173. Vector2[] expectedPositions =
  174. {
  175. new Vector2(0, -50),
  176. new Vector2(100, -50),
  177. new Vector2(200, -50),
  178. new Vector2(300, -50),
  179. new Vector2(0, -100),
  180. new Vector2(100, -100),
  181. new Vector2(200, -100),
  182. new Vector2(300, -100),
  183. };
  184. Vector2 expectedSize = new Vector2(90, 50);
  185. for (int i = 0; i < expectedPositions.Length; ++i)
  186. {
  187. var element = m_LayoutGroup.transform.Find("Element" + (i + 1));
  188. var rectTransform = element.GetComponent<RectTransform>();
  189. Assert.AreEqual(expectedPositions[i], rectTransform.anchoredPosition, $"Expected Element { i + 1 } position to match but it did not");
  190. Assert.AreEqual(expectedSize, rectTransform.sizeDelta, $"Expected Element { i + 1 } size to match but it did not");
  191. }
  192. }
  193. [Test]
  194. public void TestHorizontallyContrainedCalculateLayoutHorizontal()
  195. {
  196. m_LayoutGroup.constraint = GridLayoutGroup.Constraint.FixedColumnCount;
  197. m_LayoutGroup.constraintCount = 2;
  198. Assert.AreEqual(GridLayoutGroup.Constraint.FixedColumnCount, m_LayoutGroup.constraint, "Expected layout group constraint mode to match but it did not");
  199. Assert.AreEqual(2, m_LayoutGroup.constraintCount, "Expected layout group constraint count mode to match but it did not");
  200. LayoutRebuilder.ForceRebuildLayoutImmediate(m_LayoutGroup.GetComponent<RectTransform>());
  201. Assert.AreEqual(190, m_LayoutGroup.minWidth, "Expected layout group min width to match but it did not");
  202. Assert.AreEqual(200, m_LayoutGroup.minHeight, "Expected layout group min height to match but it did not");
  203. Assert.AreEqual(190, m_LayoutGroup.preferredWidth, "Expected layout group preferred width to match but it did not");
  204. Assert.AreEqual(200, m_LayoutGroup.preferredHeight, "Expected layout group preferred height to match but it did not");
  205. Assert.AreEqual(-1, m_LayoutGroup.flexibleWidth, "Expected layout group flexiblle width to match but it did not");
  206. Assert.AreEqual(-1, m_LayoutGroup.flexibleHeight, "Expected layout group flexiblle height to match but it did not");
  207. Vector2[] expectedPositions =
  208. {
  209. new Vector2(0, -50),
  210. new Vector2(100, -50),
  211. new Vector2(0, -100),
  212. new Vector2(100, -100),
  213. new Vector2(0, -150),
  214. new Vector2(100, -150),
  215. new Vector2(0, -200),
  216. new Vector2(100, -200),
  217. };
  218. Vector2 expectedSize = new Vector2(90, 50);
  219. for (int i = 0; i < expectedPositions.Length; ++i)
  220. {
  221. var element = m_LayoutGroup.transform.Find("Element" + (i + 1));
  222. var rectTransform = element.GetComponent<RectTransform>();
  223. Assert.AreEqual(expectedPositions[i], rectTransform.anchoredPosition, $"Expected Element { i + 1 } position to match but it did not");
  224. Assert.AreEqual(expectedSize, rectTransform.sizeDelta, $"Expected Element { i + 1 } size to match but it did not");
  225. }
  226. }
  227. [Test]
  228. public void TestVerticallyContrainedCalculateLayoutHorizontal()
  229. {
  230. m_LayoutGroup.constraint = GridLayoutGroup.Constraint.FixedRowCount;
  231. m_LayoutGroup.constraintCount = 2;
  232. Assert.AreEqual(GridLayoutGroup.Constraint.FixedRowCount, m_LayoutGroup.constraint, "Expected layout group constraint mode to match but it did not");
  233. Assert.AreEqual(2, m_LayoutGroup.constraintCount, "Expected layout group constraint count mode to match but it did not");
  234. LayoutRebuilder.ForceRebuildLayoutImmediate(m_LayoutGroup.GetComponent<RectTransform>());
  235. Assert.AreEqual(390, m_LayoutGroup.minWidth, "Expected layout group min width to match but it did not");
  236. Assert.AreEqual(100, m_LayoutGroup.minHeight, "Expected layout group min height to match but it did not");
  237. Assert.AreEqual(390, m_LayoutGroup.preferredWidth, "Expected layout group preferred width to match but it did not");
  238. Assert.AreEqual(100, m_LayoutGroup.preferredHeight, "Expected layout group preferred height to match but it did not");
  239. Assert.AreEqual(-1, m_LayoutGroup.flexibleWidth, "Expected layout group flexiblle width to match but it did not");
  240. Assert.AreEqual(-1, m_LayoutGroup.flexibleHeight, "Expected layout group flexiblle height to match but it did not");
  241. Vector2[] expectedPositions =
  242. {
  243. new Vector2(0, -50),
  244. new Vector2(100, -50),
  245. new Vector2(200, -50),
  246. new Vector2(300, -50),
  247. new Vector2(0, -100),
  248. new Vector2(100, -100),
  249. new Vector2(200, -100),
  250. new Vector2(300, -100),
  251. };
  252. Vector2 expectedSize = new Vector2(90, 50);
  253. for (int i = 0; i < expectedPositions.Length; ++i)
  254. {
  255. var element = m_LayoutGroup.transform.Find("Element" + (i + 1));
  256. var rectTransform = element.GetComponent<RectTransform>();
  257. Assert.AreEqual(expectedPositions[i], rectTransform.anchoredPosition, $"Expected Element { i + 1 } position to match but it did not");
  258. Assert.AreEqual(expectedSize, rectTransform.sizeDelta, $"Expected Element { i + 1 } size to match but it did not");
  259. }
  260. }
  261. [Test]
  262. public void TestHorizontallyContrainedCalculateLayoutVertical()
  263. {
  264. m_LayoutGroup.constraint = GridLayoutGroup.Constraint.FixedColumnCount;
  265. m_LayoutGroup.constraintCount = 2;
  266. m_LayoutGroup.startAxis = GridLayoutGroup.Axis.Vertical;
  267. Assert.AreEqual(GridLayoutGroup.Constraint.FixedColumnCount, m_LayoutGroup.constraint, "Expected layout group constraint mode to match but it did not");
  268. Assert.AreEqual(2, m_LayoutGroup.constraintCount, "Expected layout group constraint count mode to match but it did not");
  269. LayoutRebuilder.ForceRebuildLayoutImmediate(m_LayoutGroup.GetComponent<RectTransform>());
  270. Assert.AreEqual(190, m_LayoutGroup.minWidth, "Expected layout group min width to match but it did not");
  271. Assert.AreEqual(200, m_LayoutGroup.minHeight, "Expected layout group min height to match but it did not");
  272. Assert.AreEqual(190, m_LayoutGroup.preferredWidth, "Expected layout group preferred width to match but it did not");
  273. Assert.AreEqual(200, m_LayoutGroup.preferredHeight, "Expected layout group preferred height to match but it did not");
  274. Assert.AreEqual(-1, m_LayoutGroup.flexibleWidth, "Expected layout group flexiblle width to match but it did not");
  275. Assert.AreEqual(-1, m_LayoutGroup.flexibleHeight, "Expected layout group flexiblle height to match but it did not");
  276. Vector2[] expectedPositions =
  277. {
  278. new Vector2(0, -50),
  279. new Vector2(0, -100),
  280. new Vector2(0, -150),
  281. new Vector2(0, -200),
  282. new Vector2(100, -50),
  283. new Vector2(100, -100),
  284. new Vector2(100, -150),
  285. new Vector2(100, -200),
  286. };
  287. Vector2 expectedSize = new Vector2(90, 50);
  288. for (int i = 0; i < expectedPositions.Length; ++i)
  289. {
  290. var element = m_LayoutGroup.transform.Find("Element" + (i + 1));
  291. var rectTransform = element.GetComponent<RectTransform>();
  292. Assert.AreEqual(expectedPositions[i], rectTransform.anchoredPosition, $"Expected Element { i + 1 } position to match but it did not");
  293. Assert.AreEqual(expectedSize, rectTransform.sizeDelta, $"Expected Element { i + 1 } size to match but it did not");
  294. }
  295. }
  296. [Test]
  297. public void TestVerticallyContrainedCalculateLayoutVertical()
  298. {
  299. m_LayoutGroup.constraint = GridLayoutGroup.Constraint.FixedRowCount;
  300. m_LayoutGroup.constraintCount = 2;
  301. m_LayoutGroup.startAxis = GridLayoutGroup.Axis.Vertical;
  302. m_LayoutGroup.startCorner = GridLayoutGroup.Corner.LowerRight;
  303. Assert.AreEqual(GridLayoutGroup.Constraint.FixedRowCount, m_LayoutGroup.constraint, "Expected layout group constraint mode to match but it did not");
  304. Assert.AreEqual(2, m_LayoutGroup.constraintCount, "Expected layout group constraint count mode to match but it did not");
  305. m_LayoutGroup.CalculateLayoutInputHorizontal();
  306. m_LayoutGroup.SetLayoutHorizontal();
  307. m_LayoutGroup.CalculateLayoutInputVertical();
  308. m_LayoutGroup.SetLayoutVertical();
  309. Assert.AreEqual(390, m_LayoutGroup.minWidth, "Expected layout group min width to match but it did not");
  310. Assert.AreEqual(100, m_LayoutGroup.minHeight, "Expected layout group min height to match but it did not");
  311. Assert.AreEqual(390, m_LayoutGroup.preferredWidth, "Expected layout group preferred width to match but it did not");
  312. Assert.AreEqual(100, m_LayoutGroup.preferredHeight, "Expected layout group preferred height to match but it did not");
  313. Assert.AreEqual(-1, m_LayoutGroup.flexibleWidth, "Expected layout group flexiblle width to match but it did not");
  314. Assert.AreEqual(-1, m_LayoutGroup.flexibleHeight, "Expected layout group flexiblle height to match but it did not");
  315. Vector2[] expectedPositions =
  316. {
  317. new Vector2(300, -100),
  318. new Vector2(300, -50),
  319. new Vector2(200, -100),
  320. new Vector2(200, -50),
  321. new Vector2(100, -100),
  322. new Vector2(100, -50),
  323. new Vector2(0, -100),
  324. new Vector2(0, -50),
  325. };
  326. Vector2 expectedSize = new Vector2(90, 50);
  327. for (int i = 0; i < expectedPositions.Length; ++i)
  328. {
  329. var element = m_LayoutGroup.transform.Find("Element" + (i + 1));
  330. var rectTransform = element.GetComponent<RectTransform>();
  331. Assert.AreEqual(expectedPositions[i], rectTransform.anchoredPosition, $"Expected Element { i + 1 } position to match but it did not");
  332. Assert.AreEqual(expectedSize, rectTransform.sizeDelta, $"Expected Element { i + 1 } size to match but it did not");
  333. }
  334. }
  335. [Test]
  336. public void TestHorizontallyContrainedCalculateLayoutHorizontal_WithChildrenToMove()
  337. {
  338. m_LayoutGroup.constraint = GridLayoutGroup.Constraint.FixedRowCount;
  339. m_LayoutGroup.constraintCount = 5;
  340. m_LayoutGroup.startAxis = GridLayoutGroup.Axis.Horizontal;
  341. m_LayoutGroup.startCorner = GridLayoutGroup.Corner.UpperLeft;
  342. Assert.AreEqual(GridLayoutGroup.Constraint.FixedRowCount, m_LayoutGroup.constraint, "Expected layout group constraint mode to match but it did not");
  343. Assert.AreEqual(5, m_LayoutGroup.constraintCount, "Expected layout group constraint count mode to match but it did not");
  344. m_LayoutGroup.CalculateLayoutInputHorizontal();
  345. m_LayoutGroup.SetLayoutHorizontal();
  346. m_LayoutGroup.CalculateLayoutInputVertical();
  347. m_LayoutGroup.SetLayoutVertical();
  348. Assert.AreEqual(190, m_LayoutGroup.minWidth, "Expected layout group min width to match but it did not");
  349. Assert.AreEqual(250, m_LayoutGroup.minHeight, "Expected layout group min height to match but it did not");
  350. Assert.AreEqual(190, m_LayoutGroup.preferredWidth, "Expected layout group preferred width to match but it did not");
  351. Assert.AreEqual(250, m_LayoutGroup.preferredHeight, "Expected layout group preferred height to match but it did not");
  352. Assert.AreEqual(-1, m_LayoutGroup.flexibleWidth, "Expected layout group flexiblle width to match but it did not");
  353. Assert.AreEqual(-1, m_LayoutGroup.flexibleHeight, "Expected layout group flexiblle height to match but it did not");
  354. Vector2[] expectedPositions =
  355. {
  356. new Vector2(0, -50),
  357. new Vector2(100, -50),
  358. new Vector2(0, -100),
  359. new Vector2(100, -100),
  360. new Vector2(0, -150),
  361. new Vector2(100, -150),
  362. new Vector2(0, -200),
  363. new Vector2(0, -250)
  364. };
  365. Vector2 expectedSize = new Vector2(90, 50);
  366. for (int i = 0; i < expectedPositions.Length; ++i)
  367. {
  368. var element = m_LayoutGroup.transform.Find("Element" + (i + 1));
  369. var rectTransform = element.GetComponent<RectTransform>();
  370. Assert.AreEqual(expectedPositions[i], rectTransform.anchoredPosition, $"Expected Element { i + 1 } position to match but it did not");
  371. Assert.AreEqual(expectedSize, rectTransform.sizeDelta, $"Expected Element { i + 1 } size to match but it did not");
  372. }
  373. }
  374. [Test]
  375. public void TestVerticallyContrainedCalculateLayoutVertical_WithChildrenToMove()
  376. {
  377. m_LayoutGroup.constraint = GridLayoutGroup.Constraint.FixedColumnCount;
  378. m_LayoutGroup.constraintCount = 5;
  379. m_LayoutGroup.startAxis = GridLayoutGroup.Axis.Vertical;
  380. m_LayoutGroup.startCorner = GridLayoutGroup.Corner.UpperLeft;
  381. Assert.AreEqual(GridLayoutGroup.Constraint.FixedColumnCount, m_LayoutGroup.constraint, "Expected layout group constraint mode to match but it did not");
  382. Assert.AreEqual(5, m_LayoutGroup.constraintCount, "Expected layout group constraint count mode to match but it did not");
  383. m_LayoutGroup.CalculateLayoutInputHorizontal();
  384. m_LayoutGroup.SetLayoutHorizontal();
  385. m_LayoutGroup.CalculateLayoutInputVertical();
  386. m_LayoutGroup.SetLayoutVertical();
  387. Assert.AreEqual(490, m_LayoutGroup.minWidth, "Expected layout group min width to match but it did not");
  388. Assert.AreEqual(100, m_LayoutGroup.minHeight, "Expected layout group min height to match but it did not");
  389. Assert.AreEqual(490, m_LayoutGroup.preferredWidth, "Expected layout group preferred width to match but it did not");
  390. Assert.AreEqual(100, m_LayoutGroup.preferredHeight, "Expected layout group preferred height to match but it did not");
  391. Assert.AreEqual(-1, m_LayoutGroup.flexibleWidth, "Expected layout group flexiblle width to match but it did not");
  392. Assert.AreEqual(-1, m_LayoutGroup.flexibleHeight, "Expected layout group flexiblle height to match but it did not");
  393. Vector2[] expectedPositions =
  394. {
  395. new Vector2(0, -50),
  396. new Vector2(0, -100),
  397. new Vector2(100, -50),
  398. new Vector2(100, -100),
  399. new Vector2(200, -50),
  400. new Vector2(200, -100),
  401. new Vector2(300, -50),
  402. new Vector2(400, -50)
  403. };
  404. Vector2 expectedSize = new Vector2(90, 50);
  405. for (int i = 0; i < expectedPositions.Length; ++i)
  406. {
  407. var element = m_LayoutGroup.transform.Find("Element" + (i + 1));
  408. var rectTransform = element.GetComponent<RectTransform>();
  409. Assert.AreEqual(expectedPositions[i], rectTransform.anchoredPosition, $"Expected Element { i + 1 } position to match but it did not");
  410. Assert.AreEqual(expectedSize, rectTransform.sizeDelta, $"Expected Element { i + 1 } size to match but it did not");
  411. }
  412. }
  413. }