LeapTestBase.cs
3.73 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
/******************************************************************************
* Copyright (C) Leap Motion, Inc. 2011-2017. *
* Leap Motion proprietary and confidential. *
* *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
******************************************************************************/
#if LEAP_TESTS
using Leap.Unity.Query;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Leap.Unity {
public abstract class LeapTestBase {
protected GameObject testObj;
/// <summary>
/// Should be called at the start of a test. The argument is the name of a prefab to
/// spawn, or the name of a GameObject in the current scene.
///
/// This method populates the testObj field with the loaded or spawned object.
///
/// Module-specific tests can then set additional fields that may be contained within
/// the testing object by overriding this method.
/// </summary>
protected virtual void InitTest(string objectName) {
testObj = LoadObject(objectName);
}
#region Spawn Utilities
protected T Spawn<T>(T original, Vector3 position) where T : MonoBehaviour {
return GameObject.Instantiate<T>(original,
position,
original.transform.rotation,
original.transform.parent);
}
protected GameObject Spawn(GameObject original, Vector3 position) {
return GameObject.Instantiate(original,
position,
original.transform.rotation,
original.transform.parent);
}
protected UnityEngine.Object Spawn(UnityEngine.Object original, Vector3 position) {
return UnityEngine.Object.Instantiate(original, position, Quaternion.identity);
}
/// <summary>
/// Attempts to load the GameObject by name in one of the currently-loaded scenes,
/// or from an EditorResources folder if there is no GameObject with that name in any
/// loaded scene.
/// </summary>
protected GameObject LoadObject(string objectName) {
GameObject obj = null;
for (int i = 0; i < SceneManager.sceneCount; i++) {
var scene = SceneManager.GetSceneAt(i);
obj = scene.GetRootGameObjects()
.Query()
.FirstOrDefault(g => g.name == objectName);
if (obj != null) {
obj.SetActive(true);
break;
}
}
if (obj == null) {
var prefab = EditorResources.Load<GameObject>(objectName);
if (prefab == null) {
throw new Exception("Could not find an object or prefab with the name "
+ objectName);
}
obj = UnityEngine.Object.Instantiate(prefab);
}
return obj;
}
#endregion
#region Frame Utilities
protected const bool GO_SLOW = false;
protected int aBit { get { return GO_SLOW ? 50 : 5; } }
protected int aWhile { get { return GO_SLOW ? 200 : 20; } }
protected int aLot { get { return GO_SLOW ? 1000 : 100; } }
protected int beginningTestWait { get { return aBit; } }
protected int endingTestWait { get { return aWhile; } }
protected IEnumerator wait(int numFrames) {
for (int i = 0; i < numFrames; i++) {
yield return null;
}
}
#endregion
}
}
#endif