ScrollRectTests.cs
18.1 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
using System;
using System.Collections;
using System.Reflection;
using System.IO;
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.TestTools;
using System.Runtime.CompilerServices;
public class ScrollRectTests : IPrebuildSetup
{
const int ScrollSensitivity = 3;
GameObject m_PrefabRoot;
const string kPrefabPath = "Assets/Resources/ScrollRectPrefab.prefab";
public void Setup()
{
#if UNITY_EDITOR
var rootGO = new GameObject("rootGo");
GameObject eventSystemGO = new GameObject("EventSystem", typeof(EventSystem));
eventSystemGO.transform.SetParent(rootGO.transform);
var canvasGO = new GameObject("Canvas", typeof(Canvas));
canvasGO.transform.SetParent(rootGO.transform);
var canvas = canvasGO.GetComponent<Canvas>();
canvas.referencePixelsPerUnit = 100;
GameObject scrollRectGO = new GameObject("ScrollRect", typeof(RectTransform), typeof(ScrollRect));
scrollRectGO.transform.SetParent(canvasGO.transform);
GameObject contentGO = new GameObject("Content", typeof(RectTransform));
contentGO.transform.SetParent(scrollRectGO.transform);
GameObject horizontalScrollBarGO = new GameObject("HorizontalScrollBar", typeof(Scrollbar));
horizontalScrollBarGO.transform.SetParent(scrollRectGO.transform);
GameObject verticalScrollBarGO = new GameObject("VerticalScrollBar", typeof(Scrollbar));
verticalScrollBarGO.transform.SetParent(scrollRectGO.transform);
ScrollRect scrollRect = scrollRectGO.GetComponent<ScrollRect>();
scrollRect.transform.position = Vector3.zero;
scrollRect.transform.rotation = Quaternion.identity;
scrollRect.transform.localScale = Vector3.one;
(scrollRect.transform as RectTransform).sizeDelta = new Vector3(0.5f, 0.5f);
scrollRect.horizontalScrollbar = horizontalScrollBarGO.GetComponent<Scrollbar>();
scrollRect.verticalScrollbar = verticalScrollBarGO.GetComponent<Scrollbar>();
scrollRect.content = contentGO.GetComponent<RectTransform>();
scrollRect.content.anchoredPosition = Vector2.zero;
scrollRect.content.sizeDelta = new Vector2(3, 3);
scrollRect.scrollSensitivity = ScrollSensitivity;
scrollRect.GetComponent<RectTransform>().sizeDelta = new Vector2(1, 1);
if (!Directory.Exists("Assets/Resources/"))
Directory.CreateDirectory("Assets/Resources/");
PrefabUtility.SaveAsPrefabAsset(rootGO, kPrefabPath);
GameObject.DestroyImmediate(rootGO);
#endif
}
[SetUp]
public void TestSetup()
{
m_PrefabRoot = UnityEngine.Object.Instantiate(Resources.Load("ScrollRectPrefab")) as GameObject;
}
[TearDown]
public void TearDown()
{
EventSystem.current = null;
GameObject.DestroyImmediate(m_PrefabRoot);
}
[OneTimeTearDown]
public void OneTimeTearDown()
{
#if UNITY_EDITOR
AssetDatabase.DeleteAsset(kPrefabPath);
#endif
}
#region Enable disable scrollbars
[UnityTest]
[TestCase(true, ExpectedResult = null)]
[TestCase(false, ExpectedResult = null)]
public IEnumerator OnEnableShouldAddListeners(bool isHorizontal)
{
ScrollRect scrollRect = m_PrefabRoot.GetComponentInChildren<ScrollRect>();
Scrollbar scrollbar = isHorizontal ? scrollRect.horizontalScrollbar : scrollRect.verticalScrollbar;
scrollRect.enabled = false;
yield return null;
FieldInfo field = scrollbar.onValueChanged.GetType().BaseType.BaseType.GetField("m_Calls", BindingFlags.NonPublic | BindingFlags.Instance);
object invokeableCallList = field.GetValue(scrollbar.onValueChanged);
PropertyInfo property = invokeableCallList.GetType().GetProperty("Count", BindingFlags.Public | BindingFlags.Instance);
int callCount = (int)property.GetValue(invokeableCallList, null);
scrollRect.enabled = true;
yield return null;
Assert.AreEqual(callCount + 1, (int)property.GetValue(invokeableCallList, null));
}
[UnityTest]
[TestCase(true, ExpectedResult = null)]
[TestCase(false, ExpectedResult = null)]
public IEnumerator OnDisableShouldRemoveListeners(bool isHorizontal)
{
ScrollRect scrollRect = m_PrefabRoot.GetComponentInChildren<ScrollRect>();
Scrollbar scrollbar = isHorizontal ? scrollRect.horizontalScrollbar : scrollRect.verticalScrollbar;
scrollRect.enabled = true;
yield return null;
FieldInfo field = scrollbar.onValueChanged.GetType().BaseType.BaseType.GetField("m_Calls", BindingFlags.NonPublic | BindingFlags.Instance);
object invokeableCallList = field.GetValue(scrollbar.onValueChanged);
PropertyInfo property = invokeableCallList.GetType().GetProperty("Count", BindingFlags.Public | BindingFlags.Instance);
Assert.AreNotEqual(0, (int)property.GetValue(invokeableCallList, null));
scrollRect.enabled = false;
yield return null;
Assert.AreEqual(0, (int)property.GetValue(invokeableCallList, null));
}
[Test]
[TestCase(true)]
[TestCase(false)]
public void SettingScrollbarShouldRemoveThenAddListeners(bool testHorizontal)
{
ScrollRect scrollRect = m_PrefabRoot.GetComponentInChildren<ScrollRect>();
Scrollbar scrollbar = testHorizontal ? scrollRect.horizontalScrollbar : scrollRect.verticalScrollbar;
GameObject scrollBarGO = new GameObject("scrollBar", typeof(RectTransform), typeof(Scrollbar));
scrollBarGO.transform.SetParent(scrollRect.transform);
Scrollbar newScrollbar = scrollBarGO.GetComponent<Scrollbar>();
FieldInfo field = newScrollbar.onValueChanged.GetType().BaseType.BaseType.GetField("m_Calls", BindingFlags.NonPublic | BindingFlags.Instance);
PropertyInfo property = field.GetValue(newScrollbar.onValueChanged).GetType().GetProperty("Count", BindingFlags.Public | BindingFlags.Instance);
int newCallCount = (int)property.GetValue(field.GetValue(newScrollbar.onValueChanged), null);
if (testHorizontal)
scrollRect.horizontalScrollbar = newScrollbar;
else
scrollRect.verticalScrollbar = newScrollbar;
Assert.AreEqual(0, (int)property.GetValue(field.GetValue(scrollbar.onValueChanged), null), "The previous scrollbar should not have listeners anymore");
Assert.AreEqual(newCallCount + 1, (int)property.GetValue(field.GetValue(newScrollbar.onValueChanged), null), "The new scrollbar should have listeners now");
}
#endregion
#region Drag
[Test]
[TestCase(PointerEventData.InputButton.Left, true)]
[TestCase(PointerEventData.InputButton.Right, false)]
[TestCase(PointerEventData.InputButton.Middle, false)]
public void PotentialDragNeedsLeftClick(PointerEventData.InputButton button, bool expectedEqualsZero)
{
ScrollRect scrollRect = m_PrefabRoot.GetComponentInChildren<ScrollRect>();
scrollRect.velocity = Vector2.one;
Assert.AreNotEqual(Vector2.zero, scrollRect.velocity);
scrollRect.OnInitializePotentialDrag(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = button });
if (expectedEqualsZero)
Assert.AreEqual(Vector2.zero, scrollRect.velocity);
else
Assert.AreNotEqual(Vector2.zero, scrollRect.velocity);
}
[Test]
[TestCase(PointerEventData.InputButton.Left, true, true)]
[TestCase(PointerEventData.InputButton.Left, false, false)]
[TestCase(PointerEventData.InputButton.Right, true, false)]
[TestCase(PointerEventData.InputButton.Middle, true, false)]
public void LeftClickShouldStartDrag(PointerEventData.InputButton button, bool active, bool expectedIsDragging)
{
ScrollRect scrollRect = m_PrefabRoot.GetComponentInChildren<ScrollRect>();
scrollRect.enabled = active;
scrollRect.velocity = Vector2.one;
Assert.AreNotEqual(Vector2.zero, scrollRect.velocity);
var pointerEventData = new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = button };
scrollRect.OnInitializePotentialDrag(pointerEventData);
FieldInfo field = typeof(ScrollRect).GetField("m_Dragging", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.IsFalse((bool)field.GetValue(scrollRect));
scrollRect.OnBeginDrag(pointerEventData);
Assert.AreEqual(expectedIsDragging, (bool)field.GetValue(scrollRect));
}
[Test]
[TestCase(PointerEventData.InputButton.Left, true, false)]
[TestCase(PointerEventData.InputButton.Left, false, false)]
[TestCase(PointerEventData.InputButton.Right, false, false)]
[TestCase(PointerEventData.InputButton.Right, true, true)]
[TestCase(PointerEventData.InputButton.Middle, true, true)]
[TestCase(PointerEventData.InputButton.Middle, false, false)]
public void LeftClickUpShouldEndDrag(PointerEventData.InputButton button, bool active, bool expectedIsDragging)
{
ScrollRect scrollRect = m_PrefabRoot.GetComponentInChildren<ScrollRect>();
scrollRect.velocity = Vector2.one;
Assert.AreNotEqual(Vector2.zero, scrollRect.velocity);
var startDragEventData = new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = PointerEventData.InputButton.Left };
scrollRect.OnInitializePotentialDrag(startDragEventData);
FieldInfo field = typeof(ScrollRect).GetField("m_Dragging", BindingFlags.NonPublic | BindingFlags.Instance);
Assert.IsFalse((bool)field.GetValue(scrollRect));
scrollRect.OnBeginDrag(startDragEventData);
Assert.IsTrue((bool)field.GetValue(scrollRect), "Prerequisite: dragging should be true to test if it is set to false later");
scrollRect.enabled = active;
var endDragEventData = new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>()) { button = button };
scrollRect.OnEndDrag(endDragEventData);
Assert.AreEqual(expectedIsDragging, (bool)field.GetValue(scrollRect));
}
#endregion
#region LateUpdate
[UnityTest]
public IEnumerator LateUpdateWithoutInertiaOrElasticShouldZeroVelocity()
{
ScrollRect scrollRect = m_PrefabRoot.GetComponentInChildren<ScrollRect>();
scrollRect.velocity = Vector2.one;
scrollRect.inertia = false;
scrollRect.movementType = ScrollRect.MovementType.Clamped;
yield return null;
Assert.AreEqual(Vector2.zero, scrollRect.velocity);
}
[UnityTest]
public IEnumerator LateUpdateWithInertiaShouldDecelerate()
{
ScrollRect scrollRect = m_PrefabRoot.GetComponentInChildren<ScrollRect>();
scrollRect.velocity = Vector2.one;
scrollRect.inertia = true;
scrollRect.movementType = ScrollRect.MovementType.Clamped;
yield return null;
Assert.Less(scrollRect.velocity.magnitude, 1);
}
[UnityTest][Ignore("Fails")]
public IEnumerator LateUpdateWithElasticShouldDecelerate()
{
ScrollRect scrollRect = m_PrefabRoot.GetComponentInChildren<ScrollRect>();
scrollRect.velocity = Vector2.one;
scrollRect.inertia = false;
scrollRect.content.anchoredPosition = Vector2.one * 2;
scrollRect.movementType = ScrollRect.MovementType.Elastic;
yield return null;
Assert.AreNotEqual(1, scrollRect.velocity.magnitude);
var newMagnitude = scrollRect.velocity.magnitude;
yield return null;
Assert.AreNotEqual(newMagnitude, scrollRect.velocity.magnitude);
}
[UnityTest]
public IEnumerator LateUpdateWithElasticNoOffsetShouldZeroVelocity()
{
ScrollRect scrollRect = m_PrefabRoot.GetComponentInChildren<ScrollRect>();
scrollRect.velocity = Vector2.one;
scrollRect.inertia = false;
scrollRect.movementType = ScrollRect.MovementType.Elastic;
yield return null;
Assert.AreEqual(Vector2.zero, scrollRect.velocity);
}
#endregion
[Test]
public void SetNormalizedPositionShouldSetContentLocalPosition()
{
ScrollRect scrollRect = m_PrefabRoot.GetComponentInChildren<ScrollRect>();
scrollRect.normalizedPosition = Vector2.one;
Assert.AreEqual(new Vector3(-1f, -1f, 0), scrollRect.content.localPosition);
}
#region Scroll, offset, ...
[Test]
[TestCase(1, 1, true, true, 1, -1, TestName = "Horizontal and vertical scroll")]
[TestCase(1, 1, false, true, 0, -1, TestName = "Vertical scroll")]
[TestCase(1, 1, true, false, 1, 0, TestName = "Horizontal scroll")]
public void OnScrollClampedShouldMoveContentAnchoredPosition(int scrollDeltaX, int scrollDeltaY, bool horizontal,
bool vertical, int expectedPosX, int expectedPosY)
{
ScrollRect scrollRect = m_PrefabRoot.GetComponentInChildren<ScrollRect>();
Vector2 scrollDelta = new Vector2(scrollDeltaX, scrollDeltaY);
var expected = new Vector2(expectedPosX, expectedPosY) * ScrollSensitivity;
scrollRect.horizontal = horizontal;
scrollRect.vertical = vertical;
scrollRect.OnScroll(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>())
{
scrollDelta = scrollDelta
});
Assert.AreEqual(expected, scrollRect.content.anchoredPosition);
}
[Test][Ignore("Tests fail without mocking")]
[TestCase(ScrollRect.MovementType.Clamped, 1f, 1f)]
[TestCase(ScrollRect.MovementType.Unrestricted, 150, 150)]
[TestCase(ScrollRect.MovementType.Elastic, 150, 150)]
public void OnScrollClampedShouldClampContentAnchoredPosition(ScrollRect.MovementType movementType, float anchoredPosX,
float anchoredPosY)
{
ScrollRect scrollRect = m_PrefabRoot.GetComponentInChildren<ScrollRect>();
Vector2 scrollDelta = new Vector2(50, -50);
scrollRect.movementType = movementType;
scrollRect.content.anchoredPosition = new Vector2(2.5f, 2.5f);
scrollRect.OnScroll(new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>())
{
scrollDelta = scrollDelta
});
Assert.AreEqual(new Vector2(anchoredPosX, anchoredPosY), scrollRect.content.anchoredPosition);
}
[Test]
public void GetBoundsShouldEncapsulateAllCorners()
{
Matrix4x4 matrix = Matrix4x4.identity;
object[] arguments = new object[2]
{
new Vector3[] { Vector3.zero, Vector3.one, Vector3.one * 2, Vector3.one },
matrix
};
MethodInfo method = typeof(ScrollRect).GetMethod("InternalGetBounds", BindingFlags.NonPublic | BindingFlags.Static);
var bounds = (Bounds)method.Invoke(null, arguments);
Assert.AreEqual(Vector3.zero, bounds.min);
Assert.AreEqual(Vector3.one * 2, bounds.max);
}
[Test]
public void UpdateBoundsShouldPad()
{
Bounds viewBounds = new Bounds(Vector3.zero, Vector3.one * 2);
Vector3 contentSize = Vector3.one;
Vector3 contentPos = Vector3.one;
var contentPivot = new Vector2(0.5f, 0.5f);
object[] arguments = new object[] { viewBounds, contentPivot, contentSize, contentPos };
MethodInfo method = typeof(ScrollRect).GetMethod("AdjustBounds", BindingFlags.NonPublic | BindingFlags.Static);
method.Invoke(null, arguments);
//ScrollRect.AdjustBounds(ref viewBounds, ref contentPivot, ref contentSize, ref contentPos);
Assert.AreEqual(new Vector3(2, 2, 1), arguments[2]);
}
[Test]
[TestCase(true, true, 2, 4, -2, -2, TestName = "Should clamp offset")]
[TestCase(false, true, 2, 4, 0, -2, TestName = "Vertical should clamp offset on one axis")]
[TestCase(true, false, 2, 4, -2, 0, TestName = "Horizontal should clamp offset on one axis")]
[TestCase(false, false, 2, 4, 0, 0, TestName = "No axis should not clamp offset")]
[TestCase(true, true, 8, 10, 2, 2, TestName = "Should clamp negative offset")]
[TestCase(false, true, 8, 10, 0, 2, TestName = "Vertical should clamp negative offset on one axis")]
[TestCase(true, false, 8, 10, 2, 0, TestName = "Horizontal should clamp negative offset on one axis")]
[TestCase(false, false, 8, 10, 0, 0, TestName = "No axis should not clamp negative offset")]
public void CalculateOffsetShouldClamp(bool horizontal, bool vertical, int viewX, float viewY, float resX, float resY)
{
TestCalculateOffset(ScrollRect.MovementType.Clamped, horizontal, vertical, viewX, viewY, resX, resY, new Bounds(new Vector3(5, 7), new Vector3(4, 4)));
}
[Test]
[TestCase(true, true, 2, 4, -2, -2, TestName = "Should clamp offset")]
[TestCase(false, true, 2, 4, 0, -2, TestName = "Vertical should clamp offset on one axis")]
[TestCase(true, false, 2, 4, -2, 0, TestName = "Horizontal should clamp offset on one axis")]
[TestCase(false, false, 2, 4, 0, 0, TestName = "No axis should not clamp offset")]
[TestCase(true, true, 8, 10, 2, 2, TestName = "Should clamp negative offset")]
[TestCase(false, true, 8, 10, 0, 2, TestName = "Vertical should clamp negative offset on one axis")]
[TestCase(true, false, 8, 10, 2, 0, TestName = "Horizontal should clamp negative offset on one axis")]
[TestCase(false, false, 8, 10, 0, 0, TestName = "No axis should not clamp negative offset")]
public void CalculateOffsetUnrestrictedShouldNotClamp(bool horizontal, bool vertical, int viewX, float viewY, float resX, float resY)
{
TestCalculateOffset(ScrollRect.MovementType.Unrestricted, horizontal, vertical, viewX, viewY, 0, 0, new Bounds(new Vector3(5, 7), new Vector3(4, 4)));
}
private static void TestCalculateOffset(ScrollRect.MovementType movementType, bool horizontal, bool vertical, int viewX, float viewY, float resX, float resY, Bounds contentBounds)
{
Bounds viewBounds = new Bounds(new Vector3(viewX, viewY), new Vector3(2, 2));
// content is south east of view
Vector2 delta = Vector2.zero;
object[] arguments = new object[] { viewBounds, contentBounds, horizontal, vertical, movementType, delta };
MethodInfo method = typeof(ScrollRect).GetMethod("InternalCalculateOffset", BindingFlags.NonPublic | BindingFlags.Static);
var result = (Vector2)method.Invoke(null, arguments);
Console.WriteLine(result);
Assert.AreEqual(new Vector2(resX, resY), result);
}
#endregion
}