GraphicTests.cs
6.7 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System.Collections.Generic;
using System.IO;
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.TestTools;
using UnityEngine.UI;
using UnityEngine.UI.Tests;
namespace Graphics
{
class GraphicTests : IPrebuildSetup
{
private GameObject m_PrefabRoot;
private ConcreteGraphic m_graphic;
private Canvas m_canvas;
const string kPrefabPath = "Assets/Resources/GraphicTestsPrefab.prefab";
bool m_dirtyVert;
bool m_dirtyLayout;
bool m_dirtyMaterial;
public void Setup()
{
#if UNITY_EDITOR
var rootGO = new GameObject("rootGo");
var canvasGO = new GameObject("CanvasRoot", typeof(Canvas), typeof(GraphicRaycaster));
canvasGO.transform.SetParent(rootGO.transform);
var graphicGO = new GameObject("Graphic", typeof(RectTransform), typeof(ConcreteGraphic));
graphicGO.transform.SetParent(canvasGO.transform);
var gameObject = new GameObject("EventSystem", typeof(EventSystem));
gameObject.transform.SetParent(rootGO.transform);
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("GraphicTestsPrefab")) as GameObject;
m_canvas = m_PrefabRoot.GetComponentInChildren<Canvas>();
m_graphic = m_PrefabRoot.GetComponentInChildren<ConcreteGraphic>();
m_graphic.RegisterDirtyVerticesCallback(() => m_dirtyVert = true);
m_graphic.RegisterDirtyLayoutCallback(() => m_dirtyLayout = true);
m_graphic.RegisterDirtyMaterialCallback(() => m_dirtyMaterial = true);
ResetDirtyFlags();
}
[TearDown]
public void TearDown()
{
m_graphic = null;
m_canvas = null;
GameObject.DestroyImmediate(m_PrefabRoot);
}
[OneTimeTearDown]
public void OneTimeTearDown()
{
#if UNITY_EDITOR
AssetDatabase.DeleteAsset(kPrefabPath);
#endif
}
private void ResetDirtyFlags()
{
m_dirtyVert = m_dirtyLayout = m_dirtyMaterial = false;
}
[Test]
public void SettingDirtyOnActiveGraphicCallsCallbacks()
{
m_graphic.enabled = false;
m_graphic.enabled = true;
m_graphic.SetAllDirty();
Assert.True(m_dirtyVert, "Vertices have not been dirtied");
Assert.True(m_dirtyLayout, "Layout has not been dirtied");
Assert.True(m_dirtyMaterial, "Material has not been dirtied");
ResetDirtyFlags();
m_graphic.SetLayoutDirty();
Assert.True(m_dirtyLayout, "Layout has not been dirtied");
m_graphic.SetVerticesDirty();
Assert.True(m_dirtyVert, "Vertices have not been dirtied");
m_graphic.SetMaterialDirty();
Assert.True(m_dirtyMaterial, "Material has not been dirtied");
}
private bool ContainsGraphic(IList<Graphic> array, int size, Graphic element)
{
for (int i = 0; i < size; ++i)
{
if (array[i] == element)
return true;
}
return false;
}
private void RefreshGraphicByRaycast()
{
// force refresh by raycast
List<RaycastResult> raycastResultCache = new List<RaycastResult>();
PointerEventData data = new PointerEventData(EventSystem.current);
var raycaster = m_canvas.GetComponent<GraphicRaycaster>();
raycaster.Raycast(data, raycastResultCache);
}
private bool CheckGraphicAddedToGraphicRaycaster()
{
// check that Graphic is removed from m_CanvasGraphics
var raycaster = m_canvas.GetComponent<GraphicRaycaster>();
var graphicList = GraphicRegistry.GetGraphicsForCanvas(m_canvas);
var graphicListSize = graphicList.Count;
return ContainsGraphic(graphicList, graphicListSize, m_graphic);
}
private void CheckGraphicRaycastDisableValidity()
{
RefreshGraphicByRaycast();
Assert.False(CheckGraphicAddedToGraphicRaycaster(), "Graphic should no longer be registered in m_CanvasGraphics");
}
[Test]
public void OnEnableLeavesGraphicInExpectedState()
{
m_graphic.enabled = false;
m_graphic.enabled = true; // on enable is called directly
Assert.True(CheckGraphicAddedToGraphicRaycaster(), "Graphic should be registered in m_CanvasGraphics");
Assert.AreEqual(Texture2D.whiteTexture, m_graphic.mainTexture, "mainTexture should be Texture2D.whiteTexture");
Assert.NotNull(m_graphic.canvas);
Assert.True(m_dirtyVert, "Vertices have not been dirtied");
Assert.True(m_dirtyLayout, "Layout has not been dirtied");
Assert.True(m_dirtyMaterial, "Material has not been dirtied");
}
[Test]
public void OnEnableTwiceLeavesGraphicInExpectedState()
{
m_graphic.enabled = true;
// force onEnable by reflection to call it second time
m_graphic.InvokeOnEnable();
m_graphic.enabled = false;
CheckGraphicRaycastDisableValidity();
}
[Test]
public void OnDisableLeavesGraphicInExpectedState()
{
m_graphic.enabled = true;
m_graphic.enabled = false;
CheckGraphicRaycastDisableValidity();
}
[Test]
public void OnPopulateMeshWorksAsExpected()
{
m_graphic.rectTransform.anchoredPosition = new Vector2(100, 100);
m_graphic.rectTransform.sizeDelta = new Vector2(150, 742);
m_graphic.color = new Color(50, 100, 150, 200);
VertexHelper vh = new VertexHelper();
m_graphic.InvokeOnPopulateMesh(vh);
GraphicTestHelper.TestOnPopulateMeshDefaultBehavior(m_graphic, vh);
}
[Test]
public void OnDidApplyAnimationPropertiesSetsAllDirty()
{
m_graphic.enabled = true; // Usually set through SetEnabled, from Behavior
m_graphic.InvokeOnDidApplyAnimationProperties();
Assert.True(m_dirtyVert, "Vertices have not been dirtied");
Assert.True(m_dirtyLayout, "Layout has not been dirtied");
Assert.True(m_dirtyMaterial, "Material has not been dirtied");
}
}
}