Tanoda
GraphicRendererTestBase.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
10using System;
11using NUnit.Framework;
12using System.Reflection;
13using UnityEngine;
14using UnityEngine.SceneManagement;
15using Leap.Unity.Query;
16
18
19 public abstract class GraphicRendererTestBase : LeapTestBase {
20
21 protected LeapGraphicRenderer renderer { get; private set; }
22
23 protected LeapGraphic oneGraphic { get; private set; }
24
25 protected LeapGraphicGroup firstGroup { get; private set; }
26
27 protected LeapGraphicGroup secondGroup { get; private set; }
28
29 private string _sceneToUnload;
30
31 [TearDown]
32 protected virtual void Teardown() {
33 UnityEngine.Object.DestroyImmediate(renderer.gameObject);
34
35 if (!string.IsNullOrEmpty(_sceneToUnload)) {
36 SceneManager.UnloadSceneAsync(_sceneToUnload);
37 _sceneToUnload = null;
38 }
39 }
40
53 protected override void InitTest(string objectName) {
54 base.InitTest(objectName);
55
56 renderer = testObj.GetComponent<LeapGraphicRenderer>();
57
58 oneGraphic = renderer.GetComponentInChildren<LeapGraphic>(includeInactive: true);
59
60 firstGroup = renderer.groups[0];
61
62 secondGroup = renderer.groups.Count > 1 ? renderer.groups[1] : null;
63 }
64
70 protected void LoadScene(string sceneName) {
71 _sceneToUnload = sceneName;
72 SceneManager.LoadScene(sceneName, LoadSceneMode.Additive);
73 }
74
80 protected LeapGraphic CreateGraphic(string prefabName) {
81 var prefab = EditorResources.Load<GameObject>(prefabName);
82 var obj = UnityEngine.Object.Instantiate(prefab);
83 obj.transform.SetParent(renderer.transform);
84
85 var graphic = obj.GetComponent<LeapGraphic>();
86
87 if (oneGraphic == null) {
88 oneGraphic = graphic;
89 }
90
91 return graphic;
92 }
93
98 protected object shallowCopy(object obj) {
99 Assert.That(obj, Is.Not.Null);
100
101 var copy = Activator.CreateInstance(obj.GetType());
102
103 foreach (var field in copy.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) {
104 field.SetValue(copy, field.GetValue(obj));
105 }
106
107 return copy;
108 }
109
114 protected void assertValueFieldsEqual(object a, object b) {
115 Assert.That(a, Is.Not.Null);
116 Assert.That(b, Is.Not.Null);
117
118 Assert.That(a.GetType(), Is.EqualTo(b.GetType()));
119
120 foreach (var field in a.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) {
121 if (!field.FieldType.IsValueType) {
122 continue;
123 }
124
125 var valueA = field.GetValue(a);
126 var valueB = field.GetValue(b);
127 Assert.That(valueA, Is.EqualTo(valueB), "Field " + field.Name + " did not match");
128 }
129 }
130 }
131}
132#endif