HorizonalLayoutGroupTests.cs
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System.IO;
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.TestTools;
namespace LayoutTests
{
public class HorizontalLayoutGroupTests : IPrebuildSetup
{
GameObject m_PrefabRoot;
const string kPrefabPath = "Assets/Resources/HorizontalLayoutGroupPrefab.prefab";
public void Setup()
{
#if UNITY_EDITOR
var rootGO = new GameObject("rootGo");
var canvasGO = new GameObject("Canvas", typeof(Canvas));
canvasGO.transform.SetParent(rootGO.transform);
var groupGO = new GameObject("Group", typeof(RectTransform), typeof(HorizontalLayoutGroup));
groupGO.transform.SetParent(canvasGO.transform);
var horizontalLayoutGroup = groupGO.GetComponent<HorizontalLayoutGroup>();
horizontalLayoutGroup.padding = new RectOffset(2, 4, 3, 5);
horizontalLayoutGroup.spacing = 1;
horizontalLayoutGroup.childForceExpandWidth = false;
horizontalLayoutGroup.childForceExpandHeight = false;
horizontalLayoutGroup.childControlWidth = true;
horizontalLayoutGroup.childControlHeight = true;
var element1GO = new GameObject("Element1", typeof(RectTransform), typeof(LayoutElement));
element1GO.transform.SetParent(groupGO.transform);
var layoutElement1 = element1GO.GetComponent<LayoutElement>();
layoutElement1.minWidth = 5;
layoutElement1.minHeight = 10;
layoutElement1.preferredWidth = 100;
layoutElement1.preferredHeight = 50;
layoutElement1.flexibleWidth = 0;
layoutElement1.flexibleHeight = 0;
var element2GO = new GameObject("Element2", typeof(RectTransform), typeof(LayoutElement));
element2GO.transform.SetParent(groupGO.transform);
var layoutElement2 = element2GO.GetComponent<LayoutElement>();
layoutElement2.minWidth = 10;
layoutElement2.minHeight = 5;
layoutElement2.preferredWidth = -1;
layoutElement2.preferredHeight = -1;
layoutElement2.flexibleWidth = 0;
layoutElement2.flexibleHeight = 0;
var element3GO = new GameObject("Element3", typeof(RectTransform), typeof(LayoutElement));
element3GO.transform.SetParent(groupGO.transform);
var layoutElement3 = element3GO.GetComponent<LayoutElement>();
layoutElement3.minWidth = 25;
layoutElement3.minHeight = 15;
layoutElement3.preferredWidth = 200;
layoutElement3.preferredHeight = 80;
layoutElement3.flexibleWidth = 1;
layoutElement3.flexibleHeight = 1;
if (!Directory.Exists("Assets/Resources/"))
Directory.CreateDirectory("Assets/Resources/");
PrefabUtility.SaveAsPrefabAsset(rootGO, kPrefabPath);
GameObject.DestroyImmediate(rootGO);
#endif
}
[SetUp]
public void TestSetup()
{
m_PrefabRoot = Object.Instantiate(Resources.Load("HorizontalLayoutGroupPrefab")) as GameObject;
}
[TearDown]
public void TearDown()
{
GameObject.DestroyImmediate(m_PrefabRoot);
}
[OneTimeTearDown]
public void OneTimeTearDown()
{
#if UNITY_EDITOR
AssetDatabase.DeleteAsset(kPrefabPath);
#endif
}
[Test]
public void TestCalculateLayoutInputHorizontal()
{
HorizontalLayoutGroup layoutGroup = m_PrefabRoot.GetComponentInChildren<HorizontalLayoutGroup>();
layoutGroup.CalculateLayoutInputHorizontal();
layoutGroup.SetLayoutHorizontal();
layoutGroup.CalculateLayoutInputVertical();
layoutGroup.SetLayoutVertical();
Assert.AreEqual(48, layoutGroup.minWidth);
Assert.AreEqual(318, layoutGroup.preferredWidth);
Assert.AreEqual(1, layoutGroup.flexibleWidth);
}
[Test]
public void TestCalculateLayoutInputVertical()
{
HorizontalLayoutGroup layoutGroup = m_PrefabRoot.GetComponentInChildren<HorizontalLayoutGroup>();
layoutGroup.CalculateLayoutInputHorizontal();
layoutGroup.SetLayoutHorizontal();
layoutGroup.CalculateLayoutInputVertical();
layoutGroup.SetLayoutVertical();
Assert.AreEqual(23, layoutGroup.minHeight);
Assert.AreEqual(88, layoutGroup.preferredHeight);
Assert.AreEqual(1, layoutGroup.flexibleHeight);
Assert.AreEqual(1, layoutGroup.flexibleHeight);
}
}
}