Tanoda
LeapTestBase.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9#if LEAP_TESTS
10
11using Leap.Unity.Query;
12using System;
13using System.Collections;
14using UnityEngine;
15using UnityEngine.SceneManagement;
16
17namespace Leap.Unity {
18
19 public abstract class LeapTestBase {
20
21 protected GameObject testObj;
22
32 protected virtual void InitTest(string objectName) {
33 testObj = LoadObject(objectName);
34 }
35
36 #region Spawn Utilities
37
38 protected T Spawn<T>(T original, Vector3 position) where T : MonoBehaviour {
39 return GameObject.Instantiate<T>(original,
40 position,
41 original.transform.rotation,
42 original.transform.parent);
43 }
44
45 protected GameObject Spawn(GameObject original, Vector3 position) {
46 return GameObject.Instantiate(original,
47 position,
48 original.transform.rotation,
49 original.transform.parent);
50 }
51
52 protected UnityEngine.Object Spawn(UnityEngine.Object original, Vector3 position) {
53 return UnityEngine.Object.Instantiate(original, position, Quaternion.identity);
54 }
55
61 protected GameObject LoadObject(string objectName) {
62 GameObject obj = null;
63
64 for (int i = 0; i < SceneManager.sceneCount; i++) {
65 var scene = SceneManager.GetSceneAt(i);
66
67 obj = scene.GetRootGameObjects()
68 .Query()
69 .FirstOrDefault(g => g.name == objectName);
70
71 if (obj != null) {
72 obj.SetActive(true);
73 break;
74 }
75 }
76
77 if (obj == null) {
78 var prefab = EditorResources.Load<GameObject>(objectName);
79
80 if (prefab == null) {
81 throw new Exception("Could not find an object or prefab with the name "
82 + objectName);
83 }
84
85 obj = UnityEngine.Object.Instantiate(prefab);
86 }
87
88 return obj;
89 }
90
91 #endregion
92
93 #region Frame Utilities
94
95 protected const bool GO_SLOW = false;
96
97 protected int aBit { get { return GO_SLOW ? 50 : 5; } }
98 protected int aWhile { get { return GO_SLOW ? 200 : 20; } }
99 protected int aLot { get { return GO_SLOW ? 1000 : 100; } }
100
101 protected int beginningTestWait { get { return aBit; } }
102 protected int endingTestWait { get { return aWhile; } }
103
104 protected IEnumerator wait(int numFrames) {
105 for (int i = 0; i < numFrames; i++) {
106 yield return null;
107 }
108 }
109
110 #endregion
111
112 }
113
114}
115#endif